sagemath/sage

Remove or tag uses of SR and symbolic functions in combinat, categories, etc.

Closed this issue · 53 comments

Follow-up on #32411.

Use of floor/ceil from sage.functions can be avoided.

Modules that only need symbolics in some methods should avoid module-level imports from sage.functions, sage.symbolic, sage.calculus.

Doctests that need these modules should be marked # optional - sage.symbolic.

This can be tested using #32601 (sagemath-standard-no-symbolics)

CC: @tscrim

Component: refactoring

Author: Matthias Koeppe

Branch/Commit: 4a1b489

Reviewer: Samuel Lelièvre

Issue created by migration from https://trac.sagemath.org/ticket/32609

Commit: 04855a1

New commits:

6bd6903PowerSeriesRing_generic._element_constructor_: Only import sage.symbolic.expression.SymbolicSeries after isinstance test with ABC Expression
9730b10src/sage/categories/metric_spaces.py: Mark doctests using HyperbolicPlane # optional - sage.symbolic
dca7113In doctests, replace floor(.../...) by //
44ac5absrc/sage/categories/vector_bundles.py: Mark doctests # optional - sage.symbolic
71303ddsrc/sage/matrix/operation_table.py: Use math.log, log10 insteadd of importing from sage.misc.functional
04855a1src/sage/groups/matrix_gps/coxeter_group.py: Do not use sage.symbolic for easy cases of cos(pi/x)

Description changed:

--- 
+++ 
@@ -2,31 +2,9 @@
 
 Use of `floor`/`ceil` from `sage.functions` can be avoided.
 
-Modules that only need symbolics in some methods should avoid module-level imports from `sage.functions`, `sage.symbolic`, `sage.calculus`
+Modules that only need symbolics in some methods should avoid module-level imports from `sage.functions`, `sage.symbolic`, `sage.calculus`.
 
-```
-src/sage/categories/loop_crystals.py:from sage.functions.other import ceil
-src/sage/combinat/binary_recurrence_sequences.py:from sage.functions.log import log
-src/sage/combinat/binary_recurrence_sequences.py:from sage.functions.other import sqrt
-src/sage/combinat/crystals/littelmann_path.py:from sage.functions.other import floor
-src/sage/combinat/crystals/tensor_product.py:from sage.functions.other import floor, ceil
-src/sage/combinat/crystals/tensor_product_element.pyx:from sage.functions.other import ceil
-src/sage/combinat/diagram_algebras.py:from sage.functions.other import floor, ceil
-src/sage/combinat/finite_state_machine.py:from sage.calculus.var import var
-src/sage/combinat/finite_state_machine.py:from sage.functions.trig import atan2
-src/sage/combinat/k_tableau.py:from sage.functions.generalized import sgn
-src/sage/combinat/parallelogram_polyomino.py:from sage.functions.trig import cos, sin
-src/sage/combinat/parallelogram_polyomino.py:from sage.functions.other import sqrt
-src/sage/combinat/partition.py:from sage.symbolic.ring import var
-src/sage/combinat/partition_algebra.py:from sage.functions.all import ceil
-src/sage/combinat/root_system/coxeter_type.py:from sage.symbolic.ring import SR
-src/sage/combinat/sine_gordon.py:from sage.functions.trig import cos, sin
-src/sage/combinat/sine_gordon.py:from sage.symbolic.constants import pi, I
-src/sage/combinat/sine_gordon.py:from sage.functions.log import exp
-src/sage/combinat/sine_gordon.py:from sage.functions.other import ceil
-src/sage/combinat/sine_gordon.py:from sage.symbolic.ring import SR
-src/sage/combinat/sine_gordon.py:from sage.functions.other import real_part, imag_part
-src/sage/combinat/sloane_functions.py:from sage.functions.all import prime_pi
-src/sage/combinat/symmetric_group_representations.py:from sage.symbolic.ring import SR
-src/sage/combinat/symmetric_group_representations.py:from sage.functions.all import sqrt
-```
+Doctests that need these modules should be marked `# optional - sage.symbolic`.
+
+This can be tested using #32601 (**sagemath-standard-no-symbolics**)
+

Author: Matthias Koeppe

Changed commit from 04855a1 to ecad015

Branch pushed to git repo; I updated commit sha1. New commits:

ecad015src/sage/groups/matrix_gps/coxeter_group.py: Do not use sage.symbolic for easy cases of cos(pi/x) (fixup)

Changed commit from ecad015 to 2511e3e

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

