views:

2374

answers:

10

Short Question:

Lets say, I want to solve Project Euler problem 9 using python and some equation solving libraries.

 a + b + c = 1000.
 a2 + b2 = c2

How do you do it.

Long Question:

I want to solve a set of equations; linear, sometimes may be quadratic too. Not a specific problem; but often, I have been in situation I wanted to.

It is simple to use the web equivalent of mathematica, wolframalpha.com to solve them. But that doesn't provide the comfort and convenience of an iPython shell.

Is there a simple libray to work on linear and quadratic equations on a python shell.

Personally, I use and find extremely convenient to use the Casio 991 MS scientific calculator. I know to set variables, solve equations, and do a lot. I want such a tool preferably within a ipython shell. I am surprised not to have found any. I'm not impressed enough by sage; perhaps I am missing something.

+2  A: 

I'd use Octave for this but I agree, the syntax of Octave isn't what I'd call thrilling (and the docs always confuse me more than they help, too).

Aaron Digulla
+2  A: 

For inexact solutions, read up on linear programming and sequential quadratic optimization, then search for Python libraries that performs such optimizations for you.

If the equations require integer solutions, you should search for Diophantine equation solvers for Python.

Just note that using a simple solver for Project Euler is missing the point. The fun, and educational part, is learning how to solve it yourself using primitive methods!

csl
+7  A: 

You discount the best answer as unacceptable.

Your question is "I want a free Computer Algebra System that I can use in Python."

The answer is "SAGE does that."

Have you looked at maxima/macsyma? SAGE provides bindings for it, and that's one of the more powerful free ones.

http://maxima.sourceforge.net/

Paul McMillan
A: 

I don't think there is a unified way of dealing with both linear and quadratic (or generally nonlinear) equations simultaneously. With linear systems, python has bindings to linear algebra and matrix packages. Nonlinear problems tend to be solved on a case by case basis.

Victor Liu
SAGE's maxima bindings can deal with pretty much anything you throw at them.
Paul McMillan
+3  A: 

Have you looked at SciPy?

It has an example in the tutorials on solving linear algebra:

http://docs.scipy.org/doc/scipy/reference/tutorial/linalg.html#solving-linear-system

Andre Miller
+11  A: 

sympy is exactly what you're looking for.

Autoplectic
+2  A: 

I have just started using GNU Scientific Library, which however is C library. Looks like there are Python bindings too. So, it might be worth looking at.

Amit
+1, GSL is a great library.
Paul McMillan
+2  A: 

On second thoughts, I went through sage in detail and clearly it is the best math free software available.

Just some of the different python math related libraries, it integrates is absolutely awesome.

Mathematics packages contained in Sage:

Algebra GAP, Maxima, Singular 
Algebraic Geometry  Singular
Arbitrary    Precision
Arithmetic  GMP, MPFR, MPFI,    NTL
Arithmetic Geometry PARI, NTL,      
mwrank, ecm Calculus    Maxima, SymPy, 
GiNaC Combinatorics Symmetrica,     
Sage-Combinat Linear Algebra    Linbox,
IML Graph Theory    NetworkX Group     
Theory  GAP Numerical
computation GSL,    SciPy, NumPy,
ATLAS

Other packages contained in Sage:

Command line    IPython Database ZODB,
Python Pickles, SQLite Graphical
Interface   Sage Notebook, jsmath
Graphics    Matplotlib, Tachyon3d, GD,
Jmol Interactive 
programming language  Python 
Networking  Twisted
Lakshman Prasad
A: 

It depends on your needs:

If you want an interactive graphical interface, then sage is probably the best solution.

If you want to avoid using a graphical interface, but you still want to do computer algebra, then sympy or maxima may cover your needs. (sympy looks very promising, but it still have a long way to go before they can replace mathematica).

If you don't really need symbolic algrebra, but you need a way to program with matrices, solve differential equations, and minimize functions, then scipy or octave are excelent starting points.

niels
A: 

Here is how to solve your original question using Python (via Sage). This basically clarifies the remark Paul McMillan makes above.

sage: a,b,c = var('a,b,c')
sage: solve([a+b+c==1000, a^2+b^2==c^2], a,b,c)
[[a == 1000*(r1 + sqrt(r1^2 + 2000*r1 - 1000000))/(r1 + sqrt(r1^2 + 2000*r1 - 1000000) + 1000), b == -1/2*r1 - 1/2*sqrt(r1^2 + 2000*r1 - 1000000) + 500, c == r1], [a == 1000*(r2 - sqrt(r2^2 + 2000*r2 - 1000000))/(r2 - sqrt(r2^2 + 2000*r2 - 1000000) + 1000), b == -1/2*r2 + 1/2*sqrt(r2^2 + 2000*r2 - 1000000) + 500, c == r2]]