grand now works with complexes, polynomials, booleans, integers, strings and sparses.
Also, it can take row vectors, matrices and hypermatrices of these types.
grand is overloaded to call the new internal function %grand_perm.
Change-Id: Ifa2cb794bec98f83a54bb2fe121bf3f74286b30a
* Bug #5016 fixed - condestsp could return different results when repeated calls were performed.
+* Bug #6689 & #6690 fixed - grand now works with complexes, polynomials, booleans, integers, sparses and strings,
+ and can take row vectors, matrices and hypermatrices of thoses types.
+
* Bug #7047 fixed - milk_drop is now obsolete. It will be removed in Scilab 5.5.1, but will be kept as a demo.
* Bug #7570 fixed - The switch criterion on x and y is now explicited in beta() help page.
// you should have received as part of this distribution. The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+
+//Load functions libraries
+// =============================================================================
+ load('SCI/modules/randlib/macros/lib');
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
- * are also available at
+ * are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
-->
<term>Y</term>
<listitem>
<para>
- depending on the input, a matrix or hypermatrix of doubles, with random entries.
+ depending on the input, a matrix or hypermatrix, with random entries.
</para>
</listitem>
</varlistentry>
<programlisting role="no-scilab-exec"><![CDATA[
S = grand("getgen")
grand("setgen", gen)
-
+
S = grand("getsd")
grand("setsd", S)
-
+
grand("setcgn", G)
S = grand("getcgn")
-
+
grand("initgn", I)
-
+
grand("setall", s1, s2, s3, s4)
-
+
grand("advnst", K)
]]></programlisting>
<para>
<varlistentry>
<term>multivariate gaussian (multivariate normal)</term>
<listitem>
- <para>
+ <para>
<code>Y = grand(n, "mn", Mean, Cov)</code>
- generates multivariate normal random
+ generates multivariate normal random
variates; <varname>Mean</varname> must be a <literal>m x 1</literal>
column vector and <varname>Cov</varname> a <varname>m</varname>-by-<varname>m</varname>
symmetric positive definite matrix (<varname>Y</varname> is then
<listitem>
<para>
<code>Y = grand(n, "prm", vect)</code> generates
- <varname>n</varname> random permutations of the column
- vector (<literal>m x 1</literal>)
- <varname>vect</varname>.
+ <varname>n</varname> random permutations of <varname>vect</varname>.
+ <varname>vect</varname> can be a column vector of reals, or a vector, matrix or hypermatrix of
+ complexes, integers, booleans, polynomials or strings; full or sparse.
Due to the stack structure, <varname>vect</varname> should not and cannot be a row vector.
This feature covers Matlab's <code>randperm()</code> function,
because <code>randperm(n)</code> is equivalent to <code>grand(1,'prm',(1:n)')</code>
<variablelist>
<varlistentry>
<term>I = -1</term>
- <listitem>
- <para>sets the state to its initial seed</para>
+ <listitem>
+ <para>sets the state to its initial seed</para>
</listitem>
</varlistentry>
<varlistentry>
<term>I = 0</term>
<listitem>
- <para>sets the state to its last (previous) seed (i.e. to the beginning of the current segment)</para>
+ <para>sets the state to its last (previous) seed (i.e. to the beginning of the current segment)</para>
</listitem>
</varlistentry>
<varlistentry>
<term>I = 1</term>
- <listitem>
- <para>
- sets the state to a new seed <literal>W</literal> values from its last seed (i.e. to the beginning
- of the next segment) and resets the current segment parameters.
+ <listitem>
+ <para>
+ sets the state to a new seed <literal>W</literal> values from its last seed (i.e. to the beginning
+ of the next segment) and resets the current segment parameters.
</para>
</listitem>
</varlistentry>
In the following example, we generate random numbers from various distributions and
plot the associated histograms.
</para>
- <programlisting role="example"><![CDATA[
-// Returns a 400-by-800 matrix of random doubles,
+ <programlisting role="example"><![CDATA[
+// Returns a 400-by-800 matrix of random doubles,
// with normal distribution and average 0 and standard deviation 1.
R = grand(400,800,"nor",0,1);
scf();
xtitle("Normal random numbers from grand","X","Frequency");
</scilab:image>
- <programlisting role="example"><![CDATA[
-// Returns a 400-by-800 matrix of random doubles,
+ <programlisting role="example"><![CDATA[
+// Returns a 400-by-800 matrix of random doubles,
// uniform in [0,1).
R = grand(400,800,"def");
scf();
xtitle("Uniform random numbers from grand","X","Frequency");
</scilab:image>
- <programlisting role="example"><![CDATA[
-// Returns a 400-by-800 matrix of random doubles,
+ <programlisting role="example"><![CDATA[
+// Returns a 400-by-800 matrix of random doubles,
// with Poisson distribution and average equal to 5.
R = grand(400,800,"poi",5);
scf();
In the following example, we generate random numbers from the exponential distribution and
then compare the empirical with the theoretical distribution.
</para>
- <programlisting role="example"><![CDATA[
+ <programlisting role="example"><![CDATA[
lambda=1.6;
N=100000;
X = grand(1,N,"exp",lambda);
In the following example, we generate random numbers from the gamma distribution and
then compare the empirical with the theoretical distribution.
</para>
- <programlisting role="example"><![CDATA[
+ <programlisting role="example"><![CDATA[
N=10000;
A=10;
B=4;
-R=grand(1,N,"gam",A,B);
+R=grand(1,N,"gam",A,B);
XS=gsort(R,"g","i")';
PS=(1:N)'/N;
P=cdfgam("PQ",XS,A*ones(XS),B*ones(XS));
N=10000;
A=10;
B=4;
- R=grand(1,N,"gam",A,B);
+ R=grand(1,N,"gam",A,B);
XS=gsort(R,"g","i")';
PS=(1:N)'/N;
P=cdfgam("PQ",XS,A*ones(XS),B*ones(XS));
<para>
In the following example, we generate 10 random integers in the [1,365] interval.
</para>
- <programlisting role="example"><![CDATA[
+ <programlisting role="example"><![CDATA[
grand(10,1,"uin",1,365)
]]></programlisting>
<para>
In the following example, we generate 12 permutations of the [1,2,...,7] set.
The 12 permutations are stored column-by-column.
</para>
- <programlisting role="example"><![CDATA[
+ <programlisting role="example"><![CDATA[
grand(12,"prm",(1:7)')
]]></programlisting>
<para>
hypermatrix of random numbers from the "normal" distribution and plot the associated histograms.
Showing the first and last layers.
</para>
- <programlisting role="example"><![CDATA[
+ <programlisting role="example"><![CDATA[
// Returns a 400-by-800-by-10 hypermatrix of random doubles,
// with normal distribution, average 0 and standard deviation 1.
// Showing the first and last layers.
<para>
<literal>Y = grand(n, "prm", vect)</literal> génère <literal>n</literal> permutations aléatoire du
vecteur colonne (<literal>m x 1</literal>) <literal>vect</literal>.
+ <varname>vect</varname> peut être un scalaire, vecteur, matrices ou hypermatrice de réels,
+ complexes, entiers, booléens, polynômes ou chaînes de caractères; plein ou creux.
A cause de la structure de la pile, <varname>vect</varname> ne doit pas et ne peut pas être un vecteur ligne.
Cette fonctionnalité couvre la fonction Matlab <code>randperm()</code>,
car <code>randperm(n)</code> est équivalent à <code>grand(1,'prm',(1:n)')</code>
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab\r
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier\r
+//\r
+// This file must be used under the terms of the CeCILL.\r
+// This source file is licensed as described in the file COPYING, which\r
+// you should have received as part of this distribution. The terms\r
+// are also available at\r
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt\r
+\r
+function y = %b_grand(n, prm, vect)\r
+ // Overload to use grand on boolean input\r
+\r
+ y = %grand_perm(floor(n), vect);\r
+\r
+endfunction\r
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab\r
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier\r
+//\r
+// This file must be used under the terms of the CeCILL.\r
+// This source file is licensed as described in the file COPYING, which\r
+// you should have received as part of this distribution. The terms\r
+// are also available at\r
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt\r
+\r
+function y = %c_grand(n, prm, vect)\r
+ // Overload to use grand on string input\r
+\r
+ y = %grand_perm(floor(n), vect);\r
+\r
+endfunction\r
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab\r
+// Copyright (C) 2013 - Samuel Gougeon <sgougeon@free.fr>\r
+//\r
+// This file must be used under the terms of the CeCILL.\r
+// This source file is licensed as described in the file COPYING, which\r
+// you should have received as part of this distribution. The terms\r
+// are also available at\r
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt\r
+\r
+function rep = %grand_perm(N, V)\r
+ // Returns N random permutations of V's components.\r
+ // V may be a scalar, a row or column vector, a matrix, or an hypermatrix.\r
+ // All data types are supported: Integers, reals, complexes, strings, polynomes..\r
+ // if V is a row, permutations are returned as N rows\r
+ // if V is a column, permutations are returned as N columns\r
+ // if V is a matrix or a true hypermatrix, permutations are returned\r
+ // through an additional dimension of size N.\r
+\r
+ // Author : Samuel Gougeon - 2010\r
+ // Version: 1.2\r
+ // Software: Scilab and related\r
+ //\r
+ // Tests:\r
+ // with a row of reals:\r
+ // m = grand(1, 6, "uin", 0, 10)\r
+ // grand_perm(3, m)\r
+ // // with a column of reals:\r
+ // grand_perm(4, m')\r
+ // // with a matrix of reals:\r
+ // M = grand(2, 5, "uin", 0, 10)\r
+ // grand_perm(3, M)\r
+ // // with strings:\r
+ // grand_perm(5, ["a" "b" "c" "d"])\r
+ // // with polynomes:\r
+ // p = (m'-%z)^2\r
+ // grand_perm(3, p)\r
+\r
+ s = size(V);\r
+ L = prod(s);\r
+\r
+ if L == 1 then\r
+ rep = V(ones(1, N));\r
+ else\r
+ //[tmp, k] = gsort(rand(L, N), "r", "i");\r
+ k = grand(N, "prm", (1:L).'); // Faster for large L\r
+ if length(s) < 3 then\r
+ if s(1) == 1 then\r
+ rep = matrix(V(k), [s(2) N]).';\r
+ elseif s(2) == 1 then\r
+ rep = matrix(V(k),[s(1) N]);\r
+ else\r
+ rep = matrix(V(k), [s N]);\r
+ end\r
+ else\r
+ rep = matrix(V(k), [s N]); end\r
+ end\r
+\r
+endfunction\r
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab\r
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier\r
+//\r
+// This file must be used under the terms of the CeCILL.\r
+// This source file is licensed as described in the file COPYING, which\r
+// you should have received as part of this distribution. The terms\r
+// are also available at\r
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt\r
+\r
+function y = %hm_grand(n, prm, vect)\r
+ // Overload to use grand on hypermatrix input\r
+\r
+ y = %grand_perm(floor(n), vect);\r
+\r
+endfunction\r
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab\r
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier\r
+//\r
+// This file must be used under the terms of the CeCILL.\r
+// This source file is licensed as described in the file COPYING, which\r
+// you should have received as part of this distribution. The terms\r
+// are also available at\r
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt\r
+\r
+function y = %i_grand(n, prm, vect)\r
+ // Overload to use grand on integer input\r
+\r
+ y = %grand_perm(floor(n), vect);\r
+\r
+endfunction\r
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab\r
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier\r
+//\r
+// This file must be used under the terms of the CeCILL.\r
+// This source file is licensed as described in the file COPYING, which\r
+// you should have received as part of this distribution. The terms\r
+// are also available at\r
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt\r
+\r
+function y = %p_grand(n, prm, vect)\r
+ // Overload to use grand on polynomial input\r
+\r
+ y = %grand_perm(floor(n), vect);\r
+\r
+endfunction\r
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab\r
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier\r
+//\r
+// This file must be used under the terms of the CeCILL.\r
+// This source file is licensed as described in the file COPYING, which\r
+// you should have received as part of this distribution. The terms\r
+// are also available at\r
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt\r
+\r
+function y = %s_grand(n, prm, vect)\r
+ // Overload to use grand on complex input\r
+\r
+ y = %grand_perm(floor(n), vect);\r
+\r
+endfunction\r
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab\r
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier\r
+//\r
+// This file must be used under the terms of the CeCILL.\r
+// This source file is licensed as described in the file COPYING, which\r
+// you should have received as part of this distribution. The terms\r
+// are also available at\r
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt\r
+\r
+function y = %sp_grand(n, prm, vect)\r
+ // Overload to use grand on sparse input\r
+\r
+ y = %grand_perm(floor(n), vect);\r
+\r
+endfunction\r
exec(SCI+"/modules/functions/scripts/buildmacros/loadgenlib.sce");
end
-//genlib("randliblib","SCI/modules/randlib/macros",%f,%t);
+genlib("randliblib","SCI/modules/randlib/macros",%f,%t);
;
;Source: modules\{#RANDLIB}\includes\*.h; DestDir: {app}\modules\{#RANDLIB}\includes; Components: {#COMPN_SCILAB}
;
-;Source: modules\{#RANDLIB}\macros\lib; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
-;Source: modules\{#RANDLIB}\macros\names; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
-;Source: modules\{#RANDLIB}\macros\*.bin; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
-;Source: modules\{#RANDLIB}\macros\*.sci; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
+Source: modules\{#RANDLIB}\macros\lib; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
+Source: modules\{#RANDLIB}\macros\names; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
+Source: modules\{#RANDLIB}\macros\*.bin; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
+Source: modules\{#RANDLIB}\macros\*.sci; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
Source: modules\{#RANDLIB}\macros\buildmacros.sce; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
Source: modules\{#RANDLIB}\macros\buildmacros.bat; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
Source: modules\{#RANDLIB}\macros\cleanmacros.bat; DestDir: {app}\modules\{#RANDLIB}\macros; Flags: recursesubdirs; Components: {#COMPN_SCILAB}
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+* Copyright (C) 2013 - Scilab Enterprises - Paul Bignier: added support for complexes, polynomials,
+* integers, booleans, strings and sparses.
* Copyright (C) ENPC
*
* This file must be used under the terms of the CeCILL.
}
else if ( strcmp(cstk(ls), "prm") == 0)
{
- int nn;
+ int nn, Cmplx, *header;
if ( suite != 3 || ResL * ResC != 1)
{
Scierror(999, _("%s: Wrong value for input argument: Number of random simulation expected.\n"), fname);
Scierror(999, _("Missing vect for random permutation\n"));
return 0;
}
- GetRhsVar(suite, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &la);
- if ( n1 != 1)
+ switch (GetType(suite))
{
- Scierror(999, _("%s: Wrong type for input argument: Column vector expected.\n"), fname);
- return 0;
+ case 1:
+ header = (int *)GetData(suite);
+ Cmplx = header[3];
+ if (Cmplx == 0)
+ {
+ GetRhsVar(suite, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &la);
+ if ( n1 != 1)
+ {
+ OverLoad(suite);
+ return 0;
+ }
+ break;
+ }
+ else
+ {
+ OverLoad(suite); // Complex, call %s_grand
+ return 0;
+ break;
+ }
+ case 2: // Polynomial, call %p_grand
+ case 4: // Boolean, call %b_grand
+ case 5: // Sparse, call %sp_grand
+ case 8: // Integer, call %i_grand
+ case 10: // String, call %c_grand
+ case 17: // String, call %c_grand
+ OverLoad(suite);
+ return 0;
+ break;
+ default:
+ Scierror(999, _("%s: Wrong type for input argument: Matrix (full or sparse) or Hypermatrix of Reals, Complexes, Integers, Booleans, Strings or Polynomials expected.\n"), fname);
+ return 0;
}
CreateVar(suite + 1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &nn, &lr);
for ( i = 0 ; i < nn ; i++)
--- /dev/null
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier
+//
+// This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+//
+// Set the seed to always get the same random numbers
+rand("seed", 0);
+grand("setsd", 0);
+//
+// 1. Permute some column vectors, and check that the output is basically correct.
+//
+// With Complexes.
+//
+X = (2:10)';
+P = grand(5,"prm",X*%i);
+assert_checkequal ( isreal(P) , %f );
+assert_checkequal ( typeof(P) , "constant" );
+assert_checkequal ( size(P) , [9 5] );
+assert_checkequal ( gsort(P,"r","i") , X*%i*ones(1,5) );
+//
+// With Integers.
+//
+P = grand(5,"prm",int8(X));
+assert_checkequal ( typeof(P) , "int8" );
+assert_checkequal ( size(P) , [9 5] );
+assert_checkequal ( gsort(P,"r","i") , int8(X)*ones(1,5) );
+//
+P = grand(5,"prm",int16(X));
+assert_checkequal ( typeof(P) , "int16" );
+assert_checkequal ( size(P) , [9 5] );
+assert_checkequal ( gsort(P,"r","i") , int16(X)*ones(1,5) );
+//
+P = grand(5,"prm",int32(X));
+assert_checkequal ( typeof(P) , "int32" );
+assert_checkequal ( size(P) , [9 5] );
+assert_checkequal ( gsort(P,"r","i") , int32(X)*ones(1,5) );
+//
+// With Strings.
+//
+X = string(X);
+P = grand(1,"prm",X);
+assert_checkequal ( typeof(P) , "string" );
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( P , ["3" "9" "5" "7" "10" "6" "8" "2" "4"]');
+//
+// With Booleans.
+//
+X = [%f %t %t %t %t %f]';
+P = grand(1,"prm",X);
+assert_checkequal ( typeof(P) , "boolean" );
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( P , [%t %f %t %t %f %t]' );
+//
+// With Polynomials.
+//
+s = poly(0,"s");
+X = [0 s 1+s 1+s^2 s^4]';
+P = grand(1,"prm",X);
+assert_checkequal ( typeof(P) , "polynomial" );
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( P , [1+s^2 1+s s s^4 0]' );
+//
+// With Sparses.
+//
+X = sparse([1 2 3 0 0 0 0 5 6])';
+P = grand(1,"prm",X);
+assert_checkequal ( typeof(P) , "sparse" );
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( P , sparse([0 0 0 3 5 6 2 1 0])' );
+//
+// 2. With row vectors / matrices.
+//
+X = 2:10;
+P = grand(1,"prm",X);
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( gsort(P,"g","i") , X );
+X(2, :) = 12:20;
+P = grand(2,"prm",X*%i);
+refP = [
+13 9 20 3 10 2 4 5 15
+19 14 6 18 12 16 7 8 17 ];
+refP(:, :, 2) = [
+14 7 8 9 13 10 15 19 4
+12 5 2 17 6 16 18 3 20 ];
+assert_checkequal ( size(P) , [size(X) 2] );
+assert_checkequal ( P, refP*%i );
+//
+// With Integers.
+//
+X_int = int32(X);
+P = grand(2,"prm",X_int);
+refP = int32([
+5 17 20 7 15 10 2 13 4
+3 16 8 12 19 9 18 14 6 ]);
+refP(:, :, 2) = int32([
+16 13 6 2 20 19 18 14 7
+9 12 3 8 15 17 10 4 5 ]);
+assert_checkequal ( size(P) , [size(X_int) 2] );
+assert_checkequal ( P, refP );
+//
+// With Strings.
+//
+X_str = string(X);
+P = grand(2,"prm",X_str);
+refP = string([
+19 7 14 10 18 17 5 3 4
+9 15 2 20 13 12 6 8 16 ]);
+refP(:, :, 2) = string([
+13 20 9 6 8 14 3 4 16
+15 5 19 2 18 12 7 10 17 ]);
+assert_checkequal ( size(P) , [size(X_str) 2] );
+assert_checkequal ( P, refP );
+//
+// With Hypermatrices.
+//
+X(:, :, 2) = X+20;
+P = grand(2,"prm",X);
+assert_checkequal ( size(P) , [size(X) 2] );
+assert_checkequal ( members(P, 2:40), ones(P) );
+//
+// Of Complexes.
+//
+P = grand(2,"prm",X*%i);
+assert_checkequal ( size(P) , [size(X) 2] );
+assert_checkequal ( members(P, (2:40)*%i), ones(P) );
+//
+// Of Integers.
+//
+P = grand(2,"prm",int16(X));
+assert_checkequal ( size(P) , [size(X) 2] );
+assert_checkequal ( members(P, int16(2:40)), ones(P) );
+//
+// Of Strings.
+//
+Xs = string(X);
+Ps = grand(2,"prm",Xs);
+assert_checkequal ( size(Ps) , [size(Xs) 2] );
+assert_checkequal ( members(Ps, string(2:40)), ones(Ps) );
+//
+// Of Booleans.
+//
+Xb = floor(10*rand(2, 9, 2));
+Xb = Xb<5;
+Pb = grand(2,"prm",Xb);
+refPb = gsort(Pb+0,"g","i");
+assert_checkequal ( size(Pb) , [size(Xb) 2] );
+assert_checkequal ( refPb(1:36), zeros(36, 1) );
+assert_checkequal ( refPb(37:72), ones(36, 1) );
+//
+// Of Ploynomials.
+//
+Xp = matrix(X(:)+s, 2, 9, 2);
+Pp = grand(2,"prm",Xp);
+refPp = matrix(Pp(:)-s, 2, 9, 2, 2);
+refPp = gsort(coeff(refPp(:)),"g","i");
+assert_checkequal ( size(Pp) , [size(Xp) 2] );
+assert_checkequal ( members(2:40, refPp), 2*([ones(1,9) 0 ones(1,9) 0 ones(1,9) 0 ones(1,9)]) );
+//
+// Sparse hypermatrices do not exist yet.
+//
+//
+// Bug #6689
+//
+X = [%i 1-%i 2+3*%i].';
+P = grand(4, "prm", X);
+refP = [
+2+3*%i %i 2+3*%i 1-%i
+%i 2+3*%i %i %i
+1-%i 1-%i 1-%i 2+3*%i ];
+assert_checkequal ( typeof(P) , "constant" );
+assert_checkequal ( size(P) , [3 4] );
+assert_checkequal ( P , refP );
+refMsg = msprintf(_("%s: Wrong type for input argument: Matrix (full or sparse) or Hypermatrix of Reals, Complexes, Integers, Booleans, Strings or Polynomials expected.\n"), "grand");
+assert_checkerror("grand(2, ""prm"", list())", refMsg);
--- /dev/null
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier
+//
+// This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+//
+// Set the seed to always get the same random numbers
+rand("seed", 0);
+grand("setsd", 0);
+
+//
+// 1. Permute some column vectors, and check that the output is basically correct.
+//
+// With Complexes.
+//
+X = (2:10)';
+P = grand(5,"prm",X*%i);
+assert_checkequal ( isreal(P) , %f );
+assert_checkequal ( typeof(P) , "constant" );
+assert_checkequal ( size(P) , [9 5] );
+assert_checkequal ( gsort(P,"r","i") , X*%i*ones(1,5) );
+//
+// With Integers.
+//
+P = grand(5,"prm",int8(X));
+assert_checkequal ( typeof(P) , "int8" );
+assert_checkequal ( size(P) , [9 5] );
+assert_checkequal ( gsort(P,"r","i") , int8(X)*ones(1,5) );
+//
+P = grand(5,"prm",int16(X));
+assert_checkequal ( typeof(P) , "int16" );
+assert_checkequal ( size(P) , [9 5] );
+assert_checkequal ( gsort(P,"r","i") , int16(X)*ones(1,5) );
+//
+P = grand(5,"prm",int32(X));
+assert_checkequal ( typeof(P) , "int32" );
+assert_checkequal ( size(P) , [9 5] );
+assert_checkequal ( gsort(P,"r","i") , int32(X)*ones(1,5) );
+//
+// With Strings.
+//
+X = string(X);
+P = grand(1,"prm",X);
+assert_checkequal ( typeof(P) , "string" );
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( P , ["3" "9" "5" "7" "10" "6" "8" "2" "4"]');
+//
+// With Booleans.
+//
+X = [%f %t %t %t %t %f]';
+P = grand(1,"prm",X);
+assert_checkequal ( typeof(P) , "boolean" );
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( P , [%t %f %t %t %f %t]' );
+//
+// With Polynomials.
+//
+s = poly(0,"s");
+X = [0 s 1+s 1+s^2 s^4]';
+P = grand(1,"prm",X);
+assert_checkequal ( typeof(P) , "polynomial" );
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( P , [1+s^2 1+s s s^4 0]' );
+//
+// With Sparses.
+//
+X = sparse([1 2 3 0 0 0 0 5 6])';
+P = grand(1,"prm",X);
+assert_checkequal ( typeof(P) , "sparse" );
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( P , sparse([0 0 0 3 5 6 2 1 0])' );
+
+//
+// 2. With row vectors / matrices.
+//
+X = 2:10;
+P = grand(1,"prm",X);
+assert_checkequal ( size(P) , size(X) );
+assert_checkequal ( gsort(P,"g","i") , X );
+X(2, :) = 12:20;
+P = grand(2,"prm",X*%i);
+refP = [
+13 9 20 3 10 2 4 5 15
+19 14 6 18 12 16 7 8 17 ];
+refP(:, :, 2) = [
+14 7 8 9 13 10 15 19 4
+12 5 2 17 6 16 18 3 20 ];
+assert_checkequal ( size(P) , [size(X) 2] );
+assert_checkequal ( P, refP*%i );
+//
+// With Integers.
+//
+X_int = int32(X);
+P = grand(2,"prm",X_int);
+refP = int32([
+5 17 20 7 15 10 2 13 4
+3 16 8 12 19 9 18 14 6 ]);
+refP(:, :, 2) = int32([
+16 13 6 2 20 19 18 14 7
+9 12 3 8 15 17 10 4 5 ]);
+assert_checkequal ( size(P) , [size(X_int) 2] );
+assert_checkequal ( P, refP );
+//
+// With Strings.
+//
+X_str = string(X);
+P = grand(2,"prm",X_str);
+refP = string([
+19 7 14 10 18 17 5 3 4
+9 15 2 20 13 12 6 8 16 ]);
+refP(:, :, 2) = string([
+13 20 9 6 8 14 3 4 16
+15 5 19 2 18 12 7 10 17 ]);
+assert_checkequal ( size(P) , [size(X_str) 2] );
+assert_checkequal ( P, refP );
+//
+// With Hypermatrices.
+//
+X(:, :, 2) = X+20;
+P = grand(2,"prm",X);
+assert_checkequal ( size(P) , [size(X) 2] );
+assert_checkequal ( members(P, 2:40), ones(P) );
+//
+// Of Complexes.
+//
+P = grand(2,"prm",X*%i);
+assert_checkequal ( size(P) , [size(X) 2] );
+assert_checkequal ( members(P, (2:40)*%i), ones(P) );
+//
+// Of Integers.
+//
+P = grand(2,"prm",int16(X));
+assert_checkequal ( size(P) , [size(X) 2] );
+assert_checkequal ( members(P, int16(2:40)), ones(P) );
+//
+// Of Strings.
+//
+Xs = string(X);
+Ps = grand(2,"prm",Xs);
+assert_checkequal ( size(Ps) , [size(Xs) 2] );
+assert_checkequal ( members(Ps, string(2:40)), ones(Ps) );
+//
+// Of Booleans.
+//
+Xb = floor(10*rand(2, 9, 2));
+Xb = Xb<5;
+Pb = grand(2,"prm",Xb);
+refPb = gsort(Pb+0,"g","i");
+assert_checkequal ( size(Pb) , [size(Xb) 2] );
+assert_checkequal ( refPb(1:36), zeros(36, 1) );
+assert_checkequal ( refPb(37:72), ones(36, 1) );
+//
+// Of Ploynomials.
+//
+Xp = matrix(X(:)+s, 2, 9, 2);
+Pp = grand(2,"prm",Xp);
+refPp = matrix(Pp(:)-s, 2, 9, 2, 2);
+refPp = gsort(coeff(refPp(:)),"g","i");
+assert_checkequal ( size(Pp) , [size(Xp) 2] );
+assert_checkequal ( members(2:40, refPp), 2*([ones(1,9) 0 ones(1,9) 0 ones(1,9) 0 ones(1,9)]) );
+//
+// Sparse hypermatrices do not exist yet.
+//
+
+//
+// Bug #6689
+//
+X = [%i 1-%i 2+3*%i].';
+P = grand(4, "prm", X);
+refP = [
+2+3*%i %i 2+3*%i 1-%i
+%i 2+3*%i %i %i
+1-%i 1-%i 1-%i 2+3*%i ];
+assert_checkequal ( typeof(P) , "constant" );
+assert_checkequal ( size(P) , [3 4] );
+assert_checkequal ( P , refP );
+
+refMsg = msprintf(_("%s: Wrong type for input argument: Matrix (full or sparse) or Hypermatrix of Reals, Complexes, Integers, Booleans, Strings or Polynomials expected.\n"), "grand");
+assert_checkerror("grand(2, ""prm"", list())", refMsg);