Matrix(Rational)
In the same way we can declare the rings of matrices over Z/6Z and Z.
Now these new domains can be used to create/generate matrices over rational values.
+- -+
| 1/2 , 1/2 , 1/2 |
| |
| 1/2 , 1/2 , 1/2 |
| |
| 1/2 , 1/2 , 1/2 |
+- -+
We can see that A is of type Matrix(Rational).
Matrix(Rational)
+- -+
| 3 mod 6 , 0 mod 6 , 2 mod 6 , 1 mod 6 |
| |
| 1 mod 6 , 1 mod 6 , 0 mod 6 , 0 mod 6 |
+- -+
We can also declare a function f(i,j) to compute the elements of a matrix.
>> f:= proc(i,j) begin if i = j then 2 elif i-1 = j or i+1 = j then -1 else 0 end_if end_proc: C:= MZ( 6, 6, f );
+- -+
| 2 , - 1 , 0 , 0 , 0 , 0 |
| |
| - 1 , 2 , - 1 , 0 , 0 , 0 |
| |
| 0 , - 1 , 2 , - 1 , 0 , 0 |
| |
| 0 , 0 , - 1 , 2 , - 1 , 0 |
| |
| 0 , 0 , 0 , - 1 , 2 , - 1 |
| |
| 0 , 0 , 0 , 0 , - 1 , 2 |
+- -+
Also implicit and expilcit matrix conversion are possible. The following statement converts the matrix C over Z into a matrix F over Z/6Z.
+- -+
| 2 mod 6 , 5 mod 6 , 0 mod 6 , 0 mod 6 , 0 mod 6 , 0 mod 6 |
| |
| 5 mod 6 , 2 mod 6 , 5 mod 6 , 0 mod 6 , 0 mod 6 , 0 mod 6 |
| |
| 0 mod 6 , 5 mod 6 , 2 mod 6 , 5 mod 6 , 0 mod 6 , 0 mod 6 |
| |
| 0 mod 6 , 0 mod 6 , 5 mod 6 , 2 mod 6 , 5 mod 6 , 0 mod 6 |
| |
| 0 mod 6 , 0 mod 6 , 0 mod 6 , 5 mod 6 , 2 mod 6 , 5 mod 6 |
| |
| 0 mod 6 , 0 mod 6 , 0 mod 6 , 0 mod 6 , 5 mod 6 , 2 mod 6 |
+- -+
We can also check the type conversion by using the function testtype.
TRUE
Symbolic matrices can be declared over the field of expressions (defined by the domain constructor ExpressionField). This is the default coefficient domain for matrices.
Matrix(ExpressionField(id, iszero))
+- -+
| 1 , 2 , 3 |
| |
| 4 , 5 , 6 |
| |
| 7 , 8 , 9 |
+- -+
-- +- -+ --
| | 1 , 2 , 3 | |
| | | |
| | 0 , - 3 , - 6 |, 2, 0 |
| | | |
| | 0 , 0 , 0 | |
-- +- -+ --
+- -+
| 2 |
| |
| 5 |
| |
| 8 |
+- -+
+- -+
| 4 , 6 |
| |
| 7 , 9 |
+- -+
+- -+
| 3 , 2 , 1 |
| |
| 6 , 5 , 4 |
| |
| 9 , 8 , 7 |
+- -+
+- -+
| 1 , 2 , 3 |
| |
| 4 , 5 , 6 |
| |
| 1 , 0 , 1 |
+- -+
+- -+
| 1 , 4 , 7 |
| |
| 2 , 5 , 8 |
| |
| 3 , 6 , 9 |
+- -+
+- -+
| 1 , 2 , 3 |
| |
| 4 , 5 , 6 |
| |
| 7 , 8 , 9 |
| |
| 1 , 2 , 3 |
+- -+
+- -+
| 1 , 2 , 3 , 2 |
| |
| 4 , 5 , 6 , 5 |
| |
| 7 , 8 , 9 , 8 |
+- -+
+- -+
| 0 , 1 , 1 |
| |
| 0 , 1 , 0 |
| |
| 1 , 0 , 0 |
+- -+
+- -+
| 1 , 0 |
| |
| 0 , 5 |
+- -+
+- -+
| 1 , 2 |
| |
| 3 , 4 |
+- -+
+- -+
| 0 , 4 |
| |
| 5 , 0 |
+- -+
+- -+
| 9 , 37/5 |
| |
| - 43/4 , - 1 |
+- -+
+- -+
| - 4 , 7/2 |
| |
| 21/4 , 39062501/4 |
+- -+
+- -+
| 0 , 1/5 |
| |
| 1/20 , 0 |
+- -+
Arithmetical operation can also be performed on matrices over the field of expressions.
+- -+
| sin(a) , (I) b |
| |
| (- I) c , cos(d) |
+- -+
+- -+
| b c |
| ---------------------- + 1 |
| - b c + cos(d) sin(a) (- I) b |
| -------------------------- , ---------------------- |
| sin(a) - b c + cos(d) sin(a) |
| |
| (I) c sin(a) |
| ---------------------- , ---------------------- |
| - b c + cos(d) sin(a) - b c + cos(d) sin(a) |
+- -+
-- +- -+ --
| | sin(a) , (I) b | |
| | |, 2, - b c + cos(d) sin(a) |
| | 0 , - b c + cos(d) sin(a) | |
-- +- -+ --
+- -+
| sin(conjugate(a)) , (- I) conjugate(b) |
| |
| (I) conjugate(c) , cos(conjugate(d)) |
+- -+
SquareMatrix(3, Integer)
Such a domain forms a ring, which can be determined by the function hasProp. can be used.
TRUE
>> loadlib("linalg"): export(linalg): info(linalg); MZ:=Matrix(Integer): MQ:=Matrix(Rational): MExpr:=Matrix():
>> Library 'linalg': the linear algebra package
Interface:
delRow
setRow
grad
curl
transpose
vectorDimen
charPolynomial
gaussJordan
crossProduct
freeSet
randomMatrix
linearSolve
linalg::norm
ogCoordTab
charMatrix
addCol
ogSystem
isOrthogonal
multiplyCol
adjoint
nonZeros
addRow
normalize
innerProduct
multiplyRow
extractMatrix
stackMatrix
scalarMult
angle
rank
ncols
onSystem
nullSpace
swapCol
vectorPotential
nrows
divergence
gaussElim
det
concatMatrix
swapRow
dimen
col
delCol
sylvester
setCol
factorQR
linalg::trace
row
expr2Matrix
isHermitian
+- -+
| 1 , 2 , 3 , 4 |
| |
| 0 , 2 , 4 , 4 |
| |
| 0 , 0 , - 2 , - 2 |
+- -+
+- -+
| 1 , 0 , 0 , 1 |
| |
| 0 , 1 , 0 , 0 |
| |
| 0 , 0 , 1 , 1 |
+- -+
+- -+
| 3 4 5 3 |
| - 2 x + 2 x + 2 x + 3 x , - x |
| |
| 2 3 2 3 |
| 3 x + x , 2 x - 2 x - x + 2 |
+- -+
2 3 4 5 6 7 8
3 x + 3 x + x + 3 x + 2 x + x - x - 3 x
array(1..2, 1..2,
3 4 5
(1, 1) = - 2 x + 2 x + 2 x + 3 x ,
(1, 2) = 0,
(2, 1) = 0,
2 3 4 5 6 7 8
(2, 2) = 3 x + 3 x + x + 3 x + 2 x + x - x - 3 x
)
+- -+
| 3 4 5 3 |
| 2 x - 2 x - 2 x - 3 x , x |
| |
| 2 3 2 3 |
| - 3 x - x , - 2 x + 2 x + x - 2 |
+- -+
+- -+
| 0 , 0 |
| |
| 0 , 0 |
+- -+
TRUE
The function linalg::norm offers different norms of matrices and vectors. For example the infinity-norm.
11
The following example computes the sylvester matrix for two polynomials f and g.
>> f:=poly( 1+2*x+10*x^2,IntMod(7) ); g:=poly( x^2+23*x-5,IntMod(7) ); sylvester( f, g, x );
/ 2 \
poly \ 2 x + 3 x + 1, [x], IntMod(7) /
/ 2 \
poly \ 2 x + x + 2, [x], IntMod(7) /
+- -+
| 3 mod 7 , 2 mod 7 , 1 mod 7 , 0 mod 7 |
| |
| 0 mod 7 , 3 mod 7 , 2 mod 7 , 1 mod 7 |
| |
| 1 mod 7 , 2 mod 7 , 2 mod 7 , 0 mod 7 |
| |
| 0 mod 7 , 1 mod 7 , 2 mod 7 , 2 mod 7 |
+- -+
The function freeSet computes a set of lenearly independent vectors over a field.
>> v1:= MQ( [1,0,1,1,0] ): v2:= MQ( [2,2,6,2,4] ): v3:= MQ( [1,2,5,3,5] ): v4:= MQ( [0,-1,-2,2,-1] ): freeSet( [v1,v2,v3,v4] );
-- +- -+ +- -+ +- -+ --
| | 1 | | 2 | | 1 | |
| | | | | | | |
| | 0 | | 2 | | 2 | |
| | | | | | | |
| | 1 |, | 6 |, | 5 | |
| | | | | | | |
| | 1 | | 2 | | 3 | |
| | | | | | | |
| | 0 | | 4 | | 5 | |
-- +- -+ +- -+ +- -+ --
This can be used to test the lenearly independent of vectors over a field.
TRUE
The function factorQR computes a QR-factorization or a matrix.
>> f:= factorQR( MExpr([[2,-3,-1],[1,1,-1],[0,1,-1]]) ): Q:=op(f,1); R:=op(f,2);
+- -+
| - 1/2 - 1/2 |
| - 1/2 - 1/2 2 15 8 15 |
| 2 5 , - 6 , - ------------------- |
| 15 |
| |
| - 1/2 - 1/2 |
| - 1/2 - 1/2 4 15 8 15 |
| 5 , 2 6 , ------------------- |
| 15 |
| |
| - 1/2 - 1/2 |
| - 1/2 2 15 8 15 |
| 0 , 6 , - ------------------- |
| 3 |
+- -+
+- -+
| 1/2 |
| 1/2 1/2 3 5 |
| 5 , - 5 , - ------ |
| 5 |
| |
| 1/2 |
| 1/2 6 |
| 0 , 6 , - ---- |
| 3 |
| |
| 1/2 1/2 |
| 8 15 |
| 0 , 0 , ---------- |
| 15 |
+- -+
At least let us compute the "mull space" of a matrix.
-- +- -+ --
| | 1 | |
| | | |
| | - 2 | |
| | | |
| | 1 | |
-- +- -+ --
Matrix(Rational)
We want to solve the system M*x = v defined by:
>> M:=MQ( [[1,3,-4,3],[3,9,-2,-11],[4,12,-6,-8],[2,6,2,-14]] ); v:=MQ( [9,-3,6,-12] );
+- -+
| 1 , 3 , - 4 , 3 |
| |
| 3 , 9 , - 2 , - 11 |
| |
| 4 , 12 , - 6 , - 8 |
| |
| 2 , 6 , 2 , - 14 |
+- -+
+- -+
| 9 |
| |
| - 3 |
| |
| 6 |
| |
| - 12 |
+- -+
The system is solved by using the function linearSolve. Because the system has no unique solution linearSolve returns a list whose first argument is a special solution and the second argument is a list of basis for the space of solutions of the corresponding homogenous system.
-- +- -+ -- +- -+ +- -+ -- --
| | - 3 | | | - 3 | | 5 | | |
| | | | | | | | | |
| | 0 | | | 1 | | 0 | | |
| | |, | | |, | | | |
| | - 3 | | | 0 | | 2 | | |
| | | | | | | | | |
| | 0 | | | 0 | | 1 | | |
-- +- -+ -- +- -+ +- -+ -- --
You can express the general solution in form of a linear combination by using a free identifier (here c is used).
[- 3 c[1] + 5 c[2] - 3 , c[1], 2 c[2] - 3, c[2]]
You can also compute a special solution by using special linear factors.
+- -+
| - 34 |
| |
| 2 |
| |
| - 13 |
| |
| - 5 |
+- -+
To solve a system with respect to a matrix on the right side of the equation the user simply replaces the vector by a matrix.
+- -+
| 1 , 2 |
| |
| 1 , 3 |
+- -+
+- -+
| 1 , 1 |
| |
| - 2 , 1 |
+- -+
Then the solution is a matrix whose columns represent the solutions of the certain systems.
+- -+
| 7 , 1 |
| |
| - 3 , 0 |
+- -+
If you have a set or list of linear equations you could first use the function expr2Matrix to build up a matrix and a right-side vector representing the coefficients of your equations. Besides you can simply use the function solve.
>> expr2Matrix( [x+2*y-2=-5*z+3*x,x+z+10=-8*z], [x,y,z], Rational ); linearSolve( op(%) );
-- +- -+ +- -+ --
| | - 2 , 2 , 5 | | 2 | |
| | |, | | |
| | 1 , 0 , 9 | | - 10 | |
-- +- -+ +- -+ --
-- +- -+ -- +- -+ -- --
| | - 10 | | | - 9 | | |
| | | | | | | |
| | - 9 |, | | - 23/2 | | |
| | | | | | | |
| | 0 | | | 1 | | |
-- +- -+ -- +- -+ -- --