Skip to content

Commit

Permalink
created imag,real,even,odd,angular,radial methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Jun 28, 2019
1 parent b7ebf60 commit e645888
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 12 deletions.
22 changes: 13 additions & 9 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ for Blade ∈ MSB
end
end

export
export ,
const = *

## exterior product

Expand Down Expand Up @@ -300,7 +301,9 @@ end

## sandwich product

>>>(x::TensorAlgebra{V},y::TensorAlgebra{V}) where V = x * y * ~x
>>>(x::TensorAlgebra{V},y::TensorAlgebra{V}) where V = (~x) * y * x
const = >>>
export

### Product Algebra Constructor

Expand Down Expand Up @@ -980,9 +983,10 @@ for (nv,d) ∈ ((:inv,:/),(:inv_rat,://))
rm = ~m
d = rm*m
fd = norm(d)
for k 0:ndims(V)
dk = d(k)
norm(dk) fd && (return $d(rm,dk))
sd = scalar(d)
value(sd) fd && (return $d(rm,sd))
for k 1:ndims(V)
norm(d[k]) fd && (return $d(rm,d(k)))
end
throw(error("inv($m) is undefined"))
end
Expand All @@ -1006,23 +1010,23 @@ for op ∈ (:div,:rem,:mod,:mod1,:fld,:fld1,:cld,:ldexp)
@eval Base.$op(b::$Value{V,G,B,T},m) where {V,G,B,T} = $Value{V,G,B}($op(value(b),m))
end
for Blade MSB
@eval Base.$op(a::$Blade{T,V,G},m) where {T,V,G} = $Blade{T,V,G}($op.(value(a),m))
@eval Base.$op(a::$Blade{T,V,G},m::S) where {T,V,G,S} = $Blade{promote_type(T,S),V,G}($op.(value(a),m))
end
@eval begin
Base.$op(a::Basis{V,G},m) where {V,G} = Basis{V,G}($op(value(a),m))
Base.$op(a::MultiVector{T,V},m) where {T,V} = MultiVector{T,V}($op.(value(a),m))
Base.$op(a::MultiVector{T,V},m::S) where {T,V,S} = MultiVector{promote_type(T,S),V}($op.(value(a),m))
end
end
for op (:mod2pi,:rem2pi,:rad2deg,:deg2rad)
for Value MSV
@eval Base.$op(b::$Value{V,G,B,T}) where {V,G,B,T} = $Value{V,G,B}($op(value(b)))
end
for Blade MSB
@eval Base.$op(a::$Blade{T,V,G}) where {T,V,G} = $Blade{T,V,G}($op.(value(a)))
@eval Base.$op(a::$Blade{T,V,G}) where {T,V,G} = $Blade{promote_type(T,Float64),V,G}($op.(value(a)))
end
@eval begin
Base.$op(a::Basis{V,G}) where {V,G} = Basis{V,G}($op(value(a)))
Base.$op(a::MultiVector{T,V}) where {T,V} = MultiVector{T,V}($op.(value(a)))
Base.$op(a::MultiVector{T,V}) where {T,V} = MultiVector{promote_type(T,Float64),V}($op.(value(a)))
end
end
for Value MSV
Expand Down
5 changes: 3 additions & 2 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ setindex!(m::MultiVector{T},k::T,i::Int,j::Int) where T = (m[i][j] = k)
Base.firstindex(m::MultiVector) = 0
Base.lastindex(m::MultiVector{T,V} where T) where V = ndims(V)

function (m::MultiVector{T,V})(g::Int,::Type{B}=SBlade) where {T,V,B}
(m::MultiVector{T,V})(g::Int,b::Type{B}=SBlade) where {T,V,B} = m(Dim{g}(),b)
function (m::MultiVector{T,V})(::Dim{g},::Type{B}=SBlade) where {T,V,g,B}
B SBlade ? MBlade{T,V,g}(m[g]) : SBlade{T,V,g}(m[g])
end
function (m::MultiVector{T,V})(g::Int,i::Int,::Type{B}=SValue) where {T,V,B}
Expand Down Expand Up @@ -366,7 +367,7 @@ end

@inline vector(t::T) where T<:TensorTerm{V,1} where V = t
@inline vector(t::T) where T<:TensorTerm{V} where V = zero(V)
@inline vector(t::MultiVector{T,V}) where {T,V} = SBlade{T,V,0}(t[1])
@inline vector(t::MultiVector{T,V}) where {T,V} = SBlade{T,V,1}(t[1])
for Blade MSB
@eval begin
@inline vector(t::$Blade{T,V,1} where {T,V}) = t
Expand Down
93 changes: 93 additions & 0 deletions src/parity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,96 @@ for par ∈ (:conformal,:regressive,:interior,:crossprod)
@eval @pure $par(a::Basis{V,G,B},b::Basis{V,L,C}) where {V,G,B,L,C} = $par(bits(a),bits(b),V)
end

import Base: signbit, imag, real
export odd, even, angular, radial

@pure signbit(V::T) where T<:VectorSpace{N} where N = (ib=indexbasis(N); parity.(ib,ib,Ref(V)))
@pure signbit(V::T,G) where T<:VectorSpace{N} where N = (ib=indexbasis(N,G); parity.(ib,ib,Ref(V)))
@pure angular(V::T) where T<:VectorSpace = SVector(findall(signbit(V))...)
@pure radial(V::T) where T<:VectorSpace = SVector(findall(.!signbit(V))...)
@pure angular(V::T,G) where T<:VectorSpace = findall(signbit(V,G))
@pure radial(V::T,G) where T<:VectorSpace = findall(.!signbit(V,G))

for (op,other) ((:angular,:radial),(:radial,:angular))
@eval $op(t::T) where T<:TensorTerm{V,G} where {V,G} = basisindex(ndims(V),bits(basis(t))) $op(V,G) ? t : zero(V)
for Blade MSB
@eval function $op(t::$Blade{T,V,G}) where {T,V,G}
out = copy(value(t,mvec(ndims(V),G,T)))
for k $other(V,G)
@inbounds out[k]0 && (out[k] = zero(T))
end
MBlade{T,V,G}(out)
end
end
@eval function $op(t::MultiVector{T,V}) where {T,V}
out = copy(value(t,mvec(ndims(V),T)))
for k $other(V)
@inbounds out[k]0 && (out[k] = zero(T))
end
MultiVector{T,V}(out)
end
end

odd(t::T) where T<:TensorTerm{V,G} where {V,G} = parityinvolute(G) ? t : zero(V)
even(t::T) where T<:TensorTerm{V,G} where {V,G} = parityinvolute(G) ? zero(V) : t
for Blade MSB
@eval begin
odd(t::$Blade{V,G}) where {V,G} = parityinvolute(G) ? t : zero(V)
even(t::$Blade{V,G}) where {V,G} = parityinvolute(G) ? zero(V) : t
end
end
function odd(t::MultiVector{T,V}) where {T,V}
N = ndims(V)
out = copy(value(t,mvec(N,T)))
bs = binomsum_set(N)
out[1]0 && (out[1] = zero(T))
for g 3:2:N+1
for k bs[g]+1:bs[g+1]
@inbounds out[k]0 && (out[k] = zero(T))
end
end
MultiVector{T,V}(out)
end
function even(t::MultiVector{T,V}) where {T,V}
N = ndims(V)
out = copy(value(t,mvec(N,T)))
bs = binomsum_set(N)
for g 2:2:N+1
for k bs[g]+1:bs[g+1]
@inbounds out[k]0 && (out[k] = zero(T))
end
end
MultiVector{T,V}(out)
end

imag(t::T) where T<:TensorTerm{V,G} where {V,G} = parityreverse(G) ? t : zero(V)
real(t::T) where T<:TensorTerm{V,G} where {V,G} = parityreverse(G) ? zero(V) : t
for Blade MSB
@eval begin
imag(t::$Blade{V,G}) where {V,G} = parityreverse(G) ? t : zero(V)
real(t::$Blade{V,G}) where {V,G} = parityreverse(G) ? zero(V) : t
end
end
function imag(t::MultiVector{T,V}) where {T,V}
N = ndims(V)
out = copy(value(t,mvec(N,T)))
bs = binomsum_set(N)
out[1]0 && (out[1] = zero(T))
for g 3:N+1
!parityreverse(g-1) && for k bs[g]+1:bs[g+1]
out[k]0 && (out[k] = zero(T))
end
end
MultiVector{T,V}(out)
end
function real(t::MultiVector{T,V}) where {T,V}
N = ndims(V)
out = copy(value(t,mvec(N,T)))
bs = binomsum_set(N)
for g 3:N+1
parityreverse(g-1) && for k bs[g]+1:bs[g+1]
out[k]0 && (out[k] = zero(T))
end
end
MultiVector{T,V}(out)
end
4 changes: 3 additions & 1 deletion src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Grassmann Copyright (C) 2019 Michael Reed

import Base: @pure, print, show, getindex, setindex!, promote_rule, ==, convert, ndims
import DirectSum: Bits, bit2int, doc2m, indexbits, indices, diffmode
import DirectSum: Bits, bit2int, doc2m, indexbits, indices, diffmode, Dim, Grade

bcast(op,arg) = op (:(Reduce.Algebra.:+),:(Reduce.Algebra.:-)) ? Expr(:.,op,arg) : Expr(:call,op,arg.args...)

Expand Down Expand Up @@ -201,6 +201,8 @@ indexbasis(Int((sparse_limit+cache_limit)/2),1)

@pure indexbasis_set(N) = SVector(((N0 && N<sparse_limit) ? indexbasis_cache[N] : Vector{Bits}[indexbasis(N,g) for g 0:N])...)

@pure indexbasis(N) = vcat(indexbasis(N,0),indexbasis_set(N)...)

## Grade{G}

struct Grade{G}
Expand Down

0 comments on commit e645888

Please sign in to comment.