7 min read
Numerics and Linear Algebra
Prerequisites: Calculus
Machine and arbitrary precision
0.1 + 0.2
InputForm[0.1 + 0.2]
0.1 + 0.2 == 0.3
$MachinePrecision
Precision[N[Pi, 30]]
0.3
0.30000000000000004
True
15.9546
30.
- Machine numbers are ordinary 64-bit floats (about 16 digits), so
0.1 + 0.2isn't exactly0.3—InputFormshows every digit. ==on approximate numbers tolerates differences in the last few binary digits, so the comparison isTrue. Other languages would sayFalse.- Numbers created with
N[expr, n]are arbitrary-precision and track their precision through calculations.
N[Exp[Pi Sqrt[163]], 35]
262537412640768743.99999999999925007
With machine precision this famous number looks like an integer; 35 digits reveal that it isn't.
Vectors and matrices
Vectors are lists; matrices are lists of rows.
m = {{1, 2}, {3, 4}};
Det[m]
Inverse[m]
m . {1, 1}
m . m
m * m
Transpose[m]
LinearSolve[m, {5, 11}]
-2
{{-2, 1}, {3/2, -1/2}}
{3, 7}
{{7, 10}, {15, 22}}
{{1, 4}, {9, 16}}
{{1, 3}, {2, 4}}
{1, 2}
.(Dot) is matrix multiplication;*multiplies element by element.LinearSolve[m, b]solvesm . x == b— better than computing the inverse.MatrixForm[m]displays a matrix as a grid (for display only — don't store it in a variable you compute with).
Eigenvalues[{{2, 1}, {1, 2}}]
Eigenvectors[{{2, 1}, {1, 2}}]
IdentityMatrix[2]
{3, 1}
{{1, 1}, {-1, 1}}
{{1, 0}, {0, 1}}
Statistics and fitting
Variance[{1, 2, 3, 4}]
data = {{1, 2.1}, {2, 3.9}, {3, 6.2}, {4, 7.8}};
lm = LinearModelFit[data, x, x];
Normal[lm]
lm["RSquared"]
lm[5]
5/3
0.15 + 1.94 x
0.995661
9.85
VarianceandStandardDeviationuse the sample (n − 1) formula.LinearModelFitreturns a fitted model object:lm["RSquared"],lm["ParameterTable"], andlm[x0]for predictions.NonlinearModelFitandFindFitfit any formula.
Common mistakes
- Mixing exact and approximate numbers in a huge symbolic computation —
exact arithmetic on big expressions can be slow. Use
Nearly when you only need numbers. - Using
*for matrix multiplication.