Skip to content

Commit

Permalink
refined MultiVector reverse and printing
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Sep 11, 2019
1 parent e0138ce commit 2ab467b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
29 changes: 22 additions & 7 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ Grassmann-Poincare left complement: ⋆'ω = I∗'ω
import Base: reverse, conj, ~
export involute

@pure grade(V,B) = count_ones(B&(one(Bits)<<DirectSum.grade(V)-1))

for r (:reverse,:involute,:conj)
p = Symbol(:parity,r)
@eval @pure $r(b::Basis{V,G,B}) where {V,G,B} =$p(G) ? SBlade{V}(-value(b),b) : b
@eval @pure function $r(b::Basis{V,G,B}) where {V,G,B}
$p(grade(V,B)) ? SBlade{V}(-value(b),b) : b
end
for Blade MSB
@eval $r(b::$Blade) = value(b) 0 ? value(b) * $r(basis(b)) : g_zero(vectorspace(b))
end
Expand Down Expand Up @@ -740,27 +744,38 @@ function generate_product_algebra(Field=Field,VEC=:mvec,MUL=:*,ADD=:+,SUB=:-,CON
for Chain MSC
@eval begin
function $reverse(b::$Chain{T,V,G}) where {T<:$Field,V,G}
!$p(G) && (return b)
D = diffvars(V)
D==0 && !$p(G) && (return b)
$(insert_expr((:N,:ib),VEC)...)
out = zeros($VEC(N,G,T))
for k 1:binomial(N,G)
@inbounds val = b.v[k]
@inbounds val0 && setblade!(out,$SUB(val),ib[k],Dimension{N}())
@inbounds v = b.v[k]
v0 && if D==0
@inbounds setblade!(out,$SUB(v),ib[k],Dimension{N}())
else
@inbounds B = ib[k]
setblade!(out,$p(grade(V,B)) ? $SUB(v) : v,B,Dimension{N}())
end
end
return $Chain{T,V,G}(out)
end
end
end
@eval begin
function $reverse(m::MultiVector{T,V}) where {T<:$Field,V}
$(insert_expr((:N,:bs,:bn),VEC)...)
$(insert_expr((:N,:bs,:bn,:D),VEC)...)
out = zeros($VEC(N,T))
for g 1:N+1
pg = $p(g-1)
ib = indexbasis(N,g-1)
@inbounds for i 1:bn[g]
@inbounds val = m.v[bs[g]+i]
@inbounds val0 && setmulti!(out,pg ? $SUB(val) : val,ib[i],Dimension{N}())
@inbounds v = m.v[bs[g]+i]
v0 && if D==0
@inbounds setmulti!(out,pg ? $SUB(v) : v,ib[i],Dimension{N}())
else
@inbounds B = ib[i]
setmulti!(out,$p(grade(V,B)) ? $SUB(v) : v,B,Dimension{N}())
end
end
end
return MultiVector{T,V}(out)
Expand Down
32 changes: 16 additions & 16 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Fields = (Real,Complex)

# symbolic print types

parany = (Expr,Complex,Any)
parsym = (Expr,Complex,Symbol)
parval = (Expr,Complex)
parany = (Expr,Complex,Rational,TensorMixed)
parsym = (Expr,Complex,Rational,TensorAlgebra,Symbol)
parval = (Expr,Complex,Rational,TensorMixed)

## pseudoscalar

Expand Down Expand Up @@ -103,14 +103,16 @@ for Blade ∈ MSB
function $Blade{V,G}(v::T,b::Basis{V,G}) where {V,G,T}
order(v)+order(b)>diffmode(V) ? zero(V) : $Blade{V,G,b,T}(v)
end
function $Blade{V,G}(v::T,b::Basis{V,G}) where T<:TensorTerm{V} where {V,G}
order(v)+order(b)>diffmode(V) ? zero(V) : $Blade{V,G,b,Any}(v)
end
function $Blade{V,G,B}(b::T) where T<:TensorTerm{V} where {V,G,B}
order(B)+order(b)>diffmode(V) ? zero(V) : $Blade{V,G,B,Any}(b)
end
function show(io::IO,m::$Blade)
T = valuetype(m)
par = (!(T<:TensorMixed)) && Tparany
!par && (par = typeof(value(m)) <: TensorTerm)
print(io,(par ? [m.v] : ['(',m.v,')'])...,basis(m))
T = typeof(value(m))
par = !(T <: TensorTerm) && |(broadcast(<:,T,parany)...)
print(io,(par ? ['(',m.v,')'] : [m.v])...,basis(m))
end
end
for Other MSB, VG ((:V,),(:V,:G))
Expand Down Expand Up @@ -168,10 +170,9 @@ for (Chain,vector,Blade) ∈ ((MSC[1],:MVector,MSB[1]),(MSC[2],:SVector,MSB[2]))
for k 2:length(ib)
@inbounds mvs = m.v[k]
tmv = typeof(mvs)
if T == Any && (tmv parsym || tmv <: TensorAlgebra)
par = (!(tmv<:TensorMixed)) && tmvparval
!par && (par = typeof(mvs) <: TensorTerm)
par ? print(io," + ",mvs) : print(io," + (",mvs,")")
if |(broadcast(<:,tmv,parsym)...)
par = (!(tmv<:TensorTerm)) && |(broadcast(<:,tmv,parval)...)
par ? print(io," + (",mvs,")") : print(io," + ",mvs)
else
sbm = signbit(mvs)
print(io,sbm ? " - " : " + ",sbm ? abs(mvs) : mvs)
Expand Down Expand Up @@ -309,10 +310,9 @@ function show(io::IO, m::MultiVector{T,V}) where {T,V}
@inbounds mvs = m.v[s]
@inbounds if mvs 0
tmv = typeof(mvs)
if T == Any && (tmv parsym || tmv <: TensorAlgebra)
par = (!(tmv<:TensorMixed)) && tmvparval
!par && (par = typeof(mvs) <: TensorTerm)
par ? print(io," + ",mvs) : print(io," + (",mvs,")")
if |(broadcast(<:,tmv,parsym)...)
par = (!(tmv<:TensorTerm)) && |(broadcast(<:,tmv,parval)...)
par ? print(io," + (",mvs,")") : print(io," + ",mvs)
else
sba = signbit(mvs)
print(io,sba ? " - " : " + ",sba ? abs(mvs) : mvs)
Expand Down Expand Up @@ -478,7 +478,7 @@ valuetype(t::MultiGrade) = promote_type(valuetype.(terms(t))...)
@pure valuetype(::Basis) = Int
@pure valuetype(::Union{MBlade{V,G,B,T},SBlade{V,G,B,T}} where {V,G,B}) where T = T
@pure valuetype(::TensorMixed{T}) where T = T
@inline value(::Basis,T=Int) = one(T)
@inline value(::Basis,T=Int) = T==Any ? 1 : one(T)
@inline value(m::VBV,T::DataType=valuetype(m)) = T(valuetype(m),Any) ? convert(T,m.v) : m.v
@inline value_diff(m::T) where T<:TensorTerm = (v=value(m);typeof(v)<:TensorAlgebra ? v : m)
@pure basis(m::Basis) = m
Expand Down
6 changes: 0 additions & 6 deletions src/parity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,7 @@ for par ∈ (:conformal,:regressive,:interior,:crossprod)
@inbounds push!($cache[n][m1][s],$T[])
end
@inbounds for k length($cache[n][m1][s][a1]):b
try
@inbounds push!($cache[n][m1][s][a1],$calc(V,a,k))
catch err
println(a)
println(k)
throw(err)
end
end
@inbounds $cache[n][m1][s][a1][b+1]
end
Expand Down

0 comments on commit 2ab467b

Please sign in to comment.