5d2cc6bPowerSeriesRing_generic._element_constructor_: Only import sage.symbolic.expression.SymbolicSeries after isinstance test with ABC Expression
dc8cc8csrc/sage/categories/metric_spaces.py: Mark doctests using HyperbolicPlane # optional - sage.symbolic
9ca27e7In doctests, replace floor(.../...) by //
444b659src/sage/categories/vector_bundles.py: Mark doctests # optional - sage.symbolic
63b0ad8src/sage/matrix/operation_table.py: Use math.log, log10 insteadd of importing from sage.misc.functional
13c5092src/sage/groups/matrix_gps/coxeter_group.py: Do not use sage.symbolic for easy cases of cos(pi/x)
2511e3esrc/sage/groups/matrix_gps/coxeter_group.py: Do not use sage.symbolic for easy cases of cos(pi/x) (fixup)

Changed commit from 2511e3e to ea8d379

Branch pushed to git repo; I updated commit sha1. New commits:

e3b36fasrc/sage/categories/algebra_functor.py: Make doctests using sqrt # optional - sage.symbolic
ea8d379src/sage/categories/rings.py: Mark doctests using sqrt # optional - sage.symbolic

Changed commit from ea8d379 to 9cae6cb

Branch pushed to git repo; I updated commit sha1. New commits:

c7bd9adsrc/sage/arith/multi_modular.pyx: Use primecountpy.primecount directly instead of going through sage.functions
9cae6cbsrc/sage/groups/semimonomial_transformations/semimonomial_transformation_group.py: Import factorial from sage.arith.misc

Branch pushed to git repo; I updated commit sha1. New commits:

994f47bsrc/sage/categories/number_fields.py: Remove abuse of predefined x in doctests
4a1fc16src/sage/structure/element.pyx: Remove abuse of predefined x in doctests
c6714aasrc/sage/categories/coxeter_groups.py: Mark a doctest # optional - sage.symbolic
30fa675src/sage/categories/pushout.py: Remove abuse of predefined x in doctests
3cd7195src/sage/categories/modules_with_basis.py: In doctest, import factorial from sage.arith.misc

Changed commit from 9cae6cb to 3cd7195

Branch pushed to git repo; I updated commit sha1. New commits:

2445b36src/sage/categories/chain_complexes.py: Mark doctests using manifolds # optional - sage.symbolic

Changed commit from 3cd7195 to 2445b36

Branch pushed to git repo; I updated commit sha1. New commits:

fe0c388src/sage/categories/lie_algebras.py: Mark doctests using manifolds # optional - sage.symbolic

Changed commit from 2445b36 to fe0c388

Changed commit from fe0c388 to e197558

Branch pushed to git repo; I updated commit sha1. New commits:

e197558src/sage/categories/finite_dimensional_nilpotent_lie_algebras_with_basis.py: Mark doctests using manifolds # optional - sage.symbolic

Branch pushed to git repo; I updated commit sha1. New commits:

3ede66asrc/sage/categories/finite_permutation_groups.py: Mark a doctest # optional - sage.symbolic
caf233esrc/sage/categories/metric_spaces.py: Mark a doctest # optional - sage.symbolic

Changed commit from e197558 to caf233e

Changed commit from caf233e to 486fafb

Branch pushed to git repo; I updated commit sha1. New commits:

486fafbsrc/sage/categories/pushout.py: Mark doctests # optional - sage.symbolic

Branch pushed to git repo; I updated commit sha1. New commits:

adab09fsrc/sage/categories/map.pyx: Mark doctests # optional - sage.symbolic

Changed commit from 486fafb to adab09f

Branch pushed to git repo; I updated commit sha1. New commits:

016cd5bsrc/sage/categories/finite_posets.py: In doctest, remove use of floor
dd9dccdsrc/sage/categories/primer.py: Mark doctests # optional - sage.symbolic
b0fe2b7src/sage/categories/homset.py: Remove abuse of predefined x in doctests
dbecc5fsrc/sage/categories/homset.py: Mark doctests # optional - sage.symbolic
e177b45src/sage/categories/number_fields.py: Mark doctests # optional - sage.symbolic

Changed commit from adab09f to e177b45

slel commented
comment:17

Some more parts can be made symbolics-free,
though sometimes it makes things convoluted.

File src/sage/categories/algebra_functor.py

We can bypass using sqrt(5) to construct ZZ[sqrt(5)]:

    sage: G = GL(2, 7)
    sage: K = QuadraticField(5, 'srqt5')
    sage: Zr5 = K.order(K.gen())  # This is ZZ[sqrt(5)]
    sage: OG = GroupAlgebra(G, Zr5)
    sage: OG(2)
    2*[1 0]
    [0 1]
    sage: OG(G(2))
    [2 0]
    [0 2]
 
    sage: OG(FormalSum([ (1, G(2)), (2, RR(0.77)) ]) )
    Traceback (most recent call last):
    ...
    TypeError: Attempt to coerce non-integral RealNumber to Integer
    sage: OG(OG.base_ring().basis()[1])
    sqrt5*[1 0]
    [0 1]

