@@ -96,22 +96,48 @@ isequal(x::LazyTensor, y::LazyTensor) = samebases(x,y) && isequal(x.indices, y.i
9696# Arithmetic operations
9797- (a:: LazyTensor ) = LazyTensor (a, - a. factor)
9898
99- function + (a:: LazyTensor{B1,B2} , b:: LazyTensor{B1,B2} ) where {B1,B2}
100- if length (a. indices) == 1 && a. indices == b. indices
101- op = a. operators[1 ] * a. factor + b. operators[1 ] * b. factor
102- return LazyTensor (a. basis_l, a. basis_r, a. indices, (op,))
103- end
104- throw (ArgumentError (" Addition of LazyTensor operators is only defined in case both operators act nontrivially on the same, single tensor factor." ))
99+ function + (a:: LazyTensor{B1,B2} ,b:: LazyTensor{B1,B2} ) where {B1,B2}
100+ LazySum (a,b)
101+ end
102+ function - (a:: LazyTensor{B1,B2} ,b:: LazyTensor{B1,B2} ) where {B1,B2}
103+ LazySum ((1 ,- 1 ),(a,b))
104+ end
105+ function + (a:: LazyTensor{B1,B2} ,b:: Operator{B1,B2} ) where {B1,B2}
106+ LazySum (a) + b
107+ end
108+ function + (a:: Operator{B1,B2} ,b:: LazyTensor{B1,B2} ) where {B1,B2}
109+ + (b,a)
110+ end
111+ function - (a:: LazyTensor{B1,B2} ,b:: Operator{B1,B2} ) where {B1,B2}
112+ LazySum (a) - b
113+ end
114+ function - (a:: Operator{B1,B2} ,b:: LazyTensor{B1,B2} ) where {B1,B2}
115+ a - LazySum (b)
105116end
106117
107- function - (a:: LazyTensor{B1,B2} , b:: LazyTensor{B1,B2} ) where {B1,B2}
108- if length (a. indices) == 1 && a. indices == b. indices
109- op = a. operators[1 ] * a. factor - b. operators[1 ] * b. factor
110- return LazyTensor (a. basis_l, a. basis_r, a. indices, (op,))
118+ function tensor (a:: LazyTensor{B1,B1} ,b:: Operator{B2,B2} ) where {B1,B2}
119+ if isequal (b,identityoperator (basis (b)))
120+ btotal = basis (a) ⊗ basis (b)
121+ LazyTensor (btotal,btotal,a. indices,(a. operators... ,),a. factor)
122+ elseif B2 <: CompositeBasis
123+ throw (ArgumentError (" tensor(a::LazyTensor{B1,B1},b::Operator{B2,B2}) is not implemented for B2 being CompositeBasis " ))
124+ else
125+ a ⊗ LazyTensor (b. basis_l,b. basis_r,[1 ],(b,),1 )
126+ end
127+ end
128+ function tensor (a:: Operator{B1,B1} ,b:: LazyTensor{B2,B2} ) where {B1,B2}
129+ if isequal (a,identityoperator (basis (a)))
130+ btotal = basis (a) ⊗ basis (b)
131+ LazyTensor (btotal,btotal,b. indices.+ length (basis (a). shape) ,(b. operators... ,),b. factor)
132+ elseif B1 <: CompositeBasis
133+ throw (ArgumentError (" tensor(a::Operator{B1,B1},b::LazyTensor{B2,B2}) is not implemented for B1 being CompositeBasis " ))
134+ else
135+ LazyTensor (a. basis_l,a. basis_r,[1 ],(a,),1 ) ⊗ b
111136 end
112- throw (ArgumentError (" Subtraction of LazyTensor operators is only defined in case both operators act nontrivially on the same, single tensor factor." ))
113137end
114138
139+
140+
115141function * (a:: LazyTensor{B1,B2} , b:: LazyTensor{B2,B3} ) where {B1,B2,B3}
116142 indices = sort (union (a. indices, b. indices))
117143 # ops = Vector{AbstractOperator}(undef, length(indices))
0 commit comments