-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.jl
More file actions
67 lines (52 loc) · 1.73 KB
/
util.jl
File metadata and controls
67 lines (52 loc) · 1.73 KB
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
# #
# #
# #
# A função calcula um vetor ortogonal ao vetor passado como parametro #
function orto(vector::Array{Float64,1})
# Assert para checar se o vetor vector não é o vetor zero!! #
#TODO#
# Geramos um novo vetor Y com valores aleatorios#
tam = size(vector)
y = rand(tam)*mean(vector)
tmp = 0.0
for i = 1:tam[1] - 1
tmp = tmp + vector[i]*y[i]
end
# Retornando Y #
y[tam[1]] = - tmp/vector[tam[1]]
return y
end
# A função recebe uma matriz A e um vetor B #
# .... e retornar o resultado da operação #
function solve(matrix::Array{Float64,2}, rhs::Array{Float64,1})
# Assert para checar se primeiro matriz é não singular #
# Segundo Assert checar se o matrix e rhs tem o mesmo número de linhas #
#TODO#
tmp = inv(matrix)
result = tmp*rhs
end
# Recebe a matriz A e o vetor b e tenta reproduzir um sistema com condicinamento melhor #
# trocando a linha(line) por sua ortogonal, e retornando um sistema identico, de mesma solução #
function improve(matrix::Array{Float64,2}, rhs::Array{Float64,1}, line::Int64)
# Assert para checar se a linha está dentro do esperado!!!! #
#TODO#
A_rows, A_cols = size(matrix)
# Criamos uma nova matrix que iremos trocar a linha #line #
new_line = zeros(A_cols)
new_matrix = matrix'
# Copiando vetor que será trocado #
for i = 1:A_cols
new_line[i] = new_matrix[(line - 1)*A_cols + i]
end
# Achando vetor ortogonal #
new_line = orto(new_line)
# Copiando vetor mudado #
for i = 1:A_cols
new_matrix[(line - 1)*A_cols + i]= new_line[i]
end
new_matrix = new_matrix'
# Recalculando RHS(Right-hand-side) #
new_rhs = new_matrix*inv(matrix)*rhs
# Retornando resultados #
return new_matrix, new_rhs
end