QuatRotation construction using symbolics
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
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.
x-ref: https://github.com/JuliaGeometry/Quaternions.jl/issues/123
@hyrodium Thanks a lot! Then, in short, is it not encouraged to use symbolics for this package for now?
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.
I think we have the following three ways to solve this problem:
- Create SymbolicsRotations.jl package with some type piracies.
- Support Symbolics.jl compatibility with "package extensions" in Rotations.jl. (https://github.com/JuliaLang/julia/pull/47695)
- Support Rotations.jl compatibility with "package extensions" in Symbolics.jl. (https://github.com/JuliaLang/julia/pull/47695)
I'm not familiar with Symbolics.jl, so I think the first and the last choices will be acceptable for now.
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.