-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_space.py
285 lines (225 loc) · 6 KB
/
vector_space.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 2 14:52:37 2021
@author: angel
"""
from numpy import transpose, linalg, set_printoptions, full, copy, array, zeros, asarray, vstack, shape, ones, dot, arange, empty
from pylab import legend, xlabel, ylabel, title, plot, xlim, grid
from linear_algebra import transposition, rank, echelon_form, pivot_index, laplace, gauss_elimination
set_printoptions(precision=4)
def linear_independent(*v):
"""
OK
Determines which vectors are linearly independent.
The algorithm inserts every column-like vector in an emtpy matrix, then proceeds to reduce this
matrix to echelon form; indexes of pivot columns are searched and stored in a list by pivot_column()
Parameters
----------
*v : array
Arbitraty number of vectors.
Returns
-------
independent : array
Array of linear independent vectors.
"""
vectors = copy(v)
c = len(vectors)
r = len(vectors[0])
M = full((c, r), vectors)
M = transposition(M)
E = copy(M)
E = echelon_form(E)
pivot_row, pivot_column = pivot_index(E)
independent = []
for p in pivot_column:
independent.append(M[:,p])
print('Column index(es) of linear independet vectors: ', pivot_column)
return independent
def generator(*v):
"""
Determines whether the input vectors are a generating set.
Parameters
----------
*v : array
Arbitraty number of vectors.
Returns
-------
generators : array
Array of generating set vectors.
"""
vectors = copy(v)
c = len(vectors)
r = len(vectors[0])
M = full((c, r), vectors)
M = transposition(M)
E = copy(M)
E = echelon_form(E)
pivot_row, pivot_column = pivot_index(E)
generators = []
for p in pivot_row:
generators.append(M[p,:])
print('Row index(es) of generator vectors: ', pivot_row)
return generators
def base(*v):
"""
Determines whether the input vectors are base of a vector space.
Parameters
----------
*v : array
Arbitraty number of vectors.
Returns
-------
is_base: bool
base: bidimensional array
Base.
"""
tol=1e-10
vectors = copy(v)
c = len(vectors)
r = len(vectors[0])
is_base = False
base = full((c, r), vectors)
base = transposition(base)
if abs(c-r)<tol and laplace(base)!=0:
is_base = True
return is_base, base
else:
return is_base
def norm(A, s):
"""
Computes norm 1 or Inf of a generic matrix m-by-n.
Parameters
----------
A : bidimensional array
m-by-n matrix.
s : string
type of norm.
Returns
-------
norm : floar
computed norm.
"""
[m,n] = shape(A)
values = []
if s == '1':
for j in range(0, n):
sum = 0
for i in range(0, m):
sum = abs(A[i,j]) + sum
values.append(sum)
elif s == 'inf':
for i in range(0, m):
sum = 0
for j in range(0, n):
sum = abs(A[i,j]) + sum
values.append(sum)
norm = max(values)
return norm
def hilbert_matrix(n):
"""
Generates a n-by-n Hilbert matrix
Parameters
----------
n : int
Dimension of the matrix.
Returns
-------
H : bidimensional array
Hilber matrix.
"""
x = int(n)
H = empty((x, x))
for i in range(0, x):
for j in range(0, x):
H[i,j] = 1/((i+1)+(j+1)-1)
return H
def hilbert_system(n):
"""
Solve a linear system using its theoric solution e, n-by-1 array of ones.
Its matrix of coefficients is the Hilbert matrix.
Parameters
----------
n : int
Dimension of the Hilbert matrix.
Returns
-------
x : array
Vector n-by-1 of variables.
"""
H = hilbert_matrix(n)
e = ones((n))
b = dot(H, e)
x = linalg.solve(H, b)
#x = inverse_linear_system(H, b)
print('x:', x)
return x
def hilbert_cond(n):
"""
Computes the conditioning on the solution of a linear system whose matrix of
coefficients is an Hilbert matrix.
Parameters
----------
n : int
Starting from dimension 1, computes matrices until dimension n is reached.
Returns
-------
None.
"""
y = []
for i in range(1, n+1):
H = hilbert_matrix(i)
b = hilbert_system(i)
c = linalg.cond(vstack((H, b)))
print(c)
y.append(c)
x = arange(1, n+1, 1)
xlim(1, n)
#ylim(0, 10)
title('Condizionamento della matrice di Hilbert')
xlabel('Dimensione n')
ylabel('CONDIZIONAMENTO della soluzione')
grid(axis='both')
plot(x, y, label="Condizionamento")
legend(loc='upper left')
def hilbert_RE(n):
"""
Computer relative error on the theoric solution and the actual one of a Hilbert
linear system.
Parameters
----------
n : int
Starting from dimension 1, computes matrices until dimension n is reached.
Returns
-------
None.
"""
for i in range(1, n+1):
b = hilbert_system(i)
e = ones((i))
y = abs(b - e)/abs(e)
x = arange(1, n+1, 1)
xlim(1, n)
#ylim(0, 10)
title('Condizionamento della matrice di Hilbert')
xlabel('Dimensione n')
ylabel('ERRORE RELATIVO sulla soluzione')
grid(axis='both')
plot(x, y, label="Errore relativo")
legend(loc='upper left')
#TEST LINEAR INDEPENDENCE
#v1 = array([-1,2,-1,2,3], dtype=float)
#v2 = array([0,1,0,1,1], dtype=float)
#v3 = array([-2,2,-2,1,3], dtype=float)
#v4 = array([1,1,1,1,1], dtype=float)
#v5 = array([-2,0,1,2])
#v6 = array([-1,-1,2,-2])
#v7 = array([2,2,-2,-2])
#v1 = array([-1, 1, -1])
#v2 = array([-3, 3, -3])
#v3 = array([2, 1, 5])
#v4 = array([-3, 0, -6])
#pivot = [0,2]
#BASE
#e1 = array([1,0,0])
#e2 = array([0,1,0])
#e3 = array([0,0,1])