JuliaGeometry / Rotations.jl

Julia implementations for different rotation parameterizations
https://juliageometry.github.io/Rotations.jl
MIT License
176 stars 44 forks source link

QuatRotation construction using symbolics #248

Open JinraeKim opened 1 year ago

JinraeKim commented 1 year ago

I was trying to use a symbolic to construct a QuatRotation but failed.

Can anyone help me to debug this or is it a limitation of this package?

using Symbolics
using Rotations

julia> @variables q[1:4] = [1.0, 0, 0, 0]
1-element Vector{Symbolics.Arr{Num, 1}}:
 q[1:4]

julia> QuatRotation(q...)
ERROR: MethodError: /(::Quaternions.Quaternion{Num}, ::Num) is ambiguous. Candidates:
  /(a::Number, b::Num) in Symbolics at /home/jinrae/.julia/packages/SymbolicUtils/qulQp/src/methods.jl:71
  /(q::Quaternions.Quaternion, x::Real) in Quaternions at /home/jinrae/.julia/packages/Quaternions/kqEsP/src/Quaternion.jl:123
Possible fix, define
  /(::Quaternions.Quaternion, ::Num)
Stacktrace:
 [1] sign(x::Quaternions.Quaternion{Num})
   @ Base ./number.jl:161
 [2] QuatRotation
   @ ~/.julia/packages/Rotations/VYTDG/src/unitquaternion.jl:22 [inlined]
 [3] QuatRotation
   @ ~/.julia/packages/Rotations/VYTDG/src/unitquaternion.jl:46 [inlined]
 [4] QuatRotation(w::Num, x::Num, y::Num, z::Num, normalize::Bool)
   @ Rotations ~/.julia/packages/Rotations/VYTDG/src/unitquaternion.jl:50
 [5] QuatRotation(w::Num, x::Num, y::Num, z::Num)
   @ Rotations ~/.julia/packages/Rotations/VYTDG/src/unitquaternion.jl:49
 [6] top-level scope
   @ REPL[35]:1
 [7] top-level scope
   @ ~/.julia/packages/Infiltrator/r3Hf5/src/Infiltrator.jl:710
hyrodium commented 1 year ago

The error is due to the normalization in the QuatRotation's constructor. This normalization can be avoided with the second variable.

julia> using Rotations

julia> using Symbolics

julia> @variables q[1:4] = [1.0, 0, 0, 0]
1-element Vector{Symbolics.Arr{Num, 1}}:
 q[1:4]

julia> r = QuatRotation(q..., false)
3×3 QuatRotation{Num} with indices SOneTo(3)×SOneTo(3)(Quaternion{Num}(q[1], q[2], q[3], q[4])):
 q[1]^2 + q[2]^2 - (q[3]^2) - (q[4]^2)                2q[2]*q[3] - 2q[1]*q[4]                2q[1]*q[3] + 2q[2]*q[4]
               2q[2]*q[3] + 2q[1]*q[4]  q[1]^2 + q[3]^2 - (q[2]^2) - (q[4]^2)                2q[3]*q[4] - 2q[1]*q[2]
               2q[2]*q[4] - 2q[1]*q[3]                2q[1]*q[2] + 2q[3]*q[4]  q[1]^2 + q[4]^2 - (q[2]^2) - (q[3]^2)

julia> r^2
3×3 QuatRotation{Num} with indices SOneTo(3)×SOneTo(3)(Quaternion{Num}(q[1]^2 - (q[2]^2) - (q[3]^2) - (q[4]^2), 2q[1]*q[2], 2q[1]*q[3], 2q[1]*q[4])):
 (q[1]^2 - (q[2]^2) - (q[3]^2) - (q[4]^2))^2 + 4(q[1]^2)*(q[2]^2) - 4(q[1]^2)*(q[3]^2) - 4(q[1]^2)*(q[4]^2)  …                                  4(q[1]^2 - (q[2]^2) - (q[3]^2) - (q[4]^2))*q[1]*q[3] + 8(q[1]^2)*q[2]*q[4]
                                 8(q[1]^2)*q[2]*q[3] + 4(q[1]^2 - (q[2]^2) - (q[3]^2) - (q[4]^2))*q[1]*q[4]                                     8(q[1]^2)*q[3]*q[4] - 4(q[1]^2 - (q[2]^2) - (q[3]^2) - (q[4]^2))*q[1]*q[2]
                                 8(q[1]^2)*q[2]*q[4] - 4(q[1]^2 - (q[2]^2) - (q[3]^2) - (q[4]^2))*q[1]*q[3]     (q[1]^2 - (q[2]^2) - (q[3]^2) - (q[4]^2))^2 + 4(q[1]^2)*(q[4]^2) - 4(q[1]^2)*(q[2]^2) - 4(q[1]^2)*(q[3]^2)

julia> principal_value(r)
ERROR: TypeError: non-boolean (Num) used in boolean context

Some methods (e.g. ^) are supported, but I think most of the methods in Rotations.jl (e.g. principal_value) does not work properly.

hyrodium commented 1 year ago

x-ref: https://github.com/JuliaGeometry/Quaternions.jl/issues/123

JinraeKim commented 1 year ago

@hyrodium Thanks a lot! Then, in short, is it not encouraged to use symbolics for this package for now?

hyrodium commented 1 year ago

Then, in short, is it not encouraged to use symbolics for this package for now?

Yes, that's right. If Symbolics.jl had compatibility with Quaternions.jl (https://github.com/JuliaGeometry/Quaternions.jl/issues/123), the original error in this issue will be fixed, but I don't know whether other operations work correctly.

hyrodium commented 1 year ago

I think we have the following three ways to solve this problem:

I'm not familiar with Symbolics.jl, so I think the first and the last choices will be acceptable for now.

sethaxen commented 1 year ago

One should test if simply making Symbolics compatible with Quaternions resolves this issue. If so, a cleaner way would be to add Symbolics support with package extensions to Quaternions.