genius r664 - in trunk: . help/C src



Author: jirka
Date: Thu Jun  5 16:45:21 2008
New Revision: 664
URL: http://svn.gnome.org/viewvc/genius?rev=664&view=rev

Log:

Thu Jun 05 11:40:58 2008  Jiri (George) Lebl <jirka 5z com>

	* src/funclib.c: fix argument checking in MillerRabin.  Also
	  zeros, ones, rand, randint, I, SetMatrixSize now accept 0 for size
	  and return null as an empty matrix.  Also wait, IndexComplement
	  accept 0 and work accordingly.
	
	* help/C/gel-function-list.gel: update accordingly



Modified:
   trunk/ChangeLog
   trunk/help/C/gel-function-list.xml
   trunk/help/C/genius.txt
   trunk/src/funclib.c

Modified: trunk/help/C/gel-function-list.xml
==============================================================================
--- trunk/help/C/gel-function-list.xml	(original)
+++ trunk/help/C/gel-function-list.xml	Thu Jun  5 16:45:21 2008
@@ -358,7 +358,9 @@
          <term>wait</term>
          <listitem>
           <synopsis>wait (secs)</synopsis>
-          <para>Waits a specified number of seconds.</para>
+          <para>Waits a specified number of seconds.  <varname>secs</varname>
+must be nonnegative.  Zero is accepted and nothing happens in this case,
+except possibly user interface events are processed.</para>
          </listitem>
         </varlistentry>
 
@@ -1012,7 +1014,8 @@
          <listitem>
           <synopsis>rand (size...)</synopsis>
           <para>Generate random float in the range <literal>[0,1)</literal>.
-If size is given then a matrix (if two numbers are specified) or vector (if one number is specified) of the given size returned.</para>
+If size is given then a matrix (if two numbers are specified) or vector (if one
+number is specified) of the given size returned.</para>
          </listitem>
         </varlistentry>
 
@@ -2203,7 +2206,7 @@
          <listitem>
           <synopsis>I (n)</synopsis>
           <para>Aliases: <function>eye</function></para>
-          <para>Return an identity matrix of a given size, that is <varname>n</varname> by <varname>n</varname>.</para>
+          <para>Return an identity matrix of a given size, that is <varname>n</varname> by <varname>n</varname>.  If <varname>n</varname> is zero, returns null.</para>
           <para>
 	    See
 	    <ulink url="http://planetmath.org/encyclopedia/IdentityMatrix.html";>Planetmath</ulink> for more information.
@@ -2215,7 +2218,9 @@
          <term>IndexComplement</term>
          <listitem>
           <synopsis>IndexComplement (vec,msize)</synopsis>
-          <para>Return the index complement of a vector of indexes.</para>
+          <para>Return the index complement of a vector of indexes.  Everything is one based.  For example for vector <userinput>[2,3]</userinput> and size
+<userinput>5</userinput>, we return <userinput>[1,4,5]</userinput>.  If
+<varname>msize</varname> is 0, we always return null.</para>
          </listitem>
         </varlistentry>
 
@@ -2473,6 +2478,8 @@
           <para>Make new matrix of given size from old one.  That is, a new
 	  matrix will be returned to which the old one is copied.  Entries that
 	  don't fit are clipped and extra space is filled with zeros.
+	  if <varname>rows</varname> or <varname>columns</varname> are zero
+	  then null is returned.
 	  </para>
          </listitem>
         </varlistentry>
@@ -2548,7 +2555,7 @@
          <term>ones</term>
          <listitem>
           <synopsis>ones (rows,columns...)</synopsis>
-          <para>Make an matrix of all ones (or a row vector if only one argument is given).</para>
+          <para>Make an matrix of all ones (or a row vector if only one argument is given).  Returns null if either rows or columns are zero.</para>
          </listitem>
         </varlistentry>
 
@@ -2564,7 +2571,7 @@
          <term>zeros</term>
          <listitem>
           <synopsis>zeros (rows,columns...)</synopsis>
