|
From: Hans-Bernhard B. <HBB...@t-...> - 2011-01-11 20:07:09
|
On 11.01.2011 18:00, mw...@gm... wrote:
> I want to add finite summation to gnuplot, that is a function sum(a,
> b, x, f), where a, b (int) are the summation boundary, x (double) is
> a varialbe, f (string) is the name of the function to eveluate a
> term. Example usage:
Without changing gnuplot, one can already do this recursive function:
sum_f(a,b,delta) = (a < b) ? 0 : (f(a) + sum_f(a+delta,b,delta))
For more tricky examples, see bivariat.dem
The only aspect of your job this doesn't cover is passing in a function
as another function's argument. gnuplot is a plotting program after
all, not a full-fledged functional programming language.
I don't think adding 'sum' as a new builtin is worth the effort. If a
new builtin is to be added, then that should be an eval() function, i.e.
a function that takes a string and treats it as an expression to
evaluate, so the above could be expanded to
sum(f,a,b,delta) = (a<b)?0 : (eval("f(a)")+sum_f(a+delta,b,delta))
|