chakravala / Grassmann.jl

⟨Grassmann-Clifford-Hodge⟩ multilinear differential geometric algebra
https://grassmann.crucialflow.com
GNU Affero General Public License v3.0
464 stars 39 forks source link

Errors with symbolic coefficients, mixed-grades multivectors / operators related ? (both Reduce and SymEngine) #85

Closed 6r4ph closed 2 years ago

6r4ph commented 3 years ago

Hi,

I'm just getting started with Julia in order to experiment with Grassmann.jl (for Lie Sphere Algebra, multi-qubits, etc), which seems like a very interesting library to work with.

Setup : Julia 1.5.3 (fresh install), Reduce v1.2.10, Grassmann v0.7.2, on Windows 8.1 x64, I also installed all packages listed as possible dependencies (AbstractTensors, ComputedFieldTypes, etc). Grassmann v0.7.1 and Reduce v1.2.9 also create these errors.

Reduce and Grassmann seem to be working quite fine on their own. However I necessarily have to use @force using Reduce.Algebra and when using both using Reduce; @force using Reduce.Algebra; using Grassmann doesn't allow the same operations as using Reduce; @force using Reduce.Algebra; using Grassmann (see below). Grassmann generates bases of all kinds with no troubles and correctly performs operations on multivectors using operators (∧, ⋅, ^2, etc). However when using both (eg. defining r = :x v1 + :y v12, say) I run into a lot of errors, which are listed below. Some operations do work, and return the correct results, other produce weird results or errors, please see below for examples of outputs in various cases. The problems seem mostly related to expressions returning mixed-grades multivectors. (:x + v1 + :y v2 works fine, 1 + :x v12 doesn't). I'm also having trouble when using SymEngine instead of Reduce, which is why I'm posting it in this rep, although it seems quite related to Reduce.jl issue #38.

Any ideas where this might be coming from ?

Thanks and best regards, Julien

julia> using Reduce

julia> @force using Reduce.Algebra

julia> using Grassmann
WARNING: using Grassmann.vector in module Main conflicts with an existing identifier.
WARNING: using Grassmann.odd in module Main conflicts with an existing identifier.
WARNING: using Grassmann.select in module Main conflicts with an existing identifier.
WARNING: using Grassmann.even in module Main conflicts with an existing identifier.

julia> (:x + :y + :x * :y)
:((y + 1) * x + y)

julia> sqrt(ans)
:(sqrt((y + 1) * x + y))

julia> 1 - 1/:n
:((n - 1) / n)

julia> :a + :b / :c
:((a * c + b) / c)

julia> basis"3"
(⟨×××⟩, v, v₁, v₂, v₃, v₁₂, v₁₃, v₂₃, v₁₂₃)

julia> x = :a * v1
av₁

julia> x^2
ERROR: MethodError: no method matching *(::Simplex{⟨×××⟩,0,v,Int64}, ::Expr)
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
  *(::Simplex{V,G,B,T} where B, ::F) where {F<:Real, V, G, T<:Real} at C:\Users\maison\.julia\packages\DirectSum\aSUXQ\src\DirectSum.jl:407
  *(::Simplex{V,G,B,T} where T where B where G, ::SubManifold{V,n,Indices} where Indices where n) where V at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\algebra.jl:32
  ...
Stacktrace:
 [1] ^(::Simplex{⟨×××⟩,1,v₁,Symbol}, ::Int64) at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\algebra.jl:383
 [2] ^ at C:\Users\maison\.julia\packages\AbstractTensors\f9LPE\src\AbstractTensors.jl:315 [inlined]
 [3] literal_pow(::typeof(^), ::Simplex{⟨×××⟩,1,v₁,Symbol}, ::Val{2}) at .\intfuncs.jl:283
 [4] top-level scope at none:1

julia> x * x
(a ^ 2)v

julia> (:a * v1 + :b * v2) · (:c * v1 + :d * v2)
ERROR: syntax: invalid character "·" near column 21

julia> (:a * v1 + :b * v2) | (:c * v1 + :d * v2)
(-(((impart(a) * im - repart(a)) * c + (impart(b) * im - repart(b)) * d)))v

julia> (:a * v1 + :b * v2) * (:c * v1 + :d * v2)
a * c + b * d + (a * d - b * c)v₁₂

julia> (:a * v1 + :b * v2) ∧ (:c * v1 + :d * v2)
ERROR: StackOverflowError:
Stacktrace:
 [1] promote_result(::Type{T} where T, ::Type{T} where T, ::Type{Any}, ::Type{Chain{⟨×××⟩,1,Any,3}}) at .\promotion.jl:237
 [2] promote_type at .\promotion.jl:223 [inlined]
 ... (the last 2 lines are repeated 79982 more times)

