| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.14.7 Linear algebra
The matrix class can be used with indices to do some simple linear
algebra (linear combinations and products of vectors and matrices, traces
and scalar products):
{
idx i(symbol("i"), 2), j(symbol("j"), 2);
symbol x("x"), y("y");
// A is a 2x2 matrix, X is a 2x1 vector
matrix A(2, 2), X(2, 1);
A = 1, 2,
3, 4;
X = x, y;
cout << indexed(A, i, i) << endl;
// -> 5
ex e = indexed(A, i, j) * indexed(X, j);
cout << e.simplify_indexed() << endl;
// -> [[2*y+x],[4*y+3*x]].i
e = indexed(A, i, j) * indexed(X, i) + indexed(X, j) * 2;
cout << e.simplify_indexed() << endl;
// -> [[3*y+3*x,6*y+2*x]].j
}
|
You can of course obtain the same results with the matrix::add(),
matrix::mul() and matrix::trace() methods (see section Matrices)
but with indices you don't have to worry about transposing matrices.
Matrix indices always start at 0 and their dimension must match the number of rows/columns of the matrix. Matrices with one row or one column are vectors and can have one or two indices (it doesn't matter whether it's a row or a column vector). Other matrices must have two indices.
You should be careful when using indices with variance on matrices. GiNaC doesn't look at the variance and doesn't know that ‘F~mu~nu’ and ‘F.mu.nu’ are different matrices. In this case you should use only one form for ‘F’ and explicitly multiply it with a matrix representation of the metric tensor.
