Skip to content

Commit

Permalink
generalized /,⊛ and scalar methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Jun 17, 2019
1 parent 5022c0d commit 5e1ddbb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ for Blade ∈ MSB
end
end

export

(a::TensorAlgebra{V},b::TensorAlgebra{V}) where V = scalar(a*conj(b))

## exterior product

export , ,
Expand Down Expand Up @@ -981,11 +985,14 @@ for Blade ∈ MSB
div(a::$Blade{T,V,G},m) where {T,V,G} = $Blade{T,V,G}(div.(value(a),m))
end
end
@pure /(a,b::T) where T<:TensorTerm = a*inv(b)
for Blade MSB
@eval /(a,b::T) where T<:$Blade = a*inv(b)
end
for Term (:TensorTerm,MSB...,:MultiVector,:MultiGrade)
@eval begin
@pure /(a::$Term,b::TensorTerm) = a*inv(b)
@pure /(a::$Term,b::Number) = a*inv(b)
@pure /(a::$Term,b::UniformScaling) = a*inv(vectorspace(a)(b))
@pure /(a::S,b::T) where {S<:$Term,T<:Number} = a*inv(b)
@pure /(a::S,b::UniformScaling) where S<:$Term = a*inv(vectorspace(a)(b))
end
end
rem(a::MultiVector{T,V},m) where {T,V} = MultiVector{T,V}(rem.(value(a),m))
Expand Down
38 changes: 31 additions & 7 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,46 @@ const VBV = Union{MValue,SValue,MBlade,SBlade,MultiVector}
@pure hasorigin(t::Union{MValue,SValue}) = hasorigin(basis(t))
@pure hasorigin(m::TensorAlgebra) = hasorigin(vectorspace(m))

function isapprox(a::TensorMixed{T1}, b::TensorMixed{T2}) where {T1, T2}
function isapprox(a::S,b::T) where {S<:TensorMixed{T1},T<:TensorMixed{T2}} where {T1, T2}
rtol = Base.rtoldefault(T1, T2, 0)
return norm(value(a-b)) <= rtol * max(norm(value(a)), norm(value(b)))
end
isapprox(a::TensorMixed, b::TensorTerm) = isapprox(a, MultiVector(b))
isapprox(b::TensorTerm, a::TensorMixed) = isapprox(a, MultiVector(b))
isapprox(a::TensorTerm, b::TensorTerm) = isapprox(MultiVector(a), MultiVector(b))
isapprox(a::S,b::T) where {S<:TensorMixed,T<:TensorTerm} = isapprox(a, MultiVector(b))
isapprox(b::S,a::T) where {S<:TensorTerm,T<:TensorMixed} = isapprox(a, MultiVector(b))
isapprox(a::S,b::T) where {S<:TensorTerm,T<:TensorTerm} = isapprox(MultiVector(a), MultiVector(b))

"""
scalar(multivector)
Return the scalar (grade 0) part of any multivector.
"""
scalar(a::TensorMixed) = grade(a) == 0 ? a[1] : 0
scalar(a::MultiVector) = a[0][1]
scalar(a::TensorAlgebra) = scalar(MultiVector(a))
scalar(t::T) where T<:TensorTerm{V,0} where V = t
scalar(t::T) where T<:TensorTerm{V} where V = zero(V)
scalar(t::MultiVector) = t(0,1)
for Blade MSB
@eval begin
scalar(t::$Blade{T,V,0} where {T,V}) = a(1)
scalar(t::$Blade{T,V} where T) where V = zero(V)
end
end

vector(t::T) where T<:TensorTerm{V,1} where V = t
vector(t::T) where T<:TensorTerm{V} where V = zero(V)
vector(t::MultiVector) = t(1)
for Blade MSB
@eval begin
vector(t::$Blade{T,V,1} where {T,V}) = a
vector(t::$Blade{T,V} where T) where V = zero(V)
end
end

volume(t::T) where T<:TensorTerm{V,G} where {V,G} = G == ndims(V) ? t : zero(V)
volume(t::MultiVector{T,V} where T) where V = t(ndims(V),1)
for Blade MSB
@eval begin
volume(t::$Blade{T,V,G} where T) where {V,G} = G == ndims(V) ? t : zero(V)
end
end

## MultiGrade{N}

Expand Down

0 comments on commit 5e1ddbb

Please sign in to comment.