Skip to content

Commit 85a9331

Browse files
committed
fixed blockvecdot for complex, added complex var test NonLinearCompose
1 parent cf8300c commit 85a9331

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/utilities/block.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ blockset!(y::Tuple, x) = blockset!.(y, x)
5454
blockset!(y::AbstractArray, x) = (y .= x)
5555

5656
blockvecdot(x::T, y::T) where {T <: Tuple} = sum(blockvecdot.(x,y))
57-
blockvecdot(x::AbstractArray{R}, y::AbstractArray{R}) where {R <: Number} = vecdot(x, y)
57+
blockvecdot(x::AbstractArray{R}, y::AbstractArray{R}) where {R <: Number} = real(vecdot(x, y))
58+
# inner product must be always real see section 4.2 of TFOCS manual
5859

5960
blockzeros(t::Tuple, s::Tuple) = blockzeros.(t, s)
6061
blockzeros(t::Type, n::NTuple{N, Integer} where {N}) = zeros(t, n)

test/test_block.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ blockset!(yb,xb)
4848

4949
z = blockvecdot(x,x2)
5050
zb = blockvecdot(xb,x2b)
51-
@test z == vecdot(x,x2)
52-
@test zb == vecdot(xb[1],x2b[1])+vecdot(xb[2],x2b[2])+vecdot(xb[3],x2b[3])
51+
@test z == real(vecdot(x,x2))
52+
@test zb == real(vecdot(xb[1],x2b[1])+vecdot(xb[2],x2b[2])+vecdot(xb[3],x2b[3]))
5353

5454
y = blockzeros(x)
5555
yb = blockzeros(xb)

test/test_nonlinear_operators_calculus.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ y, grad = test_NLop(op,x,r,verb)
180180
Y = A*x+opB*x
181181
@test vecnorm(Y-y) <1e-8
182182

183-
#testing NonLinearCompose
183+
## testing NonLinearCompose
184184

185185
#with vectors
186186
n,m = 3,4
@@ -208,7 +208,25 @@ Y = A*x[1]*B*x[2]
208208
@test norm(Y - y) <= 1e-12
209209

210210
#further test on gradient with analytical formulas
211-
grad2 = ((B*x[2])*(A'*r)')', B'*(A*x[1])'*r
211+
grad2 = (A'*r)*(B*x[2])', B'*(A*x[1])'*r
212+
@test vecnorm(grad[1]-grad2[1]) <1e-7
213+
@test vecnorm(grad[2]-grad2[2]) <1e-7
214+
215+
#with complex matrices
216+
l,m1,m2,n1,n2 = 2,3,4,5,6
217+
x = (randn(m1,m2)+im*randn(m1,m2),randn(n1,n2)+im*randn(n1,n2))
218+
A = randn(l,m1) +im*randn(l,m1)
219+
B = randn(m2,n1)+im*randn(m2,n1)
220+
r = randn(l,n2) +im*randn(l,n2)
221+
222+
P = NonLinearCompose( MatrixOp(A,m2), MatrixOp(B,n2) )
223+
y, grad = test_NLop(P,x,r,verb)
224+
225+
Y = A*x[1]*B*x[2]
226+
@test norm(Y - y) <= 1e-12
227+
228+
##test on gradient with analytical formulas
229+
grad2 = (A'*r)*(B*x[2])', B'*(A*x[1])'*r
212230
@test vecnorm(grad[1]-grad2[1]) <1e-7
213231
@test vecnorm(grad[2]-grad2[2]) <1e-7
214232

test/utils.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function test_op(A::AbstractOperator, x, y, verb::Bool = false)
2626
return Ax
2727
end
2828

29-
########### Test for LinearOperators
29+
########### Test for NonLinearOperators
3030
function test_NLop(A::AbstractOperator, x, y, verb::Bool = false)
3131

3232
verb && (println(),println(A))
@@ -54,9 +54,11 @@ function test_NLop(A::AbstractOperator, x, y, verb::Bool = false)
5454

5555
@test AbstractOperators.blockvecnorm(grad .- grad2) < 1e-8
5656

57-
grad3 = gradient_fd(A,Ax,x,y) #calculate gradient using finite differences
57+
if all(isreal.(grad)) # currently finite difference gradient not working with complex variables
58+
grad3 = gradient_fd(A,Ax,x,y) #calculate gradient using finite differences
5859

59-
@test AbstractOperators.blockvecnorm(grad .- grad3) < 1e-4
60+
@test AbstractOperators.blockvecnorm(grad .- grad3) < 1e-4
61+
end
6062

6163
return Ax, grad
6264
end

0 commit comments

Comments
 (0)