You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-15Lines changed: 44 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,10 +9,10 @@ Sparse dynamic representation of multivariate polynomials that can be used with
9
9
Both commutative and non-commutative variables are supported.
10
10
The following types are defined:
11
11
12
-
*`PolyVar{C}`: A variable which is commutative with `*` when `C` is `true`. Commutative variables are created using the `@polyvar` macro, e.g. `@polyvar x y`, `@polyvar x[1:8]` and non-commutative variables are created likewise using the `@ncpolyvar` macro.
13
-
*`Monomial{C}`: A product of variables: e.g. `x*y^2`.
14
-
*`Term{C, T}`: A product between an element of type `T` and a `Monomial{C}`, e.g `2x`, `3.0x*y^2`.
15
-
*`Polynomial{C, T}`: A sum of `Term{C, T}`, e.g. `2x + 3.0x*y^2 + y`.
12
+
*`Variable{V,M}`: A variable which is commutative with `*` when `V<:Commutative`. Commutative variables are created using the `@polyvar` macro, e.g. `@polyvar x y`, `@polyvar x[1:8]` and non-commutative variables are created likewise using the `@ncpolyvar` macro. The type parameter `M` is the monomial ordering.
13
+
*`Monomial{V,M}`: A product of variables: e.g. `x*y^2`.
14
+
*`MultivariatePolynomials.Term{T,Monomial{V,M}}`: A product between an element of type `T` and a `Monomial{V,M}`, e.g `2x`, `3.0x*y^2`.
15
+
*`Polynomial{V,M,T}`: A sum of `Term{T,Monomial{V,M}}`, e.g. `2x + 3.0x*y^2 + y`.
16
16
17
17
All common algebraic operations between those types are designed to be as efficient as possible without doing any assumption on `T`.
18
18
Typically, one imagine `T` to be a subtype of `Number` but it can be anything.
@@ -30,19 +30,19 @@ julia> @polyvar x y # assigns x (resp. y) to a variable of name x (resp. y)
30
30
(x, y)
31
31
32
32
julia> p =2x +3.0x*y^2+ y # define a polynomial in variables x and y
33
-
3.0xy²+2.0x +y
33
+
y+2.0x +3.0xy²
34
34
35
35
julia>differentiate(p, x) # compute the derivative of p with respect to x
36
-
3.0y²+2.0
36
+
2.0+3.0y²
37
37
38
38
julia>differentiate.(p, (x, y)) # compute the gradient of p
39
-
(3.0y²+2.0, 6.0xy+1.0)
39
+
(2.0+3.0y², 1.0+6.0xy)
40
40
41
41
julia>p((x, y)=>(y, x)) # replace any x by y and y by x
42
-
3.0x²y+ x +2.0y
42
+
2.0y+ x +3.0x²y
43
43
44
44
julia>subs(p, y=>x^2) # replace any occurence of y by x^2
45
-
3.0x⁵+ x² +2.0x
45
+
2.0x + x² +3.0x⁵
46
46
47
47
julia>p(x=>1, y=>2) # evaluate p at [1, 2]
48
48
16.0
@@ -53,21 +53,50 @@ Below is an example with `@polyvar x[1:n]`
53
53
julia> n =3;
54
54
55
55
julia>@polyvar x[1:n] # assign x to a tuple of variables x1, x2, x3
julia> p =sum(x .* x) # compute the sum of squares
59
-
x₁² + x₂² + x₃²
59
+
x₃² + x₂² + x₁²
60
60
61
61
julia>subs(p, x[1]=>2, x[3]=>3) # make a partial substitution
62
-
x₂²+13
62
+
13+x₂²
63
63
64
64
julia> A =reshape(1:9, 3, 3);
65
65
66
66
julia>p(x => A *vec(x)) # corresponds to dot(A*x, A*x), need vec to convert the tuple to a vector
67
-
14x₁² +64x₁x₂ +100x₁x₃ +77x₂² +244x₂x₃ +194x₃²
67
+
194x₃² +244x₂x₃ +77x₂² +100x₁x₃ +64x₁x₂ +14x₁²
68
68
```
69
-
Note that, when doing substitution, it is required to give the `PolyVar` ordering that is meant.
70
-
Indeed, the ordering between the `PolyVar` is not alphabetical but rather by order of creation
69
+
70
+
The terms of a polynomial are ordered in increasing monomial order. The default
71
+
ordering is the graded lex order but it can be modified using the
72
+
`monomial_order` keyword argument of the `@polyvar` macro.
73
+
We illustrate this below by borrowing the example p. 59 of "Ideals, Varieties and Algorithms"
74
+
of Cox, Little and O'Shea:
75
+
```julia
76
+
julia>p(x, y, z) =4x*y^2*z +4z^2-5x^3+7x^2*z^2
77
+
p (generic function with 1 method)
78
+
79
+
julia>@polyvar x y z monomial_order = LexOrder
80
+
(x, y, z)
81
+
82
+
julia>p(x, y, z)
83
+
4z² +4xy²z +7x²z² -5x³
84
+
85
+
julia>@polyvar x y z
86
+
(x, y, z)
87
+
88
+
julia>p(x, y, z)
89
+
4z² -5x³ +4xy²z +7x²z²
90
+
91
+
julia>@polyvar x y z monomial_order = Graded{Reverse{InverseLexOrder}}
92
+
(x, y, z)
93
+
94
+
julia>p(x, y, z)
95
+
4z² -5x³ +7x²z² +4xy²z
96
+
```
97
+
98
+
Note that, when doing substitution, it is required to give the `Variable` ordering that is meant.
99
+
Indeed, the ordering between the `Variable` is not alphabetical but rather by order of creation
71
100
which can be undeterministic with parallel computing.
72
101
Therefore, this order cannot be used for substitution, even as a default (see [here](https://github.com/JuliaAlgebra/MultivariatePolynomials.jl/issues/3) for a discussion about this).
0 commit comments