Jump to content

How to make a calculator that can solve equations?

-TesseracT-
Go to solution Solved by ZEJ,

To get your head moving, something you can't overlook from the beginning is you're going to need some way for your calculator to determine WHAT formula is currently provided by the user input, because your parser is going to vary heavily as you get into different formulas. Regular Expressions are going to be your friend here, and learning those will absolutely help you down the road as a programmer. Once you can identify formulas from user input you'll then need to start building your methods to handle parsing out those formulas. Try to be as modular as you can, keeping certain chunks that you know will carry over to a number of formulas their own methods so you can re-use them. 

 

From a Programming standpoint most of the basic math is going to be very simple, you're going to find determining what formula you're dealing with and parsing said formulas to be the hardest part, as you will need a deep understanding from a Mathematical point of view, and then you'll need to be able to put that understanding to code. 

 

Good luck! And don't let the haters make it sound impossible, there are TONS of programs out their that charge a ton of money for their services, it doesn't mean its not approachable it just likely means it will take a good portion of time and knowledge. 

I'm familiar with basic calculators, I've done them multiple times. But now I want to make one that can solve equations and show each step for solving it. How on earth do you do this? 

 

Similar to this:  http://www.wolframalpha.com/

Link to comment
Share on other sites

Link to post
Share on other sites

You know that Wolframalpha is made by very competent mathematicians and programmers, and that they sell their software for huge amounts of money right? If anyone could make it I don't think they could earn any money from their software ...

Link to comment
Share on other sites

Link to post
Share on other sites

doing a calculator that solves equations shouldn't be very hard, depending on which equations you want to solve.

of the top of my head, you ither input all the equation as a string and then process it to get each term or you request each term in orde (i mean for example in a ax^2+bx+c=0 request a, then b and finally c)

then it would be a matter of writting every output you may want

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

You know that Wolframalpha is made by very competent mathematicians and programmers, and that they sell their software for huge amounts of money right? If anyone could make it I don't think they could earn any money from their software ...

yeah I figured that, I just wanted to maybe start small with very simple equations.

 

doing a calculator that solves equations shouldn't be very hard, depending on which equations you want to solve.

of the top of my head, you ither input all the equation as a string and then process it to get each term or you request each term in orde (i mean for example in a ax^2+bx+c=0 request a, then b and finally c)

then it would be a matter of writting every output you may want

ok thanks, seems fairly complicated.

Link to comment
Share on other sites

Link to post
Share on other sites

This is absolutely no small task. The programming aspect might not really be your hardest proponent with this one, you'd need some VERY strong mathematical skills once you start getting into some complex formulas. This could definitely be a fun project to keep yourself busy and learn, however with this one you'd be learning more about Math than Programming, so its up to what your end goal is. 

Link to comment
Share on other sites

Link to post
Share on other sites

This is absolutely no small task. The programming aspect might not really be your hardest proponent with this one, you'd need some VERY strong mathematical skills once you start getting into some complex formulas. This could definitely be a fun project to keep yourself busy and learn, however with this one you'd be learning more about Math than Programming, so its up to what your end goal is. 

I'm very interested in both programming and mathematics, so this could be something for me.

Link to comment
Share on other sites

Link to post
Share on other sites

ok thanks, seems fairly complicated.

 

yfX2KDp.png

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

To get your head moving, something you can't overlook from the beginning is you're going to need some way for your calculator to determine WHAT formula is currently provided by the user input, because your parser is going to vary heavily as you get into different formulas. Regular Expressions are going to be your friend here, and learning those will absolutely help you down the road as a programmer. Once you can identify formulas from user input you'll then need to start building your methods to handle parsing out those formulas. Try to be as modular as you can, keeping certain chunks that you know will carry over to a number of formulas their own methods so you can re-use them. 

 

From a Programming standpoint most of the basic math is going to be very simple, you're going to find determining what formula you're dealing with and parsing said formulas to be the hardest part, as you will need a deep understanding from a Mathematical point of view, and then you'll need to be able to put that understanding to code. 

 

Good luck! And don't let the haters make it sound impossible, there are TONS of programs out their that charge a ton of money for their services, it doesn't mean its not approachable it just likely means it will take a good portion of time and knowledge. 

Link to comment
Share on other sites

Link to post
Share on other sites

yfX2KDp.png

Yeah ok, you don't have to make such "comments" if you're not contributing to the thread. 

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah ok, you don't have to make such "comments" if you're not contributing to the thread. 

 

Alas my jests seem wasted  :lol:

 

5qfF77G.jpg

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

