ATell-SoundTheory / CliffordAlgebras.jl

A fast and lightweight Julia package for Clifford and geometric algebras.
https://atell-soundtheory.github.io/CliffordAlgebras.jl/
MIT License
36 stars 4 forks source link

Iterating over multivectors and applying function? #10

Closed davibarreira closed 1 year ago

davibarreira commented 1 year ago

Hey, me again. So, I'm writing some auxiliary functions, such as isapprox. I was wondering what is the "correct" way of iterating over a multivector. For example, it would be nice to have map(f, a::Multivector).

Based on the last answer you gave me, it's not advisable to use the vector function to turn the multivector into an iterable vector. My question is, what is the correct way of doing this? Here is my naive implementation of the isapprox function:

function Base.:≈(a::MultiVector,b::MultiVector)
    for e in propertynames(a)
        if !isapprox(getproperty(a,e),getproperty(b,e))
            return false
        end
    end
    return true
end

Now,

ATell-SoundTheory commented 1 year ago

Your implementation is mostly correct. I would enforce a and b be from the same algebra. You can also use coefficient to extract the coefficients without reference to the properties:

function Base.:≈(a::MultiVector{CA},b::MultiVector{CA}) where {CA}
    K = order(CA)
    N = 2^K
    for n = 1:N
        if !isapprox(coefficient(a,n), coefficient(b,n))
            return false
        end
    end
    return true
end

However, for large algebras you may want to use the sparse encoding of the multi vectors, which can be accessed using coefficients and baseindices. With this, you can check if the coefficients of a and b are approximately the same for the intersection of their respective base indices and approximately zero for the set differences.

This is clearly more complicated than the code above, but may be worth it if you need the performance.