[genius] Wed Oct 24 13:38:31 2018 Jiri (George) Lebl <jirka 5z com>



commit 7bd9728b79a886070407ad0027d72a9c16fd9f9c
Author: Jiri (George) Lebl <jiri lebl gmail com>
Date:   Wed Oct 24 13:38:34 2018 -0500

    Wed Oct 24 13:38:31 2018  Jiri (George) Lebl <jirka 5z com>
    
            * src/funclib.c, src/funclibhelper.cP: Add AppendVector, plus a few
              minor fixes
    
            * lib/sets/basic.gel: improve performance, and fix sets of vectors
    
            * lib/linear_algebra/misc.gel: Add MakeColumnVector (alias to
              MakeVecctor), MakeRowVector
    
            * help/C/genius.xml: add docs for the new functions

 ChangeLog                       | 12 ++++++++
 gtkextra/gtkextratypebuiltins.c |  4 +--
 gtkextra/gtkextratypebuiltins.h |  4 +--
 help/C/genius.xml               | 24 +++++++++++++++-
 lib/library-strings.c           |  1 +
 lib/linear_algebra/misc.gel     | 23 +++++++++++++--
 lib/sets/basic.gel              |  4 +--
 src/funclib.c                   | 62 +++++++++++++++++++++++++++++++++++++----
 src/funclibhelper.cP            | 13 +++++++++
 src/geniustests.txt             | 16 +++++++++++
 10 files changed, 149 insertions(+), 14 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 4e4d85b7..fba4351b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+Wed Oct 24 13:38:31 2018  Jiri (George) Lebl <jirka 5z com>
+
+       * src/funclib.c, src/funclibhelper.cP: Add AppendVector, plus a few
+         minor fixes
+       
+       * lib/sets/basic.gel: improve performance, and fix sets of vectors
+
+       * lib/linear_algebra/misc.gel: Add MakeColumnVector (alias to
+         MakeVecctor), MakeRowVector
+
+       * help/C/genius.xml: add docs for the new functions
+
 Tue Oct 09 23:18:17 2018  Jiri (George) Lebl <jirka 5z com>
 
        * examples/duffing-equation-trajectories.gel: Add a comment
diff --git a/gtkextra/gtkextratypebuiltins.c b/gtkextra/gtkextratypebuiltins.c
index 04ee4993..a4002116 100644
--- a/gtkextra/gtkextratypebuiltins.c
+++ b/gtkextra/gtkextratypebuiltins.c
@@ -1,6 +1,6 @@
 #include "gtkextratypebuiltins.h"
 
-
+/* This file is generated by glib-mkenums, do not modify it. This code is licensed under the same license as 
the containing project. Note that it links to GLib, so must comply with the LGPL linking clauses. */
 
 #define GTKEXTRA_ENABLE_BROKEN
 #include "gtkextra.h"
@@ -447,5 +447,5 @@ gtk_plot_units_get_type (void)
   return etype;
 }
 
-
+/* Generated data ends here */
 
diff --git a/gtkextra/gtkextratypebuiltins.h b/gtkextra/gtkextratypebuiltins.h
index 4fb787c9..7e136e90 100644
--- a/gtkextra/gtkextratypebuiltins.h
+++ b/gtkextra/gtkextratypebuiltins.h
@@ -1,5 +1,5 @@
 
-
+/* This file is generated by glib-mkenums, do not modify it. This code is licensed under the same license as 
the containing project. Note that it links to GLib, so must comply with the LGPL linking clauses. */
 
 #ifndef __GTKEXTRA_TYPE_BUILTINS_H__
 #define __GTKEXTRA_TYPE_BUILTINS_H__
@@ -71,5 +71,5 @@ G_END_DECLS
 
 #endif /* __GTKEXTRA_TYPE_BUILTINS_H__ */
 
-
+/* Generated data ends here */
 
diff --git a/help/C/genius.xml b/help/C/genius.xml
index f4f4d830..dd730b56 100644
--- a/help/C/genius.xml
+++ b/help/C/genius.xml
@@ -5381,6 +5381,16 @@ If <varname>q</varname> is not prime results are bogus.</para>
     <sect1 id="genius-gel-function-list-matrix">
       <title>Matrix Manipulation</title>
       <variablelist>
