Skip to content
Linear Algebra

linear-algebra-ai999x

Vectors, matrices, determinants, linear systems, eigenvalues, decompositions, least squares, sparse matrices and numerical methods (root finding, interpolation, quadrature).

89capabilities
13topics
89formulas typeset
8worked examples below
1.0.0version
About this model

What Linear Algebra does

Linear Algebra (linear-algebra-ai999x) is an offline Python package for linear algebra. It covers vectors, matrices, determinants, linear systems, eigenvalues, decompositions, least squares, sparse matrices and numerical methods (root finding, interpolation, quadrature).

It has 89 capabilities — 89 computational procedures — in 13 topics, the largest being Numerical methods, Vectors, Matrices, Linear systems, Eigenvalues and Decompositions. Ask a question in words with ask(), or call a capability by name with typed inputs with solve(). Every answer carries its value and unit, the steps that produced it and the formula in LaTeX.

When a question is not determined — a value is missing, an input is out of range, or the request is outside the package — it says so and names what it needs, instead of guessing. It runs on an ordinary CPU without a network connection (Python >=3.10), and the same question always gets the same answer.

Install

pip install linear-algebra-ai999x
hf download AI999X/linear-algebra-ai999x --local-dir linear-algebra-ai999x

What it covers

Decompositions6Best rank-k approximation (Eckart-Young) · Cholesky decomposition · LU decomposition with partial pivoting …Determinants5Adjugate (classical adjoint) of a matrix · Determinant of a 2 x 2 matrix · Determinant of a 3 x 3 matrix …Eigenvalues8Characteristic polynomial of a matrix · Dominant eigenvalue by power iteration · Eigenvalues and eigenvectors of a symmetric matrix …Least squares5Least-squares polynomial fit · Least-squares regression line · Least-squares solution of an overdetermined system …Linear systems9Conjugate gradient method for Ax = b · Gauss-Seidel iteration for Ax = b · Jacobi iteration for Ax = b …Linear transformations2Matrix times vector (image under a linear map) · Rotation of a plane vectorMatrices12Condition number of a matrix · Difference of two matrices · Inverse of a square matrix …Matrix functions2Matrix exponential · Square root of a positive definite matrixNumerical PDE21-D Poisson equation by finite differences · 2-D Poisson equation on a grid (5-point stencil)Numerical methods16Clamped cubic spline interpolation · Coefficients of the interpolating polynomial · Composite midpoint rule for a polynomial …Sparse matrices4Compressed sparse column (CSC) form of a matrix · Compressed sparse row (CSR) form of a matrix · Dense matrix from CSR arrays …Vector spaces5Column space of a matrix · Coordinates of a vector in a basis · Gram-Schmidt orthonormalization …Vectors13Angle between two vectors · Area of the parallelogram (and triangle) spanned by two vectors · Cross product of two 3-D vectors …
Try it

Worked examples

Each question below was put to linear-algebra-ai999x and the answer shown is the one it returned, with the steps it took.

Determinantsm.ask(…)

Find the determinant of [[2, 0, 1], [1, 3, 2], [1, 1, 2]].

det = 6
Show the steps
  1. Determinant of a 3 x 3 matrix
    \det A = a_{11}a_{22}a_{33} + a_{12}a_{23}a_{31} + a_{13}a_{21}a_{32} - a_{13}a_{22}a_{31} - a_{11}a_{23}a_{32} - a_{12}a_{21}a_{33}
    with A = [2 0 1; 1 3 2; 1 1 2]
    → det = 6
Linear systemsm.ask(…)

Solve the system 2x + 3y = 7, x - y = 1.

solution = [2, 1]
det = -5
Show the steps
  1. Two linear equations in two unknowns
    x = \frac{\begin{vmatrix} e & b\\ f & d\end{vmatrix}}{\begin{vmatrix} a & b\\ c & d\end{vmatrix}},\quad y = \frac{\begin{vmatrix} a & e\\ c & f\end{vmatrix}}{\begin{vmatrix} a & b\\ c & d\end{vmatrix}}
    with A = [2 3; 1 -1], b = [7, 1]
    → solution = [2, 1], det = -5
Eigenvaluesm.ask(…)

Find the eigenvalues of [[2, 1], [1, 2]].

lambda1 = 1
lambda2 = 3
Show the steps
  1. Eigenvalues of a 2 x 2 matrix
    \lambda = \frac{\operatorname{tr}A \pm \sqrt{(\operatorname{tr}A)^2 - 4\det A}}{2}
    with A = [2 1; 1 2]
    → lambda1 = 1, lambda2 = 3
Numerical methodsm.ask(…)

Use Newton's method to find a root of x^3 - 2x - 5 starting from x0 = 2.

root = 2.094551482
Show the steps
  1. Root of a polynomial by Newton's method
    x_{k+1} = x_k - \frac{p(x_k)}{p'(x_k)}
    with coefficients = [1, 0, -2, -5], x0 = 2
    → root = 2.094551482
Decompositionsm.solve(…)
m.solve("qr_decomposition", A="[[3, 1], [4, 2]]")
Q = [[0.6, -0.8], [0.8, 0.6]]
R = [[5, 2.2], [-0, 0.4]]
Show the steps
  1. QR decomposition (Householder)
    A = QR,\ Q^{T}Q = I
    with A = [3 1; 4 2]
    → Q = [[0.6, -0.8], [0.8, 0.6]], R = [[5, 2.2], [-0, 0.4]]
Least squaresm.ask(…)

Find the least squares line for the points (1, 2), (2, 3), (3, 5), (4, 4).

slope = 0.8
intercept = 1.5
r_squared = 0.64
Show the steps
  1. Least-squares regression line
    b = \frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sum (x_i-\bar x)^2},\quad a = \bar y - b\bar x
    with x = [1, 2, 3, 4], y = [2, 3, 5, 4]
    → slope = 0.8, intercept = 1.5, r_squared = 0.64
Linear transformationsm.ask(…)

Multiply the matrix [[1, 2], [3, 4]] by the vector (5, 6).

image = [17, 39]
Show the steps
  1. Matrix times vector (image under a linear map)
    (A\mathbf{x})_i = \sum_j a_{ij} x_j
    with A = [1 2; 3 4], x = [5, 6]
    → image = [17, 39]
Matricesm.ask(…)

What is the condition number of the matrix [2 0; 0 0.5]?

cond2 = 4
cond1 = 4
Show the steps
  1. Condition number of a matrix
    \kappa_2(A) = \frac{\sigma_{\max}}{\sigma_{\min}},\quad \kappa_1(A) = \|A\|_1\|A^{-1}\|_1
    with A = [2 0; 0 0.5]
    → cond2 = 4, cond1 = 4
Use it

From Python or the command line

Every snippet was run as shown; the output under it is what it printed.

import linear_algebra_ai999x as m

a = m.ask("Find the determinant of [[2, 0, 1], [1, 3, 2], [1, 1, 2]].")
print(a.outcome)
print(a.values)
for step in a.steps:
    print(step["name"])
Output
SOLVED
{'det': 6.0}
Determinant of a 3 x 3 matrix
Figures

Computed, not drawn by hand

Every value in these figures was computed by the package, each point a run of solve(); in a notebook, plot() and plot_relation() draw a capability's curves the same way.

A linear map of the plane and its eigenvectors

