This notebook is licenced under CC BY-SA 4.0.

FriCAS Tutorial (Linear Algebra)

Ralf Hemmecke <ralf@hemmecke.org>

Sources at Github.

In [1]:
)set message type off
)set output algebra off
setFormat!(FormatMathJax)$JFriCASSupport
)set message type on
In [2]:
)version
Value = "FriCAS a9422d32eb6f6b03d98ac6adfc9bf05ec0f5e8bd compiled at Sa 03 Apr 2021 00:29:13 CEST"

Linear Algebra

In [2]:
solve([x + 3*y = 2, -2*x + 5*y = 1])
Out[2]:
\[ \left[\left[y=\frac{5}{11}, x=\frac{7}{11}\right]\right] \]

Solving linear equations with parameters is no problem.

In [3]:
solve([x + 3*y = 2, -2*x + 5*y = 3*z],[x,z])
Out[3]:
\[ \left[\left[x=-3\, y+2, z=\frac{11\, y-4}{3}\right]\right] \]

One can solve a system or linear equations in full generality.

In [4]:
solve([a1*x + b1*y = c1, a2*x + b2*y = c2], [x,y])
Out[4]:
\[ \left[\left[x=\frac{-b1\, c2+b2\, c1}{a1\, b2-a2\, b1}, y=\frac{a1\, c2-a2\, c1}{a1\, b2-a2\, b1}\right]\right] \]

One can, also specify matrix equations and solve those.

In [5]:
A := matrix [[1,2],[3,4]]
Out[5]:
\[ \begin{bmatrix}1&2\\3&4\end{bmatrix} \]

Note that the type of the result is a Union, since it cannot be guaranteed that the inverse matrix always exists.

In [6]:
inverse A
Out[6]:
\[ \begin{bmatrix}-2&1\\\frac{3}{2}&-\frac{1}{2}\end{bmatrix} \]
In [7]:
inverse matrix [[1,1],[1,1]]
Out[7]:
\[ \texttt{"failed"} \]
Union("failed",...)

The Matrix domain is a pool for matrices of arbitrary size.

In [8]:
B := matrix [[5,2,-1],[3,2,1]]
Out[8]:
\[ \begin{bmatrix}5&2&-1\\3&2&1\end{bmatrix} \]

It implements matrix multiplication.

In [9]:
A*B
Out[9]:
\[ \begin{bmatrix}11&6&1\\27&14&1\end{bmatrix} \]

However, multiplication is a partial operation that is only defined for compatible matrices.

In [11]:
B*A
   >> Error detected within library code:
   can't multiply matrices of incompatible dimensions
In [10]:
C := transpose B
Out[10]:
\[ \begin{bmatrix}5&3\\2&2\\-1&1\end{bmatrix} \]
In [11]:
B * C
Out[11]:
\[ \begin{bmatrix}30&18\\18&14\end{bmatrix} \]
In [12]:
B * C - 4 * A
Out[12]:
\[ \begin{bmatrix}26&10\\6&-2\end{bmatrix} \]

The symbol % denotes the last evaluated expression.

In [13]:
symmetric? %
Out[13]:
\[ \texttt{false} \]
In [14]:
C * B
Out[14]:
\[ \begin{bmatrix}34&16&-2\\16&8&0\\-2&0&2\end{bmatrix} \]
In [15]:
rank %
Out[15]:
\[ 2 \]
In [16]:
rowEchelon(C*B)
Out[16]:
\[ \begin{bmatrix}2&0&-2\\0&8&16\\0&0&0\end{bmatrix} \]

FriCAS interprets certain symbols according to the context they appear in.

In [17]:
(A + 1) - A
Out[17]:
\[ \begin{bmatrix}1&0\\0&1\end{bmatrix} \]
In [18]:
nullSpace B
Out[18]:
\[ \left[\left[1, -2, 1\right]\right] \]

The usual operations are available for matrices.

In [21]:
A
determinant A
nullSpace A
Out[21]:
\[ \begin{bmatrix}1&2\\3&4\end{bmatrix} \]
Out[21]:
\[ -2 \]
Out[21]:
\[ \left[\right] \]

The characteristic polynomial of a matrix is determined by the following commands.

In [23]:
id := diagonalMatrix [1,1]
q := determinant(x*id-A)
Out[23]:
\[ \begin{bmatrix}1&0\\0&1\end{bmatrix} \]
Out[23]:
\[ {x}^{2}-5\, x-2 \]

Or with a direct command.

In [24]:
p:=characteristicPolynomial(A,x)
Out[24]:
\[ {x}^{2}-5\, x-2 \]
In [25]:
radicalSolve(p, x)
Out[25]:
\[ \left[x=\frac{-\sqrt{33}+5}{2}, x=\frac{\sqrt{33}+5}{2}\right] \]

Cayley-Hamilton-Theorem: Every every square matrix over a commutative ring satisfies its own characteristic equation.

