2015-02-24 09:41:38

Hello,
So I had some free time on my hands and I wanted to implement something that could solve algebraic questions like 3*x+10=22 and the like. I looked at simpy, but it seems to be more twards implementing symbolic equasion rather than solving them. I could make a symbolic to nuramical function, where in the equasion x would be ittirated over and over until the equasion is complete, but I think that'll just eat unnessessary power and wouldn't really work. Any thoughts?

This is not a signature.

2015-02-24 17:15:17 (edited by camlorn 2015-02-24 17:15:50)

That this is really, really, really hard.  Way harder than you're thinking.
Sympy can certainly solve them for you, anyway.
Alternatively for polynomial solving, numpy.polynomial.polynomial.polyroots.
And Sympy can do at least differentiation, if Calculus is your cup of tea.  I'm also seeing some support for symbolic integration, and it's probably got a numeric approximator for definite integrals, somewhere, too.
Implementing this yourself will take weeks, and you'll need to read a bunch of academic papers first in all likelihood.  These algorithms are not simple by any stretch of the imagination.

My Blog
Twitter: @ajhicks1992

2015-02-24 17:29:54

Simpy only appears to have support for equasions where the answer is 0, for something like 4*m-20 where m is 5, but it doesn't really seem to have support for nonzero equasions.

This is not a signature.

2015-02-24 19:05:36 (edited by camlorn 2015-02-24 19:06:32)

That doesn't matter.  Every equation can be made equal to zero.  if a=b, then a-b=0 for all a and b, whether they be numbers or whatever.  As I read the docs, it's not assuming that the equation is equal to zero, it's setting the equation equal to zero and then solving that.
Edit: yeah, you need to do the transformation yourself.  But even so.

My Blog
Twitter: @ajhicks1992

2015-02-25 03:55:17

Hm, give me some prospective here. How would I transform 3*x*10=22 into a 0 equasion? we haven't done transformations yet

This is not a signature.

2015-02-25 04:37:27 (edited by camlorn 2015-02-25 04:43:53)

Subtract 22 from both sides?  it's not an actual graphical transformation, just a little algorithm.  Transformation is mathematically the wrong word.  Algebraic manipulation is cumbersome.  Mapping function for equations is completely unhelpful and possibly not even correct vocabulary, but it does at least tickle my internal mathematical correctness conscious.
More generally and more explicitly, assume a and b are functions or terms or numbers or whatever.  Then:

a=b
a-b=b-b
a-b=0

And the last equation still has solutions the same as the first one because all we did is basic algebra.
So, in words, take the right side, subtract it from the left side, and set the resulting thing to 0.
The techniques for actually solving algebraic equations requires a lot of math and theory that you don't have.  I don't have it, either.  For the stuff where it's actually worth using a computer program, you also often end up in the realm of numeric approximation.  For example, Numpy.  We can't actually solve really high degree polynomials with an algorithm, but we can keep approximating the zeros to a really, really precise degree and use them to keep making the polynomial smaller.  When you get to calculus, the published algorithm (not the code, just the description of how to do it) is something or other hundred pages.  Without explanation.  The truth is that if you find using Sympy complex, just imagine rewriting it from scratch, because that's what you're saying you want to do.
Giving you a complete program is not trivial because someone needs to figure out how to parse the input.  I think Sympy also has something for this, but I'm going to leave that as an exercise to the reader.  If nothing else, you can still do it in the Python REPL.  For examples, read the linked page of the Sympy docs, as they show you exactly how.  This is way easier than you are making it.
Edit: I like it when people tell me about my most common, consistent  and obvious spelling mistakes.  I did this one for years too.  Equation is with a t, not an s.  For the record, that's not my worst: my personal worst is ought versus aught, because both are actually words and not flagged by spell check, but aught (with an a) is old English.  And one day I'll stop having to look up which is which.
edit 2: And for those wondering why I care and how I ever found out, I'm on Espeak and Espeak says it really, really wrong with an s.  Really.

My Blog
Twitter: @ajhicks1992

2015-02-25 05:02:28

So basically, it would go something like solve(3*x*10-3*x*10)? that's what I'm gathering, though I mite be wrong.

This is not a signature.

2015-02-25 05:28:32

yeah, pretty much.  But the example you gave has infinitely many solutions so won't be really useful and you need some extra arguments.  Start with the Sympy getting started, then read the docs I linked.  You'll almost always want to wrap the second part, that is the bit representing the right hand side, in parentheses.
It's been a while since I've done the Sympy getting started, but basically you have to tell Sympy what your variables are called and stuff.  Their docs are pretty good and  I can't really make the basics sections any simpler.
At the level of algebra I suspect you're doing, though, it may just be easier to do the algebra yourself.  And possibly better for you in the long run.  I guess this could be useful as a checking program, but the overhead of using anything you're likely to end up with in a reasonable amount of time is going to be pretty high.  You also run up against this: if you use it wrong it will give the wrong answer, and you're not going to ever know without checking it manually anyway.  The packages you're playing with are kind of like trying to build a tree house with a full set of construction equipment including a bulldozer, when all you really needed was a ladder, a saw, and a drill.  This extends to the fact that you have to know more stuff to use your overpowered solution, and you probably had to spend a lot of extra time to learn it.
Also, it appears that you can actually pass equations to solve without the rearrangement after all, but you have to use something like eq(foo, bar) instead of the equals sign.  The fact that this was not obvious is not surprising given how they word it, but it's there if you scroll down the page some and know what you're looking at.
But I think in terms of effort, this may not be worth it for you.  Your problems are going to take a minute or two each at most. Sympy is intended for people with problems that will take 10 or 20 minutes each at minimum.  You don't start seeing these without the stipulation that you also use a specific method until well into college, and when you hit calculus you can't use it either.  Around the point Sympy would actually be useful you also reach the point where your  answer is literally meaningless without all the work that goes with it.  In something like Calculus, there are actually hundreds of right answers.  The professor can only tell by looking at your work, and what something like Sympy gives will be wildly different than yours.  Sympy and similar software packages are intended for real world problems, not academic ones.  Every time I've tried to do what you're doing for school work, I've ended up wasting a lot of time for no meaningful gain.  Maybe it can help you, I don't know, but I think by the time you can trivially swallow something like Sympy you already know a lot of math.

My Blog
Twitter: @ajhicks1992