Skip to content

Commit

Permalink
Prefer symbols for VarNames over strings
Browse files Browse the repository at this point in the history
Both are valid and allowed and will remain so. But using symbols overall
avoids a bunch of allocations, so it makes sense to adjust our internal
code as well as user-facing documentation to use symbols to set a good
precedent.

To demonstrate the effect, here a benchmark with strings:

    julia> @Btime polynomial_ring(QQ, ["x", "y"]);
      756.274 ns (27 allocations: 1.22 KiB)

Here the same with symbols:

    julia> @Btime polynomial_ring(QQ, [:x, :y]);
      535.963 ns (20 allocations: 896 bytes)

Of course code where this noticeable would be code that calls polynomial_ring
far too many times, and should be changed to not do *that*.
  • Loading branch information
fingolfin committed Sep 20, 2024
1 parent 0f40154 commit d3338f2
Show file tree
Hide file tree
Showing 31 changed files with 233 additions and 233 deletions.
18 changes: 9 additions & 9 deletions docs/src/fraction.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ resulting parent objects to coerce various elements into the fraction field.
**Examples**

```jldoctest
julia> R, x = polynomial_ring(ZZ, "x")
julia> R, x = polynomial_ring(ZZ, :x)
(Univariate polynomial ring in x over integers, x)
julia> S = fraction_field(R)
Expand Down Expand Up @@ -93,7 +93,7 @@ FactoredFractionField(R::Ring; cached::Bool = true)
**Examples**

```jldoctest
julia> R, (x, y) = polynomial_ring(ZZ, ["x", "y"])
julia> R, (x, y) = polynomial_ring(ZZ, [:x, :y])
(Multivariate polynomial ring in 2 variables over integers, AbstractAlgebra.Generic.MPoly{BigInt}[x, y])
julia> S = FactoredFractionField(R)
Expand Down Expand Up @@ -137,7 +137,7 @@ fraction field without constructing the fraction field parent first.
**Examples**

```jldoctest
julia> R, x = polynomial_ring(QQ, "x")
julia> R, x = polynomial_ring(QQ, :x)
(Univariate polynomial ring in x over rationals, x)
julia> S = fraction_field(R)
Expand Down Expand Up @@ -185,7 +185,7 @@ characteristic is not known an exception is raised.
**Examples**

```jldoctest
julia> R, x = polynomial_ring(QQ, "x")
julia> R, x = polynomial_ring(QQ, :x)
(Univariate polynomial ring in x over rationals, x)
julia> S = fraction_field(R)
Expand Down Expand Up @@ -242,7 +242,7 @@ denominator(a::FracElem)
**Examples**

```jldoctest
julia> R, x = polynomial_ring(QQ, "x")
julia> R, x = polynomial_ring(QQ, :x)
(Univariate polynomial ring in x over rationals, x)
julia> S = fraction_field(R)
Expand Down Expand Up @@ -286,7 +286,7 @@ gcd{T <: RingElem}(::FracElem{T}, ::FracElem{T})
**Examples**

```jldoctest
julia> R, x = polynomial_ring(QQ, "x")
julia> R, x = polynomial_ring(QQ, :x)
(Univariate polynomial ring in x over rationals, x)
julia> f = (x + 1)//(x^3 + 3x + 1)
Expand All @@ -313,7 +313,7 @@ Base.sqrt(::FracElem{T}) where {T <: RingElem}
**Examples**

```jldoctest
julia> R, x = polynomial_ring(QQ, "x")
julia> R, x = polynomial_ring(QQ, :x)
(Univariate polynomial ring in x over rationals, x)
julia> S = fraction_field(R)
Expand Down Expand Up @@ -346,7 +346,7 @@ valuation{T <: RingElem}(::FracElem{T}, ::T)
**Examples**

```jldoctest
julia> R, x = polynomial_ring(ZZ, "x")
julia> R, x = polynomial_ring(ZZ, :x)
(Univariate polynomial ring in x over integers, x)
julia> f = (x + 1)//(x^3 + 3x + 1)
Expand Down Expand Up @@ -381,7 +381,7 @@ Rationals
julia> f = rand(K, -10:10)
-1//3
julia> R, x = polynomial_ring(ZZ, "x")
julia> R, x = polynomial_ring(ZZ, :x)
(Univariate polynomial ring in x over integers, x)
julia> S = fraction_field(R)
Expand Down
12 changes: 6 additions & 6 deletions docs/src/free_associative_algebra.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ the parent object `S` from being cached.
**Examples**

