1
- struct LinearCache{TA,Tb,Tu,Tp,Talg,Tc,Tr,Tl }
1
+ struct LinearCache{TA,Tb,Tu,Tp,Talg,Tc,Tl,Tr }
2
2
A:: TA
3
3
b:: Tb
4
4
u:: Tu
5
5
p:: Tp
6
6
alg:: Talg
7
- cacheval:: Tc
7
+ cacheval:: Tc # store alg cache here
8
8
isfresh:: Bool
9
- Pr:: Tr
10
9
Pl:: Tl
11
- # k::Tk # iteration count
10
+ Pr :: Tr
12
11
end
13
12
14
13
function set_A (cache, A) # and ! to function name
15
14
@set! cache. A = A
16
15
@set! cache. isfresh = true
16
+ return cache
17
17
end
18
18
19
19
function set_b (cache, b)
20
20
@set! cache. b = b
21
+ return cache
21
22
end
22
23
23
24
function set_u (cache, u)
24
25
@set! cache. u = u
26
+ return cache
25
27
end
26
28
27
29
function set_p (cache, p)
28
30
@set! cache. p = p
29
31
# @set! cache.isfresh = true
32
+ return cache
30
33
end
31
34
32
- function set_cacheval (cache:: LinearCache , alg )
35
+ function set_cacheval (cache, alg_cache )
33
36
if cache. isfresh
34
- @set! cache. cacheval = alg
37
+ @set! cache. cacheval = alg_cache
35
38
@set! cache. isfresh = false
36
39
end
37
40
return cache
38
41
end
39
42
40
- function SciMLBase. init (
41
- prob:: LinearProblem ,
42
- alg,
43
- args... ;
44
- alias_A = false ,
45
- alias_b = false ,
46
- kwargs... ,
47
- )
43
+ # function init_cacheval(cacheval, alg::SciMLLinearSolveAlgorithm)
44
+ #
45
+ # return
46
+ # end
47
+
48
+ function SciMLBase. init (prob:: LinearProblem , alg, args... ;
49
+ alias_A = false , alias_b = false ,
50
+ kwargs... ,
51
+ )
48
52
@unpack A, b, u0, p = prob
49
53
50
54
if u0 == nothing
@@ -58,8 +62,8 @@ function SciMLBase.init(
58
62
fact = nothing
59
63
Tfact = Any
60
64
end
61
- Pr = LinearAlgebra. I
62
65
Pl = LinearAlgebra. I
66
+ Pr = LinearAlgebra. I
63
67
64
68
A = alias_A ? A : deepcopy (A)
65
69
b = alias_b ? b : deepcopy (b)
@@ -71,8 +75,8 @@ function SciMLBase.init(
71
75
typeof (p),
72
76
typeof (alg),
73
77
Tfact,
74
- typeof (Pr),
75
78
typeof (Pl),
79
+ typeof (Pr),
76
80
}(
77
81
A,
78
82
b,
@@ -81,8 +85,8 @@ function SciMLBase.init(
81
85
alg,
82
86
fact,
83
87
true ,
84
- Pr,
85
88
Pl,
89
+ Pr,
86
90
)
87
91
return cache
88
92
end
@@ -92,26 +96,35 @@ SciMLBase.solve(prob::LinearProblem, alg, args...; kwargs...) =
92
96
93
97
SciMLBase. solve (cache) = solve (cache, cache. alg)
94
98
99
+ function (alg:: SciMLLinearSolveAlgorithm )(prob:: LinearProblem ,args... ;
100
+ u0= nothing ,kwargs... )
101
+ x = solve (prob, alg, args... ; kwargs... )
102
+ return x
103
+ end
104
+
95
105
function (alg:: SciMLLinearSolveAlgorithm )(x,A,b,args... ;u0= nothing ,kwargs... )
96
106
prob = LinearProblem (A,b;u0= x)
97
- x = solve (prob,alg, args... ;kwargs... )
107
+ x = alg (prob, args... ; kwargs... )
98
108
return x
99
109
end
100
110
101
- # how to initialize cahce?
102
-
103
- # use the same cache to solve multiple linear problems
104
- function (cache:: LinearCache )(x,A,b,args... ;u0= nothing ,kwargs... )
105
- set_A (cache, A)
106
- set_b (cache, b)
111
+ function (cache:: LinearCache )(prob:: LinearProblem ,args... ;u0= nothing ,kwargs... )
107
112
108
- if u0 == nothing
109
- x = zero (x)
110
- else
111
- x = u0
113
+ if prob. u0 == nothing
114
+ prob. u0 = zero (x)
112
115
end
113
- set_u (cache, x)
114
116
115
- x = solve (cache)
117
+ cache = set_A (cache, prob. A)
118
+ cache = set_b (cache, prob. b)
119
+ cache = set_u (cache, prob. u0)
120
+
121
+ x = solve (cache,args... ;kwargs... )
122
+ return x
123
+ end
124
+
125
+ function (cache:: LinearCache )(x,A,b,args... ;u0= nothing ,kwargs... )
126
+
127
+ prob = LinearProblem (A,b;u0= x)
128
+ x = cache (prob, args... ; kwargs... )
116
129
return x
117
130
end
0 commit comments