Sym: SymPy, SymEngine, PySym, SymCXX, Diofant

(re: \pi, symbolic computation and trigonometry
instead of surprisingly useful piecewise optimizations)

On Fri, Jun 8, 2018 at 10:09 PM Wes Turner <wes.turner@gmail.com> wrote:
# Python, NumPy, SymPy, mpmath, sage trigonometric functions
https://en.wikipedia.org/wiki/Trigonometric_functions

## Python math module
https://docs.python.org/3/library/math.html#trigonometric-functions
- degrees(radians): Float degrees
- radians(degrees): Float degrees

## NumPy
https://docs.scipy.org/doc/numpy/reference/routines.math.html#trigonometric-functions
- degrees(radians) : List[float] degrees
- rad2deg(radians): List[float] degrees
- radians(degrees) : List[float] radians
- deg2rad(degrees): List[float] radians

https://docs.scipy.org/doc/numpy/reference/generated/numpy.sin.html


# Symbolic computation
 

## SymPy
http://docs.sympy.org/latest/modules/functions/elementary.html#sympy-functions-elementary-trigonometric
http://docs.sympy.org/latest/modules/functions/elementary.html#trionometric-functions

- sympy.mpmath.degrees(radians): Float degrees
- sympy.mpmath.radians(degrees): Float radians

https://stackoverflow.com/questions/31072815/cosd-and-sind-with-sympy
  - cosd, sind
  - https://stackoverflow.com/questions/31072815/cosd-and-sind-with-sympy#comment50176770_31072815
 
    > Let x, theta, phi, etc. be Symbols representing quantities in radians. Keep a list of these symbols: angles = [x, theta, phi]. Then, at the very end, use y.subs([(angle, angle*pi/180) for angle in angles]) to change the meaning of the symbols to degrees"

http://docs.sympy.org/latest/tutorial/simplification.html#trigonometric-simplification

https://github.com/sympy/sympy/blob/master/sympy/functions/elementary/trigonometric.py
https://github.com/sympy/sympy/blob/master/sympy/functions/elementary/tests/test_trigonometric.py#
https://github.com/sympy/sympy/blob/master/sympy/simplify/trigsimp.py
https://github.com/sympy/sympy/blob/master/sympy/simplify/tests/test_trigsimp.py
https://github.com/sympy/sympy/blob/master/sympy/integrals/trigonometry.py
https://github.com/sympy/sympy/blob/master/sympy/integrals/tests/test_trigonometry.py

https://github.com/sympy/sympy/blob/master/sympy/utilities/tests/test_wester.py
https://github.com/sympy/sympy/blob/master/sympy/utilities/tests/test_wester.py#L593 (I. Trigonometry)


## Sym
Src: https://github.com/bjodah/sym
PyPI: https://pypi.org/project/sym/
> sym provides a unified wrapper to some symbolic manipulation libraries in Python. It

## SymEngine
- Src: https://github.com/symengine/symengine
- Src: https://github.com/symengine/symengine.py
- Docs: https://github.com/symengine/symengine/blob/master/doc/design.md
- SymEngine / SymPy compatibility tests:
  https://github.com/symengine/symengine.py/blob/master/symengine/tests/test_sympy_compat.py

## Diofant
Src: https://github.com/diofant/diofant
https://diofant.readthedocs.io/en/latest/tutorial/intro.html
https://diofant.readthedocs.io/en/latest/tutorial/basics.html#substitution
https://diofant.readthedocs.io/en/latest/tutorial/simplification.html#trigonometric-functions

from diofant import symbols, pi
x,y,z,_pi = symbols('x y z _pi')
expr =  pi**x # TODO: see diofant/tests/test_wester.py#L511
expr.subs(x, 1e11)
print(operator.sub(
      expr.subs(pi, 3.14),
      expr.subs(pi, 3.14159265)))
assert expr.subs(pi, 3.14) != expr.subs(pi, 3.14159265)
print(expr.subs(pi, 3.14159).evalf(70))

- CAS capability tests:
  https://github.com/diofant/diofant/blob/master/diofant/tests/test_wester.py

> """ Tests from Michael Wester's 1999 paper "Review of CAS mathematical
> capabilities".
> http://www.math.unm.edu/~wester/cas/book/Wester.pdf
> See also http://math.unm.edu/~wester/cas_review.html for detailed output of
> each tested system.
"""