julia> v1 + v12
0 + 1v₁ + 1v₁₂

julia> :a * v1 + v12
ERROR: MethodError: no method matching setmulti!(::AbstractTensors.Variables{8,Any}, ::Symbol, ::UInt64, ::Val{3})
Closest candidates are:
  setmulti!(::AbstractTensors.Variables{M,T}, ::S, ::Any) where {M, T, S} at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:166
  setmulti!(::Q, ::S, ::UInt64, ::Val{N}) where {M, T, S, N, Q<:AbstractTensors.FixedVector{M,T}} at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:139
  setmulti!(::Q, ::S, ::UInt64, ::Val{N}) where {M, T<:Number, S<:Number, N, Q<:AbstractTensors.Variables{M,T}} at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:139
  ...
Stacktrace:
 [1] macro expansion at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\algebra.jl:538 [inlined]
 [2] macro expansion at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:326 [inlined]
 [3] +(::Simplex{⟨×××⟩,1,v₁,Symbol}, ::SubManifold{⟨×××⟩,2,0x0000000000000003}) at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:326
 [4] ∑(::Simplex{⟨×××⟩,1,v₁,Symbol}, ::SubManifold{⟨×××⟩,2,0x0000000000000003}) at C:\Users\maison\.julia\packages\AbstractTensors\f9LPE\src\AbstractTensors.jl:328
 [5] top-level scope at none:1

Changing the order of using and @force using produces a different behavior of / and

julia> using Reduce, Grassmann

julia> @force using Reduce.Algebra

julia> 1 - 1/:n
ERROR: MethodError: no method matching /(::Int64, ::Symbol)
Closest candidates are:
  /(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, ::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) at int.jl:92
  /(::T, ::T) where T<:Integer at int.jl:90
  /(::R, ::S) where {R<:Real, S<:Complex} at complex.jl:332
  ...
Stacktrace:
 [1] top-level scope at none:1

julia> 1 - 1//:n
:((n - 1) / n)

julia> (:a * v1 + :b * v2) · (:c * v1 + :d * v2)
(-(((impart(a) * im - repart(a)) * c + (impart(b) * im - repart(b)) * d)))v

Same kind of errors using SymEngine instead

julia> using SymEngine, Grassmann

 julia> basis"3"
 (⟨×××⟩, v, v₁, v₂, v₃, v₁₂, v₁₃, v₂₃, v₁₂₃)

 julia> @vars a b c d
 (a, b, c, d)

 julia> x = (a * v1 + b * v2)
 (a)v₁ + (b)v₂ + (0.0)v₃

 julia> x * x
 a^2 + b^2 + (0.0)v₁ + (0.0)v₂ + (0.0)v₃ + (0.0)v₁₂₃

 julia> x | x
 ERROR: MethodError: no method matching conj(::Basic)
 Closest candidates are:
   conj(::Missing) at missing.jl:100
   conj(::Real) at number.jl:167
   conj(::Complex) at complex.jl:263
   ...
 Stacktrace:
  [1] dot(::Basic, ::Basic) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\generic.jl:884
  [2] macro expansion at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:484 [inlined]
  [3] contraction(::Chain{⟨×××⟩,1,Basic,3}, ::Chain{⟨×××⟩,1,Basic,3}) at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:484
  [4] |(::Chain{⟨×××⟩,1,Basic,3}, ::Chain{⟨×××⟩,1,Basic,3}) at C:\Users\maison\.julia\packages\AbstractTensors\f9LPE\src\AbstractTensors.jl:163
  [5] top-level scope at none:1

 julia> x ∧ x
 ERROR: MethodError: promote_rule(::Type{Chain{⟨×××⟩,1,Basic,3}}, ::Type{Basic}) is ambiguous. Candidates:
   promote_rule(::Type{S}, ::Type{Basic}) where S<:Number in SymEngine at C:\Users\maison\.julia\packages\SymEngine\Qs90w\src\types.jl:97
   promote_rule(::Type{Chain{V,G,T,B}}, ::Type{S}) where {V, G, T, B, S} in Grassmann at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:421
 Possible fix, define
   promote_rule(::Type{Chain{V,G,T,B}}, ::Type{Basic}) where {V, G, T, B}
 Stacktrace:
  [1] promote_type(::Type{Basic}, ::Type{Chain{⟨×××⟩,1,Basic,3}}) at .\promotion.jl:223
  [2] promote_type(::Type{T} where T, ::Vararg{Type{T} where T,N} where N) at C:\Users\maison\.julia\packages\Leibniz\ke93r\src\utilities.jl:22
  [3] product_∧(::Type{Chain{⟨×××⟩,1,Basic,3}}, ::Type{Chain{⟨×××⟩,1,Basic,3}}, ::Bool) at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\algebra.jl:812
  [4] product_∧(::Type{Chain{⟨×××⟩,1,Basic,3}}, ::Type{Chain{⟨×××⟩,1,Basic,3}}) at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\algebra.jl:806
  [5] #s224#68 at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:493 [inlined]
  [6] #s224#68(::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any, ::Any) at .\none:0
  [7] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at .\boot.jl:527
  [8] top-level scope at none:1

  julia> :x * v1 + :y * v2
