Skip to content

Commit

Permalink
created generate_inverses methods for symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Oct 6, 2019
1 parent b6a79ab commit 9968946
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
9 changes: 5 additions & 4 deletions src/Grassmann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ function __init__()
@require Reduce="93e0c654-6965-5f22-aba9-9c1ae6b3c259" begin
*(a::Reduce.RExpr,b::Basis{V}) where V = SBlade{V}(a,b)
*(a::Basis{V},b::Reduce.RExpr) where V = SBlade{V}(b,a)
*(a::Reduce.RExpr,b::MultiVector{T,V}) where {T,V} = MultiVector{promote_type(T,F),V}(broadcast(Reduce.Algebra.:*,a,b.v))
*(a::Reduce.RExpr,b::MultiVector{T,V}) where {T,V} = MultiVector{promote_type(T,F),V}(broadcast(Reduce.Algebra.:*,Refa,b.v))
*(a::MultiVector{T,V},b::Reduce.RExpr) where {T,V} = MultiVector{promote_type(T,F),V}(broadcast(Reduce.Algebra.:*,a.v,b))
*(a::Reduce.RExpr,b::MultiGrade{V}) where V = MultiGrade{V}(broadcast(Reduce.Algebra.:*,a,b.v))
*(a::MultiGrade{V},b::Reduce.RExpr) where V = MultiGrade{V}(broadcast(Reduce.Algebra.:*,a.v,b))
Expand All @@ -443,9 +443,10 @@ function __init__()
parany = (parany...,Reduce.RExpr)
parval = (parval...,Reduce.RExpr)
parsym = (parsym...,Reduce.RExpr)
generate_derivation(:(Reduce.Algebra),:RExpr,:df,:RExpr)
generate_derivation(:(Reduce.Algebra),:Symbol,:df,:RExpr)
generate_derivation(:(Reduce.Algebra),:Expr,:df,:RExpr)
for T (:RExpr,:Symbol,:Expr)
generate_inverses(:(Reduce.Algebra),T)
generate_derivation(:(Reduce.Algebra),T,:df,:RExpr)
end
end
@require SymPy="24249f21-da20-56a4-8eb1-6a02cf4ae2e6" generate_algebra(:SymPy,:Sym,:diff,:symbols)
@require SymEngine="123dc426-2d89-5057-bbad-38513e3affd8" generate_algebra(:SymEngine,:Basic,:diff,:symbols)
Expand Down
45 changes: 27 additions & 18 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,10 @@ function generate_products(Field=Field,VEC=:mvec,MUL=:*,ADD=:+,SUB=:-,CONJ=:conj
end
*(a::F,b::Basis{V}) where {F<:$EF,V} = SBlade{V}(a,b)
*(a::Basis{V},b::F) where {F<:$EF,V} = SBlade{V}(b,a)
*(a::F,b::MultiVector{T,V}) where {F<:$Field,T,V} = MultiVector{promote_type(T,F),V}(broadcast($Sym.∏,a,b.v))
*(a::MultiVector{T,V},b::F) where {F<:$Field,T,V} = MultiVector{promote_type(T,F),V}(broadcast($Sym.∏,a.v,b))
*(a::F,b::MultiGrade{V}) where {F<:$EF,V} = MultiGrade{V}(broadcast($MUL,a,b.v))
*(a::MultiGrade{V},b::F) where {F<:$EF,V} = MultiGrade{V}(broadcast($MUL,a.v,b))
*(a::F,b::MultiVector{T,V}) where {F<:$Field,T,V} = MultiVector{promote_type(T,F),V}(broadcast($Sym.∏,Ref(a),b.v))
*(a::MultiVector{T,V},b::F) where {F<:$Field,T,V} = MultiVector{promote_type(T,F),V}(broadcast($Sym.∏,a.v,Ref(b)))
*(a::F,b::MultiGrade{V}) where {F<:$EF,V} = MultiGrade{V}(broadcast($MUL,Ref(a),b.v))
*(a::MultiGrade{V},b::F) where {F<:$EF,V} = MultiGrade{V}(broadcast($MUL,a.v,Ref(b)))
(a::$Field,b::$Field) = $MUL(a,b)
(a::F,b::B) where B<:TensorTerm{V,G} where {F<:$EF,V,G} = SBlade{V,G}(a,b)
(a::A,b::F) where A<:TensorTerm{V,G} where {F<:$EF,V,G} = SBlade{V,G}(b,a)
Expand Down Expand Up @@ -613,8 +613,8 @@ function generate_products(Field=Field,VEC=:mvec,MUL=:*,ADD=:+,SUB=:-,CONJ=:conj
end
$Chain{$TF,dual(V),G}(out)
end
*(a::F,b::$Chain{T,V,G}) where {F<:$Field,T<:$Field,V,G} = SChain{promote_type(T,F),V,G}(broadcast($MUL,a,b.v))
*(a::$Chain{T,V,G},b::F) where {F<:$Field,T<:$Field,V,G} = SChain{promote_type(T,F),V,G}(broadcast($MUL,a.v,b))
*(a::F,b::$Chain{T,V,G}) where {F<:$Field,T<:$Field,V,G} = SChain{promote_type(T,F),V,G}(broadcast($MUL,Ref(a),b.v))
*(a::$Chain{T,V,G},b::F) where {F<:$Field,T<:$Field,V,G} = SChain{promote_type(T,F),V,G}(broadcast($MUL,a.v,Ref(b)))
#∧(a::$Field,b::$Chain{T,V,G}) where {T<:$Field,V,G} = SChain{T,V,G}(a.*b.v)
#∧(a::$Chain{T,V,G},b::$Field) where {T<:$Field,V,G} = SChain{T,V,G}(a.v.*b)
function contraction(a::$Chain{T,V,G},b::Basis{V,G}) where {T<:$Field,V,G}
Expand Down Expand Up @@ -1317,8 +1317,8 @@ function generate_products(Field=Field,VEC=:mvec,MUL=:*,ADD=:+,SUB=:-,CONJ=:conj
end

@eval begin
*(a::F,b::MultiVector{T,V}) where {F<:Number,T,V} = MultiVector{promote_type(T,F),V}(broadcast($Sym.:∏,a,b.v))
*(a::MultiVector{T,V},b::F) where {F<:Number,T,V} = MultiVector{promote_type(T,F),V}(broadcast($Sym.:∏,a.v,b))
*(a::F,b::MultiVector{T,V}) where {F<:Number,T,V} = MultiVector{promote_type(T,F),V}(broadcast($Sym.:∏,Ref(a),b.v))
*(a::MultiVector{T,V},b::F) where {F<:Number,T,V} = MultiVector{promote_type(T,F),V}(broadcast($Sym.:∏,a.v,Ref(b)))
end
for Blade MSB
@eval begin
Expand All @@ -1328,15 +1328,15 @@ for Blade ∈ MSB
end
for Chain MSC
@eval begin
*(a::F,b::$Chain{T,V,G}) where {F<:Number,T,V,G} = SChain{promote_type(T,F),V,G}(broadcast($Sym.:∏,a,b.v))
*(a::$Chain{T,V,G},b::F) where {F<:Number,T,V,G} = SChain{promote_type(T,F),V,G}(broadcast($Sym.:∏,a.v,b))
*(a::F,b::$Chain{T,V,G}) where {F<:Number,T,V,G} = SChain{promote_type(T,F),V,G}(broadcast($Sym.:∏,Ref(a),b.v))
*(a::$Chain{T,V,G},b::F) where {F<:Number,T,V,G} = SChain{promote_type(T,F),V,G}(broadcast($Sym.:∏,a.v,Ref(b)))
end
end

for F Fields
@eval begin
*(a::F,b::MultiVector{T,V}) where {F<:$F,T<:Number,V} = MultiVector{promote_type(T,F),V}(broadcast(*,a,b.v))
*(a::MultiVector{T,V},b::F) where {F<:$F,T<:Number,V} = MultiVector{promote_type(T,F),V}(broadcast(*,a.v,b))
*(a::F,b::MultiVector{T,V}) where {F<:$F,T<:Number,V} = MultiVector{promote_type(T,F),V}(broadcast(*,Ref(a),b.v))
*(a::MultiVector{T,V},b::F) where {F<:$F,T<:Number,V} = MultiVector{promote_type(T,F),V}(broadcast(*,a.v,Ref(b)))
end
for Blade MSB
@eval begin
Expand All @@ -1346,8 +1346,8 @@ for F ∈ Fields
end
for Chain MSC
@eval begin
*(a::F,b::$Chain{T,V,G}) where {F<:$F,T<:Number,V,G} = SChain{promote_type(T,F),V,G}(broadcast(*,a,b.v))
*(a::$Chain{T,V,G},b::F) where {F<:$F,T<:Number,V,G} = SChain{promote_type(T,F),V,G}(broadcast(*,a.v,b))
*(a::F,b::$Chain{T,V,G}) where {F<:$F,T<:Number,V,G} = SChain{promote_type(T,F),V,G}(broadcast(*,Ref(a),b.v))
*(a::$Chain{T,V,G},b::F) where {F<:$F,T<:Number,V,G} = SChain{promote_type(T,F),V,G}(broadcast(*,a.v,Ref(b)))
end
end
end
Expand All @@ -1373,6 +1373,7 @@ function generate_derivation(m,t,d,c)
end
function generate_algebra(m,t,d=nothing,c=nothing)
generate_products(:($m.$t),:svec,:($m.:*),:($m.:+),:($m.:-),:($m.conj),true)
generate_inverses(m,t)
!isnothing(d) && generate_derivation(m,t,d,c)
end

Expand Down Expand Up @@ -1598,14 +1599,22 @@ for (nv,d) ∈ ((:inv,:/),(:inv_rat,://))
end
end
for Term (:TensorTerm,MSC...,:MultiVector,:MultiGrade)
@eval begin
@pure $d(a::S,b::T) where {S<:$Term,T<:Real} = a*$d(1,b)
@pure $d(a::S,b::T) where {S<:$Term,T<:Complex} = a*$d(1,b)
@pure $d(a::S,b::UniformScaling) where S<:$Term = a*$nv(vectorspace(a)(b))
@eval @pure $d(a::S,b::UniformScaling) where S<:$Term = a*$nv(vectorspace(a)(b))
end
end

function generate_inverses(Sym,T)
for (nv,d,ds) ((:inv,:/,:($Sym.:/)),(:inv_rat,://,:($Sym.://)))
for Term (:TensorTerm,MSC...,:MultiVector,:MultiGrade)
@eval $d(a::S,b::T) where {S<:$Term,T<:$Sym.$T} = a*$ds(1,b)
end
end
end

for T (:Real,:Complex)
generate_inverses(Base,T)
end

for op (:div,:rem,:mod,:mod1,:fld,:fld1,:cld,:ldexp)
for Blade MSB
@eval Base.$op(b::$Blade{V,G,B,T},m) where {V,G,B,T} = $Blade{V,G,B}($op(value(b),m))
Expand Down

2 comments on commit 9968946

@chakravala
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/4124

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.1 -m "<description of version>" 996894656db1cfbc768a98e32b05900cf1c688c5
git push origin v0.3.1

Please sign in to comment.