JuliaAlgebra / DynamicPolynomials.jl

Multivariate polynomials implementation of commutative and non-commutative variables
Other
60 stars 20 forks source link

Lazy multiplication of polynomials #55

Open mforets opened 4 years ago

mforets commented 4 years ago

When computing with intervals (ref. IntervalArithmetic), the order in which an evaluation is performed matters. A simple example is

julia> using IntervalArithmetic

julia> a, b = -1..1, 2..3
[-1, 1]

julia> (a+b)*b
[2, 12]

julia> a*b + b*b
[1, 12]

The example shows that evaluating the factored expression gives a better (tighter) result than using the expanded expression.

Have you considered a "lazy" multiplication of polynomials within this package, such that the expression is hold until asked to be evaluated (i mean expanded symbolically)? A "factor" function would also help, but if it exists, i didn't see it. There is this related issue, but it hasn't received much attention.

To illustrate my use case in a concrete example, consider:

julia> using DynamicPolynomials

julia> @polyvar M[1:2, 1:2]
(PolyVar{true}[M₁₋₁ M₁₋₂; M₂₋₁ M₂₋₂],)

julia> first(M^3) # expands everything
M₁₋₁³ + 2M₁₋₁M₂₋₁M₁₋₂ + M₂₋₁M₁₋₂M₂₋₂

julia> using SymEngine

julia> M = convert(Matrix{SymEngine.Basic}, ["M₁₁" "M₁₂"; "M₂₁" "M₂₂"])
2×2 Array{Basic,2}:
 M₁₁  M₁₂
 M₂₁  M₂₂

julia> first(M^3) # keeps the terms factored
(M₂₁*M₁₂ + M₁₁^2)*M₁₁ + (M₂₁*M₁₁ + M₂₂*M₂₁)*M₁₂
blegat commented 4 years ago

There is no feature like that that I know of. This is the kind of features that could be built on top of the MultivariatePolynomials API