Contents Changes to MuPAD 1.2.2 Advanced Calculus Polynomials Linear Algebra Graphics Gröbner Bases Further Nice Examples
Dom::Interval
Dom::Matrix
Dom::Rational
Dom::SquareMatrix
linalg::expr2Matrix
linalg::linearSolve
linalg::randomMatrix
This example will show how to do linear algebra with ranges as coefficients of matrices and vectors.
Consider the following problem:
>> Ab:= linalg::expr2Matrix( [x+5*y=-2, 2*x+y=4 ],[x,y],Dom::Rational );
-- +- -+ +- -+ -- | | 1, 5 | | -2 | | | | |, | | | | | 2, 1 | | 4 | | -- +- -+ +- -+ --
Now it is easy to solve the first task:
>> linalg::linearSolve( op(Ab) );
+- -+ | 22/9 | | | | -8/9 | +- -+
To solve the second task we will use the domain
Dom::Interval
representing real intervals, which builds a field by using the standard
interval arithmetic.
We create the set of matrices over this field of real intervals and
define the same matrix as A
and b
but
perturbing each coefficent by 0.01:
>> alias( IV = Dom::Interval ): IAb:= linalg::expr2Matrix( [IV(0.99..1.01)*x + IV(4.99..5.01)*y = IV(-2.01..-1.99), IV(1.99..2.01)*x + IV(0.99..1.01)*y = IV(3.99..4.01)] ,IV );
-- +- -+ +- -+ -- | | 0.99..1.01, 4.99..5.01 | | -2.01..-1.99 | | | | |, | | | | | 1.99..2.01, 0.99..1.01 | | 3.99..4.01 | | -- +- -+ +- -+ --and solve the new linear system:
>> linalg::linearSolve( op(IAb) );
+- -+ | 2.309250509..2.584883855 | | | | -0.9079910213..-0.8702090209 | +- -+
This means that if you know your coefficients with a precision of , then the error on the result is about on by taking , and about on by taking .
We want to show a small but nice example how to define and to do arithmetic with block matrices.
Let us first define the ring of square matrices over the rationals:
Now we define four square matrices:
The following defines the matrices over the ring of 2x2 matrices:
We define a matrix containing and some random matrices:
We perform some standard arithmetic:
Finally we want to compute the inverse of matrix Let us prove the suppositions:
Hence we can apply the following formula for the inverse of Finally, we test our result:
>> SqMat:= Dom::SquareMatrix( 2,Dom::Rational );
Dom::SquareMatrix(2, Dom::Rational)
>> a:= SqMat( [[2,3],[1,0]] ); b:= SqMat( [[1,0],[-2,3]] );
c:= SqMat( [[0,-4],[2,1]] ); d:= SqMat( [[0,1],[2,-1]] );
+- -+
| 2, 3 |
| |
| 1, 0 |
+- -+
+- -+
| 1, 0 |
| |
| -2, 3 |
+- -+
+- -+
| 0, -4 |
| |
| 2, 1 |
+- -+
+- -+
| 0, 1 |
| |
| 2, -1 |
+- -+
>> MS:= Dom::Matrix( SqMat );
Dom::Matrix(Dom::SquareMatrix(2, Dom::Rational))
a, b, c
and d
>> A:= MS( [[a,b],[c,d]] );
+- -+
| +- -+ +- -+ |
| | 2, 3 | | 1, 0 | |
| | |, | | |
| | 1, 0 | | -2, 3 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | 0, -4 | | 0, 1 | |
| | |, | | |
| | 2, 1 | | 2, -1 | |
| +- -+ +- -+ |
+- -+
>> f:= fun( SqMat(
randomMatrix( 2,2,Dom::Integer )
) ):
B:= MS( 2,2,f );
C:= MS( 2,2,f,Diagonal );
+- -+
| +- -+ +- -+ |
| | 824, -65 | | -979, -764 | |
| | |, | | |
| | -814, -741 | | 216, 663 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | 880, 916 | | 597, -245 | |
| | |, | | |
| | 617, -535 | | 79, 747 | |
| +- -+ +- -+ |
+- -+
+- -+
| +- -+ +- -+ |
| | 477, -535 | | 0, 0 | |
| | |, | | |
| | -906, -905 | | 0, 0 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | 0, 0 | | -266, -8 | |
| | | , | | |
| | 0, 0 | | 765, 448 | |
| +- -+ +- -+ |
+- -+
Please note that your output may be different (we have created
the matrices randomly).
>> A*B + B - 2*C;
+- -+
| +- -+ +- -+ |
| | -44, -432 | | -1692, -548 | |
| | |, | | |
| | 1913, -2433 | | -1720, 2630 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | 4753, 3345 | | 344 , -2134 | |
| | |, | | |
| | 2594, 961 | | -2078, -2251 | |
| +- -+ +- -+ |
+- -+
>> A^5;
+- -+
| +- -+ +- -+ |
| | 558, 777 | | 431, -222 | |
| | |, | | |
| | 290, -278 | | -551, 959 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | -537, -992 | | -649, 551 | |
| | |, | | |
| | 196, 424 | | 345, -340 | |
| +- -+ +- -+ |
+- -+
>> B*C - C*B;
+- -+
| +- -+ +- -+ |
| | -376600, -747445 | | 258497, 384693 | |
| | |, | | |
| | 292942, 376600 | | -241755, 203127 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | -171120, -1060404 | | -186793, -173730 | |
| | |, | | |
| | -170597, -306980 | | 58344 , 186793 | |
| +- -+ +- -+ |
+- -+
A
.
It is well known that for 2x2 matrices a, b, c, d, the
matrix [[a,b],[c,d]] is invertible if det(a) <> 0 and
det( d - d*c/a ) <> 0.
>> det( a );
-3
>> det( d-c*(1/a)*b );
-3
a
:
>> f:= d-c*(1/a)*b:
iA:= MS( [[1/a+1/a*b*1/f*c*1/a, -1/a*b*1/f],
[ -1/f*c*1/a , 1/f ]]);
+- -+
| +- -+ +- -+ |
| | -2, 1 | | -1, 2 | |
| | |, | | |
| | 2/3, -2/9 | | 1/9, -5/9 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | 3 , -4/3 | | 5/3, -7/3 | |
| | |, | | |
| | 8/3, -8/9 | | 13/9, -20/9 | |
| +- -+ +- -+ |
+- -+
>> A * iA; iA * A;
+- -+
| +- -+ +- -+ |
| | 1, 0 | | 0, 0 | |
| | |, | | |
| | 0, 1 | | 0, 0 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | 0, 0 | | 1, 0 | |
| | |, | | |
| | 0, 0 | | 0, 1 | |
| +- -+ +- -+ |
+- -+
+- -+
| +- -+ +- -+ |
| | 1, 0 | | 0, 0 | |
| | |, | | |
| | 0, 1 | | 0, 0 | |
| +- -+ +- -+ |
| |
| +- -+ +- -+ |
| | 0, 0 | | 1, 0 | |
| | |, | | |
| | 0, 0 | | 0, 1 | |
| +- -+ +- -+ |
+- -+