```jldoctest
julia> R, (x, y) = free_associative_algebra(ZZ, ["x", "y"])
julia> R, (x, y) = free_associative_algebra(ZZ, [:x, :y])
(Free associative algebra on 2 indeterminates over integers, AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{BigInt}[x, y])
julia> (x + y + 1)^2
Expand All @@ -73,7 +73,7 @@ with coefficients and monomial words and not exponent vectors.
**Examples**

```jldoctest
julia> R, (x, y, z) = free_associative_algebra(ZZ, ["x", "y", "z"])
julia> R, (x, y, z) = free_associative_algebra(ZZ, [:x, :y, :z])
(Free associative algebra on 3 indeterminates over integers, AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{BigInt}[x, y, z])
julia> B = MPolyBuildCtx(R)
Expand Down Expand Up @@ -141,7 +141,7 @@ exponent_word(a::Generic.FreeAssociativeAlgebraElem{T}, i::Int) where T <: RingE
**Examples**

```jldoctest
julia> R, (x, y, z) = free_associative_algebra(ZZ, ["x", "y", "z"])
julia> R, (x, y, z) = free_associative_algebra(ZZ, [:x, :y, :z])
(Free associative algebra on 3 indeterminates over integers, AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{BigInt}[x, y, z])
julia> map(total_degree, (R(0), R(1), -x^2*y^2*z^2*x + z*y))
Expand Down Expand Up @@ -190,7 +190,7 @@ exponent_words(a::FreeAssociativeAlgebraElem{T}) where T <: RingElement
**Examples**

```jldoctest
julia> R, (a, b, c) = free_associative_algebra(ZZ, ["a", "b", "c"])
julia> R, (a, b, c) = free_associative_algebra(ZZ, [:a, :b, :c])
(Free associative algebra on 3 indeterminates over integers, AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{BigInt}[a, b, c])
julia> collect(terms(3*b*a*c - b + c + 2))
Expand Down Expand Up @@ -245,7 +245,7 @@ The implementation uses a non-commutative version of the Buchberger algorithm as
**Examples**

```jldoctest; setup = :(using AbstractAlgebra)

Check failure on line 247 in docs/src/free_associative_algebra.md

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in src/free_associative_algebra.md:247-258 ```jldoctest; setup = :(using AbstractAlgebra) julia> R = @free_associative_algebra(GF(2), [:x, :y, :u, :v, :t, :s]) (Free associative algebra on 6 indeterminates over finite field F_2, AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{AbstractAlgebra.GFElem{Int64}}[x, y, u, v, t, s]) julia> g = Generic.groebner_basis([u*(x*y)^3 + u*(x*y)^2 + u + v, (y*x)^3*t + (y*x)^2*t + t + s]) 5-element Vector{AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{AbstractAlgebra.GFElem{Int64}}}: u*x*y*x*y*x*y + u*x*y*x*y + u + v y*x*y*x*y*x*t + y*x*y*x*t + t + s u*x*s + v*x*t u*x*y*x*s + v*x*y*x*t u*x*y*x*y*x*s + v*x*y*x*y*x*t ``` Subexpression: R = @free_associative_algebra(GF(2), [:x, :y, :u, :v, :t, :s]) Evaluated output: Free associative algebra on 6 indeterminates x, y, u, v, ..., s over finite field F_2 Expected output: (Free associative algebra on 6 indeterminates over finite field F_2, AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{AbstractAlgebra.GFElem{Int64}}[x, y, u, v, t, s]) diff = Warning: Diff output requires color. (Free Free associative algebra on 6 indeterminates over finite field F_2, AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{AbstractAlgebra.GFElem{Int64}}[x, x, y, u, v, t, s])..., s over finite field F_2
julia> R, (x, y, u, v, t, s) = free_associative_algebra(GF(2), ["x", "y", "u", "v", "t", "s"])
julia> R = @free_associative_algebra(GF(2), [:x, :y, :u, :v, :t, :s])
(Free associative algebra on 6 indeterminates over finite field F_2, AbstractAlgebra.Generic.FreeAssociativeAlgebraElem{AbstractAlgebra.GFElem{Int64}}[x, y, u, v, t, s])
julia> g = Generic.groebner_basis([u*(x*y)^3 + u*(x*y)^2 + u + v, (y*x)^3*t + (y*x)^2*t + t + s])
Expand All @@ -260,7 +260,7 @@ julia> g = Generic.groebner_basis([u*(x*y)^3 + u*(x*y)^2 + u + v, (y*x)^3*t + (y
In order to check whether a given element of the algebra is in the ideal generated by a Groebner
basis `g`, one can compute its normal form.
```jldoctest; setup = :(using AbstractAlgebra)
julia> R, (x, y, u, v, t, s) = free_associative_algebra(GF(2), ["x", "y", "u", "v", "t", "s"]);
julia> R = @free_associative_algebra(GF(2), [:x, :y, :u, :v, :t, :s]);
julia> g = Generic.groebner_basis([u*(x*y)^3 + u*(x*y)^2 + u + v, (y*x)^3*t + (y*x)^2*t + t + s]);
Expand Down
28 changes: 14 additions & 14 deletions docs/src/function_field.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ resulting parent objects to coerce various elements into the function field.
**Examples**