ERROR: MethodError: no method matching +(::Symbol)
Closest candidates are:
  +(::Union{Expr, Symbol, var"#s64", var"#s65"} where var"#s64"<:Complex where var"#s65"<:Real, ::T) where T<:TensorGraded at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:322
  +(::Union{Expr, Symbol, var"#s64", var"#s65"} where var"#s64"<:Complex where var"#s65"<:Real, ::T) where T<:TensorMixed at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:322
  +(::Any, ::ChainRulesCore.DoesNotExist) at C:\Users\maison\.julia\packages\ChainRulesCore\7d1hl\src\differential_arithmetic.jl:24
  ...
Stacktrace:
 [1] ∑ at C:\Users\maison\.julia\packages\AbstractTensors\f9LPE\src\AbstractTensors.jl:328 [inlined]
 [2] macro expansion at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:326 [inlined]
 [3] +(::Simplex{⟨×××⟩,1,v₁,Symbol}, ::Simplex{⟨×××⟩,1,v₂,Symbol}) at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:326
 [4] top-level scope at none:1
chakravala commented 3 years ago

It is not recommended to use the @force using Reduce.Algebra command combined with using Grassmann.

Instead, you should just do using Reduce, Grassmann and dispatch Reduce.Algebra manually.

6r4ph commented 3 years ago

Thank you for this very fast answer and for this precision !

I added it thinking it may solve some errors, which I also get with using Reduce, Grassmann only :

julia> using Reduce, Grassmann

 julia> @basis ℝ^3
(⟨+++⟩, v, v₁, v₂, v₃, v₁₂, v₁₃, v₂₃, v₁₂₃)

julia> :x * v1 + v12
ERROR: MethodError: no method matching setmulti!(::AbstractTensors.Variables{8,Any}, ::Symbol, ::UInt64, ::Val{3})
Closest candidates are:
  setmulti!(::AbstractTensors.Variables{M,T}, ::S, ::Any) where {M, T, S} at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:166
  setmulti!(::Q, ::S, ::UInt64, ::Val{N}) where {M, T, S, N, Q<:AbstractTensors.FixedVector{M,T}} at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:139
  setmulti!(::Q, ::S, ::UInt64, ::Val{N}) where {M, T<:Number, S<:Number, N, Q<:AbstractTensors.Variables{M,T}} at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:139
  ...
Stacktrace:
 [1] macro expansion at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\algebra.jl:538 [inlined]
 [2] macro expansion at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:326 [inlined]
 [3] +(::Simplex{⟨+++⟩,1,v₁,Symbol}, ::SubManifold{⟨+++⟩,2,0x0000000000000003}) at C:\Users\maison\.julia\packages\Grassmann\Tvf1n\src\products.jl:326
 [4] top-level scope at none:1

 julia> (:a * v1 + :b * v2) ∧ (:c * v1 + :d * v2)
 ERROR: StackOverflowError:
 Stacktrace:
  [1] promote_result(::Type{T} where T, ::Type{T} where T, ::Type{Any}, ::Type{Chain{⟨+++⟩,1,Any,3}}) at .\promotion.jl:237
  [2] promote_type at .\promotion.jl:223 [inlined]
  ... (the last 2 lines are repeated 79982 more times)
chakravala commented 3 years ago

Thanks for reporting these examples. This situation can probably be improved; however, it is not my immediate concern to improve the situation involving symbolic computation interoperability, although I may attend to this in the future when I feel like addressing it.

chakravala commented 2 years ago

@KabalGraph all of the issues described in this issue have now been fixed in v0.8 of Grassmann, which is available on the latest commit but hasn't been released as of yet.