In [26]:
eval(p,x=A)
Out[26]:
\[ \begin{bmatrix}0&0\\0&0\end{bmatrix} \]

Closely related to matrices are vectors. Like the domain Matrix, the domain Vector does not have any size specifications.

The ==> notation is used to form macros.

In [28]:
Q ==> Fraction Integer
V ==> Vector Q
Out[28]:
Out[28]:
In [29]:
inverse(C*B +1)
Out[29]:
\[ \begin{bmatrix}\frac{9}{47}&-\frac{16}{47}&\frac{6}{47}\\-\frac{16}{47}&\frac{101}{141}&-\frac{32}{141}\\\frac{6}{47}&-\frac{32}{141}&\frac{59}{141}\end{bmatrix} \]
In [30]:
lv := % :: List(V)
Out[30]:
\[ \left[\left[\frac{9}{47}, -\frac{16}{47}, \frac{6}{47}\right], \left[-\frac{16}{47}, \frac{101}{141}, -\frac{32}{141}\right], \left[\frac{6}{47}, -\frac{32}{141}, \frac{59}{141}\right]\right] \]

The first vector in this list is accessed through the dot notation.

In [31]:
v1 := lv.1
Out[31]:
\[ \left[\frac{9}{47}, -\frac{16}{47}, \frac{6}{47}\right] \]

The cross product can only be taken for vectors of length 3.

In [32]:
vc := cross(lv.1, lv.2)
Out[32]:
\[ \left[-\frac{2}{141}, 0, \frac{1}{47}\right] \]

The cross product vector vc is orthogonal to the vectors lv.1 and lv.2, but not to lv.3.

In [33]:
[dot(lv.i, vc) for i in 1..3]
Out[33]:
\[ \left[0, 0, \frac{1}{141}\right] \]

Matrix and vector elements can be accessed and set by a dot notation.

In [34]:
A.(1,2)
Out[34]:
\[ 2 \]
In [35]:
A.(2,1) := -11
Out[35]:
\[ -11 \]
In [36]:
A
Out[36]:
\[ \begin{bmatrix}1&2\\-11&4\end{bmatrix} \]

The dot notation allows to get and set entries.

In [37]:
vc.3
Out[37]:
\[ \frac{1}{47} \]
In [38]:
vc.1 := -33/2
Out[38]:
\[ -\frac{33}{2} \]
In [39]:
vc
Out[39]:
\[ \left[-\frac{33}{2}, 0, \frac{1}{47}\right] \]

In fact, the above notation A.i and A.i := v is syntactic sugar for elt(A, i) and setelt(A, i, v).

In [42]:
elt(vc, 2)
setelt!(vc, 2, 7)
vc
Out[42]:
\[ 0 \]
Out[42]:
\[ 7 \]
Out[42]:
\[ \left[-\frac{33}{2}, 7, \frac{1}{47}\right] \]

Furthermore, A(i) and A i is just the same as A.i.

In [45]:
vc(2)
vc 2
vc.2
Out[45]:
\[ 7 \]
Out[45]:
\[ 7 \]
Out[45]:
\[ 7 \]

Note that the result of an operation depends on the type of the argument.

In [46]:
rowEchelon A
Out[46]:
\[ \begin{bmatrix}1&2\\0&26\end{bmatrix} \]
In [47]:
rowEchelon(A::Matrix(Fraction Integer))
Out[47]:
\[ \begin{bmatrix}1&0\\0&1\end{bmatrix} \]

Matrices over more complex structures can be dealt with in FriCAS.

In [48]:
P := matrix [[x^2+1, %i/x], [-1/(x+%i), 2*x^3]]
Out[48]:
\[ \begin{bmatrix}{x}^{2}+1&\frac{i}{x}\\-\frac{1}{x+i}&2\, {x}^{3}\end{bmatrix} \]
In [49]:
d := determinant P
Out[49]:
\[ \frac{2\, {x}^{7}+2\, i\, {x}^{6}+2\, {x}^{5}+2\, i\, {x}^{4}+i}{{x}^{2}+i\, x} \]

On can easily convert the result to other structures.

In [50]:
d :: Fraction(Complex Polynomial Integer)
Out[50]:
\[ \frac{2\, {x}^{7}+2\, {x}^{5}+\left(2\, {x}^{6}+2\, {x}^{4}+1\right)\, i}{{x}^{2}+x\, i} \]
In [51]:
d :: Complex(Fraction Polynomial Integer)
Out[51]:
\[ \frac{2\, {x}^{8}+4\, {x}^{6}+2\, {x}^{4}+1}{{x}^{3}+x}+\frac{1}{{x}^{2}+1}\, i \]

Since the coefficient ring of the matrix P forms a field, the row-echelon form is again the unit matrix.

In [52]:
rowEchelon P
Out[52]:
\[ \begin{bmatrix}1&0\\0&1\end{bmatrix} \]