Commit 7bd9728b authored by Jiri (George) Lebl's avatar Jiri (George) Lebl

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
parent f48a0395
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> Tue Oct 09 23:18:17 2018 Jiri (George) Lebl <jirka@5z.com>
* examples/duffing-equation-trajectories.gel: Add a comment * examples/duffing-equation-trajectories.gel: Add a comment
......
#include "gtkextratypebuiltins.h" #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 #define GTKEXTRA_ENABLE_BROKEN
#include "gtkextra.h" #include "gtkextra.h"
...@@ -447,5 +447,5 @@ gtk_plot_units_get_type (void) ...@@ -447,5 +447,5 @@ gtk_plot_units_get_type (void)
return etype; return etype;
} }
/* Generated data ends here */
/* 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__ #ifndef __GTKEXTRA_TYPE_BUILTINS_H__
#define __GTKEXTRA_TYPE_BUILTINS_H__ #define __GTKEXTRA_TYPE_BUILTINS_H__
...@@ -71,5 +71,5 @@ G_END_DECLS ...@@ -71,5 +71,5 @@ G_END_DECLS
#endif /* __GTKEXTRA_TYPE_BUILTINS_H__ */ #endif /* __GTKEXTRA_TYPE_BUILTINS_H__ */
/* Generated data ends here */
...@@ -5381,6 +5381,16 @@ If <varname>q</varname> is not prime results are bogus.</para> ...@@ -5381,6 +5381,16 @@ If <varname>q</varname> is not prime results are bogus.</para>
<sect1 id="genius-gel-function-list-matrix"> <sect1 id="genius-gel-function-list-matrix">
<title>Matrix Manipulation</title> <title>Matrix Manipulation</title>
<variablelist> <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> <varlistentry>
<term><anchor id="gel-function-ApplyOverMatrix"/>ApplyOverMatrix</term> <term><anchor id="gel-function-ApplyOverMatrix"/>ApplyOverMatrix</term>
<listitem> <listitem>
...@@ -5694,8 +5704,20 @@ functions make this check. Values can be any number including complex numbers.< ...@@ -5694,8 +5704,20 @@ functions make this check. Values can be any number including complex numbers.<
<term><anchor id="gel-function-MakeVector"/>MakeVector</term> <term><anchor id="gel-function-MakeVector"/>MakeVector</term>
<listitem> <listitem>
<synopsis>MakeVector (A)</synopsis> <synopsis>MakeVector (A)</synopsis>
<para>Alias: <function>MakeColumnVector</function></para>
<para>Make column vector out of matrix by putting columns above <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> </listitem>
</varlistentry> </varlistentry>
......
...@@ -80,6 +80,7 @@ char *fake = N_("Delete a row of a matrix"); ...@@ -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_("Get the dot product of two vectors (no conjugates)");
char *fake = N_("Zero out entries above the diagonal"); char *fake = N_("Zero out entries above the diagonal");
char *fake = N_("Make diagonal matrix from a vector"); 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_("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 product of all elements in a matrix");
char *fake = N_("Calculate the sum of all elements in a matrix"); char *fake = N_("Calculate the sum of all elements in a matrix");
......
...@@ -340,12 +340,31 @@ function CompoundMatrix(k,A) = ( ...@@ -340,12 +340,31 @@ function CompoundMatrix(k,A) = (
SetHelp("MakeVector", "matrix", "Make column vector out of matrix by putting columns above each other") SetHelp("MakeVector", "matrix", "Make column vector out of matrix by putting columns above each other")
function MakeVector(A) = ( function MakeVector(A) = (
if IsNull(A) then return null if IsNull(A) then
return null
else if not IsMatrix(A) then 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; r = null;
for k=1 to columns(A) do ( for k=1 to columns(A) do (
r = [r;A@(,k)] r = [r;A@(,k)]
); );
r 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
)
...@@ -6,7 +6,7 @@ SetHelp ("Union", "sets", "Returns a set theoretic union of X and Y (X and Y are ...@@ -6,7 +6,7 @@ SetHelp ("Union", "sets", "Returns a set theoretic union of X and Y (X and Y are
function Union(X,Y) = function Union(X,Y) =
( (
for x in X do 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 Y
) )
...@@ -15,6 +15,6 @@ function MakeSet(X) = ...@@ -15,6 +15,6 @@ function MakeSet(X) =
( (
S = null; S = null;
for x in X do 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 S
) )
/* GENIUS Calculator /* GENIUS Calculator
* Copyright (C) 1997-2017 Jiri (George) Lebl * Copyright (C) 1997-2018 Jiri (George) Lebl
* *
* Author: Jiri (George) Lebl * Author: Jiri (George) Lebl
* *
...@@ -2541,6 +2541,7 @@ exp_op(GelCtx *ctx, GelETree * * a, gboolean *exception) ...@@ -2541,6 +2541,7 @@ exp_op(GelCtx *ctx, GelETree * * a, gboolean *exception)
return NULL; return NULL;
} }
if G_UNLIKELY (_internal_exp_function == NULL) { if G_UNLIKELY (_internal_exp_function == NULL) {
/* FIXME: this really is not good*/
_internal_exp_function = d_makeufunc(d_intern("<internal>exp"), _internal_exp_function = d_makeufunc(d_intern("<internal>exp"),
gel_parseexp gel_parseexp
("s = float(x^0); " ("s = float(x^0); "
...@@ -3709,6 +3710,55 @@ SetMatrixSize_op(GelCtx *ctx, GelETree * * a, gboolean *exception) ...@@ -3709,6 +3710,55 @@ SetMatrixSize_op(GelCtx *ctx, GelETree * * a, gboolean *exception)
return n; 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 * static GelETree *
IndexComplement_op(GelCtx *ctx, GelETree * * a, gboolean *exception) IndexComplement_op(GelCtx *ctx, GelETree * * a, gboolean *exception)
{ {
...@@ -7215,13 +7265,15 @@ gel_funclib_addall(void) ...@@ -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)")); VFUNC (ones, 2, "rows,columns", "matrix", N_("Make an matrix of all ones (or a row vector)"));
f->no_mod_all_args = 1; 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 (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 (columns, 1, "M", "matrix", N_("Get the number of columns of a matrix"));
FUNC (IsMatrixSquare, 1, "M", "matrix", N_("Is a matrix square")); 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 (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 (IsUpperTriangular, 1, "M", "matrix", N_("Is a matrix upper triangular"));
FUNC (IsLowerTriangular, 1, "v", "matrix", N_("Is a matrix lower triangular")); FUNC (IsLowerTriangular, 1, "M", "matrix", N_("Is a matrix lower triangular"));
FUNC (IsDiagonal, 1, "v", "matrix", N_("Is a matrix diagonal")); 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 (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")); FUNC (ref, 1, "M", "linear_algebra", N_("Get the row echelon form of a matrix"));
...@@ -7289,7 +7341,7 @@ gel_funclib_addall(void) ...@@ -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")); 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 (PolyDerivative, 1, "p", "polynomial", N_("Take polynomial (as vector) derivative"));
FUNC (Poly2ndDerivative, 1, "p", "polynomial", N_("Take second 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")); 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)")); 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)")); FUNC (PolyToFunction, 1, "p", "polynomial", N_("Make function out of a polynomial (as vector)"));
......
...@@ -182,6 +182,19 @@ check_argument_matrix_or_null (GelETree **a, int argnum, const char *funcname) ...@@ -182,6 +182,19 @@ check_argument_matrix_or_null (GelETree **a, int argnum, const char *funcname)
return TRUE; 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 static inline gboolean
check_argument_value_only_matrix (GelETree **a, int argnum, const char *funcname) check_argument_value_only_matrix (GelETree **a, int argnum, const char *funcname)
{ {
......
...@@ -866,6 +866,10 @@ if 1i then 1 else 0 1 ...@@ -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 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 IntegerFromBoolean(1i) 1
MakeVector([1,2;3,4]) [1;3;2;4] 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) MakeVector(null)+1 ((null)+1)
Gamma(5) 24.0 Gamma(5) 24.0
ToeplitzMatrix ([1,2i,3i]) [1,2i,3i;-2i,1,2i;-3i,-2i,1] 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] ...@@ -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] DoubleFactorial([0,1,2,3,4,5,6]) [1,1,2,3,8,15,48]
StripZeroColumns(zeros(4,4))+0 ((null)+0) StripZeroColumns(zeros(4,4))+0 ((null)+0)
Image(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 "nullspacetest.gel" true
load "longtest.gel" true load "longtest.gel" true
load "testprec.gel" true load "testprec.gel" true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment