Skip to content

Commit

Permalink
Overloaded + for common operations between Eye and sparse operators
Browse files Browse the repository at this point in the history
  • Loading branch information
SatyaBade12 committed Jun 22, 2023
1 parent 94ac58b commit 704bcbe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/operators_sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ mul!(result::DenseOpType{B1,B3},M::SparseOpType{B1,B2},b::DenseOpType{B2,B3},alp
mul!(result::DenseOpType{B1,B3},a::DenseOpType{B1,B2},M::SparseOpType{B2,B3},alpha,beta) where {B1,B2,B3} = (gemm!(alpha,a.data,M.data,beta,result.data); result)
mul!(result::Ket{B1},M::SparseOpPureType{B1,B2},b::Ket{B2},alpha,beta) where {B1,B2} = (gemv!(alpha,M.data,b.data,beta,result.data); result)
mul!(result::Bra{B2},b::Bra{B1},M::SparseOpPureType{B1,B2},alpha,beta) where {B1,B2} = (gemv!(alpha,b.data,M.data,beta,result.data); result)

+(op1::EyeOpType{BL,BR},op2::SparseOpType{BL,BR}) where {BL,BR} = sparse(op1) + op2
-(op1::EyeOpType{BL,BR},op2::SparseOpType{BL,BR}) where {BL,BR} = sparse(op1) - op2
+(op1::SparseOpType{BL,BR},op2::EyeOpType{BL,BR}) where {BL,BR} = sparse(op2) + op1
-(op2::SparseOpType{BL,BR},op1::EyeOpType{BL,BR}) where {BL,BR} = op2 - sparse(op1)
*(op::EyeOpType,a::T) where {T<:Number} = a*sparse(op)
*(a::T,op::EyeOpType) where {T<:Number} = a*sparse(op)
/(op::EyeOpType,a::T) where {T<:Number} = sparse(op)/a
7 changes: 7 additions & 0 deletions test/test_operators_sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ IEye = identityoperator(b_l)
@test sparse(IEye) == I
Icomp = identityoperator(b1a) identityoperator(b2a) identityoperator(b3a)
@test IEye == Icomp
@test typeof(2*IEye)<:SparseOpType
@test typeof(IEye*2)<:SparseOpType
@test typeof(IEye/2)<:SparseOpType
@test typeof(IEye+sparse(IEye))<:SparseOpType
@test typeof(sparse(IEye)+IEye)<:SparseOpType
@test typeof(sparse(IEye)-IEye)<:SparseOpType
@test typeof(IEye-sparse(IEye))<:SparseOpType

# Test tr and normalize
op = sparse(DenseOperator(GenericBasis(3), [1 3 2;5 2 2;-1 2 5]))
Expand Down

0 comments on commit 704bcbe

Please sign in to comment.