This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
pfi.jl
87 lines (73 loc) · 1.88 KB
/
pfi.jl
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
import Base.Ac_ldiv_B, Base.(\), Base.dot, Base.copy, Base.show
type PackedEtaVector
elts::Vector{Float64}
idx::Vector{Int64}
nnz::Int64
pivotalIndex::Int64
end
function PackedEtaVector(v::Vector{Float64},pivotalIndex)
n = length(v)
nnz = 0
for i in 1:n
if (abs(v[i]) > 1e-10)
nnz += 1
end
end
idx = Array(Int64,nnz)
elts = Array(Float64,nnz)
nnz = 0
pivot = 1/v[pivotalIndex]
v[pivotalIndex] = -1.
for i in 1:n
if (abs(v[i]) > 1e-10)
nnz += 1
idx[nnz] = i
elts[nnz] = -pivot*v[i]
end
end
PackedEtaVector(elts,idx,nnz,pivotalIndex)
end
function dot(eta::PackedEtaVector,v::Vector{Float64})
out = 0.
for j in 1:eta.nnz
out += v[eta.idx[j]]*eta.elts[j]
end
return out
end
function applyRowEta(eta::PackedEtaVector,v::Vector{Float64})
v[eta.pivotalIndex] = dot(eta,v)
end
function applyColumnEta(eta::PackedEtaVector,v::Vector{Float64})
pivot = v[eta.pivotalIndex]
v[eta.pivotalIndex] = 0.
for j in 1:eta.nnz
v[eta.idx[j]] += pivot*eta.elts[j]
end
end
type PFIManager
origFactor
npfi::Int
etas::Vector{PackedEtaVector}
end
function PFIManager(mat)
return PFIManager(lufact!(mat),0,Array(PackedEtaVector,1000))
end
function replaceColumn(pfi::PFIManager,tableauColumn::Vector{Float64},pivotalIndex)
@assert pfi.npfi < 1000
pfi.npfi += 1
pfi.etas[pfi.npfi] = PackedEtaVector(tableauColumn,pivotalIndex)
end
# these destroy the input, doesn't really matter
function (\)(pfi::PFIManager,v::Vector{Float64})
x = pfi.origFactor \ v
for i in 1:pfi.npfi
applyColumnEta(pfi.etas[i],x)
end
return x
end
function Ac_ldiv_B(pfi::PFIManager,v::Vector{Float64})
for i = pfi.npfi:-1:1
applyRowEta(pfi.etas[i],v)
end
return pfi.origFactor'\v
end