+        <varlistentry>
+         <term><anchor id="gel-function-AppendElement"/>AppendElement</term>
+         <listitem>
+          <synopsis>AppendElement (v,elt)</synopsis>
+         <para>Append an element to a vector and return the vector.  No expansion is done.  Normally
+                 a row vector is built if starting from <constant>null</constant> or a 1-by-1 matrix,
+                 but if given a column vector it will properly build a column vector.</para>
+         </listitem>
+        </varlistentry>
+
         <varlistentry>
          <term><anchor id="gel-function-ApplyOverMatrix"/>ApplyOverMatrix</term>
          <listitem>
@@ -5694,8 +5704,20 @@ functions make this check.  Values can be any number including complex numbers.<
          <term><anchor id="gel-function-MakeVector"/>MakeVector</term>
          <listitem>
           <synopsis>MakeVector (A)</synopsis>
+          <para>Alias: <function>MakeColumnVector</function></para>
           <para>Make column vector out of matrix by putting columns above
-                 each other.  Returns <constant>null</constant> when given <constant>null</constant>.</para>
+                 each other.  Returns <constant>null</constant> when given <constant>null</constant>.
+                 Can be used to ensure a vector is a column vector.</para>
+         </listitem>
+        </varlistentry>
+
+        <varlistentry>
+         <term><anchor id="gel-function-MakeRowVector"/>MakeRowVector</term>
+         <listitem>
+          <synopsis>MakeRowVector (A)</synopsis>
+         <para>Make row vector out of matrix by putting rows one after another.
+                 Returns <constant>null</constant> when given <constant>null</constant>.
+                 Can be used to ensure a vector is a row vector.</para>
          </listitem>
         </varlistentry>
 
diff --git a/lib/library-strings.c b/lib/library-strings.c
index d9ac1504..27c63408 100644
--- a/lib/library-strings.c
+++ b/lib/library-strings.c
@@ -80,6 +80,7 @@ char *fake = N_("Delete a row of a matrix");
 char *fake = N_("Get the dot product of two vectors (no conjugates)");
 char *fake = N_("Zero out entries above the diagonal");
 char *fake = N_("Make diagonal matrix from a vector");
+char *fake = N_("Make column vector out of matrix by putting rows after each other");
 char *fake = N_("Make column vector out of matrix by putting columns above each other");
 char *fake = N_("Calculate the product of all elements in a matrix");
 char *fake = N_("Calculate the sum of all elements in a matrix");
