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
+74-16Lines changed: 74 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@
5
5
6
6
Module Elements –- elements of free modules.
7
7
8
-
A `ModuleElt{K,V}` represents an element of a free module where basis elements are of type `K` and coefficients of type `V`. Usually you want objects of type `V` to be elements of a ring, but it could also be useful if they just belong to an abelian group. This is similar to the SageMath CombinatorialFreeModule. You can also see them as `SparseVector`s where keys can be type `K` instead of integers.
8
+
A `ModuleElt{K,V}` represents an element of a free module where basis elements are of type `K` and coefficients of type `V`. Usually you want objects of type `V` to be elements of a (non-necessarily commutative) ring, but it could also be useful if they just belong to an abelian group. This is similar to the SageMath CombinatorialFreeModule. You can also see them as `SparseVector`s where keys can be of type `K` instead of integers.
9
9
10
10
This basic data structure is used in my packages as an efficient representation at many places. For example, the `Monomial` type representing multivariate monomials is a `ModuleElt{Symbol,Int}`:
11
11
@@ -25,17 +25,17 @@ We provide two implementations:
25
25
26
26
This requires that the type `K` is hashable. It is a very simple implementation since the interface of the type is close to that of dicts; the only difference is weeding out keys which have a zero cofficient –- which is necessary since for testing equality of module elements one needs a canonical form for each element.
27
27
28
-
*`ModuleElt`, a faster implementation which keeps a list of pairs sorted by key.
28
+
*`ModuleElt`, a faster implementation by a vector of pairs sorted by key.
29
29
30
-
This demands that the type `K` has a `isless` method. This implementation is two to four times faster than the `Dict` one and requires half the memory.
30
+
This requires that the type `K` has a `isless` method. This implementation is two to four times faster than the `Dict` one and requires half the memory.
31
31
32
32
Both implementations have the same methods, which are mostly the same methods as a `Dict` (`haskey`, `getindex`, `setindex`, `keys`, `values`. `pairs`, `first`, `iterate`, `length`, `eltype`, `copy`), with some exceptions. Adding elements is implemented as `merge(+,...)` which is a variation on `merge` for `Dict`s where keys with zero value are deleted after the operation (here `+` can be replaced by any operation `op` with the property that `op(0,x)=op(x,0)=x`).
33
33
34
-
A module element can also be negated, or multiplied or divided (`/`or `//`) by some element (acting on coefficients) if the method is defined between type `V` and that element; there are also `zero` and `iszero` methods.
34
+
A module element can also be negated, or multiplied or divided (`/`or `//` or `\`) by some element (acting on coefficients) if the method is defined between type `V` and that element; the order of the arguments is respected, which allows to implement left and right modules when multiplication for `V` is non-commutative. There are also `zero` and `iszero` methods.
35
35
36
-
`ModuleElt`s have methods `cmp`and `isless` which `HModuleElt`s don't have. There is also `ModuleElts.merge2` which does the same as merge but is valid for more general operations (I use it with`min` and `max` which implement `gcd` and `lcm` for `Monomial`s and `CycPol`s).
36
+
`ModuleElt`s have methods `cmp` and `isless` which `HModuleElt`s don't have (the definition is lexicographic order). There is also `ModuleElts.merge2` which does the same as merge but is valid for more general operations – thus is more expensive since it needs more checks for zero results (I use it with `min` and `max` which implement `gcd` and `lcm` for `Monomial`s and `CycPol`s).
37
37
38
-
Here is an example where basis elements are `Symbol`s and coefficients are `Int`. As you can see in the examples, at the REPL (or in Jupyter or Pluto, when `IO`has the `:limit`attribute) the `show`method shows the coefficients (bracketed if necessary, which is when they have inner occurences of `+-*/`), followed by showing the basis elements. The `repr` method gives a representation which can be read back in julia:
38
+
We show now an an example; here basis elements are `Symbol`s and coefficients are `Int`. As you can see in the examples, at the REPL (or in Jupyter or Pluto, when `IO` has the `:limit` attribute) the `show` method gives a nice display where the coefficients (bracketed if necessary, which is when they have inner occurences of `+-*/`) preced the keys. The `repr` method gives a representation which can be read back in julia:
`ModuleElt{K,V}` has a similar interface to `Dict{K,V}`, but instead of assuming that `K` is hashable, it assumes that `K` is sortable. It also has the advantage that ModuleElts are naturally sortable.
142
+
143
+
The only field, a `Vector{Pair{K,V}}`, is kept sorted by `K`; the constructor by default checks sorting, adds values with same key, and suppresses keys with zero value. This can be bypassed by the keyword `check=false`.
is like `merge(op,a,b)` for `Dict`s, except that keys with value `0` are deleted after the operation is done.
156
+
157
+
The code is only valid for `op`s such that `op(0,x)=op(x,0)=x` otherwise the result is wrong. You can use `ModuleElts.merge2` for a (more expensive) function which always works.
158
+
159
+
`merge(op,m::ModuleElt,b)` does `v->op(v,b)` on the values of `m`.
160
+
161
+
`merge(op,b,m::ModuleElt)` does `v->op(b,v)` on the values of `m`.
162
+
163
+
`merge(op,m::ModuleElt)` does `v->op(v)` on the values of `m`.
does `op` between coefficients of the same basis element in `a` and `b`. This version works for general ops (not necessarily commutative or which need not satisfy op(0,x)=op(x,0)=x), but has too much overhead currently to replace `merge` for + or other ops such that op(0,x)==op(x,0)=x. It is useful for max or min which do lcm and gcd of `Monomial`s or `CycPol`s.
0 commit comments