Keeping the clearer ZZ[sqrt(5)] and adding
# optional - sage.symbolic makes sense too.

I wish sqrt(5) gave an element in a number field
or AA or QQbar, not in the symbolic ring...

File src/sage/categories/coxeter_groups.py

Using a polynomial variable can bypass symbolics here:

                 sage: W = CoxeterGroups().example()
-                sage: sum((x^w.length()) for w in W) - expand(prod(sum(x^i for i in range(j+1)) for j in range(4))) # This is scandalously slow!!!  # optional - sage.symbolic
+                sage: x = polygen(ZZ)
+                sage: s = sum(x^w.length() for w in W)
+                sage: p = prod(sum(x^i for i in range(j)) for j in range(1, 5))
+                sage: s - p
                 0

The 2009 "scandalously slow" comment can probably go?

File src/sage/categories/pushout.py

Using sqrt(3.) and (2.)^(1/3) makes this symbolics-free:

-            sage: K.<a> = NumberField(x^3-2, embedding=CDF(1/2*I*2^(1/3)*sqrt(3) - 1/2*2^(1/3)))
+            sage: cbrt2 = CDF(2)^(1/3)
+            sage: zeta3 = CDF(-1/2, sqrt(3.)/2)
+            sage: K.<a> = NumberField(x^3 - 2, embedding=cbrt2 * zeta3)
comment:18

Replying to @slel:

Some more parts can be made symbolics-free,
though sometimes it makes things convoluted.

File src/sage/categories/algebra_functor.py

We can bypass using sqrt(5) to construct ZZ[sqrt(5)]:

    sage: G = GL(2, 7)
    sage: K = QuadraticField(5, 'srqt5')
    sage: Zr5 = K.order(K.gen())  # This is ZZ[sqrt(5)]

Or just ZZ[AA(5).sqrt()]

Changed commit from e177b45 to bbe19ad

Branch pushed to git repo; I updated commit sha1. New commits:

bbe19adsrc/sage/categories/algebra_functor.py: In doctest, use ZZ[AA(5).sqrt()] instead of ZZ[sqrt(5)] to avoid symbolics

Changed commit from bbe19ad to 5d82771

Branch pushed to git repo; I updated commit sha1. New commits:

5d82771src/sage/categories/coxeter_groups.py: In doctest, avoid using symbolics
comment:21

Replying to @slel:

File src/sage/categories/coxeter_groups.py

Using a polynomial variable can bypass symbolics here [...]

Thanks, I've made a similar change

Branch pushed to git repo; I updated commit sha1. New commits:

3851908src/sage/categories/pushout.py: In doctest, avoid using symbolics

Changed commit from 5d82771 to 3851908

slel commented
comment:23

I like ZZ[AA(5).sqrt()] but it does not name
the generator sqrt5 as ZZ[sqrt(5)]:

sage: ZZ[sqrt(5)]
Order in Number Field in sqrt5 with defining polynomial x^2 - 5 with sqrt5 = 2.236067977499790?
sage: ZZ[AA(5).sqrt()]
Order in Number Field in a with defining polynomial x^2 - 5 with a = 2.236067977499790?

so you need to change:

     sage: OG(OG.base_ring().basis()[1])
-    sqrt5*[1 0]
+    a*[1 0]
     [0 1]

Branch pushed to git repo; I updated commit sha1. New commits:

595f99asrc/sage/categories/algebra_functor.py: Update doctest output

Changed commit from 3851908 to 595f99a

Changed commit from 595f99a to ed59c28

Branch pushed to git repo; I updated commit sha1. New commits:

ed59c28src/sage/categories/pushout.py: Fix typo in doctest

Branch pushed to git repo; I updated commit sha1. New commits:

518bb96src/sage/categories/modules_with_basis.py: Update doctest output

Changed commit from ed59c28 to 518bb96

comment:27

The remaining doctest failure (and the pyflakes warnings) seen in the patchbot run are not from this ticket.

Reviewer: Samuel Lelièvre

Changed commit from 518bb96 to 4a1b489

Branch pushed to git repo; I updated commit sha1. New commits:

4a1b489Merge tag '9.6.beta4' into t/32609/remove_more_unnecessary_uses_of_sr_and_symbolic_functions_in_sage_combinat
slel commented
comment:30

If bots go green or only report errors unrelated to this, positive review.

comment:31

Patchbot failures and pyflakes warnings are unrelated.

comment:32

Thank you!