-          <para>Make a matrix of all zeros (or a row vector if only one argument is given).</para>
+          <para>Make a matrix of all zeros (or a row vector if only one argument is given).  Returns null if either rows or columns are zero.</para>
          </listitem>
         </varlistentry>
 

Modified: trunk/help/C/genius.txt
==============================================================================
--- trunk/help/C/genius.txt	(original)
+++ trunk/help/C/genius.txt	Thu Jun  5 16:45:21 2008
@@ -2152,7 +2152,9 @@
 
  wait (secs)
 
-           Waits a specified number of seconds.
+           Waits a specified number of seconds. secs must be nonnegative.
+           Zero is accepted and nothing happens in this case, except possibly
+           user interface events are processed.
 
    version
 
@@ -3471,7 +3473,8 @@
 
            Aliases: eye
 
-           Return an identity matrix of a given size, that is n by n.
+           Return an identity matrix of a given size, that is n by n. If n is
+           zero, returns null.
 
            See Planetmath for more information.
 
@@ -3479,7 +3482,9 @@
 
  IndexComplement (vec,msize)
 
-           Return the index complement of a vector of indexes.
+           Return the index complement of a vector of indexes. Everything is
+           one based. For example for vector [2,3] and size 5, we return
+           [1,4,5]. If msize is 0, we always return null.
 
    IsDiagonal
 
@@ -3671,7 +3676,8 @@
 
            Make new matrix of given size from old one. That is, a new matrix
            will be returned to which the old one is copied. Entries that
-           don't fit are clipped and extra space is filled with zeros.
+           don't fit are clipped and extra space is filled with zeros. if
+           rows or columns are zero then null is returned.
 
    SortVector
 
@@ -3730,7 +3736,7 @@
  ones (rows,columns...)
 
            Make an matrix of all ones (or a row vector if only one argument
-           is given).
+           is given). Returns null if either rows or columns are zero.
 
    rows
 
@@ -3743,7 +3749,7 @@
  zeros (rows,columns...)
 
            Make a matrix of all zeros (or a row vector if only one argument
-           is given).
+           is given). Returns null if either rows or columns are zero.
 
      ----------------------------------------------------------------------
 

Modified: trunk/src/funclib.c
==============================================================================
--- trunk/src/funclib.c	(original)
+++ trunk/src/funclib.c	Thu Jun  5 16:45:21 2008
@@ -109,8 +109,9 @@
 		error_num = 0;
 		return -1;
 	}
