r442 - in trunk/nuiton-matrix/src: main/java/org/nuiton/math/matrix test/java/org/nuiton/math/matrix
Author: bpoussin Date: 2012-08-31 00:52:26 +0200 (Fri, 31 Aug 2012) New Revision: 442 Url: http://nuiton.org/repositories/revision/nuiton-matrix/442 Log: - Implantation d'un Vector sur des fichiers mapper - Utilisation des tests pour chaque type Vector Added: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigMappedVector.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleBigMappedVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixFloatBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixFloatVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleBigMappedVectorTest1.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperFloatBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperFloatVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleBigMappedVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDFloatBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDFloatVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleBigMappedVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfFloatBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfFloatVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleBigMappedVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixFloatBigVectorTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixFloatVectorTest.java Removed: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/AbstractMatrixND.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/BasicMatrix.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigVector.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/FloatVector.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixFactory.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixHelper.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixNDImpl.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/Vector.java Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/AbstractMatrixND.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/AbstractMatrixND.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/AbstractMatrixND.java 2012-08-30 22:52:26 UTC (rev 442) @@ -441,7 +441,7 @@ // coordonnées sémantique @Override public boolean equals(Object o) { - return o instanceof MatrixND && equals((MatrixND) o); + return this == o || o instanceof MatrixND && equals((MatrixND) o); } public boolean equals(MatrixND mat) { Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/BasicMatrix.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/BasicMatrix.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/BasicMatrix.java 2012-08-30 22:52:26 UTC (rev 442) @@ -73,17 +73,46 @@ System.arraycopy(dimensions, 0, this.dimensions, 0, dimensions.length); // calcul du linearFactor - linearFactor = new int[dimensions.length]; - linearFactor[linearFactor.length - 1] = 1; - for (int i = linearFactor.length - 2; i >= 0; i--) { - linearFactor[i] = linearFactor[i + 1] * dimensions[i + 1]; - } + linearFactor = MatrixHelper.getLinearFactor(dimensions); // creation de la matrice lineaire - data = factory.createVector(linearFactor[0] * dimensions[0]); + int dataSize = MatrixHelper.getVectorSize(dimensions, linearFactor); + data = factory.createVector(dataSize); } /** + * Crée une nouvelle matrice ayant les dimensions demandées. + * + * @param factory factory + * @param dimensions dimensions + */ + public BasicMatrix(MatrixFactory factory, Vector data, int[] dimensions) { + this.factory = factory; + checkDim(dimensions); + + // copie des dimensions pour que personne à l'extérieur de l'objet + // ne puisse les modifiers par la suite + this.dimensions = new int[dimensions.length]; + System.arraycopy(dimensions, 0, this.dimensions, 0, dimensions.length); + + // calcul du linearFactor + linearFactor = MatrixHelper.getLinearFactor(dimensions); + + // utilisation du vector passe en argument, il doit avoir les bonnes dimensions + this.data = data; + int dataSize = MatrixHelper.getVectorSize(dimensions, linearFactor); + if (dataSize != data.size()) { + throw new IllegalArgumentException(String.format( + "Vector backend in argument don't have right size. Vector size %s but need %d", + data.size(), dataSize)); + } + } + + public Vector getInternalVector() { + return data; + } + + /** * Retourne la valeur la plus courrement rencontrer dans la matrice. si * plusieurs valeurs ont le même nombre d'occurence la plus petite valeur * est retourné. Copied: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigMappedVector.java (from rev 437, trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigVector.java) =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigMappedVector.java (rev 0) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigMappedVector.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,211 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.DoubleBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.util.Arrays; + +/** + * DoubleBigVector. + * + * Created: 30 aout 2012 11:56:36 CEST + * + * @author Benjamin POUSSIN <poussin@codelutin.com> + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class DoubleBigMappedVector implements Vector { // DoubleBigMappedVector + + static final public int DOUBLE_SIZE = 8; + + /** le fichier temporaire creer pour la matrice, a effacer lorsque la matrice n'est plus utiliser */ + protected File file; + protected int capacity; + protected DoubleBuffer data = null; + + @Override + protected void finalize() throws Throwable { + super.finalize(); + file.delete(); + } + + /** + * Create temp file, this file is deleted when you stop application or + * when this instance is no more used. + * @param capacity + * @throws IOException + */ + public DoubleBigMappedVector(int capacity) throws IOException { + this.capacity = capacity; + this.file = File.createTempFile("matrix", ".mapped"); + this.file.deleteOnExit(); + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + data = raf.getChannel() + .map(FileChannel.MapMode.READ_WRITE, 0, capacity * DOUBLE_SIZE) + .asDoubleBuffer(); + } + + /** + * Utilise une partie du fichier pour stocker les informations + * @param raf le fichier a utiliser + * @param offset l'endroit ou l'on doit faire le stockage + * @param capacity le nombre de double a y stocker + * @throws IOException + */ + public DoubleBigMappedVector(RandomAccessFile raf, int offset, int capacity) throws IOException { + this(raf.getChannel().map( + FileChannel.MapMode.READ_WRITE, offset, capacity*DOUBLE_SIZE).asDoubleBuffer(), capacity); + } + + public DoubleBigMappedVector(MappedByteBuffer bytes, int capacity) { + this(bytes.asDoubleBuffer(), capacity); + } + + public DoubleBigMappedVector(DoubleBuffer data, int capacity) { + this.capacity = capacity; + this.data = data; + } + + @Override + public int size() { + return capacity; + } + + @Deprecated + @Override + public double getMaxOccurence() { + return getMaxOccurrence(); + } + + @Override + public double getMaxOccurrence() { + // on se place au debut avant de faire la reecherche + data.position(0); + + double[] tmp = new double[capacity]; + data.get(tmp); + return MatrixHelper.maxOccurrence(tmp); + } + + @Override + public double getValue(int pos) { + return data.get(pos); + } + + @Override + public void setValue(int pos, double value) { + data.put(pos, value); + } + + @Override + public boolean equals(Object o) { + boolean result = this == o; + if (!result) { + if (o instanceof DoubleBigMappedVector) { + DoubleBigMappedVector other = (DoubleBigMappedVector) o; + // on se place au debut avant de faire la comparaison + other.data.position(0); + data.position(0); + + result = this.data.equals(other.data); + } else if (o instanceof Vector) { + Vector other = (Vector) o; + result = true; + for (int i = 0; i < size() && result; i++) { + result = getValue(i) == other.getValue(i); + } + } + } + return result; + } + + @Override + public boolean isImplementedPaste(Vector v) { + return v instanceof DoubleBigMappedVector; + } + + @Override + public boolean isImplementedAdd(Vector v) { + return v instanceof DoubleBigMappedVector; + } + + @Override + public boolean isImplementedMinus(Vector v) { + return v instanceof DoubleBigMappedVector; + } + + @Override + public boolean isImplementedMap() { + return true; + } + + @Override + public void paste(Vector src) { + DoubleBigMappedVector dbmSrc = (DoubleBigMappedVector) src; + // on se place au debut avant de faire la copie + dbmSrc.data.position(0); + data.position(0); + + data.put(dbmSrc.data); + } + + @Override + public void add(Vector v) { + DoubleBigMappedVector fbv = (DoubleBigMappedVector) v; + for (int i = 0; i < capacity; i++) { + double value = data.get(i); + value += fbv.data.get(i); + data.put(i, value); + } + } + + @Override + public void minus(Vector v) { + DoubleBigMappedVector fbv = (DoubleBigMappedVector) v; + for (int i = 0; i < capacity; i++) { + double value = data.get(i); + value -= fbv.data.get(i); + data.put(i, value); + } + } + + @Override + public void map(MapFunction f) { + for (int i = 0; i < capacity; i++) { + double v = data.get(i); + v = f.apply(v); + data.put(i, v); + } + } + +} Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigVector.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigVector.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigVector.java 2012-08-30 22:52:26 UTC (rev 442) @@ -109,8 +109,8 @@ } @Override - public void paste(Vector v) { - DoubleBigVector fbv = (DoubleBigVector) v; + public void paste(Vector src) { + DoubleBigVector fbv = (DoubleBigVector) src; System.arraycopy(fbv.data, 0, this.data, 0, this.size()); } Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/FloatVector.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/FloatVector.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/FloatVector.java 2012-08-30 22:52:26 UTC (rev 442) @@ -182,7 +182,7 @@ @Override public boolean equals(Object o) { boolean result = false; - if (o instanceof FloatVector) { + if (o instanceof FloatVector && defaultValue == ((FloatVector)o).defaultValue) { FloatVector other = (FloatVector) o; result = Arrays.equals(this.position, other.position) && data.equals(other.data); Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixFactory.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixFactory.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixFactory.java 2012-08-30 22:52:26 UTC (rev 442) @@ -25,6 +25,7 @@ package org.nuiton.math.matrix; +import java.io.RandomAccessFile; import java.lang.reflect.Constructor; import java.util.List; @@ -147,11 +148,26 @@ return new MatrixNDImpl(this, name, semantics, dimNames); } + /** + * Create new matrix by copying matrix in argument + * @param matrix + * @return + */ public MatrixND create(MatrixND matrix) { return new MatrixNDImpl(this, matrix); } /** + * Create new matrix by copying matrix in argument, and force backend + * to be data. + * @param matrix matrix to copied + * @param data backend used to new matrix + */ + public MatrixND create(MatrixND matrix, Vector data) { + return new MatrixNDImpl(this, matrix, data); + } + + /** * Crée une nouvelle matrice identité. Une matrice identité est une matrice * à 2 dimensions dont tous les éléments de la diagonal vaut 1 * Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixHelper.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixHelper.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixHelper.java 2012-08-30 22:52:26 UTC (rev 442) @@ -46,6 +46,45 @@ public class MatrixHelper { // MatrixHelper /** + * Retourne le linearFactor, ce tableau sert a convertir une position donnee + * par les dimensions d'une matrice en une position dans un Vecteur. + * @param dimensions + * @return + */ + public static int[] getLinearFactor(int[] dimensions) { + // calcul du linearFactor + int[] linearFactor = new int[dimensions.length]; + linearFactor[linearFactor.length - 1] = 1; + for (int i = linearFactor.length - 2; i >= 0; i--) { + linearFactor[i] = linearFactor[i + 1] * dimensions[i + 1]; + } + return linearFactor; + } + + /** + * Retourne la taillle que doit avoir un vecteur pour etre utilise avec + * les dimensions et le linearFactor en argument + * @param dimensions + * @param linearFactor calculer prealablement par getLinearFactor + * @return + */ + public static int getVectorSize(int[] dimensions, int[] linearFactor) { + int result = linearFactor[0] * dimensions[0]; + return result; + } + + /** + * Retourne la taillle que doit avoir un vecteur pour etre utilise avec + * les dimensions en argument + * @param dimensions + * @return + */ + public static int getVectorSize(int[] dimensions) { + int result = getLinearFactor(dimensions)[0] * dimensions[0]; + return result; + } + + /** * Convert Matrix to identity matrix must have 2 dimensions. If dimension * haven't same length, then the small dimension is used. * Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixNDImpl.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixNDImpl.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixNDImpl.java 2012-08-30 22:52:26 UTC (rev 442) @@ -86,6 +86,17 @@ this.paste(matrix); } + protected MatrixNDImpl(MatrixFactory factory, MatrixND matrix, Vector data) { + super(factory, matrix.getName(), matrix.getSemantics(), matrix + .getDimensionNames()); + this.matrix = new BasicMatrix(factory, data, dim); + this.paste(matrix); + } + + public BasicMatrix getInternalMatrix() { + return matrix; + } + @Override public boolean equalsValues(MatrixND mat) { boolean result; Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/Vector.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/Vector.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/Vector.java 2012-08-30 22:52:26 UTC (rev 442) @@ -86,9 +86,9 @@ /** * Copie les valeurs du vector passé en argument dans ce vector. * - * @param v vector to paste + * @param source vector to paste */ - public void paste(Vector v); + public void paste(Vector source); /** * Ajoute les valeurs du vector passé en argument a ce vector. Deleted: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -1,47 +0,0 @@ -/* - * #%L - * NuitonMatrix - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.math.matrix; - -/** - * BasicMatrixBigTest. - * - * Created: 27 oct. 2004 - * - * @author Benjamin Poussin <poussin@codelutin.com> - * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ - */ -public class BasicMatrixBigTest extends BasicMatrixTest { // BasicMatrixBigTest - - @Override - public MatrixFactory getFactory() throws Exception { - return MatrixFactory.getInstance(FloatBigVector.class); - } - -} // BasicMatrixBigTest - Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleBigMappedVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleBigMappedVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleBigMappedVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * BasicMatrixBigTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class BasicMatrixDoubleBigMappedVectorTest extends BasicMatrixDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleBigMappedVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleBigVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,326 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +import org.apache.commons.lang3.time.DurationFormatUtils; +import org.junit.Assert; +import org.junit.Test; + +/** + * BasicMatrixTest. + * + * Created: 27 oct. 2004 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class BasicMatrixDoubleBigVectorTest { // BasicMatrixTest + + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleBigVector.class); + } + + @Test + public void testNew() throws Exception { + BasicMatrix mat = null; + try { + mat = new BasicMatrix(getFactory(), null); + Assert.assertFalse(true); // on ne doit pas etre ici + } catch (NullPointerException eee) { + Assert.assertTrue(true); // mais on doit etre la + } + + try { + mat = new BasicMatrix(getFactory(), new int[] { 0 }); + Assert.assertFalse(true); // on ne doit pas etre ici + } catch (IllegalArgumentException eee) { + Assert.assertTrue(true); // mais on doit etre la + } + + mat = new BasicMatrix(getFactory(), new int[] { 100 }); + mat = new BasicMatrix(getFactory(), new int[] { 10, 1 }); + Assert.assertEquals(0.0, mat.getMaxOccurrence(), 0); + mat = new BasicMatrix(getFactory(), new int[] { 10, 10, 10, 10 }); + try { + mat = new BasicMatrix(getFactory(), new int[] { -10 }); + Assert.assertFalse(true); // on ne doit pas etre ici + } catch (IllegalArgumentException eee) { + Assert.assertTrue(true); // mais on doit etre la + } + try { + mat = new BasicMatrix(getFactory(), new int[] { 10, 20, -10, 20 }); + Assert.assertFalse(true); // on ne doit pas etre ici + } catch (IllegalArgumentException eee) { + Assert.assertTrue(true); // mais on doit etre la + } + } + + @Test + public void testDimension() throws Exception { + BasicMatrix mat = null; + mat = new BasicMatrix(getFactory(), new int[] { 1, 10, 30, 5 }); + + Assert.assertEquals(4, mat.getNbDim()); + Assert.assertEquals(1, mat.getDim(0)); + Assert.assertEquals(10, mat.getDim(1)); + Assert.assertEquals(30, mat.getDim(2)); + Assert.assertEquals(5, mat.getDim(3)); + + try { + mat.getDim(-3); + Assert.assertFalse(true); // on ne doit pas etre ici + } catch (IndexOutOfBoundsException eee) { + Assert.assertTrue(true); // mais on doit etre la + } + try { + mat.getDim(4); + Assert.assertFalse(true); // on ne doit pas etre ici + } catch (IndexOutOfBoundsException eee) { + Assert.assertTrue(true); // mais on doit etre la + } + } + + @Test + public void testGetSet() throws Exception { + BasicMatrix mat = null; + + // test avec la plus petit BasicMatrix possible + mat = new BasicMatrix(getFactory(), new int[] { 1 }); + // test la valeur par defaut doit etre 0 + Assert.assertEquals(0, mat.getValue(new int[] { 0 }), 0); + mat.setValue(new int[] { 0 }, 30); + Assert.assertEquals(30, mat.getValue(new int[] { 0 }), 0); + + // acces a un element qui n'existe pas + try { + mat.getValue(new int[] { 1 }); + Assert.assertFalse(true); // on ne doit pas etre ici + } catch (NoSuchElementException eee) { + Assert.assertTrue(true); // mais on doit etre la + } + + mat = new BasicMatrix(getFactory(), new int[] { 1, 10, 5 }); + mat.setValue(new int[] { 0, 0, 0 }, 0); + mat.setValue(new int[] { 0, 0, 1 }, 1); + mat.setValue(new int[] { 0, 0, 2 }, 2); + mat.setValue(new int[] { 0, 0, 3 }, 3); + mat.setValue(new int[] { 0, 0, 4 }, 4); + mat.setValue(new int[] { 0, 1, 0 }, 10); + mat.setValue(new int[] { 0, 1, 1 }, 11); + mat.setValue(new int[] { 0, 1, 2 }, 12); + mat.setValue(new int[] { 0, 1, 3 }, 13); + mat.setValue(new int[] { 0, 1, 4 }, 14); + mat.setValue(new int[] { 0, 2, 0 }, 20); + mat.setValue(new int[] { 0, 2, 1 }, 21); + mat.setValue(new int[] { 0, 2, 2 }, 22); + mat.setValue(new int[] { 0, 9, 4 }, 98); + mat.setValue(new int[] { 0, 4, 2 }, 97); + Assert.assertEquals(0, mat.getValue(new int[] { 0, 0, 0 }), 0); + Assert.assertEquals(98, mat.getValue(new int[] { 0, 9, 4 }), 0); + Assert.assertEquals(97, mat.getValue(new int[] { 0, 4, 2 }), 0); + + // System.out.println(mat.toString()); + + // acces a un element qui n'existe pas + try { + mat.setValue(new int[] { 0, 9, 5 }, 44); + Assert.assertFalse(true); // on ne doit pas etre ici + } catch (NoSuchElementException eee) { + Assert.assertTrue(true); // mais on doit etre la + } + + } + + @Test + public void testEquals() throws Exception { + BasicMatrix m1 = new BasicMatrix(getFactory(), new int[] { 3, 3, 3, 3 }); + BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 3, 3, 3, 3 }); + + Assert.assertEquals(m1, m2); + + m1.setValue(new int[] { 1, 2, 1, 2 }, 123); + m2.setValue(new int[] { 1, 2, 1, 2 }, 123); + + Assert.assertEquals(m1, m2); + + m1.setValue(new int[] { 1, 0, 1, 0 }, 321); + Assert.assertFalse(m1.equals(m2)); + } + + @Test + public void testIterator() throws Exception { + + int[][] val27 = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, + { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, { 0, 2, 0 }, + { 0, 2, 1 }, { 0, 2, 2 }, { 1, 0, 0 }, { 1, 0, 1 }, + { 1, 0, 2 }, { 1, 1, 0 }, { 1, 1, 1 }, { 1, 1, 2 }, + { 1, 2, 0 }, { 1, 2, 1 }, { 1, 2, 2 }, { 2, 0, 0 }, + { 2, 0, 1 }, { 2, 0, 2 }, { 2, 1, 0 }, { 2, 1, 1 }, + { 2, 1, 2 }, { 2, 2, 0 }, { 2, 2, 1 }, { 2, 2, 2 }, }; + int[][] val1 = new int[][] { { 0 } }; + + BasicMatrix m1 = new BasicMatrix(getFactory(), new int[] { 3, 3, 3 }); + // System.out.println("Matrice a 27 valeurs"); + int cpt = 0; + for (BasicMatrixIterator iter = m1.iterator(); iter.next();) { + Arrays.equals(val27[cpt], iter.getCoordinates()); + cpt++; + } + + Assert.assertEquals(27, cpt); + + cpt = 0; + BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 1 }); + // System.out.println("Matrice a 1 valeurs"); + for (BasicMatrixIterator iter = m2.iterator(); iter.next();) { + Arrays.equals(val1[cpt], iter.getCoordinates()); + cpt++; + } + + Assert.assertEquals(1, cpt); + } + + protected MapFunction f = new MapFunction() { + public double apply(double value) { + return value + 2; + } + }; + + @Test + public void testPerfLineaire() throws Exception { + long time = System.nanoTime(); + BasicMatrix m1 = new BasicMatrix(getFactory(), new int[] { 30, 30, 30, + 30 }); + m1.map(f); + long time1 = System.nanoTime(); + System.out.println("testPerfLineaire: " + + DurationFormatUtils.formatDuration((time1 - time) / 1000000, + "s'.'S")); + } + + @Test + public void testPerfCoordonnee() throws Exception { + long time = System.nanoTime(); + BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 30, 30, 30, + 30 }); + + BasicMatrixIterator inc = m2.iterator(); + while (inc.next()) { + inc.setValue(inc.getValue() + 2); + } + long time1 = System.nanoTime(); + System.out.println("testPerfCoordonnee iter: " + + DurationFormatUtils.formatDuration((time1 - time) / 1000000, + "s'.'S")); + + inc = m2.iterator(); + while (inc.next()) { + inc.setValue(inc.getValue() + 2); + } + long time2 = System.nanoTime(); + System.out.println("testPerfCoordonnee iter: " + + DurationFormatUtils.formatDuration((time2 - time1) / 1000000, + "s'.'S")); + + m2.map(f); + long time3 = System.nanoTime(); + System.out.println("testPerfLineaire map: " + + DurationFormatUtils.formatDuration((time3 - time2) / 1000000, + "s'.'S")); + + } + + @Test + public void testPerfCoordonnee2() throws Exception { + long time = System.nanoTime(); + BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 30, 30, 30, + 30 }); + + BasicMatrixIterator inc = m2.iterator(); + while (inc.next() && inc.next()) { + inc.setValue(inc.getValue() + 2); + } + long time1 = System.nanoTime(); + System.out.println("testPerfCoordonnee2: " + + DurationFormatUtils.formatDuration((time1 - time) / 1000000, + "s'.'S")); + + inc = m2.iterator(); + while (inc.next() && inc.next()) { + inc.setValue(inc.getValue() + 2); + } + long time2 = System.nanoTime(); + System.out.println("testPerfCoordonnee2 re: " + + DurationFormatUtils.formatDuration((time2 - time1) / 1000000, + "s'.'S")); + + m2.map(f); + long time3 = System.nanoTime(); + System.out.println("testPerfLineaire2: " + + DurationFormatUtils.formatDuration((time3 - time2) / 1000000, + "s'.'S")); + + } + + @Test + public void testPerfCoordonnee4() throws Exception { + long time = System.nanoTime(); + BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 30, 30, 30, + 30 }); + + BasicMatrixIterator inc = m2.iterator(); + while (inc.next() && inc.next() && inc.next() && inc.next()) { + inc.setValue(inc.getValue() + 2); + } + long time1 = System.nanoTime(); + System.out.println("testPerfCoordonnee4: " + + DurationFormatUtils.formatDuration((time1 - time) / 1000000, + "s'.'S")); + + inc = m2.iterator(); + while (inc.next() && inc.next() && inc.next() && inc.next()) { + inc.setValue(inc.getValue() + 2); + } + long time2 = System.nanoTime(); + System.out.println("testPerfCoordonnee4 re: " + + DurationFormatUtils.formatDuration((time2 - time1) / 1000000, + "s'.'S")); + + m2.map(f); + long time3 = System.nanoTime(); + System.out.println("testPerfLineaire4: " + + DurationFormatUtils.formatDuration((time3 - time2) / 1000000, + "s'.'S")); + + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixDoubleVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * BasicMatrixDoubleVectorTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class BasicMatrixDoubleVectorTest extends BasicMatrixDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixFloatBigVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixFloatBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixFloatBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * BasicMatrixFloatBigVectorTest.java + * + * Created: 27 oct. 2004 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class BasicMatrixFloatBigVectorTest extends BasicMatrixDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(FloatBigVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixFloatVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixFloatVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixFloatVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * BasicMatrixFloatVectorTest.java + * + * Created: 27 oct. 2004 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class BasicMatrixFloatVectorTest extends BasicMatrixDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(FloatVector.class); + } + +} Deleted: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixTest.java =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixTest.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -1,327 +0,0 @@ -/* - * #%L - * NuitonMatrix - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.math.matrix; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -import org.apache.commons.lang3.time.DurationFormatUtils; -import org.junit.Assert; -import org.junit.Test; - -/** - * BasicMatrixTest. - * - * Created: 27 oct. 2004 - * - * @author Benjamin Poussin <poussin@codelutin.com> - * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ - */ -public class BasicMatrixTest { // BasicMatrixTest - - public MatrixFactory getFactory() throws Exception { - return MatrixFactory.getInstance(); - } - - @Test - public void testNew() throws Exception { - BasicMatrix mat = null; - try { - mat = new BasicMatrix(getFactory(), null); - Assert.assertFalse(true); // on ne doit pas etre ici - } catch (NullPointerException eee) { - Assert.assertTrue(true); // mais on doit etre la - } - - try { - mat = new BasicMatrix(getFactory(), new int[] { 0 }); - Assert.assertFalse(true); // on ne doit pas etre ici - } catch (IllegalArgumentException eee) { - Assert.assertTrue(true); // mais on doit etre la - } - - mat = new BasicMatrix(getFactory(), new int[] { 100 }); - mat = new BasicMatrix(getFactory(), new int[] { 10, 1 }); - Assert.assertEquals(0.0, mat.getMaxOccurrence(), 0); - mat = new BasicMatrix(getFactory(), new int[] { 10, 10, 10, 10 }); - try { - mat = new BasicMatrix(getFactory(), new int[] { -10 }); - Assert.assertFalse(true); // on ne doit pas etre ici - } catch (IllegalArgumentException eee) { - Assert.assertTrue(true); // mais on doit etre la - } - try { - mat = new BasicMatrix(getFactory(), new int[] { 10, 20, -10, 20 }); - Assert.assertFalse(true); // on ne doit pas etre ici - } catch (IllegalArgumentException eee) { - Assert.assertTrue(true); // mais on doit etre la - } - } - - @Test - public void testDimension() throws Exception { - BasicMatrix mat = null; - mat = new BasicMatrix(getFactory(), new int[] { 1, 10, 30, 5 }); - - Assert.assertEquals(4, mat.getNbDim()); - Assert.assertEquals(1, mat.getDim(0)); - Assert.assertEquals(10, mat.getDim(1)); - Assert.assertEquals(30, mat.getDim(2)); - Assert.assertEquals(5, mat.getDim(3)); - - try { - mat.getDim(-3); - Assert.assertFalse(true); // on ne doit pas etre ici - } catch (IndexOutOfBoundsException eee) { - Assert.assertTrue(true); // mais on doit etre la - } - try { - mat.getDim(4); - Assert.assertFalse(true); // on ne doit pas etre ici - } catch (IndexOutOfBoundsException eee) { - Assert.assertTrue(true); // mais on doit etre la - } - } - - @Test - public void testGetSet() throws Exception { - BasicMatrix mat = null; - - // test avec la plus petit BasicMatrix possible - mat = new BasicMatrix(getFactory(), new int[] { 1 }); - // test la valeur par defaut doit etre 0 - Assert.assertEquals(0, mat.getValue(new int[] { 0 }), 0); - mat.setValue(new int[] { 0 }, 30); - Assert.assertEquals(30, mat.getValue(new int[] { 0 }), 0); - - // acces a un element qui n'existe pas - try { - mat.getValue(new int[] { 1 }); - Assert.assertFalse(true); // on ne doit pas etre ici - } catch (NoSuchElementException eee) { - Assert.assertTrue(true); // mais on doit etre la - } - - mat = new BasicMatrix(getFactory(), new int[] { 1, 10, 5 }); - mat.setValue(new int[] { 0, 0, 0 }, 0); - mat.setValue(new int[] { 0, 0, 1 }, 1); - mat.setValue(new int[] { 0, 0, 2 }, 2); - mat.setValue(new int[] { 0, 0, 3 }, 3); - mat.setValue(new int[] { 0, 0, 4 }, 4); - mat.setValue(new int[] { 0, 1, 0 }, 10); - mat.setValue(new int[] { 0, 1, 1 }, 11); - mat.setValue(new int[] { 0, 1, 2 }, 12); - mat.setValue(new int[] { 0, 1, 3 }, 13); - mat.setValue(new int[] { 0, 1, 4 }, 14); - mat.setValue(new int[] { 0, 2, 0 }, 20); - mat.setValue(new int[] { 0, 2, 1 }, 21); - mat.setValue(new int[] { 0, 2, 2 }, 22); - mat.setValue(new int[] { 0, 9, 4 }, 98); - mat.setValue(new int[] { 0, 4, 2 }, 97); - Assert.assertEquals(0, mat.getValue(new int[] { 0, 0, 0 }), 0); - Assert.assertEquals(98, mat.getValue(new int[] { 0, 9, 4 }), 0); - Assert.assertEquals(97, mat.getValue(new int[] { 0, 4, 2 }), 0); - - // System.out.println(mat.toString()); - - // acces a un element qui n'existe pas - try { - mat.setValue(new int[] { 0, 9, 5 }, 44); - Assert.assertFalse(true); // on ne doit pas etre ici - } catch (NoSuchElementException eee) { - Assert.assertTrue(true); // mais on doit etre la - } - - } - - @Test - public void testEquals() throws Exception { - BasicMatrix m1 = new BasicMatrix(getFactory(), new int[] { 3, 3, 3, 3 }); - BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 3, 3, 3, 3 }); - - Assert.assertEquals(m1, m2); - - m1.setValue(new int[] { 1, 2, 1, 2 }, 123); - m2.setValue(new int[] { 1, 2, 1, 2 }, 123); - - Assert.assertEquals(m1, m2); - - m1.setValue(new int[] { 1, 0, 1, 0 }, 321); - Assert.assertFalse(m1.equals(m2)); - } - - @Test - public void testIterator() throws Exception { - - int[][] val27 = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, - { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, { 0, 2, 0 }, - { 0, 2, 1 }, { 0, 2, 2 }, { 1, 0, 0 }, { 1, 0, 1 }, - { 1, 0, 2 }, { 1, 1, 0 }, { 1, 1, 1 }, { 1, 1, 2 }, - { 1, 2, 0 }, { 1, 2, 1 }, { 1, 2, 2 }, { 2, 0, 0 }, - { 2, 0, 1 }, { 2, 0, 2 }, { 2, 1, 0 }, { 2, 1, 1 }, - { 2, 1, 2 }, { 2, 2, 0 }, { 2, 2, 1 }, { 2, 2, 2 }, }; - int[][] val1 = new int[][] { { 0 } }; - - BasicMatrix m1 = new BasicMatrix(getFactory(), new int[] { 3, 3, 3 }); - // System.out.println("Matrice a 27 valeurs"); - int cpt = 0; - for (BasicMatrixIterator iter = m1.iterator(); iter.next();) { - Arrays.equals(val27[cpt], iter.getCoordinates()); - cpt++; - } - - Assert.assertEquals(27, cpt); - - cpt = 0; - BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 1 }); - // System.out.println("Matrice a 1 valeurs"); - for (BasicMatrixIterator iter = m2.iterator(); iter.next();) { - Arrays.equals(val1[cpt], iter.getCoordinates()); - cpt++; - } - - Assert.assertEquals(1, cpt); - } - - protected MapFunction f = new MapFunction() { - public double apply(double value) { - return value + 2; - } - }; - - @Test - public void testPerfLineaire() throws Exception { - long time = System.nanoTime(); - BasicMatrix m1 = new BasicMatrix(getFactory(), new int[] { 30, 30, 30, - 30 }); - m1.map(f); - long time1 = System.nanoTime(); - System.out.println("testPerfLineaire: " - + DurationFormatUtils.formatDuration((time1 - time) / 1000000, - "s'.'S")); - } - - @Test - public void testPerfCoordonnee() throws Exception { - long time = System.nanoTime(); - BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 30, 30, 30, - 30 }); - - BasicMatrixIterator inc = m2.iterator(); - while (inc.next()) { - inc.setValue(inc.getValue() + 2); - } - long time1 = System.nanoTime(); - System.out.println("testPerfCoordonnee iter: " - + DurationFormatUtils.formatDuration((time1 - time) / 1000000, - "s'.'S")); - - inc = m2.iterator(); - while (inc.next()) { - inc.setValue(inc.getValue() + 2); - } - long time2 = System.nanoTime(); - System.out.println("testPerfCoordonnee iter: " - + DurationFormatUtils.formatDuration((time2 - time1) / 1000000, - "s'.'S")); - - m2.map(f); - long time3 = System.nanoTime(); - System.out.println("testPerfLineaire map: " - + DurationFormatUtils.formatDuration((time3 - time2) / 1000000, - "s'.'S")); - - } - - @Test - public void testPerfCoordonnee2() throws Exception { - long time = System.nanoTime(); - BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 30, 30, 30, - 30 }); - - BasicMatrixIterator inc = m2.iterator(); - while (inc.next() && inc.next()) { - inc.setValue(inc.getValue() + 2); - } - long time1 = System.nanoTime(); - System.out.println("testPerfCoordonnee2: " - + DurationFormatUtils.formatDuration((time1 - time) / 1000000, - "s'.'S")); - - inc = m2.iterator(); - while (inc.next() && inc.next()) { - inc.setValue(inc.getValue() + 2); - } - long time2 = System.nanoTime(); - System.out.println("testPerfCoordonnee2 re: " - + DurationFormatUtils.formatDuration((time2 - time1) / 1000000, - "s'.'S")); - - m2.map(f); - long time3 = System.nanoTime(); - System.out.println("testPerfLineaire2: " - + DurationFormatUtils.formatDuration((time3 - time2) / 1000000, - "s'.'S")); - - } - - @Test - public void testPerfCoordonnee4() throws Exception { - long time = System.nanoTime(); - BasicMatrix m2 = new BasicMatrix(getFactory(), new int[] { 30, 30, 30, - 30 }); - - BasicMatrixIterator inc = m2.iterator(); - while (inc.next() && inc.next() && inc.next() && inc.next()) { - inc.setValue(inc.getValue() + 2); - } - long time1 = System.nanoTime(); - System.out.println("testPerfCoordonnee4: " - + DurationFormatUtils.formatDuration((time1 - time) / 1000000, - "s'.'S")); - - inc = m2.iterator(); - while (inc.next() && inc.next() && inc.next() && inc.next()) { - inc.setValue(inc.getValue() + 2); - } - long time2 = System.nanoTime(); - System.out.println("testPerfCoordonnee4 re: " - + DurationFormatUtils.formatDuration((time2 - time1) / 1000000, - "s'.'S")); - - m2.map(f); - long time3 = System.nanoTime(); - System.out.println("testPerfLineaire4: " - + DurationFormatUtils.formatDuration((time3 - time2) / 1000000, - "s'.'S")); - - } - -} // BasicMatrixTest - Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleBigMappedVectorTest1.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleBigMappedVectorTest1.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleBigMappedVectorTest1.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * MatrixHelperTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixHelperDoubleBigMappedVectorTest1 extends MatrixHelperDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() { + return MatrixFactory.getInstance(DoubleBigMappedVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleBigVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,177 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Test; + +/** + * MatrixHelperTest. + * + * Created: 29 oct. 2004 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixHelperDoubleBigVectorTest { // MatrixHelperTest + + /** to use log facility, just put in your code: log.info(\"...\"); */ + private static Log log = LogFactory.getLog(MatrixHelperDoubleBigVectorTest.class); + + public MatrixFactory getFactory() { + return MatrixFactory.getInstance(DoubleBigVector.class); + } + + @Test + public void testCoordinatesToString() { + Assert.assertEquals("1", MatrixHelper + .coordinatesToString(new int[] { 1 })); + Assert.assertEquals("2,3,4,5", MatrixHelper + .coordinatesToString(new int[] { 2, 3, 4, 5 })); + Assert.assertEquals("2,3,4,5,234", MatrixHelper + .coordinatesToString(new int[] { 2, 3, 4, 5, 234 })); + + Assert.assertEquals("a", MatrixHelper + .coordinatesToString(new String[] { "a" })); + Assert.assertEquals("a,b,n,m", MatrixHelper + .coordinatesToString(new String[] { "a", "b", "n", "m" })); + Assert.assertEquals("a,b,f,e,aze", + MatrixHelper.coordinatesToString(new String[] { "a", "b", "f", + "e", "aze" })); + } + + @Test + public void testSameDimension() { + Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 1 }, + new int[] { 1 })); + Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 1, 2 }, + new int[] { 1, 2 })); + Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 1, 324, 3 }, + new int[] { 1, 324, 3 })); + } + + @Test + public void testDimensionToSemantics() { + // TODO faire un test pour dimensionToSemantics + } + + @Test + public void testSemanticsToDimension() { + // TODO faire un test pour semanticsToDimension + } + + @Test + public void testFill() { + MatrixND mat = getFactory().create(new int[] { 3, 3 }); + MatrixHelper.fill(mat, 4); + + Assert.assertEquals(4, mat.getValue(1, 1), 0); + } + + @Test + public void testMatrixId() { + MatrixND mat = getFactory().matrixId(4); + Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 4, 4 }, mat + .getDim())); + Assert.assertEquals(0, mat.getValue(1, 2), 0); + Assert.assertEquals(1, mat.getValue(0, 0), 0); + Assert.assertEquals(1, mat.getValue(1, 1), 0); + Assert.assertEquals(1, mat.getValue(2, 2), 0); + Assert.assertEquals(1, mat.getValue(3, 3), 0); + } + + @Test + public void testToList() { + MatrixND mat1 = getFactory().create(new int[] { 3, 2, 1 }); + mat1.setValue(0, 0, 0, -1.0E-7); + mat1.setValue(1, 1, 0, 2); + mat1.setValue(2, 1, 0, 5.0E-7); + + List l = mat1.toList(); + String s = String.valueOf(l); + List l2 = MatrixHelper.convertStringToList(s); + + System.out.println(l); + System.out.println(l2); + + Assert.assertEquals(l, l2); + + } + + @Test + public void testMaxOccurrence() { + double[] val = new double[5]; + + Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); + + val[2] = -1; + Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); + val[0] = -1; + Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); + val[1] = -1; + Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); + val[4] = -3; + Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); + val[3] = 3; + Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); + + val = new double[6]; + + Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); + + val[2] = -1; + Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); + val[0] = -1; + Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); + val[1] = -1; + Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); + val[4] = -3; + Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); + val[3] = -3; + Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); + val[5] = -3; + Assert.assertEquals(-3, MatrixHelper.maxOccurrence(val), 0); + + val = new double[0]; + try { + MatrixHelper.maxOccurrence(val); + Assert.fail("An exception must be thrown"); + } catch (IllegalArgumentException e) { + if (log.isDebugEnabled()) { + log.debug("Exception normally thrown", e); + } + } + + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperDoubleVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * MatrixHelperTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixHelperDoubleVectorTest extends MatrixHelperDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() { + return MatrixFactory.getInstance(DoubleVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperFloatBigVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperFloatBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperFloatBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * MatrixHelperTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixHelperFloatBigVectorTest extends MatrixHelperDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() { + return MatrixFactory.getInstance(FloatBigVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperFloatVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperFloatVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperFloatVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * MatrixHelperTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixHelperFloatVectorTest extends MatrixHelperDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() { + return MatrixFactory.getInstance(FloatVector.class); + } + +} Deleted: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperTest.java =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperTest.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixHelperTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -1,178 +0,0 @@ -/* - * #%L - * NuitonMatrix - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.math.matrix; - -import java.util.List; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.junit.Assert; -import org.junit.Test; - -/** - * MatrixHelperTest. - * - * Created: 29 oct. 2004 - * - * @author Benjamin Poussin <poussin@codelutin.com> - * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ - */ -public class MatrixHelperTest { // MatrixHelperTest - - /** to use log facility, just put in your code: log.info(\"...\"); */ - private static Log log = LogFactory.getLog(MatrixHelperTest.class); - - public MatrixFactory getFactory() { - return MatrixFactory.getInstance(); - } - - @Test - public void testCoordinatesToString() { - Assert.assertEquals("1", MatrixHelper - .coordinatesToString(new int[] { 1 })); - Assert.assertEquals("2,3,4,5", MatrixHelper - .coordinatesToString(new int[] { 2, 3, 4, 5 })); - Assert.assertEquals("2,3,4,5,234", MatrixHelper - .coordinatesToString(new int[] { 2, 3, 4, 5, 234 })); - - Assert.assertEquals("a", MatrixHelper - .coordinatesToString(new String[] { "a" })); - Assert.assertEquals("a,b,n,m", MatrixHelper - .coordinatesToString(new String[] { "a", "b", "n", "m" })); - Assert.assertEquals("a,b,f,e,aze", - MatrixHelper.coordinatesToString(new String[] { "a", "b", "f", - "e", "aze" })); - } - - @Test - public void testSameDimension() { - Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 1 }, - new int[] { 1 })); - Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 1, 2 }, - new int[] { 1, 2 })); - Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 1, 324, 3 }, - new int[] { 1, 324, 3 })); - } - - @Test - public void testDimensionToSemantics() { - // TODO faire un test pour dimensionToSemantics - } - - @Test - public void testSemanticsToDimension() { - // TODO faire un test pour semanticsToDimension - } - - @Test - public void testFill() { - MatrixND mat = getFactory().create(new int[] { 3, 3 }); - MatrixHelper.fill(mat, 4); - - Assert.assertEquals(4, mat.getValue(1, 1), 0); - } - - @Test - public void testMatrixId() { - MatrixND mat = getFactory().matrixId(4); - Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 4, 4 }, mat - .getDim())); - Assert.assertEquals(0, mat.getValue(1, 2), 0); - Assert.assertEquals(1, mat.getValue(0, 0), 0); - Assert.assertEquals(1, mat.getValue(1, 1), 0); - Assert.assertEquals(1, mat.getValue(2, 2), 0); - Assert.assertEquals(1, mat.getValue(3, 3), 0); - } - - @Test - public void testToList() { - MatrixND mat1 = getFactory().create(new int[] { 3, 2, 1 }); - mat1.setValue(0, 0, 0, -1.0E-7); - mat1.setValue(1, 1, 0, 2); - mat1.setValue(2, 1, 0, 5.0E-7); - - List l = mat1.toList(); - String s = String.valueOf(l); - List l2 = MatrixHelper.convertStringToList(s); - - System.out.println(l); - System.out.println(l2); - - Assert.assertEquals(l, l2); - - } - - @Test - public void testMaxOccurrence() { - double[] val = new double[5]; - - Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); - - val[2] = -1; - Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); - val[0] = -1; - Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); - val[1] = -1; - Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); - val[4] = -3; - Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); - val[3] = 3; - Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); - - val = new double[6]; - - Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); - - val[2] = -1; - Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); - val[0] = -1; - Assert.assertEquals(0, MatrixHelper.maxOccurrence(val), 0); - val[1] = -1; - Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); - val[4] = -3; - Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); - val[3] = -3; - Assert.assertEquals(-1, MatrixHelper.maxOccurrence(val), 0); - val[5] = -3; - Assert.assertEquals(-3, MatrixHelper.maxOccurrence(val), 0); - - val = new double[0]; - try { - MatrixHelper.maxOccurrence(val); - Assert.fail("An exception must be thrown"); - } catch (IllegalArgumentException e) { - if (log.isDebugEnabled()) { - log.debug("Exception normally thrown", e); - } - } - - } - -} // MatrixHelperTest - Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleBigMappedVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleBigMappedVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleBigMappedVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * MatrixNDDoubleBigMappedVectorTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixNDDoubleBigMappedVectorTest extends MatrixNDDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleBigMappedVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleBigVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,1098 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +import java.io.IOException; +import java.io.StreamTokenizer; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Stack; + +import org.apache.commons.lang3.time.DurationFormatUtils; +import org.junit.Assert; +import org.junit.Test; +import org.nuiton.util.StringUtil; + +/** + * MatrixNDTest.java + * + * Created: 10 mai 2004 + * + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixNDDoubleBigVectorTest { + + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleBigVector.class); + } + + @Test + public void testNew() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new int[] { 3, 3, 3 }); + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }); + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + Assert.assertEquals(0.0, mat.getMaxOccurence(), 0); + + } + + @Test + public void testSemantique() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create(new int[] { 3, 3, 3 }); + + Assert.assertNull(mat.getSemantic(1).get(1)); + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }); + + // la matrice doit avoir ca propre copie des semantiques + s2.set(1, "pas bon"); + Assert.assertEquals("f", mat.getSemantic(1).get(1)); + + mat.setSemantics(1, s1); + Assert.assertEquals("b", mat.getSemantic(1).get(1)); + } + + @Test + public void testName() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + Assert.assertEquals("Ma mat", mat.getName()); + mat.setName("Renamed"); + Assert.assertEquals("Renamed", mat.getName()); + + Assert.assertEquals("dim abc", mat.getDimensionName(0)); + mat.setDimensionName(0, "dim renamed"); + Assert.assertEquals("dim renamed", mat.getDimensionName(0)); + } + + @Test + public void testGetSet() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + mat.setValue(1, 1, 1, 34); + Assert.assertEquals(34, mat.getValue("b", "f", "l"), 0); + + mat.setValue("a", "f", "m", 22); + Assert.assertEquals(22, mat.getValue(0, 1, 2), 0); + } + + @Test + public void testIterator() throws Exception { + int[][] val27 = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, + { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, { 0, 2, 0 }, + { 0, 2, 1 }, { 0, 2, 2 }, { 1, 0, 0 }, { 1, 0, 1 }, + { 1, 0, 2 }, { 1, 1, 0 }, { 1, 1, 1 }, { 1, 1, 2 }, + { 1, 2, 0 }, { 1, 2, 1 }, { 1, 2, 2 }, { 2, 0, 0 }, + { 2, 0, 1 }, { 2, 0, 2 }, { 2, 1, 0 }, { 2, 1, 1 }, + { 2, 1, 2 }, { 2, 2, 0 }, { 2, 2, 1 }, { 2, 2, 2 }, }; + + String[][] vals27 = new String[][] { { "a", "e", "k" }, + { "a", "e", "l" }, { "a", "e", "m" }, { "a", "f", "k" }, + { "a", "f", "l" }, { "a", "f", "m" }, { "a", "g", "k" }, + { "a", "g", "l" }, { "a", "g", "m" }, { "b", "e", "k" }, + { "b", "e", "l" }, { "b", "e", "m" }, { "b", "f", "k" }, + { "b", "f", "l" }, { "b", "f", "m" }, { "b", "g", "k" }, + { "b", "g", "l" }, { "b", "g", "m" }, { "c", "e", "k" }, + { "c", "e", "l" }, { "c", "e", "m" }, { "c", "f", "k" }, + { "c", "f", "l" }, { "c", "f", "m" }, { "c", "g", "k" }, + { "c", "g", "l" }, { "c", "g", "m" }, }; + + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + int cpt = 0; + for (MatrixIterator i = mat.iterator(); i.hasNext();) { + i.next(); + Assert.assertTrue(Arrays.equals(val27[cpt], i.getCoordinates())); + Assert.assertTrue(Arrays.equals(vals27[cpt], i + .getSemanticsCoordinates())); + cpt++; + } + Assert.assertEquals(27, cpt); + } + + @Test + public void testAdd() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat1 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + MatrixND mat2 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat1, 3); + MatrixHelper.fill(mat2, 26); + + mat1.add(mat2); + + Assert.assertEquals(29, mat1.getValue(0, 0, 0), 0); + Assert.assertEquals(29, mat1.getValue(1, 2, 0), 0); + Assert.assertEquals(29, mat1.getValue(2, 2, 2), 0); + } + + @Test + public void testMinus() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat1 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + MatrixND mat2 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat1, 3); + MatrixHelper.fill(mat2, 26); + + mat1.minus(mat2); + + Assert.assertEquals(-23, mat1.getValue(0, 0, 0), 0); + Assert.assertEquals(-23, mat1.getValue(1, 2, 0), 0); + Assert.assertEquals(-23, mat1.getValue(2, 2, 2), 0); + } + + @Test + public void testEquals() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat1 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + MatrixND mat2 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + MatrixND mat3 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat1, 3); + MatrixHelper.fill(mat2, 26); + MatrixHelper.fill(mat3, 29); + + mat1.add(mat2); + + Assert.assertEquals(mat1, mat3); + } + + @Test + public void testsumOverDim() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create(new int[] { 4, 4 }); + MatrixND mat2 = mat; + int i = 0; + for (MatrixIterator mi = mat.iterator(); mi.next();) { + mi.setValue(++i); + } + + Assert.assertEquals(mat, mat2); + mat2 = mat.sumOverDim(1, 0); + Assert.assertEquals(mat, mat2); + mat2 = mat.sumOverDim(1, 1); + Assert.assertEquals(mat, mat2); + mat2 = mat.sumOverDim(1, 2); + Assert.assertEquals(2, mat2.getDim(1)); + Assert.assertEquals(3, mat2.getValue(0, 0), 0); + Assert.assertEquals(7, mat2.getValue(0, 1), 0); + Assert.assertEquals(11, mat2.getValue(1, 0), 0); + Assert.assertEquals(15, mat2.getValue(1, 1), 0); + Assert.assertEquals(19, mat2.getValue(2, 0), 0); + Assert.assertEquals(23, mat2.getValue(2, 1), 0); + Assert.assertEquals(27, mat2.getValue(3, 0), 0); + Assert.assertEquals(31, mat2.getValue(3, 1), 0); + + mat2 = mat.sumOverDim(1, 3); + Assert.assertEquals(1, mat2.getDim(1)); + Assert.assertEquals(6, mat2.getValue(0, 0), 0); + Assert.assertEquals(18, mat2.getValue(1, 0), 0); + Assert.assertEquals(30, mat2.getValue(2, 0), 0); + Assert.assertEquals(42, mat2.getValue(3, 0), 0); + + mat2 = mat.sumOverDim(1, 4); + Assert.assertEquals(1, mat2.getDim(1)); + Assert.assertEquals(10, mat2.getValue(0, 0), 0); + Assert.assertEquals(26, mat2.getValue(1, 0), 0); + Assert.assertEquals(42, mat2.getValue(2, 0), 0); + Assert.assertEquals(58, mat2.getValue(3, 0), 0); + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat, 3); + mat.setValue(0, 0, 0, 2); + mat.setValue(2, 2, 2, 4); + mat = mat.sumOverDim(1); + Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 3, 1, 3 }, mat + .getDim())); + + Assert.assertEquals(8, mat.getValue(0, 0, 0), 0); + Assert.assertEquals(9, mat.getValue(2, 0, 1), 0); + Assert.assertEquals(10, mat.getValue(2, 0, 2), 0); + + mat = getFactory().create(new int[] { 6, 6, 6 }); + + MatrixHelper.fill(mat, 3); + mat.setValue(0, 0, 0, 0); + mat.setValue(0, 1, 0, 1); + mat.setValue(0, 2, 0, 2); + mat.setValue(0, 3, 0, 3); + mat.setValue(0, 4, 0, 4); + mat.setValue(0, 5, 0, 5); + + mat = mat.sumOverDim(1, 3); + Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 6, 2, 6 }, mat + .getDim())); + Assert.assertEquals(3, mat.getValue(0, 0, 0), 0); + Assert.assertEquals(12, mat.getValue(0, 1, 0), 0); + Assert.assertEquals(9, mat.getValue(1, 1, 5), 0); + Assert.assertEquals(9, mat.getValue(5, 0, 3), 0); + } + + /** + * Test les fonctions meanOverDim... + * + * La matrice de test est + * 1 2 3 4 + * 5 6 7 8 + * 9 10 11 12 + * 13 14 15 16 + * @throws Exception + */ + @Test + public void testMeanOverDim() throws Exception { + + // first test matrix : + // 1 2 3 4 + // 5 6 7 8 + // 9 10 11 12 + // 13 14 15 16 + + MatrixND mat = null; + mat = getFactory().create(new int[] { 4, 4 }); + MatrixND mat2 = mat; + int i = 0; + for (MatrixIterator mi = mat.iterator(); mi.next();) { + mi.setValue(++i); + } + + Assert.assertEquals(mat, mat2); + mat2 = mat.meanOverDim(1, 0); + Assert.assertEquals(mat, mat2); + mat2 = mat.meanOverDim(1, 1); + Assert.assertEquals(mat, mat2); + mat2 = mat.meanOverDim(1, 2); + Assert.assertEquals(2, mat2.getDim(1)); + Assert.assertEquals(1.5, mat2.getValue(0, 0), 0); + Assert.assertEquals(3.5, mat2.getValue(0, 1), 0); + Assert.assertEquals(5.5, mat2.getValue(1, 0), 0); + Assert.assertEquals(7.5, mat2.getValue(1, 1), 0); + Assert.assertEquals(9.5, mat2.getValue(2, 0), 0); + Assert.assertEquals(11.5, mat2.getValue(2, 1), 0); + Assert.assertEquals(13.5, mat2.getValue(3, 0), 0); + Assert.assertEquals(15.5, mat2.getValue(3, 1), 0); + + mat2 = mat.meanOverDim(1, 3); + Assert.assertEquals(1, mat2.getDim(1)); + Assert.assertEquals(2.0, mat2.getValue(0, 0), 0); + Assert.assertEquals(6.0, mat2.getValue(1, 0), 0); + Assert.assertEquals(10.0, mat2.getValue(2, 0), 0); + Assert.assertEquals(14.0, mat2.getValue(3, 0), 0); + + mat2 = mat.meanOverDim(1, 4); + Assert.assertEquals(1, mat2.getDim(1)); + Assert.assertEquals(2.5, mat2.getValue(0, 0), 0); + Assert.assertEquals(6.5, mat2.getValue(1, 0), 0); + Assert.assertEquals(10.5, mat2.getValue(2, 0), 0); + Assert.assertEquals(14.5, mat2.getValue(3, 0), 0); + + // meanAll + double meanAll = mat.meanAll(); + Assert.assertEquals(8.5, meanAll, 0); + + // seconde matrice (en 3 dimension :D) + // 2 3 3 3 3 3 3 3 3 + // 3 3 3 3 3 3 3 3 3 + // 3 3 3 3 3 3 3 3 4 + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat, 3); + mat.setValue(0, 0, 0, 2); + mat.setValue(2, 2, 2, 4); + mat = mat.meanOverDim(1); + // donne : + // 2.66 3 3 3 3 3 3 3 3.33 + Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 3, 1, 3 }, mat + .getDim())); + + Assert.assertEquals(2.6666, mat.getValue(0, 0, 0), 0.0001); + Assert.assertEquals(3, mat.getValue(2, 0, 1), 0); + Assert.assertEquals(3.3333, mat.getValue(2, 0, 2), 0.0001); + + mat = getFactory().create(new int[] { 6, 6, 6 }); + + MatrixHelper.fill(mat, 3); + mat.setValue(0, 0, 0, 0); + mat.setValue(0, 1, 0, 1); + mat.setValue(0, 2, 0, 2); + mat.setValue(0, 3, 0, 3); + mat.setValue(0, 4, 0, 4); + mat.setValue(0, 5, 0, 5); + + // meanAll + meanAll = mat.meanAll(); + Assert.assertEquals(2.9861, meanAll, 0.0001); + + mat = mat.meanOverDim(1, 3); + Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 6, 2, 6 }, mat + .getDim())); + Assert.assertEquals(1.0, mat.getValue(0, 0, 0), 0); + Assert.assertEquals(4.0, mat.getValue(0, 1, 0), 0); + Assert.assertEquals(3.0, mat.getValue(1, 1, 5), 0); + Assert.assertEquals(3.0, mat.getValue(5, 0, 3), 0); + } + + /** + * Test de reduction simple par indice. + * + * @throws Exception + */ + @Test + public void testSubMatrixIntIntArray() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + // reduction de "e", "f", "g" en "f", "g" + MatrixND mat2 = mat.getSubMatrix(1, new int[]{1,2}); + Assert.assertEquals(Arrays.asList(new String[]{"f","g"}), mat2.getSemantic(1)); + mat2 = mat2.getSubMatrix(2, new int[]{2}); + Assert.assertEquals(Arrays.asList(new String[]{"m"}), mat2.getSemantic(2)); + } + + /** + * Test de reduction simple par semantic. + * + * @throws Exception + */ + @Test + public void testSubMatrixIntObjectArray() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + // reduction de "e", "f", "g" en "f", "g" + MatrixND mat2 = mat.getSubMatrix(1, "f", "g"); + Assert.assertEquals(Arrays.asList(new String[]{"f","g"}), mat2.getSemantic(1)); + mat2 = mat2.getSubMatrix(2, "m"); + Assert.assertEquals(Arrays.asList(new String[]{"m"}), mat2.getSemantic(2)); + } + + /** + * Test de reduction multiple par indices. + * + * @throws Exception + */ + @Test + public void testSubMatrixIntArrayArray() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + // reduction de "e", "f", "g" en "f", "g" et "k", "l", "m" en "m" + MatrixND mat2 = mat.getSubMatrix(new int[][]{new int[]{0, 1, 2}, new int[]{1,2}, new int[]{2}}); + Assert.assertEquals(Arrays.asList(new String[]{"f","g"}), mat2.getSemantic(1)); + Assert.assertEquals(Arrays.asList(new String[]{"m"}), mat2.getSemantic(2)); + + // meme test avec des null + mat2 = mat.getSubMatrix(new int[][]{null, new int[]{1,2}, new int[]{2}}); + Assert.assertEquals(Arrays.asList(new String[]{"f","g"}), mat2.getSemantic(1)); + Assert.assertEquals(Arrays.asList(new String[]{"m"}), mat2.getSemantic(2)); + } + + /** + * Test de reduction sur le mauvais nombre de dimension. + * + * @throws Exception + */ + @Test(expected=IllegalArgumentException.class) + public void testSubMatrixIntArrayArrayException() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + // reduction de "e", "f", "g" en "f", "g" + mat.getSubMatrix(new int[][]{null}); + } + + @Test + public void testTranspose() throws Exception { + MatrixND mat = null; + + mat = getFactory().create(new int[] { 2, 4 }); + mat.setValue(1, 3, 56); + mat.setValue(0, 2, 34); + mat.setValue(0, 1, 64); + mat.setValue(1, 0, 46); + + mat = mat.transpose(); + Assert.assertEquals(56, mat.getValue(3, 1), 0); + Assert.assertEquals(34, mat.getValue(2, 0), 0); + Assert.assertEquals(64, mat.getValue(1, 0), 0); + Assert.assertEquals(46, mat.getValue(0, 1), 0); + + mat = getFactory().create(new int[] { 4 }); + mat.setValue(1, 56); + mat.setValue(2, 34); + mat.setValue(3, 64); + mat.setValue(0, 46); + + mat = mat.transpose(); + Assert.assertEquals(56, mat.getValue(0, 1), 0); + Assert.assertEquals(34, mat.getValue(0, 2), 0); + Assert.assertEquals(64, mat.getValue(0, 3), 0); + Assert.assertEquals(46, mat.getValue(0, 0), 0); + } + + @Test + public void testMults() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat1 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + MatrixND mat2 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat1, 3); + MatrixHelper.fill(mat2, 26); + + mat1.mults(3); + mat2.mults(0); + + Assert.assertEquals(9, mat1.getValue(0, 0, 0), 0); + Assert.assertEquals(9, mat1.getValue(1, 2, 0), 0); + Assert.assertEquals(0, mat2.getValue(2, 2, 2), 0); + + MatrixND mat0 = getFactory().create(new int[] { 4, 4 }); + MatrixND matId = getFactory().matrixId(4); + matId.mults(0); + Assert.assertEquals(mat0, matId); + } + + @Test + public void testDivs() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat1 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + MatrixND mat2 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat1, 3); + MatrixHelper.fill(mat2, 26); + + mat1.divs(3); + mat2.divs(4); + + Assert.assertEquals(1, mat1.getValue(0, 0, 0), 0); + Assert.assertEquals(6.5, mat2.getValue(1, 2, 0), 0); + Assert.assertEquals(6.5, mat2.getValue(2, 2, 2), 0); + } + + @Test + public void testAdds() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat1 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + MatrixND mat2 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat1, 3); + MatrixHelper.fill(mat2, 26); + + mat1.adds(3); + mat2.adds(4); + + Assert.assertEquals(6, mat1.getValue(0, 0, 0), 0); + Assert.assertEquals(30, mat2.getValue(1, 2, 0), 0); + Assert.assertEquals(30, mat2.getValue(2, 2, 2), 0); + } + + @Test + public void testMinuss() throws Exception { + List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat1 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + MatrixND mat2 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat1, 3); + MatrixHelper.fill(mat2, 26); + + mat1.minuss(3); + mat2.minuss(4); + + Assert.assertEquals(0, mat1.getValue(0, 0, 0), 0); + Assert.assertEquals(22, mat2.getValue(1, 2, 0), 0); + Assert.assertEquals(22, mat2.getValue(2, 2, 2), 0); + } + + @Test + public void testPaste() throws Exception { + + MatrixND mat1 = getFactory().create(new int[] { 6, 7, 8 }); + MatrixND mat2 = getFactory().create(new int[] { 2, 2, 2 }); + + MatrixHelper.fill(mat1, 3); + MatrixHelper.fill(mat2, 26); + + mat1.paste(new int[] { 3, 0, 4 }, mat2); + + Assert.assertEquals(3, mat1.getValue(0, 0, 0), 0); + Assert.assertEquals(26, mat1.getValue(3, 0, 4), 0); + Assert.assertEquals(26, mat1.getValue(4, 1, 5), 0); + Assert.assertEquals(3, mat1.getValue(5, 2, 6), 0); + } + + @Test + public void testSumOverDim() throws Exception { + + MatrixND mat = null; + + mat = getFactory().create(new int[] { 6, 7 }); + MatrixND result = getFactory().create(new int[] { 3, 7 }); + + MatrixHelper.fill(mat, 3); + + MatrixND sub = mat.getSubMatrix(0, 0, 1); + result.paste(new int[] { 0, 0 }, sub.sumOverDim(0)); + + sub = mat.getSubMatrix(0, 1, 3); + result.paste(new int[] { 1, 0 }, sub.sumOverDim(0)); + + sub = mat.getSubMatrix(0, 4, 2); + result.paste(new int[] { 2, 0 }, sub.sumOverDim(0)); + + mat = getFactory().create(new int[] { 6, 7 }); + MatrixHelper.fill(mat, 3); + + mat = mat.sumOverDim(0, 0, 1); + mat = mat.sumOverDim(0, 1, 3); + mat = mat.sumOverDim(0, 2, 2); + + Assert.assertTrue(result.equalsValues(mat)); + } + + @Test + public void testReduce() throws Exception { + + MatrixND mat = null; + + mat = getFactory().create(new int[] { 6, 7, 8 }); + MatrixHelper.fill(mat, 3); + + mat = mat.sumOverDim(1); + Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); + mat = mat.reduce(); + Assert.assertEquals(2, mat.getNbDim()); + Assert.assertEquals(6, mat.getDim(0)); + Assert.assertEquals(8, mat.getDim(1)); + Assert.assertEquals(21, mat.getValue(0, 0), 0); + Assert.assertEquals(21, mat.getValue(5, 7), 0); + + mat = getFactory().create(new int[] { 2, 2, 2 }); + MatrixHelper.fill(mat, 26); + + mat = mat.sumOverDim(1); + mat = mat.sumOverDim(0); + Assert.assertEquals(104, mat.getValue(0, 0, 0), 0); + mat = mat.reduce(); + Assert.assertEquals(1, mat.getNbDim()); + Assert.assertEquals(2, mat.getDim(0)); + Assert.assertEquals(104, mat.getValue(0), 0); + Assert.assertEquals(104, mat.getValue(1), 0); + + mat = getFactory().create(new int[] { 2, 2, 2 }); + MatrixHelper.fill(mat, 6); + + mat = mat.sumOverDim(1); + mat = mat.sumOverDim(2); + mat = mat.sumOverDim(0); + Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); + mat = mat.reduce(); + Assert.assertEquals(1, mat.getNbDim()); + Assert.assertEquals(1, mat.getDim(0)); + Assert.assertEquals(48, mat.getValue(0), 0); + + mat = getFactory().create(new int[] { 6, 7, 8 }); + MatrixHelper.fill(mat, 3); + + mat = mat.sumOverDim(1); + Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); + mat = mat.reduce(2); + Assert.assertEquals(2, mat.getNbDim()); + Assert.assertEquals(6, mat.getDim(0)); + Assert.assertEquals(8, mat.getDim(1)); + Assert.assertEquals(21, mat.getValue(0, 0), 0); + Assert.assertEquals(21, mat.getValue(5, 7), 0); + + mat = getFactory().create(new int[] { 2, 2, 2 }); + MatrixHelper.fill(mat, 26); + + mat = mat.sumOverDim(1); + mat = mat.sumOverDim(0); + Assert.assertEquals(104, mat.getValue(0, 0, 0), 0); + mat = mat.reduce(2); + Assert.assertEquals(2, mat.getNbDim()); + Assert.assertEquals(1, mat.getDim(0)); + Assert.assertEquals(2, mat.getDim(1)); + Assert.assertEquals(104, mat.getValue(0, 0), 0); + Assert.assertEquals(104, mat.getValue(0, 1), 0); + + mat = getFactory().create(new int[] { 2, 2, 2 }); + MatrixHelper.fill(mat, 6); + + mat = mat.sumOverDim(1); + mat = mat.sumOverDim(2); + mat = mat.sumOverDim(0); + Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); + mat = mat.reduce(5); + Assert.assertEquals(3, mat.getNbDim()); + Assert.assertEquals(1, mat.getDim(0)); + Assert.assertEquals(1, mat.getDim(1)); + Assert.assertEquals(1, mat.getDim(2)); + Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); + + } + + @Test + public void testCreateFromDoubleArray() throws Exception { + MatrixND mat = null; + + mat = getFactory().create(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, + new int[] { 3, 3 }); + + Assert.assertEquals(2, mat.getDim().length); + Assert.assertEquals(3, mat.getDim(0)); + Assert.assertEquals(3, mat.getDim(1)); + Assert.assertEquals(1, (int) mat.getValue(new int[] { 0, 0 })); + Assert.assertEquals(2, (int) mat.getValue(new int[] { 0, 1 })); + Assert.assertEquals(3, (int) mat.getValue(new int[] { 0, 2 })); + Assert.assertEquals(4, (int) mat.getValue(new int[] { 1, 0 })); + Assert.assertEquals(5, (int) mat.getValue(new int[] { 1, 1 })); + Assert.assertEquals(6, (int) mat.getValue(new int[] { 1, 2 })); + Assert.assertEquals(7, (int) mat.getValue(new int[] { 2, 0 })); + Assert.assertEquals(8, (int) mat.getValue(new int[] { 2, 1 })); + Assert.assertEquals(9, (int) mat.getValue(new int[] { 2, 2 })); + + mat = getFactory().create(new double[] { 1, 2, 3, 4, 5, 6}, + new int[] { 2, 3 }); + + Assert.assertEquals(2, mat.getDim().length); + Assert.assertEquals(2, mat.getDim(0)); + Assert.assertEquals(3, mat.getDim(1)); + Assert.assertEquals(1, (int) mat.getValue(new int[] { 0, 0 })); + Assert.assertEquals(2, (int) mat.getValue(new int[] { 0, 1 })); + Assert.assertEquals(3, (int) mat.getValue(new int[] { 0, 2 })); + Assert.assertEquals(4, (int) mat.getValue(new int[] { 1, 0 })); + Assert.assertEquals(5, (int) mat.getValue(new int[] { 1, 1 })); + Assert.assertEquals(6, (int) mat.getValue(new int[] { 1, 2 })); + + mat = getFactory().create(new double[] { 1, 2, 3, 4, 5, 6}, + new int[] { 3, 2 }); + + Assert.assertEquals(2, mat.getDim().length); + Assert.assertEquals(3, mat.getDim(0)); + Assert.assertEquals(2, mat.getDim(1)); + Assert.assertEquals(1, (int) mat.getValue(new int[] { 0, 0 })); + Assert.assertEquals(2, (int) mat.getValue(new int[] { 0, 1 })); + Assert.assertEquals(3, (int) mat.getValue(new int[] { 1, 0 })); + Assert.assertEquals(4, (int) mat.getValue(new int[] { 1, 1 })); + Assert.assertEquals(5, (int) mat.getValue(new int[] { 2, 0 })); + Assert.assertEquals(6, (int) mat.getValue(new int[] { 2, 1 })); + + mat = getFactory().create(new double[] { 1, 2, 3 }, new int[] { 3 }); + Assert.assertEquals(1, mat.getDim().length); + Assert.assertEquals(3, mat.getDim(0)); + Assert.assertEquals(1, (int) mat.getValue(new int[] { 0 })); + Assert.assertEquals(2, (int) mat.getValue(new int[] { 1 })); + Assert.assertEquals(3, (int) mat.getValue(new int[] { 2 })); + + mat = getFactory().create(new double[] { 1, 2, 3, 4, 5, 6, 7, 8 }, + new int[] { 2, 2, 2 }); + Assert.assertNull(mat); + + } + + @Test + public void testReduceDims() throws Exception { + + MatrixND mat = null; + + mat = getFactory().create(new int[] { 6, 7, 8 }); + MatrixHelper.fill(mat, 3); + + mat = mat.sumOverDim(1); + Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); + mat = mat.reduceDims(1); + Assert.assertEquals(2, mat.getNbDim()); + Assert.assertEquals(6, mat.getDim(0)); + Assert.assertEquals(8, mat.getDim(1)); + Assert.assertEquals(21, mat.getValue(0, 0), 0); + Assert.assertEquals(21, mat.getValue(5, 7), 0); + + mat = getFactory().create(new int[] { 2, 2, 2 }); + MatrixHelper.fill(mat, 26); + + mat = mat.sumOverDim(1); + mat = mat.sumOverDim(0); + Assert.assertEquals(104, mat.getValue(0, 0, 0), 0); + mat = mat.reduceDims(1, 0); + Assert.assertEquals(1, mat.getNbDim()); + Assert.assertEquals(2, mat.getDim(0)); + Assert.assertEquals(104, mat.getValue(0), 0); + Assert.assertEquals(104, mat.getValue(1), 0); + + mat = getFactory().create(new int[] { 2, 2, 2 }); + MatrixHelper.fill(mat, 6); + + mat = mat.sumOverDim(1); + mat = mat.sumOverDim(2); + mat = mat.sumOverDim(0); + Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); + mat = mat.reduceDims(1, 2, 0); + Assert.assertEquals(1, mat.getNbDim()); + Assert.assertEquals(1, mat.getDim(0)); + Assert.assertEquals(48, mat.getValue(0), 0); + + mat = getFactory().create(new int[] { 6, 7, 8 }); + MatrixHelper.fill(mat, 3); + + mat = mat.sumOverDim(1); + Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); + mat = mat.reduceDims(0); + Assert.assertEquals(3, mat.getNbDim()); + Assert.assertEquals(6, mat.getDim(0)); + Assert.assertEquals(1, mat.getDim(1)); + Assert.assertEquals(8, mat.getDim(2)); + Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); + Assert.assertEquals(21, mat.getValue(5, 0, 7), 0); + + mat = getFactory().create(new int[] { 2, 2, 2 }); + MatrixHelper.fill(mat, 26); + + mat = mat.sumOverDim(1); + mat = mat.sumOverDim(0); + Assert.assertEquals(104, mat.getValue(0, 0, 0), 0); + mat = mat.reduceDims(1); + Assert.assertEquals(2, mat.getNbDim()); + Assert.assertEquals(1, mat.getDim(0)); + Assert.assertEquals(2, mat.getDim(1)); + Assert.assertEquals(104, mat.getValue(0, 0), 0); + Assert.assertEquals(104, mat.getValue(0, 1), 0); + + mat = getFactory().create(new int[] { 2, 2, 2 }); + MatrixHelper.fill(mat, 6); + + mat = mat.sumOverDim(1); + mat = mat.sumOverDim(2); + mat = mat.sumOverDim(0); + Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); + mat = mat.reduceDims(0, 1, 2); + Assert.assertEquals(1, mat.getNbDim()); + Assert.assertEquals(1, mat.getDim(0)); + Assert.assertEquals(48, mat.getValue(0), 0); + + } + + @Test + public void testToList() throws Exception { + MatrixND mat1 = getFactory().create(new int[] { 3, 2, 1 }); + mat1.setValue(0, 0, 0, 1.0E-7); + mat1.setValue(1, 1, 0, 2); + mat1.setValue(2, 1, 0, 5.0E-7); + List l = mat1.toList(); + System.out.println(l); + + MatrixND mat2 = getFactory().create(new int[] { 3, 2, 1 }); + mat2.fromList(l); + + System.out.println(mat1); + System.out.println(mat2); + + Assert.assertEquals(mat1, mat2); + + String s = String.valueOf(l); + List l2 = convertStringToList2(s); + List l1 = convertStringToList1(s); + List l0 = convertStringToList0(s); + + System.out.println(l); + System.out.println(l2); + System.out.println(l1); + System.out.println(l0); // implatation fausse + + Assert.assertEquals(l, l2); + + int MAX = 10000; + int dummy = 0; + { + long timeStart = System.nanoTime(); + for (int i = 0; i < MAX; i++) { + List<Object> tmp = convertStringToList1(s); + dummy += tmp.size(); + } + long time = System.nanoTime() - timeStart; + time = time / 1000000; + System.out.println(MAX + " call to convertStringToList time: " + + DurationFormatUtils.formatDuration(time, "s'.'S")); + } + { + long timeStart = System.nanoTime(); + for (int i = 0; i < MAX; i++) { + List<Object> tmp = convertStringToList2(s); + dummy += tmp.size(); + } + long time = System.nanoTime() - timeStart; + time = time / 1000000; + System.out.println(MAX + " call to convertStringToList2 time: " + + DurationFormatUtils.formatDuration(time, "s'.'S")); + } + System.out.println("dummy: " + dummy); + } + + /** + * implantation peut performante. + * + * @param s String to parse + * @return converted list + */ + public static List<Object> convertStringToList1(String s) { + List<Object> result = new ArrayList<Object>(); + String locals = s.trim(); + if (locals.startsWith("[") && locals.endsWith("]")) { + locals = locals.substring(1, locals.length() - 1); + } + String[] las = StringUtil.split(locals, ","); + for (String l : las) { + l = l.trim(); + if (l.startsWith("[")) { + // another list, recursive call + result.add(convertStringToList1(l)); + } else { + // if no list, must be number + Double d = Double.valueOf(l); + result.add(d); + } + } + return result; + } + + /** + * implantation utilisé actuellement dans MatrixHelper. + * + * @param s string to parse + * @return converted string + */ + public static List<Object> convertStringToList2(String s) { + List<Object> result = null; + Stack<List<Object>> stack = new Stack<List<Object>>(); + StringBuffer number = new StringBuffer(20); // initial to 20 char + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == ' ') { + // skip space + } + if (c == '[') { + stack.push(new ArrayList<Object>()); + } else if (c == ',') { + if (number.length() != 0) { + // on a une ',' on doit donc avoir un nombre dans number + // a moins que ce ne soit une ',' entre deux listes + Double value = Double.valueOf(number.toString()); + stack.peek().add(value); + } + number.setLength(0); + } else if (c == ']') { + // fin d'une liste, il doit rester un nombre dans number + // a mois que la liste etait vide + if (number.length() != 0) { + Double value = Double.valueOf(number.toString()); + stack.peek().add(value); + number.setLength(0); + } + + List<Object> current = stack.pop(); + if (stack.empty()) { + result = current; + } else { + stack.peek().add(current); + } + } else { + // pas un '[' ou ']', pas une ',' devrait etre + // un bout du nombre, si c un espace qui traine n'importe ou + // c pas grace car Double.valueOf gere les espaces + number.append(c); + } + } + return result; + } + + /** + * implantation fausse pour les nombres 5.0E-7. + * + * @param s string to parse + * @return converted list + * @throws IOException + */ + public static List<Object> convertStringToList0(String s) throws IOException { + List<Object> result = null; + Stack<List<Object>> stack = new Stack<List<Object>>(); + //StringBuffer number = new StringBuffer(20); // initial to 20 char + + StreamTokenizer tok = new StreamTokenizer(new StringReader(s)); + int v = tok.nextToken(); + while (v != StreamTokenizer.TT_EOF) { + if (v == '[') { + stack.push(new ArrayList<Object>()); + } else if (v == ']') { + List<Object> current = stack.pop(); + if (stack.empty()) { + result = current; + } else { + stack.peek().add(current); + } + } else if (v == StreamTokenizer.TT_NUMBER) { + double value = tok.nval; + stack.peek().add(value); + } + v = tok.nextToken(); + } + return result; + } + + @Test + public void testSemanticsArraySynthax() throws Exception { + + List<String>[] semantics = new ArrayList[3]; + semantics[0] = new ArrayList<String>(); + semantics[0].add("a"); + semantics[1] = new ArrayList<String>(); + semantics[1].add("b"); + semantics[2] = new ArrayList<String>(); + semantics[2].add("c"); + MatrixND mat = getFactory().create(semantics); + + // this don't work in 2.0.0 + // List<String> a = mat.getSemantics()[0]; + mat.setSemantic(0, new ArrayList<String>()); + } +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDDoubleVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * MatrixNDDoubleVectorTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixNDDoubleVectorTest extends MatrixNDDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDFloatBigVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDFloatBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDFloatBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * MatrixNDFloatBigVectorTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixNDFloatBigVectorTest extends MatrixNDDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(FloatBigVector.class); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDFloatVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/BasicMatrixBigTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDFloatVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDFloatVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * MatrixNDFloatVectorTest. + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class MatrixNDFloatVectorTest extends MatrixNDDoubleBigVectorTest { // BasicMatrixBigTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(FloatVector.class); + } + +} Deleted: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDTest.java =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDTest.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/MatrixNDTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -1,1098 +0,0 @@ -/* - * #%L - * NuitonMatrix - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.math.matrix; - -import java.io.IOException; -import java.io.StreamTokenizer; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Stack; - -import org.apache.commons.lang3.time.DurationFormatUtils; -import org.junit.Assert; -import org.junit.Test; -import org.nuiton.util.StringUtil; - -/** - * MatrixNDTest.java - * - * Created: 10 mai 2004 - * - * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ - */ -public class MatrixNDTest { - - public MatrixFactory getFactory() throws Exception { - return MatrixFactory.getInstance(DoubleVector.class); - } - - @Test - public void testNew() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new int[] { 3, 3, 3 }); - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }); - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - Assert.assertEquals(0.0, mat.getMaxOccurence(), 0); - - } - - @Test - public void testSemantique() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create(new int[] { 3, 3, 3 }); - - Assert.assertNull(mat.getSemantic(1).get(1)); - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }); - - // la matrice doit avoir ca propre copie des semantiques - s2.set(1, "pas bon"); - Assert.assertEquals("f", mat.getSemantic(1).get(1)); - - mat.setSemantics(1, s1); - Assert.assertEquals("b", mat.getSemantic(1).get(1)); - } - - @Test - public void testName() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - Assert.assertEquals("Ma mat", mat.getName()); - mat.setName("Renamed"); - Assert.assertEquals("Renamed", mat.getName()); - - Assert.assertEquals("dim abc", mat.getDimensionName(0)); - mat.setDimensionName(0, "dim renamed"); - Assert.assertEquals("dim renamed", mat.getDimensionName(0)); - } - - @Test - public void testGetSet() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - mat.setValue(1, 1, 1, 34); - Assert.assertEquals(34, mat.getValue("b", "f", "l"), 0); - - mat.setValue("a", "f", "m", 22); - Assert.assertEquals(22, mat.getValue(0, 1, 2), 0); - } - - @Test - public void testIterator() throws Exception { - int[][] val27 = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, - { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, { 0, 2, 0 }, - { 0, 2, 1 }, { 0, 2, 2 }, { 1, 0, 0 }, { 1, 0, 1 }, - { 1, 0, 2 }, { 1, 1, 0 }, { 1, 1, 1 }, { 1, 1, 2 }, - { 1, 2, 0 }, { 1, 2, 1 }, { 1, 2, 2 }, { 2, 0, 0 }, - { 2, 0, 1 }, { 2, 0, 2 }, { 2, 1, 0 }, { 2, 1, 1 }, - { 2, 1, 2 }, { 2, 2, 0 }, { 2, 2, 1 }, { 2, 2, 2 }, }; - - String[][] vals27 = new String[][] { { "a", "e", "k" }, - { "a", "e", "l" }, { "a", "e", "m" }, { "a", "f", "k" }, - { "a", "f", "l" }, { "a", "f", "m" }, { "a", "g", "k" }, - { "a", "g", "l" }, { "a", "g", "m" }, { "b", "e", "k" }, - { "b", "e", "l" }, { "b", "e", "m" }, { "b", "f", "k" }, - { "b", "f", "l" }, { "b", "f", "m" }, { "b", "g", "k" }, - { "b", "g", "l" }, { "b", "g", "m" }, { "c", "e", "k" }, - { "c", "e", "l" }, { "c", "e", "m" }, { "c", "f", "k" }, - { "c", "f", "l" }, { "c", "f", "m" }, { "c", "g", "k" }, - { "c", "g", "l" }, { "c", "g", "m" }, }; - - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - int cpt = 0; - for (MatrixIterator i = mat.iterator(); i.hasNext();) { - i.next(); - Assert.assertTrue(Arrays.equals(val27[cpt], i.getCoordinates())); - Assert.assertTrue(Arrays.equals(vals27[cpt], i - .getSemanticsCoordinates())); - cpt++; - } - Assert.assertEquals(27, cpt); - } - - @Test - public void testAdd() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat1 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - MatrixND mat2 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat1, 3); - MatrixHelper.fill(mat2, 26); - - mat1.add(mat2); - - Assert.assertEquals(29, mat1.getValue(0, 0, 0), 0); - Assert.assertEquals(29, mat1.getValue(1, 2, 0), 0); - Assert.assertEquals(29, mat1.getValue(2, 2, 2), 0); - } - - @Test - public void testMinus() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat1 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - MatrixND mat2 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat1, 3); - MatrixHelper.fill(mat2, 26); - - mat1.minus(mat2); - - Assert.assertEquals(-23, mat1.getValue(0, 0, 0), 0); - Assert.assertEquals(-23, mat1.getValue(1, 2, 0), 0); - Assert.assertEquals(-23, mat1.getValue(2, 2, 2), 0); - } - - @Test - public void testEquals() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat1 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - MatrixND mat2 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - MatrixND mat3 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat1, 3); - MatrixHelper.fill(mat2, 26); - MatrixHelper.fill(mat3, 29); - - mat1.add(mat2); - - Assert.assertEquals(mat1, mat3); - } - - @Test - public void testsumOverDim() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create(new int[] { 4, 4 }); - MatrixND mat2 = mat; - int i = 0; - for (MatrixIterator mi = mat.iterator(); mi.next();) { - mi.setValue(++i); - } - - Assert.assertEquals(mat, mat2); - mat2 = mat.sumOverDim(1, 0); - Assert.assertEquals(mat, mat2); - mat2 = mat.sumOverDim(1, 1); - Assert.assertEquals(mat, mat2); - mat2 = mat.sumOverDim(1, 2); - Assert.assertEquals(2, mat2.getDim(1)); - Assert.assertEquals(3, mat2.getValue(0, 0), 0); - Assert.assertEquals(7, mat2.getValue(0, 1), 0); - Assert.assertEquals(11, mat2.getValue(1, 0), 0); - Assert.assertEquals(15, mat2.getValue(1, 1), 0); - Assert.assertEquals(19, mat2.getValue(2, 0), 0); - Assert.assertEquals(23, mat2.getValue(2, 1), 0); - Assert.assertEquals(27, mat2.getValue(3, 0), 0); - Assert.assertEquals(31, mat2.getValue(3, 1), 0); - - mat2 = mat.sumOverDim(1, 3); - Assert.assertEquals(1, mat2.getDim(1)); - Assert.assertEquals(6, mat2.getValue(0, 0), 0); - Assert.assertEquals(18, mat2.getValue(1, 0), 0); - Assert.assertEquals(30, mat2.getValue(2, 0), 0); - Assert.assertEquals(42, mat2.getValue(3, 0), 0); - - mat2 = mat.sumOverDim(1, 4); - Assert.assertEquals(1, mat2.getDim(1)); - Assert.assertEquals(10, mat2.getValue(0, 0), 0); - Assert.assertEquals(26, mat2.getValue(1, 0), 0); - Assert.assertEquals(42, mat2.getValue(2, 0), 0); - Assert.assertEquals(58, mat2.getValue(3, 0), 0); - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat, 3); - mat.setValue(0, 0, 0, 2); - mat.setValue(2, 2, 2, 4); - mat = mat.sumOverDim(1); - Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 3, 1, 3 }, mat - .getDim())); - - Assert.assertEquals(8, mat.getValue(0, 0, 0), 0); - Assert.assertEquals(9, mat.getValue(2, 0, 1), 0); - Assert.assertEquals(10, mat.getValue(2, 0, 2), 0); - - mat = getFactory().create(new int[] { 6, 6, 6 }); - - MatrixHelper.fill(mat, 3); - mat.setValue(0, 0, 0, 0); - mat.setValue(0, 1, 0, 1); - mat.setValue(0, 2, 0, 2); - mat.setValue(0, 3, 0, 3); - mat.setValue(0, 4, 0, 4); - mat.setValue(0, 5, 0, 5); - - mat = mat.sumOverDim(1, 3); - Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 6, 2, 6 }, mat - .getDim())); - Assert.assertEquals(3, mat.getValue(0, 0, 0), 0); - Assert.assertEquals(12, mat.getValue(0, 1, 0), 0); - Assert.assertEquals(9, mat.getValue(1, 1, 5), 0); - Assert.assertEquals(9, mat.getValue(5, 0, 3), 0); - } - - /** - * Test les fonctions meanOverDim... - * - * La matrice de test est - * 1 2 3 4 - * 5 6 7 8 - * 9 10 11 12 - * 13 14 15 16 - * @throws Exception - */ - @Test - public void testMeanOverDim() throws Exception { - - // first test matrix : - // 1 2 3 4 - // 5 6 7 8 - // 9 10 11 12 - // 13 14 15 16 - - MatrixND mat = null; - mat = getFactory().create(new int[] { 4, 4 }); - MatrixND mat2 = mat; - int i = 0; - for (MatrixIterator mi = mat.iterator(); mi.next();) { - mi.setValue(++i); - } - - Assert.assertEquals(mat, mat2); - mat2 = mat.meanOverDim(1, 0); - Assert.assertEquals(mat, mat2); - mat2 = mat.meanOverDim(1, 1); - Assert.assertEquals(mat, mat2); - mat2 = mat.meanOverDim(1, 2); - Assert.assertEquals(2, mat2.getDim(1)); - Assert.assertEquals(1.5, mat2.getValue(0, 0), 0); - Assert.assertEquals(3.5, mat2.getValue(0, 1), 0); - Assert.assertEquals(5.5, mat2.getValue(1, 0), 0); - Assert.assertEquals(7.5, mat2.getValue(1, 1), 0); - Assert.assertEquals(9.5, mat2.getValue(2, 0), 0); - Assert.assertEquals(11.5, mat2.getValue(2, 1), 0); - Assert.assertEquals(13.5, mat2.getValue(3, 0), 0); - Assert.assertEquals(15.5, mat2.getValue(3, 1), 0); - - mat2 = mat.meanOverDim(1, 3); - Assert.assertEquals(1, mat2.getDim(1)); - Assert.assertEquals(2.0, mat2.getValue(0, 0), 0); - Assert.assertEquals(6.0, mat2.getValue(1, 0), 0); - Assert.assertEquals(10.0, mat2.getValue(2, 0), 0); - Assert.assertEquals(14.0, mat2.getValue(3, 0), 0); - - mat2 = mat.meanOverDim(1, 4); - Assert.assertEquals(1, mat2.getDim(1)); - Assert.assertEquals(2.5, mat2.getValue(0, 0), 0); - Assert.assertEquals(6.5, mat2.getValue(1, 0), 0); - Assert.assertEquals(10.5, mat2.getValue(2, 0), 0); - Assert.assertEquals(14.5, mat2.getValue(3, 0), 0); - - // meanAll - double meanAll = mat.meanAll(); - Assert.assertEquals(8.5, meanAll, 0); - - // seconde matrice (en 3 dimension :D) - // 2 3 3 3 3 3 3 3 3 - // 3 3 3 3 3 3 3 3 3 - // 3 3 3 3 3 3 3 3 4 - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat, 3); - mat.setValue(0, 0, 0, 2); - mat.setValue(2, 2, 2, 4); - mat = mat.meanOverDim(1); - // donne : - // 2.66 3 3 3 3 3 3 3 3.33 - Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 3, 1, 3 }, mat - .getDim())); - - Assert.assertEquals(2.6666, mat.getValue(0, 0, 0), 0.0001); - Assert.assertEquals(3, mat.getValue(2, 0, 1), 0); - Assert.assertEquals(3.3333, mat.getValue(2, 0, 2), 0.0001); - - mat = getFactory().create(new int[] { 6, 6, 6 }); - - MatrixHelper.fill(mat, 3); - mat.setValue(0, 0, 0, 0); - mat.setValue(0, 1, 0, 1); - mat.setValue(0, 2, 0, 2); - mat.setValue(0, 3, 0, 3); - mat.setValue(0, 4, 0, 4); - mat.setValue(0, 5, 0, 5); - - // meanAll - meanAll = mat.meanAll(); - Assert.assertEquals(2.9861, meanAll, 0.0001); - - mat = mat.meanOverDim(1, 3); - Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 6, 2, 6 }, mat - .getDim())); - Assert.assertEquals(1.0, mat.getValue(0, 0, 0), 0); - Assert.assertEquals(4.0, mat.getValue(0, 1, 0), 0); - Assert.assertEquals(3.0, mat.getValue(1, 1, 5), 0); - Assert.assertEquals(3.0, mat.getValue(5, 0, 3), 0); - } - - /** - * Test de reduction simple par indice. - * - * @throws Exception - */ - @Test - public void testSubMatrixIntIntArray() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - // reduction de "e", "f", "g" en "f", "g" - MatrixND mat2 = mat.getSubMatrix(1, new int[]{1,2}); - Assert.assertEquals(Arrays.asList(new String[]{"f","g"}), mat2.getSemantic(1)); - mat2 = mat2.getSubMatrix(2, new int[]{2}); - Assert.assertEquals(Arrays.asList(new String[]{"m"}), mat2.getSemantic(2)); - } - - /** - * Test de reduction simple par semantic. - * - * @throws Exception - */ - @Test - public void testSubMatrixIntObjectArray() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - // reduction de "e", "f", "g" en "f", "g" - MatrixND mat2 = mat.getSubMatrix(1, "f", "g"); - Assert.assertEquals(Arrays.asList(new String[]{"f","g"}), mat2.getSemantic(1)); - mat2 = mat2.getSubMatrix(2, "m"); - Assert.assertEquals(Arrays.asList(new String[]{"m"}), mat2.getSemantic(2)); - } - - /** - * Test de reduction multiple par indices. - * - * @throws Exception - */ - @Test - public void testSubMatrixIntArrayArray() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - // reduction de "e", "f", "g" en "f", "g" et "k", "l", "m" en "m" - MatrixND mat2 = mat.getSubMatrix(new int[][]{new int[]{0, 1, 2}, new int[]{1,2}, new int[]{2}}); - Assert.assertEquals(Arrays.asList(new String[]{"f","g"}), mat2.getSemantic(1)); - Assert.assertEquals(Arrays.asList(new String[]{"m"}), mat2.getSemantic(2)); - - // meme test avec des null - mat2 = mat.getSubMatrix(new int[][]{null, new int[]{1,2}, new int[]{2}}); - Assert.assertEquals(Arrays.asList(new String[]{"f","g"}), mat2.getSemantic(1)); - Assert.assertEquals(Arrays.asList(new String[]{"m"}), mat2.getSemantic(2)); - } - - /** - * Test de reduction sur le mauvais nombre de dimension. - * - * @throws Exception - */ - @Test(expected=IllegalArgumentException.class) - public void testSubMatrixIntArrayArrayException() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - // reduction de "e", "f", "g" en "f", "g" - mat.getSubMatrix(new int[][]{null}); - } - - @Test - public void testTranspose() throws Exception { - MatrixND mat = null; - - mat = getFactory().create(new int[] { 2, 4 }); - mat.setValue(1, 3, 56); - mat.setValue(0, 2, 34); - mat.setValue(0, 1, 64); - mat.setValue(1, 0, 46); - - mat = mat.transpose(); - Assert.assertEquals(56, mat.getValue(3, 1), 0); - Assert.assertEquals(34, mat.getValue(2, 0), 0); - Assert.assertEquals(64, mat.getValue(1, 0), 0); - Assert.assertEquals(46, mat.getValue(0, 1), 0); - - mat = getFactory().create(new int[] { 4 }); - mat.setValue(1, 56); - mat.setValue(2, 34); - mat.setValue(3, 64); - mat.setValue(0, 46); - - mat = mat.transpose(); - Assert.assertEquals(56, mat.getValue(0, 1), 0); - Assert.assertEquals(34, mat.getValue(0, 2), 0); - Assert.assertEquals(64, mat.getValue(0, 3), 0); - Assert.assertEquals(46, mat.getValue(0, 0), 0); - } - - @Test - public void testMults() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat1 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - MatrixND mat2 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat1, 3); - MatrixHelper.fill(mat2, 26); - - mat1.mults(3); - mat2.mults(0); - - Assert.assertEquals(9, mat1.getValue(0, 0, 0), 0); - Assert.assertEquals(9, mat1.getValue(1, 2, 0), 0); - Assert.assertEquals(0, mat2.getValue(2, 2, 2), 0); - - MatrixND mat0 = getFactory().create(new int[] { 4, 4 }); - MatrixND matId = getFactory().matrixId(4); - matId.mults(0); - Assert.assertEquals(mat0, matId); - } - - @Test - public void testDivs() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat1 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - MatrixND mat2 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat1, 3); - MatrixHelper.fill(mat2, 26); - - mat1.divs(3); - mat2.divs(4); - - Assert.assertEquals(1, mat1.getValue(0, 0, 0), 0); - Assert.assertEquals(6.5, mat2.getValue(1, 2, 0), 0); - Assert.assertEquals(6.5, mat2.getValue(2, 2, 2), 0); - } - - @Test - public void testAdds() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat1 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - MatrixND mat2 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat1, 3); - MatrixHelper.fill(mat2, 26); - - mat1.adds(3); - mat2.adds(4); - - Assert.assertEquals(6, mat1.getValue(0, 0, 0), 0); - Assert.assertEquals(30, mat2.getValue(1, 2, 0), 0); - Assert.assertEquals(30, mat2.getValue(2, 2, 2), 0); - } - - @Test - public void testMinuss() throws Exception { - List<String> s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List<String> s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List<String> s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat1 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - MatrixND mat2 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat1, 3); - MatrixHelper.fill(mat2, 26); - - mat1.minuss(3); - mat2.minuss(4); - - Assert.assertEquals(0, mat1.getValue(0, 0, 0), 0); - Assert.assertEquals(22, mat2.getValue(1, 2, 0), 0); - Assert.assertEquals(22, mat2.getValue(2, 2, 2), 0); - } - - @Test - public void testPaste() throws Exception { - - MatrixND mat1 = getFactory().create(new int[] { 6, 7, 8 }); - MatrixND mat2 = getFactory().create(new int[] { 2, 2, 2 }); - - MatrixHelper.fill(mat1, 3); - MatrixHelper.fill(mat2, 26); - - mat1.paste(new int[] { 3, 0, 4 }, mat2); - - Assert.assertEquals(3, mat1.getValue(0, 0, 0), 0); - Assert.assertEquals(26, mat1.getValue(3, 0, 4), 0); - Assert.assertEquals(26, mat1.getValue(4, 1, 5), 0); - Assert.assertEquals(3, mat1.getValue(5, 2, 6), 0); - } - - @Test - public void testSumOverDim() throws Exception { - - MatrixND mat = null; - - mat = getFactory().create(new int[] { 6, 7 }); - MatrixND result = getFactory().create(new int[] { 3, 7 }); - - MatrixHelper.fill(mat, 3); - - MatrixND sub = mat.getSubMatrix(0, 0, 1); - result.paste(new int[] { 0, 0 }, sub.sumOverDim(0)); - - sub = mat.getSubMatrix(0, 1, 3); - result.paste(new int[] { 1, 0 }, sub.sumOverDim(0)); - - sub = mat.getSubMatrix(0, 4, 2); - result.paste(new int[] { 2, 0 }, sub.sumOverDim(0)); - - mat = getFactory().create(new int[] { 6, 7 }); - MatrixHelper.fill(mat, 3); - - mat = mat.sumOverDim(0, 0, 1); - mat = mat.sumOverDim(0, 1, 3); - mat = mat.sumOverDim(0, 2, 2); - - Assert.assertTrue(result.equalsValues(mat)); - } - - @Test - public void testReduce() throws Exception { - - MatrixND mat = null; - - mat = getFactory().create(new int[] { 6, 7, 8 }); - MatrixHelper.fill(mat, 3); - - mat = mat.sumOverDim(1); - Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); - mat = mat.reduce(); - Assert.assertEquals(2, mat.getNbDim()); - Assert.assertEquals(6, mat.getDim(0)); - Assert.assertEquals(8, mat.getDim(1)); - Assert.assertEquals(21, mat.getValue(0, 0), 0); - Assert.assertEquals(21, mat.getValue(5, 7), 0); - - mat = getFactory().create(new int[] { 2, 2, 2 }); - MatrixHelper.fill(mat, 26); - - mat = mat.sumOverDim(1); - mat = mat.sumOverDim(0); - Assert.assertEquals(104, mat.getValue(0, 0, 0), 0); - mat = mat.reduce(); - Assert.assertEquals(1, mat.getNbDim()); - Assert.assertEquals(2, mat.getDim(0)); - Assert.assertEquals(104, mat.getValue(0), 0); - Assert.assertEquals(104, mat.getValue(1), 0); - - mat = getFactory().create(new int[] { 2, 2, 2 }); - MatrixHelper.fill(mat, 6); - - mat = mat.sumOverDim(1); - mat = mat.sumOverDim(2); - mat = mat.sumOverDim(0); - Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); - mat = mat.reduce(); - Assert.assertEquals(1, mat.getNbDim()); - Assert.assertEquals(1, mat.getDim(0)); - Assert.assertEquals(48, mat.getValue(0), 0); - - mat = getFactory().create(new int[] { 6, 7, 8 }); - MatrixHelper.fill(mat, 3); - - mat = mat.sumOverDim(1); - Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); - mat = mat.reduce(2); - Assert.assertEquals(2, mat.getNbDim()); - Assert.assertEquals(6, mat.getDim(0)); - Assert.assertEquals(8, mat.getDim(1)); - Assert.assertEquals(21, mat.getValue(0, 0), 0); - Assert.assertEquals(21, mat.getValue(5, 7), 0); - - mat = getFactory().create(new int[] { 2, 2, 2 }); - MatrixHelper.fill(mat, 26); - - mat = mat.sumOverDim(1); - mat = mat.sumOverDim(0); - Assert.assertEquals(104, mat.getValue(0, 0, 0), 0); - mat = mat.reduce(2); - Assert.assertEquals(2, mat.getNbDim()); - Assert.assertEquals(1, mat.getDim(0)); - Assert.assertEquals(2, mat.getDim(1)); - Assert.assertEquals(104, mat.getValue(0, 0), 0); - Assert.assertEquals(104, mat.getValue(0, 1), 0); - - mat = getFactory().create(new int[] { 2, 2, 2 }); - MatrixHelper.fill(mat, 6); - - mat = mat.sumOverDim(1); - mat = mat.sumOverDim(2); - mat = mat.sumOverDim(0); - Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); - mat = mat.reduce(5); - Assert.assertEquals(3, mat.getNbDim()); - Assert.assertEquals(1, mat.getDim(0)); - Assert.assertEquals(1, mat.getDim(1)); - Assert.assertEquals(1, mat.getDim(2)); - Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); - - } - - @Test - public void testCreateFromDoubleArray() throws Exception { - MatrixND mat = null; - - mat = getFactory().create(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, - new int[] { 3, 3 }); - - Assert.assertEquals(2, mat.getDim().length); - Assert.assertEquals(3, mat.getDim(0)); - Assert.assertEquals(3, mat.getDim(1)); - Assert.assertEquals(1, (int) mat.getValue(new int[] { 0, 0 })); - Assert.assertEquals(2, (int) mat.getValue(new int[] { 0, 1 })); - Assert.assertEquals(3, (int) mat.getValue(new int[] { 0, 2 })); - Assert.assertEquals(4, (int) mat.getValue(new int[] { 1, 0 })); - Assert.assertEquals(5, (int) mat.getValue(new int[] { 1, 1 })); - Assert.assertEquals(6, (int) mat.getValue(new int[] { 1, 2 })); - Assert.assertEquals(7, (int) mat.getValue(new int[] { 2, 0 })); - Assert.assertEquals(8, (int) mat.getValue(new int[] { 2, 1 })); - Assert.assertEquals(9, (int) mat.getValue(new int[] { 2, 2 })); - - mat = getFactory().create(new double[] { 1, 2, 3, 4, 5, 6}, - new int[] { 2, 3 }); - - Assert.assertEquals(2, mat.getDim().length); - Assert.assertEquals(2, mat.getDim(0)); - Assert.assertEquals(3, mat.getDim(1)); - Assert.assertEquals(1, (int) mat.getValue(new int[] { 0, 0 })); - Assert.assertEquals(2, (int) mat.getValue(new int[] { 0, 1 })); - Assert.assertEquals(3, (int) mat.getValue(new int[] { 0, 2 })); - Assert.assertEquals(4, (int) mat.getValue(new int[] { 1, 0 })); - Assert.assertEquals(5, (int) mat.getValue(new int[] { 1, 1 })); - Assert.assertEquals(6, (int) mat.getValue(new int[] { 1, 2 })); - - mat = getFactory().create(new double[] { 1, 2, 3, 4, 5, 6}, - new int[] { 3, 2 }); - - Assert.assertEquals(2, mat.getDim().length); - Assert.assertEquals(3, mat.getDim(0)); - Assert.assertEquals(2, mat.getDim(1)); - Assert.assertEquals(1, (int) mat.getValue(new int[] { 0, 0 })); - Assert.assertEquals(2, (int) mat.getValue(new int[] { 0, 1 })); - Assert.assertEquals(3, (int) mat.getValue(new int[] { 1, 0 })); - Assert.assertEquals(4, (int) mat.getValue(new int[] { 1, 1 })); - Assert.assertEquals(5, (int) mat.getValue(new int[] { 2, 0 })); - Assert.assertEquals(6, (int) mat.getValue(new int[] { 2, 1 })); - - mat = getFactory().create(new double[] { 1, 2, 3 }, new int[] { 3 }); - Assert.assertEquals(1, mat.getDim().length); - Assert.assertEquals(3, mat.getDim(0)); - Assert.assertEquals(1, (int) mat.getValue(new int[] { 0 })); - Assert.assertEquals(2, (int) mat.getValue(new int[] { 1 })); - Assert.assertEquals(3, (int) mat.getValue(new int[] { 2 })); - - mat = getFactory().create(new double[] { 1, 2, 3, 4, 5, 6, 7, 8 }, - new int[] { 2, 2, 2 }); - Assert.assertNull(mat); - - } - - @Test - public void testReduceDims() throws Exception { - - MatrixND mat = null; - - mat = getFactory().create(new int[] { 6, 7, 8 }); - MatrixHelper.fill(mat, 3); - - mat = mat.sumOverDim(1); - Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); - mat = mat.reduceDims(1); - Assert.assertEquals(2, mat.getNbDim()); - Assert.assertEquals(6, mat.getDim(0)); - Assert.assertEquals(8, mat.getDim(1)); - Assert.assertEquals(21, mat.getValue(0, 0), 0); - Assert.assertEquals(21, mat.getValue(5, 7), 0); - - mat = getFactory().create(new int[] { 2, 2, 2 }); - MatrixHelper.fill(mat, 26); - - mat = mat.sumOverDim(1); - mat = mat.sumOverDim(0); - Assert.assertEquals(104, mat.getValue(0, 0, 0), 0); - mat = mat.reduceDims(1, 0); - Assert.assertEquals(1, mat.getNbDim()); - Assert.assertEquals(2, mat.getDim(0)); - Assert.assertEquals(104, mat.getValue(0), 0); - Assert.assertEquals(104, mat.getValue(1), 0); - - mat = getFactory().create(new int[] { 2, 2, 2 }); - MatrixHelper.fill(mat, 6); - - mat = mat.sumOverDim(1); - mat = mat.sumOverDim(2); - mat = mat.sumOverDim(0); - Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); - mat = mat.reduceDims(1, 2, 0); - Assert.assertEquals(1, mat.getNbDim()); - Assert.assertEquals(1, mat.getDim(0)); - Assert.assertEquals(48, mat.getValue(0), 0); - - mat = getFactory().create(new int[] { 6, 7, 8 }); - MatrixHelper.fill(mat, 3); - - mat = mat.sumOverDim(1); - Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); - mat = mat.reduceDims(0); - Assert.assertEquals(3, mat.getNbDim()); - Assert.assertEquals(6, mat.getDim(0)); - Assert.assertEquals(1, mat.getDim(1)); - Assert.assertEquals(8, mat.getDim(2)); - Assert.assertEquals(21, mat.getValue(0, 0, 0), 0); - Assert.assertEquals(21, mat.getValue(5, 0, 7), 0); - - mat = getFactory().create(new int[] { 2, 2, 2 }); - MatrixHelper.fill(mat, 26); - - mat = mat.sumOverDim(1); - mat = mat.sumOverDim(0); - Assert.assertEquals(104, mat.getValue(0, 0, 0), 0); - mat = mat.reduceDims(1); - Assert.assertEquals(2, mat.getNbDim()); - Assert.assertEquals(1, mat.getDim(0)); - Assert.assertEquals(2, mat.getDim(1)); - Assert.assertEquals(104, mat.getValue(0, 0), 0); - Assert.assertEquals(104, mat.getValue(0, 1), 0); - - mat = getFactory().create(new int[] { 2, 2, 2 }); - MatrixHelper.fill(mat, 6); - - mat = mat.sumOverDim(1); - mat = mat.sumOverDim(2); - mat = mat.sumOverDim(0); - Assert.assertEquals(48, mat.getValue(0, 0, 0), 0); - mat = mat.reduceDims(0, 1, 2); - Assert.assertEquals(1, mat.getNbDim()); - Assert.assertEquals(1, mat.getDim(0)); - Assert.assertEquals(48, mat.getValue(0), 0); - - } - - @Test - public void testToList() throws Exception { - MatrixND mat1 = getFactory().create(new int[] { 3, 2, 1 }); - mat1.setValue(0, 0, 0, 1.0E-7); - mat1.setValue(1, 1, 0, 2); - mat1.setValue(2, 1, 0, 5.0E-7); - List l = mat1.toList(); - System.out.println(l); - - MatrixND mat2 = getFactory().create(new int[] { 3, 2, 1 }); - mat2.fromList(l); - - System.out.println(mat1); - System.out.println(mat2); - - Assert.assertEquals(mat1, mat2); - - String s = String.valueOf(l); - List l2 = convertStringToList2(s); - List l1 = convertStringToList1(s); - List l0 = convertStringToList0(s); - - System.out.println(l); - System.out.println(l2); - System.out.println(l1); - System.out.println(l0); // implatation fausse - - Assert.assertEquals(l, l2); - - int MAX = 10000; - int dummy = 0; - { - long timeStart = System.nanoTime(); - for (int i = 0; i < MAX; i++) { - List<Object> tmp = convertStringToList1(s); - dummy += tmp.size(); - } - long time = System.nanoTime() - timeStart; - time = time / 1000000; - System.out.println(MAX + " call to convertStringToList time: " - + DurationFormatUtils.formatDuration(time, "s'.'S")); - } - { - long timeStart = System.nanoTime(); - for (int i = 0; i < MAX; i++) { - List<Object> tmp = convertStringToList2(s); - dummy += tmp.size(); - } - long time = System.nanoTime() - timeStart; - time = time / 1000000; - System.out.println(MAX + " call to convertStringToList2 time: " - + DurationFormatUtils.formatDuration(time, "s'.'S")); - } - System.out.println("dummy: " + dummy); - } - - /** - * implantation peut performante. - * - * @param s String to parse - * @return converted list - */ - public static List<Object> convertStringToList1(String s) { - List<Object> result = new ArrayList<Object>(); - String locals = s.trim(); - if (locals.startsWith("[") && locals.endsWith("]")) { - locals = locals.substring(1, locals.length() - 1); - } - String[] las = StringUtil.split(locals, ","); - for (String l : las) { - l = l.trim(); - if (l.startsWith("[")) { - // another list, recursive call - result.add(convertStringToList1(l)); - } else { - // if no list, must be number - Double d = Double.valueOf(l); - result.add(d); - } - } - return result; - } - - /** - * implantation utilisé actuellement dans MatrixHelper. - * - * @param s string to parse - * @return converted string - */ - public static List<Object> convertStringToList2(String s) { - List<Object> result = null; - Stack<List<Object>> stack = new Stack<List<Object>>(); - StringBuffer number = new StringBuffer(20); // initial to 20 char - - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (c == ' ') { - // skip space - } - if (c == '[') { - stack.push(new ArrayList<Object>()); - } else if (c == ',') { - if (number.length() != 0) { - // on a une ',' on doit donc avoir un nombre dans number - // a moins que ce ne soit une ',' entre deux listes - Double value = Double.valueOf(number.toString()); - stack.peek().add(value); - } - number.setLength(0); - } else if (c == ']') { - // fin d'une liste, il doit rester un nombre dans number - // a mois que la liste etait vide - if (number.length() != 0) { - Double value = Double.valueOf(number.toString()); - stack.peek().add(value); - number.setLength(0); - } - - List<Object> current = stack.pop(); - if (stack.empty()) { - result = current; - } else { - stack.peek().add(current); - } - } else { - // pas un '[' ou ']', pas une ',' devrait etre - // un bout du nombre, si c un espace qui traine n'importe ou - // c pas grace car Double.valueOf gere les espaces - number.append(c); - } - } - return result; - } - - /** - * implantation fausse pour les nombres 5.0E-7. - * - * @param s string to parse - * @return converted list - * @throws IOException - */ - public static List<Object> convertStringToList0(String s) throws IOException { - List<Object> result = null; - Stack<List<Object>> stack = new Stack<List<Object>>(); - //StringBuffer number = new StringBuffer(20); // initial to 20 char - - StreamTokenizer tok = new StreamTokenizer(new StringReader(s)); - int v = tok.nextToken(); - while (v != StreamTokenizer.TT_EOF) { - if (v == '[') { - stack.push(new ArrayList<Object>()); - } else if (v == ']') { - List<Object> current = stack.pop(); - if (stack.empty()) { - result = current; - } else { - stack.peek().add(current); - } - } else if (v == StreamTokenizer.TT_NUMBER) { - double value = tok.nval; - stack.peek().add(value); - } - v = tok.nextToken(); - } - return result; - } - - @Test - public void testSemanticsArraySynthax() throws Exception { - - List<String>[] semantics = new ArrayList[3]; - semantics[0] = new ArrayList<String>(); - semantics[0].add("a"); - semantics[1] = new ArrayList<String>(); - semantics[1].add("b"); - semantics[2] = new ArrayList<String>(); - semantics[2].add("c"); - MatrixND mat = getFactory().create(semantics); - - // this don't work in 2.0.0 - // List<String> a = mat.getSemantics()[0]; - mat.setSemantic(0, new ArrayList<String>()); - } -} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleBigMappedVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleBigMappedVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleBigMappedVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * PerfDoubleBigMappedVectorTest.java + * + * Created: 30 aout 2012 + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class PerfDoubleBigMappedVectorTest extends PerfDoubleBigVectorTest { + + @Override + protected Class<? extends Vector> getVectorClass() throws Exception { + return DoubleBigMappedVector.class; + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleBigVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,182 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.lang3.time.DurationFormatUtils; +import org.junit.Test; +import org.nuiton.util.StringUtil; + +/** + * PerfDoubleBigVectorTest.java + * + * Created: 7 sept. 06 02:28:51 + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class PerfDoubleBigVectorTest { + + protected static final char CMAX = 'j'; + protected static final int MAX = 10; + + protected Class<? extends Vector> getVectorClass() throws Exception { + return DoubleBigVector.class; + } + + protected List<String> getSem() { + List<String> sem = new ArrayList<String>(); + for (char c = 'a'; c <= CMAX; c++) { + for (int i = 1; i <= MAX; i++) { + sem.add(c + " " + i); + } + } + return sem; + } + + @Test + public void testDoubleVectorIterator() throws Exception { + List<String> sem = getSem(); + + // [ 2600 x 2600 x 2600 ] + System.gc(); // force gc now to minimize gc call during timing + long timestart = System.nanoTime(); + MatrixND mat = MatrixFactory.getInstance(getVectorClass()).create("Ma mat", + new List[] { sem, sem, sem }); + long timeinit = System.nanoTime(); + // parcours avec iterator + long cpt = 1; + for (MatrixIterator i = mat.iterator(); i.hasNext();) { + i.next(); + i.setValue(cpt++); + } + long timeend = System.nanoTime(); + + long freeMem = Runtime.getRuntime().freeMemory(); + long totalMem = Runtime.getRuntime().totalMemory(); + + long vectorSize = ((MatrixNDImpl)mat).getInternalMatrix().getInternalVector().size(); + + System.out.println( + "Perf " + getVectorClass().getSimpleName() + "(" + + vectorSize + ")" + + "\tIterator\tinit: " + + DurationFormatUtils.formatDuration( + (timeinit - timestart) / 1000000, "s'.'S") + + " fill: " + + DurationFormatUtils.formatDuration( + (timeend - timeinit) / 1000000, "s'.'S") + + " memory: " + StringUtil.convertMemory(totalMem - freeMem) + "/" + + StringUtil.convertMemory(totalMem)); + } + + @Test + public void testDoubleVectorIndex() throws Exception { + List<String> sem = getSem(); + int size = sem.size(); + + // [ 2600 x 2600 x 2600 ] + System.gc(); // force gc now to minimize gc call during timing + long timestart = System.nanoTime(); + MatrixND mat = MatrixFactory.getInstance(getVectorClass()).create("Ma mat", + new List[] { sem, sem, sem }); + long timeinit = System.nanoTime(); + // parcours avec iterator + long cpt = 1; + for (int x = 0; x < size; x++) { + for (int y = 0; y < size; y++) { + for (int z = 0; z < size; z++) { + mat.setValue(x, y, z, cpt++); + } + } + } + long timeend = System.nanoTime(); + + long freeMem = Runtime.getRuntime().freeMemory(); + long totalMem = Runtime.getRuntime().totalMemory(); + + long vectorSize = ((MatrixNDImpl)mat).getInternalMatrix().getInternalVector().size(); + + System.out.println( + "Perf " + getVectorClass().getSimpleName() + "(" + + vectorSize + ")" + + "\tIndex\t\tinit: " + DurationFormatUtils.formatDuration( + (timeinit - timestart) / 1000000, "s'.'S") + + " fill: " + + DurationFormatUtils.formatDuration( + (timeend - timeinit) / 1000000, "s'.'S") + + " memory: " + StringUtil.convertMemory(totalMem - freeMem) + "/" + + StringUtil.convertMemory(totalMem)); + } + + @Test + public void testDoubleVectorSemantic() throws Exception { + List<String> sem = getSem(); + int size = sem.size(); + + // [ 2600 x 2600 x 2600 ] + System.gc(); // force gc now to minimize gc call during timing + long timestart = System.nanoTime(); + MatrixND mat = MatrixFactory.getInstance(getVectorClass()).create("Ma mat", + new List[] { sem, sem, sem }); + long timeinit = System.nanoTime(); + // parcours avec iterator + long cpt = 1; + for (int x = 0; x < size; x++) { + for (int y = 0; y < size; y++) { + for (int z = 0; z < size; z++) { + String X = sem.get(x); + String Y = sem.get(y); + String Z = sem.get(z); + mat.setValue(X, Y, Z, cpt++); + } + } + } + long timeend = System.nanoTime(); + + long freeMem = Runtime.getRuntime().freeMemory(); + long totalMem = Runtime.getRuntime().totalMemory(); + + long vectorSize = ((MatrixNDImpl)mat).getInternalMatrix().getInternalVector().size(); + + System.out.println( + "Perf " + getVectorClass().getSimpleName() + "(" + + vectorSize + ")" + + "\tSemantic\tinit: " + + DurationFormatUtils.formatDuration( + (timeinit - timestart) / 1000000, "s'.'S") + + " fill: " + + DurationFormatUtils.formatDuration( + (timeend - timeinit) / 1000000, "s'.'S") + + " memory: " + StringUtil.convertMemory(totalMem - freeMem) + "/" + + StringUtil.convertMemory(totalMem)); + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfDoubleVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * PerfDoubleVectorTest.java + * + * Created: 30 aout 2012 + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class PerfDoubleVectorTest extends PerfDoubleBigVectorTest { + + @Override + protected Class<? extends Vector> getVectorClass() throws Exception { + return DoubleVector.class; + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfFloatBigVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfFloatBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfFloatBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * PerfFloatBigVectorTest.java + * + * Created: 30 aout 2012 + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class PerfFloatBigVectorTest extends PerfDoubleBigVectorTest { + + @Override + protected Class<? extends Vector> getVectorClass() throws Exception { + return FloatBigVector.class; + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfFloatVectorTest.java (from rev 437, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfFloatVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfFloatVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,46 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * PerfFloatVectorTest.java + * + * Created: 30 aout 2012 + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class PerfFloatVectorTest extends PerfDoubleBigVectorTest { + + @Override + protected Class<? extends Vector> getVectorClass() throws Exception { + return FloatVector.class; + } + +} Deleted: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/PerfTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -1,478 +0,0 @@ -/* - * #%L - * NuitonMatrix - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2012 CodeLutin, Chatellier Eric - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.math.matrix; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.commons.lang3.time.DurationFormatUtils; -import org.junit.Test; - -/** - * PerfTest.java - * - * Created: 7 sept. 06 02:28:51 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ -public class PerfTest { - - protected static final char CMAX = 'j'; - protected static final int MAX = 10; - - @Test - public void testDoubleVectorIterator() throws Exception { - List sem = new ArrayList(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(DoubleVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (MatrixIterator i = mat.iterator(); i.hasNext();) { - i.next(); - i.setValue(cpt++); - } - long timeend = System.nanoTime(); - - System.out.println("Iterator DoubleVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testDoubleVectorIndex() throws Exception { - List sem = new ArrayList(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - int size = sem.size(); - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(DoubleVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - for (int z = 0; z < size; z++) { - mat.setValue(x, y, z, cpt++); - } - } - } - long timeend = System.nanoTime(); - - System.out.println("Index DoubleVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testDoubleVectorSemantic() throws Exception { - List<String> sem = new ArrayList<String>(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - int size = sem.size(); - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(DoubleVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - for (int z = 0; z < size; z++) { - String X = sem.get(x); - String Y = sem.get(y); - String Z = sem.get(z); - mat.setValue(X, Y, Z, cpt++); - } - } - } - long timeend = System.nanoTime(); - - System.out.println("Semantic DoubleVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testDoubleBigVectorIterator() throws Exception { - List sem = new ArrayList(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(DoubleBigVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (MatrixIterator i = mat.iterator(); i.hasNext();) { - i.next(); - i.setValue(cpt++); - } - long timeend = System.nanoTime(); - - System.out.println("Iterator DoubleBigVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testDoubleBigVectorIndex() throws Exception { - List sem = new ArrayList(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - int size = sem.size(); - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(DoubleBigVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - for (int z = 0; z < size; z++) { - mat.setValue(x, y, z, cpt++); - } - } - } - long timeend = System.nanoTime(); - - System.out.println("Index DoubleBigVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testDoubleBigVectorSemantic() throws Exception { - List<String> sem = new ArrayList<String>(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - int size = sem.size(); - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(DoubleBigVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - for (int z = 0; z < size; z++) { - String X = sem.get(x); - String Y = sem.get(y); - String Z = sem.get(z); - mat.setValue(X, Y, Z, cpt++); - } - } - } - long timeend = System.nanoTime(); - - System.out.println("Semantic DoubleBigVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testFloatVectorIterator() throws Exception { - List sem = new ArrayList(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(FloatVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (MatrixIterator i = mat.iterator(); i.hasNext();) { - i.next(); - i.setValue(cpt++); - } - long timeend = System.nanoTime(); - - System.out.println("Iterator FloatVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testFloatVectorIndex() throws Exception { - List sem = new ArrayList(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - int size = sem.size(); - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(FloatVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - for (int z = 0; z < size; z++) { - mat.setValue(x, y, z, cpt++); - } - } - } - long timeend = System.nanoTime(); - - System.out.println("Index FloatVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testFloatVectorSemantic() throws Exception { - List<String> sem = new ArrayList<String>(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - int size = sem.size(); - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(FloatVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - for (int z = 0; z < size; z++) { - String X = sem.get(x); - String Y = sem.get(y); - String Z = sem.get(z); - mat.setValue(X, Y, Z, cpt++); - } - } - } - long timeend = System.nanoTime(); - - System.out.println("Semantic FloatVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testFloatBigVectorIterator() throws Exception { - List sem = new ArrayList(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(FloatBigVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (MatrixIterator i = mat.iterator(); i.hasNext();) { - i.next(); - i.setValue(cpt++); - } - long timeend = System.nanoTime(); - - System.out.println("Iterator FloatBigVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testFloatBigVectorIndex() throws Exception { - List sem = new ArrayList(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - int size = sem.size(); - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(FloatBigVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - for (int z = 0; z < size; z++) { - mat.setValue(x, y, z, cpt++); - } - } - } - long timeend = System.nanoTime(); - - System.out.println("Index FloatBigVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - - @Test - public void testFloatBigVectorSemantic() throws Exception { - List<String> sem = new ArrayList<String>(); - for (char c = 'a'; c <= CMAX; c++) { - for (int i = 1; i <= MAX; i++) { - sem.add(c + " " + i); - } - } - int size = sem.size(); - - MatrixND mat = null; - - // [ 2600 x 2600 x 2600 ] - long timestart = System.nanoTime(); - mat = MatrixFactory.getInstance(FloatBigVector.class).create("Ma mat", - new List[] { sem, sem, sem }); - long timeinit = System.nanoTime(); - // parcours avec iterator - long cpt = 1; - for (int x = 0; x < size; x++) { - for (int y = 0; y < size; y++) { - for (int z = 0; z < size; z++) { - String X = sem.get(x); - String Y = sem.get(y); - String Z = sem.get(z); - mat.setValue(X, Y, Z, cpt++); - } - } - } - long timeend = System.nanoTime(); - - System.out.println("Semantic FloatBigVector init: " - + DurationFormatUtils.formatDuration( - (timeinit - timestart) / 1000000, "s'.'S") - + " fill: " - + DurationFormatUtils.formatDuration( - (timeend - timeinit) / 1000000, "s'.'S")); - } - -} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleBigMappedVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleBigMappedVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleBigMappedVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,45 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * SubMatrixDoubleBigMappedVectorTest.java + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class SubMatrixDoubleBigMappedVectorTest extends SubMatrixDoubleBigVectorTest { // SubMatrixTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleBigMappedVector.class); + } +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleBigVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,282 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +/** + * SubMatrixDoubleBigTest. + * + * Created: 29 oct. 2004 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class SubMatrixDoubleBigVectorTest { // SubMatrixTest + + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleBigVector.class); + } + + @Test + public void testNew() throws Exception { + List s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixND smat = mat.getSubMatrix(1, 0, 2); + Assert.assertTrue(Arrays.equals(new int[] { 3, 2, 3 }, smat.getDim())); + } + + @Test + public void testIterator() throws Exception { + int[][] val18 = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, + { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, { 1, 0, 0 }, + { 1, 0, 1 }, { 1, 0, 2 }, { 1, 1, 0 }, { 1, 1, 1 }, + { 1, 1, 2 }, { 2, 0, 0 }, { 2, 0, 1 }, { 2, 0, 2 }, + { 2, 1, 0 }, { 2, 1, 1 }, { 2, 1, 2 }, }; + + String[][] vals18 = new String[][] { { "a", "e", "k" }, + { "a", "e", "l" }, { "a", "e", "m" }, { "a", "f", "k" }, + { "a", "f", "l" }, { "a", "f", "m" }, { "b", "e", "k" }, + { "b", "e", "l" }, { "b", "e", "m" }, { "b", "f", "k" }, + { "b", "f", "l" }, { "b", "f", "m" }, { "c", "e", "k" }, + { "c", "e", "l" }, { "c", "e", "m" }, { "c", "f", "k" }, + { "c", "f", "l" }, { "c", "f", "m" }, }; + + List s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixND smat = mat.getSubMatrix(1, 0, 2); + + // System.out.println(MatrixHelper.coordinatesToString(smat.getDim())); + + int cpt = 0; + for (MatrixIterator i = smat.iterator(); i.hasNext();) { + i.next(); + + // System.out.println(MatrixHelper.coordinatesToString(i. + // getCoordinates())); + // System.out.println(MatrixHelper.coordinatesToString(i. + // getSemanticsCoordinates())); + + Assert.assertTrue(Arrays.equals(val18[cpt], i.getCoordinates())); + Assert.assertTrue(Arrays.equals(vals18[cpt], i.getSemanticsCoordinates())); + cpt++; + } + Assert.assertEquals(18, cpt); + } + + @Test + public void testSubSubMatrix() throws Exception { + int[][] val6 = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, + { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, }; + + String[][] vals6 = new String[][] { { "c", "e", "k" }, + { "c", "e", "l" }, { "c", "e", "m" }, { "c", "f", "k" }, + { "c", "f", "l" }, { "c", "f", "m" }, }; + + List s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixND smat1 = mat.getSubMatrix(1, 0, 2); + MatrixND smat2 = smat1.getSubMatrix(0, 2, 1); + + //System.out.println(" smat2.getDim: "+MatrixHelper.coordinatesToString( + // smat2.getDim())); + Assert.assertTrue(Arrays.equals(new int[] { 1, 2, 3 }, smat2.getDim())); + + // System.out.println(MatrixHelper.coordinatesToString(smat2.getDim())); + + int cpt = 0; + for (MatrixIterator i = smat2.iterator(); i.hasNext();) { + i.next(); + + Assert.assertTrue(Arrays.equals(val6[cpt], i.getCoordinates())); + Assert.assertTrue(Arrays.equals(vals6[cpt], i.getSemanticsCoordinates())); + cpt++; + } + Assert.assertEquals(6, cpt); + } + + @Test + public void testSubSubGetSet() throws Exception { + List s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixND smat1 = mat.getSubMatrix(1, 0, 2); + MatrixND smat2 = smat1.getSubMatrix(0, 2, 1); + + smat2.setValue(0, 0, 0, 34); + Assert.assertEquals(34, smat2.getValue("c", "e", "k"), 0); + smat2.setValue("c", "f", "l", 23); + Assert.assertEquals(23, smat2.getValue(0, 1, 1), 0); + } + + @Test + public void testSubMapping() throws Exception { + List s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixND smat1 = mat.getSubMatrix(1, new int[] { 0, 1 }); + Assert.assertEquals(2, smat1.getDim(1)); + Assert.assertEquals(Arrays.asList(new String[] { "e", "f" }), smat1 + .getSemantics(1)); + MatrixND smat2 = smat1.getSubMatrix(0, new Object[] { "c" }); + Assert.assertEquals(1, smat2.getDim(0)); + Assert.assertEquals(Arrays.asList(new String[] { "c" }), smat2.getSemantics(0)); + + mat.setValue("c", "f", "k", 12); + Assert.assertEquals(12, smat1.getValue(2, 1, 0), 0); + Assert.assertEquals(12, smat1.getValue("c", "f", "k"), 0); + Assert.assertEquals(12, smat2.getValue(0, 1, 0), 0); + Assert.assertEquals(12, smat2.getValue("c", "f", "k"), 0); + + smat2.setValue(0, 0, 0, 34); + Assert.assertEquals(34, smat2.getValue("c", "e", "k"), 0); + Assert.assertEquals(34, mat.getValue(2, 0, 0), 0); + Assert.assertEquals(34, mat.getValue("c", "e", "k"), 0); + smat2.setValue("c", "f", "l", 23); + Assert.assertEquals(23, smat2.getValue(0, 1, 1), 0); + Assert.assertEquals(23, mat.getValue(2, 1, 1), 0); + Assert.assertEquals(23, mat.getValue("c", "f", "l"), 0); + } + + @Test + public void testSubAdd() throws Exception { + List s1 = Arrays.asList(new String[] { "a", "b", "c" }); + List s2 = Arrays.asList(new String[] { "e", "f", "g" }); + List s3 = Arrays.asList(new String[] { "k", "l", "m" }); + + MatrixND mat1 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixND mat2 = getFactory().create("Ma mat", + new List[] { s1, s2, s3 }, + new String[] { "dim abc", "dim efg", "dim klm" }); + + MatrixHelper.fill(mat1, 5); + MatrixHelper.fill(mat2, 3); + + MatrixND smat1 = mat1.getSubMatrix(1, "e", 1); + MatrixND smat2 = mat2.getSubMatrix(1, "f", 1); + + smat1.add(smat2); + + Assert.assertEquals(5, mat1.getValue(0, 1, 0), 0); + Assert.assertEquals(5, mat1.getValue(0, 1, 1), 0); + Assert.assertEquals(5, mat1.getValue(0, 1, 2), 0); + Assert.assertEquals(5, mat1.getValue(1, 1, 0), 0); + Assert.assertEquals(5, mat1.getValue(1, 1, 1), 0); + Assert.assertEquals(5, mat1.getValue(1, 1, 2), 0); + Assert.assertEquals(5, mat1.getValue(2, 1, 0), 0); + Assert.assertEquals(5, mat1.getValue(2, 1, 1), 0); + Assert.assertEquals(5, mat1.getValue(2, 1, 2), 0); + Assert.assertEquals(5, mat1.getValue(0, 2, 0), 0); + Assert.assertEquals(5, mat1.getValue(0, 2, 1), 0); + Assert.assertEquals(5, mat1.getValue(0, 2, 2), 0); + Assert.assertEquals(5, mat1.getValue(1, 2, 0), 0); + Assert.assertEquals(5, mat1.getValue(1, 2, 1), 0); + Assert.assertEquals(5, mat1.getValue(1, 2, 2), 0); + Assert.assertEquals(5, mat1.getValue(2, 2, 0), 0); + Assert.assertEquals(5, mat1.getValue(2, 2, 1), 0); + Assert.assertEquals(5, mat1.getValue(2, 2, 2), 0); + + Assert.assertEquals(8, mat1.getValue(0, 0, 0), 0); + Assert.assertEquals(8, mat1.getValue(0, 0, 1), 0); + Assert.assertEquals(8, mat1.getValue(0, 0, 2), 0); + Assert.assertEquals(8, mat1.getValue(1, 0, 0), 0); + Assert.assertEquals(8, mat1.getValue(1, 0, 1), 0); + Assert.assertEquals(8, mat1.getValue(1, 0, 2), 0); + Assert.assertEquals(8, mat1.getValue(2, 0, 0), 0); + Assert.assertEquals(8, mat1.getValue(2, 0, 1), 0); + Assert.assertEquals(8, mat1.getValue(2, 0, 2), 0); + + } + + @Test + public void testSubSubMults() throws Exception { + MatrixND mat = null; + + mat = getFactory().create("Ma mat", new int[] { 4, 4 }); + + MatrixHelper.fill(mat, 4); + + mat = getFactory().create(mat); + + MatrixND smat1 = mat.getSubMatrix(1, 0, 2); + MatrixND smat2 = smat1.getSubMatrix(0, 2, 1); + + System.out.println("mat:" + mat); + System.out.println("smat1:" + smat1); + System.out.println("smat2:" + smat2); + + smat2.mults(4); + + System.out.println("mat:" + mat); + System.out.println("smat1:" + smat1); + System.out.println("smat2:" + smat2); + + } + +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixDoubleVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,45 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * SubMatrixDoubleVectorTest.java + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class SubMatrixDoubleVectorTest extends SubMatrixDoubleBigVectorTest { // SubMatrixTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(DoubleVector.class); + } +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixFloatBigVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixFloatBigVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixFloatBigVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,45 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * SubMatrixFloatBigVectorTest.java + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class SubMatrixFloatBigVectorTest extends SubMatrixDoubleBigVectorTest { // SubMatrixTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(FloatBigVector.class); + } +} Copied: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixFloatVectorTest.java (from rev 441, trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java) =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixFloatVectorTest.java (rev 0) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixFloatVectorTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -0,0 +1,45 @@ +/* + * #%L + * NuitonMatrix + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * SubMatrixFloatVectorTest.java + * + * Created: 30 aout 2012 + * + * @author Benjamin Poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class SubMatrixFloatVectorTest extends SubMatrixDoubleBigVectorTest { // SubMatrixTest + + @Override + public MatrixFactory getFactory() throws Exception { + return MatrixFactory.getInstance(FloatVector.class); + } +} Deleted: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java 2012-08-30 14:43:45 UTC (rev 441) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/SubMatrixTest.java 2012-08-30 22:52:26 UTC (rev 442) @@ -1,283 +0,0 @@ -/* - * #%L - * NuitonMatrix - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.math.matrix; - -import java.util.Arrays; -import java.util.List; - -import org.junit.Assert; -import org.junit.Test; - -/** - * SubMatrixTest. - * - * Created: 29 oct. 2004 - * - * @author Benjamin Poussin <poussin@codelutin.com> - * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ - */ -public class SubMatrixTest { // SubMatrixTest - - public MatrixFactory getFactory() throws Exception { - return MatrixFactory.getInstance(); - } - - @Test - public void testNew() throws Exception { - List s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixND smat = mat.getSubMatrix(1, 0, 2); - Assert.assertTrue(Arrays.equals(new int[] { 3, 2, 3 }, smat.getDim())); - } - - @Test - public void testIterator() throws Exception { - int[][] val18 = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, - { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, { 1, 0, 0 }, - { 1, 0, 1 }, { 1, 0, 2 }, { 1, 1, 0 }, { 1, 1, 1 }, - { 1, 1, 2 }, { 2, 0, 0 }, { 2, 0, 1 }, { 2, 0, 2 }, - { 2, 1, 0 }, { 2, 1, 1 }, { 2, 1, 2 }, }; - - String[][] vals18 = new String[][] { { "a", "e", "k" }, - { "a", "e", "l" }, { "a", "e", "m" }, { "a", "f", "k" }, - { "a", "f", "l" }, { "a", "f", "m" }, { "b", "e", "k" }, - { "b", "e", "l" }, { "b", "e", "m" }, { "b", "f", "k" }, - { "b", "f", "l" }, { "b", "f", "m" }, { "c", "e", "k" }, - { "c", "e", "l" }, { "c", "e", "m" }, { "c", "f", "k" }, - { "c", "f", "l" }, { "c", "f", "m" }, }; - - List s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixND smat = mat.getSubMatrix(1, 0, 2); - - // System.out.println(MatrixHelper.coordinatesToString(smat.getDim())); - - int cpt = 0; - for (MatrixIterator i = smat.iterator(); i.hasNext();) { - i.next(); - - // System.out.println(MatrixHelper.coordinatesToString(i. - // getCoordinates())); - // System.out.println(MatrixHelper.coordinatesToString(i. - // getSemanticsCoordinates())); - - Assert.assertTrue(Arrays.equals(val18[cpt], i.getCoordinates())); - Assert.assertTrue(Arrays.equals(vals18[cpt], i.getSemanticsCoordinates())); - cpt++; - } - Assert.assertEquals(18, cpt); - } - - @Test - public void testSubSubMatrix() throws Exception { - int[][] val6 = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, - { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, }; - - String[][] vals6 = new String[][] { { "c", "e", "k" }, - { "c", "e", "l" }, { "c", "e", "m" }, { "c", "f", "k" }, - { "c", "f", "l" }, { "c", "f", "m" }, }; - - List s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixND smat1 = mat.getSubMatrix(1, 0, 2); - MatrixND smat2 = smat1.getSubMatrix(0, 2, 1); - - //System.out.println(" smat2.getDim: "+MatrixHelper.coordinatesToString( - // smat2.getDim())); - Assert.assertTrue(Arrays.equals(new int[] { 1, 2, 3 }, smat2.getDim())); - - // System.out.println(MatrixHelper.coordinatesToString(smat2.getDim())); - - int cpt = 0; - for (MatrixIterator i = smat2.iterator(); i.hasNext();) { - i.next(); - - Assert.assertTrue(Arrays.equals(val6[cpt], i.getCoordinates())); - Assert.assertTrue(Arrays.equals(vals6[cpt], i.getSemanticsCoordinates())); - cpt++; - } - Assert.assertEquals(6, cpt); - } - - @Test - public void testSubSubGetSet() throws Exception { - List s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixND smat1 = mat.getSubMatrix(1, 0, 2); - MatrixND smat2 = smat1.getSubMatrix(0, 2, 1); - - smat2.setValue(0, 0, 0, 34); - Assert.assertEquals(34, smat2.getValue("c", "e", "k"), 0); - smat2.setValue("c", "f", "l", 23); - Assert.assertEquals(23, smat2.getValue(0, 1, 1), 0); - } - - @Test - public void testSubMapping() throws Exception { - List s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixND smat1 = mat.getSubMatrix(1, new int[] { 0, 1 }); - Assert.assertEquals(2, smat1.getDim(1)); - Assert.assertEquals(Arrays.asList(new String[] { "e", "f" }), smat1 - .getSemantics(1)); - MatrixND smat2 = smat1.getSubMatrix(0, new Object[] { "c" }); - Assert.assertEquals(1, smat2.getDim(0)); - Assert.assertEquals(Arrays.asList(new String[] { "c" }), smat2.getSemantics(0)); - - mat.setValue("c", "f", "k", 12); - Assert.assertEquals(12, smat1.getValue(2, 1, 0), 0); - Assert.assertEquals(12, smat1.getValue("c", "f", "k"), 0); - Assert.assertEquals(12, smat2.getValue(0, 1, 0), 0); - Assert.assertEquals(12, smat2.getValue("c", "f", "k"), 0); - - smat2.setValue(0, 0, 0, 34); - Assert.assertEquals(34, smat2.getValue("c", "e", "k"), 0); - Assert.assertEquals(34, mat.getValue(2, 0, 0), 0); - Assert.assertEquals(34, mat.getValue("c", "e", "k"), 0); - smat2.setValue("c", "f", "l", 23); - Assert.assertEquals(23, smat2.getValue(0, 1, 1), 0); - Assert.assertEquals(23, mat.getValue(2, 1, 1), 0); - Assert.assertEquals(23, mat.getValue("c", "f", "l"), 0); - } - - @Test - public void testSubAdd() throws Exception { - List s1 = Arrays.asList(new String[] { "a", "b", "c" }); - List s2 = Arrays.asList(new String[] { "e", "f", "g" }); - List s3 = Arrays.asList(new String[] { "k", "l", "m" }); - - MatrixND mat1 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixND mat2 = getFactory().create("Ma mat", - new List[] { s1, s2, s3 }, - new String[] { "dim abc", "dim efg", "dim klm" }); - - MatrixHelper.fill(mat1, 5); - MatrixHelper.fill(mat2, 3); - - MatrixND smat1 = mat1.getSubMatrix(1, "e", 1); - MatrixND smat2 = mat2.getSubMatrix(1, "f", 1); - - smat1.add(smat2); - - Assert.assertEquals(5, mat1.getValue(0, 1, 0), 0); - Assert.assertEquals(5, mat1.getValue(0, 1, 1), 0); - Assert.assertEquals(5, mat1.getValue(0, 1, 2), 0); - Assert.assertEquals(5, mat1.getValue(1, 1, 0), 0); - Assert.assertEquals(5, mat1.getValue(1, 1, 1), 0); - Assert.assertEquals(5, mat1.getValue(1, 1, 2), 0); - Assert.assertEquals(5, mat1.getValue(2, 1, 0), 0); - Assert.assertEquals(5, mat1.getValue(2, 1, 1), 0); - Assert.assertEquals(5, mat1.getValue(2, 1, 2), 0); - Assert.assertEquals(5, mat1.getValue(0, 2, 0), 0); - Assert.assertEquals(5, mat1.getValue(0, 2, 1), 0); - Assert.assertEquals(5, mat1.getValue(0, 2, 2), 0); - Assert.assertEquals(5, mat1.getValue(1, 2, 0), 0); - Assert.assertEquals(5, mat1.getValue(1, 2, 1), 0); - Assert.assertEquals(5, mat1.getValue(1, 2, 2), 0); - Assert.assertEquals(5, mat1.getValue(2, 2, 0), 0); - Assert.assertEquals(5, mat1.getValue(2, 2, 1), 0); - Assert.assertEquals(5, mat1.getValue(2, 2, 2), 0); - - Assert.assertEquals(8, mat1.getValue(0, 0, 0), 0); - Assert.assertEquals(8, mat1.getValue(0, 0, 1), 0); - Assert.assertEquals(8, mat1.getValue(0, 0, 2), 0); - Assert.assertEquals(8, mat1.getValue(1, 0, 0), 0); - Assert.assertEquals(8, mat1.getValue(1, 0, 1), 0); - Assert.assertEquals(8, mat1.getValue(1, 0, 2), 0); - Assert.assertEquals(8, mat1.getValue(2, 0, 0), 0); - Assert.assertEquals(8, mat1.getValue(2, 0, 1), 0); - Assert.assertEquals(8, mat1.getValue(2, 0, 2), 0); - - } - - @Test - public void testSubSubMults() throws Exception { - MatrixND mat = null; - - mat = getFactory().create("Ma mat", new int[] { 4, 4 }); - - MatrixHelper.fill(mat, 4); - - mat = getFactory().create(mat); - - MatrixND smat1 = mat.getSubMatrix(1, 0, 2); - MatrixND smat2 = smat1.getSubMatrix(0, 2, 1); - - System.out.println("mat:" + mat); - System.out.println("smat1:" + smat1); - System.out.println("smat2:" + smat2); - - smat2.mults(4); - - System.out.println("mat:" + mat); - System.out.println("smat1:" + smat1); - System.out.println("smat2:" + smat2); - - } - -} // SubMatrixTest -
participants (1)
-
bpoussin@users.nuiton.org