diff --git a/lib/linear_algebra/misc.gel b/lib/linear_algebra/misc.gel
index 4599df79..1aea93ff 100644
--- a/lib/linear_algebra/misc.gel
+++ b/lib/linear_algebra/misc.gel
@@ -340,12 +340,31 @@ function CompoundMatrix(k,A) = (
 
 SetHelp("MakeVector", "matrix", "Make column vector out of matrix by putting columns above each other")
 function MakeVector(A) = (
-       if IsNull(A) then return null
+       if IsNull(A) then
+               return null
        else if not IsMatrix(A) then
-               (error("MakeVector: arguments not a matrix");bailout);
+               (error("MakeVector: argument not a matrix");bailout)
+       else if columns(A) == 1 then
+               return A;
        r = null;
        for k=1 to columns(A) do (
                r = [r;A@(,k)]
        );
        r
 )
+SetHelpAlias("MakeVector", "MakeColumnVector")
+MakeColumnVector = MakeVector
+
+SetHelp("MakeRowVector", "matrix", "Make column vector out of matrix by putting rows after each other")
+function MakeRowVector(A) = (
+       if IsNull(A) then return null
+       else if not IsMatrix(A) then
+               (error("MakeRowVector: argument not a matrix");bailout)
+       else if rows(A) == 1 then
+               return A;
+       r = null;
+       for k=1 to rows(A) do (
+               r = [r,A@(k,)]
+       );
+       r
+)
diff --git a/lib/sets/basic.gel b/lib/sets/basic.gel
index 0dac732d..ff331d36 100644
--- a/lib/sets/basic.gel
+++ b/lib/sets/basic.gel
@@ -6,7 +6,7 @@ SetHelp ("Union", "sets", "Returns a set theoretic union of X and Y (X and Y are
 function Union(X,Y) =
   (
    for x in X do
-    if not IsIn(x,Y) then Y=[Y,x];
+    if not IsIn(x,Y) then Y@(elements(Y)+1)=x;
    Y
   )
 
@@ -15,6 +15,6 @@ function MakeSet(X) =
   (
    S = null;
    for x in X do
-    if not IsIn(x,S) then S=[S,x];
+    if not IsIn(x,S) then S@(elements(S)+1)=x;
    S
   )
diff --git a/src/funclib.c b/src/funclib.c
index 71145acf..b01f6543 100644
--- a/src/funclib.c
+++ b/src/funclib.c
@@ -1,5 +1,5 @@
 /* GENIUS Calculator
- * Copyright (C) 1997-2017 Jiri (George) Lebl
+ * Copyright (C) 1997-2018 Jiri (George) Lebl
  *
  * Author: Jiri (George) Lebl
  *
@@ -2541,6 +2541,7 @@ exp_op(GelCtx *ctx, GelETree * * a, gboolean *exception)
                        return NULL;
                }
                if G_UNLIKELY (_internal_exp_function == NULL) {
+                       /* FIXME: this really is not good*/
                        _internal_exp_function = d_makeufunc(d_intern("<internal>exp"),
                                                             gel_parseexp
                                                             ("s = float(x^0); "
@@ -3709,6 +3710,55 @@ SetMatrixSize_op(GelCtx *ctx, GelETree * * a, gboolean *exception)
        return n;
 }
 
+static GelETree *
+AppendElement_op(GelCtx *ctx, GelETree * * a, gboolean *exception)
+{
+       GelETree *n;
+       GelMatrixW *m;
+       int eltnum;
+
+       if G_UNLIKELY ( ! check_argument_vector_or_null (a, 0, "AppendElement"))
+               return NULL;
+
+       /*
+        * Evil optimization to avoid copying the node from the argument
+        */
+       n = gel_stealnode (a[0]);
+       if (n->type == GEL_NULL_NODE) {
+               n->type = GEL_MATRIX_NODE;
+               n->mat.matrix = m = gel_matrixw_new();
+               n->mat.quoted = FALSE;
+               gel_matrixw_set_size (m, 1, 1);
+
+               eltnum = 0;
+
+               /* trivially rref */
+               m->rref = 1;
+
+               m->cached_value_only = 1;
+               m->value_only = 1;
+               m->cached_value_only_real = 1;
+               m->value_only_real = 1;
+               m->cached_value_only_rational = 1;
+               m->value_only_rational = 1;
+               m->cached_value_only_integer = 1;
+               m->value_only_integer = 1;
+               m->cached_value_or_bool_only = 1;
+               m->value_or_bool_only = 1;
+       } else {
+               m = n->mat.matrix;
+               eltnum = gel_matrixw_elements(m);
+       }
+
+       gel_matrixw_set_velement (m,
+                                 eltnum,
+                                 /*
+                                  * Evil optimization to avoid copying the node from the argument
+                                  */
+                                 gel_stealnode(a[1]));
+       return n;
+}
+
 static GelETree *
 IndexComplement_op(GelCtx *ctx, GelETree * * a, gboolean *exception)
 {
@@ -7215,13 +7265,15 @@ gel_funclib_addall(void)
        VFUNC (ones, 2, "rows,columns", "matrix", N_("Make an matrix of all ones (or a row vector)"));
        f->no_mod_all_args = 1;
 
+       FUNC (AppendElement, 2, "v,e", "matrix", N_("Append an element to a vector (treating 1x1 matrix as a 
row vector)"));
+
        FUNC (rows, 1, "M", "matrix", N_("Get the number of rows of a matrix"));
        FUNC (columns, 1, "M", "matrix", N_("Get the number of columns of a matrix"));
        FUNC (IsMatrixSquare, 1, "M", "matrix", N_("Is a matrix square"));
        FUNC (IsVector, 1, "v", "matrix", N_("Is argument a horizontal or a vertical vector"));
-       FUNC (IsUpperTriangular, 1, "v", "matrix", N_("Is a matrix upper triangular"));
-       FUNC (IsLowerTriangular, 1, "v", "matrix", N_("Is a matrix lower triangular"));
-       FUNC (IsDiagonal, 1, "v", "matrix", N_("Is a matrix diagonal"));
+       FUNC (IsUpperTriangular, 1, "M", "matrix", N_("Is a matrix upper triangular"));
+       FUNC (IsLowerTriangular, 1, "M", "matrix", N_("Is a matrix lower triangular"));
+       FUNC (IsDiagonal, 1, "M", "matrix", N_("Is a matrix diagonal"));
        FUNC (elements, 1, "M", "matrix", N_("Get the number of elements of a matrix"));
 
        FUNC (ref, 1, "M", "linear_algebra", N_("Get the row echelon form of a matrix"));
@@ -7289,7 +7341,7 @@ gel_funclib_addall(void)
        VFUNC (DividePoly, 3, "p,q,r", "polynomial", N_("Divide polynomial p by q, return the remainder in 
r"));
        FUNC (PolyDerivative, 1, "p", "polynomial", N_("Take polynomial (as vector) derivative"));
        FUNC (Poly2ndDerivative, 1, "p", "polynomial", N_("Take second polynomial (as vector) derivative"));
-       FUNC (TrimPoly, 1, "p", "polynomial", N_("Trim zeros from a polynomial (as vector)"));
+       FUNC (TrimPoly, 1, "p", "polynomial", N_("Trim zeros from a vector pretending to be a polynomial, 
that is trim trailing zero elements"));
        FUNC (IsPoly, 1, "p", "polynomial", N_("Check if a vector is usable as a polynomial"));
        VFUNC (PolyToString, 2, "p,var", "polynomial", N_("Make string out of a polynomial (as vector)"));
        FUNC (PolyToFunction, 1, "p", "polynomial", N_("Make function out of a polynomial (as vector)"));
diff --git a/src/funclibhelper.cP b/src/funclibhelper.cP
index f271431e..30ae3dda 100644
--- a/src/funclibhelper.cP
+++ b/src/funclibhelper.cP
@@ -182,6 +182,19 @@ check_argument_matrix_or_null (GelETree **a, int argnum, const char *funcname)
        return TRUE;
 }
 
+static inline gboolean
+check_argument_vector_or_null (GelETree **a, int argnum, const char *funcname)
+{
+       if G_UNLIKELY (! (a[argnum]->type == GEL_NULL_NODE ||
+                         (a[argnum]->type == GEL_MATRIX_NODE &&
+                          (gel_matrixw_width (a[argnum]->mat.matrix) == 1 ||
+                           gel_matrixw_height (a[argnum]->mat.matrix) == 1)))) {
+               gel_errorout (_("%s: argument number %d not a vector or null node"), funcname, argnum+1);
+               return FALSE;
+       }
+       return TRUE;
+}
+
 static inline gboolean
 check_argument_value_only_matrix (GelETree **a, int argnum, const char *funcname)
 {
diff --git a/src/geniustests.txt b/src/geniustests.txt
index 865b8c3b..d898a8d6 100644
--- a/src/geniustests.txt
+++ b/src/geniustests.txt
@@ -866,6 +866,10 @@ if 1i then 1 else 0                                                1
 A=[1,2,3,4;1i,2,3i,4;1,1,1,1;1i,1i,1i,9];A^-1 * A == I(4)      true
 IntegerFromBoolean(1i)                                         1
 MakeVector([1,2;3,4])                                          [1;3;2;4]
+MakeRowVector([1,2;3,4])                                       [1,2,3,4]
+MakeRowVector([1,2])                                           [1,2]
+MakeVector([1,2])                                              [1;2]
+MakeVector([1;2])                                              [1;2]
 MakeVector(null)+1                                             ((null)+1)
 Gamma(5)                                                       24.0
 ToeplitzMatrix ([1,2i,3i])                                     [1,2i,3i;-2i,1,2i;-3i,-2i,1]
@@ -1289,6 +1293,18 @@ Factorial([0,1,2,3,4,5,6])                                       [1,1,2,6,24,120,720]
 DoubleFactorial([0,1,2,3,4,5,6])                               [1,1,2,3,8,15,48]
 StripZeroColumns(zeros(4,4))+0                                 ((null)+0)
 Image(zeros(4,4))+0                                            ((null)+0)
+elements(null)                                                 0
+elements([1,2])                                                        2
+elements(`[1,[1,2]])                                           2
+elements([1;2])                                                        2
+elements([1,3;2,8])                                            4
+v=[1,2,3];w=AppendElement(v,5)                                 [1,2,3,5]
+v=[1;2;3];w=AppendElement(v,5)                                 [1;2;3;5]
+v=[1];w=AppendElement(v,5)                                     [1,5]
+v=null;w=AppendElement(v,5)                                    [5]
+v=[1,2,3];w=AppendElement(v,5);v                               [1,2,3]
+v=[1,2,3];w=AppendElement(v,v)                                 [1,2,3,[1,2,3]]
+v=[1,2,3];w=AppendElement(v,v);v                               [1,2,3]
 load "nullspacetest.gel"                                       true
 load "longtest.gel"                                            true
 load "testprec.gel"                                            true


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