0

For example, I can type into Google or WolframAlpha 6+6, or 2+237, which could be programmed by asking a user for a and b, then evaluating return a+b. However, I might also type 5*5^(e) or any other combination, yet the program is hard-coded to only evaluate a+b expressions.

It's easy to represent the more complex problems in code, on any common language.

return 5*pow(5,Math.E) #pseudocode

But if I can't expect a user's input to be of a given form, then it isn't as simple as

x = Input("enter coefficient")
b = input("enter base")
p = input("enter power")
print(x*pow(b,p))

With this code, I'm locked-in to my program only able to evaluate a problem of the form x*b^p.

How do people write the code to dynamically handle math expressions of any form?

BadZen
  • 4,083
  • 2
  • 25
  • 48
Alex G
  • 747
  • 4
  • 15
  • 27
  • 1
    This is a very broad question - probably too broad and off-topic. I've edited your tags - "dynamic programming" is something very specific that you don't mean here. Try googling "symbolic math libraries". (Not your downvoter, btw. =/) – BadZen Nov 18 '16 at 03:00
  • By writing an expression parser. People write the code the way they write any other code - by using a text editor and keyboard. – Ken White Nov 18 '16 at 03:01
  • I realize it's pretty broad, moreso than any other question I ever asked here. I wasn't even certain what tags were appropriate, kind of a shot in the dark. Thank you. – Alex G Nov 18 '16 at 03:09
  • Ken, hilarious. :/ Additionally, mathematicians and physicists solve problems by using their brains. If you want to be one, that's all you need to do. Did you know Chess Grand Masters become champions by winning? So to get better at chess, start winning. Also, mechanics fix cars by using tools and their hands. So next time your car breaks, just use your hands and a few tools. Piece of cake. ..... Yes, these are all valid answers to the questions someone looking for information might ask, but they aren't helpful. Thanks for encouraging others to learn, though, with your helpful answers. – Alex G Nov 18 '16 at 03:17
  • Sorry. Broad, vague questions get broad, vague answers. Want a better answer? Ask a better question., And thanks; when my car breaks, the vast majority of the time I do use my hands and a few tools. I've done most of the repairs on my own car for decades now. I also play chess quite well; I was a champion in high school. Your most **specific** questions here was *How do people write the code to dynamically handle math expressions of any form?*, which my comment answered. Don't try to blame me because you posted poorly. – Ken White Nov 18 '16 at 16:06

1 Answers1

1

This might not be a question that 'appropriate' for this venue. But I think it's reasonable to ask. At the risk of having my answer voted out of existence along with the question, I'll offer a brief answer.

Legitimate mathematical expressions, from simple to complicated, obey grammatical rules. Although a legal mathematical expression might seem unintelligible, grammatically speaking it will be far less complicated that the grammar needed to understand small bodies of human utterances.

Still, there are levels of 'understanding' built into the products available on the 'net. Google and WolframAlpha are definitely 'high-end'. They attempt to get as close as possible to defining grammars capable of representing human utterance, in effect at least. Nearer the lower end are products such as Sympy which accept much more strictly defined input.

Once the software decides what part of the input is a noun, and what is a verb, so to speak, it proceeds to perform the actions requested.

To understand more you might have to undertake studies of formal language, artificial intelligence, programming and areas I can't imagine.

Bill Bell
  • 21,021
  • 5
  • 43
  • 58