Programming Environment for Numerical Computing
 Guide Math Algebra ODE Parser Data Plot Time Socket

Linear Algebra Functions

Comet uses the well-established Lapack library for linear algebra calculations.
These functions can be called through the linalg namespace.
Comet uses tables for matrix and vector with the row-major order:
the 3x3 matrix {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} is represented as the table {1, 2, 3, 4, 5, 6, 7, 8, 9}.
Note: the linear algebra module is not available for Android.

Below is a summary of the linalg functions:

 linalg.add(x,y,a,b) y = a*x + b*y where a, b are scalars and x,y tables the input y is modified x = linalg.array(n,a) Create n-size array and init its values to a z = linalg.cat(x,y) Concatenate x and y arrays linalg.copy(x,y) Copy x to y d = linalg.det(A) Matrix determinant d = linalg.dot(x,y) d = x' * y wr, wi, vl = linalg.eig(A) Eigenvalues and eigenvetors of matrix A wr: vector with real part of eigenvalues wi: vector with imaginary part of eigenvalues vl: matrix with the eigenvetors as columns str = linalg.format(A,m) Format matrix with m lines into string y = linalg.get(x,is,ie) Returns subarray of x from index is to ie im = linalg.max(x) Index of the max value in x im = linalg.min(x) Index of the min value in x B = linalg.inv(A) Returns the inverse matrix x = linalg.leastsq(A,b,m) Solves rectangular system Ax = bwith m lines for b and therefore m columns for A z = linalg.mult(x,y,m) z = x*y with m lines r = linalg.norm(x) Norm-2 of x A = linalg.ones(n) Returns an n-size array of 1 A = linalg.rand(n) Returns an n-size array of random values xr, yr = linalg.rot(x,y,c,s) Plane rotation of x,y vectors with: given cosine c and sine s Returns the rotated vectors x = linalg.solve(A,b) Solve the square linear system Ax = b x,iters = linalg.sparse(A,b,x0,tol,iters) Solve iteratively the square system Ax = bReturns the solution and iterations # s = linalg.sum(x) Absolute sum of x linalg.swap(x,y) Swap x et y t = linalg.trace(A) Calculate matrix trace x = linalg.tridiag(down,diag,up,b) Solve tridiagonal system with: the main, upper and lower diagonals B = linalg.trans(A,m) Transpose matrix with m lines A = linalg.zeros(n) Return an n-size array of 0

Example 1:

Copy the following script in the editor...

cls()
print "\nlinalg.solve\n"
A = {3, 2, -1, 2, -2, 4, -1, 0.5, -1 }
b = {1, -2, 0 }
x = linalg.solve(A,b)
print("x = " .. linalg.format(x,3))

... and click Run (or press F12 on Windows and Linux)

Example 2:

cls()
print "\nlinalg.inv\n"
A = {1, 2, 3, 0, 1, 4, 5, 6, 0 }
iA = linalg.inv(A)
print("iA = " .. linalg.format(iA,3))

Example 3:

cls()
print "\nlinalg.eig\n"
A = {2, 0, 0, 0, 3, 4, 0, 4, 9}
wr, wi, vl = linalg.eig(A)
print("wr = " .. linalg.format(wr,3))
print("vl = " .. linalg.format(vl,3))

Example 4:

cls()
print "\nlinalg.leastsq\n"

-- A with 6 columns
A = { 1.44, -7.84, -4.39, 4.53, -9.96, -0.28,
-3.24, 3.83, -7.55, 3.24, 6.27, -6.64,
8.34, 8.09, 5.28, 2.06, 7.08, 2.52,
0.74, -2.47, -5.45, -5.70, -1.19, 4.70 }

-- b with 6 rows
b = { 8.58,
8.26,
8.48,
-5.28,
5.72,
8.93 }

-- x will have 4 rows (as for matrix A)
-- x should be { -0.450637, -0.84915, 0.706612, 0.128886 }
x = linalg.leastsq(A,b,6)

print("x = " .. linalg.format(x,4))