Skip to content

Commit a80d9cf

Browse files
committed
Minimal docs
1 parent 6944ec5 commit a80d9cf

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,115 @@
44
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://YingboMa.github.io/SIMDPolynomials.jl/dev)
55
[![Build Status](https://github.com/YingboMa/SIMDPolynomials.jl/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/YingboMa/SIMDPolynomials.jl/actions/workflows/CI.yml?query=branch%3Amaster)
66
[![Coverage](https://codecov.io/gh/YingboMa/SIMDPolynomials.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/YingboMa/SIMDPolynomials.jl)
7+
8+
---
9+
10+
SIMDPolynomials.jl uses bit-packed monomials so that most of the operations on
11+
multivariate monomials can be done in a few CPU instructions. Currently, it is
12+
only optimized for relatively small polynomials. Contributions, especially
13+
optimizations for large polynomials, are welcome!
14+
15+
Examples:
16+
```julia
17+
julia> using SIMDPolynomials, BenchmarkTools
18+
19+
julia> x, y, z, t = [PackedMonomial{4,7}(i) for i in 0:3]; # PackedMonomial with maximum of 4 variables and 7 bits of exponents.
20+
21+
julia> p = x * y + 3 * (z * t)
22+
x₀x₁ + 3x₂x₃
23+
24+
julia> q = (p + 1) * p
25+
x₀²x₁² + 6x₀x₁x₂x₃ + 9x₂²x₃² + x₀x₁ + 3x₂x₃
26+
27+
julia> @btime gcd($p, $q)
28+
4.019 μs (94 allocations: 8.06 KiB)
29+
x₀x₁ + 3x₂x₃
30+
31+
julia> begin
32+
c1 = 10*(x * z + x)
33+
c2 = 2*(x^2 + z)
34+
c3 = 2*(2 - z )
35+
c4 = 20*(x * z^2)
36+
e1 = 0
37+
e2 = 5
38+
e3 = 7
39+
e4 = 10
40+
p = c1 * y^e1 + c2 * y^e2 + c3 * y^e3 + c4 * y^e4
41+
q = prod(i->p + i, 0:3)
42+
end;
43+
44+
julia> @btime for i in 0:3
45+
gcd($p + i, $q)
46+
end
47+
350.086 μs (1159 allocations: 588.06 KiB)
48+
```
49+
50+
The same micro-benchmark using AbstractAlgebra:
51+
```julia
52+
julia> using AbstractAlgebra, BenchmarkTools
53+
54+
julia> R, (x, y, z, t) = PolynomialRing(AbstractAlgebra.Integers{Int}(), [:x, :y, :z, :t], ordering=:deglex);
55+
56+
julia> p = x * y + 3 * (z * t)
57+
x*y + 3*z*t
58+
59+
julia> q = (p + 1) * p
60+
x^2*y^2 + 6*x*y*z*t + 9*z^2*t^2 + x*y + 3*z*t
61+
62+
julia> @btime gcd($p, $q) # SIMDPolynomials.jl is 30x faster
63+
119.795 μs (1320 allocations: 89.17 KiB)
64+
x*y + 3*z*t
65+
66+
julia> begin
67+
c1 = 10*(x * z + x)
68+
c2 = 2*(x^2 + z)
69+
c3 = 2*(2 - z )
70+
c4 = 20*(x * z^2)
71+
e1 = 0
72+
e2 = 5
73+
e3 = 7
74+
e4 = 10
75+
p = c1 * y^e1 + c2 * y^e2 + c3 * y^e3 + c4 * y^e4
76+
q = prod(i->p + i, 0:3)
77+
end;
78+
79+
julia> @btime for i in 0:3 # SIMDPolynomials.jl is 14x faster
80+
gcd($p + i, $q)
81+
end
82+
4.934 ms (32235 allocations: 3.43 MiB)
83+
```
84+
85+
The same micro-benchmark using DynamicPolynomials:
86+
```julia
87+
julia> using DynamicPolynomials, BenchmarkTools
88+
89+
julia> @polyvar x y z t;
90+
91+
julia> p = x * y + 3 * (z * t)
92+
xy + 3zt
93+
94+
julia> q = (p + 1) * p
95+
x²y² + 6xyzt + 9z²t² + xy + 3zt
96+
97+
julia> @btime gcd($p, $q) # SIMDPolynomials.jl is 65x faster
98+
264.561 μs (4962 allocations: 298.19 KiB)
99+
xy + 3zt
100+
101+
julia> begin
102+
c1 = 10*(x * z + x)
103+
c2 = 2*(x^2 + z)
104+
c3 = 2*(2 - z )
105+
c4 = 20*(x * z^2)
106+
e1 = 0
107+
e2 = 5
108+
e3 = 7
109+
e4 = 10
110+
p = c1 * y^e1 + c2 * y^e2 + c3 * y^e3 + c4 * y^e4
111+
q = prod(i->p + i, 0:3)
112+
end;
113+
114+
julia> @btime for i in 0:3 # SIMDPolynomials.jl is 82x faster
115+
gcd($p + i, $q)
116+
end
117+
28.943 ms (529642 allocations: 31.20 MiB)
118+
```

src/packedmonomial.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ function calc_K(::Val{L},::Val{E}) where {L,E}
3030
end
3131
end
3232
new_E(::Val{E}) where {E} = max(Base._nextpow2(1+E)-1, 7)
33+
34+
"""
35+
PackedMonomial{L,E}
36+
37+
Bit packed monomial with maximum of L variables and E bits of exponents.
38+
"""
3339
struct PackedMonomial{L,E,K} <: AbstractMonomial
3440
bits::NTuple{K,UInt64}
3541
function PackedMonomial{L,E}() where {L,E}

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ end
125125
e3 = 7
126126
e4 = 10
127127
p = c1 * y^e1 + c2 * y^e2 + c3 * y^e3 + c4 * y^e4
128-
q = prod(i->p + i, 0:3)
128+
q = prod(i->p + i, 0:3);
129129
@test length(terms(q)) == 262
130130
for i in 0:3
131131
@test test_gcd(p + i, q) == p + i

0 commit comments

Comments
 (0)