-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFriCAS-LinearAlgebra.input
187 lines (115 loc) · 3.55 KB
/
FriCAS-LinearAlgebra.input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
-- ---
-- jupyter:
-- jupytext:
-- formats: ipynb,input:light
-- text_representation:
-- extension: .input
-- format_name: light
-- format_version: '1.5'
-- jupytext_version: 1.10.0
-- kernelspec:
-- display_name: FriCAS
-- language: spad
-- name: jfricas
-- ---
-- This notebook is licenced under
-- [CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/).
--
-- # FriCAS Tutorial (Linear Algebra)
--
-- ## Ralf Hemmecke <[[email protected]](mailto:[email protected])>
--
-- Sources at [Github](http://github.com/fricas/fricas-notebooks/).
)read init.input )quiet
-- ## Linear Algebra
solve([x + 3*y = 2, -2*x + 5*y = 1])
-- Solving linear equations with parameters is no problem.
solve([x + 3*y = 2, -2*x + 5*y = 3*z],[x,z])
-- One can solve a system or linear equations in full generality.
solve([a1*x + b1*y = c1, a2*x + b2*y = c2], [x,y])
-- One can, also specify matrix equations and solve those.
A := matrix [[1,2],[3,4]]
-- Note that the type of the result is a `Union`, since it cannot
-- be guaranteed that the inverse matrix always exists.
inverse A
inverse matrix [[1,1],[1,1]]
-- The `Matrix` domain is a pool for matrices of arbitrary size.
B := matrix [[5,2,-1],[3,2,1]]
-- It implements matrix multiplication.
A*B
-- However, multiplication is a partial operation that is only
-- defined for compatible matrices.
B*A
C := transpose B
B * C
B * C - 4 * A
-- The symbol `%` denotes the last evaluated expression.
symmetric? %
C * B
rank %
rowEchelon(C*B)
-- FriCAS interprets certain symbols according to the
-- context they appear in.
(A + 1) - A
nullSpace B
-- The usual operations are available for matrices.
A
determinant A
nullSpace A
-- The characteristic polynomial of a matrix is determined
-- by the following commands.
id := diagonalMatrix [1,1]
q := determinant(x*id-A)
-- Or with a direct command.
p:=characteristicPolynomial(A,x)
radicalSolve(p, x)
-- **Cayley-Hamilton-Theorem**:
-- Every every square matrix over a commutative ring
-- satisfies its own characteristic equation.
eval(p,x=A)
-- 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.
Q ==> Fraction Integer
V ==> Vector Q
inverse(C*B +1)
lv := % :: List(V)
-- The first vector in this list is accessed through the dot notation.
v1 := lv.1
-- The cross product can only be taken for vectors of length 3.
vc := cross(lv.1, lv.2)
-- The cross product vector `vc` is orthogonal to the vectors
-- `lv.1` and `lv.2`, but not to `lv.3`.
[dot(lv.i, vc) for i in 1..3]
-- Matrix and vector elements can be accessed and set by a dot notation.
A.(1,2)
A.(2,1) := -11
A
-- The dot notation allows to get and set entries.
vc.3
vc.1 := -33/2
vc
-- In fact, the above notation `A.i` and `A.i := v` is syntactic
-- sugar for `elt(A, i)` and `setelt(A, i, v)`.
elt(vc, 2)
setelt!(vc, 2, 7)
vc
-- Furthermore, `A(i)` and `A i` is just the same as `A.i`.
vc(2)
vc 2
vc.2
-- Note that the result of an operation depends on the type
-- of the argument.
rowEchelon A
rowEchelon(A::Matrix(Fraction Integer))
-- Matrices over more complex structures can be dealt with in FriCAS.
P := matrix [[x^2+1, %i/x], [-1/(x+%i), 2*x^3]]
d := determinant P
-- On can easily convert the result to other structures.
d :: Fraction(Complex Polynomial Integer)
d :: Complex(Fraction Polynomial Integer)
-- Since the coefficient ring of the matrix `P` forms a field,
-- the row-echelon form is again the unit matrix.
rowEchelon P