MuPAD - Demo

# ... Linear Algebra

## Contents

1. The domain constructor Matrix
2. Manipulation of matrices
3. Standard arithmetic of matrices
4. The domain constructor SquareMatrix
5. The library package linalg
6. Solving systems of linear equations
7. Matrices over Gaussian Integers
8. Index of algebraic examples

MuPAD has a linear algebra library package. This chapter describes how to declare rings of matrices by using the constructors Matrix and SquareMatrix and how to operate on them.

### The domain constructor Matrix

The following statement declares the domain of matrices over the field of rationals.

- `>> loadlib("linalg"): export(linalg); MQ:=Matrix(Rational);`
  `Matrix(Rational)`

  In the same way we can declare the rings of matrices over Z/6Z and Z.

- `>> MZ6:=Matrix(IntegerMod(6)): MZ:=Matrix(Integer):`
  ``

  Now these new domains can be used to create/generate matrices over rational values.

- `>> A:= MQ( 3, 3, 1/2 );`
  `

      +-               -+
      | 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).

- `>> type( A );`
  `Matrix(Rational)`

- `>> B:= MZ6(2,4, [[3,0,8,1], [1,-5]]);`
  `

      +-                                     -+
      | 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.

- `>> F:= MZ6( C );`
  `

      +-                                                         -+
      | 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.

- `>> testtype( F, MZ6 );`
  `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.

- `>> MExpr:= Matrix();`
  `Matrix(ExpressionField(id, iszero))`

### Manipulation of matrices

For matrices defined over domains that was created by Matrix or SquareMatrix a large set of methods and manipulating functions are available.

- `>> MZ:=Matrix(Integer): A:=MZ([[1,2,3],[4,5,6],[7,8,9]]);`
  `

      +-         -+
      | 1 , 2 , 3 |
      |           |
      | 4 , 5 , 6 |
      |           |
      | 7 , 8 , 9 |
      +-         -+

  `

- `>> A::gaussElim( A );`
  `

      -- +-             -+       --
      |  | 1 ,  2  ,  3  |        |
      |  |               |        |
      |  | 0 , - 3 , - 6 |, 2, 0  |
      |  |               |        |
      |  | 0 ,  0  ,  0  |        |
      -- +-             -+       --

  `

- `>> A::col( A, 2 );`
  `

      +- -+
      | 2 |
      |   |
      | 5 |
      |   |
      | 8 |
      +- -+

  `

- `>> A::delRow( A::delCol(A,2), 1 );`
  `

      +-     -+
      | 4 , 6 |
      |       |
      | 7 , 9 |
      +-     -+

  `

- `>> A::swapCol( A, 1, 3 );`
  `

      +-         -+
      | 3 , 2 , 1 |
      |           |
      | 6 , 5 , 4 |
      |           |
      | 9 , 8 , 7 |
      +-         -+

  `

- `>> A::setRow( A,3,MZ(1,3,[1,0,1]) );`
  `

      +-         -+
      | 1 , 2 , 3 |
      |           |
      | 4 , 5 , 6 |
      |           |
      | 1 , 0 , 1 |
      +-         -+

  `

- `>> A::transpose( A );`
  `

      +-         -+
      | 1 , 4 , 7 |
      |           |
      | 2 , 5 , 8 |
      |           |
      | 3 , 6 , 9 |
      +-         -+

  `

- `>> A::stackMatrix( A, A::row(A,1) );`
  `

      +-         -+
      | 1 , 2 , 3 |
      |           |
      | 4 , 5 , 6 |
      |           |
      | 7 , 8 , 9 |
      |           |
      | 1 , 2 , 3 |
      +-         -+

  `

- `>> A::concatMatrix( A, A::col(A,2) );`
  `

      +-             -+
      | 1 , 2 , 3 , 2 |
      |               |
      | 4 , 5 , 6 , 5 |
      |               |
      | 7 , 8 , 9 , 8 |
      +-             -+

  `

- `>> map(A, proc(x) begin if isprime(x) then 1 else 0 end_if end_proc);`
  `

      +-         -+
      | 0 , 1 , 1 |
      |           |
      | 0 , 1 , 0 |
      |           |
      | 1 , 0 , 0 |
      +-         -+

  `

### Standard arithmetic of matrices

Also the standard arithmetic is defined for matrices created by Matrix or SquareMatrix and they can be used in the usual way.

- `>> A:=MQ(2,2,[1,5],Diagonal); B:=MQ([[1,2],[3,4]]); C:=MQ([[0,4],[5]]);`
  `

      +-     -+
      | 1 , 0 |
      |       |
      | 0 , 5 |
      +-     -+

      +-     -+
      | 1 , 2 |
      |       |
      | 3 , 4 |
      +-     -+

      +-     -+
      | 0 , 4 |
      |       |
      | 5 , 0 |
      +-     -+

  `

