Skip to content

Commit 632f179

Browse files
committed
fix to jacobian HCAT
1 parent f6cda46 commit 632f179

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

src/calculus/Jacobian.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,26 @@ Jacobian(S::T2,x::AbstractArray) where {T,L,T2<:Scale{T,L}} = Scale(S.coeff,Jaco
3232
function Jacobian(H::DCAT{N,L,P1,P2},x) where {N,L,P1,P2}
3333
A = ()
3434
c = 0
35-
for i = 1:N
36-
A = length(H.idxD[i]) == 1 ?
37-
(A...,jacobian(H.A[i],x[c+1])) :
38-
(A...,jacobian(H.A[i],x[c+1:c+length(H.idxD[i])]))
39-
c += length(H.idxD[i])
35+
for (k, idx) in enumerate(H.idxD)
36+
if length(idx) == 1
37+
A = (A...,jacobian(H.A[k],x[idx]))
38+
else
39+
xx = ([x[i] for i in idx]...)
40+
A = (A...,jacobian(H.A[k],xx))
41+
end
4042
end
4143
DCAT(A,H.idxD,H.idxC)
4244
end
4345
#Jacobian of HCAT
4446
function Jacobian(H::HCAT{M,N,L,P,C},x::D) where {M,N,L,P,C,D}
4547
A = ()
46-
c = 0
47-
for i = 1:N
48-
A = length(H.idxs[i]) == 1 ?
49-
(A...,jacobian(H.A[i],x[c+1])) :
50-
(A...,jacobian(H.A[i],x[c+1:c+length(H.idxs[i])]))
51-
c += length(H.idxs[i])
48+
for (k, idx) in enumerate(H.idxs)
49+
if length(idx) == 1
50+
A = (A...,jacobian(H.A[k],x[idx]))
51+
else
52+
xx = ([x[i] for i in idx]...)
53+
A = (A...,jacobian(H.A[k],xx))
54+
end
5255
end
5356
HCAT(A,H.idxs,H.buf,M)
5457
end

src/syntax.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function getindex(A::AbstractOperator,idx...)
3535
elseif length(idx) == 1 && ndoms(A,2) == length(idx[1])
3636
return permute(A,idx[1])
3737
else
38-
error("cannot split operator of type $(typeof(H.A[i]))")
38+
error("cannot split operator of type $(typeof(A))")
3939
end
4040
end
4141

test/test_linear_operators_calculus.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ y1 = remove_displacement(opD)*(x1,x2,x3)
190190
y2 = (A1*x1, A2*x2, A3*x3)
191191
@test all(vecnorm.(y1 .- y2) .<= 1e-12)
192192

193-
#######################
194-
## test HCAT #######
195-
#######################
193+
########################
194+
### test HCAT #######
195+
########################
196196

197197
m, n1, n2 = 4, 7, 5
198198
A1 = randn(m, n1)
@@ -206,6 +206,12 @@ y1 = test_op(opH, (x1, x2), randn(m), verb)
206206
y2 = A1*x1 + A2*x2
207207
@test vecnorm(y1-y2) <= 1e-12
208208

209+
#permuatation
210+
p = [2;1]
211+
opHp = opH[p]
212+
y1 = test_op(opHp, (x2, x1), randn(m), verb)
213+
@test vecnorm(y1-y2) <= 1e-12
214+
209215
# test HCAT longer
210216

211217
m, n1, n2, n3 = 4, 7, 5, 6

test/test_nonlinear_operators_calculus.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ y, grad = test_NLop(op,x,r,verb)
1111
Y = 30*(A*x)
1212
@test vecnorm(Y-y) <1e-8
1313

14+
m = 3
15+
x = randn(m)
16+
r = randn(m)
17+
A = Pow(Float64,(m,),2)
18+
op = -A
19+
20+
y, grad = test_NLop(op,x,r,verb)
21+
22+
Y = -A*x
23+
@test vecnorm(Y-y) <1e-8
24+
1425
#testing DCAT
1526
n,m = 4,3
1627
x = (randn(n),randn(m))
@@ -38,6 +49,25 @@ y, grad = test_NLop(op,x,r,verb)
3849
Y = A*x[1]+B*x[2]
3950
@test vecnorm(Y-y) <1e-8
4051

52+
m,n = 3,5
53+
x = (randn(m),randn(n))
54+
r = randn(m)
55+
A= Sin(Float64,(m,))
56+
M = randn(m,n)
57+
B = MatrixOp(M)
58+
op = HCAT(A,B)
59+
60+
y, grad = test_NLop(op,x,r,verb)
61+
62+
Y = A*x[1]+M*x[2]
63+
@test vecnorm(Y-y) <1e-8
64+
65+
p = [2,1]
66+
opP = permute(op,p)
67+
J = Jacobian(opP,x[p])'
68+
println(size(J,1))
69+
y, grad = test_NLop(opP,x[p],r,verb)
70+
4171
#testing VCAT
4272
n,m = 4,3
4373
x = randn(m)

0 commit comments

Comments
 (0)