```jldoctest
julia> S, x = rational_function_field(QQ, "x")
julia> S, x = rational_function_field(QQ, :x)
(Rational function field over rationals, x)
julia> f = S()
Expand All @@ -68,7 +68,7 @@ x + 1
julia> m = S(numerator(x + 1, false), numerator(x + 2, false))
(x + 1)//(x + 2)
julia> R, (x, y) = rational_function_field(QQ, ["x", "y"])
julia> R, (x, y) = rational_function_field(QQ, [:x, :y])
(Rational function field over rationals, AbstractAlgebra.Generic.RationalFunctionFieldElem{Rational{BigInt}, AbstractAlgebra.Generic.MPoly{Rational{BigInt}}}[x, y])
julia> (x + y)//y^2
Expand All @@ -85,7 +85,7 @@ We give some examples of such functionality.
**Examples**

```jldoctest
julia> S, x = rational_function_field(QQ, "x")
julia> S, x = rational_function_field(QQ, :x)
(Rational function field over rationals, x)
julia> f = S(x + 1)
Expand Down Expand Up @@ -147,7 +147,7 @@ gcd(::Generic.RationalFunctionFieldElem{T, U}, ::Generic.RationalFunctionFieldEl
**Examples**

```jldoctest
julia> R, x = rational_function_field(QQ, "x")
julia> R, x = rational_function_field(QQ, :x)
(Rational function field over rationals, x)
julia> f = (x + 1)//(x^3 + 3x + 1)
Expand All @@ -174,7 +174,7 @@ Base.sqrt(::Generic.RationalFunctionFieldElem{T, U}) where {T <: FieldElem, U <:
**Examples**

```jldoctest
julia> R, x = rational_function_field(QQ, "x")
julia> R, x = rational_function_field(QQ, :x)
(Rational function field over rationals, x)
julia> a = (21//4*x^6 - 15*x^5 + 27//14*x^4 + 9//20*x^3 + 3//7*x + 9//10)//(x + 3)
Expand Down Expand Up @@ -281,16 +281,16 @@ examples of such functionality.
**Examples**

```jldoctest
julia> R, x = rational_function_field(GF(23), "x") # characteristic p
julia> R, x = rational_function_field(GF(23), :x) # characteristic p
(Rational function field over finite field F_23, x)
julia> U, z = R["z"]
julia> U, z = R[:z]
(Univariate polynomial ring in z over rational function field, z)
julia> g = z^2 + 3z + 1
z^2 + 3*z + 1
julia> S, y = function_field(g, "y")
julia> S, y = function_field(g, :y)
(Function Field over finite field F_23 with defining polynomial y^2 + 3*y + 1, y)
julia> f = (x + 1)*y + 1
Expand Down Expand Up @@ -357,16 +357,16 @@ num_coeff(::Generic.FunctionFieldElem, ::Int)
**Examples**

```jldoctest
julia> R, x = rational_function_field(QQ, "x")
julia> R, x = rational_function_field(QQ, :x)
(Rational function field over rationals, x)
julia> U, z = R["z"]
julia> U, z = R[:z]
(Univariate polynomial ring in z over rational function field, z)
julia> g = z^2 + 3*(x + 1)//(x + 2)*z + 1
z^2 + (3*x + 3)//(x + 2)*z + 1
julia> S, y = function_field(g, "y")
julia> S, y = function_field(g, :y)
(Function Field over rationals with defining polynomial (x + 2)*y^2 + (3*x + 3)*y + x + 2, y)
julia> base_field(S)
Expand Down Expand Up @@ -420,16 +420,16 @@ norm(::Generic.FunctionFieldElem)
```

```jldoctest
julia> R, x = rational_function_field(QQ, "x")
julia> R, x = rational_function_field(QQ, :x)
(Rational function field over rationals, x)
julia> U, z = R["z"]
julia> U, z = R[:z]
(Univariate polynomial ring in z over rational function field, z)
julia> g = z^2 + 3*(x + 1)//(x + 2)*z + 1
z^2 + (3*x + 3)//(x + 2)*z + 1
julia> S, y = function_field(g, "y")
julia> S, y = function_field(g, :y)
(Function Field over rationals with defining polynomial (x + 2)*y^2 + (3*x + 3)*y + x + 2, y)
julia> f = (-3*x - 5//3)//(x - 2)*y + (x^3 + 1//9*x^2 + 5)//(x - 2)
Expand Down
8 changes: 4 additions & 4 deletions docs/src/ideal.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ contain duplicates, zero entries or be empty.
**Examples**

```jldoctest
julia> R, (x, y) = polynomial_ring(ZZ, ["x", "y"]; internal_ordering=:degrevlex)
julia> R, (x, y) = polynomial_ring(ZZ, [:x, :y]; internal_ordering=:degrevlex)
(Multivariate polynomial ring in 2 variables over integers, AbstractAlgebra.Generic.MPoly{BigInt}[x, y])
julia> V = [3*x^2*y - 3*y^2, 9*x^2*y + 7*x*y]
Expand Down Expand Up @@ -92,7 +92,7 @@ gens(::Generic.Ideal{T}) where T <: RingElement
**Examples**

```jldoctest
julia> R, x = polynomial_ring(ZZ, "x")
julia> R, x = polynomial_ring(ZZ, :x)
(Univariate polynomial ring in x over integers, x)
julia> V = [1 + 2x^2 + 3x^3, 5x^4 + 1, 2x - 1]
Expand Down Expand Up @@ -128,7 +128,7 @@ intersect(::Generic.Ideal{T}, ::Generic.Ideal{T}) where T <: RingElement
**Examples**

```jldoctest
julia> R, x = polynomial_ring(ZZ, "x")
julia> R, x = polynomial_ring(ZZ, :x)
(Univariate polynomial ring in x over integers, x)
julia> V = [1 + 2x^2 + 3x^3, 5x^4 + 1, 2x - 1]
Expand Down Expand Up @@ -170,7 +170,7 @@ normal_form(::U, ::Generic.Ideal{U}) where {T <: RingElement, U <: Union{PolyRin
**Examples**

```jldoctest
julia> R, (x, y) = polynomial_ring(ZZ, ["x", "y"]; internal_ordering=:degrevlex)
julia> R, (x, y) = polynomial_ring(ZZ, [:x, :y]; internal_ordering=:degrevlex)
(Multivariate polynomial ring in 2 variables over integers, AbstractAlgebra.Generic.MPoly{BigInt}[x, y])
julia> V = [3*x^2*y - 3*y^2, 9*x^2*y + 7*x*y]
Expand Down
12 changes: 6 additions & 6 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ This example makes use of multivariate polynomials.
```julia
using AbstractAlgebra

R, (x, y, z) = polynomial_ring(ZZ, ["x", "y", "z"])
R, (x, y, z) = polynomial_ring(ZZ, [:x, :y, :z])

f = x + y + z + 1

Expand All @@ -73,11 +73,11 @@ using AbstractAlgebra

R = GF(7)

S, y = polynomial_ring(R, "y")
S, y = polynomial_ring(R, :y)

T, = residue_ring(S, y^3 + 3y + 1)

U, z = polynomial_ring(T, "z")
U, z = polynomial_ring(T, :z)

f = (3y^2 + y + 2)*z^2 + (2*y^2 + 1)*z + 4y + 3;

Expand All @@ -95,7 +95,7 @@ Here is an example using matrices.
```julia
using AbstractAlgebra

R, x = polynomial_ring(ZZ, "x")
R, x = polynomial_ring(ZZ, :x)

S = matrix_space(R, 10, 10)

Expand All @@ -109,9 +109,9 @@ And here is an example with power series.
```julia
using AbstractAlgebra

R, x = QQ["x"]
R, x = QQ[:x]

S, t = power_series_ring(R, 30, "t")
S, t = power_series_ring(R, 30, :t)

u = t + O(t^100)

Expand Down
2 changes: 1 addition & 1 deletion docs/src/laurent_mpolynomial.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ $\prod_i x_i^{n_i}$ from the normalized representation. In particular,
this means that the output of `gcd` will not have any negative exponents.

```jldoctest
julia> R, (x, y) = laurent_polynomial_ring(ZZ, ["x", "y"]);
julia> R, (x, y) = laurent_polynomial_ring(ZZ, [:x, :y]);
julia> canonical_unit(2*x^-5 - 3*x + 4*y^-4 + 5*y^2)
-x^-5*y^-4
Expand Down
2 changes: 1 addition & 1 deletion docs/src/laurent_polynomial.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Laurent polynomials implement the ring interface, and some methods
from the polynomial interface, for example:

```jldoctest
julia> R, x = laurent_polynomial_ring(ZZ, "x")
julia> R, x = laurent_polynomial_ring(ZZ, :x)
(Univariate Laurent polynomial ring in x over integers, x)
julia> var(R)
Expand Down
Loading

0 comments on commit d3338f2

Please sign in to comment.