- `>> A - B + C*B - 3*(A/C+B);`
  `

      +-             -+
      |   9    , 37/5 |
      |               |
      | - 43/4 , - 1  |
      +-             -+

  `

- `>> 1/2*B + A^10 - B^(-2);`
  `

      +-                 -+
      | - 4  ,    7/2     |
      |                   |
      | 21/4 , 39062501/4 |
      +-                 -+

  `

- `>> C^0 * 1/(A*C);`
  `

      +-          -+
      |  0   , 1/5 |
      |            |
      | 1/20 ,  0  |
      +-          -+

  `

  Arithmetical operation can also be performed on matrices over the field of expressions.

- `>> A:=MExpr([[sin(a),I*b],[-I*c,cos(d)]]);`
  `

      +-                -+
      | sin(a)  , (I) b  |
      |                  |
      | (- I) c , cos(d) |
      +-                -+

  `

- `>> A^(-1);`
  `

      +-                                                   -+
      |          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)  |
      +-                                                   -+

  `

- `>> A::gaussElim( A );`
  `

      -- +-                               -+                            --
      |  | sin(a) ,         (I) b          |                             |
      |  |                                 |, 2, - b c + cos(d) sin(a)   |
      |  |   0    , - b c + cos(d) sin(a)  |                             |
      -- +-                               -+                            --

  `

- `>> conjugate( A );`
  `

      +-                                      -+
      | sin(conjugate(a)) , (- I) conjugate(b) |
      |                                        |
      | (I) conjugate(c)  , cos(conjugate(d))  |
      +-                                      -+

  `

### The domain constructor SquareMatrix

For domains of NxN matrices there is a special constructor, but all methods listed for NxK matrices can also be performed on square matrices.

- `>> SMZ:=SquareMatrix(3, Integer);`
  `SquareMatrix(3, Integer)`

  Such a domain forms a ring, which can be determined by the function hasProp. can be used.

- `>> SMZ::hasProp( Ring );`
  `TRUE`

### The library package linalg

At first we load the linalg package and declare the matrix domains we want to work with. The function info lists all public methods that are defined in a library package or a domain.

- `

      >> 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

  `

- `>> A:=MQ([[1,2,3,4],[-1,0,1],[3,5,6,9]]): gaussElim( A );`
  `

      +-                 -+
      | 1 , 2 ,  3  ,  4  |
      |                   |
      | 0 , 2 ,  4  ,  4  |
      |                   |
      | 0 , 0 , - 2 , - 2 |
      +-                 -+

  `

- `>> gaussJordan( A );`
  `

      +-             -+
      | 1 , 0 , 0 , 1 |
      |               |
      | 0 , 1 , 0 , 0 |
      |               |
      | 0 , 0 , 1 , 1 |
      +-             -+

  `

- `>> A:=randomMatrix( 2,2, DistributedPolynomial([x], IntegerMod(7)) );`
  `

      +-                                                 -+
      |            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 |
      +-                                                 -+

  `

- `>> det( A );`
  `

               2    3      4      5    6    7      8
      3 x + 3 x  + x  + 3 x  + 2 x  + x  - x  - 3 x

  `

- `>> gaussJordan( A );`
  `

      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
      )

  `

- `>> -A; 7*A;`
  `

      +-                                                 -+
      |          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 |
      +-     -+

  `

- `>> iszero( % );`
  `TRUE`

  The function linalg::norm offers different norms of matrices and vectors. For example the infinity-norm.

- `>> linalg::norm( MQ([[1,-2],[3,-8]]) );`
  `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.

- `>> bool( nops(freeSet({v1,v2})) = 2 );`
  `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.

- `>> A:=MQ( [[1,2,3],[5,7,9],[2,4,6]] ): nullSpace( A );`
  `

      -- +-   -+ --
      |  |  1  |  |
      |  |     |  |
      |  | - 2 |  |
      |  |     |  |
      |  |  1  |  |
      -- +-   -+ --

  `

### Solving systems of linear equations

- `>> loadlib("linalg"): export(linalg): MQ:=Matrix(Rational);`
  `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.

- `>> linearSolve( M,v );`
  `

      -- +-   -+  -- +-   -+  +- -+ -- --
      |  | - 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).

- `>> linearSolve( M,v,c );`
  `[- 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.

- `>> linearSolve( M,v,[2,-5] );`
  `

      +-    -+
      | - 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.

- `>> A:=MQ( [[1,2],[1,3]] ); B:=MQ( [[1,1],[-2,1]] );`
  `

      +-     -+
      | 1 , 2 |
      |       |
      | 1 , 3 |
      +-     -+

      +-       -+
      |  1  , 1 |
      |         |
      | - 2 , 1 |
      +-       -+

  `

  Then the solution is a matrix whose columns represent the solutions of the certain systems.

- `>> linearSolve( A,B );`
  `

      +-       -+
      |  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    |  |  |
      -- +-    -+  -- +-      -+ -- --

  `

### Matrices over Gaussian Integers

- `>>`
  ``