How can I create a custom keyboard for math functions in WPF?

  • Thread starter Thread starter DivergentSpectrum
  • Start date Start date
  • Tags Tags
    Functions
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 2K views
DivergentSpectrum
Messages
149
Reaction score
15
i would like to have the user enter a math function. ie cos(x)-e^x + x^2
it will be typed into a text box and then the program performs a numerical procedure on the function (ie integration)

In other words, how do i transform the input string into a method?
 
Physics news on Phys.org
bluntwcrackrap said:
i would like to have the user enter a math function. ie cos(x)-e^x + x^2
it will be typed into a text box and then the program performs a numerical procedure on the function (ie integration)

In other words, how do i transform the input string into a method?
Not very easily.
Your code will need to parse the input string, looking for terms (expressions that are added together), operators such as - for negation and ^ for exponents, function keywords such as cos, sin, tan, ln, and so on.
 
hmm, i was thinking it would be complicated. i suppose if i used a keypad (like on a calculator) it would be easier right?
 
bluntwcrackrap said:
hmm, i was thinking it would be complicated. i suppose if i used a keypad (like on a calculator) it would be easier right?
?
All you can get from the keypad are the numeric digits and the arithmetic operators (+, -, *, and /). You still have to parse the input, building numbers out of the input digits, and figuring out which things are added, subtracted, and so on.
 
bluntwcrackrap said:
i would like to have the user enter a math function. ie cos(x)-e^x + x^2
it will be typed into a text box and then the program performs a numerical procedure on the function (ie integration)

In other words, how do i transform the input string into a method?

Have you taken a compiler class yet? That's basically the technique you would use to parse the input and turn it into executable code...
 
You don't need a lot of "compiler theory" to do this. You could build an interpreter to evaluate the expressions quite simply with lexical analysis and parsing tools like lex and yacc (or bison, or whatever the unix community calls it these days). You can probably find something similar as a lex-and-yacc tutorial on the web.

It won't be very efficient to re-interpret the user input every time you want to evaluate the function, but something that works slowly is a better starting point than nothing at all.
 
It depends on which language you are using. There is an easy way and a hard way and a hybrid way.

1) Easy: Some scripting languages can evaluate a string as a line of code even when it is running. Look for languages that can "evaluate" a text string as though it was code in that language. Look for an eval() function. Perl and Python are two such languages.

2) Hard: Otherwise, you will have to parse the input string and perform math operations in code that you write. As AlephZero says, lex and yacc can help you do that. You will need to define a language that can be parsed.

3) Hybrid: Languages like Python can be called from C programs. You can let the Python do the evaluation part and return results to C to do the rest. I don't have any real experience with this.
 
Last edited:
Mark44 said:
?
All you can get from the keypad are the numeric digits and the arithmetic operators (+, -, *, and /). You still have to parse the input, building numbers out of the input digits, and figuring out which things are added, subtracted, and so on.

I mean a built in keyboard where I have buttons for each function built into the ui.