https://github.com/diofant/diofant/blob/79ae584e949a08/diofant/tests/test_wester.py#L511

# I. Trigonometry
@pytest.mark.xfail
def test_I1():
    assert tan(7*pi/10) == -sqrt(1 + 2/sqrt(5))
@pytest.mark.xfail
def test_I2():
    assert sqrt((1 + cos(6))/2) == -cos(3)
def test_I3():
    assert cos(n*pi) + sin((4*n - 1)*pi/2) == (-1)**n - 1
def test_I4():
    assert cos(pi*cos(n*pi)) + sin(pi/2*cos(n*pi)) == (-1)**n - 1
@pytest.mark.xfail
def test_I5():
    assert sin((n**5/5 + n**4/2 + n**3/3 - n/30) * pi) == 0
 
diofant.sin.eval() has a number of interesting conditionals in there:
https://github.com/diofant/diofant/blob/master/diofant/functions/elementary/trigonometric.py#L200

The tests for diofant.functions.elementary.trigonometric likely have a number of helpful tests for implementing methods dealing with pi and trigonometric identities:
https://github.com/diofant/diofant/blob/master/diofant/functions/elementary/tests/test_trigonometric.py

https://github.com/diofant/diofant/blob/master/diofant/simplify/trigsimp.py
https://github.com/diofant/diofant/blob/master/diofant/simplify/tests/test_trigsimp.py
https://github.com/diofant/diofant/blob/master/diofant/integrals/tests/test_trigonometry.py
https://github.com/diofant/diofant/blob/master/diofant/functions/elementary/tests/test_trigonometric.py


## mpmath
- sympy.mpmath.degrees(radians): Float degrees
- sympy.mpmath.radians(degrees): Float radians


## Sage



On Friday, June 8, 2018, Robert Vanden Eynde <robertvandeneynde@hotmail.com> wrote:
- Thanks for pointing out a language (Julia) that already had a name convention. Interestingly they don't have a atan2d function. Choosing the same convention as another language is a big plus.

- Adding trig function using floats between 0 and 1 is nice, currently one needs to do sin(tau * t) which is not so bad (from math import tau, tau sounds like turn).

- Julia has sinpi for sin(pi*x), one could have sintau(x) for sin(tau*x) or sinturn(x).

Grads are in the idea of turns but with more problems, as you guys said, grads are used by noone, but turns are more useful. sin(tau * t) For The Win.

- Even though people mentionned 1/6 not being exact, so that advantage over radians isn't that obvious ?

from math import sin, tau
from fractions import Fraction
sin(Fraction(1,6) * tau)
sindeg(Fraction(1,6) * 360)

These already work today by the way.

- As you guys pointed out, using radians implies knowing a little bit about floating point arithmetic and its limitations. Integer are more simple and less error prone. Of course it's useful to know about floats but in many case it's not necessary to learn about it right away, young students just want their player in the game move in a straight line when angle = 90.

- sin(pi/2) == 1 but cos(pi/2) != 0 and sin(3*pi/2) != 1 so sin(pi/2) is kind of an exception.




Le ven. 8 juin 2018 à 09:11, Steven D'Aprano <steve@pearwood.info> a écrit :
On Fri, Jun 08, 2018 at 03:55:34PM +1000, Chris Angelico wrote:
> On Fri, Jun 8, 2018 at 3:45 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> > Although personally I prefer the look of d as a prefix:
> >
> > dsin, dcos, dtan
> >
> > That's more obviously pronounced "d(egrees) sin" etc rather than "sined"
> > "tanned" etc.
>
> Having it as a suffix does have one advantage. The math module would
> need a hyperbolic sine function which accepts an argument in; and
> then, like Charles Napier [1], Python would finally be able to say "I
> have sindh".

Ha ha, nice pun, but no, the hyperbolic trig functions never take
arguments in degrees. Or radians for that matter. They are "hyperbolic
angles", which some electrical engineering text books refer to as
"hyperbolic radians", but all the maths text books I've seen don't call
them anything other than a real number. (Or sometimes a complex number.)

But for what it's worth, there is a correspondence of a sort between the
hyperbolic angle and circular angles. The circular angle going between 0
to 45° corresponds to the hyperbolic angle going from 0 to infinity.

https://en.wikipedia.org/wiki/Hyperbolic_angle

https://en.wikipedia.org/wiki/Hyperbolic_function


> [1] Apocryphally, alas.

Don't ruin a good story with facts ;-)



--
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/