diff --git a/src/DegreesOfFreedom.jl b/src/DegreesOfFreedom.jl index 10b575b3..400acce5 100644 --- a/src/DegreesOfFreedom.jl +++ b/src/DegreesOfFreedom.jl @@ -923,7 +923,7 @@ end *(mcp::MatrixCouplingProd, mc::MatrixCoupling) -> MatrixCouplingProd *(mcp₁::MatrixCouplingProd, mcp₂::MatrixCouplingProd) -> MatrixCouplingProd *(mc::MatrixCoupling, factor::Number) -> MatrixCouplingProd - *(mc::MatrixCoupling, factor::Number) -> MatrixCouplingProd + *(factor::Number, mc::MatrixCoupling) -> MatrixCouplingProd *(factor::Number, mcp::MatrixCouplingProd) -> MatrixCouplingProd *(mcp::MatrixCouplingProd, factor::Number) -> MatrixCouplingProd *(mcs::MatrixCouplingSum, element::Union{Number, MatrixCoupling, MatrixCouplingProd}) -> MatrixCouplingSum @@ -952,7 +952,7 @@ The product between `MatrixCoupling`s and `MatrixCouplingProd`s. +(mcs::MatrixCouplingSum, mc::Union{MatrixCoupling, MatrixCouplingProd}) -> MatrixCouplingSum +(mcs₁::MatrixCouplingSum, mcs₂::MatrixCouplingSum) -> MatrixCouplingSum -The product between `MatrixCoupling`s and `MatrixCouplingProd`s. +The addition between `MatrixCoupling`s and `MatrixCouplingProd`s. """ @inline Base.:+(mc₁::Union{MatrixCoupling, MatrixCouplingProd}, mc₂::Union{MatrixCoupling, MatrixCouplingProd}) = MatrixCouplingSum(mc₁, mc₂) @inline Base.:+(mc::Union{MatrixCoupling, MatrixCouplingProd}, mcs::MatrixCouplingSum) = MatrixCouplingSum(mc, mcs.contents...) @@ -965,6 +965,32 @@ The product between `MatrixCoupling`s and `MatrixCouplingProd`s. Get the nth power of a `MatrixCoupling`/`MatrixCouplingProd`/`MatrixCouplingSum`. """ @inline Base.:^(mc::Union{MatrixCoupling, MatrixCouplingProd, MatrixCouplingSum}, n::Int) = prod(ntuple(i->mc, Val(n)); init=1) +""" + /(mcp::MatrixCouplingProd, factor::Number) -> MatrixCouplingProd + /(mcs::MatrixCouplingSum, factor::Number) -> MatrixCouplingSum + /(mc::MatrixCoupling, factor::Number) -> MatrixCouplingProd + -(mc::MatrixCoupling) -> MatrixCouplingProd + -(mcp::MatrixCouplingProd) -> MatrixCouplingProd + -(mcs::MatrixCouplingSum) -> MatrixCouplingSum + -(mc₁::Union{MatrixCoupling, MatrixCouplingProd}, mc₂::Union{MatrixCoupling, MatrixCouplingProd}) -> MatrixCouplingSum + -(mc::Union{MatrixCoupling, MatrixCouplingProd}, mcs::MatrixCouplingSum) -> MatrixCouplingSum + -(mcs::MatrixCouplingSum, mc::Union{MatrixCoupling, MatrixCouplingProd}) -> MatrixCouplingSum + -(mcs₁::MatrixCouplingSum, mcs₂::MatrixCouplingSum) -> MatrixCouplingSum + +Define right-division, minus, substraction operator. +""" +@inline Base.:/(mcp::MatrixCouplingProd, factor::Number) = MatrixCouplingProd(mcp.value/factor, mcp.contents) +@inline Base.:/(mcs::MatrixCouplingSum, factor::Number) = MatrixCouplingSum(map(m->m/factor, mcs.contents)) +@inline Base.:/(mc::MatrixCoupling, factor::Number) = MatrixCouplingProd(1/factor, mc) +@inline Base.:-(mc::MatrixCoupling) = MatrixCouplingProd(-1, mc) +@inline Base.:-(mcp::MatrixCouplingProd) = MatrixCouplingProd(-1*mcp.value, mcp.contents) +@inline Base.:-(mcs::MatrixCouplingSum) = MatrixCouplingSum(map(m->-m, mcs.contents)) + +@inline Base.:-(mc₁::Union{MatrixCoupling, MatrixCouplingProd}, mc₂::Union{MatrixCoupling, MatrixCouplingProd}) = MatrixCouplingSum(mc₁, -mc₂) +@inline Base.:-(mc::Union{MatrixCoupling, MatrixCouplingProd}, mcs::MatrixCouplingSum) = MatrixCouplingSum(mc, map(m->-m, mcs.contents)...) +@inline Base.:-(mcs::MatrixCouplingSum, mc::Union{MatrixCoupling, MatrixCouplingProd}) = MatrixCouplingSum(mcs.contents..., -mc) +@inline Base.:-(mcs₁::MatrixCouplingSum, mcs₂::MatrixCouplingSum) = MatrixCouplingSum(mcs₁.contents..., map(m->-m, mcs₂.contents)...) + # Term """ diff --git a/test/DegreesOfFreedom.jl b/test/DegreesOfFreedom.jl index 847a65b0..ce806807 100644 --- a/test/DegreesOfFreedom.jl +++ b/test/DegreesOfFreedom.jl @@ -297,6 +297,8 @@ end @test mc[1] == Coupling(-1, Index(:, DID(1)), Index(:, DID(2))) @test mc[2] == Coupling(+1, Index(:, DID(2)), Index(:, DID(1))) @test mc^2 == mc*mc + @test mc/2 == mc*(1/2) + @test -mc == (-1)*mc another = MatrixCoupling((1, 2), DID, Component([:], [:], hcat(2.0))) @test another[1] == Coupling(2.0, Index(1, DID(:)), Index(2, DID(:))) @@ -313,6 +315,8 @@ end @test mc*mcp == MatrixCouplingProd(mc, mc, another) @test mcp*mcp == MatrixCouplingProd(mc, another, mc, another) @test mcp^2 == mcp*mcp + @test mcp/2 == mcp*(1/2) == MatrixCouplingProd(1/2, mc, another) + @test -mcp == (-1)*mcp mc₁ = MatrixCoupling((1, 2), DID, Component([1, 2], [2, 1], [0 1; 1 0])) mc₂ = MatrixCoupling((2, 1), DID, Component([1, 2], [2, 1], [0 1im; -1im 0])) @@ -331,6 +335,14 @@ end @test mcp*mcs == MatrixCouplingSum(mc*another*mc₁, mc*another*mc₂) @test mcs*mcp == MatrixCouplingSum(mc₁*mc*another, mc₂*mc*another) @test mcs^2 == mcs*mcs + @test mcs/2 == mcs*(1/2) == MatrixCouplingSum(mc₁/2, mc₂/2) + @test -mcs == (-1)*mcs == MatrixCouplingSum(-mc₁, -mc₂) + + mcs₂ = mc₁ - mc₂ + @test mcs₂ == MatrixCouplingSum(mc₁, -mc₂) + @test mcs - mc₁ == MatrixCouplingSum(mc₁, mc₂, -mc₁) + @test mc₁ - mcs₂ == MatrixCouplingSum(mc₁, -mc₁, mc₂) + @test mcs - mcs₂ == MatrixCouplingSum(mc₁, mc₂, -mc₁, mc₂) @test mcs+mc₁ == MatrixCouplingSum(mc₁, mc₂, mc₁) @test mc₁+mcs == MatrixCouplingSum(mc₁, mc₁, mc₂)