|
From: Ethan A M. <merritt@u.washington.edu> - 2009-01-27 06:54:31
|
On Monday 26 January 2009, James R. Van Zandt wrote:
>
> I propose the patch below, which adds a function that returns the
> string that defines a specified user defined function.
>
> I would like to be able to put the definition of a function into a
> label, like this:
>
> gnuplot> fun(x)=1-x**2/2
> gnuplot> set label definition('fun') at graph .05,.95
> gnuplot> plot [-1:1] fun(x),cos(x)
I agree that the functionality would be nice.
A couple of thoughts:
It is already possible to do essentially the same thing by reversing
the order of operations and using the recently added evaluate() function:
gnuplot> def = "f(x) = 1-x**2/2"
gnuplot> evaluate(def)
gnuplot> plot [-1:1] cos(x), f(x) title def
Another possibility is to make the function definitions visible as
string variables directly, exactly as the existing variables are.
gnuplot> g(x,y) = x**2 + y**3
gnuplot> show variable GPFUN
Variables beginning with GPFUN:
GPFUN_g = "g(x,y) = x**2 + y**3"
gnuplot> set label GPFUN_g at graph .05, .95
The three approaches are each a little different, although I think you
end up with equal functionality in each case. Maybe it wouldn't hurt
to have all three available?
> This becomes useful if the function definition is long, and/or you
> want to plot it for several values of a second variable. Without this
> function, one can of course use "show functions" and cut and paste the
> text into a separate "set label" command. However, that has to be
> repeated each time you change the function definition - i.e. a manual
> action to keep the function definition and the label aligned.
>
> More basically, if you have many functions, this lets you see the
> definition of one without having it scrolled off the screen by a lot
> of others:
>
> gnuplot> print definition('fun')
> fun(x)=1-x**2/2
That seems like a separate question, and could be fixed by allowing you
to request a specific subset of the user-defined functions as we already
do for user-defined variables.
gnuplot> f(x) = 4
gnuplot> show fun g
Variables beginning with GPFUN:
g(x,y) = x**2 + y**3
goo(n) = n**n
> The name is the simplest one I came up with. Maybe "definitionof"
> would be more descriptive.
>
> I included basic documentation in the patch. If it's accepted, I
> would add some cross-references.
>
> - Jim Van Zandt
>
>
> Index: src/internal.h
> ===================================================================
> RCS file: /cvsroot/gnuplot/gnuplot/src/internal.h,v
> retrieving revision 1.19
> diff -u -r1.19 internal.h
> --- src/internal.h 28 Aug 2007 06:13:07 -0000 1.19
> +++ src/internal.h 26 Jan 2009 03:00:33 -0000
> @@ -89,5 +89,6 @@
> void f_strftime __PROTO((union argument *x));
> void f_strptime __PROTO((union argument *x));
> void f_assign __PROTO((union argument *x));
> +void f_definition __PROTO((union argument *x));
>
> #endif /* GNUPLOT_INTERNAL_H */
> Index: src/internal.c
> ===================================================================
> RCS file: /cvsroot/gnuplot/gnuplot/src/internal.c,v
> retrieving revision 1.51
> diff -u -r1.51 internal.c
> --- src/internal.c 25 Sep 2008 18:33:50 -0000 1.51
> +++ src/internal.c 26 Jan 2009 03:00:33 -0000
> @@ -1521,3 +1521,33 @@
> }
> }
>
> +/* Return the string defining the specified user function
> + * JRV Jan 2008
> + */
> +void
> +f_definition(union argument *arg)
> +{
> + struct value a, result;
> + struct udft_entry *udf = first_udf;
> +
> + pop(&a); /* pop the argument */
> +
> + /* Make sure we got a string */
> + if (a.type != STRING)
> + int_error(NO_CARET,"definition requires a string parameter");
> +
> + while (udf) {
> + /* find a function with that name */
> + if (udf->definition && !strcmp(a.v.string_val, udf->udf_name)) {
> + /* push its definition string onto the stack */
> + push(Gstring(&result, udf->definition));
> + goto done;
> + }
> + udf = udf->next_udf;
> + }
> + int_error(NO_CARET,"undefined user function: %s",a.v.string_val);
> + done:
> + gpfree_string(&a);
> +}
> +
> +
> Index: src/eval.c
> ===================================================================
> RCS file: /cvsroot/gnuplot/gnuplot/src/eval.c,v
> retrieving revision 1.71
> diff -u -r1.71 eval.c
> --- src/eval.c 2 Sep 2008 21:17:01 -0000 1.71
> +++ src/eval.c 26 Jan 2009 03:00:34 -0000
> @@ -191,6 +191,7 @@
>
> {"stringcolumn", f_stringcolumn}, /* for using specs */
> {"strcol", f_stringcolumn}, /* shorthand form */
> + {"definition", f_definition}, /* for string variables only */
> {"sprintf", f_sprintf}, /* for string variables only */
> {"gprintf", f_gprintf}, /* for string variables only */
> {"strlen", f_strlen}, /* for string variables only */
> Index: docs/gnuplot.doc
> ===================================================================
> RCS file: /cvsroot/gnuplot/gnuplot/docs/gnuplot.doc,v
> retrieving revision 1.553
> diff -u -r1.553 gnuplot.doc
> --- docs/gnuplot.doc 4 Jan 2009 05:47:14 -0000 1.553
> +++ docs/gnuplot.doc 26 Jan 2009 03:00:39 -0000
> @@ -1082,6 +1082,16 @@
> %c c l .
> %Function@Arguments@Returns
> %_
> +4 definition
> +?expressions functions definition
> +?functions definition
> +?definition
> +=definition
> +#definition("name") & string & string defining the user function named "name" \\
> +%definition("name")@string@string defining the user function named "name"
> + `definition("name")` returns the string defining the user function named
> + "name". For example, `foo(x)=x+3; definition("foo")` returns the
> + string "foo(x)=x+3".
> 4 gprintf
> ?expressions functions gprintf
> ?functions gprintf
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> SourcForge Community
> SourceForge wants to tell your story.
> http://p.sf.net/sfu/sf-spreadtheword
> _______________________________________________
> gnuplot-beta mailing list
> gnu...@li...
> https://lists.sourceforge.net/lists/listinfo/gnuplot-beta
>
--
Ethan A Merritt
|