forked from JuliaSmoothOptimizers/JSOTutorials.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jmd
More file actions
268 lines (219 loc) · 6.56 KB
/
index.jmd
File metadata and controls
268 lines (219 loc) · 6.56 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
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
---
title: "Introduction to Linear Operators"
tags: ["linear-algebra", "linear-operators"]
author: "Geoffroy Leconte and Dominique Orban"
---
[LinearOperators.jl](https://jso.dev/LinearOperators.jl/stable) is a package for matrix-like operators. Linear operators are defined by how they act on a vector, which is useful in a variety of situations where you don't want to materialize the matrix.
\toc
This section of the documentation describes a few uses of LinearOperators.
## Using matrices
Operators may be defined from matrices and combined using the usual operations, but the result is deferred until the operator is applied.
```julia
using LinearOperators, Random, SparseArrays
Random.seed!(0)
A1 = rand(5,7)
A2 = sprand(7,3,.3)
op1 = LinearOperator(A1)
op2 = LinearOperator(A2)
op = op1 * op2 # Does not form A1 * A2
x = rand(3)
y = op * x
```
## Inverse
Operators may be defined to represent (approximate) inverses.
```julia
using LinearAlgebra
A = rand(5,5)
A = A' * A
op = opCholesky(A) # Use, e.g., as a preconditioner
v = rand(5)
norm(A \ v - op * v) / norm(v)
```
In this example, the Cholesky factor is computed only once and can be used many times transparently.
## mul!
It is often useful to reuse the memory used by the operator.
For that reason, we can use `mul!` on operators as if we were using matrices
using preallocated vectors:
```julia
using LinearOperators, LinearAlgebra # hide
m, n = 50, 30
A = rand(m, n) + im * rand(m, n)
op = LinearOperator(A)
v = rand(n)
res = zeros(eltype(A), m)
res2 = copy(res)
mul!(res2, op, v) # compile 3-args mul!
al = @allocated mul!(res, op, v) # op * v, store result in res
println("Allocation of LinearOperator mul! product = $al")
v = rand(n)
α, β = 2.0, 3.0
mul!(res2, op, v, α, β) # compile 5-args mul!
al = @allocated mul!(res, op, v, α, β) # α * op * v + β * res, store result in res
println("Allocation of LinearOperator mul! product = $al")
```
## Using functions
Operators may be defined from functions. They have to be based on the 5-arguments `mul!` function.
In the example below, the transposed isn't defined, but it may be inferred from the conjugate transposed.
Missing operations are represented as `nothing`.
You will have deal with cases where `β == 0` and `β != 0` separately because `*` will allocate an uninitialized `res` vector that
may contain `NaN` values, and `0 * NaN == NaN`.
```julia
using FFTW
function mulfft!(res, v, α, β)
if β == 0
res .= α .* fft(v)
else
res .= α .* fft(v) .+ β .* res
end
end
function mulifft!(res, w, α, β)
if β == 0
res .= α .* ifft(w)
else
res .= α .* ifft(w) .+ β .* res
end
end
dft = LinearOperator(ComplexF64, 10, 10, false, false,
mulfft!,
nothing, # will be inferred
mulifft!)
x = rand(10)
y = dft * x
norm(dft' * y - x) # DFT is a unitary operator
```
```julia
transpose(dft) * y
```
Another example:
```julia
function customfunc!(res, v, α, β)
if β == 0
res[1] = (v[1] + v[2]) * α
res[2] = v[2] * α
else
res[1] = (v[1] + v[2]) * α + res[1] * β
res[2] = v[2] * α + res[2] * β
end
end
function tcustomfunc!(res, w, α, β)
if β == 0
res[1] = w[1] * α
res[2] = (w[1] + w[2]) * α
else
res[1] = w[1] * α + res[1] * β
res[2] = (w[1] + w[2]) * α + res[2] * β
end
end
op = LinearOperator(Float64, 2, 2, false, false,
customfunc!,
nothing,
tcustomfunc!)
```
Operators can also be defined with the 3-args `mul!` function:
```julia
op2 = LinearOperator(Float64, 2, 2, false, false,
(res, v) -> customfunc!(res, v, 1.0, 0.),
nothing,
(res, w) -> tcustomfunc!(res, w, 1.0, 0.))
```
When using the 5-args `mul!` with the above operator, some vectors will be allocated (only at the first call):
```julia
res, a = zeros(2), rand(2)
mul!(res, op2, a) # compile
println("allocations 1st call = ", @allocated mul!(res, op2, a, 2.0, 3.0))
println("allocations 2nd call = ", @allocated mul!(res, op2, a, 2.0, 3.0))
```
Make sure that the type passed to `LinearOperator` is correct, otherwise errors may occur.
```julia
using LinearOperators, FFTW # hide
dft = LinearOperator(Float64, 10, 10, false, false,
mulfft!,
nothing,
mulifft!)
v = rand(10)
println("eltype(dft) = $(eltype(dft))")
println("eltype(v) = $(eltype(v))")
```
```julia
try
dft * v # ERROR: expected Vector{Float64}
catch ex
println("ex = $ex")
end
```
```julia
try
Matrix(dft) # ERROR: tried to create a Matrix of Float64
catch ex
println("ex = $ex")
end
# Using external modules
```
It is possible to use certain modules made for matrices that do not need to access specific elements of their input matrices, and only use operations implemented within LinearOperators, such as `mul!`, `*`, `+`, ...
For example, we show the solution of a linear system using [`Krylov.jl`](https://github.com/JuliaSmoothOptimizers/Krylov.jl):
```julia
using Krylov
A = rand(5, 5)
opA = LinearOperator(A)
opAAT = opA + opA'
b = rand(5)
(x, stats) = minres(opAAT, b)
norm(b - opAAT * x)
```
## Limited memory BFGS and SR1
Another useful operator is the Limited-Memory BFGS in forward and inverse form.
```julia
B = LBFGSOperator(20)
H = InverseLBFGSOperator(20)
r = 0.0
for i = 1:100
global r
s = rand(20)
y = rand(20)
push!(B, s, y)
push!(H, s, y)
r += norm(B * H * s - s)
end
r
```
There is also a Limited-Memory SR1 operator for which only the forward form is implemented.
Note that the SR1 operator can be indefinite; therefore, its inverse form is less relevant than for the BFGS approximation.
For this reason, the inverse form is not implemented for the SR1 operator.
## Restriction, extension and slices
The restriction operator restricts a vector to a set of indices.
```julia
v = collect(1:5)
R = opRestriction([2;5], 5)
R * v
```
Notice that it corresponds to a matrix with rows of the identity given by the indices.
```julia
Matrix(R)
```
The extension operator is the transpose of the restriction. It extends a vector with zeros.
```julia
v = collect(1:2)
E = opExtension([2;5], 5)
E * v
```
With these operators, we define the slices of an operator `op`.
```julia
A = rand(5,5)
opA = LinearOperator(A)
I = [1;3;5]
J = 2:4
A[I,J] * ones(3)
```
```julia
opRestriction(I, 5) * opA * opExtension(J, 5) * ones(3)
```
A main difference with matrices, is that slices **do not** return vectors nor numbers.
```julia
opA[1,:] * ones(5)
```
```julia
opA[:,1] * ones(1)
```
```julia
opA[1,1] * ones(1)
```