Index: lutinmatrix/src/java/org/codelutin/math/matrix/MatrixND.java diff -u lutinmatrix/src/java/org/codelutin/math/matrix/MatrixND.java:1.8 lutinmatrix/src/java/org/codelutin/math/matrix/MatrixND.java:1.9 --- lutinmatrix/src/java/org/codelutin/math/matrix/MatrixND.java:1.8 Wed Mar 22 19:38:47 2006 +++ lutinmatrix/src/java/org/codelutin/math/matrix/MatrixND.java Thu Jun 1 17:38:56 2006 @@ -23,15 +23,18 @@ * Created: 29 oct. 2004 * * @author Benjamin Poussin -* @version $Revision: 1.8 $ +* @version $Revision: 1.9 $ * -* Mise a jour: $Date: 2006/03/22 19:38:47 $ -* par : $Author: bpoussin $ +* Mise a jour: $Date: 2006/06/01 17:38:56 $ +* par : $Author: ruchaud $ */ package org.codelutin.math.matrix; +import java.io.IOException; +import java.io.Reader; import java.io.Serializable; +import java.io.Writer; import java.util.List; public interface MatrixND extends Serializable, Cloneable { // MatrixND @@ -496,6 +499,26 @@ */ public void fromList(List list); + /** + * Determine si la matrice supporte l'import et l'export CSV + * @return support du CSV + */ + public boolean isSupportedCSV(); + + /** + * Import depuis un reader au format CSV des données dans la matrice + * @param reader le reader à importer + * @param origin le point à partir duquel il faut faire l'importation + */ + public void importCSV(Reader reader, int[] origin) throws IOException; + + /** + * Export dans un writer au format CSV de la matrice + * @param writer le writer ou copier la matrice + * @param withSemantics export ou pas des semantiques de la matrice dans le writer + */ + public void exportCSV(Writer writer, boolean withSemantics) throws IOException; + // /** // * Multiplication d'une vecteur [i] avec une matrice [i,j], // * le resultat est result[i,j]=matrice[i,j]*vecteur[i] Index: lutinmatrix/src/java/org/codelutin/math/matrix/AbstractMatrixND.java diff -u lutinmatrix/src/java/org/codelutin/math/matrix/AbstractMatrixND.java:1.11 lutinmatrix/src/java/org/codelutin/math/matrix/AbstractMatrixND.java:1.12 --- lutinmatrix/src/java/org/codelutin/math/matrix/AbstractMatrixND.java:1.11 Mon May 22 12:39:07 2006 +++ lutinmatrix/src/java/org/codelutin/math/matrix/AbstractMatrixND.java Thu Jun 1 17:38:56 2006 @@ -23,14 +23,18 @@ * Created: 29 oct. 2004 * * @author Benjamin Poussin - * @version $Revision: 1.11 $ + * @version $Revision: 1.12 $ * - * Mise a jour: $Date: 2006/05/22 12:39:07 $ - * par : $Author: bpoussin $ + * Mise a jour: $Date: 2006/06/01 17:38:56 $ + * par : $Author: ruchaud $ */ package org.codelutin.math.matrix; +import java.io.IOException; +import java.io.Reader; +import java.io.StreamTokenizer; +import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -597,5 +601,100 @@ return this; } -} // AbstractMatrixND + /** + * Separateur CSV par défaut la virgule + */ + public static String CSV_SEPARATOR = ";"; + + /** + * Determine si la matrice supporte l'import et l'export CSV + * @return support du CSV + */ + public boolean isSupportedCSV() { + return getNbDim() <= 2; + } + + /** + * Import depuis un reader au format CSV des données dans la matrice + * @param reader le reader à importer + * @param origin le point à partir duquel il faut faire l'importation int[]{x,y} + */ + public void importCSV(Reader reader, int[] origin) throws IOException { + int rowsCount = 0; + StreamTokenizer tokenizer; + List row = new ArrayList(); + + tokenizer = new StreamTokenizer(reader); + tokenizer.eolIsSignificant(true); + + while(tokenizer.nextToken() != StreamTokenizer.TT_EOF) { + switch (tokenizer.ttype) { + case StreamTokenizer.TT_EOL: + if(!row.isEmpty()) { + MatrixND matrix = getFactory().create(new int[]{1, row.size()}); + int columnNumber = 0; + for (Double value : row) { + matrix.setValue(new int[]{0, columnNumber}, value); + columnNumber++; + } + paste(new int[]{origin[0] + rowsCount, origin[1]}, matrix); + rowsCount ++; + row.clear(); + } + break; + case StreamTokenizer.TT_NUMBER: + row.add(tokenizer.nval); + break; + case StreamTokenizer.TT_WORD: + break; + default: + break; + } + } + } + + /** + * Export dans un writer au format CSV de la matrice + * @param writer le writer ou copier la matrice + * @param withSemantics export ou pas des semantiques de la matrice dans le writer + */ + public void exportCSV(Writer writer, boolean withSemantics) throws IOException { + int dimsCount = getNbDim(); + int rowsCount = dimsCount == 1 ? 1 : getDim(0); + int columnsCount = dimsCount == 1 ? getDim(0) : getDim(1); + int[] coordinates; + + if(!isSupportedCSV()) { + throw new UnsupportedOperationException(); + } + + /* Création de l'entete */ + if(withSemantics) { + /* Recuperation de la liste sur la bonne dimenssion */ + List listSemantics = getSemantics(dimsCount - 1); + /* Ajout d'un décalage de l'entete pour la dimenssion 2 */ + writer.append(dimsCount == 2 ? " " + CSV_SEPARATOR : ""); + for (Object semantic : listSemantics) { + writer.append(semantic + CSV_SEPARATOR); + } + writer.append("\n"); + } + + for (int rowNb = 0; rowNb < rowsCount; rowNb++) { + /* Ajout de la semantic devant la ligne pour la dimenssion 2 */ + if(withSemantics && dimsCount == 2) { + Object semantic = getSemantics(0).get(rowNb); + writer.append(semantic + CSV_SEPARATOR); + } + + for (int columnNb = 0; columnNb < columnsCount; columnNb++) { + /* Calcul des coordonnees */ + coordinates = dimsCount == 1 ? + new int[]{columnNb} : new int[]{rowNb, columnNb}; + writer.append(getValue(coordinates) + CSV_SEPARATOR); + } + writer.append("\n"); + } + } +} // AbstractMatrixND