|
From: Ethan A M. <merritt@u.washington.edu> - 2009-01-29 07:00:32
|
On Wednesday 28 January 2009, Ethan A Merritt wrote:
> On Wednesday 28 January 2009, James R. Van Zandt wrote:
> >
> > Ethan A Merritt <merritt@u.washington.edu> writes:
> > >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.
> > ...
> > >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
> >
> > This seems awkward to me.
>
> I agree. I was just it pointing out as something that it is already possible.
Bleah. Make that "I was just pointing it out as something..."
> >
> > >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
> >
> > I think this is about as handy as the definition() function.
> > It would even let us retire the "show functions" command.
> >
> > It would be easy to create an associated string variable when
> > recording the definition of a function.
Right. Here you go:
--- gnuplot/src/command.c 2008-12-12 19:11:34.000000000 -0800
+++ gnuplot-cvs/src/command.c 2009-01-28 22:45:09.000000000 -0800
@@ -530,6 +530,15 @@ define()
memcpy(c_dummy_var, save_dummy, sizeof(save_dummy));
m_capture(&(udf->definition), start_token, c_token - 1);
dummy_func = NULL; /* dont let anyone else use our workspace */
+
+ /* Save function definition in a user-accessible variable */
+ if (1) {
+ char *tmpnam = gp_alloc(6+strlen(udf->udf_name), "varname");
+ strcpy(tmpnam, "GPFUN_");
+ strcat(tmpnam, udf->udf_name);
+ fill_gpval_string(tmpnam, udf->definition);
+ free(tmpnam);
+ }
} else {
/* variable ! */
char *varname = gp_input_line + token[c_token].start_index;
|