[Mathlib-commitlog] mathlib/Source/MathLib/Functions/Matrix zeros.java, 1.8, 1.9 ones.java, 1.5, 1.
Status: Beta
Brought to you by:
st_mueller
|
From: Stefan M. <st_...@us...> - 2007-01-09 19:08:11
|
Update of /cvsroot/mathlib/mathlib/Source/MathLib/Functions/Matrix In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv1556/Source/MathLib/Functions/Matrix Modified Files: zeros.java ones.java Log Message: added support for ND-arrays Index: ones.java =================================================================== RCS file: /cvsroot/mathlib/mathlib/Source/MathLib/Functions/Matrix/ones.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ones.java 26 Dec 2006 12:25:07 -0000 1.5 --- ones.java 9 Jan 2007 19:08:06 -0000 1.6 *************** *** 15,53 **** { ! int columns; ! int rows; ! ! // at least one operands (e.g. ones(n) ) ! if (operands.length < 1) return null; ! if (operands[0] == null) return null; ! if (!(operands[0] instanceof NumberToken)) return null; ! ! rows = (int)(((NumberToken)operands[0]).getReValues())[0][0]; ! columns = rows; ! ! // two operands (e.g. ones(n,m) ) ! if (operands.length == 2) ! { ! if (operands[1] == null) return null; ! if (!(operands[1] instanceof NumberToken)) return null; ! columns = (int)((NumberToken)operands[1]).getReValues()[0][0]; ! } ! ! // only positive indices ! if ((rows <= 0) || (columns <= 0)) return null; ! // create matrix ! double[][] values = new double[rows][columns]; ! for (int yi=0; yi<=(rows-1) ; yi++) ! { ! for (int xi=0; xi<=(columns-1) ; xi++) ! { ! values[yi][xi] = 1.0; ! } ! } ! return new NumberToken(values); ! } // end eval } --- 15,62 ---- { ! // at least one operand ! if (getNArgIn(operands) < 1) ! throwMathLibException("ones: number of arguments <1 "); ! ! // number of arguments ! int n = getNArgIn(operands); ! ! // set up dimension array ! int[] dim = new int[n]; ! ! // only NumberTokens accepted ! // each token is one dimension ! for (int i=0; i<n; i++) ! { ! if (!(operands[i] instanceof NumberToken)) ! throwMathLibException("ones: arguments must be numbers"); ! ! // get requested dimension ! dim[i] = (int)((NumberToken)operands[i]).getValueRe(); ! if (dim[i]<0) ! throwMathLibException("ones: dimension <0"); ! } ! ! // special case for rand(k) -> rand(k,k) ! if (dim.length==1) ! { ! int d = dim[0]; ! dim = new int[]{d,d}; ! } ! ! // ceate array of correct size with dimensions "dim" ! NumberToken num = new NumberToken(dim, null, null); ! ! // create "1" value for all values of num ! for (int i=0; i< num.getNumberOfElements(); i++) ! { ! num.setValue(i, 1, 0); ! } ! ! return num; ! } // end eval } *************** *** 57,60 **** --- 66,70 ---- @SYNTAX ones(sizex, sizey) + ones(n,m,k,...) @DOC Returns a matrix of ones. Index: zeros.java =================================================================== RCS file: /cvsroot/mathlib/mathlib/Source/MathLib/Functions/Matrix/zeros.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** zeros.java 30 Dec 2006 18:07:10 -0000 1.8 --- zeros.java 9 Jan 2007 19:08:06 -0000 1.9 *************** *** 15,52 **** { - int columns; - int rows; - // at least one operands (e.g. zeros(n) ) if (getNArgIn(operands)<1) throwMathLibException("zeros: number of arguments < 1"); ! if (!(operands[0] instanceof NumberToken)) ! throwMathLibException("zeros: works only on numbers"); ! ! rows = (int)(((NumberToken)operands[0]).getReValues())[0][0]; ! columns = rows; ! ! // two operands (e.g. zeros(n,m) ) ! if (getNArgIn(operands)==2) ! { ! if (!(operands[1] instanceof NumberToken)) return null; ! ! columns = (int)((NumberToken)operands[1]).getReValues()[0][0]; ! } ! ! // only positive indices ! if ((rows <= 0) || (columns <= 0)) return null; ! ! // create matrix ! double[][] values = new double[rows][columns]; ! for (int yi=0; yi<=(rows-1) ; yi++) ! { ! for (int xi=0; xi<=(columns-1) ; xi++) ! { ! values[yi][xi] = 0.0; ! } ! } ! return new NumberToken(values); } // end eval --- 15,60 ---- { // at least one operands (e.g. zeros(n) ) if (getNArgIn(operands)<1) throwMathLibException("zeros: number of arguments < 1"); ! // number of arguments ! int n = getNArgIn(operands); ! ! // set up dimension array ! int[] dim = new int[n]; ! ! // only NumberTokens accepted ! // each token is one dimension ! for (int i=0; i<n; i++) ! { ! if (!(operands[i] instanceof NumberToken)) ! throwMathLibException("zeros: arguments must be numbers"); ! ! // get requested dimension ! dim[i] = (int)((NumberToken)operands[i]).getValueRe(); ! ! if (dim[i]<0) ! throwMathLibException("zeros: dimension <0"); ! } ! ! // special case for rand(k) -> rand(k,k) ! if (dim.length==1) ! { ! int d = dim[0]; ! dim = new int[]{d,d}; ! } ! ! // ceate array of correct size with dimensions "dim" ! NumberToken num = new NumberToken(dim, null, null); ! ! // create random value for all values of num ! for (int i=0; i< num.getNumberOfElements(); i++) ! { ! num.setValue(i, 0, 0); ! } ! ! return num; ! } // end eval *************** *** 57,61 **** matrix @SYNTAX ! answer = zeros(sizey, [sizex]) @DOC Returns a matrix filled with zeros. --- 65,70 ---- matrix @SYNTAX ! zeros(sizey, [sizex]) ! zeros(n,m,k,l...) @DOC Returns a matrix filled with zeros. *************** *** 63,68 **** @EXAMPLES <programlisting> ! ZEROS(2,2) = [0,0;0,0] ! ZEROS(2,3) = [0,0,0;0,0,0] </programlisting> @SEE --- 72,77 ---- @EXAMPLES <programlisting> ! zeros(2,2) = [0,0;0,0] ! zeros(2,3) = [0,0,0;0,0,0] </programlisting> @SEE |