Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex SparseTensor #48

Open
cauachagas opened this issue Jun 9, 2020 · 1 comment
Open

Complex SparseTensor #48

cauachagas opened this issue Jun 9, 2020 · 1 comment

Comments

@cauachagas
Copy link

I tried to add to the code

function SparseTensor(I::Union{PyObject,Array{T,1}}, J::Union{PyObject,Array{T,1}}, 
      V::Union{Array{ComplexF64,1}, PyObject},
     m::Union{S, PyObject, Nothing}=nothing, n::Union{S, PyObject, Nothing}=nothing; is_diag::Bool=false) where {T<:Integer, S<:Integer}
    if isa(I, PyObject) && size(I,2)==2
        return SparseTensor_(I, J, V)
    end
    I, J, V = convert_to_tensor(I, dtype=Int64), convert_to_tensor(J, dtype=Int64), convert_to_tensor(V)
    m, n = convert_to_tensor(m, dtype=Int64), convert_to_tensor(n, dtype=Int64)
    indices = [I J] .- 1
    value = V
    shape = [m;n]
    sp = tf.SparseTensor(indices, value, shape)
    options.sparse.auto_reorder && (sp = tf.sparse.reorder(sp)) 
    SparseTensor(sp, is_diag)
end
sess = Session();
ii = [1;2;3;4]
jj = [1;2;3;4]
vv = [1.0;1.0;1.0;1.0] .-0.5im
rhs =collect(1:4.0)
s = SparseTensor(ii, jj, vv, 4, 4)
sol = s\rhs
run(sess, sol)
4-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0

Correct answer

sparse(ii,jj,vv)\rhs
4-element Array{Complex{Float64},1}:
                0.8 + 0.4im
                1.6 + 0.8im
 2.4000000000000004 + 1.2000000000000002im
                3.2 + 1.6im
@kailaix
Copy link
Owner

kailaix commented Jun 10, 2020

Hi @cauachagas, ADCME does not support sparse solvers for complex types directly. Your modification does not work because ADCME lacks a kernel for complex sparse solvers. However, you can make a complex sparse solver from the existing sparse solver for real numbers.
To do this, write your sparse matrix as M = A + i * B, and rhs as u + i * v, the solution is a + i * b, then

(A+iB) (a+ib) = u+iv 

The equation is equivalent to

(A*a - B*b) + i * (B*a+A*b) = u + i * v

which is equivalent to

A * a - B * b = u     ==>      [A -B] [a] = [u]           
B * a + A * b = v              [B  A] [b] = [v]

Using ADCME, you have

M = [A -B
     B  A]
rhs = [u;v]
sol = M\rhs
sol = sol[1:n] + 1im * sol[n+1:2n]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants