News for hackers. Stuff that matters.
Re: by machina_ex_deus on Saturday February 25, 2023 @06:15PM (#34941418)
I like sagemath. They have integration with sympy, and they also have many features making python simpler when you want to do mathematical things.

There's latex display feature when in notebook. It looks good, can even have symbolical matrices and display them as you would've expected.

It's on par with Wolfram notebook, but you get to program in python instead of some propietary language.

If you got some really long formula, it's easier to see you got it correctly by displaying it symbolically in latex.

They have other things to smooth out the parts of python which are annoying in math, like exponentiation works with ^, and dividing integers returns a rational number instead of floating point or an integer.

Re: by westurner on Saturday February 25, 2023 @06:41PM (#34941611)
> They have other things to smooth out the parts of python which are annoying in math, like exponentiation works with ^, and dividing integers returns a rational number instead of floating point or an integer.

SageMath also has a multivariate inequality solver IIRC. `*` is repeated multiplication (exponentiation) in Python. If you require preprocessing to translate ^ to *, you don't have valid Python code that'll run with any other interpreter.

Is it easier to import SageMath from a plain Python script now; with conda repackaging?

Type promotion in Python:

   from rational import Rational
   assert Rational(1, 4) / 2 == Rational(1, 8)
Python 2 had a __future__ import to do floatdiv instead of floordiv:

  from __future__ import division
  assert 3 / 2 == 1.5
  assert 3 // 2 == 1
  assert 4 / 2 == 2.0
Python 3 returns floats from ints or decimals IIRC:

   assert 3 / 2 == 1.5
   assert 3.0 / 2 == 1.5
   assert 3 // 2 == 1
   assert 4 / 2 == 2.0
Re: by williamstein on Saturday February 25, 2023 @06:53PM (#34941677)
> Is it easier to import SageMath from a plain Python script now; with conda repackaging?

Yes, it's easy. It was always possible, since fortunately none of the Sage library implementation uses the Sage preprocessor. You could always do "sage -python" and you get the Python interpreter that Sage uses, and can import Sage via "import sage.all". Somebody asked me exactly this question at a colloquium talk I gave on Sage yesterday, and here's my answer/demo:

https://youtu.be/dy792FEh1ns?t=1140

In the demo, I use a Jupyter kernel that runs the Python included in Sage-9.8.

Disclaimer: I created the Sage preparser long ago, in order to make Sage more palatable to mathematicians. In particular, it was inspired by audience feedback during a live demo I did at PyCon 2005.

Re: by williamstein on Saturday February 25, 2023 @07:05PM (#34941762)
> They have other things to smooth out the parts of python

I should add that this is an interesting chunk of Python code, and we've always planned to separate it out from Sage as a separate library than could be used with Sympy, etc.

https://github.com/sagemath/sage/blob/52a81cbd161ef4d5895325...

It does a surprising number of little clever things we realized would be a good idea over years, and it has some neat ideas from various Python PEP's like [1..10] that were rejected from Python, but are very useful when expressing mathematics. It's also annoying when you just want to format your code using prettier (say), and you can't because Sage isn't Python. :-(

Re: by westurner on Monday February 27, 2023 @02:41PM (#34960930)
Recently I've been working with the %%ipytest magic command for running pytest in notebooks.

Is there a %%sage IPython magic method that passes code through the preparse.py input transform?

Re: by vacuumcl on Saturday February 25, 2023 @07:56PM (#34942023)
As someone who does research with computer algebra systems (mainly Wolfram Mathematica) many hours a day, I think that SymPy is still far behind Mathematica. The fundamental design of Mathematica based on replacement rules and a style of functional programming is quite difficult to beat in terms of ease of use. A few weeks ago I gave SymPy another try, and tried to implement a nested sum of the type 1 <= a_1 < ... < a_k <= n over a complicated summand f(a_1, ... ,a_k), where k and n are integer arguments, and it took me ages to figure out how to implement this in SymPy, whereas I in Mathematica I can write this in a minute.
Re: by FPGAhacker on Saturday February 25, 2023 @08:16PM (#34942115)
Is that maybe because you are familiar with Mathematica?
Re: by auggierose on Saturday February 25, 2023 @08:26PM (#34942158)
I don't have experience with SageMath, but if they are really running on Python, then they are surely giving up on some of the nice rewriting that Mathematica is based on, as I would expect then a library call is needed instead in Python.
Re: by vacuumcl on Saturday February 25, 2023 @08:49PM (#34942274)
No. In Mathematica important operations like mapping functions over sets (arrays/tensors) at various depths have simple operators such as (/@, @, @@, @@@). You can do functional composition using @*, etc. You don't need to define your variables and functions before being able to write them in expressions. Often you want to have intermediate terms that have some temporary name, and which get substituted later on. The replacement rules in Mathematica are very powerful for this.

As another example, I googled some project Euler solutions in Mathematica and (pure Python) and found this: https://www.nayuki.io/page/project-euler-solutions. Compared to the pure Python solutions, the Mathematica code is typically much smaller. Of course this is not Sympy, but a lot of the Python syntax carries over to SymPy as well.

Re: by nayuki on Monday February 27, 2023 @02:51PM (#34961020)
My Mathematica solutions are a bit shorter than my Python solutions mainly because of convenient functions like PrimeQ, Binomial, ContinuedFraction.
Re: by loa_in_ on Saturday February 25, 2023 @08:59PM (#34942319)
SymPy is a great tool but there isn't a dedicated team of mathematicians and programmers working on it full time. I believe it has potential to compete with Mathematica someday.
Re: by kalimanzaro on Sunday February 26, 2023 @12:09AM (#34943234)
Wouldnt the following work in basic python:

  for a_k in 1 to n:
    for a_(k-1) in 1 to a_k:
    ....
I'm a heavy user of Mma but there are better examples. Automating the rewriting of formulas to make them more concise is a big one. (But also not as straightforward as it could be, tradeoffs between conciseness, ease of reading and speed of implementation need to be triaged, similar to Lisp/APL. Maybe sympy still has a chance to be more like math/TCS)
Re: by texaslonghorn5 on Sunday February 26, 2023 @05:08AM (#34944406)
no because k is an argument and you have to pre-write k nested loops
Re: by okaleniuk on Sunday February 26, 2023 @12:39AM (#34943357)
That's true, Mathematica is more advanced. But it's not cheap and it brings in a whole new world with it. You do have to learn to use it explicitly while with SymPy, you can start after a 5 minute introduction. I would say, Mathematica a good professional instrument while SymPy is a good hobbyist tool.

A note to a bypassing reader, if you want to try Mathematica, and not sure if you would stay with it, don't go for a desktop license. Get a Raspberry Pi instead. Mathematica comes free for Raspberry!

Re: by testaccc3 on Sunday February 26, 2023 @12:52AM (#34943425)
Do you have some reference on how to use the rewrite rules? I've meant to use them a few times but I've never seen a good tutorial or textbook.
Re: by kalimanzaro on Sunday February 26, 2023 @07:40AM (#34945074)
I found the books by David B. Wagner and Roman Maeder (who designed the language,btw) to be incredibly helpful.
Re: by Hendrikto on Sunday February 26, 2023 @07:45AM (#34945098)
> As someone who does research […] mainly Wolfram Mathematica […] it took me ages to figure out how to implement this in SymPy, whereas I in Mathematica I can write this in a minute.

Hardly surprising, is it? As someone with more experience in Python, it would likely be the opposite for me.

Re: by phkahler on Sunday February 26, 2023 @08:26AM (#34945346)
Do you have an opinion about Maxima or wxMaxima?
Re: by doetoe on Monday February 27, 2023 @02:09AM (#34953876)
You are replying to somebody talking about sage (rather than sympy), which in many fields (not all) is very far ahead of Mathematica. I do quite like the programming language of Mathematica, but an advantage of sage (and of sympy) is that you have the whole python standard library at your disposal. For your example you could e.g. use itertools.product.
Re: by steve1977 on Sunday February 26, 2023 @03:08AM (#34943925)
> It's on par with Wolfram notebook, but you get to program in python instead of some propietary language.

I‘m not sure if that’s a good thing, considering how nice and well-suited to maths the Wolfram language is.

I get that you need to learn yet another language, but you would also need to learn the particularities of Sagemath and/or the libraries it builds on.