# Maple Examples for Solving Linear Algebra Equations::

## Linear Algebra Beginner Examples:

### Matrix Input:

```#
#  Anything on a line after a `#` symbol is a comment in Maple.
#
# First call Maple's Linear Algebra Package so Maple linear algebra functions
# can be used (the colon ":" hides function output):
> with(linalg):
#
# Sample Input of 4X4 Matrix with Rows Separated by Square Brackets and Commas,
#    and Elements in each row Separated by Commas:
>  A:=array([[0,2,0,1],[2,2,3,2],[4,-3,0,1],[6,1,-6,-5]]);
<                               [ 0   2   0   1 ]
<                               [ 2   2   3   2 ]
<                          A := [ 4  -3   0   1 ]
<                               [ 6   1  -6  -5 ]
```

### Vector Input:

```> #
> # Sample Input of Vector for Matrix Multiplication:
> B:=vector([0,-2,-7,6]);
<
<                     B := [ 0, -2, -7, 6 ]
<
```

### Matrix Solve for A*ANS=B:

```> #
> # Matrix and Linear Solve is linsolve(A,B) in Maple to solve A*X=B for X in Math:
> X:=linsolve(A, B);
<
<                     X := [ -1/2, 1, 1/3, -2 ]
<
> Xfloat := X;
<
<                Xfloat := [ -.5000000000, 1., .3333333333, -2. ]
<
| # Note that "# " at Beginning of Line is Only a Comment!
| # Finite Precision Answer Using 4 Digits with both floating point
| #   evaluation and matrix evaluation functions:
> Xf4:= evalf(evalm(X), 4);
<
<                Xf4 := [ -.0001, 1., .3333, -2. ]
<
| # Residual Vector using proper Matrix Multiply and Add functions:
<
<                RES := [ 0, .0001, 0, -.0002 ]
<
| # Infinity Norm of Residual Vector:
> RESnorm:=norm(RES,infinity);
<
<                RESnorm :=  .0002
<

Random Matrix and Vector Input:

> #
> # Caution#2: Both yield default element values in (-99, +99):
> with(linalg):
> M:=randmatrix(5,5);  Q:=randvector(5);
<
<                 [ -85  -55  -37  -35   97 ]
<                 [  50   79   56   49   63 ]
<            M := [  57  -59   45   -8  -93 ]
<                 [  92   43  -62   77   66 ]
<                 [  54   -5   99  -61  -50 ]
<
<            Q := [ -12, -18, 31, -26, -62 ]
<
|  # More Matrix Solve to Get Y from M*Y = Q:
> Y:=linsolve(M,Q);
<
<      [    6513803886    441598229   2780317558  1161487621     4047189176 ]
< Y := [ - -----------, - ---------, -----------, ----------, - ----------- ]
<      [   10148464579    922587689  10148464579   922587689    10148464579 ]
<
>  Yfloat:=evalf(linsolve(M,Q));
<
< Yfloat := [ -.6418511722, -.4786517686, .2739643555, 1.258945502, -.3987981773 ]
<

Matrix Norms:

> #
> #  'norm(M,q)' means q-norm of Matrix M:
> #
> normM1:=norm(M,1);
<
<                      normM1 := 369
<
> normMinf:=norm(M,infinity);
<
<                      normMinf := 340
<
> normM2:=norm(M,2);
<
< normM2 := max(abs(RootOf(- 102991333311217647241 + 130759387784582485 _Z
<                    2                3            4     5
< - 36880268261270 _Z  + 3286110146 _Z  - 101373 _Z  + _Z )))^1/2
<
> normM2float:=evalf(normM2);
<
<                 normM2float := 33.05625794
<

Condition Numbers:

> #
> # Condition Number Examples, 2-norm assumed and singular values used:
> # 'cond(A)' allows no other norms and 'cond(A,1)' gives error!
> condA1:=cond(A,1);
<
<                      644
<            condA1 := ---
<                       39
<
> condA1float:=evalf(condA1);
<
<                condA1float := 16.51282051
<
> condAinf:=cond(A,`infinity`);
<
<                             282
<                 condAinf := ---
<                              13
<
>condAinffloat:=evalf(condAinf);
<
<              condAinffloat := 21.69230769
<
>condA2:=cond(A,2);
<
<                                                    2         3     4   1/2
< condA2 := max(abs(RootOf(54756 - 55978 _Z + 5339 _Z  - 150 _Z  + _Z )))
<
<                                          2           3           4   1/2
<     * max(abs(RootOf(1 - 150 _Z + 5339 _Z  - 55978 _Z  + 54756 _Z )))
<
<
>condA2float:=evalf(condA2);
<
<              condA2float := .1024322204
<
> condM1:=cond(M,1);
<
<                          124630041510
<                condM1 := ------------
<                          10148464579
<
> condM1float:=evalf(condM1);
<
<              condM1float := 12.28067956
<
> condMinf:=cond(M,infinity);
<
<                          14006279600
<              condMinf := -----------
<                            922587689
<
> condMinffloat:=evalf(condMinf);
<
<             condMinffloat := 15.18151582
<
> condM2:=cond(M,2);
<
< condM2 := max(abs(RootOf(- 102991333311217647241 + 130759387784582485 _Z
<
<                    2                3            4     5
< - 36880268261270 _Z  + 3286110146 _Z  - 101373 _Z  + _Z )))^1/2 max(abs(
<
<                                       2                    3
< RootOf(- 1 + 101373 _Z - 3286110146 _Z  + 36880268261270 _Z
<
<                        4                           5
< - 130759387784582485 _Z  + 102991333311217647241 _Z )))^1/2
<
> condM2float:=evalf(condM2);
<
<                 condM2float := .1535244049
<

