@@ -4,47 +4,6 @@ struct Inplace <: HomotopySystemVariant end
4
4
struct OutOfPlace <: HomotopySystemVariant end
5
5
struct Scalar <: HomotopySystemVariant end
6
6
7
- """
8
- $(TYPEDEF)
9
-
10
- A simple struct that wraps a polynomial function which takes complex input and returns
11
- complex output in a form that supports automatic differentiation. If the wrapped
12
- function if ``f: \\ mathbb{C}^n \\ rightarrow \\ mathbb{C}^n`` then it is assumed
13
- the input arrays are real-valued and have length ``2n``. They are `reinterpret`ed
14
- into complex arrays and passed into the function. This struct has an in-place signature
15
- regardless of the signature of ``f``.
16
- """
17
- @concrete struct ComplexJacobianWrapper{variant <: HomotopySystemVariant }
18
- f
19
- end
20
-
21
- function (cjw:: ComplexJacobianWrapper{Inplace} )(
22
- u:: AbstractVector{T} , x:: AbstractVector{T} , p) where {T}
23
- x = reinterpret (Complex{T}, x)
24
- u = reinterpret (Complex{T}, u)
25
- cjw. f (u, x, p)
26
- u = parent (u)
27
- return u
28
- end
29
-
30
- function (cjw:: ComplexJacobianWrapper{OutOfPlace} )(
31
- u:: AbstractVector{T} , x:: AbstractVector{T} , p) where {T}
32
- x = reinterpret (Complex{T}, x)
33
- u_tmp = cjw. f (x, p)
34
- u_tmp = reinterpret (T, u_tmp)
35
- copyto! (u, u_tmp)
36
- return u
37
- end
38
-
39
- function (cjw:: ComplexJacobianWrapper{Scalar} )(
40
- u:: AbstractVector{T} , x:: AbstractVector{T} , p) where {T}
41
- x = reinterpret (Complex{T}, x)
42
- u_tmp = cjw. f (x[1 ], p)
43
- u[1 ] = real (u_tmp)
44
- u[2 ] = imag (u_tmp)
45
- return u
46
- end
47
-
48
7
"""
49
8
$(TYPEDEF)
50
9
@@ -62,34 +21,24 @@ $(FIELDS)
62
21
"""
63
22
f
64
23
"""
65
- The jacobian function, if provided to the `NonlinearProblem` being solved. Otherwise,
66
- a `ComplexJacobianWrapper` wrapping `f` used for automatic differentiation.
24
+ A function which calculates both the polynomial and the jacobian. Must be a function
25
+ of the form `f(u, U, x, p)` where `x` is the current unknowns and `p` is the parameter
26
+ object, writing the value of the polynomial to `u` and the jacobian to `U`. Must be able
27
+ to handle complex `x`.
67
28
"""
68
29
jac
69
30
"""
70
31
The parameter object.
71
32
"""
72
33
p
73
34
"""
74
- The ADType for automatic differentiation.
75
- """
76
- autodiff
77
- """
78
- The result from `DifferentiationInterface.prepare_jacobian`.
79
- """
80
- prep
81
- """
82
35
HomotopyContinuation.jl's symbolic variables for the system.
83
36
"""
84
37
vars
85
38
"""
86
39
The `TaylorDiff.TaylorScalar` objects used to compute the taylor series of `f`.
87
40
"""
88
41
taylorvars
89
- """
90
- Preallocated intermediate buffers used for calculating the jacobian.
91
- """
92
- jacobian_buffers
93
42
end
94
43
95
44
Base. size (sys:: HomotopySystemWrapper ) = (length (sys. vars), length (sys. vars))
@@ -112,54 +61,11 @@ function HC.ModelKit.evaluate!(u, sys::HomotopySystemWrapper{Scalar}, x, p = not
112
61
end
113
62
114
63
function HC. ModelKit. evaluate_and_jacobian! (
115
- u, U, sys:: HomotopySystemWrapper{Inplace} , x, p = nothing )
116
- p = sys. p
117
- sys. f (u, x, p)
118
- sys. jac (U, x, p)
119
- return u, U
120
- end
121
-
122
- function HC. ModelKit. evaluate_and_jacobian! (
123
- u, U, sys:: HomotopySystemWrapper{OutOfPlace} , x, p = nothing )
124
- p = sys. p
125
- u_tmp = sys. f (x, p)
126
- copyto! (u, u_tmp)
127
- j_tmp = sys. jac (x, p)
128
- copyto! (U, j_tmp)
64
+ u, U, sys:: HomotopySystemWrapper , x, p = nothing )
65
+ sys. jac (u, U, x, sys. p)
129
66
return u, U
130
67
end
131
68
132
- function HC. ModelKit. evaluate_and_jacobian! (
133
- u, U, sys:: HomotopySystemWrapper{Scalar} , x, p = nothing )
134
- p = sys. p
135
- u[1 ] = sys. f (x[1 ], p)
136
- U[1 ] = sys. jac (x[1 ], p)
137
- return u, U
138
- end
139
-
140
- for V in (Inplace, OutOfPlace, Scalar)
141
- @eval function HC. ModelKit. evaluate_and_jacobian! (
142
- u, U, sys:: HomotopySystemWrapper{$V, F, J} , x,
143
- p = nothing ) where {F, J <: ComplexJacobianWrapper }
144
- p = sys. p
145
- U_tmp = sys. jacobian_buffers
146
- x = reinterpret (Float64, x)
147
- u = reinterpret (Float64, u)
148
- DI. value_and_jacobian! (sys. jac, u, U_tmp, sys. prep, sys. autodiff, x, DI. Constant (p))
149
- U = reinterpret (Float64, U)
150
- @inbounds for j in axes (U, 2 )
151
- jj = 2 j - 1
152
- for i in axes (U, 1 )
153
- U[i, j] = U_tmp[i, jj]
154
- end
155
- end
156
- u = parent (u)
157
- U = parent (U)
158
-
159
- return u, U
160
- end
161
- end
162
-
163
69
function update_taylorvars_from_taylorvector! (
164
70
vars, x:: HC.ModelKit.TaylorVector )
165
71
for i in eachindex (x)
185
91
186
92
function check_taylor_equality (vars, x:: HC.ModelKit.TaylorVector )
187
93
for i in eachindex (x)
188
- TaylorDiff. flatten (vars[2 i- 1 ]) == map (real, x[i]) || return false
94
+ TaylorDiff. flatten (vars[2 i - 1 ]) == map (real, x[i]) || return false
189
95
TaylorDiff. flatten (vars[2 i]) == map (imag, x[i]) || return false
190
96
end
191
97
return true
192
98
end
193
99
function check_taylor_equality (vars, x:: AbstractVector )
194
100
for i in eachindex (x)
195
- TaylorDiff. value (vars[2 i- 1 ]) != real (x[i]) && return false
101
+ TaylorDiff. value (vars[2 i - 1 ]) != real (x[i]) && return false
196
102
TaylorDiff. value (vars[2 i]) != imag (x[i]) && return false
197
103
end
198
104
return true
@@ -212,7 +118,7 @@ function update_maybe_taylorvector_from_taylorvars!(
212
118
for i in eachindex (vars)
213
119
rval = TaylorDiff. flatten (real (buffer[i]))
214
120
ival = TaylorDiff. flatten (imag (buffer[i]))
215
- u[i] = ntuple (i -> rval[i] + im * ival[i], Val (length (rval)))
121
+ u[i] = ntuple (i -> rval[i] + im * ival[i], Val (length (rval)))
216
122
end
217
123
end
218
124
0 commit comments