Skip to content

Commit

Permalink
Fixed the case where scalars divide multivectors
Browse files Browse the repository at this point in the history
  • Loading branch information
brainandforce committed May 12, 2024
1 parent 79e5712 commit 637fdc8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,27 @@ end

# TODO: is it more efficient to define some more specific methods for some types?

#---Scalar multiplication--------------------------------------------------------------------------#
#---Scalar multiplication and division-------------------------------------------------------------#

for op in (:*, :/, ://)
@inline function *(x::AbstractCliffordNumber, y::Number)
data = map(_x -> (_x * y), Tuple(x))
return similar_type(typeof(x), eltype(data))(data)
end

# Don't assume commutative multiplication, just to be safe
@inline function *(x::Number, y::AbstractCliffordNumber)
data = map(_y -> (x * _y), Tuple(y))
return similar_type(typeof(y), eltype(data))(data)
end

for op in (:/, ://)
@eval begin
@inline function $op(x::AbstractCliffordNumber, y::BaseNumber)
data = map(_x -> $op(_x, y), Tuple(x))
return similar_type(typeof(x), eltype(data))(data)
end
@inline function $op(x::BaseNumber, y::AbstractCliffordNumber)
data = map(_y -> $op(x, _y), Tuple(y))
data = Tuple(y') .* $op(x, sum(Tuple(y) .* Tuple(y')))
return similar_type(typeof(y), eltype(data))(data)
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ end
@test KVector{0,VGA(3)}(2) * KVector{0,VGA(3)}(3) === KVector{0,VGA(3)}(6)
end

@testset "Inverses" begin
k = KVector{1,VGA(3)}(1, 2, 3)
l = KVector{2,VGA(3)}(4, 5, 6)
# TODO: fix approximate equality methods to consolidate these tests
@test isscalar(k * (1 / k))
@test scalar(k * (1 / k)) 1
@test isscalar((1 / k) * k)
@test scalar((1 / k) * k) 1
@test isscalar(k * (1 / k))
@test scalar(k * (1 / k)) 1
@test isscalar((1 / k) * k)
@test scalar((1 / k) * k) 1
end

@testset "Contractions" begin
k = KVector{1,VGA(3)}(1, 2, 3)
l = KVector{2,VGA(3)}(4, 5, 6)
Expand Down

0 comments on commit 637fdc8

Please sign in to comment.