Skip to content

Commit

Permalink
Merge pull request #24 from wwangnju/master
Browse files Browse the repository at this point in the history
add :/ and :- method
  • Loading branch information
waltergu authored Oct 10, 2022
2 parents a945407 + 1bbfbc5 commit 58071b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/DegreesOfFreedom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)
Expand All @@ -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
"""
Expand Down
12 changes: 12 additions & 0 deletions test/DegreesOfFreedom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(:)))
Expand All @@ -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]))
Expand All @@ -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₂)
Expand Down

0 comments on commit 58071b7

Please sign in to comment.