-	if G_UNLIKELY (i <= 0) {
-		gel_errorout (_("%s: argument can't be negative or 0"), funcname);
+	if G_UNLIKELY (i < 0) {
+		/* This should already have been checked */
+		/*gel_errorout (_("%s: argument can't be negative"), funcname);*/
 		return -1;
 	}
 	if G_UNLIKELY (i > G_MAXINT) {
@@ -371,7 +372,7 @@
 {
 	int secs;
 
-	if ( ! check_argument_integer (a, 0, "wait"))
+	if ( ! check_argument_nonnegative_integer (a, 0, "wait"))
 		return NULL;
 
 	secs = gel_get_nonnegative_integer (a[0]->val.value, "wait");
@@ -541,13 +542,16 @@
 		GelMatrix *m;
 		int size, i;
 
-		if ( ! check_argument_integer (a, 0, "rand"))
+		if ( ! check_argument_nonnegative_integer (a, 0, "rand"))
 			return NULL;
 
 		size = gel_get_nonnegative_integer (a[0]->val.value, "rand");
 		if (size < 0)
 			return NULL;
 
+		if (size == 0)
+			return gel_makenum_null ();
+
 		m = gel_matrix_new ();
 		gel_matrix_set_size (m, size, 1, FALSE /* padding */);
 		for (i = 0; i < size; i++) {
@@ -569,8 +573,8 @@
 		GelMatrix *m;
 		int sizex, sizey, i, j;
 
-		if ( ! check_argument_integer (a, 0, "rand") ||
-		     ! check_argument_integer (a, 1, "rand"))
+		if ( ! check_argument_nonnegative_integer (a, 0, "rand") ||
+		     ! check_argument_nonnegative_integer (a, 1, "rand"))
 			return NULL;
 
 		sizey = gel_get_nonnegative_integer (a[0]->val.value, "rand");
@@ -580,6 +584,9 @@
 		if (sizex < 0)
 			return NULL;
 
+		if (sizex == 0 || sizey == 0)
+			return gel_makenum_null ();
+
 		m = gel_matrix_new ();
 		gel_matrix_set_size (m, sizex, sizey, FALSE /* padding */);
 		for (i = 0; i < sizex; i++) {
@@ -637,13 +644,16 @@
 		int size, i;
 
 		if ( ! check_argument_integer (a, 0, "randint") ||
-		     ! check_argument_integer (a, 1, "randint"))
+		     ! check_argument_nonnegative_integer (a, 1, "randint"))
 			return NULL;
 
 		size = gel_get_nonnegative_integer (a[1]->val.value, "randint");
 		if (size < 0)
 			return NULL;
 
+		if (size == 0)
+			return gel_makenum_null ();
+
 		m = gel_matrix_new ();
 		gel_matrix_set_size (m, size, 1, FALSE /* padding */);
 		for (i = 0; i < size; i++) {
@@ -676,8 +686,8 @@
 		int sizex, sizey, i, j;
 
 		if ( ! check_argument_integer (a, 0, "randint") ||
-		     ! check_argument_integer (a, 1, "randint") ||
-		     ! check_argument_integer (a, 2, "randint"))
+		     ! check_argument_nonnegative_integer (a, 1, "randint") ||
+		     ! check_argument_nonnegative_integer (a, 2, "randint"))
 			return NULL;
 
 		sizey = gel_get_nonnegative_integer (a[1]->val.value, "randint");
@@ -687,6 +697,9 @@
 		if (sizex < 0)
 			return NULL;
 
+		if (sizex == 0 || sizey == 0)
+			return gel_makenum_null ();
+
 		m = gel_matrix_new ();
 		gel_matrix_set_size (m, sizex, sizey, FALSE /* padding */);
 		for (i = 0; i < sizex; i++) {
@@ -2668,13 +2681,16 @@
 	static int cached_size = -1;
 	static GelMatrixW *cached_m = NULL;
 
-	if G_UNLIKELY ( ! check_argument_integer (a, 0, "I"))
+	if G_UNLIKELY ( ! check_argument_nonnegative_integer (a, 0, "I"))
 		return NULL;
 
 	size = gel_get_nonnegative_integer (a[0]->val.value, "I");
 	if G_UNLIKELY (size < 0)
 		return NULL;
 
+	if (size == 0)
+		return gel_makenum_null ();
+
 	/*make us a new empty node*/
 	GET_NEW_NODE(n);
 	n->type = MATRIX_NODE;
@@ -2721,9 +2737,9 @@
 	GelMatrixW *m;
 	long rows, cols;
 
-	if G_UNLIKELY ( ! check_argument_integer (a, 0, "zeros") ||
+	if G_UNLIKELY ( ! check_argument_nonnegative_integer (a, 0, "zeros") ||
 			(a[1] != NULL &&
-			 ! check_argument_integer (a, 1, "zeros")))
+			 ! check_argument_nonnegative_integer (a, 1, "zeros")))
 		return NULL;
 
 	if G_UNLIKELY (a[1] != NULL && a[2] != NULL) {
@@ -2744,6 +2760,9 @@
 		rows = 1;
 	}
 
+	if (cols == 0 || rows == 0)
+		return gel_makenum_null ();
+
 	/*make us a new empty node*/
 	GET_NEW_NODE(n);
 	n->type = MATRIX_NODE;
@@ -2776,9 +2795,9 @@
 	long rows, cols;
 	int i, j;
 
-	if G_UNLIKELY ( ! check_argument_integer (a, 0, "ones") ||
+	if G_UNLIKELY ( ! check_argument_nonnegative_integer (a, 0, "ones") ||
 			(a[1] != NULL &&
-			 ! check_argument_integer (a, 1, "ones")))
+			 ! check_argument_nonnegative_integer (a, 1, "ones")))
 		return NULL;
 
 	if G_UNLIKELY (a[1] != NULL && a[2] != NULL) {
@@ -2799,6 +2818,9 @@
 		rows = 1;
 	}
 
+	if (cols == 0 || rows == 0)
+		return gel_makenum_null ();
+
 	/*make us a new empty node*/
 	GET_NEW_NODE(n);
 	n->type = MATRIX_NODE;
@@ -2951,9 +2973,9 @@
 
 	if G_UNLIKELY ( ! check_argument_matrix (a, 0, "SetMatrixSize"))
 		return NULL;
-	if G_UNLIKELY ( ! check_argument_number (a, 1, "SetMatrixSize"))
+	if G_UNLIKELY ( ! check_argument_nonnegative_integer (a, 1, "SetMatrixSize"))
 		return NULL;
-	if G_UNLIKELY ( ! check_argument_number (a, 2, "SetMatrixSize"))
+	if G_UNLIKELY ( ! check_argument_nonnegative_integer (a, 2, "SetMatrixSize"))
 		return NULL;
 
 	w = gel_get_nonnegative_integer (a[1]->val.value, "SetMatrixSize");
@@ -2963,6 +2985,9 @@
 	if G_UNLIKELY (h < 0)
 		return NULL;
 
+	if (w == 0 || h == 0)
+		return gel_makenum_null ();
+
 	n = gel_stealnode (a[0]);
 	gel_matrixw_set_size (n->mat.matrix, h, w);
 	return n;
@@ -2980,12 +3005,16 @@
 	int len;
 
 	if G_UNLIKELY ( ! check_argument_integer_or_matrix (a, 0, "IndexComplement") ||
-			! check_argument_integer (a, 1, "IndexComplement"))
+			! check_argument_nonnegative_integer (a, 1, "IndexComplement"))
 		return NULL;
 
 	len = gel_get_nonnegative_integer (a[1]->val.value, "IndexComplement");
 	if G_UNLIKELY (len < 0)
 		return NULL;
+
+	if G_UNLIKELY (len == 0)
+		return gel_makenum_null ();
+
 	if (a[0]->type == MATRIX_NODE) {
 		index = g_new0 (char, len);
 
@@ -3005,6 +3034,14 @@
 				g_free (index);
 				return NULL;
 			}
+
+			if G_UNLIKELY (elt == 0) {
+				gel_errorout (_("%s: argument can't be negative or 0"),
+					      "IndexComplement");
+				g_free (index);
+				return NULL;
+			}
+
 			elt--;
 			if G_UNLIKELY (elt >= len) {
 				gel_errorout (_("%s: vector argument has too large entries"), "IndexComplement");
@@ -3037,6 +3074,11 @@
 		int elt = gel_get_nonnegative_integer (a[0]->val.value, "IndexComplement");
 		if G_UNLIKELY (elt < 0)
 			return NULL;
+		if G_UNLIKELY (elt == 0) {
+			gel_errorout (_("%s: argument can't be negative or 0"),
+				      "IndexComplement");
+			return NULL;
+		}
 		if G_UNLIKELY (elt > len) {
 			gel_errorout (_("%s: vector argument has too large entries"), "IndexComplement");
 			return NULL;
@@ -3717,7 +3759,7 @@
 	if(a[0]->type==MATRIX_NODE)
 		return gel_apply_func_to_matrix(ctx,a[0],Prime_op,"prime", exception);
 
-	if G_UNLIKELY ( ! check_argument_integer (a, 0, "Prime"))
+	if G_UNLIKELY ( ! check_argument_positive_integer (a, 0, "Prime"))
 		return NULL;
 
 	num = gel_get_nonnegative_integer (a[0]->val.value, "Prime");
@@ -3869,6 +3911,8 @@
 		return NULL;
 
 	reps = gel_get_nonnegative_integer (a[1]->val.value, "MillerRabinTest");
+	if (reps < 0)
+		return NULL;
 
 	num = mpw_peek_real_mpz (a[0]->val.value);
 



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