[genius] Mon Mar 17 22:31:01 2014 Jiri (George) Lebl <jirka 5z com>



commit c42c50134417dab028ee1dd0787156f2257c3b6d
Author: Jiri (George) Lebl <jiri lebl gmail com>
Date:   Mon Mar 17 22:32:01 2014 -0500

    Mon Mar 17 22:31:01 2014  Jiri (George) Lebl <jirka 5z com>
    
        * src/calc.[ch], src/funclib.c: Implement DisplayVariables which
          shows either individual variables or a complete listing including
          stacktrace if called inside another function.
    
        * lib/misc/misc.gel: Add PrintTable which prints a table of a
          function
    
        * help/C/genius.xml: Document the above

 ChangeLog             |   11 ++++
 NEWS                  |    3 +-
 help/C/genius.xml     |   36 +++++++++++++-
 help/genius.txt       |   30 +++++++++++
 lib/library-strings.c |    1 +
 lib/misc/misc.gel     |   18 +++++++
 src/calc.c            |   33 ++++++++++++-
 src/calc.h            |    4 ++
 src/funclib.c         |  128 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/gnome-genius.c    |    2 -
 10 files changed, 261 insertions(+), 5 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3ae2af2..a7dae1e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+Mon Mar 17 22:31:01 2014  Jiri (George) Lebl <jirka 5z com>
+
+       * src/calc.[ch], src/funclib.c: Implement DisplayVariables which
+         shows either individual variables or a complete listing including
+         stacktrace if called inside another function.
+
+       * lib/misc/misc.gel: Add PrintTable which prints a table of a
+         function
+       
+       * help/C/genius.xml: Document the above
+
 Wed Mar 12 17:01:07 2014  Jiri (George) Lebl <jirka 5z com>
 
        * src/funclib.c, src/calc.h: Implement NonzeroColumns and
diff --git a/NEWS b/NEWS
index a8027d4..e86d6c6 100644
--- a/NEWS
+++ b/NEWS
@@ -1,7 +1,8 @@
 Changes to 1.0.18
 
 * New functions: KroneckerProduct (alias TensorProduct), NewtonsMethod,
-  HalleysMethod, LambertW, LambertWm1, NonzeroColumns, NonzeroElements
+  HalleysMethod, LambertW, LambertWm1, NonzeroColumns, NonzeroElements,
+  DisplayVariables, PrintTable
 * New PlotCanvasFreeze/PlotCanvasThaw functions to improve flicker
   if doing animations with genius.
 * Allow setting color in LinePlotDrawLine with RGB vector.
diff --git a/help/C/genius.xml b/help/C/genius.xml
index eddb0cd..c749238 100644
--- a/help/C/genius.xml
+++ b/help/C/genius.xml
@@ -4,7 +4,7 @@
   <!ENTITY app "<application>Genius Mathematics Tool</application>">
   <!ENTITY appname "Genius">
   <!ENTITY appversion "1.0.18">
-  <!ENTITY date "October 2013">
+  <!ENTITY date "March 2014">
 
   <!ENTITY legal SYSTEM "legal.xml">
 
