1

I'm trying to build a program in Pascal to differentiate mathematical functions. It's working very well (calculate min/max, symmetry, drawing the graph, etc.) but I have to put the functions (i.e. x^3+3x+2) into the source code like this:

function f(x : real): real;
begin 
   f := x * x * x + 3 * x + 2;
end;

Though, I want the user to define the function to differentiate. Obviosly the readln function does not help. Somebody told me the only solution would be a specific parser. But it's very difficult, and I don't know how to do it. My idea would be to extract the function into a *.txt file for example so that it could be changed easily. Is that possible? Can somebody show me a parser which could solve this problem or have anybody some other great solution? I would really appreciate your help! Thanks in advance ;)

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
Konstantin
  • 33
  • 5
  • It's not necessary to put the same info in your title that you're putting in the tags. The tag system here works very well, and trying to help it by adding it in the subject just causes clutter. Please don't do so. Thanks. – Ken White Apr 04 '13 at 17:20
  • Some information here: http://stackoverflow.com/questions/1326258/mathematical-expression-parser-in-delphi – 500 - Internal Server Error Apr 04 '13 at 17:21
  • @KenWhite Sorry, I'm new here :) – Konstantin Apr 04 '13 at 17:25
  • You want [automatic differentiation](http://en.wikipedia.org/wiki/Automatic_differentiation). – jxh Apr 04 '13 at 18:31
  • Parser10, by Renate Schaaf and updated by Hallvard Vassbotn, is a mathematical parser that would work in your case. See [`Parsing a string formula to an integer result`](http://stackoverflow.com/a/8228855/576719) for more details. – LU RD Apr 04 '13 at 19:54
  • If you use Free Pascal see unit symbolic integrated with the distribution. It also does (symbolic) differentiation. – Marco van de Voort Apr 05 '13 at 08:30
  • you can first ask for the order of the function, and then for the coefficients –  Apr 09 '13 at 16:00

2 Answers2

0

Free Pascal ships with the symbolic package, which has both a parser and evaluator for mathematical expressions. You can probably use this as a starting point. See the documentation for usage.

tangentstorm
  • 7,183
  • 2
  • 29
  • 38
0

There are also a number of parsers/evalutaors on SWAG:

I bolded the ones I thought were the most useful. I don't think any of them are as complete as the symbolic package in my other answer, but they might be worth reading if you need help.

(All of this is fairly old code. Unless otherwise stated, the rule with SWAG is to treat this stuff as having a new-style BSD license)

tangentstorm
  • 7,183
  • 2
  • 29
  • 38