-
Notifications
You must be signed in to change notification settings - Fork 98
/
NonlinearOperators.jl
172 lines (145 loc) · 3.82 KB
/
NonlinearOperators.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
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
"""
abstract type NonlinearOperator <: GridapType end
- [`residual!(b::AbstractVector,op::NonlinearOperator,x::AbstractVector)`](@ref)
- [`jacobian!(A::AbstractMatrix,op::NonlinearOperator,x::AbstractVector)`](@ref)
- [`zero_initial_guess(op::NonlinearOperator)`](@ref)
- [`allocate_residual(op::NonlinearOperator,x::AbstractVector)`](@ref)
- [`allocate_jacobian(op::NonlinearOperator,x::AbstractVector)`](@ref)
"""
abstract type NonlinearOperator <: GridapType end
"""
residual!(b::AbstractVector,op::NonlinearOperator,x::AbstractVector)
"""
function residual!(b::AbstractVector,op::NonlinearOperator,x::AbstractVector)
@abstractmethod
end
"""
residual(op::NonlinearOperator,x::AbstractVector)
"""
function residual(op::NonlinearOperator,x::AbstractVector)
b = allocate_residual(op,x)
residual!(b,op,x)
b
end
"""
jacobian!(A::AbstractMatrix,op::NonlinearOperator,x::AbstractVector)
"""
function jacobian!(A::AbstractMatrix,op::NonlinearOperator,x::AbstractVector)
@abstractmethod
end
"""
jacobian(op::NonlinearOperator,x::AbstractVector)
"""
function jacobian(op::NonlinearOperator,x::AbstractVector)
A = allocate_jacobian(op,x)
jacobian!(A,op,x)
A
end
"""
residual_and_jacobian!(
b::AbstractVector, A::AbstractMatrix,
op::NonlinearOperator, x::AbstractVector)
"""
function residual_and_jacobian!(
b::AbstractVector, A::AbstractMatrix, op::NonlinearOperator, x::AbstractVector)
residual!(b,op,x)
jacobian!(A,op,x)
(b, A)
end
"""
residual_and_jacobian(op::NonlinearOperator,x::AbstractVector)
"""
function residual_and_jacobian(op::NonlinearOperator,x::AbstractVector)
b, A = allocate_residual_and_jacobian(op,x)
residual_and_jacobian!(b,A,op,x)
(b, A)
end
function hessian end
function hessian! end
"""
zero_initial_guess(op::NonlinearOperator)
"""
function zero_initial_guess(op::NonlinearOperator)
@abstractmethod
end
"""
allocate_residual(op::NonlinearOperator,x::AbstractVector)
"""
function allocate_residual(op::NonlinearOperator,x::AbstractVector)
@abstractmethod
end
"""
allocate_jacobian(op::NonlinearOperator,x::AbstractVector)
"""
function allocate_jacobian(op::NonlinearOperator,x::AbstractVector)
@abstractmethod
end
"""
allocate_residual_and_jacobian(op::NonlinearOperator,x::AbstractVector)
"""
function allocate_residual_and_jacobian(op::NonlinearOperator,x::AbstractVector)
b = allocate_residual(op,x)
A = allocate_jacobian(op,x)
(b,A)
end
"""
test_nonlinear_operator(
op::NonlinearOperator,
x::AbstractVector,
b::AbstractVector,
pred=isapprox;
jac=nothing)
"""
function test_nonlinear_operator(
op::NonlinearOperator,
x::AbstractVector,
b::AbstractVector,
pred=isapprox;
jac=nothing)
b1 = allocate_residual(op,x)
residual!(b1,op,x)
@test pred(b,b1)
x0 = zero_initial_guess(op)
if jac != nothing
nrows, ncols = size(jac)
A = allocate_jacobian(op,x)
jacobian!(A,op,x)
@test pred(A,jac)
residual_and_jacobian!(b1,A,op,x)
@test pred(A,jac)
@test pred(b,b1)
b1, A = residual_and_jacobian(op,x)
@test pred(A,jac)
@test pred(b,b1)
end
end
# Mock for testing purposes
struct NonlinearOperatorMock <: NonlinearOperator end
function residual!(b::AbstractVector,::NonlinearOperatorMock,x::AbstractVector)
b[1] = (x[1]-2)*(x[1]-1)
b[2] = x[2]-3
b
end
function jacobian!(A::AbstractMatrix,::NonlinearOperatorMock,x::AbstractVector)
A[1,1] = (x[1]-1) + (x[1]-2)
A[1,2] = 0
A[2,1] = 0
A[2,2] = 1
A
end
function zero_initial_guess(op::NonlinearOperatorMock)
x = allocate_residual(op,Float64[])
fill!(x,zero(eltype(x)))
x
end
function allocate_residual(op::NonlinearOperatorMock,x::AbstractVector)
T = eltype(x)
n = 2
similar(x,T,n)
end
function allocate_jacobian(op::NonlinearOperatorMock,x::AbstractVector)
T = eltype(x)
n = 2
m = 2
zeros(T,n,m)
end