Inverse Matrix:

> #
> inverseM:=inverse(M);
<
<  inverseM :=
<
<  [   6261545        37591591     2148964       89143795       78454729   ]
<  [ -----------  - -----------  -----------   -----------    -----------  ]
<  [ 10148464579    10148464579  10148464579   10148464579    10148464579  ]
<  [                                                                       ]
<  [    7898369       2333230       12120121       581895       3512618    ]
<  [ - ---------   - ---------   - ---------   - ---------     ---------   ]
<  [   922587689     922587689     922587689     922587689     922587689   ]
<  [                                                                       ]
<  [   31311760     108924930      72189234       63328753       19875703  ]
<  [ -----------   -----------   -----------  - -----------  - ----------- ]
<  [ 10148464579   10148464579   10148464579    10148464579    10148464579 ]
<  [                                                                       ]
<  [    267815       10557436      11775387       4531902        14062400  ]
<  [  ---------     ---------     ---------    - ---------    - ---------  ]
<  [  922587689     922587689     922587689      922587689      922587689  ]
<  [                                                                       ]
<  [   73853882      35958205       562004       32342577       27261452   ]
<  [ -----------   -----------   -----------   -----------    -----------  ]
<  [ 10148464579   10148464579   10148464579   10148464579    10148464579  ]
<

Determinants:

> #
> # Determinant Examples:
> detA:=det(A)
<
<                       detA := -234
<
> detM:=det(M);
<
<                       detM := -10148464579
<

Symbolic Class Example of Nearly Singular Model for Very Small H:

> #
> AH:=array([[1,1-H],[1+H,1]]);
<
<                 [   1    1 - H ]
<           AH := [              ]
<                 [ 1 + H    1   ]
<
> condAH:=cond(AH);
<
<     condAH := max(1 + abs(1 - H), abs(1 + H) + 1)
<
<        1          - 1 + H       1 + H       1
< max(------- + abs(-------), abs(-----) + -------)
<           2            2           2           2
<     abs(H)            H           H      abs(H)
<
<
> detAH:=det(AH);
<
<                              2
<                    detAH := H
<
> normAH1:= norm(AH,1);
<
<         normAH1:= max(1 + abs(1 - H), abs(1 + H) + 1)
<
> normAHinf:= norm(AH,'infinity');
<
<         normAHinf:= max(1 + abs(1 - H), abs(1 + H) + 1)
<
> normAH2:=norm(AH,2);
<
<                         2           2 1/2            2           2 1/2  1/2
< normAH2 := max(abs(2 + H  + 2 (1 + H )   ), abs(2 + H  - 2 (1 + H )   ))
<
> condAH1:=cond(AH,1);
<
<   condAH1 := max(1 + abs(1 - H), abs(1 + H) + 1)
<
<                 (   1         (- 1 + H)     (1 + H)      1   )
<            * max(------- + abs(-------), abs(-----) + -------)
<                 (      2      (    2  )     (   2 )         2)
<                 (abs(H)       (   H   )     (  H  )   abs(H) )
<
> condAHinf:=cond(AH,'infinity');
<
<   condAHinf := max(1 + abs(1 - H), abs(1 + H) + 1)
<
<                   (   1         (- 1 + H)     (1 + H)      1   )
<              * max(------- + abs(-------), abs(-----) + -------)
<                   (      2      (    2  )     (   2 )         2)
<                   (abs(H)       (   H   )     (  H  )   abs(H) )
> condAH2:=cond(AH,2);
<
<                         2           2 1/2            2           2 1/2  1/2
< condAH2 := max(abs(2 + H  + 2 (1 + H )   ), abs(2 + H  - 2 (1 + H )   ))
<
<      (       (       2           2 1/2)                 2           2 1/2))1/2
<      (       (4 + 2 H  - 4 (1 + H )   )          4 + 2 H  + 4 (1 + H )   ))
< * max(1/2 abs(------------------------), 1/2 abs(------------------------))
<      (       (          4             )                    4             ))
<      (       (         H              )                   H              ))
<
>    AHinv:=inverse(AH);
<
<                    [     1    - 1 + H ]
<                    [   ----   ------- ]
<                    [     2        2   ]
<                    [    H        H    ]
<           AHinv := [                  ]
<                    [   1 + H      1   ]
<                    [ - -----    ----  ]
<                    [      2       2   ]
<                    [     H       H    ]
<
>   multiply(AH,AHinv);  # check of truth of inverse property
<
<              [ 1  0 ]
<              [      ]
<              [ 0  1 ]
<
> norm(AHinv,1);
<
<             (   1         (- 1 + H)     (1 + H)      1   )
<          max(------- + abs(-------), abs(-----) + -------)
<             (      2      (    2  )     (   2 )         2)
<             (abs(H)       (   H   )     (  H  )   abs(H) )
<
>    simplify(");
<
<           (       (- 1 + H)       2     (1 + H)        2    )
<           (1 + abs(-------) abs(H)   abs(-----) abs(H)  + 1)
<           (       (     2 )             (    2)            )
<           (       (    H  )             (   H )            )
<        max(------------------------, ----------------------)
<           (               2                        2       )
<           (         abs(H)                   abs(H)        )
<

Web Source: http://www.math.uic.edu/~hanson/MAPLE/MapleLinearAlgebra.html