|
From: Ethan M. <merritt@u.washington.edu> - 2011-01-11 17:22:59
|
On Tuesday, January 11, 2011 09:00:19 am mw...@gm... wrote:
> Hi,
>
> 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:
>
> f(n, x) = sin(n*x)
> plot sum(1, 10, x, "f")
That pair of commands does not seem to be compatible with your description.
How is the code to know there is a second parameter to f()? And where would
it get this second parameter from?
> I am stuck at how to call f from sum (see call(f) below) and would appreciate your help very much. (I assume f is the string representing the name of the function to call.)
>
> - for (i=a; i<=b; ++i) {
> - // calculate f_i(x);
> - push(Gcomplex(&tmp, x.real, x.imag)); // todo check the order
> - push(Ginteger(&tmp, i));
> -
> - //call(f); // assume f is the name (string) of the function to call
There is no existing routine that does either call-by-name or
retrieve-user-function-by-name; you would have to write one.
Something like (untested):
struct udft_entry *
get_udf_by_name(char *key)
{
struct udft_entry **udf_ptr = &first_udf;
while (*udf_ptr) {
if (!strcmp(key, (*udf_ptr)->udf_name))
return (*udf_ptr);
udf_ptr = &((*udf_ptr)->next_udf);
}
return NULL;
}
It would belong in eval.c next to add_udv_by_name().
Then you would do something like:
udft_entry *udf = get_udf_by_name( f );
if (udf)
execute_at(udf->at);
else
int_error(NOCARET, "No user-defined function %s",f);
> -
> - pop(&tmp);
> - ret.real += real(&tmp);
> - ret.imag += imag(&tmp);
> - }
>
> Thank you very much for your time and effort.
>
> --- specfun.c 2011-01-11 17:40:19.000000000 +0100
> +++ specfun.c.orig 2011-01-11 17:37:27.000000000 +0100
> @@ -1933,44 +1933,3 @@
> push(Gcomplex(&a, x, 0.0));
> }
>
> -void
> -f_sum(union argument *arg)
> -{
> - struct value tmp;
> - int a, b;
> - struct cmplx x, ret, result;
> - int i;
> -
> - (void) arg; /* avoid -Wunused warning */
> -
> - // pop x
> - pop(&tmp);
> - x.real = real(&tmp);
> - x.imag = imag(&tmp);
> -
> - // pop b
> - if (pop(&tmp)->type != INTGR)
> - int_warn(NO_CARET, "expecting an integer as second argument to sum");
> - b = tmp.v.int_val;
> -
> - // pop a
> - if (pop(&tmp)->type != INTGR)
> - int_warn(NO_CARET, "expecting an integer as first argument to sum");
> - a = tmp.v.int_val;
> -
> - ret.real = 0;
> - ret.imag = 0;
> - for (i=a; i<=b; ++i) {
> - // calculate f_i(x);
> - push(Gcomplex(&tmp, x.real, x.imag)); // todo check the order
> - push(Ginteger(&tmp, i));
> -
> - //call(f);
> -
> - pop(&tmp);
> - ret.real += real(&tmp);
> - ret.imag += imag(&tmp);
> - }
> -
> - push(Gcomplex(&tmp, ret.real, ret.imag));
> -}
>
> --- eval.c 2011-01-11 16:25:01.000000000 +0100
> +++ eval.c.orig 2011-01-11 17:38:00.000000000 +0100
> @@ -176,8 +176,6 @@
> {"atanh", f_atanh},
> {"lambertw", f_lambertw}, /* HBB, from G.Kuhnle 20001107 */
>
> - {"sum", f_sum}, /* summation */
> -
> {"column", f_column}, /* for using */
> {"valid", f_valid}, /* for using */
> {"timecolumn", f_timecolumn}, /* for using */
> --- specfun.h 2011-01-11 16:22:05.000000000 +0100
> +++ specfun.h.orig 2011-01-11 17:37:21.000000000 +0100
> @@ -61,6 +61,4 @@
> void f_inverse_erf __PROTO((union argument *x));
> void f_lambertw __PROTO((union argument *x));
>
> -void f_sum __PROTO((union argument *x));
> -
> #endif /* GNUPLOT_SPECFUN_H */
>
>
|