@@ -3102,6 +3102,25 @@ is called inside a block that was evaluated using modular arithmetic (using <lit
         </varlistentry>
 
         <varlistentry>
+         <term><anchor id="gel-function-DisplayVariables"/>DisplayVariables</term>
+         <listitem>
+          <synopsis>DisplayVariables (var1,var2,...)</synopsis>
+         <para>Display set of variables.  The variables can be given as
+                 strings or identifiers.  For example:
+           <programlisting>DisplayVariables(`x,`y,`z)
+           </programlisting>
+         </para>
+         <para>
+                 If called without arguments (must supply empty argument list) as
+           <programlisting>DisplayVariables()
+           </programlisting>
+           then all variables are printed including a stacktrace similar to
+           <guilabel>Show user variables</guilabel> in the graphical version.
+         </para>
+         </listitem>
+        </varlistentry>
+
+        <varlistentry>
          <term><anchor id="gel-function-error"/>error</term>
          <listitem>
           <synopsis>error (str)</synopsis>
@@ -3154,6 +3173,21 @@ made into a string before being printed.</para>
         </varlistentry>
 
         <varlistentry>
+         <term><anchor id="gel-function-PrintTable"/>PrintTable</term>
+         <listitem>
+          <synopsis>PrintTable (f,v)</synopsis>
+         <para>Print a table of values for a function.  The values are in the 
+                 vector <varname>v</varname>.  You can use the vector
+                 building notation as follows:
+                 <programlisting>PrintTable (f,[0:10])
+           </programlisting>
+           If <varname>v</varname> is a positive integer, then the table of
+           integers from 1 up to and including v will be used.
+         </para>
+         </listitem>
+        </varlistentry>
+
+        <varlistentry>
          <term><anchor id="gel-function-protect"/>protect</term>
          <listitem>
           <synopsis>protect (id)</synopsis>
diff --git a/help/genius.txt b/help/genius.txt
index c09d9cf..70017c0 100644
--- a/help/genius.txt
+++ b/help/genius.txt
@@ -2383,6 +2383,23 @@ display (str,expr)
           Display a string and an expression with a colon to
           separate them.
 
+   DisplayVariables
+
+DisplayVariables (var1,var2,...)
+
+          Display set of variables. The variables can be given as
+          strings or identifiers. For example:
+
+DisplayVariables(`x,`y,`z)
+
+          If called without arguments (must supply empty argument
+          list) as
+
+DisplayVariables()
+
+          then all variables are printed including a stacktrace
+          similar to Show user variables in the graphical version.
+
    error
 
 error (str)
@@ -2427,6 +2444,19 @@ printn (str)
           argument str can be any expression. It is made into a
           string before being printed.
 
+   PrintTable
+
+PrintTable (f,v)
+
+          Print a table of values for a function. The values are
+          in the vector v. You can use the vector building
+          notation as follows:
+
+PrintTable (f,[0:10])
+
+          If v is a positive integer, then the table of integers
+          from 1 up to and including v will be used.
+
    protect
 
 protect (id)
diff --git a/lib/library-strings.c b/lib/library-strings.c
index 7b962c7..35bb600 100644
--- a/lib/library-strings.c
+++ b/lib/library-strings.c
@@ -1,5 +1,6 @@
 char *fake = N_("Compose two functions");
 char *fake = N_("Compose a function with itself n times, passing x as argument, and returning x if n == 0");
+char *fake = N_("Print a table of values for f(n) for numbers from from vector v, or if v is a number for 
integers from 1 to v");
 char *fake = N_("Make a string");
 char *fake = N_("Tolerance of the Chop function");
 char *fake = N_("How many iterations to try to find the limit for continuity and limits");
diff --git a/lib/misc/misc.gel b/lib/misc/misc.gel
index 194ca5a..b64d91f 100644
--- a/lib/misc/misc.gel
+++ b/lib/misc/misc.gel
@@ -14,3 +14,21 @@ function ComposePower(f,n,x) = (
                x = f call (x);
        x
 )
+
+SetHelp("PrintTable","basic","Print a table of values for f(n) for numbers from from vector v, or if v is a 
number for integers from 1 to v")
+function PrintTable(f,v) = (
+       local *;
+       # Note we can't check the 2 arguments, FIXME
+       if not IsFunction(f) then
+               (error("PrintTable: f must be a function of one argument");bailout);
+
+       if IsVector(v) then (
+               for k in v do
+                       print("" + k + "\t" + (f call (k)))
+       ) else if IsPositiveInteger(v) then (
+               for k=1 to v do
+                       print("" + k + "\t" + (f call (k)))
+       ) else (
+               (error("PrintTable: v must be a vector or a positive integer");bailout)
+       )
+)
diff --git a/src/calc.c b/src/calc.c
index 9b97d9a..1dd0dae 100644
--- a/src/calc.c
+++ b/src/calc.c
@@ -1,5 +1,5 @@
 /* GENIUS Calculator
- * Copyright (C) 1997-2012 Jiri (George) Lebl
+ * Copyright (C) 1997-2014 Jiri (George) Lebl
  *
  * Author: Jiri (George) Lebl
  *
@@ -1375,6 +1375,37 @@ append_func (GelOutput *gelo, GelEFunc *f)
        }
 }
 
+void
+gel_print_func (GelOutput *gelo,
+               GelEFunc *f)
+{
+       if G_UNLIKELY (f == NULL) {
+               gel_errorout (_("NULL function!"));
+               gel_output_string (gelo, "(?)");
+               return;
+       }
+
+       if (f->type == GEL_VARIABLE_FUNC) {
+               gel_print_etree (gelo, f->data.user, FALSE);
+       } else {
+               gel_output_push_nonotify (gelo);
+
+               /* all non-value nodes printed as <ci></ci> and
+                * value nodes as <cn></cn> */
+               if (gel_calcstate.output_style == GEL_OUTPUT_MATHML)
+                       gel_output_string (gelo, "<ci>");
+
+               append_func (gelo, f);
+
+               /* all non-value nodes printed as <ci></ci> and
+                * value nodes as <cn></cn> */
+               if (gel_calcstate.output_style == GEL_OUTPUT_MATHML)
+                       gel_output_string (gelo, "</ci>");
+
+               gel_output_pop_nonotify (gelo);
+       }
+}
+
 /*make a string representation of an expression*/
 void
 gel_print_etree (GelOutput *gelo,
diff --git a/src/calc.h b/src/calc.h
index 1e7f2d6..89a88d5 100644
--- a/src/calc.h
+++ b/src/calc.h
@@ -86,6 +86,10 @@ void gel_pretty_print_etree  (GelOutput *gelo,
 /* make a newly allocated string for this */
 char * gel_string_print_etree  (GelETree *n);
 
+/* same as gel_print_etree, but only for function */
+void   gel_print_func (GelOutput *gelo,
+                       GelEFunc *f);
+
 /* Note that infile must be gel_lexer_open'ed */
 /*this is the function to be mostly called outsied of calc.c
   evaluate the xpression string and give back a string with the
diff --git a/src/funclib.c b/src/funclib.c
index 7232abe..a7d3e58 100644
--- a/src/funclib.c
+++ b/src/funclib.c
@@ -565,6 +565,133 @@ set_op (GelCtx *ctx, GelETree * * a, gboolean *exception)
        return gel_stealnode (a[1]);
 }
 
+static void
+display_all_vars (void)
+{
+       GelContextFrame *all_contexts, *lic;
+       GSList *funcs;
+       GSList *li;
+       gboolean printed_local_title = FALSE;
+
+       all_contexts = d_get_all_contexts ();
+       funcs = d_getcontext_global ();
+
+       gel_output_string (gel_main_out,
+                          _("Global variables:\n\n"));
+
+       for (li = funcs; li != NULL; li = li->next) {
+               GelEFunc *f = li->data;
+               if (f->type != GEL_VARIABLE_FUNC ||
+                   f->id == NULL ||
+                   /* only for toplevel */ f->id->parameter ||
+                   /* only for toplevel */ f->id->protected_ ||
+                   f->id->token == NULL ||
+                   f->data.user == NULL ||
+                   f->context > 0)
+                       continue;
+
+               gel_output_printf (gel_main_out, "%s = ", f->id->token);
+               gel_print_etree (gel_main_out, f->data.user, FALSE /*no toplevel, keep this short*/);
+               gel_output_string (gel_main_out, "\n");
+       }
+
+
+       if (d_curcontext () > 0) {
+               int i = d_curcontext ();
+
+               gel_output_string
+                       (gel_main_out, _("\nFunction call stack:\n"));
+               gel_output_string
+                       (gel_main_out, _("(depth of context in parentheses)\n\n"));
+
+               /* go over all local contexts (not the last one, that is global) */
+               for (lic = all_contexts; lic != NULL && lic->next != NULL; lic = lic->next) {
+                       GelToken *tok = lic->name;
+
+                       if (tok == NULL) {
+                               gel_output_string (gel_main_out, "??");
+                       } else {
+                               gel_output_string (gel_main_out, tok->token);
+                       }
+
+                       gel_output_printf (gel_main_out, " (%d)", i);
+
+                       if (i <= 1) {
+                               gel_output_string (gel_main_out, "\n");
+                       } else {
+                               gel_output_string (gel_main_out, ", ");
+                       }
+
+                       i--;
+               }
+       }
+
+
+       /* go over all local contexts (not the last one, that is global) */
+       for (lic = all_contexts; lic != NULL && lic->next != NULL; lic = lic->next) {
+               for (li = lic->functions; li != NULL; li = li->next) {
+                       GelEFunc *f = li->data;
+                       if (f->type != GEL_VARIABLE_FUNC ||
+                           f->id == NULL ||
+                           f->id->token == NULL ||
+                           f->data.user == NULL ||
+                           f->context <= 0)
+                               continue;
+
+                       if ( ! printed_local_title) {
+                               gel_output_string (gel_main_out,
+                                       _("\nLocal variables:\n"));
+                               gel_output_string (gel_main_out,
+                                       _("(depth of context in parentheses)\n\n"));
+                               printed_local_title = TRUE;
+                       }
+
+                       gel_output_printf (gel_main_out, "(%d) %s = ", f->context, f->id->token);
+                       gel_print_etree (gel_main_out, f->data.user, FALSE /*no toplevel, keep this short*/);
+                       gel_output_string (gel_main_out, "\n");
+               }
+       }
+}
+
+/*set function*/
+static GelETree *
+DisplayVariables_op (GelCtx *ctx, GelETree * * a, gboolean *exception)
+{
+       int j;
+
+       if (a == NULL)
+               display_all_vars ();
+
+       j = 0;
+       while (a != NULL && a[j] != NULL) {
+               GelToken *id;
+               GelEFunc *f;
+               if (a[j]->type == GEL_IDENTIFIER_NODE) {
+                       id = a[j]->id.id;
+               } else if (a[j]->type == GEL_STRING_NODE) {
+                       id = d_intern (a[j]->str.str);
+               } else {
+                       gel_errorout (_("%s: Argument number %d not a string or identifier"),
+                                     "DisplayVariables",
+                                     j+1);
+                       return NULL;
+               }
+
+               f = d_lookup_global (id);
+
+               if (f == NULL) {
+                       gel_output_printf(gel_main_out, _("%s undefined\n"), id->token);
+               } else {
+                       gel_output_printf(gel_main_out, "%s = ", id->token);
+                       gel_print_func (gel_main_out, f);
+                       gel_output_string(gel_main_out, "\n");
+               }
+               j++;
+       }
+
+       return gel_makenum_null ();
+}
+
 /*rand function*/
 static GelETree *
 rand_op (GelCtx *ctx, GelETree * * a, gboolean *exception)