i thought about this at the time of the Problem Solving thingy involving AI

i always wondered how wolfram alpha could solve equations, and with best first search algorithms i thought i could have some sort of idea of how to face such a problem. that said, i never actually tried to implement such a thing, but i might one day, it seems challenging yet feasible

 

the alternative is to match the input with a ton of regexp and just insert a resolutive expression for each format, but it's fairly uninteresting and it's got a lot of limits

Link to comment
Share on other sites

Link to post
Share on other sites

There are a few open-source CAS (computer algebra systems) whose code you could have a look at to see how these things are done. But that's basically what you'd need to implement, a CAS.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

There are a few open-source CAS (computer algebra systems) whose code you could have a look at to see how these things are done. But that's basically what you'd need to implement, a CAS.

*Goes to Wikipedia*

"Wolfram Alpha is written in 15 million lines of Wolfram Language code"

*gives up*

:P

Link to comment
Share on other sites

Link to post
Share on other sites

*Goes to Wikipedia*

"Wolfram Alpha is written in 15 million lines of Wolfram Language code"

*gives up*

:P

Yeah... it's not at all a trivial task (although Wolfram Alpha is not really a CAS, it's a bit more than that, but yes, it has its origins in Mathematica, which is a CAS). Not open source though (see this list if you haven't come across it already).

However, if your aim is to have fun and just write some code about something you're interested in, a fully-fledged CAS system would not necessarily need to be your end goal.

For example, you could start out with polynomials. Educate yourself about the mathematics behind them, then try to maybe implement a program which finds roots for polynomials. To keep things simpler, maybe start out with linear, quadratic and cubic polynomials, then move on to higher powers and so on.

But yeah, if you're really serious about this... it's not going to be a "quick project on the side"... :D

(it could however be a fantastic opportunity to improve both your math and programming knowledge, quite a nice idea for a learning project actually IMHO)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah... it's not at all a trivial task (although Wolfram Alpha is not really a CAS, it's a bit more than that, but yes, it has its origins in Mathematica, which is a CAS). Not open source though (see this list if you haven't come across it already).

However, if your aim is to have fun and just write some code about something you're interested in, a fully-fledged CAS system would not necessarily need to be your end goal.

For example, you could start out with polynomials. Educate yourself about the mathematics behind them, then try to maybe implement a program which finds roots for polynomials. To keep things simpler, maybe start out with linear, quadratic and cubic polynomials, then move on to higher powers and so on.

But yeah, if you're really serious about this... it's not going to be a "quick project on the side"... :D

(it could however be a fantastic opportunity to improve both your math and programming knowledge, quite a nice idea for a learning project actually IMHO)

Thank you. Yes I'm interested in mathematics and programming and want to expand my knowledge and just have fun. I get A's in math and programming courses and would really like to become a programmer or/and a software engineer. AI intrigues me as well and I think I have the potential of becoming something great if I try hard enough. Fortunately I've come to love programming more and more recently and am very excited about continuing coding. :)
Link to comment
Share on other sites

Link to post
Share on other sites

If you find mathematics and programming interesting, then Project Euler might be a great site for you. It has hundreds of problems that will test both your mathematical and programming knowledge. It gets very difficult but many of the starting problems are beginner friendly. When you solve a problem you get to see how other people solved it in many different languages which can also be neat. You'll likely see a solution for the language you use and may see ways to improve your own solution.

Link to comment
Share on other sites

Link to post
Share on other sites

A general strategy is to build a tree. This is preferable to using regular expressions as there are simpy too many possible forms of equations to account for - just think of the number of ways a simple equation can be written.

 

2 + 3 * (1 - 5)^2

 

This should translate to the tree:

  + / \ 2  \    *   / \   3  \      ^     / \     - 2    / \    1 5

The tree is built by tokenizing the input string one character at time. Once you have built such a data structure you're ready for the hard part of determining what kind of equation you have and what can be derived from it - roots, integrals, a graph, and so forth.

Link to comment
Share on other sites

Link to post
Share on other sites

If you find mathematics and programming interesting, then Project Euler might be a great site for you. It has hundreds of problems that will test both your mathematical and programming knowledge. It gets very difficult but many of the starting problems are beginner friendly. When you solve a problem you get to see how other people solved it in many different languages which can also be neat. You'll likely see a solution for the language you use and may see ways to improve your own solution.

I've heard of it, thank you I will try. :)

Link to comment
Share on other sites

Link to post
Share on other sites

You know that Wolframalpha is made by very competent mathematicians and programmers

 

Although whoever's writing the parser isn't amazing.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×