A\mathbf{q}_i = \lambda_i \mathbf{q}_i,\ Q^{T}Q = I
A linear map of the plane and its eigenvectorsThe unit square and its image under A = [[2, 1], [1, 2]] (each corner mapped by the package's matrix–vector product), with the eigenvectors it returned and their images: λ1 = 1 along (0.707107, -0.707107); λ2 = 3 along (0.707107, 0.707107).024-10123unit squareits image under Aeigenvector vA v = λ vx₁x₂
The unit square and its image under A = [[2, 1], [1, 2]] (each corner mapped by the package's matrix–vector product), with the eigenvectors it returned and their images: λ1 = 1 along (0.707107, -0.707107); λ2 = 3 along (0.707107, 0.707107).

The unit circle under a matrix: singular values as semi-axes

A = U\Sigma V^{T},\quad \sigma_i = \sqrt{\lambda_i(A^{T}A)}
The unit circle under a matrix: singular values as semi-axes72 points of the unit circle mapped by A = [[3, 0], [4, 5]]: an ellipse whose semi-axes are the singular values σ₁ = 6.7082 and σ₂ = 2.23607 returned by the package (the longest and shortest mapped radii drawn: 6.7082 and 2.23607).-10010-505unit circleimage under Aσ₁ = 6.7082σ₂ = 2.23607x₁x₂
72 points of the unit circle mapped by A = [[3, 0], [4, 5]]: an ellipse whose semi-axes are the singular values σ₁ = 6.7082 and σ₂ = 2.23607 returned by the package (the longest and shortest mapped radii drawn: 6.7082 and 2.23607).

LU factorisation as matrices

A = PLU
LU factorisation as matricesThe LU factorisation with partial pivoting returned by the package for A = [[2, 1, 1], [4, -6, 0], [-2, 7, 2]]: A = P L U with P a permutation, L unit lower triangular and U upper triangular (cells shaded by magnitude; negative values in the second colour). The product P L U, also computed by the package, equals A.2114-60-272A010100001P1000.510-0.511L4-60041001U
The LU factorisation with partial pivoting returned by the package for A = [[2, 1, 1], [4, -6, 0], [-2, 7, 2]]: A = P L U with P a permutation, L unit lower triangular and U upper triangular (cells shaded by magnitude; negative values in the second colour). The product P L U, also computed by the package, equals A.

Least-squares polynomial and line through the same points

\min_{\mathbf{c}} \sum_i \Big(y_i - \sum_{k=0}^{d} c_k x_i^k\Big)^2
Least-squares polynomial and line through the same pointsLeast-squares fits to the points x = 0, 1, 2, 3, y = 1, 0, 3, 10: the degree-2 polynomial y = 1 − 3x + 2x² (coefficients from the package, the curve evaluated by it at 61 points) and the straight line y = −1 + 3x with R² = 0.737705.01230510line, R² = 0.737705degree-2 fitdataxy
Least-squares fits to the points x = 0, 1, 2, 3, y = 1, 0, 3, 10: the degree-2 polynomial y = 1 − 3x + 2x² (coefficients from the package, the curve evaluated by it at 61 points) and the straight line y = −1 + 3x with R² = 0.737705.

Interpolation through five nodes: spline, polynomial, linear

h_{i-1}M_{i-1} + 2(h_{i-1}+h_i)M_i + h_iM_{i+1} = 6\Big(\frac{y_{i+1}-y_i}{h_i} - \frac{y_i-y_{i-1}}{h_{i-1}}\Big),\ M_0 = M_n = 0
Interpolation through five nodes: spline, polynomial, linearThree interpolants through the nodes x = 0, 1, 2, 3, 4, y = 0, 2, 1, 3, 2, each evaluated by the package at 81 points: the natural cubic spline, the degree-4 Lagrange polynomial and piecewise-linear interpolation; marked: x = 2.5, spline value = 1.91964.01234012341.91964piecewise linearLagrange polynomialnatural cubic splinenodesxy
Three interpolants through the nodes x = 0, 1, 2, 3, 4, y = 0, 2, 1, 3, 2, each evaluated by the package at 81 points: the natural cubic spline, the degree-4 Lagrange polynomial and piecewise-linear interpolation; marked: x = 2.5, spline value = 1.91964.
Formulas

The mathematics inside

12 of its 89 formulas; every one is in the capability browser below.

Determinant of a 3 x 3 matrix
\det A = a_{11}a_{22}a_{33} + a_{12}a_{23}a_{31} + a_{13}a_{21}a_{32} - a_{13}a_{22}a_{31} - a_{11}a_{23}a_{32} - a_{12}a_{21}a_{33}
Two linear equations in two unknowns
x = \frac{\begin{vmatrix} e & b\\ f & d\end{vmatrix}}{\begin{vmatrix} a & b\\ c & d\end{vmatrix}},\quad y = \frac{\begin{vmatrix} a & e\\ c & f\end{vmatrix}}{\begin{vmatrix} a & b\\ c & d\end{vmatrix}}
Eigenvalues of a 2 x 2 matrix
\lambda = \frac{\operatorname{tr}A \pm \sqrt{(\operatorname{tr}A)^2 - 4\det A}}{2}
Root of a polynomial by Newton's method
x_{k+1} = x_k - \frac{p(x_k)}{p'(x_k)}
Least-squares regression line
b = \frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sum (x_i-\bar x)^2},\quad a = \bar y - b\bar x
Matrix times vector (image under a linear map)
(A\mathbf{x})_i = \sum_j a_{ij} x_j
Condition number of a matrix
\kappa_2(A) = \frac{\sigma_{\max}}{\sigma_{\min}},\quad \kappa_1(A) = \|A\|_1\|A^{-1}\|_1
Best rank-k approximation (Eckart-Young)
A_k = \sum_{i=1}^{k} \sigma_i \mathbf{u}_i \mathbf{v}_i^{T}
Adjugate (classical adjoint) of a matrix
\operatorname{adj}(A)_{ij} = C_{ji}
Characteristic polynomial of a matrix
p(\lambda) = \det(\lambda I - A)
Least-squares polynomial fit
\min_{\mathbf{c}} \sum_i \Big(y_i - \sum_{k=0}^{d} c_k x_i^k\Big)^2
Capabilities

Every capability, with an example

Search by name, formula, input or topic. Each capability opens to show what it takes, what it returns, when it applies, and a question it answered.

89 capabilities

Decompositions 6

Best rank-k approximation (Eckart-Young)low_rank_approximation

The rank-k matrix closest to A in the 2- and Frobenius norms (truncated SVD), with both approximation errors.

A_k = \sum_{i=1}^{k} \sigma_i \mathbf{u}_i \mathbf{v}_i^{T}

Inputs

  • A the matrix matrix
  • k target rank k integer

Returns

  • approximation the best rank-k approximation A_k
  • error2 |A - A_k|_2 = sigma_(k+1)
  • errorF |A - A_k|_F = sqrt(sigma_(k+1)^2 + ...)

Applies when

  • 1 <= k <= min(rows, columns)
  • sigma_k > sigma_(k+1) (otherwise A_k is not unique)
  • at most 10 x 10

Example

m.ask("Find the best rank-1 approximation of [[3, 0], [0, 1]].")
→ approximation = [[3.0, 0.0], [0.0, 0.0]], error2 = 1, errorF = 1
Cholesky decompositioncholesky

Factor a symmetric positive definite matrix as A = L L^T with L lower triangular, positive diagonal.

A = LL^{T}

Inputs

  • A a symmetric positive definite matrix matrix

Returns

  • L lower triangular factor with positive diagonal

Applies when

  • A is symmetric
  • A is positive definite (checked exactly by its leading minors)
  • at most 10 x 10

Example

m.ask("Compute the Cholesky factor of the matrix [4 12 -16; 12 37 -43; -16 -43 98].")
→ L = [[2.0, 0.0, 0.0], [6.0, 1.0, 0.0], [-8.0, 5.0, 3.0]]
LU decomposition with partial pivotinglu_decomposition

Factor a nonsingular square matrix as A = P L U (permutation, unit lower triangular, upper triangular).

A = PLU

Inputs

  • A a nonsingular square matrix matrix

Returns

  • P permutation matrix (A = P L U)
  • L unit lower triangular factor, |l_ij| <= 1
  • U upper triangular factor

Applies when

  • A is square and nonsingular
  • at most 10 x 10
  • pivot: the first row of largest magnitude in the column (the scipy.linalg.lu convention A = P L U)

Example

m.ask("Find the LU decomposition of [[1, 2], [3, 4]].")
→ P = [[0.0, 1.0], [1.0, 0.0]], L = [[1.0, 0.0], [0.3333333333333333, 1.0]], U = [[3.0, 4.0], [0.0, 0.6666666666666667]]
Polar decompositionpolar_decomposition

Write a nonsingular square matrix as A = U P: U orthogonal (a rotation or reflection), P symmetric positive definite (a stretch).

A = UP,\quad P = (A^{T}A)^{1/2},\ U = AP^{-1}

Inputs

  • A a nonsingular square matrix matrix

Returns

  • U orthogonal factor
  • P symmetric positive definite factor (A^T A)^(1/2)

Applies when

  • A is square and nonsingular
  • at most 10 x 10
  • right polar form A = U P (scipy.linalg.polar's default)

Example

m.ask("Compute the polar decomposition of the matrix [0 -2; 1 0].")
→ U = [[0.0, -1.0], [1.0, 0.0]], P = [[1.0, 0.0], [0.0, 2.0]]
QR decomposition (Householder)qr_decomposition

Factor A = Q R with orthonormal columns Q and upper triangular R (reduced form, positive diagonal of R).

A = QR,\ Q^{T}Q = I

Inputs

  • A a matrix with at least as many rows as columns, independent columns matrix

Returns

  • Q m x n matrix with orthonormal columns
  • R n x n upper triangular, positive diagonal

Applies when

  • rows >= columns
  • independent columns
  • at most 10 x 10
  • convention: the diagonal of R is positive (unique)

Example

m.ask("Find the QR decomposition of [[3, 1], [4, 2]].")
→ Q = [[0.5999999999999999, -0.7999999999999999], [0.7999999999999999, 0.6000000000000001]], R = [[5.0, 2.1999999999999997], [-0.0, 0.40000000000000013]]
Singular values of a matrixsingular_values

The singular values of a matrix (any shape up to 10 x 10), largest first.

A = U\Sigma V^{T},\quad \sigma_i = \sqrt{\lambda_i(A^{T}A)}

Inputs

  • A the matrix matrix

Returns

  • singular_values singular values, descending

Applies when

  • at most 10 x 10

Example

m.ask("Find the singular values of [[3, 0], [4, 5]].")
→ singular_values = [6.708203932499369, 2.2360679774997894]

Determinants 5

Adjugate (classical adjoint) of a matrixadjugate

adj(A): the transpose of the matrix of cofactors; A adj(A) = det(A) I.

\operatorname{adj}(A)_{ij} = C_{ji}

Inputs

  • A a square matrix (up to 7 x 7) matrix

Returns

  • adjugate the adjugate matrix adj(A)

Applies when

  • A is square
  • at most 7 x 7

Example

m.ask("Find the adjugate of [[1, 2], [3, 4]].")
→ adjugate = [[4.0, -2.0], [-3.0, 1.0]]
Determinant of a 2 x 2 matrixdeterminant_2x2

det [[a, b], [c, d]] = ad - bc.

\det\begin{pmatrix}a&b\\c&d\end{pmatrix} = ad - bc

Inputs

  • A a 2 x 2 matrix matrix

Returns

  • det the determinant ad - bc

Applies when

  • A is 2 x 2

Example

m.ask("Find the determinant of [[1, 2], [3, 4]].")
→ det = -2
Determinant of a 3 x 3 matrixdeterminant_3x3

The determinant of a 3 x 3 matrix by the rule of Sarrus.

\det A = a_{11}a_{22}a_{33} + a_{12}a_{23}a_{31} + a_{13}a_{21}a_{32} - a_{13}a_{22}a_{31} - a_{11}a_{23}a_{32} - a_{12}a_{21}a_{33}

Inputs

  • A a 3 x 3 matrix matrix

Returns

  • det the determinant

Applies when

  • A is 3 x 3

Example

m.ask("Find the determinant of [[2, 0, 1], [1, 3, 2], [1, 1, 2]].")
→ det = 6
Determinant of an n x n matrixdeterminant

The determinant of a square matrix of any size up to 10 x 10, by row reduction.

\det A = (-1)^{s}\prod_i u_{ii}

Inputs

  • A a square matrix (up to 10 x 10) matrix

Returns

  • det the determinant

Applies when

  • A is square
  • at most 10 x 10

Example

m.ask("Find the determinant of [[1, 2, 0, 0], [3, 4, 0, 0], [0, 0, 5, 6], [0, 0, 7, 8]].")
→ det = 4
Minor and cofactor of an entryminor_cofactor

The minor M_ij (determinant with row i and column j deleted) and the cofactor C_ij = (-1)^(i+j) M_ij.

C_{ij} = (-1)^{i+j} M_{ij}

Inputs

  • A a square matrix matrix
  • i row of the entry (1-based) integer
  • j column of the entry (1-based) integer

Returns

  • minor the minor M_ij
  • cofactor the cofactor C_ij

Applies when

  • A is square, at least 2 x 2
  • 1 <= i, j <= n

Example

m.ask("Find the cofactor C23 of [[1, 2, 3], [4, 5, 6], [7, 8, 10]].")
→ minor = -6, cofactor = 6

Eigenvalues 8

Characteristic polynomial of a matrixcharacteristic_polynomial

The coefficients of det(lambda I - A) = lambda^n + c1 lambda^(n-1) + ... + cn, highest power first.

p(\lambda) = \det(\lambda I - A)

Inputs

  • A a square matrix (up to 10 x 10) matrix

Returns

  • coefficients [1, c1, ..., cn] of det(lambda I - A), highest power first

Applies when

  • A is square
  • at most 10 x 10

Example

m.ask("Find the characteristic polynomial of [[2, 1], [1, 2]].")
→ coefficients = [1.0, -4.0, 3.0]
Dominant eigenvalue by power iterationpower_iteration

The eigenvalue of largest magnitude and its unit eigenvector, by the power method.

\mathbf{x}_{k+1} = \frac{A\mathbf{x}_k}{\|A\mathbf{x}_k\|},\quad \lambda \approx \mathbf{x}_k^{T}A\mathbf{x}_k

Inputs

  • A a square matrix with a single real dominant eigenvalue matrix

Returns

  • eigenvalue the dominant eigenvalue
  • eigenvector its unit eigenvector (first largest entry positive)

Applies when

  • A is square, at most 10 x 10
  • one real eigenvalue is strictly largest in magnitude

Example

m.ask("Find the dominant eigenvalue of [4 1; 2 3] by the power method.")
→ eigenvalue = 5, eigenvector = [0.7071067811865475, 0.7071067811865475]
Eigenvalues and eigenvectors of a symmetric matrixsymmetric_eigenvectors

The orthonormal eigenvectors of a real symmetric matrix with distinct eigenvalues (spectral decomposition).

A\mathbf{q}_i = \lambda_i \mathbf{q}_i,\ Q^{T}Q = I

Inputs

  • A a symmetric matrix with distinct eigenvalues matrix

Returns

  • eigenvalues eigenvalues, ascending
  • eigenvectors unit eigenvectors in the same order, one per row

Applies when

  • A is symmetric
  • the eigenvalues are distinct
  • at most 10 x 10
  • sign convention: the first entry of largest magnitude of each eigenvector is positive

Example

m.ask("Find the eigenvectors of [[2, 1], [1, 2]].")
→ eigenvalues = [0.9999999999999998, 2.9999999999999996], eigenvectors = [[0.7071067811865476, -0.7071067811865476], [0.7071067811865476, 0.7071067811865476]]
Eigenvalues of a 2 x 2 matrixeigenvalues_2x2

Both eigenvalues of a 2 x 2 matrix from its characteristic equation lambda^2 - tr(A) lambda + det(A) = 0.

\lambda = \frac{\operatorname{tr}A \pm \sqrt{(\operatorname{tr}A)^2 - 4\det A}}{2}

Inputs

  • A a 2 x 2 matrix matrix

Returns

  • lambda1 first eigenvalue (the smaller real one, or the one with positive imaginary part)
  • lambda2 second eigenvalue

Applies when

  • A is 2 x 2

Example

m.ask("Find the eigenvalues of [[2, 1], [1, 2]].")
→ lambda1 = 1, lambda2 = 3
Eigenvalues of a general square matrix (QR algorithm)general_eigenvalues

All eigenvalues (real or complex) of a real square matrix, and its spectral radius.

\det(A - \lambda I) = 0,\quad \rho(A) = \max_i |\lambda_i|

Inputs

  • A a square matrix (up to 10 x 10) matrix

Returns

  • eigenvalues eigenvalues: real ones ascending, then complex pairs by real part, positive imaginary part first
  • spectral_radius largest |eigenvalue|

Applies when

  • A is square
  • at most 10 x 10

Example

m.ask("Find the eigenvalues of [[1, 2, 0], [0, 3, 0], [2, -4, 2]].")
→ eigenvalues = [0.999999999999998, 1.9999999999999991, 3.0000000000000018], spectral_radius = 3
Eigenvalues of a symmetric matrix (Jacobi method)symmetric_eigenvalues

All eigenvalues of a real symmetric matrix (they are real), in ascending order.

A = Q\Lambda Q^{T}

Inputs

  • A a symmetric matrix (up to 10 x 10) matrix

Returns

  • eigenvalues eigenvalues, ascending

Applies when

  • A is symmetric
  • at most 10 x 10

Example

m.ask("Find the eigenvalues of [[2, 1, 0], [1, 2, 1], [0, 1, 2]].")
→ eigenvalues = [0.585786437626905, 1.999999999999999, 3.4142135623730936]
Positive definiteness test (Sylvester's criterion)positive_definiteness

Whether a symmetric matrix is positive definite, with its leading principal minors.

A \succ 0 \iff D_k = \det A_{[1..k],[1..k]} > 0,\ k = 1..n

Inputs

  • A a symmetric matrix matrix

Returns

  • positive_definite true when x^T A x > 0 for every x != 0
  • leading_minors the leading principal minors D1, ..., Dn

Applies when

  • A is symmetric
  • at most 10 x 10

Example

m.ask("Is the matrix [[2, -1], [-1, 2]] positive definite?")
→ positive_definite = True, leading_minors = [2.0, 3.0]
Smallest eigenvalue by inverse iterationinverse_iteration

The eigenvalue of smallest magnitude and its unit eigenvector, by inverse power iteration.

A\mathbf{y}_{k+1} = \mathbf{x}_k,\quad \mathbf{x}_{k+1} = \mathbf{y}_{k+1}/\|\mathbf{y}_{k+1}\|

Inputs

  • A a nonsingular square matrix with a single real eigenvalue of smallest magnitude matrix

Returns

  • eigenvalue the eigenvalue of smallest magnitude
  • eigenvector its unit eigenvector (first largest entry positive)

Applies when

  • A is square and nonsingular, at most 10 x 10
  • one real eigenvalue is strictly smallest in magnitude

Example

m.ask("Find the smallest eigenvalue of [4 1; 2 3] by inverse power iteration.")
→ eigenvalue = 2, eigenvector = [-0.447213595499958, 0.894427190999916]

Least squares 5

Least-squares polynomial fitleast_squares_polynomial

The polynomial c0 + c1 x + ... + cd x^d of a given degree that best fits data in the least-squares sense.

\min_{\mathbf{c}} \sum_i \Big(y_i - \sum_{k=0}^{d} c_k x_i^k\Big)^2

Inputs

  • x x values vector
  • y y values vector
  • degree degree d of the polynomial (0 to 8) integer

Returns

  • coefficients [c0, c1, ..., cd], lowest power first

Applies when

  • x and y have the same number of values
  • more distinct x values than the degree
  • degree at most 8

Example

m.ask("Fit a quadratic to the points (0, 1), (1, 0), (2, 3), (3, 10) by least squares.")
→ coefficients = [1.0, -3.0, 2.0]
Least-squares regression lineleast_squares_line

The straight line y = a + b x that best fits data points in the least-squares sense, with R^2.

b = \frac{\sum (x_i-\bar x)(y_i-\bar y)}{\sum (x_i-\bar x)^2},\quad a = \bar y - b\bar x

Inputs

  • x x values vector
  • y y values vector

Returns

  • slope slope b
  • intercept intercept a
  • r_squared coefficient of determination R^2

Applies when

  • x and y have the same number of values, at least 2
  • the x values are not all equal
  • the y values are not all equal (otherwise R^2 is undefined)

Example

m.ask("Find the least squares line for the points (1, 2), (2, 3), (3, 5), (4, 4).")
→ slope = 0.8, intercept = 1.5, r_squared = 0.64
Least-squares solution of an overdetermined systemleast_squares_solution

The x minimizing |Ax - b| for a tall matrix A with independent columns, and the residual length.

A^{T}A\,\hat{\mathbf{x}} = A^{T}\mathbf{b}

Inputs

  • A coefficient matrix matrix
  • b right-hand side vector

Returns

  • solution least-squares solution x
  • residual_norm |Ax - b| at the solution

Applies when

  • at least as many rows as columns
  • the columns of A are linearly independent
  • at most 10 x 10

Example

m.ask("Find the least squares solution of Ax = b for A = [[1, 0], [0, 1], [1, 1]] and b = (1, 1, 0).")
→ solution = [0.3333333333333333, 0.3333333333333333], residual_norm = 1.1547
Moore-Penrose pseudoinverse (full rank)pseudoinverse

The pseudoinverse A+ of a matrix with independent columns, (A^T A)^-1 A^T, or independent rows, A^T (A A^T)^-1.

A^{+} = (A^{T}A)^{-1}A^{T}\ \text{or}\ A^{T}(AA^{T})^{-1}

Inputs

  • A a matrix of full rank (up to 10 x 10) matrix

Returns

  • pseudoinverse the matrix A+

Applies when

  • A has full rank (independent rows or independent columns)
  • at most 10 x 10

Example

m.ask("Find the pseudoinverse of [[1, 0], [0, 1], [1, 1]].")
→ pseudoinverse = [[0.6666666666666666, -0.3333333333333333, 0.3333333333333333], [-0.3333333333333333, 0.6666666666666666, 0.3333333333333333]]
Orthogonal projection matrix onto a column spaceprojection_matrix

The matrix P = A (A^T A)^-1 A^T that projects orthogonally onto the column space of A.

P = A(A^{T}A)^{-1}A^{T}

Inputs

  • A a matrix with independent columns matrix

Returns

  • projection the projection matrix P

Applies when

  • the columns of A are linearly independent
  • at most 10 x 10

Example

m.ask("What is the projection matrix onto the column space of [1; 2]?")
→ projection = [[0.2, 0.4], [0.4, 0.8]]

Linear systems 9

Conjugate gradient method for Ax = bconjugate_gradient

Solve Ax = b for a symmetric positive definite A by the method of conjugate gradients.

\alpha_k = \frac{\mathbf{r}_k^{T}\mathbf{r}_k}{\mathbf{p}_k^{T}A\mathbf{p}_k},\ \mathbf{x}_{k+1} = \mathbf{x}_k + \alpha_k\mathbf{p}_k,\ \mathbf{p}_{k+1} = \mathbf{r}_{k+1} + \frac{\mathbf{r}_{k+1}^{T}\mathbf{r}_{k+1}}{\mathbf{r}_k^{T}\mathbf{r}_k}\mathbf{p}_k

Inputs

  • A coefficient matrix matrix
  • b right-hand side vector

Returns

  • solution the solution x

Applies when

  • A is symmetric positive definite (checked exactly)
  • at most 10 x 10

Example

m.ask("Use conjugate gradients to solve Ax = b with A = [[4, 1], [1, 3]] and b = (1, 2).")
→ solution = [0.09090909090909091, 0.6363636363636364]
Gauss-Seidel iteration for Ax = bgauss_seidel

Solve Ax = b by the Gauss-Seidel method (each new value used at once), iterated to convergence.

x_i^{(k+1)} = \frac{1}{a_{ii}}\Big(b_i - \sum_{j<i} a_{ij} x_j^{(k+1)} - \sum_{j>i} a_{ij} x_j^{(k)}\Big)

Inputs

  • A coefficient matrix matrix
  • b right-hand side vector

Returns

  • solution the converged solution x

Applies when

  • A is square and strictly diagonally dominant, or symmetric positive definite (guarantees convergence)
  • at most 10 x 10

Example

m.ask("Use Gauss-Seidel to solve Ax = b with A = [[4, 1], [2, 5]] and b = (6, 12).")
→ solution = [1.0, 2.0]
Jacobi iteration for Ax = bjacobi_iteration

Solve Ax = b for a strictly diagonally dominant A by the Jacobi method, iterated to convergence.

x_i^{(k+1)} = \frac{1}{a_{ii}}\Big(b_i - \sum_{j\ne i} a_{ij} x_j^{(k)}\Big)

Inputs

  • A coefficient matrix matrix
  • b right-hand side vector

Returns

  • solution the converged solution x

Applies when

  • A is square and strictly diagonally dominant by rows (guarantees convergence)
  • at most 10 x 10

Example

m.ask("Use Jacobi iteration to solve Ax = b with A = [[4, 1], [2, 5]] and b = (6, 12).")
→ solution = [1.0, 2.0]
Number of solutions of a linear systemlinear_system_classification

Whether Ax = b has a unique solution, infinitely many, or none (Rouche-Capelli), with the two ranks.

\operatorname{rank}A < \operatorname{rank}[A\,|\,\mathbf{b}] \Rightarrow \text{none};\ \operatorname{rank}A = \operatorname{rank}[A\,|\,\mathbf{b}] = n \Rightarrow \text{unique}

Inputs

  • A coefficient matrix matrix
  • b right-hand side vector

Returns

  • solutions unique / infinitely many / none
  • rank_A rank of A
  • rank_augmented rank of the augmented matrix [A | b]

Applies when

  • b has one entry per row of A
  • at most 10 x 10

Example

m.ask("How many solutions does the system x + y = 2, 2x + 2y = 5 have?")
→ solutions = none, rank_A = 1, rank_augmented = 2
Reduced row echelon formrref

The reduced row echelon form (RREF) of a matrix, its pivot columns (1-based) and its rank.

A \sim \operatorname{rref}(A)

Inputs

  • A the matrix (up to 10 x 10) matrix

Returns

  • rref the reduced row echelon form
  • pivot_columns pivot columns, 1-based
  • rank number of pivots

Applies when

  • at most 10 x 10

Example

m.ask("Find the reduced row echelon form of [[1, 2, 3], [4, 5, 6], [7, 8, 9]].")
→ rref = [[1.0, 0.0, -1.0], [0.0, 1.0, 2.0], [0.0, 0.0, 0.0]], pivot_columns = [1.0, 2.0], rank = 2
Solve a square linear system Ax = bsolve_linear_system

The unique solution of Ax = b for a nonsingular n x n matrix A (n up to 10).

A\mathbf{x} = \mathbf{b}

Inputs

  • A coefficient matrix matrix
  • b right-hand side vector

Returns

  • solution the solution vector x

Applies when

  • A is square and nonsingular
  • b has one entry per row of A
  • at most 10 x 10

Example

m.ask("Solve Ax = b for A = [[2, 1], [1, 3]] and b = (3, 5).")
→ solution = [0.8, 1.4]
Three linear equations in three unknownssolve_3x3_system

Solve three linear equations in x, y, z by Cramer's rule.

x_j = \frac{\det A_j}{\det A}

Inputs

  • A coefficient matrix matrix
  • b right-hand side vector

Returns

  • solution the solution (x, y, z)
  • det determinant of the coefficient matrix

Applies when

  • A is 3 x 3
  • det A != 0 (otherwise there is no unique solution)

Example

m.ask("Solve the system x + y + z = 6, 2x - y + z = 3, x + 2y - z = 2.")
→ solution = [1.0, 2.0, 3.0], det = 7
Tridiagonal system (Thomas algorithm)tridiagonal_solve

Solve a tridiagonal system given its three diagonals and right-hand side, in O(n) operations.

a_i x_{i-1} + b_i x_i + c_i x_{i+1} = d_i

Inputs

  • sub sub-diagonal a_2..a_n (n-1 entries) vector
  • diag main diagonal b_1..b_n vector
  • sup super-diagonal c_1..c_(n-1) (n-1 entries) vector
  • d right-hand side (n entries) vector

Returns

  • solution the solution x

Applies when

  • n >= 2, at most 200
  • no zero pivot arises in the elimination (true for diagonally dominant matrices)

Example

m.ask("Solve the tridiagonal system with sub-diagonal (1, 1), diagonal (4, 4, 4), super-diagonal (1, 1) and right-hand side (5, 6, 5).")
→ solution = [1.0, 1.0, 1.0]
Two linear equations in two unknownssolve_2x2_system

Solve a x + b y = e, c x + d y = f for (x, y) by Cramer's rule.

x = \frac{\begin{vmatrix} e & b\\ f & d\end{vmatrix}}{\begin{vmatrix} a & b\\ c & d\end{vmatrix}},\quad y = \frac{\begin{vmatrix} a & e\\ c & f\end{vmatrix}}{\begin{vmatrix} a & b\\ c & d\end{vmatrix}}

Inputs

  • A coefficient matrix matrix
  • b right-hand side vector

Returns

  • solution the solution (x, y)
  • det determinant of the coefficient matrix

Applies when

  • A is 2 x 2
  • det A != 0 (otherwise there is no unique solution)

Example

m.ask("Solve the system 2x + 3y = 7, x - y = 1.")
→ solution = [2.0, 1.0], det = -5

Linear transformations 2

Matrix times vector (image under a linear map)matrix_vector_product

The vector Ax: the image of x under the linear transformation with matrix A.

(A\mathbf{x})_i = \sum_j a_{ij} x_j

Inputs

  • A the matrix matrix
  • x the vector vector

Returns

  • image the vector Ax

Applies when

  • columns of A = length of x

Example

m.ask("Multiply the matrix [[1, 2], [3, 4]] by the vector (5, 6).")
→ image = [17.0, 39.0]
Rotation of a plane vectorrotate_vector_2d

Rotate a 2-D vector counterclockwise about the origin by an angle given in degrees.

R(\theta)\mathbf{v} = \begin{pmatrix}\cos\theta & -\sin\theta\\ \sin\theta & \cos\theta\end{pmatrix}\mathbf{v}

Inputs

  • v the vector (2 entries) vector
  • angle rotation angle in degrees (counterclockwise)

Returns

  • rotated the rotated vector R(angle) v

Applies when

  • v has 2 entries
  • a positive angle turns counterclockwise

Example

m.ask("Rotate the vector (1, 0) by 90 degrees.")
→ rotated = [0.0, 1.0]

Matrices 12

Condition number of a matrixcondition_number

How sensitive Ax = b is to errors: the 2-norm condition number sigma_max/sigma_min and the 1-norm one.

\kappa_2(A) = \frac{\sigma_{\max}}{\sigma_{\min}},\quad \kappa_1(A) = \|A\|_1\|A^{-1}\|_1

Inputs

  • A a nonsingular square matrix matrix

Returns

  • cond2 2-norm condition number sigma_max / sigma_min
  • cond1 1-norm condition number |A|_1 |A^-1|_1

Applies when

  • A is square and nonsingular
  • at most 10 x 10

Example

m.ask("What is the condition number of the matrix [2 0; 0 0.5]?")
→ cond2 = 4, cond1 = 4
Difference of two matricesmatrix_subtraction

Subtract the second matrix from the first, entry by entry.

(A-B)_{ij} = a_{ij} - b_{ij}

Inputs

  • A first matrix matrix
  • B matrix subtracted matrix

Returns

  • difference the matrix A - B

Applies when

  • A and B have the same shape

Example

m.ask("Subtract the matrix [[1, 1], [1, 1]] from [[5, 6], [7, 8]].")
→ difference = [[4.0, 5.0], [6.0, 7.0]]
Inverse of a square matrixmatrix_inverse

The inverse A^-1 of a nonsingular square matrix (any size up to 10 x 10).

A A^{-1} = A^{-1} A = I

Inputs

  • A a nonsingular square matrix matrix

Returns

  • inverse the matrix A^-1

Applies when

  • A is square
  • det A != 0 (a singular matrix has no inverse)
  • at most 10 x 10

Example

m.ask("Find the inverse of [[4, 7], [2, 6]].")
→ inverse = [[0.6, -0.7], [-0.2, 0.4]]
Kronecker (tensor) product of two matriceskronecker_product

The block matrix A (x) B whose (i, j) block is a_ij B.

A\otimes B = [a_{ij}B]

Inputs

  • A first matrix matrix
  • B second matrix matrix

Returns

  • kron the Kronecker product A (x) B

Applies when

  • each matrix at most 6 x 6

Example

m.ask("Find the Kronecker product of [[1, 2], [3, 4]] and [[0, 1], [1, 0]].")
→ kron = [[0.0, 1.0, 0.0, 2.0], [1.0, 0.0, 2.0, 0.0], [0.0, 3.0, 0.0, 4.0], [3.0, 0.0, 4.0, 0.0]]
Norms of a matrixmatrix_norms

The Frobenius norm, the 1-norm (largest column sum), the infinity-norm (largest row sum) and the spectral 2-norm.

\|A\|_F = \sqrt{\textstyle\sum a_{ij}^2},\ \|A\|_1 = \max_j \textstyle\sum_i |a_{ij}|,\ \|A\|_\infty = \max_i \textstyle\sum_j |a_{ij}|,\ \|A\|_2 = \sigma_{\max}

Inputs

  • A the matrix matrix

Returns

  • frobenius Frobenius norm
  • norm1 1-norm: largest absolute column sum
  • norm_inf infinity-norm: largest absolute row sum
  • norm2 spectral norm: the largest singular value

Applies when

  • at most 10 x 10

Example

m.ask("Find the norms of the matrix [[1, -2], [3, 4]].")
→ frobenius = 5.47723, norm1 = 6, norm_inf = 7, norm2 = 5.11667
Power of a square matrixmatrix_power

A^k for a square matrix A and a whole number k (A^0 = I).

A^k = \underbrace{A A \cdots A}_{k}

Inputs

  • A a square matrix (at most 6 x 6) matrix
  • k the exponent (0 to 64) integer

Returns

  • power the matrix A^k

Applies when

  • A is square
  • k is a whole number, 0 <= k <= 64

Example

m.ask("Compute A^3 for A = [[1, 1], [0, 1]].")
→ power = [[1.0, 3.0], [0.0, 1.0]]
Product of two matricesmatrix_multiplication

The matrix product AB (rows of A times columns of B).

(AB)_{ij} = \sum_k a_{ik} b_{kj}

Inputs

  • A left matrix matrix
  • B right matrix matrix

Returns

  • product the matrix AB

Applies when

  • columns of A = rows of B
  • at most 10 rows and columns

Example

m.ask("Multiply the matrices [[1, 2], [3, 4]] and [[5, 6], [7, 8]].")
→ product = [[19.0, 22.0], [43.0, 50.0]]
Rank of a matrixmatrix_rank

The rank: the number of linearly independent rows (equivalently columns) of a matrix.

\operatorname{rank}A = \dim\operatorname{col}A

Inputs

  • A the matrix (up to 10 x 10) matrix

Returns

  • rank rank of A

Applies when

  • at most 10 x 10

Example

m.ask("Find the rank of [[1, 2, 3], [2, 4, 6], [1, 0, 1]].")
→ rank = 2
Scalar multiple of a matrixscalar_matrix_multiplication

Multiply every entry of a matrix by a number k.

(kA)_{ij} = k\,a_{ij}

Inputs

  • k the scalar
  • A the matrix matrix

Returns

  • product the matrix k A

Example

m.ask("Multiply the matrix [[1, 2], [3, 4]] by 3.")
→ product = [[3.0, 6.0], [9.0, 12.0]]
Sum of two matricesmatrix_addition

Add two matrices of the same shape entry by entry.

(A+B)_{ij} = a_{ij} + b_{ij}

Inputs

  • A first matrix matrix
  • B second matrix matrix

Returns

  • sum the matrix A + B

Applies when

  • A and B have the same shape

Example

m.ask("Add the matrices [[1, 2], [3, 4]] and [[5, 6], [7, 8]].")
→ sum = [[6.0, 8.0], [10.0, 12.0]]
Trace of a square matrixmatrix_trace

The trace: the sum of the diagonal entries of a square matrix.

\operatorname{tr}A = \sum_i a_{ii}

Inputs

  • A a square matrix matrix

Returns

  • trace tr(A) = sum of a_ii

Applies when

  • A is square

Example

m.ask("Find the trace of [[1, 2], [3, 4]].")
→ trace = 5
Transpose of a matrixmatrix_transpose

Swap rows and columns: the transpose A^T with (A^T)_ij = a_ji.

(A^{T})_{ij} = a_{ji}

Inputs

  • A the matrix matrix

Returns

  • transpose the matrix A^T

Example

m.ask("Find the transpose of [[1, 2, 3], [4, 5, 6]].")
→ transpose = [[1.0, 4.0], [2.0, 5.0], [3.0, 6.0]]

Matrix functions 2

Matrix exponentialmatrix_exponential

exp(A) = I + A + A^2/2! + ..., the solution operator of x' = A x.

e^{A} = \sum_{k=0}^{\infty} \frac{A^k}{k!} = \big(e^{A/2^s}\big)^{2^s}

Inputs

  • A a square matrix with |A|_1 <= 50 matrix

Returns

  • exponential the matrix exp(A)

Applies when

  • A is square, at most 10 x 10
  • |A|_1 <= 50

Example

m.ask("Find the matrix exponential of [[0, 1], [0, 0]].")
→ exponential = [[1.0, 1.0], [0.0, 1.0]]
Square root of a positive definite matrixmatrix_square_root

The unique symmetric positive definite X with X^2 = A, for a symmetric positive definite A.

A^{1/2} = Q\,\Lambda^{1/2} Q^{T}

Inputs

  • A a symmetric positive definite matrix matrix

Returns

  • sqrt the principal square root A^(1/2)

Applies when

  • A is symmetric positive definite
  • at most 10 x 10

Example

m.ask("Find the square root of the matrix [[5, 4], [4, 5]].")
→ sqrt = [[1.9999999999999991, 0.9999999999999996], [0.9999999999999996, 1.9999999999999991]]

Numerical PDE 2

1-D Poisson equation by finite differencespoisson_1d

Solve -u'' = f on (0, L) with u(0) = u(L) = 0 on n equally spaced interior points (second-order finite differences).

\frac{-u_{k-1} + 2u_k - u_{k+1}}{h^2} = f_k,\quad u_0 = u_{n+1} = 0

Inputs

  • f values of f at the n interior grid points x_k = k L/(n+1) vector
  • L length of the interval

Returns

  • u approximate u at the interior points
  • h grid spacing L/(n+1)

Applies when

  • L > 0
  • u = 0 at both ends (homogeneous Dirichlet conditions)
  • at most 200 interior points

Example

m.ask("Solve the 1-D Poisson equation -u'' = f with zero boundary values, L = 1 and f = (1, 1, 1) at the interior points.")
→ u = [0.09375, 0.125, 0.09375], h = 0.25
2-D Poisson equation on a grid (5-point stencil)poisson_2d

Solve -(u_xx + u_yy) = f on a rectangle with u = 0 on the boundary, on an n x m grid of interior points with spacing h.

\frac{4u_{i,j} - u_{i-1,j} - u_{i+1,j} - u_{i,j-1} - u_{i,j+1}}{h^2} = f_{i,j}

Inputs

  • f values of f at the interior grid points (row i, column j) matrix
  • h grid spacing (equal in x and y)

Returns

  • u approximate u at the interior grid points

Applies when

  • h > 0
  • u = 0 on the boundary
  • at most 12 x 12 interior points

Example

m.ask("Solve the 2-D Poisson equation with zero boundary values on the grid with h = 0.5 and f = [[1, 1], [1, 1]].")
→ u = [[0.125, 0.125], [0.12499999999999999, 0.12499999999999999]]

Numerical methods 16

Clamped cubic spline interpolationclamped_cubic_spline

The value at a point of the cubic spline through the data with prescribed slopes at the two ends.

2h_0M_0 + h_0M_1 = 6\Big(\frac{y_1-y_0}{h_0} - s_0\Big),\ h_{n-1}M_{n-1} + 2h_{n-1}M_n = 6\Big(s_n - \frac{y_n-y_{n-1}}{h_{n-1}}\Big)

Inputs

  • x knots, strictly increasing vector
  • y values at the knots vector
  • slope_start slope S'(x0)
  • slope_end slope S'(xn)
  • at the point (within the knots)

Returns

  • value spline value S(at)

Applies when

  • x strictly increasing, at least 2 knots (at most 60)
  • x[0] <= at <= x[-1]

Example

m.ask("Use a clamped cubic spline through the points (0, 0), (1, 1), (2, 0) with end slopes 1 and -1 to estimate the value at x = 0.5.")
→ value = 0.625
Coefficients of the interpolating polynomialinterpolating_polynomial

The polynomial c0 + c1 x + ... + c(n-1) x^(n-1) passing exactly through n data points.

\sum_k c_k x_i^k = y_i

Inputs

  • x x values (distinct) vector
  • y y values vector

Returns

  • coefficients [c0, c1, ...], lowest power first

Applies when

  • distinct x values
  • at most 15 points

Example

m.ask("Find the interpolating polynomial through the points (0, 1), (1, 3), (2, 7).")
→ coefficients = [1.0, 1.0, 1.0]
Composite midpoint rule for a polynomialmidpoint_rule

Approximate the integral of a polynomial over [a, b] by the composite midpoint rule with n subintervals.

\int_a^b p \approx h\sum_{k=0}^{n-1} p\big(a + (k+\tfrac12)h\big)

Inputs

  • coefficients polynomial coefficients, highest power first (written out in x in a question) vector
  • a lower limit
  • b upper limit
  • n number of subintervals integer

Returns

  • integral midpoint-rule estimate

Applies when

  • a < b
  • 1 <= n <= 1000

Example

m.ask("Use the midpoint rule with 4 subintervals to integrate x^2 from 0 to 2.")
→ integral = 2.625
Derivative of equally spaced data (finite differences)numerical_derivative

Estimate dy/dx at every sample of equally spaced data: central differences inside, one-sided differences at the ends.

y'_i \approx \frac{y_{i+1} - y_{i-1}}{2h}

Inputs

  • y samples y_0, ..., y_n (at least 2) vector
  • h spacing between samples

Returns

  • derivative estimated dy/dx at each sample

Applies when

  • h > 0
  • at least 2 samples (at most 200)

Example

m.ask("Estimate the derivative of the samples y = (0, 1, 4, 9, 16) with spacing h = 1.")
→ derivative = [1.0, 2.0, 4.0, 6.0, 7.0]
Evaluate a polynomial and its derivative (Horner's method)polynomial_evaluation

The value p(x) and the derivative p'(x) of a polynomial at a point.

p(x) = (\cdots((a_n x + a_{n-1})x + a_{n-2})x + \cdots)x + a_0

Inputs

  • coefficients polynomial coefficients, highest power first (written out in x in a question) vector
  • x the point

Returns

  • value p(x)
  • derivative p'(x)

Applies when

  • at most degree 12

Example

m.ask("Evaluate p(x) = 2x^3 - 6x^2 + 2x - 1 at x = 3.")
→ value = 5, derivative = 20
Gauss-Legendre quadrature nodes and weightsgauss_legendre_rule

The nodes and weights of the n-point Gauss-Legendre rule on [-1, 1] (exact for polynomials of degree up to 2n - 1).

\int_{-1}^{1} f(x)\,dx \approx \sum_{i=1}^{n} w_i f(x_i),\quad P_n(x_i) = 0

Inputs

  • n number of points (1 to 40) integer

Returns

  • nodes nodes x_i in [-1, 1], ascending
  • weights weights w_i

Applies when

  • 1 <= n <= 40

Example

m.ask("Find the nodes and weights of the 3-point Gauss-Legendre rule.")
→ nodes = [-0.7745966692414834, 0.0, 0.7745966692414834], weights = [0.5555555555555552, 0.8888888888888888, 0.5555555555555552]
Gauss-Legendre quadrature of a polynomialgauss_legendre_integral

The n-point Gauss-Legendre approximation of the integral of a polynomial over [a, b] (exact up to degree 2n - 1).

\int_a^b p(x)\,dx \approx \frac{b-a}{2}\sum_i w_i\,p\Big(\frac{a+b}{2} + \frac{b-a}{2}x_i\Big)

Inputs

  • coefficients polynomial coefficients, highest power first (written out in x in a question) vector
  • a lower limit
  • b upper limit
  • n number of Gauss points (1 to 40) integer

Returns

  • integral Gauss-Legendre estimate

Applies when

  • a < b
  • 1 <= n <= 40

Example

m.ask("Use 2-point Gauss-Legendre quadrature to integrate x^3 + x^2 from 0 to 2.")
→ integral = 6.66667
Lagrange interpolationlagrange_interpolation

The value at a point of the polynomial of lowest degree through given data points (Lagrange form).

p(t) = \sum_i y_i \prod_{j\ne i}\frac{t - x_j}{x_i - x_j}

Inputs

  • x x values of the data (distinct) vector
  • y y values of the data vector
  • at the point where the interpolant is evaluated

Returns

  • value interpolated value p(at)

Applies when

  • distinct x values
  • x and y of equal length (at most 60)

Example

m.ask("Use Lagrange interpolation through the points (0, 1), (1, 3), (2, 2) to estimate the value at x = 1.5.")
→ value = 2.875
Natural cubic spline interpolationnatural_cubic_spline

The value at a point of the natural cubic spline through the data (second derivative zero at both ends).

h_{i-1}M_{i-1} + 2(h_{i-1}+h_i)M_i + h_iM_{i+1} = 6\Big(\frac{y_{i+1}-y_i}{h_i} - \frac{y_i-y_{i-1}}{h_{i-1}}\Big),\ M_0 = M_n = 0

Inputs

  • x knots, strictly increasing (at least 3) vector
  • y values at the knots vector
  • at the point (within the knots)

Returns

  • value spline value S(at)

Applies when

  • x strictly increasing, at least 3 knots (at most 60)
  • x[0] <= at <= x[-1]
  • natural end conditions S''(x0) = S''(xn) = 0

Example

m.ask("Use a natural cubic spline through the points (0, 0), (1, 1), (2, 0) to estimate the value at x = 0.5.")
→ value = 0.6875
Newton divided differencesnewton_divided_differences

The coefficients f[x0], f[x0,x1], ..., f[x0..xn] of the Newton form of the interpolating polynomial.

f[x_i,\ldots,x_{i+k}] = \frac{f[x_{i+1},\ldots,x_{i+k}] - f[x_i,\ldots,x_{i+k-1}]}{x_{i+k} - x_i}

Inputs

  • x x values (distinct) vector
  • y y values vector

Returns

  • coefficients divided differences f[x0], f[x0,x1], ...: p(t) = c0 + c1 (t - x0) + c2 (t - x0)(t - x1) + ...

Applies when

  • distinct x values
  • x and y of equal length (at most 60)

Example

m.ask("Find the Newton divided differences for the points (1, 1), (2, 4), (4, 16).")
→ coefficients = [1.0, 3.0, 1.0]
Piecewise linear interpolationlinear_interpolation

The value at a point of the broken line joining consecutive data points.

y = y_k + (y_{k+1}-y_k)\frac{t - x_k}{x_{k+1} - x_k}

Inputs

  • x x values, strictly increasing vector
  • y y values vector
  • at the point (within the data range)

Returns

  • value interpolated value

Applies when

  • x strictly increasing
  • x[0] <= at <= x[-1] (no extrapolation)

Example

m.ask("Use linear interpolation on the points (0, 0), (1, 2), (3, 3) to estimate the value at x = 2.")
→ value = 2.5
Root of a polynomial by bisectionbisection_root

The root of a polynomial inside an interval [a, b] where it changes sign, by repeated halving.

m = \tfrac{a+b}{2};\ [a,b] \leftarrow [a,m]\ \text{if}\ p(a)p(m) < 0\ \text{else}\ [m,b]

Inputs

  • coefficients polynomial coefficients, highest power first (written out in x in a question) vector
  • a left end of the interval
  • b right end of the interval

Returns

  • root the root in [a, b]

Applies when

  • a < b
  • p(a) and p(b) have opposite signs
  • exactly one root in (a, b) (checked exactly by a Sturm sequence)
  • the root is simple and well conditioned

Example

m.ask("Find the root of f(x) = x^2 - 2 between 1 and 2 by the bisection method.")
→ root = 1.41421
Root of a polynomial by Newton's methodnewton_root

A root of a polynomial by Newton's method x <- x - p(x)/p'(x) from a starting value x0.

x_{k+1} = x_k - \frac{p(x_k)}{p'(x_k)}

Inputs

  • coefficients polynomial coefficients, highest power first (written out in x in a question) vector
  • x0 starting value

Returns

  • root the root the iteration converges to

Applies when

  • the iteration converges (within 200 steps) to a simple, well-conditioned root
  • p'(x) never vanishes at an iterate

Example

m.ask("Use Newton's method to find a root of x^3 - 2x - 5 starting from x0 = 2.")
→ root = 2.09455
Root of a polynomial by the secant methodsecant_root

A root of a polynomial by the secant method from two starting values x0 and x1.

x_{k+1} = x_k - p(x_k)\,\frac{x_k - x_{k-1}}{p(x_k) - p(x_{k-1})}

Inputs

  • coefficients polynomial coefficients, highest power first (written out in x in a question) vector
  • x0 first starting value
  • x1 second starting value

Returns

  • root the root the iteration converges to

Applies when

  • x0 != x1
  • the iteration converges (within 200 steps) to a simple, well-conditioned root

Example

m.ask("Use the secant method to find a root of x^3 - 2x - 5 with x0 = 2 and x1 = 3.")
→ root = 2.09455
Simpson's rule for tabulated datasimpson_rule

The integral of equally spaced tabulated data by the composite Simpson 1/3 rule.

\int y\,dx \approx \frac{h}{3}\Big(y_0 + 4\sum_{\text{odd}} y_i + 2\sum_{\text{even}} y_i + y_n\Big)

Inputs

  • x equally spaced, increasing x values (odd count) vector
  • y y values vector

Returns

  • integral Simpson estimate of the integral

Applies when

  • x equally spaced and increasing
  • an odd number of points, at least 3 (an even number of intervals)

Example

m.ask("Use Simpson's rule to integrate the data (0, 1), (1, 2), (2, 5).")
→ integral = 4.66667
Trapezoidal rule for tabulated datatrapezoid_rule

The integral of tabulated data by the composite trapezoidal rule (any spacing).

\int y\,dx \approx \sum_i \frac{x_{i+1}-x_i}{2}(y_i + y_{i+1})

Inputs

  • x x values, strictly increasing vector
  • y y values vector

Returns

  • integral trapezoidal estimate of the integral

Applies when

  • x strictly increasing
  • at least 2 points (at most 60)

Example

m.ask("Use the trapezoidal rule to integrate the data (0, 1), (1, 3), (2, 2), (4, 0).")
→ integral = 6.5

Sparse matrices 4

Compressed sparse column (CSC) form of a matrixcsc_format

Store a matrix in compressed sparse column form: the nonzero values, their row indices and the column pointers.

\texttt{data}[\texttt{indptr}[k]:\texttt{indptr}[k+1]]

Inputs

  • A the matrix (up to 50 x 50) matrix

Returns

  • data nonzero values, column by column
  • indices 0-based row index of each value
  • indptr column pointers: the values of column k are data[indptr[k]:indptr[k+1]]
  • nnz number of stored nonzeros
  • density nnz / (rows x columns)

Applies when

  • at most 50 x 50
  • zeros are not stored; indices sorted within each column (canonical form)

Example

m.ask("Convert [[1, 0, 2], [0, 0, 3], [4, 5, 0]] to CSC format.")
→ data = [1.0, 4.0, 5.0, 2.0, 3.0], indices = [0, 2, 2, 0, 1], indptr = [0, 2, 3, 5], nnz = 5, density = 0.555556
Compressed sparse row (CSR) form of a matrixcsr_format

Store a matrix in compressed sparse row form: the nonzero values, their column indices and the row pointers.

\texttt{data}[\texttt{indptr}[k]:\texttt{indptr}[k+1]]

Inputs

  • A the matrix (up to 50 x 50) matrix

Returns

  • data nonzero values, row by row
  • indices 0-based column index of each value
  • indptr row pointers: the values of row k are data[indptr[k]:indptr[k+1]]
  • nnz number of stored nonzeros
  • density nnz / (rows x columns)

Applies when

  • at most 50 x 50
  • zeros are not stored; indices sorted within each row (canonical form)

Example

m.ask("Convert [[1, 0, 2], [0, 0, 3], [4, 5, 0]] to CSR format.")
→ data = [1.0, 2.0, 3.0, 4.0, 5.0], indices = [0, 2, 2, 0, 1], indptr = [0, 2, 3, 5], nnz = 5, density = 0.555556
Dense matrix from CSR arrayscsr_to_dense

Rebuild the full matrix from compressed sparse row arrays (data, indices, indptr) and the number of columns.

A_{r,\,\texttt{indices}[k]} \mathrel{+}= \texttt{data}[k],\ \texttt{indptr}[r] \le k < \texttt{indptr}[r+1]

Inputs

  • data stored values vector
  • indices 0-based column index of each value vector
  • indptr row pointers (rows + 1 entries) vector
  • ncols number of columns integer

Returns

  • dense the full matrix

Applies when

  • indptr starts at 0, never decreases, ends at len(data)
  • indices within 0..ncols-1
  • duplicate entries are summed
  • at most 50 x 50

Example

m.ask("Build the dense matrix with 3 columns from the CSR arrays data = [1, 2, 3], indices = [0, 2, 1], indptr = [0, 2, 3].")
→ dense = [[1.0, 0.0, 2.0], [0.0, 3.0, 0.0]]
Sparse matrix times vector (CSR)sparse_matvec

The product y = A x for a matrix A given in compressed sparse row form.

y_r = \sum_{k=\texttt{indptr}[r]}^{\texttt{indptr}[r+1]-1} \texttt{data}[k]\,x_{\texttt{indices}[k]}

Inputs

  • data stored values vector
  • indices 0-based column index of each value vector
  • indptr row pointers (rows + 1 entries) vector
  • x the vector (one entry per column) vector

Returns

  • product the vector A x

Applies when

  • consistent CSR arrays
  • column indices below len(x)
  • at most 50 x 50

Example

m.ask("Multiply the CSR matrix data = [1, 2, 3], indices = [0, 2, 1], indptr = [0, 2, 3] by x = [1, 1, 1].")
→ product = [3.0, 3.0]

Vector spaces 5

Column space of a matrixcolumn_space

A basis of the column space (range) of A: the pivot columns of A itself, and its dimension.

\operatorname{col}A = \operatorname{span}\{\mathbf{a}_j : j \text{ a pivot column}\}

Inputs

  • A the matrix (up to 10 x 10) matrix

Returns

  • dimension dimension of the column space (the rank)
  • basis the pivot columns of A, left to right

Applies when

  • at most 10 x 10

Example

m.ask("Find a basis for the column space of [[1, 2, 3], [2, 4, 7]].")
→ dimension = 2, basis = [[1.0, 2.0], [3.0, 7.0]]
Coordinates of a vector in a basischange_of_basis

The coordinates c of v with respect to a basis b1, ..., bn: v = c1 b1 + ... + cn bn.

\mathbf{v} = \sum_i c_i \mathbf{b}_i

Inputs

  • basis the basis vectors, one per row matrix
  • v the vector vector

Returns

  • coordinates the coordinate vector [v]_B

Applies when

  • n basis vectors of length n that are linearly independent
  • v has n entries

Example

m.ask("Find the coordinates of (3, 1) in the basis (1, 1), (1, -1).")
→ coordinates = [2.0, 1.0]
Gram-Schmidt orthonormalizationgram_schmidt

Turn linearly independent vectors v1, ..., vk into orthonormal vectors e1, ..., ek spanning the same nested subspaces.

\mathbf{w}_j = \mathbf{v}_j - \sum_{i<j} (\mathbf{e}_i\cdot\mathbf{v}_j)\,\mathbf{e}_i,\quad \mathbf{e}_j = \mathbf{w}_j/\|\mathbf{w}_j\|

Inputs

  • vectors the vectors, one per row (independent) matrix

Returns

  • orthonormal orthonormal vectors e1, ..., ek, one per row

Applies when

  • the vectors are linearly independent
  • at most 10 vectors of at most 10 entries

Example

m.ask("Apply Gram-Schmidt to the vectors (3, 4) and (1, 0).")
→ orthonormal = [[0.6, 0.8], [0.7999999999999999, -0.6]]
Linear independence of vectorslinear_independence

Whether a list of vectors is linearly independent, with the rank of the list.

c_1\mathbf{v}_1 + \cdots + c_k\mathbf{v}_k = \mathbf{0} \Rightarrow c_1 = \cdots = c_k = 0

Inputs

  • vectors the vectors, one per row (equal lengths) matrix

Returns

  • independent true when no vector is a combination of the others
  • rank dimension of their span

Applies when

  • all vectors have the same length
  • at most 10 vectors of at most 10 entries

Example

m.ask("Are the vectors (1, 2, 3), (4, 5, 6) and (7, 8, 9) linearly independent?")
→ independent = False, rank = 2
Null space (kernel) of a matrixnull_space

The dimension of the null space {x : Ax = 0} (the nullity) and a basis of it.

\operatorname{null}A = \{\mathbf{x} : A\mathbf{x} = \mathbf{0}\},\quad \dim = n - \operatorname{rank}A

Inputs

  • A the matrix (up to 10 x 10) matrix

Returns

  • nullity dimension of the null space = columns - rank
  • basis basis vectors: one per free variable, that variable 1, the other free variables 0

Applies when

  • at most 10 x 10

Example

m.ask("Find the null space of [[1, 2, 3], [2, 4, 6]].")
→ nullity = 2, basis = [[-2.0, 1.0, 0.0], [-3.0, 0.0, 1.0]]

Vectors 13

Angle between two vectorsangle_between_vectors

The angle between two nonzero vectors, in degrees and radians (between 0 and 180 degrees).

\cos\theta = \frac{\mathbf{u}\cdot\mathbf{v}}{\|\mathbf{u}\|\,\|\mathbf{v}\|}

Inputs

  • u first vector vector
  • v second vector vector

Returns

  • angle_degrees angle in degrees
  • angle_radians angle in radians

Applies when

  • u and v have the same number of entries
  • neither vector is zero

Example

m.ask("Find the angle between the vectors (1, 0) and (1, 1).")
→ angle_degrees = 45, angle_radians = 0.785398
Area of the parallelogram (and triangle) spanned by two vectorsparallelogram_area

|u x v|: the area of the parallelogram with sides u and v (2-D or 3-D), and half of it for the triangle.

A = \|\mathbf{u}\times\mathbf{v}\| = \sqrt{\|\mathbf{u}\|^2\|\mathbf{v}\|^2 - (\mathbf{u}\cdot\mathbf{v})^2}

Inputs

  • u first side (2 or 3 entries) vector
  • v second side (2 or 3 entries) vector

Returns

  • area parallelogram area |u x v|
  • triangle_area triangle area |u x v| / 2

Applies when

  • u and v have the same number of entries, 2 or 3

Example

m.ask("Find the area of the parallelogram spanned by (3, 0) and (1, 2).")
→ area = 6, triangle_area = 3
Cross product of two 3-D vectorscross_product

The cross product u x v of two vectors in three dimensions (right-hand rule).

\mathbf{u}\times\mathbf{v} = (u_2v_3-u_3v_2,\; u_3v_1-u_1v_3,\; u_1v_2-u_2v_1)

Inputs

  • u first vector (3 entries) vector
  • v second vector (3 entries) vector

Returns

  • cross the vector u x v

Applies when

  • u and v have exactly 3 entries

Example

m.ask("Find the cross product of (1, 2, 3) and (4, 5, 6).")
→ cross = [-3.0, 6.0, -3.0]
Difference of two vectorsvector_subtraction

Subtract the second vector from the first, entry by entry.

\mathbf{u}-\mathbf{v} = (u_1-v_1, \ldots, u_n-v_n)

Inputs

  • u first vector vector
  • v vector subtracted vector

Returns

  • difference the vector u - v

Applies when

  • u and v have the same number of entries

Example

m.ask("Find the difference of the vectors [5, 3] and [2, 7].")
→ difference = [3.0, -4.0]
Distance between two pointsdistance_between_points

The Euclidean distance between two points given by their coordinates (any dimension).

d = \sqrt{\textstyle\sum_i (p_i - r_i)^2}

Inputs

  • p coordinates of the first point vector
  • r coordinates of the second point vector

Returns

  • distance Euclidean distance |p - r|

Applies when

  • the points have the same number of coordinates

Example

m.ask("Find the distance between the points (1, 2) and (4, 6).")
→ distance = 5
Dot product of two vectorsdot_product

The dot (scalar, inner) product u . v = sum of u_i v_i.

\mathbf{u}\cdot\mathbf{v} = \sum_i u_i v_i

Inputs

  • u first vector vector
  • v second vector vector

Returns

  • dot the dot product u . v

Applies when

  • u and v have the same number of entries

Example

m.ask("Find the dot product of (1, 2, 3) and (4, -5, 6).")
→ dot = 12
Length (norm) of a vectorvector_norm

The Euclidean length |v| of a vector, with its 1-norm (sum of magnitudes) and max-norm.

\|\mathbf{v}\|_2 = \sqrt{\textstyle\sum_i v_i^2}

Inputs

  • v the vector vector

Returns

  • norm Euclidean length |v|_2
  • norm1 1-norm: sum of |v_i|
  • norm_inf max-norm: largest |v_i|

Example

m.ask("Find the magnitude of the vector (3, 4).")
→ norm = 5, norm1 = 7, norm_inf = 4
Outer product of two vectorsouter_product

The matrix u v^T with entries u_i v_j.

(\mathbf{u}\mathbf{v}^{T})_{ij} = u_i v_j

Inputs

  • u first vector (rows) vector
  • v second vector (columns) vector

Returns

  • outer the matrix u v^T

Example

m.ask("Find the outer product of (1, 2) and (3, 4, 5).")
→ outer = [[3.0, 4.0, 5.0], [6.0, 8.0, 10.0]]
Projection of one vector onto anothervector_projection

The orthogonal projection of u onto v, (u.v / v.v) v, and the scalar projection (component) u.v / |v|.

\mathrm{proj}_{\mathbf{v}}\mathbf{u} = \frac{\mathbf{u}\cdot\mathbf{v}}{\mathbf{v}\cdot\mathbf{v}}\,\mathbf{v}

Inputs

  • u vector to project vector
  • v direction projected onto (not zero) vector

Returns

  • projection vector projection of u onto v
  • scalar_projection scalar projection u.v / |v|

Applies when

  • u and v have the same number of entries
  • v is not the zero vector

Example

m.ask("Find the projection of (2, 3) onto (1, 0).")
→ projection = [2.0, 0.0], scalar_projection = 2
Scalar multiple of a vectorscalar_vector_multiplication

Multiply every entry of a vector by a number k.

k\mathbf{v} = (k v_1, \ldots, k v_n)

Inputs

  • k the scalar
  • v the vector vector

Returns

  • product the vector k v

Example

m.ask("Multiply the vector (1, -2, 3) by 4.")
→ product = [4.0, -8.0, 12.0]
Scalar triple product (volume of a parallelepiped)triple_scalar_product

u . (v x w), the signed volume of the parallelepiped spanned by three 3-D vectors, and its absolute value.

\mathbf{u}\cdot(\mathbf{v}\times\mathbf{w}) = \det[\mathbf{u};\mathbf{v};\mathbf{w}]

Inputs

  • u first vector (3 entries) vector
  • v second vector (3 entries) vector
  • w third vector (3 entries) vector

Returns

  • triple u . (v x w)
  • volume volume |u . (v x w)|

Applies when

  • each vector has exactly 3 entries

Example

m.ask("Find the scalar triple product of (1, 0, 0), (0, 1, 0) and (0, 0, 1).")
→ triple = 1, volume = 1
Sum of two vectorsvector_addition

Add two vectors of the same length entry by entry.

\mathbf{u}+\mathbf{v} = (u_1+v_1, \ldots, u_n+v_n)

Inputs

  • u first vector vector
  • v second vector vector

Returns

  • sum the vector u + v

Applies when

  • u and v have the same number of entries

Example

m.ask("Add the vectors (1, 2, 3) and (4, 5, 6).")
→ sum = [5.0, 7.0, 9.0]
Unit vector in the direction of a vectorunit_vector

Normalize a nonzero vector: v / |v|, the vector of length 1 pointing the same way.

\hat{\mathbf{v}} = \mathbf{v}/\|\mathbf{v}\|

Inputs

  • v the vector (not all zero) vector

Returns

  • unit the unit vector v / |v|

Applies when

  • v is not the zero vector

Example

m.ask("Find the unit vector in the direction of (3, 4).")
→ unit = [0.6, 0.8]
Questions

Frequently asked

What does linear-algebra-ai999x do?

Linear Algebra answers questions in linear algebra: vectors, matrices, determinants, linear systems, eigenvalues, decompositions, least squares, sparse matrices and numerical methods (root finding, interpolation, quadrature). Each answer gives the value with its unit, the steps and the formula in LaTeX.

How do I install linear-algebra-ai999x?

With pip: pip install linear-algebra-ai999x. The same files are on Hugging Face (hf download AI999X/linear-algebra-ai999x --local-dir linear-algebra-ai999x) and GitHub.

Does it run offline?

Yes. It is a Python package that runs on an ordinary CPU without a network connection (Python >=3.10); no GPU and no API key are needed.

How many capabilities does linear-algebra-ai999x have?

89 capabilities in 13 topics, including Numerical methods, Vectors, Matrices, Linear systems, Eigenvalues.

Which field and tasks does it cover?

Field: Mathematics & Statistics. Tasks: Question Answering, Token Classification, Text Classification, Sentence Similarity, Feature Extraction.

What does an answer look like?

Asked "Find the determinant of [[2, 0, 1], [1, 3, 2], [1, 1, 2]].", it answers: det = 6.

What happens when a question cannot be answered?

It says so and names what is missing or out of range, instead of guessing a number.

More from AI999X

Explore every model.