@@ -6699,6 +6826,7 @@ gel_funclib_addall(void)
        FUNC (printn, 1, "str", "basic", N_("Prints an expression without a trailing newline"));
        FUNC (display, 2, "str,expr", "basic", N_("Display a string and an expression"));
        FUNC (set, 2, "id,val", "basic", N_("Set a global variable"));
+       VFUNC (DisplayVariables, 1, "var", "basic", N_("Display values of variables, or all if called without 
arguments"));
 
        FUNC (SetHelp, 3, "id,category,desc", "basic", N_("Set the category and help description line for a 
function"));
        FUNC (SetHelpAlias, 2, "id,alias", "basic", N_("Sets up a help alias"));
diff --git a/src/gnome-genius.c b/src/gnome-genius.c
index e67aa99..19775c2 100644
--- a/src/gnome-genius.c
+++ b/src/gnome-genius.c
@@ -1224,8 +1224,6 @@ populate_var_box (GtkTextBuffer *buffer)
                        if (i <= 1) {
                                gtk_text_buffer_insert_with_tags_by_name
                                        (buffer, &iter, "\n", -1, "context", NULL);
-                               if (i < 1)
-                                       printf ("EKI!!\n");
                        } else if (cols >= 58) {
                                gtk_text_buffer_insert_with_tags_by_name
                                        (buffer, &iter, ",\n", -1, "context", NULL);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]