sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.4k stars 474 forks source link

factors and sqrt don't simplify as expected #38130

Open koushik-ms opened 4 months ago

koushik-ms commented 4 months ago

Steps To Reproduce

  1. Startup sage (I used a local installation as well as a cocalc worksheet)
  2. Run sqrt(15)*sqrt(15). Output is 15. This is as expected.
  3. Run the following snippet:
    q = sqrt(15)*sqrt(5)*sqrt(3)
    q.simplify_full()   # or simplify_real() or simplify_rational()

Expected Behavior

Output is 15

Actual Behavior

Output is sqrt(15)*sqrt(5)*sqrt(3)

Additional Information

Below is a larger transcript with some comments of this problem manifesting in other ways:

sage: p = 8*sqrt(15)/(2*sqrt(3)) # problem: simplify p to 4*sqrt(5)
sage: p
4/3*sqrt(15)*sqrt(3)            # some "auto-simplification"
sage: p.full_simplify()         # every other simplification function...
4/3*sqrt(15)*sqrt(3)
sage: p.simplify_rational()     # ... has ...
4/3*sqrt(15)*sqrt(3)
sage: p.simplify_real()         # ... no ...
4/3*sqrt(15)*sqrt(3)
sage: p.simplify_full()         # ... effect.
4/3*sqrt(15)*sqrt(3)
sage: p
4/3*sqrt(15)*sqrt(3)
sage: p = 8*sqrt(15)/(2*sqrt(3))
sage: p
4/3*sqrt(15)*sqrt(3)
sage: p*3
4*sqrt(15)*sqrt(3)
sage: p*sqrt(3)
4*sqrt(15)
sage: p/sqrt(5)
4/15*sqrt(15)*sqrt(5)*sqrt(3)
sage: q = p/sqrt(5)   # here I expect q == 4
sage: simplify(q)
4/15*sqrt(15)*sqrt(5)*sqrt(3) #... which is very roundabout way of writing 4
sage: q
4/15*sqrt(15)*sqrt(5)*sqrt(3)
sage: q.full_simplify()
4/15*sqrt(15)*sqrt(5)*sqrt(3)
sage: sqrt(15)*sqrt(15)           # v15 * v15 == 15 but ...
15
sage: sqrt(15)*sqrt(5)*sqrt(3)    # v15 * v5 * v3 is NOT!
sqrt(15)*sqrt(5)*sqrt(3)
sage: sqrt(5)*sqrt(3)*sqrt(5)*sqrt(3) # more asymmetry
15

Environment

- **OS**: Linux/ MacOS
- **Sage Version**: SageMath version 10.3

Checklist

DaveWitteMorris commented 4 months ago

The name full_simplify is misleading, and should perhaps be changed, because the method does not perform all possible simplifications.

I think the one you want is canonicalize_radical.

sage: q = sqrt(15)*sqrt(5)*sqrt(3)
sage: q.canonicalize_radical()
15
koushik-ms commented 4 months ago

That's absolutely right. Thanks for the hint!