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
+20-21Lines changed: 20 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,11 @@
2
2
**`ModuleElts`**—*Module*.
3
3
4
4
5
-
6
5
Module Elements –- elements of free modules.
7
6
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.
7
+
A `ModuleElt{K,V}` represents an element of a free module where the basis elements are of type `K` and the coefficients are of type `V`. Usually you want objects of type `V` to be elements of a (not necessarily commutative) ring, but it can also be useful if they just belong to an abelian group. This is similar to the SageMath CombinatorialFreeModule. You can also think of them as `SparseVector`s, where the keys can be of type `K` instead of integers.
9
8
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}`:
9
+
This basic data structure is used in many places in my packages as an efficient representation. For example, the type `Monomial`, which represents multivariate monomials is a `ModuleElt{Symbol,Int}`:
11
10
12
11
`x^2y^-3` is represented by `ModuleElt(:x=>2,:y=>-3)`
13
12
@@ -23,19 +22,19 @@ We provide two implementations:
23
22
24
23
*`HModuleElt`, an implementation by `Dict`s
25
24
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.
25
+
This requires the type `K`to be hashable. This is a very simple implementation since the interface of the type is close to that of dicts; the only difference is that keys with cofficient zero are discarded –- which is necessary, since for checking the equality of module elements one needs a canonical form for each element.
27
26
28
27
*`ModuleElt`, a faster implementation by a vector of pairs sorted by key.
29
28
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.
29
+
This requires that the type `K` has an`isless` method. This implementation is two to four times faster than the `Dict`implementation and requires half the memory.
31
30
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`).
31
+
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 coefficient zero are discarded after the operation (here `+` can be replaced by any operation `op` with the property that `op(0,x)=op(x,0)=x`).
33
32
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.
33
+
A module element can also be negated, or multiplied or divided (`/`or `//` or `\`) by any element (acting on coefficients) if the method is defined between the type `V`and that element; the order of the arguments is respected, which allows to implement left and right modules if the multiplication is not commutative for `V`. There are also `zero` and `iszero` methods.
35
34
36
35
`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
36
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:
37
+
We now show an an example; here the basis elements are `Symbol`s and the coefficients are `Int`. As you can see from 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, that is when they have inner occurrences of `+-*/`) precede the keys. The `repr` method gives a representation which can be read back in julia:
39
38
40
39
```julia-repl
41
40
julia> a=ModuleElt(:xy=>1,:yx=>-1)
@@ -113,7 +112,7 @@ julia> eltype(a)
113
112
Pair{Symbol, Int64}
114
113
```
115
114
116
-
In both implementations the constructor normalizes the constructed element, removing zero coefficients and merging duplicate basis elements, adding the corresponding coefficients (and sorting the basis in the default implementation). If you know this normalisation is unnecessary, to get maximum speed you can disable this by giving the keyword `check=false` to the constructor.
115
+
In both implementations the constructor normalises the constructed element, removing zero coefficients and merging duplicate basis elements, adding the corresponding coefficients (and sorting the basis in the default implementation). If you know that this normalisation is unnecessary, you can disable it for maximum speed by passing the keyword `check=false` to the constructor.
`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.
140
+
`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. This also has the advantage that ModuleElts are naturally sortable.
142
141
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`.
142
+
The only field, a `Vector{Pair{K,V}}`, is kept sorted by `K`; by default, the constructor checks sorting, adds values with the same key, and deletes keys with zero value. This can be overriden with the keyword `check=false`.
@@ -172,28 +171,28 @@ The code is only valid for `op`s such that `op(0,x)=op(x,0)=x` otherwise the
172
171
173
172
`merge2(op::Function,a::ModuleElt,b::ModuleElt)`
174
173
175
-
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.
174
+
does `op` between coefficients of the same basis element in `a` and `b`. This version works for general ops (not necessarily commutative or not satisfying op(0,x)=op(x,0)=x). It currently has too much overhead 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