Nuiton-matrix-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
March 2009
- 3 participants
- 13 discussions
[Lutinmatrix-commits] r131 - lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui
by chatellier@users.labs.libre-entreprise.org 31 Mar '09
by chatellier@users.labs.libre-entreprise.org 31 Mar '09
31 Mar '09
Author: chatellier
Date: 2009-03-31 08:14:00 +0000 (Tue, 31 Mar 2009)
New Revision: 131
Modified:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPopupMenu.java
Log:
Fix NPE if user choose cancel option.
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPopupMenu.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPopupMenu.java 2009-03-24 15:33:41 UTC (rev 130)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPopupMenu.java 2009-03-31 08:14:00 UTC (rev 131)
@@ -47,7 +47,7 @@
import org.codelutin.util.FileUtil;
/**
- * Ajout d'un menu contextuel sur la matrice dans l'editeur
+ * Ajout d'un menu contextuel sur la matrice dans l'editeur.
*
* Created: 22 mars 2006 12:11:46
*
@@ -61,7 +61,7 @@
/** serialVersionUID. */
private static final long serialVersionUID = 3349189688987885915L;
-
+
private MatrixEditor matrixEditor;
private JFileChooser fileChooser;
@@ -507,9 +507,11 @@
private void sendToFileAllCopyPerformed() {
try {
Writer writer = getFileChooserWriter();
- getMatrix().exportCSV(writer, withSemantics.getState());
- writer.close();
- matrixEditor.repaint();
+ if (writer != null) {
+ getMatrix().exportCSV(writer, withSemantics.getState());
+ writer.close();
+ matrixEditor.repaint();
+ }
} catch (Exception e) {
JOptionPane.showMessageDialog(matrixEditor,
_("lutinmatrix.error.file.write"), _("lutinmatrix.error"),
@@ -521,10 +523,12 @@
private void sendToFileAllPastePerformed() {
try {
Reader reader = getFileChooserReader();
- getMatrix().importCSV(reader, new int[] { 0, 0 });
- reader.close();
- matrixEditor.fireEvent();
- matrixEditor.repaint();
+ if (reader != null) { // cancel
+ getMatrix().importCSV(reader, new int[] { 0, 0 });
+ reader.close();
+ matrixEditor.fireEvent();
+ matrixEditor.repaint();
+ }
} catch (Exception e) {
JOptionPane.showMessageDialog(matrixEditor,
_("lutinmatrix.error.file.read"), _("lutinmatrix.error"),
@@ -536,9 +540,11 @@
private void sendToFileSelectionCopyPerformed() {
try {
Writer writer = getFileChooserWriter();
- getSelectedMatrix().exportCSV(writer, withSemantics.getState());
- writer.close();
- matrixEditor.repaint();
+ if (writer != null) {
+ getSelectedMatrix().exportCSV(writer, withSemantics.getState());
+ writer.close();
+ matrixEditor.repaint();
+ }
} catch (Exception e) {
JOptionPane.showMessageDialog(matrixEditor,
_("lutinmatrix.error.file.write"), _("lutinmatrix.error"),
@@ -550,11 +556,13 @@
private void sendToFileCurrentPastePerformed() {
try {
Reader reader = getFileChooserReader();
- getMatrix().importCSV(reader,
- getCoordinatesFirstCellSelectedMatrix());
- reader.close();
- matrixEditor.fireEvent();
- matrixEditor.repaint();
+ if (reader != null) { // cancel
+ getMatrix().importCSV(reader,
+ getCoordinatesFirstCellSelectedMatrix());
+ reader.close();
+ matrixEditor.fireEvent();
+ matrixEditor.repaint();
+ }
} catch (Exception e) {
JOptionPane.showMessageDialog(matrixEditor,
_("lutinmatrix.error.file.read"), _("lutinmatrix.error"),
1
0
[Lutinmatrix-commits] r130 - in lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix: . gui
by sletellier@users.labs.libre-entreprise.org 24 Mar '09
by sletellier@users.labs.libre-entreprise.org 24 Mar '09
24 Mar '09
Author: sletellier
Date: 2009-03-24 15:33:41 +0000 (Tue, 24 Mar 2009)
New Revision: 130
Modified:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/JAXXMatrixEditor.jaxx
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixEditor.java
Log:
Debug
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java 2009-03-23 14:07:41 UTC (rev 129)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java 2009-03-24 15:33:41 UTC (rev 130)
@@ -147,7 +147,7 @@
/*
* (non-Javadoc)
- *
+ *
* @see org.codelutin.math.matrix.MatrixND#copy()
*/
public MatrixND copy() {
@@ -157,11 +157,11 @@
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#clone()
*/
@Override
- protected Object clone() throws CloneNotSupportedException {
+ public MatrixND clone() {
return copy();
}
@@ -201,25 +201,25 @@
public String[] getDimensionNames() {
return dimNames;
}
-
+
public void setDimensionNames(String[] names) {
for (int i = 0; names != null && i < names.length; i++) {
setDimensionName(i, names[i]);
}
}
-
+
/**
* {@inheritDoc}
- *
+ *
* @deprecated Use #getDimensionNames()
*/
public String[] getDimensionName() {
return getDimensionNames();
}
-
+
/**
* {@inheritDoc}
- *
+ *
* @deprecated Use #setDimensionName(String[])
*/
public void setDimensionName(String[] names) {
@@ -396,7 +396,7 @@
/**
* Verifie si les matrices sont egales en ne regardant que les valeurs et
* pas les semantiques
- *
+ *
* @param mat
* @return
*/
@@ -499,7 +499,7 @@
/*
* (non-Javadoc)
- *
+ *
* @see org.codelutin.math.matrix.MatrixND#sumAll()
*/
public double sumAll() {
@@ -591,7 +591,7 @@
/**
* Modifie la matrice actuel en metant les valeurs de mat passé en parametre
- *
+ *
* @param origin le point d'origine a partir duquel on colle la matrice
* @param mat une matrice avec le meme nombre de dimension, si la matrice
* que l'on colle est trop grande, les valeurs qui depasse ne
@@ -651,7 +651,7 @@
/**
* Add to desambiguas some call with xpath engine, but do the same thing
* {@link #getSubMatrix(int, Object[])}
- *
+ *
* @param dim
* @param elem
* @return
@@ -798,7 +798,7 @@
/**
* Create new matrice from the current matrix.
- *
+ *
* @param dimName dimension name for new matrix
* @param sem semantic for new matrix
* @param correspondance array to do the link between current matrix and
@@ -876,7 +876,7 @@
});
return this;
}
-
+
public MatrixND adds(final double d) {
map(new MapFunction() {
public double apply(double val) {
@@ -885,7 +885,7 @@
});
return this;
}
-
+
public MatrixND minuss(final double d) {
map(new MapFunction() {
public double apply(double val) {
@@ -897,7 +897,7 @@
/**
* Determine si la matrice supporte l'import et l'export CSV
- *
+ *
* @return support du CSV
*/
public boolean isSupportedCSV() {
@@ -906,7 +906,7 @@
/**
* 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}
@@ -979,13 +979,13 @@
// StreamTokenizer tokenizer;
// List<Double> row = new ArrayList<Double>();
// boolean stop = false;
- //
+ //
// tokenizer = new StreamTokenizer(reader);
// tokenizer.eolIsSignificant(true);
- //
+ //
// while(!stop) {
// tokenizer.nextToken();
- //
+ //
// switch (tokenizer.ttype) {
// case StreamTokenizer.TT_EOF:
// stop = true; // no break we do next case too
@@ -1017,7 +1017,7 @@
/**
* 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
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java 2009-03-23 14:07:41 UTC (rev 129)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java 2009-03-24 15:33:41 UTC (rev 130)
@@ -38,25 +38,25 @@
/**
* Retourne la factory qui a permit de creer la matrice.
- *
+ *
* @return la {@link MatrixFactory}
- *
+ *
* @see MatrixFactory
*/
public MatrixFactory getFactory();
/**
* Donne toutes les semantiques de la matrice.
- *
+ *
* Si la matrice n'a pas de semantique retourne null.
- *
+ *
* @return la liste des semantics
*/
public List[] getSemantics();
/**
* Retourne la semantique pour une dimension
- *
+ *
* @param dim la dimension pour lequel on veut la semantique
* @return la semantique de la dimension on null s'il n'y a pas de
* semantique
@@ -70,55 +70,55 @@
/**
* Permet de donner un nom à la matrice.
- *
+ *
* @param name name to set
*/
public void setName(String name);
/**
* Retourne le nom de la matrice
- *
+ *
* @return le nom de la matrice ou la chaine vide si pas de nom.
*/
public String getName();
/**
* Permet de mettre des noms aux différentes dimension.
- *
+ *
* @deprecated (since 1.0.3) Use #setDimensionNames(String[])
*/
public void setDimensionName(String[] names);
/**
* Permet de mettre des noms aux différentes dimension.
- *
+ *
* @param names names to set
- *
+ *
* @since 1.0.3
*/
public void setDimensionNames(String[] names);
-
+
/**
* Permet de recuperer les noms des dimension.
- *
+ *
* @return tableau des noms de dimension.
- *
+ *
* @deprecated (since 1.0.3) Use #getDimensionNames()
*/
public String[] getDimensionName();
-
+
/**
* Permet de recuperer les noms des dimension.
- *
+ *
* @return tableau des noms de dimension.
- *
+ *
* @since 1.0.3
*/
public String[] getDimensionNames();
-
+
/**
* Permet de mettre un nom à une dimension.
- *
+ *
* @param dim la dimension dont on veut changer le nom
* @param name le nom à donner à la dimension
*/
@@ -126,7 +126,7 @@
/**
* Retourne le nom de la dimension demandé.
- *
+ *
* @param dim la dimension dont on veut le nom
* @return le nom de la dimension ou la chaine vide si la dimension n'a pas
* de nom @ si la dimension demandé n'est pas valide
@@ -137,7 +137,7 @@
* Retourne la valeur la plus courrement rencontrer dans un tableau. si
* plusieurs valeurs ont le même nombre d'occurence la plus petite valeur
* est retourné.
- *
+ *
* @return la valeur la plus nombreuse dans le tableau
*/
public double getMaxOccurence();
@@ -191,14 +191,14 @@
/**
* Renvoie un element de la matrice demandée en fonction des dimensions
* passé en paramètre.<br>
- *
+ *
* Exemple: Si on a un matrice 3D.<br>
* getValue(1,1,1) retourne un element de la matrice.<br>
- *
+ *
* @param dimensions les différentes dimension à extraire. Le tableau doit
* contenir toutes les dimensions de la matrice, et seulement des
* nombres positif
- *
+ *
* @return un entier double.
*/
public double getValue(int[] dimensions);
@@ -246,12 +246,12 @@
/**
* Modifie un element de la matrice en fonction des dimensions passé en
* paramètre.<br>
- *
+ *
* Exemple: Si on a un matrice 3D.<br>
* set([1,1,1], m) modifie un element de la matrice.<br>
- *
+ *
* @param dimensions les différentes dimension à extraire.
- *
+ *
* @param d l'entier double qui doit remplacer l'entier double spécifié par
* l'argument dimensions
*/
@@ -281,6 +281,11 @@
*/
public MatrixND copy();
+ /**
+ * Créer une nouvelle instance clonée de celle-ci
+ */
+ public MatrixND clone();
+
// public String toString();
// /**
@@ -307,17 +312,17 @@
* <p>
* par exemple pour la matrice suivante si on somme sur la dimension 1 cela
* donnera
- *
+ *
* <pre>
* 1 2 3
* 2 3 4
* 3 4 5
* </pre>
- *
+ *
* <pre>
* 6 9 12
* </pre>
- *
+ *
* @param dim la dimension sur lequel il faut faire la somme
*/
public MatrixND sumOverDim(int dim);
@@ -327,24 +332,24 @@
* permet juste de regrouper dans une dimension un certain nombre de valeur.
* <p>
* pour la matrice suivante :
- *
+ *
* <pre>
* 1 2 3 4
* 2 3 4 5
* 3 4 5 6
* 4 5 6 7
* </pre>
- *
+ *
* la somme sur la dimension 1 avec un pas de 2 donnera :
- *
+ *
* <pre>
* 3 5 7 9
* 7 9 11 13
* </pre>
- *
+ *
* c'est à dire que la ligne 0 et la ligne 1 sont sommées. ainsi que la
* ligne 2 avec la ligne 3.
- *
+ *
* @param dim la dimension sur lequel il faut faire les sommes
* @param step le pas qu'il faut utiliser pour regrouper les elements. Si le
* pas est inférieur à 0, le pas se comporte comme si on avait
@@ -367,23 +372,23 @@
/**
* Permet de supprimer des éléments de la matrice. par exemple pour la
* matrice
- *
+ *
* <pre>
* 1 2 3 4
* 2 3 4 5
* 3 4 5 6
* 4 5 6 7
* </pre>
- *
+ *
* un cut(1, [0,2]) donnera
- *
+ *
* <pre>
* 2 4
* 3 5
* 4 6
* 5 7
* </pre>
- *
+ *
* @param dim la dimension dans lequel il faut supprimer des éléments
* @param toCut les éléments à supprimer
* @return une nouvelle matrice, la matrice actuelle n'est pas modifiée
@@ -394,7 +399,7 @@
* Copie une matrice dans la matrice actuelle. La matrice à copier à le même
* nombre de dimension. Si la matrice à copier est trop grande seul les
* éléments pouvant être copier le seront.
- *
+ *
* @param mat la matrice à copier
* @return return la matrice courante.
*/
@@ -404,7 +409,7 @@
* Copie une matrice dans la matrice actuelle. La matrice à copier à le même
* nombre de dimension. Si la matrice à copier est trop grande seul les
* éléments pouvant être copier le seront.
- *
+ *
* @param origin le point à partir duquel il faut faire la copie
* @param mat la matrice à copier
* @return return la matrice courante.
@@ -458,7 +463,7 @@
* Permet de prendre une sous matrice dans la matrice courante. La sous
* matrice a le même nombre de dimensions mais sur une des dimensions on ne
* prend que certain élément.
- *
+ *
* @param dim la dimension dans lequel on veut une sous matrice si dim est
* négatif alors la dimension est prise à partir de la fin par
* exemple si l'on veut la derniere dimension il faut passer -1
@@ -475,7 +480,7 @@
* Permet de prendre une sous matrice dans la matrice courante. La sous
* matrice a le même nombre de dimensions mais sur une des dimensions on ne
* prend que certain élément.
- *
+ *
* @param dim la dimension dans lequel on veut une sous matrice
* @param start la position dans dim d'ou il faut partir pour prendre la
* sous matrice. 0 <= start < dim.size si start est négatif alors
@@ -491,7 +496,7 @@
* Permet de prendre une sous matrice dans la matrice courante. La sous
* matrice a le même nombre de dimensions mais sur une des dimensions on ne
* prend que certain élément.
- *
+ *
* @param dim la dimension dans lequel on veut une sous matrice
* @param elem les éléments dans la dimension à conserver
*/
@@ -501,7 +506,7 @@
* Permet de prendre une sous matrice dans la matrice courante. La sous
* matrice a le même nombre de dimensions mais sur une des dimensions on ne
* prend que certain élément.
- *
+ *
* @param dim la dimension dans lequel on veut une sous matrice
* @param elem les éléments dans la dimension à conserver
*/
@@ -528,7 +533,7 @@
* Reduit la matrice de sorte que toutes les dimensions qui n'ont qu'un
* élement soit supprimée. Au pire cette méthode retourne une matrice à une
* seule dimension à un seul élément.
- *
+ *
* @return une nouvelle matrice plus petite que la matrice actuelle ou egal
* s'il n'y a aucune dimension à supprimer
*/
@@ -538,7 +543,7 @@
* Reduit la matrice de sorte que toutes les dimensions qui n'ont qu'un
* élement soit supprimée. Au pire cette méthode retourne une matrice à une
* seule dimension à un seul élément.
- *
+ *
* @param minNbDim le nombre minimum de dimension que l'on souhaite pour la
* matrice résultat
* @return une nouvelle matrice plus petite que la matrice actuelle ou egal
@@ -550,7 +555,7 @@
* Reduit le matrice seulement sur les dimensions passées en argument. Si
* une des dimensions passées en arguement n'a pas un seul élément, cette
* dimension n'est pas prise en compte.
- *
+ *
* @param dims les dimensions sur lequel il faut faire la reduction
* @return une nouvelle matrice
*/
@@ -575,12 +580,12 @@
* Multiplication d'une matrice par un scalaire
*/
public MatrixND divs(final double d);
-
+
/**
* Addition d'un scalaire à une matrice
*/
public MatrixND adds(final double d);
-
+
/**
* Soustractiond'un scalaire à une matrice
*/
@@ -588,28 +593,28 @@
/**
* Donne la matrice sous forme de List de list ... de double
- *
+ *
* @return
*/
public List toList();
/**
* Permet de charger une matrice a partir d'une representation List
- *
+ *
* @param list la matrice sous forme de List de list ... de double
*/
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
*/
@@ -617,7 +622,7 @@
/**
* 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
@@ -628,7 +633,7 @@
/**
* Verifie si les matrices sont egales en ne regardant que les valeurs et
* pas les semantiques
- *
+ *
* @param mat
* @return
*/
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/JAXXMatrixEditor.jaxx
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/JAXXMatrixEditor.jaxx 2009-03-23 14:07:41 UTC (rev 129)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/JAXXMatrixEditor.jaxx 2009-03-24 15:33:41 UTC (rev 130)
@@ -163,10 +163,10 @@
}
]]>
</script>
- <JButton id='buttonEdit' text='lutinmatrix.create.matrix.button' enabled='{isDimensionEdit()}'
- onActionPerformed='btnAction()' constraints='BorderLayout.SOUTH'/>
<JScrollPane id='editArea' constraints='BorderLayout.CENTER'>
<JTable id='table' autoResizeMode='{JTable.AUTO_RESIZE_OFF}'
cellSelectionEnabled='{true}' selectionMode='{ListSelectionModel.SINGLE_INTERVAL_SELECTION}'/>
</JScrollPane>
+ <JButton id='buttonEdit' text='lutinmatrix.create.matrix.button' visible='{isDimensionEdit()}'
+ onActionPerformed='btnAction()' constraints='BorderLayout.SOUTH'/>
</MatrixEditor>
\ No newline at end of file
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixEditor.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixEditor.java 2009-03-23 14:07:41 UTC (rev 129)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixEditor.java 2009-03-24 15:33:41 UTC (rev 130)
@@ -33,7 +33,7 @@
// Inisialize matrixND
public abstract void setMatrix(MatrixND matrix);
-
+
/**
* @return Returns the linearModel.
*/
@@ -70,7 +70,6 @@
public void setVisible(Boolean visible){
this.visible = visible;
getTable().setVisible(visible);
- getButtonEdit().setVisible(visible);
super.setVisible(visible);
}
1
0
[Lutinmatrix-commits] r129 - in lutinmatrix/trunk: . src/main/java/org/codelutin/math/matrix/gui
by sletellier@users.labs.libre-entreprise.org 23 Mar '09
by sletellier@users.labs.libre-entreprise.org 23 Mar '09
23 Mar '09
Author: sletellier
Date: 2009-03-23 14:07:41 +0000 (Mon, 23 Mar 2009)
New Revision: 129
Added:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/JAXXMatrixEditor.jaxx
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixEditor.java
Modified:
lutinmatrix/trunk/pom.xml
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEvent.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPopupMenu.java
Log:
JAXX implamentation of MatrixPanelEditor
Modified: lutinmatrix/trunk/pom.xml
===================================================================
--- lutinmatrix/trunk/pom.xml 2009-03-20 13:46:40 UTC (rev 128)
+++ lutinmatrix/trunk/pom.xml 2009-03-23 14:07:41 UTC (rev 129)
@@ -46,6 +46,14 @@
<scope>compile</scope>
</dependency>
+ <!--Jaxx-->
+ <dependency>
+ <groupId>org.codelutin.jaxx</groupId>
+ <artifactId>jaxx-runtime-swing</artifactId>
+ <version>${jaxx.version}</version>
+ <scope>compile</scope>
+ </dependency>
+
</dependencies>
<!-- ************************************************************* -->
@@ -66,7 +74,8 @@
<!-- id du projet du labs -->
<labs.id>63</labs.id>
<labs.project>${project.artifactId}</labs.project>
-
+
+ <jaxx.version>1.3-SNAPSHOT</jaxx.version>
<i18n.version>0.9</i18n.version>
<lutinutil.version>1.0.3</lutinutil.version>
</properties>
@@ -74,6 +83,27 @@
<build>
<plugins>
+ <plugin>
+ <groupId>org.codelutin.jaxx</groupId>
+ <artifactId>maven-jaxx-plugin</artifactId>
+ <version>${jaxx.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>generate</goal>
+ </goals>
+ <configuration>
+ <extraImports>
+ <value>jaxx.runtime.SwingUtil</value>
+ <value>static jaxx.runtime.Util.getStringValue</value>
+ </extraImports>
+ <addSourcesToClassPath>true</addSourcesToClassPath>
+ <addProjectClassPath>true</addProjectClassPath>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
<!-- plugin i18n -->
<plugin>
<groupId>org.codelutin</groupId>
Added: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/JAXXMatrixEditor.jaxx
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/JAXXMatrixEditor.jaxx (rev 0)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/JAXXMatrixEditor.jaxx 2009-03-23 14:07:41 UTC (rev 129)
@@ -0,0 +1,172 @@
+<!--
+/* *##%
+ * Copyright (C) 2005
+ * Ifremer, Code Lutin, Cedric Pineau, Benjamin Poussin
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * 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 Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *##%*/
+
+/* *
+ * IsisFish.java
+ *
+ * Created: 1 aout 2005 18:37:25 CEST
+ *
+ * @author Benjamin POUSSIN <poussin(a)codelutin.com>
+ * @version $Revision: 1312 $
+ *
+ * Last update: $Date: 2008-08-28 10:21:07 +0200 (jeu, 28 aoû 2008) $
+ * by : $Author: sletellier $
+ */
+ -->
+<MatrixEditor id='jaxxMatrixManel' layout='{new BorderLayout()}'>
+
+ <MatrixTableModel id='tableModel' javaBean='null'/>
+
+ <!-- if true, use linear representation of matrix -->
+ <Boolean id='linearModel' javaBean='false'/>
+
+ <!-- if false don't show default value in matrix (ex: 0) -->
+ <Boolean id='linearModelShowDefault' javaBean='false'/>
+
+ <!-- Boolean to autorize matrice dimension changes. -->
+ <Boolean id='dimensionEdit' javaBean='false'/>
+
+ <script><![CDATA[
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.Event;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
+import java.util.Iterator;
+
+import org.codelutin.i18n.I18n;
+import org.codelutin.math.matrix.MatrixFactory;
+import org.codelutin.math.matrix.MatrixND;
+import org.codelutin.util.ListenerSet;
+
+private final static int DEFAULT_WIDTH = 150;
+
+private final static int DEFAULT_HEIGHT = 150;
+
+protected ListenerSet listeners = new ListenerSet();
+protected MatrixPopupMenu popupMenu = null;
+protected MatrixND matrix = null;
+initObject();
+public JAXXMatrixEditor(MatrixND m, boolean dimensionEdit) {
+ this(dimensionEdit, DEFAULT_WIDTH, DEFAULT_HEIGHT);
+}
+
+public JAXXMatrixEditor(boolean dimensionEdit, int width, int height) {
+ this.dimensionEdit = dimensionEdit;
+ setPreferredSize(new Dimension(width, height));
+}
+
+public JAXXMatrixEditor(boolean dimensionEdit) {
+ this(dimensionEdit, DEFAULT_WIDTH, DEFAULT_HEIGHT);
+}
+
+public void setMatrix(MatrixND m){
+ this.matrix = m;
+ initObject();
+}
+
+public MatrixND getMatrix() {
+ return matrix;
+}
+
+protected MatrixFactory getFactory() {
+ return MatrixFactory.getInstance();
+}
+
+public void addMatrixListener(MatrixPanelListener l) {
+ listeners.add(l);
+}
+
+public void removeMatrixPanelListener(MatrixPanelListener l) {
+ listeners.remove(l);
+}
+
+protected void initObject() {
+ if (getMatrix() == null) {
+ editArea.setViewportView(null);
+ }
+ else {
+ popupMenu = new MatrixPopupMenu(this);
+ table = new JTable() {
+ public void processMouseEvent(MouseEvent event) {
+ if (event.isPopupTrigger()) {
+ popupMenu.show(event.getComponent(), event.getX(), event.getY());
+ }
+ super.processMouseEvent(event);
+ }
+ };
+
+ table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK),"copy");
+ table.getActionMap().put("copy",popupMenu.getSendToClipBoardSelectionCopyAction());
+ table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK),"paste");
+ table.getActionMap().put("paste",popupMenu.getSendToClipBoardCurrentPasteAction());
+ if (isLinearModel()) {
+ setTableModel(new MatrixTableModelLinear(getMatrix(), isLinearModelShowDefault()));
+ }
+ else {
+ setTableModel(new MatrixTableModelND(getMatrix()));
+ }
+
+ getTableModel().addTableModelListener(new TableModelListener() {
+
+ @Override
+ public void tableChanged(TableModelEvent e) {
+ fireEvent();
+ }
+ });
+
+ table.setModel(getTableModel());
+ table.setDefaultRenderer(String.class, tableModel.getMatrixCellRenderer());
+ editArea.setViewportView(table);
+ }
+ repaint();
+}
+
+protected void btnAction(){
+ String dim;
+ dim = JOptionPane.showInputDialog(null, I18n._("lutinmatrix.create.matrix.message"), I18n._("lutinmatrix.create.matrix.title"), JOptionPane.DEFAULT_OPTION);
+
+ if (dim != null) {
+ String[] sdim = dim.split(";");
+ int[] idim = new int[sdim.length];
+ for (int i = 0; i < idim.length; i++) {
+ idim[i] = Integer.parseInt(sdim[i]);
+ }
+ setMatrix(getFactory().create(idim));
+ }
+}
+
+protected void fireEvent() {
+ MatrixPanelEvent e = new MatrixPanelEvent(this);
+ for (Iterator i = listeners.iterator(); i.hasNext();) {
+ MatrixPanelListener l = (MatrixPanelListener) i.next();
+ l.matrixChanged(e);
+ }
+}
+ ]]>
+ </script>
+ <JButton id='buttonEdit' text='lutinmatrix.create.matrix.button' enabled='{isDimensionEdit()}'
+ onActionPerformed='btnAction()' constraints='BorderLayout.SOUTH'/>
+ <JScrollPane id='editArea' constraints='BorderLayout.CENTER'>
+ <JTable id='table' autoResizeMode='{JTable.AUTO_RESIZE_OFF}'
+ cellSelectionEnabled='{true}' selectionMode='{ListSelectionModel.SINGLE_INTERVAL_SELECTION}'/>
+ </JScrollPane>
+</MatrixEditor>
\ No newline at end of file
Added: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixEditor.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixEditor.java (rev 0)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixEditor.java 2009-03-23 14:07:41 UTC (rev 129)
@@ -0,0 +1,77 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.codelutin.math.matrix.gui;
+
+import javax.swing.JButton;
+import javax.swing.JPanel;
+import org.codelutin.math.matrix.MatrixND;
+import javax.swing.JTable;
+
+/**
+ *
+ * @author letellier
+ */
+public abstract class MatrixEditor extends JPanel {
+
+ protected boolean enabled = false;
+ protected boolean visible = false;
+
+ // Return JTable componant
+ public abstract JTable getTable();
+
+ // Return edit button componant
+ public abstract JButton getButtonEdit();
+
+ // Fire the matrix
+ protected abstract void fireEvent();
+
+ // Return matrixND
+ public abstract MatrixND getMatrix();
+
+ // Inisialize matrixND
+ public abstract void setMatrix(MatrixND matrix);
+
+ /**
+ * @return Returns the linearModel.
+ */
+ public abstract Boolean isLinearModel();
+
+ /**
+ * @param linearModel The linearModel to set.
+ */
+ public abstract void setLinearModel(Boolean b);
+
+ /**
+ * @return Returns the linearModelShowDefault.
+ */
+ public abstract Boolean isLinearModelShowDefault();
+
+ /**
+ * @param linearModelShowDefault The linearModelShowDefault to set.
+ */
+ public abstract void setLinearModelShowDefault(Boolean b);
+
+ /**
+ * Enabled component
+ */
+ public void setEnabled(Boolean enabled){
+ this.enabled = enabled;
+ getTable().setEnabled(enabled);
+ getButtonEdit().setEnabled(enabled);
+ super.setEnabled(enabled);
+ }
+
+ /**
+ * Enabled component
+ */
+ public void setVisible(Boolean visible){
+ this.visible = visible;
+ getTable().setVisible(visible);
+ getButtonEdit().setVisible(visible);
+ super.setVisible(visible);
+ }
+
+}
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java 2009-03-20 13:46:40 UTC (rev 128)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java 2009-03-23 14:07:41 UTC (rev 129)
@@ -64,7 +64,7 @@
* Mise a jour: $Date$
* par : $Author$
*/
-public class MatrixPanelEditor extends JPanel implements TableModelListener { // MatrixPanelEditor
+public class MatrixPanelEditor extends MatrixEditor implements TableModelListener { // MatrixPanelEditor
/** serialVersionUID */
private static final long serialVersionUID = 2097859265435050946L;
@@ -161,7 +161,7 @@
protected JButton bEdit = null;
- protected JButton getButtonEdit() {
+ public JButton getButtonEdit() {
if (bEdit == null) {
bEdit = new JButton(I18n._("lutinmatrix.create.matrix.button"));
bEdit.addActionListener(new ActionListener() {
@@ -191,14 +191,14 @@
/**
* @return Returns the linearModel.
*/
- public boolean isLinearModel() {
+ public Boolean isLinearModel() {
return this.linearModel;
}
/**
* @param linearModel The linearModel to set.
*/
- public void setLinearModel(boolean linearModel) {
+ public void setLinearModel(Boolean linearModel) {
this.linearModel = linearModel;
initObject(m);
}
@@ -206,14 +206,14 @@
/**
* @return Returns the linearModelShowDefault.
*/
- public boolean isLinearModelShowDefault() {
+ public Boolean isLinearModelShowDefault() {
return this.linearModelShowDefault;
}
/**
* @param linearModelShowDefault The linearModelShowDefault to set.
*/
- public void setLinearModelShowDefault(boolean linearModelShowDefault) {
+ public void setLinearModelShowDefault(Boolean linearModelShowDefault) {
this.linearModelShowDefault = linearModelShowDefault;
initObject(m);
}
@@ -345,6 +345,10 @@
if (tableModel != null) {
tableModel.setEnabled(enabled);
}
+ if (table != null){
+ table.setEnabled(enabled);
+ }
+ super.setEnabled(enabled);
// if (text != null) {
// text.setEditable(enabled);
// }
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEvent.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEvent.java 2009-03-20 13:46:40 UTC (rev 128)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEvent.java 2009-03-23 14:07:41 UTC (rev 129)
@@ -40,12 +40,12 @@
/**
* @param source
*/
- public MatrixPanelEvent(MatrixPanelEditor source) {
+ public MatrixPanelEvent(MatrixEditor source) {
super(source);
}
public MatrixND getMatrix() {
- MatrixND result = ((MatrixPanelEditor) getSource()).getMatrix();
+ MatrixND result = ((MatrixEditor) getSource()).getMatrix();
return result;
}
}
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPopupMenu.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPopupMenu.java 2009-03-20 13:46:40 UTC (rev 128)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPopupMenu.java 2009-03-23 14:07:41 UTC (rev 129)
@@ -62,7 +62,7 @@
/** serialVersionUID. */
private static final long serialVersionUID = 3349189688987885915L;
- private MatrixPanelEditor matrixEditor;
+ private MatrixEditor matrixEditor;
private JFileChooser fileChooser;
private JMenu sendToClipBoard;
@@ -80,7 +80,7 @@
private Action sendToFileSelectionCopyAction;
private Action sendToFileCurrentPasteAction;
- public MatrixPopupMenu(MatrixPanelEditor matrixEditor) {
+ public MatrixPopupMenu(MatrixEditor matrixEditor) {
super();
this.matrixEditor = matrixEditor;
1
0
[Lutinmatrix-commits] r128 - lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui
by sletellier@users.labs.libre-entreprise.org 20 Mar '09
by sletellier@users.labs.libre-entreprise.org 20 Mar '09
20 Mar '09
Author: sletellier
Date: 2009-03-20 13:46:40 +0000 (Fri, 20 Mar 2009)
New Revision: 128
Modified:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java
Log:
Bug visible matrix fixed
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java 2009-03-16 17:59:20 UTC (rev 127)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/gui/MatrixPanelEditor.java 2009-03-20 13:46:40 UTC (rev 128)
@@ -51,11 +51,11 @@
/**
* JPanel contenant une JTable pour afficher une Matrice a une ou deux
* dimension.
- *
+ *
* TODO: Une methode permettant de retourne la sous matrice de la selection que
* la matrice soit reprensentée en lineaire ou non. (avoir un mapping cellule de
* table vers element de matrice
- *
+ *
* Created: 29 oct. 2004
*
* @author Benjamin Poussin <poussin(a)codelutin.com>
@@ -94,12 +94,15 @@
/** Boolean to autorize table editing. */
protected boolean enabled = true;
+ /** Boolean to show matrix. */
+ protected boolean visible = true;
+
/** Boolean to autorize matrice dimension changes. */
protected boolean dimensionEdit;
/**
* Construct a new JPanel to edit matrix.
- *
+ *
* @param m the matrix to edit.
* @param dimensionEdit to enabled matrix dimension changes.
*/
@@ -110,7 +113,7 @@
/**
* Construct a new JPanel to edit matrix.
- *
+ *
* @param dimensionEdit to enabled matrix dimension changes.
* @param width width prefered for the component
* @param height height prefered for the component
@@ -123,7 +126,7 @@
/**
* Construct a new JPanel to edit matrix.
- *
+ *
* @param dimensionEdit to enabled matrix dimension changes.
*/
public MatrixPanelEditor(boolean dimensionEdit) {
@@ -217,7 +220,7 @@
/**
* Get the value of dimensionEdit.
- *
+ *
* @return value of dimensionEdit.
*/
public boolean isDimensionEdit() {
@@ -226,7 +229,7 @@
/**
* Set the value of dimensionEdit.
- *
+ *
* @param v Value to assign to dimensionEdit.
*/
public void setDimensionEdit(boolean v) {
@@ -273,6 +276,7 @@
// editArea.setColumnHeaderView(null);
// }
setEnabled(enabled);
+ setVisible(visible);
repaint();
}
@@ -360,6 +364,20 @@
return enabled;
}
+ /**
+ * Set the matrix visible. By default, the matrix is visible.
+ */
+ public void setVisible(boolean visible) {
+ if (table != null){
+ table.setVisible(visible);
+ }
+ super.setVisible(visible);
+ this.visible = visible;
+ }
+
+ public boolean isVisible() {
+ return visible;
+ }
/*
* @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
*/
1
0
[Lutinmatrix-commits] r127 - in lutinmatrix/trunk/src: main/java/org/codelutin/math/matrix test/java/org/codelutin/math/matrix
by jcouteau@users.labs.libre-entreprise.org 16 Mar '09
by jcouteau@users.labs.libre-entreprise.org 16 Mar '09
16 Mar '09
Author: jcouteau
Date: 2009-03-16 17:59:20 +0000 (Mon, 16 Mar 2009)
New Revision: 127
Modified:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java
lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixNDTest.java
Log:
Add addition and subtraction with a scalar to MatrixND
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java 2009-03-13 17:27:34 UTC (rev 126)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java 2009-03-16 17:59:20 UTC (rev 127)
@@ -876,6 +876,24 @@
});
return this;
}
+
+ public MatrixND adds(final double d) {
+ map(new MapFunction() {
+ public double apply(double val) {
+ return val + d;
+ }
+ });
+ return this;
+ }
+
+ public MatrixND minuss(final double d) {
+ map(new MapFunction() {
+ public double apply(double val) {
+ return val - d;
+ }
+ });
+ return this;
+ }
/**
* Determine si la matrice supporte l'import et l'export CSV
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java 2009-03-13 17:27:34 UTC (rev 126)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java 2009-03-16 17:59:20 UTC (rev 127)
@@ -575,6 +575,16 @@
* Multiplication d'une matrice par un scalaire
*/
public MatrixND divs(final double d);
+
+ /**
+ * Addition d'un scalaire à une matrice
+ */
+ public MatrixND adds(final double d);
+
+ /**
+ * Soustractiond'un scalaire à une matrice
+ */
+ public MatrixND minuss(final double d);
/**
* Donne la matrice sous forme de List de list ... de double
Modified: lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixNDTest.java
===================================================================
--- lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixNDTest.java 2009-03-13 17:27:34 UTC (rev 126)
+++ lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixNDTest.java 2009-03-16 17:59:20 UTC (rev 127)
@@ -395,8 +395,56 @@
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 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, 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 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, 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 });
1
0
[Lutinmatrix-commits] r126 - lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix
by chatellier@users.labs.libre-entreprise.org 13 Mar '09
by chatellier@users.labs.libre-entreprise.org 13 Mar '09
13 Mar '09
Author: chatellier
Date: 2009-03-13 17:27:34 +0000 (Fri, 13 Mar 2009)
New Revision: 126
Modified:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java
Log:
Fix javadoc for sumOverDim
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java 2009-03-04 10:29:44 UTC (rev 125)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java 2009-03-13 17:27:34 UTC (rev 126)
@@ -326,7 +326,7 @@
* Somme la matrice mais la matrice reste de la même dimension. la somme
* permet juste de regrouper dans une dimension un certain nombre de valeur.
* <p>
- * pour la matrice suivant
+ * pour la matrice suivante :
*
* <pre>
* 1 2 3 4
@@ -335,15 +335,15 @@
* 4 5 6 7
* </pre>
*
- * la somme sur la dimension 1 avec un pas de 2 donnera
+ * la somme sur la dimension 1 avec un pas de 2 donnera :
*
* <pre>
- * 4 6 8 10
- * 6 8 10 12
+ * 3 5 7 9
+ * 7 9 11 13
* </pre>
*
- * c'est à dire que la ligne 0 et la ligne 2 sont sommées. ainsi que la
- * ligne 1 avec la ligne 3.
+ * c'est à dire que la ligne 0 et la ligne 1 sont sommées. ainsi que la
+ * ligne 2 avec la ligne 3.
*
* @param dim la dimension sur lequel il faut faire les sommes
* @param step le pas qu'il faut utiliser pour regrouper les elements. Si le
1
0
[Lutinmatrix-commits] r125 - in lutinmatrix/trunk/src: main/java/org/codelutin/math/matrix test/java/org/codelutin/math/matrix
by chatellier@users.labs.libre-entreprise.org 04 Mar '09
by chatellier@users.labs.libre-entreprise.org 04 Mar '09
04 Mar '09
Author: chatellier
Date: 2009-03-04 10:29:44 +0000 (Wed, 04 Mar 2009)
New Revision: 125
Modified:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixStringEncoder.java
lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixStringEncoderTest.java
Log:
Specials chars on columns names \"
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixStringEncoder.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixStringEncoder.java 2009-03-04 09:44:40 UTC (rev 124)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixStringEncoder.java 2009-03-04 10:29:44 UTC (rev 125)
@@ -25,6 +25,7 @@
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
+import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codelutin.util.StringUtil;
@@ -166,7 +167,7 @@
String result = "[";
String sep = "";
for (int i = 0; i < dimNamesArray.length; i++) {
- result += sep + '"' + dimNamesArray[i] + '"';
+ result += sep + '"' + StringEscapeUtils.escapeJava(dimNamesArray[i]) + '"';
sep = ", ";
}
result += "]";
@@ -188,7 +189,9 @@
for (int i = 0; i < result.length; i++) {
result[i] = result[i].trim();
if (result[i].startsWith("\"") && result[i].endsWith("\"")) {
- result[i] = result[i].substring(1, result[i].length() - 1); // remove " and "
+ String resultString = result[i].substring(1, result[i].length() - 1);// remove " and "
+ resultString = StringEscapeUtils.unescapeJava(resultString);
+ result[i] = resultString;
}
}
return result;
Modified: lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixStringEncoderTest.java
===================================================================
--- lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixStringEncoderTest.java 2009-03-04 09:44:40 UTC (rev 124)
+++ lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixStringEncoderTest.java 2009-03-04 10:29:44 UTC (rev 125)
@@ -142,5 +142,39 @@
Assert.assertEquals(2.345, matrix.getValue(1, 1), 0);
Assert.assertEquals(-8.321, matrix.getValue(1, 4), 0);
}
+
+ @Test
+ public void testMatrixNamesSpecialChars() {
+
+ 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);
+ mat1.setDimensionNames(new String[]{"col \"1\", \"2\"","col \"3\", \"4\""});
+ String rep = stringEncoder.getMatrixAsString(mat1);
+
+ System.out.println(rep);
+
+ Assert.assertTrue("Null() semantics missing", rep.indexOf("null()") > 0);
+ Assert.assertTrue("Dimentation missing", rep.indexOf("3, 2, 1") > 0);
+ Assert.assertTrue("Data missing", rep.indexOf("[2.0]") > 0);
+ Assert.assertTrue("Wrong string encoding", rep.indexOf("\"col \\\"1\\\", \\\"2\\\"\", \"col \\\"3\\\", \\\"4\\\"\", \"\"") > 0);
+ }
+
+ @Test
+ public void testMatrixFromNamesSpecialChars() {
+ String representation = "[,[3, 2, 1],[\"col \\\"1\\\", \\\"2\\\"\", \"col \\\"3\\\", \\\"4\\\"\", \"\"],[[null(), null(), null()], [null(), null()], [null()]],[[[-1.0E-7], [0.0]], [[0.0], [2.0]], [[0.0], [5.0E-7]]]]";
+
+ MatrixND matrix = stringEncoder.getMatrixFromString(representation);
+
+ Assert.assertEquals(3, matrix.getDim()[0]);
+ Assert.assertEquals(2, matrix.getDim()[1]);
+ Assert.assertEquals(1, matrix.getDim()[2]);
+ Assert.assertEquals(-1.0E-7, matrix.getValue(0, 0, 0), 0);
+ Assert.assertEquals(2, matrix.getValue(1, 1, 0), 0);
+ Assert.assertEquals(5.0E-7, matrix.getValue(2, 1, 0), 0);
+ Assert.assertEquals("col \"1\", \"2\"", matrix.getDimensionNames()[0]);
+ Assert.assertEquals("col \"3\", \"4\"", matrix.getDimensionNames()[1]);
+ }
} // MatrixHelperTest
1
0
[Lutinmatrix-commits] r124 - lutinmatrix/trunk
by chatellier@users.labs.libre-entreprise.org 04 Mar '09
by chatellier@users.labs.libre-entreprise.org 04 Mar '09
04 Mar '09
Author: chatellier
Date: 2009-03-04 09:44:40 +0000 (Wed, 04 Mar 2009)
New Revision: 124
Modified:
lutinmatrix/trunk/pom.xml
Log:
Add beanutils dep.
Modified: lutinmatrix/trunk/pom.xml
===================================================================
--- lutinmatrix/trunk/pom.xml 2009-03-04 09:42:17 UTC (rev 123)
+++ lutinmatrix/trunk/pom.xml 2009-03-04 09:44:40 UTC (rev 124)
@@ -31,6 +31,13 @@
<version>2.4</version>
<scope>compile</scope>
</dependency>
+
+ <dependency>
+ <groupId>commons-beanutils</groupId>
+ <artifactId>commons-beanutils</artifactId>
+ <version>1.8.0</version>
+ <scope>compile</scope>
+ </dependency>
<dependency>
<groupId>commons-primitives</groupId>
1
0
[Lutinmatrix-commits] r123 - in lutinmatrix/trunk: . src/main/java/org/codelutin/math/matrix src/test/java/org/codelutin/math/matrix
by chatellier@users.labs.libre-entreprise.org 04 Mar '09
by chatellier@users.labs.libre-entreprise.org 04 Mar '09
04 Mar '09
Author: chatellier
Date: 2009-03-04 09:42:17 +0000 (Wed, 04 Mar 2009)
New Revision: 123
Added:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixStringEncoder.java
lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixStringEncoderTest.java
Modified:
lutinmatrix/trunk/pom.xml
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixFactory.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixHelper.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixNDImpl.java
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/SubMatrix.java
lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixHelperTest.java
Log:
Add matrix to string/from string convertion
Modified: lutinmatrix/trunk/pom.xml
===================================================================
--- lutinmatrix/trunk/pom.xml 2009-03-03 14:01:27 UTC (rev 122)
+++ lutinmatrix/trunk/pom.xml 2009-03-04 09:42:17 UTC (rev 123)
@@ -117,8 +117,19 @@
</plugin>
</plugins>
</build>
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>cobertura-maven-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>findbugs-maven-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </reporting>
-
<!-- ************************************************************* -->
<!-- *** Build Environment ************************************** -->
<!-- ************************************************************* -->
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java 2009-03-03 14:01:27 UTC (rev 122)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/AbstractMatrixND.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -198,11 +198,33 @@
return name;
}
- public void setDimensionName(String[] names) {
+ public String[] getDimensionNames() {
+ return dimNames;
+ }
+
+ public void setDimensionNames(String[] names) {
for (int i = 0; names != null && i < names.length; i++) {
setDimensionName(i, names[i]);
}
}
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Use #getDimensionNames()
+ */
+ public String[] getDimensionName() {
+ return getDimensionNames();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated Use #setDimensionName(String[])
+ */
+ public void setDimensionName(String[] names) {
+ setDimensionNames(names);
+ }
public void setDimensionName(int dim, String name) {
dimNames[dim] = name;
@@ -212,10 +234,6 @@
return dimNames[dim];
}
- public String[] getDimensionName() {
- return dimNames;
- }
-
public double getMaxOccurence() {
// on creer un tableau dans cette classe, car on ne sait pas sur quelle
// implantation on s'appuie. Mais dans les sous classes, si on a deja
@@ -513,7 +531,7 @@
// creation du resultat
MatrixND result = getFactory().create(getName(), semantics,
- getDimensionName());
+ getDimensionNames());
for (int i = 0; i < result.getDim(dim); i++) {
MatrixND temp = getSubMatrix(dim, i * step, step);
@@ -541,7 +559,7 @@
// creation du resultat
MatrixND result = getFactory().create(getName(), semantics,
- getDimensionName());
+ getDimensionNames());
MatrixND sub1 = this.getSubMatrix(dim, 0, start);
MatrixND sub2 = this.getSubMatrix(dim, start, nb).sumOverDim(dim);
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixFactory.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixFactory.java 2009-03-03 14:01:27 UTC (rev 122)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixFactory.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -35,165 +35,185 @@
*/
public class MatrixFactory { // MatrixFactory
- /*
- * following code in only an exception generator since lutinxml in not used
- * anymore :) static {
- * // on essai d'enregistrer le converter XML try { // Il faut le faire par
- * pur introspection sinon l'exception // NoClassDefFoundError est levé
- * avant d'entrer dans le constructeur // static :( Class converterClass =
- * Class .forName("org.codelutin.math.matrix.MatrixNDXMLConverter"); Object
- * converter = converterClass.newInstance();
- *
- * Class converterFactoryClass = Class
- * .forName("org.codelutin.xml.XMLConverterFactory"); Method m =
- * converterFactoryClass.getMethod("addConverter", Class.class, Class
- * .forName("org.codelutin.xml.XMLConverter")); m.invoke(null,
- * MatrixND.class, converter);
- * // org.codelutin.xml.XMLConverterFactory.addConverter(MatrixND.class, //
- * new MatrixNDXMLConverter()); log.info("Converter XML pour MatrixND
- * ajoute");
- * // on essai d'enregistrer le converter JDBC // le JDBC depend du XML try {
- * converterClass = Class
- * .forName("org.codelutin.math.matrix.MatrixNDJDBCConverter"); converter =
- * converterClass.newInstance();
- *
- * converterFactoryClass = Class
- * .forName("org.codelutin.topia.persistence.jdbctransformer.JDBCTransformerFactory");
- * m = converterFactoryClass .getMethod( "addConverter", Class.class,
- * Class.forName("org.codelutin.topia.persistence.jdbctransformer.JDBCTransformer"));
- * m.invoke(null, MatrixND.class, converter);
- * // Class converterClass = //
- * org.codelutin.math.matrix.MatrixNDJDBCConverter(); //
- * JDBCTransformerFactory // .addConverter(MatrixND.class, new
- * MatrixNDJDBCConverter()); log.info("Converter JDBC pour MatrixND
- * ajoute"); } catch (Throwable eee) { log .info("librairie topia non
- * presente. Import/Export JDBC impossible"); log.debug("L'exception etait",
- * eee); }
- * } catch (Throwable eee) { log .info("librairie lutinxml non presente.
- * Import/Export XML impossible"); log.debug("L'exception etait", eee); }
- * }
- */
+ /* following code in only an exception generator since
+ * lutinxml in not used anymore :)
+ static {
- /** Valeur par defaut si aucun type de Vector n'est donné */
- protected static Class defaultVectorClass = DoubleBigVector.class;
+ // on essai d'enregistrer le converter XML
+ try {
+ // Il faut le faire par pur introspection sinon l'exception
+ // NoClassDefFoundError est levé avant d'entrer dans le constructeur
+ // static :(
+ Class converterClass = Class
+ .forName("org.codelutin.math.matrix.MatrixNDXMLConverter");
+ Object converter = converterClass.newInstance();
- protected Class vectorClass = null;
+ Class converterFactoryClass = Class
+ .forName("org.codelutin.xml.XMLConverterFactory");
+ Method m = converterFactoryClass.getMethod("addConverter",
+ Class.class, Class
+ .forName("org.codelutin.xml.XMLConverter"));
+ m.invoke(null, MatrixND.class, converter);
- protected MatrixFactory(Class vectorClass) {
- this.vectorClass = vectorClass;
- }
+ // org.codelutin.xml.XMLConverterFactory.addConverter(MatrixND.class,
+ // new MatrixNDXMLConverter());
+ log.info("Converter XML pour MatrixND ajoute");
- public static void setDefaultVectorClass(Class vectorClass) {
- defaultVectorClass = vectorClass;
- }
+ // on essai d'enregistrer le converter JDBC
+ // le JDBC depend du XML
+ try {
+ converterClass = Class
+ .forName("org.codelutin.math.matrix.MatrixNDJDBCConverter");
+ converter = converterClass.newInstance();
- public static Class getDefaultVectorClass() {
- return defaultVectorClass;
- }
+ converterFactoryClass = Class
+ .forName("org.codelutin.topia.persistence.jdbctransformer.JDBCTransformerFactory");
+ m = converterFactoryClass
+ .getMethod(
+ "addConverter",
+ Class.class,
+ Class.forName("org.codelutin.topia.persistence.jdbctransformer.JDBCTransformer"));
+ m.invoke(null, MatrixND.class, converter);
- /**
- * Retourne une factory utilisant vectorClass comme classe de base a
- * l'implantation des matrices
- */
- public static MatrixFactory getInstance(Class vectorClass) {
- return new MatrixFactory(vectorClass);
- }
+ // Class converterClass =
+ // org.codelutin.math.matrix.MatrixNDJDBCConverter();
+ // JDBCTransformerFactory
+ // .addConverter(MatrixND.class, new MatrixNDJDBCConverter());
+ log.info("Converter JDBC pour MatrixND ajoute");
+ } catch (Throwable eee) {
+ log
+ .info("librairie topia non presente. Import/Export JDBC impossible");
+ log.debug("L'exception etait", eee);
+ }
- /**
- * Utilise par defaut {@link FloatBigVector}
- */
- public static MatrixFactory getInstance() {
- return getInstance(defaultVectorClass);
- }
+ } catch (Throwable eee) {
+ log
+ .info("librairie lutinxml non presente. Import/Export XML impossible");
+ log.debug("L'exception etait", eee);
+ }
- public MatrixND create(int[] dim) {
- return new MatrixNDImpl(this, dim);
- }
+ }*/
- /**
- * Convert a double array into matrix.
- *
- * @param values
- * The values to fill the matrix
- * @param dim
- * An array representing the dimensions of the matrix
- * @return a 2D matrix filled with the values, null if the dimension is more
- * than 2
- */
- public MatrixND create(double[] values, int[] dim) {
+ /** Valeur par defaut si aucun type de Vector n'est donné */
+ protected static Class defaultVectorClass = DoubleBigVector.class;
- if (dim.length > 2) {
- return null;
- }
- MatrixNDImpl matrix = new MatrixNDImpl(this, dim);
+ protected Class vectorClass = null;
- if (dim.length == 2) {
- for (int i = 0; i < dim[0]; i++) {
- for (int j = 0; j < dim[1]; j++) {
- int[] coordinates = { i, j };
- matrix.setValue(coordinates, values[i * dim[1] + j]);
- }
- }
- }
- if (dim.length == 1) {
- for (int i = 0; i < dim[0]; i++) {
- int[] coordinates = { i };
- matrix.setValue(coordinates, values[i]);
- }
- }
+ protected MatrixFactory(Class vectorClass) {
+ this.vectorClass = vectorClass;
+ }
- return matrix;
- }
+ public static void setDefaultVectorClass(Class vectorClass) {
+ defaultVectorClass = vectorClass;
+ }
- public MatrixND create(List[] semantics) {
- return new MatrixNDImpl(this, semantics);
- }
+ public static Class getDefaultVectorClass() {
+ return defaultVectorClass;
+ }
- public MatrixND create(String name, int[] dim) {
- return new MatrixNDImpl(this, name, dim);
- }
+ /**
+ * Retourne une factory utilisant vectorClass comme classe de base a
+ * l'implantation des matrices
+ */
+ public static MatrixFactory getInstance(Class vectorClass) {
+ return new MatrixFactory(vectorClass);
+ }
- public MatrixND create(String name, int[] dim, String[] dimNames) {
- return new MatrixNDImpl(this, name, dim, dimNames);
- }
+ /**
+ * Utilise par defaut {@link FloatBigVector}
+ */
+ public static MatrixFactory getInstance() {
+ return getInstance(defaultVectorClass);
+ }
- public MatrixND create(String name, List[] semantics) {
- return new MatrixNDImpl(this, name, semantics);
- }
+ public MatrixND create(int[] dim) {
+ return new MatrixNDImpl(this, dim);
+ }
- public MatrixND create(String name, List[] semantics, String[] dimNames) {
- return new MatrixNDImpl(this, name, semantics, dimNames);
- }
+ /**
+ * Convert a double array into matrix.
+ *
+ * @param values
+ * The values to fill the matrix
+ * @param dim
+ * An array representing the dimensions of the matrix
+ * @return a 2D matrix filled with the values, null if the dimension is more
+ * than 2
+ */
+ public MatrixND create(double[] values, int[] dim) {
- public MatrixND create(MatrixND matrix) {
- return new MatrixNDImpl(this, matrix);
- }
+ if (dim.length > 2) {
+ return null;
+ }
+ MatrixNDImpl matrix = new MatrixNDImpl(this, dim);
- /**
- * Crée une nouvelle matrice identité. Une matrice identité est une matrice
- * à 2 dimensions dont tous les éléments de la diagonal vaut 1
- *
- * @param size
- * la taille de la matrice
- * @return une nouvelle matrice identité
- */
- public MatrixND matrixId(int size) {
- MatrixND result = create(new int[] { size, size });
- for (int i = 0; i < size; i++) {
- result.setValue(i, i, 1);
- }
- return result;
- }
+ if (dim.length == 2) {
+ for (int i = 0; i < dim[0]; i++) {
+ for (int j = 0; j < dim[1]; j++) {
+ int[] coordinates = { i, j };
+ matrix.setValue(coordinates, values[i * dim[1] + j]);
+ }
+ }
+ }
+ if (dim.length == 1) {
+ for (int i = 0; i < dim[0]; i++) {
+ int[] coordinates = { i };
+ matrix.setValue(coordinates, values[i]);
+ }
+ }
- protected Vector createVector(int length) {
- try {
- Constructor c = vectorClass
- .getConstructor(new Class[] { Integer.TYPE });
- return (Vector) c.newInstance(new Object[] { length });
- } catch (Exception eee) {
- throw new RuntimeException("Can't create vector", eee);
- }
- }
+ return matrix;
+ }
+ public MatrixND create(List[] semantics) {
+ return new MatrixNDImpl(this, semantics);
+ }
+
+ public MatrixND create(String name, int[] dim) {
+ return new MatrixNDImpl(this, name, dim);
+ }
+
+ public MatrixND create(String name, int[] dim, String[] dimNames) {
+ return new MatrixNDImpl(this, name, dim, dimNames);
+ }
+
+ public MatrixND create(String name, List[] semantics) {
+ return new MatrixNDImpl(this, name, semantics);
+ }
+
+ public MatrixND create(String name, List[] semantics, String[] dimNames) {
+ return new MatrixNDImpl(this, name, semantics, dimNames);
+ }
+
+ public MatrixND create(MatrixND matrix) {
+ return new MatrixNDImpl(this, matrix);
+ }
+
+ /**
+ * Crée une nouvelle matrice identité. Une matrice identité est une matrice
+ * à 2 dimensions dont tous les éléments de la diagonal vaut 1
+ *
+ * @param size
+ * la taille de la matrice
+ * @return une nouvelle matrice identité
+ */
+ public MatrixND matrixId(int size) {
+ MatrixND result = create(new int[] { size, size });
+ for (int i = 0; i < size; i++) {
+ result.setValue(i, i, 1);
+ }
+ return result;
+ }
+
+ protected Vector createVector(int length) {
+ try {
+ Constructor c = vectorClass
+ .getConstructor(new Class[] { Integer.TYPE });
+ return (Vector) c.newInstance(new Object[] { length });
+ } catch (Exception eee) {
+ throw new RuntimeException("Can't create vector", eee);
+ }
+ }
+
} // MatrixFactory
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixHelper.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixHelper.java 2009-03-03 14:01:27 UTC (rev 122)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixHelper.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -28,7 +28,7 @@
import org.apache.commons.logging.LogFactory;
/**
- * MatrixHelper.
+ * Contains usefull methods to get information on matrix.
*
* Created: 28 oct. 2004
*
@@ -40,6 +40,9 @@
*/
public class MatrixHelper { // MatrixHelper
+ /** Class logger */
+ private static Log log = LogFactory.getLog(MatrixHelper.class);
+
/**
* Convert Matrix to identity matrix must have 2 dimensions. If dimension
* haven't same length, then the small dimension is used.
@@ -253,21 +256,6 @@
return mat;
}
- // /**
- // * Crée une nouvelle matrice identité. Une matrice identité est une
- // * matrice à 2 dimensions dont tous les éléments de la diagonale
- // * valent 1
- // * @param size la taille de la matrice
- // * @return une nouvelle matrice identité
- // */
- // static public MatrixND matrixId(int size){
- // MatrixND result = new MatrixNDImpl(new int[]{size, size});
- // for(int i=0; i<size; i++){
- // result.setValue(i, i , 1);
- // }
- // return result;
- // }
-
/**
* Retourne la valeur la plus courrement rencontrer dans un tableau. si
* plusieurs valeurs ont le même nombre d'occurence la plus petite valeur
@@ -335,21 +323,4 @@
return result;
}
- // static public String encodeToXML(MatrixND mat){
- // StringWriter out = new StringWriter();
- // XMLEncoderDecoder.getInstance().encode(out, mat);
- // return out.toString();
- // }
- //
- // static public MatrixND decodeFromXML(String xml){
- // try{
- // return (MatrixND)XMLEncoderDecoder.getInstance().decode(new
- // StringReader(xml));
- // }catch(Exception eee){
- // throw new MatrixException("Erreur durant le decodage de la matrice",
- // eee);
- // }
- // }
-
} // MatrixHelper
-
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java 2009-03-03 14:01:27 UTC (rev 122)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixND.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -15,8 +15,6 @@
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%*/
-
-
package org.codelutin.math.matrix;
import java.io.IOException;
@@ -39,13 +37,20 @@
public interface MatrixND extends Serializable, Cloneable { // MatrixND
/**
- * Retourne la factory qui a permit de creer la matrice
+ * Retourne la factory qui a permit de creer la matrice.
+ *
+ * @return la {@link MatrixFactory}
+ *
+ * @see MatrixFactory
*/
public MatrixFactory getFactory();
/**
- * Donne toutes les semantiques de la matrice Si la matrice n'a pas de
- * semantique retourne null
+ * Donne toutes les semantiques de la matrice.
+ *
+ * Si la matrice n'a pas de semantique retourne null.
+ *
+ * @return la liste des semantics
*/
public List[] getSemantics();
@@ -65,6 +70,8 @@
/**
* Permet de donner un nom à la matrice.
+ *
+ * @param name name to set
*/
public void setName(String name);
@@ -77,10 +84,39 @@
/**
* Permet de mettre des noms aux différentes dimension.
+ *
+ * @deprecated (since 1.0.3) Use #setDimensionNames(String[])
*/
public void setDimensionName(String[] names);
/**
+ * Permet de mettre des noms aux différentes dimension.
+ *
+ * @param names names to set
+ *
+ * @since 1.0.3
+ */
+ public void setDimensionNames(String[] names);
+
+ /**
+ * Permet de recuperer les noms des dimension.
+ *
+ * @return tableau des noms de dimension.
+ *
+ * @deprecated (since 1.0.3) Use #getDimensionNames()
+ */
+ public String[] getDimensionName();
+
+ /**
+ * Permet de recuperer les noms des dimension.
+ *
+ * @return tableau des noms de dimension.
+ *
+ * @since 1.0.3
+ */
+ public String[] getDimensionNames();
+
+ /**
* Permet de mettre un nom à une dimension.
*
* @param dim la dimension dont on veut changer le nom
@@ -97,8 +133,6 @@
*/
public String getDimensionName(int dim);
- public String[] getDimensionName();
-
/**
* Retourne la valeur la plus courrement rencontrer dans un tableau. si
* plusieurs valeurs ont le même nombre d'occurence la plus petite valeur
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixNDImpl.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixNDImpl.java 2009-03-03 14:01:27 UTC (rev 122)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixNDImpl.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -72,7 +72,7 @@
protected MatrixNDImpl(MatrixFactory factory, MatrixND matrix) {
super(factory, matrix.getName(), matrix.getSemantics(), matrix
- .getDimensionName());
+ .getDimensionNames());
this.matrix = new BasicMatrix(factory, dim);
this.paste(matrix);
}
Added: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixStringEncoder.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixStringEncoder.java (rev 0)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixStringEncoder.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -0,0 +1,342 @@
+/* *##% lutinmatrix
+ * Copyright (C) 2004 - 2009 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>. ##%*/
+
+package org.codelutin.math.matrix;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.beanutils.BeanUtilsBean;
+import org.apache.commons.beanutils.ConvertUtilsBean;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codelutin.util.StringUtil;
+
+/**
+ * Convert matrix into {@link String} and inverse.
+ *
+ * Method from this class are non "static" to be overriden.
+ *
+ * Created: 04 mar. 2009
+ *
+ * @author chatellier
+ * @version $Revision: 120 $
+ *
+ * Mise a jour: $Date: 2009-03-03 11:19:18 +0100 (mar. 03 mars 2009) $
+ * par : $Author: chatellier $
+ */
+public class MatrixStringEncoder { // MatrixStringEncoder
+
+ /** Class logger */
+ private static Log log = LogFactory.getLog(MatrixStringEncoder.class);
+
+ /**
+ * Convert a matrix in string representation.
+ *
+ * String representation is composed in (ordered) :
+ * - name
+ * - dim
+ * - dimNames
+ * - semantics
+ * - data
+ *
+ * @param matrix matrix to convert
+ * @return a {@link String} representation
+ */
+ public String getMatrixAsString(MatrixND matrix) {
+ StringBuffer representationBuffer = new StringBuffer();
+ representationBuffer.append("["); // top level
+ representationBuffer.append(matrix.getName());
+ representationBuffer.append(",");
+ representationBuffer.append(getDimToString(matrix.getDim()));
+ representationBuffer.append(",");
+ representationBuffer.append(getDimensionNamesToString(matrix
+ .getDimensionNames()));
+ representationBuffer.append(",");
+ representationBuffer
+ .append(getSemanticsToString(matrix.getSemantics()));
+ representationBuffer.append(",");
+ representationBuffer.append(matrix.toList().toString());
+ representationBuffer.append("]"); // top level
+ return representationBuffer.toString();
+ }
+
+ /**
+ * Parse string as matrix representation.
+ *
+ * str must be in following format :
+ * - [name,dim,dimNames,semantics,data]
+ * @param str
+ * @return
+ */
+ public MatrixND getMatrixFromString(String str) {
+
+ MatrixND matrix = null;
+
+ // composed of 5 groups
+ Pattern matrixPattern = Pattern
+ .compile("^\\[(.*),(\\[.*\\]),(\\[.*\\]),(\\[.*\\]),(\\[.*\\])\\]$");
+ Matcher matcher = matrixPattern.matcher(str);
+
+ if (matcher.find()) {
+ String name = matcher.group(1);
+ String dimString = matcher.group(2);
+ String dimNamesString = matcher.group(3);
+ String semanticsString = matcher.group(4);
+ String dataString = matcher.group(5);
+
+ int[] dim = getDimFromString(dimString);
+ String[] dimNames = getDimensionNamesFromString(dimNamesString);
+ List<Object>[] semantics = getSemanticsFromString(semanticsString);
+
+ matrix = MatrixFactory.getInstance().create(name, semantics,
+ dimNames);
+ List<Object> data = MatrixHelper.convertStringToList(dataString);
+ matrix.fromList(data);
+ } else {
+ throw new IllegalArgumentException("Can't parse \"" + str
+ + "\" as string");
+ }
+
+ return matrix;
+ }
+
+ /**
+ * Matrix dim to string.
+ *
+ * @param dimArray dim to convert.
+ * @return a {@link String} representation
+ */
+ public String getDimToString(int[] dimArray) {
+ String result = "[";
+ String sep = "";
+ for (int i = 0; i < dimArray.length; i++) {
+ result += sep + dimArray[i];
+ sep = ", ";
+ }
+ result += "]";
+ return result;
+ }
+
+ /**
+ * String to matrix dim.
+ *
+ * @param str string to parse
+ * @return dim array
+ */
+ public int[] getDimFromString(String str) {
+ String localStr = str.trim();
+ if (localStr.startsWith("[") && localStr.endsWith("]")) {
+ localStr = localStr.substring(1, localStr.length() - 1); // remove [ and ]
+ }
+ String[] dimAsString = StringUtil.split(localStr, ",");
+ int[] result = new int[dimAsString.length];
+ int i = 0;
+ for (String dim : dimAsString) {
+ int val = Integer.parseInt(dim.trim());
+ result[i++] = val;
+ }
+ return result;
+ }
+
+ /**
+ * Dim names to string.
+ *
+ * @param dimNamesArray dim array to convert
+ * @return a {@link String} representation
+ */
+ public String getDimensionNamesToString(String[] dimNamesArray) {
+ String result = "[";
+ String sep = "";
+ for (int i = 0; i < dimNamesArray.length; i++) {
+ result += sep + '"' + dimNamesArray[i] + '"';
+ sep = ", ";
+ }
+ result += "]";
+ return result;
+ }
+
+ /**
+ * String to dim names array.
+ *
+ * @param str string to parse
+ * @return a {@link String} representation
+ */
+ public String[] getDimensionNamesFromString(String str) {
+ String localStr = str.trim();
+ if (localStr.startsWith("[") && localStr.endsWith("]")) {
+ localStr = localStr.substring(1, localStr.length() - 1); // remove [ and ]
+ }
+ String[] result = StringUtil.split(localStr, ",");
+ for (int i = 0; i < result.length; i++) {
+ result[i] = result[i].trim();
+ if (result[i].startsWith("\"") && result[i].endsWith("\"")) {
+ result[i] = result[i].substring(1, result[i].length() - 1); // remove " and "
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Semantics array to string.
+ *
+ * @param semanticsArray semantics arrayy
+ * @return string names array
+ */
+ public String getSemanticsToString(List<Object>[] semanticsArray) {
+ StringBuffer result = new StringBuffer("[");
+ for (int i = 0; i < semanticsArray.length; i++) {
+ result.append("[");
+ List<Object> semantics = semanticsArray[i];
+ for (Iterator<Object> it = semantics.iterator(); it.hasNext();) {
+ appendString(result, it.next());
+ if (it.hasNext()) {
+ result.append(", ");
+ }
+ }
+ result.append("]");
+ if ((i + 1) < semanticsArray.length) {
+ result.append(", ");
+ }
+ }
+ return result.append("]").toString();
+ }
+
+ /**
+ * String to samantics.
+ *
+ * @param str la chaine representant la semantique
+ * @return
+ */
+ public List<Object>[] getSemanticsFromString(String str) {
+ String localStr = str.trim();
+ if (localStr.startsWith("[") && localStr.endsWith("]")) {
+ localStr = localStr.substring(1, localStr.length() - 1); // remove [ and ]
+ }
+ String[] sems = StringUtil.split(localStr, ",");
+
+ List<Object>[] result = new List[sems.length];
+
+ for (int i = 0; i < sems.length; i++) {
+ result[i] = splitObjects(sems[i]);
+ }
+
+ return result;
+ }
+
+ /**
+ * Recréé chaque object de la chaine de caractere et l'ajoute dans une liste
+ * la chaine est de la forme.
+ *
+ * [null(), java.lang.String("toto"), ...]
+ *
+ * @param str la chaine representant
+ */
+ public List<Object> splitObjects(String str) {
+ List<Object> result = new LinkedList<Object>();
+ String localStr = str.trim();
+ if (localStr.startsWith("[") && localStr.endsWith("]")) {
+ localStr = localStr.substring(1, localStr.length() - 1);
+ }
+ String[] elems = StringUtil.split(localStr, ",");
+ for (String elem : elems) {
+ elem = elem.trim();
+ int openbrace = elem.indexOf('(');
+ String objectType = elem.substring(0, openbrace);
+ String objectString = elem.substring(openbrace + 1,
+ elem.length() - 1);
+
+ if ("null".equals(objectType)) {
+ result.add(null);
+ } else {
+ ConvertUtilsBean converter = getConverter();
+ Object o;
+ try {
+ o = converter.convert(objectString, Class
+ .forName(objectType));
+ } catch (Exception e) {
+ // if can't create objet, put String representation as semantics
+ o = objectType + "(" + objectString + ")";
+ if (log.isWarnEnabled()) {
+ log
+ .warn("Continuing but can't convert object in matrix from String: '"
+ + o + "'");
+ }
+ if (log.isDebugEnabled()) {
+ log.debug(
+ "Continuing but can't convert object in matrix from String: '"
+ + o + "'", e);
+ }
+ }
+ result.add(o);
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Append object type and value.
+ *
+ * qualifiedName(value)
+ * java.lang.String(test)
+ * java.lang.Double(4.5)
+ *
+ * @param buffer buffer to append to
+ * @param o object to put on buffer
+ * @return buffer
+ */
+ public StringBuffer appendString(StringBuffer buffer, Object o) {
+ if (o == null) {
+ buffer.append("null()");
+ } else {
+ String qualifiedName = getQualifiedName(o);
+ buffer.append(qualifiedName).append("(");
+ ConvertUtilsBean converter = getConverter();
+ buffer.append(converter.convert(o));
+ buffer.append(")");
+ }
+ return buffer;
+ }
+
+ /**
+ * Get object qualified name.
+ *
+ * Can't be overridden to put another impl.
+ *
+ * @param o object to get qulified name
+ * @return object qualified class name
+ */
+ public String getQualifiedName(Object o) {
+ String qualifiedName = o.getClass().getName();
+ return qualifiedName;
+ }
+
+ /**
+ * Get commons-beanutils bean converter.
+ * @return a {@link ConvertUtilsBean}
+ */
+ public ConvertUtilsBean getConverter() {
+ BeanUtilsBean instance = BeanUtilsBean.getInstance();
+ ConvertUtilsBean cub = instance.getConvertUtils();
+ return cub;
+ }
+
+} // MatrixStringEncoder
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/SubMatrix.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/SubMatrix.java 2009-03-03 14:01:27 UTC (rev 122)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/SubMatrix.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -46,7 +46,7 @@
public SubMatrix(MatrixND matrix, int dim, int start, int nb) {
super(matrix.getFactory(), matrix.getName(), matrix.getSemantics(),
- matrix.getDimensionName());
+ matrix.getDimensionNames());
this.matrix = matrix;
converter = new ShiftConverter(dim, start, nb);
@@ -56,7 +56,7 @@
public SubMatrix(MatrixND matrix, int dim, int[] elem) {
super(matrix.getFactory(), matrix.getName(), matrix.getSemantics(),
- matrix.getDimensionName());
+ matrix.getDimensionNames());
this.matrix = matrix;
converter = new MappingConverter(dim, elem);
Modified: lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixHelperTest.java
===================================================================
--- lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixHelperTest.java 2009-03-03 14:01:27 UTC (rev 122)
+++ lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixHelperTest.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -39,49 +39,51 @@
/** to use log facility, just put in your code: log.info(\"...\"); */
private static Log log = LogFactory.getLog(MatrixHelperTest.class);
-
- public MatrixFactory getFactory() throws Exception {
+
+ public MatrixFactory getFactory() {
return MatrixFactory.getInstance();
}
@Test
- public void testCoordinatesToString() throws Exception {
- 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 }));
+ 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,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() throws Exception {
- Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 1 }, new int[] { 1 }));
- Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 1, 2 }, new int[] {
- 1, 2 }));
+ 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() throws Exception {
+ public void testDimensionToSemantics() {
// TODO faire un test pour dimensionToSemantics
}
@Test
- public void testSemanticsToDimension() throws Exception {
+ public void testSemanticsToDimension() {
// TODO faire un test pour semanticsToDimension
}
@Test
- public void testFill() throws Exception {
+ public void testFill() {
MatrixND mat = getFactory().create(new int[] { 3, 3 });
MatrixHelper.fill(mat, 4);
@@ -89,9 +91,10 @@
}
@Test
- public void testMatrixId() throws Exception {
+ public void testMatrixId() {
MatrixND mat = getFactory().matrixId(4);
- Assert.assertTrue(MatrixHelper.sameDimension(new int[] { 4, 4 }, mat.getDim()));
+ 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);
@@ -100,7 +103,7 @@
}
@Test
- public void testToList() throws Exception {
+ 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);
@@ -118,7 +121,7 @@
}
@Test
- public void testMaxOccurence() throws Exception {
+ public void testMaxOccurence() {
double[] val = new double[5];
Assert.assertEquals(0, MatrixHelper.maxOccurence(val), 0);
@@ -154,7 +157,7 @@
val = new double[0];
try {
MatrixHelper.maxOccurence(val);
- Assert.fail("AN exception must be thrown");
+ Assert.fail("An exception must be thrown");
} catch (IllegalArgumentException e) {
if (log.isDebugEnabled()) {
log.debug("Exception normally thrown", e);
@@ -162,6 +165,6 @@
}
}
-
+
} // MatrixHelperTest
Added: lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixStringEncoderTest.java
===================================================================
--- lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixStringEncoderTest.java (rev 0)
+++ lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixStringEncoderTest.java 2009-03-04 09:42:17 UTC (rev 123)
@@ -0,0 +1,146 @@
+/* *##% lutinmatrix
+ * Copyright (C) 2004 - 2009 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>. ##%*/
+
+package org.codelutin.math.matrix;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * MatrixHelperTest.
+ *
+ * Created: 29 oct. 2004
+ *
+ * @author Benjamin Poussin <poussin(a)codelutin.com>
+ * @version $Revision: 120 $
+ *
+ * Mise a jour: $Date: 2009-03-03 11:19:18 +0100 (mar. 03 mars 2009) $
+ * par : $Author: chatellier $
+ */
+public class MatrixStringEncoderTest { // MatrixHelperTest
+
+ /** to use log facility, just put in your code: log.info(\"...\"); */
+ private static Log log = LogFactory.getLog(MatrixStringEncoderTest.class);
+
+ protected MatrixStringEncoder stringEncoder;
+
+ @Before
+ public void setUp() {
+ stringEncoder = new MatrixStringEncoder();
+ }
+
+ public MatrixFactory getFactory() {
+ return MatrixFactory.getInstance();
+ }
+
+ @Test
+ public void testMatrixStringRepresentation() {
+
+ 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);
+
+ String rep = stringEncoder.getMatrixAsString(mat1);
+
+ System.out.println(rep);
+
+ Assert.assertTrue("Null() semantics missing", rep.indexOf("null()") > 0);
+ Assert.assertTrue("Dimentation missing", rep.indexOf("3, 2, 1") > 0);
+ Assert.assertTrue("Data missing", rep.indexOf("[2.0]") > 0);
+ }
+
+ @Test
+ public void testMatrixFromString() {
+ String representation = "[,[3, 2, 1],[\"\", \"\", \"\"],[[null(), null(), null()], [null(), null()], [null()]],[[[-1.0E-7], [0.0]], [[0.0], [2.0]], [[0.0], [5.0E-7]]]]";
+
+ MatrixND matrix = stringEncoder.getMatrixFromString(representation);
+
+ Assert.assertEquals(3, matrix.getDim()[0]);
+ Assert.assertEquals(2, matrix.getDim()[1]);
+ Assert.assertEquals(1, matrix.getDim()[2]);
+ Assert.assertEquals(-1.0E-7, matrix.getValue(0, 0, 0), 0);
+ Assert.assertEquals(2, matrix.getValue(1, 1, 0), 0);
+ Assert.assertEquals(5.0E-7, matrix.getValue(2, 1, 0), 0);
+ }
+
+ @Test
+ public void testMatrixStringRepresentationWithMoreInfos() {
+
+ MatrixND mat1 = getFactory().create(new int[] { 2, 5 });
+ mat1.setName("StringRepresentationWithMoreInfos");
+ mat1.setValue(0, 0, -2);
+ mat1.setValue(1, 1, 2.345);
+ mat1.setValue(1, 4, -8.321);
+
+ List<Serializable> sem0 = new ArrayList<Serializable>();
+ sem0.add(new String("test"));
+ sem0.add(new Double(3.453));
+ mat1.setSemantics(0, sem0);
+ List<Serializable> sem1 = new ArrayList<Serializable>();
+ sem1.add(new Integer(7));
+ sem1.add(new Character('e'));
+ sem1.add(Byte.valueOf("1"));
+ sem1.add(Short.valueOf("3"));
+ sem1.add(new Integer(12));
+ mat1.setSemantics(1, sem1);
+
+ mat1.setDimensionNames(new String[] {"col1", "col2"});
+
+ String rep = stringEncoder.getMatrixAsString(mat1);
+
+ System.out.println(rep);
+
+ Assert.assertTrue("java.lang.String semantics missing", rep.indexOf("java.lang.String") > 0);
+ Assert.assertTrue("Dimentation missing", rep.indexOf("[2, 5]") > 0);
+ Assert.assertTrue("Data missing", rep.indexOf("[0.0, 2.345, 0.0, 0.0, -8.321]") > 0);
+ Assert.assertTrue("Columns name missing", rep.indexOf("[\"col1\", \"col2\"]") > 0);
+ Assert.assertTrue("Name missing", rep.indexOf("StringRepresentationWithMoreInfos") > 0);
+
+ }
+
+ @Test
+ public void testMatrixFromStringWithMoreInfos() {
+ String representation = "[StringRepresentationWithMoreInfos,[2, 5],[\"col1\", \"col2\"],[[java.lang.String(test), java.lang.Double(3.453)], [java.lang.Integer(7), java.lang.Character(e), java.lang.Byte(1), java.lang.Short(3), java.lang.Integer(12)]],[[-2.0, 0.0, 0.0, 0.0, 0.0], [0.0, 2.345, 0.0, 0.0, -8.321]]]";
+
+ MatrixND matrix = stringEncoder.getMatrixFromString(representation);
+
+ Assert.assertEquals("StringRepresentationWithMoreInfos", matrix.getName());
+ Assert.assertEquals(2, matrix.getDim()[0]);
+ Assert.assertEquals(5, matrix.getDim()[1]);
+ Assert.assertEquals("col1", matrix.getDimensionNames()[0]);
+ Assert.assertEquals("col2", matrix.getDimensionNames()[1]);
+ Assert.assertEquals("test", matrix.getSemantics(0).get(0));
+ Assert.assertEquals(3.453, matrix.getSemantics(0).get(1));
+ Assert.assertEquals(7, matrix.getSemantics(1).get(0));
+ Assert.assertEquals('e', matrix.getSemantics(1).get(1));
+ Assert.assertEquals(Byte.valueOf("1"), matrix.getSemantics(1).get(2));
+ Assert.assertEquals(Short.valueOf("3"), matrix.getSemantics(1).get(3));
+ Assert.assertEquals(12, matrix.getSemantics(1).get(4));
+ Assert.assertEquals(-2, matrix.getValue(0, 0), 0);
+ Assert.assertEquals(2.345, matrix.getValue(1, 1), 0);
+ Assert.assertEquals(-8.321, matrix.getValue(1, 4), 0);
+ }
+} // MatrixHelperTest
+
1
0
[Lutinmatrix-commits] r122 - in lutinmatrix/trunk/src: main/java/org/codelutin/math/matrix test/java/org/codelutin/math/matrix
by jcouteau@users.labs.libre-entreprise.org 03 Mar '09
by jcouteau@users.labs.libre-entreprise.org 03 Mar '09
03 Mar '09
Author: jcouteau
Date: 2009-03-03 14:01:27 +0000 (Tue, 03 Mar 2009)
New Revision: 122
Modified:
lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixFactory.java
lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixNDTest.java
Log:
Correct bug on asymmetrical matrices.
Add tests.
Modified: lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixFactory.java
===================================================================
--- lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixFactory.java 2009-03-03 10:26:11 UTC (rev 121)
+++ lutinmatrix/trunk/src/main/java/org/codelutin/math/matrix/MatrixFactory.java 2009-03-03 14:01:27 UTC (rev 122)
@@ -26,190 +26,174 @@
* representation interne des matrices de facon simple.
* <p>
* Created: 11 octobre 2005 20:15:20 CEST
- *
+ *
* @author Benjamin POUSSIN <poussin(a)codelutin.com>
* @version $Revision$
- *
- * Last update: $Date$
- * by : $Author$
+ *
+ * Last update: $Date$ by :
+ * $Author$
*/
public class MatrixFactory { // MatrixFactory
- /* following code in only an exception generator since
- * lutinxml in not used anymore :)
- static {
+ /*
+ * following code in only an exception generator since lutinxml in not used
+ * anymore :) static {
+ * // on essai d'enregistrer le converter XML try { // Il faut le faire par
+ * pur introspection sinon l'exception // NoClassDefFoundError est levé
+ * avant d'entrer dans le constructeur // static :( Class converterClass =
+ * Class .forName("org.codelutin.math.matrix.MatrixNDXMLConverter"); Object
+ * converter = converterClass.newInstance();
+ *
+ * Class converterFactoryClass = Class
+ * .forName("org.codelutin.xml.XMLConverterFactory"); Method m =
+ * converterFactoryClass.getMethod("addConverter", Class.class, Class
+ * .forName("org.codelutin.xml.XMLConverter")); m.invoke(null,
+ * MatrixND.class, converter);
+ * // org.codelutin.xml.XMLConverterFactory.addConverter(MatrixND.class, //
+ * new MatrixNDXMLConverter()); log.info("Converter XML pour MatrixND
+ * ajoute");
+ * // on essai d'enregistrer le converter JDBC // le JDBC depend du XML try {
+ * converterClass = Class
+ * .forName("org.codelutin.math.matrix.MatrixNDJDBCConverter"); converter =
+ * converterClass.newInstance();
+ *
+ * converterFactoryClass = Class
+ * .forName("org.codelutin.topia.persistence.jdbctransformer.JDBCTransformerFactory");
+ * m = converterFactoryClass .getMethod( "addConverter", Class.class,
+ * Class.forName("org.codelutin.topia.persistence.jdbctransformer.JDBCTransformer"));
+ * m.invoke(null, MatrixND.class, converter);
+ * // Class converterClass = //
+ * org.codelutin.math.matrix.MatrixNDJDBCConverter(); //
+ * JDBCTransformerFactory // .addConverter(MatrixND.class, new
+ * MatrixNDJDBCConverter()); log.info("Converter JDBC pour MatrixND
+ * ajoute"); } catch (Throwable eee) { log .info("librairie topia non
+ * presente. Import/Export JDBC impossible"); log.debug("L'exception etait",
+ * eee); }
+ * } catch (Throwable eee) { log .info("librairie lutinxml non presente.
+ * Import/Export XML impossible"); log.debug("L'exception etait", eee); }
+ * }
+ */
- // on essai d'enregistrer le converter XML
- try {
- // Il faut le faire par pur introspection sinon l'exception
- // NoClassDefFoundError est levé avant d'entrer dans le constructeur
- // static :(
- Class converterClass = Class
- .forName("org.codelutin.math.matrix.MatrixNDXMLConverter");
- Object converter = converterClass.newInstance();
+ /** Valeur par defaut si aucun type de Vector n'est donné */
+ protected static Class defaultVectorClass = DoubleBigVector.class;
- Class converterFactoryClass = Class
- .forName("org.codelutin.xml.XMLConverterFactory");
- Method m = converterFactoryClass.getMethod("addConverter",
- Class.class, Class
- .forName("org.codelutin.xml.XMLConverter"));
- m.invoke(null, MatrixND.class, converter);
+ protected Class vectorClass = null;
- // org.codelutin.xml.XMLConverterFactory.addConverter(MatrixND.class,
- // new MatrixNDXMLConverter());
- log.info("Converter XML pour MatrixND ajoute");
+ protected MatrixFactory(Class vectorClass) {
+ this.vectorClass = vectorClass;
+ }
- // on essai d'enregistrer le converter JDBC
- // le JDBC depend du XML
- try {
- converterClass = Class
- .forName("org.codelutin.math.matrix.MatrixNDJDBCConverter");
- converter = converterClass.newInstance();
+ public static void setDefaultVectorClass(Class vectorClass) {
+ defaultVectorClass = vectorClass;
+ }
- converterFactoryClass = Class
- .forName("org.codelutin.topia.persistence.jdbctransformer.JDBCTransformerFactory");
- m = converterFactoryClass
- .getMethod(
- "addConverter",
- Class.class,
- Class.forName("org.codelutin.topia.persistence.jdbctransformer.JDBCTransformer"));
- m.invoke(null, MatrixND.class, converter);
+ public static Class getDefaultVectorClass() {
+ return defaultVectorClass;
+ }
- // Class converterClass =
- // org.codelutin.math.matrix.MatrixNDJDBCConverter();
- // JDBCTransformerFactory
- // .addConverter(MatrixND.class, new MatrixNDJDBCConverter());
- log.info("Converter JDBC pour MatrixND ajoute");
- } catch (Throwable eee) {
- log
- .info("librairie topia non presente. Import/Export JDBC impossible");
- log.debug("L'exception etait", eee);
- }
+ /**
+ * Retourne une factory utilisant vectorClass comme classe de base a
+ * l'implantation des matrices
+ */
+ public static MatrixFactory getInstance(Class vectorClass) {
+ return new MatrixFactory(vectorClass);
+ }
- } catch (Throwable eee) {
- log
- .info("librairie lutinxml non presente. Import/Export XML impossible");
- log.debug("L'exception etait", eee);
- }
+ /**
+ * Utilise par defaut {@link FloatBigVector}
+ */
+ public static MatrixFactory getInstance() {
+ return getInstance(defaultVectorClass);
+ }
- }*/
+ public MatrixND create(int[] dim) {
+ return new MatrixNDImpl(this, dim);
+ }
- /** Valeur par defaut si aucun type de Vector n'est donné */
- protected static Class defaultVectorClass = DoubleBigVector.class;
+ /**
+ * Convert a double array into matrix.
+ *
+ * @param values
+ * The values to fill the matrix
+ * @param dim
+ * An array representing the dimensions of the matrix
+ * @return a 2D matrix filled with the values, null if the dimension is more
+ * than 2
+ */
+ public MatrixND create(double[] values, int[] dim) {
- protected Class vectorClass = null;
+ if (dim.length > 2) {
+ return null;
+ }
+ MatrixNDImpl matrix = new MatrixNDImpl(this, dim);
- protected MatrixFactory(Class vectorClass) {
- this.vectorClass = vectorClass;
- }
+ if (dim.length == 2) {
+ for (int i = 0; i < dim[0]; i++) {
+ for (int j = 0; j < dim[1]; j++) {
+ int[] coordinates = { i, j };
+ matrix.setValue(coordinates, values[i * dim[1] + j]);
+ }
+ }
+ }
+ if (dim.length == 1) {
+ for (int i = 0; i < dim[0]; i++) {
+ int[] coordinates = { i };
+ matrix.setValue(coordinates, values[i]);
+ }
+ }
- public static void setDefaultVectorClass(Class vectorClass) {
- defaultVectorClass = vectorClass;
- }
+ return matrix;
+ }
- public static Class getDefaultVectorClass() {
- return defaultVectorClass;
- }
+ public MatrixND create(List[] semantics) {
+ return new MatrixNDImpl(this, semantics);
+ }
- /**
- * Retourne une factory utilisant vectorClass comme classe de base a
- * l'implantation des matrices
- */
- public static MatrixFactory getInstance(Class vectorClass) {
- return new MatrixFactory(vectorClass);
- }
+ public MatrixND create(String name, int[] dim) {
+ return new MatrixNDImpl(this, name, dim);
+ }
- /**
- * Utilise par defaut {@link FloatBigVector}
- */
- public static MatrixFactory getInstance() {
- return getInstance(defaultVectorClass);
- }
+ public MatrixND create(String name, int[] dim, String[] dimNames) {
+ return new MatrixNDImpl(this, name, dim, dimNames);
+ }
- public MatrixND create(int[] dim) {
- return new MatrixNDImpl(this, dim);
- }
+ public MatrixND create(String name, List[] semantics) {
+ return new MatrixNDImpl(this, name, semantics);
+ }
- /**
- * Convert a double array into matrix.
- *
- * @param values The values to fill the matrix
- * @param dim An array representing the dimensions of the matrix
- * @return a 2D matrix filled with the values, null if the dimension is more than 2
- */
- public MatrixND create(double[] values, int[] dim) {
+ public MatrixND create(String name, List[] semantics, String[] dimNames) {
+ return new MatrixNDImpl(this, name, semantics, dimNames);
+ }
- if (dim.length > 2) {
- return null;
- }
- MatrixNDImpl matrix = new MatrixNDImpl(this, dim);
+ public MatrixND create(MatrixND matrix) {
+ return new MatrixNDImpl(this, matrix);
+ }
- if (dim.length == 2) {
- for (int i = 0; i < dim[0]; i++) {
- for (int j = 0; j < dim[1]; j++) {
- int[] coordinates = { i, j };
- matrix.setValue(coordinates, values[i * dim[0] + j]);
- }
- }
- }
- if (dim.length == 1) {
- for (int i = 0; i < dim[0]; i++) {
- int[] coordinates = { i };
- matrix.setValue(coordinates, values[i]);
- }
- }
+ /**
+ * Crée une nouvelle matrice identité. Une matrice identité est une matrice
+ * à 2 dimensions dont tous les éléments de la diagonal vaut 1
+ *
+ * @param size
+ * la taille de la matrice
+ * @return une nouvelle matrice identité
+ */
+ public MatrixND matrixId(int size) {
+ MatrixND result = create(new int[] { size, size });
+ for (int i = 0; i < size; i++) {
+ result.setValue(i, i, 1);
+ }
+ return result;
+ }
- return matrix;
- }
+ protected Vector createVector(int length) {
+ try {
+ Constructor c = vectorClass
+ .getConstructor(new Class[] { Integer.TYPE });
+ return (Vector) c.newInstance(new Object[] { length });
+ } catch (Exception eee) {
+ throw new RuntimeException("Can't create vector", eee);
+ }
+ }
- public MatrixND create(List[] semantics) {
- return new MatrixNDImpl(this, semantics);
- }
-
- public MatrixND create(String name, int[] dim) {
- return new MatrixNDImpl(this, name, dim);
- }
-
- public MatrixND create(String name, int[] dim, String[] dimNames) {
- return new MatrixNDImpl(this, name, dim, dimNames);
- }
-
- public MatrixND create(String name, List[] semantics) {
- return new MatrixNDImpl(this, name, semantics);
- }
-
- public MatrixND create(String name, List[] semantics, String[] dimNames) {
- return new MatrixNDImpl(this, name, semantics, dimNames);
- }
-
- public MatrixND create(MatrixND matrix) {
- return new MatrixNDImpl(this, matrix);
- }
-
- /**
- * Crée une nouvelle matrice identité. Une matrice identité est une matrice
- * à 2 dimensions dont tous les éléments de la diagonal vaut 1
- *
- * @param size la taille de la matrice
- * @return une nouvelle matrice identité
- */
- public MatrixND matrixId(int size) {
- MatrixND result = create(new int[] { size, size });
- for (int i = 0; i < size; i++) {
- result.setValue(i, i, 1);
- }
- return result;
- }
-
- protected Vector createVector(int length) {
- try {
- Constructor c = vectorClass
- .getConstructor(new Class[] { Integer.TYPE });
- return (Vector) c.newInstance(new Object[] { length });
- } catch (Exception eee) {
- throw new RuntimeException("Can't create vector", eee);
- }
- }
-
} // MatrixFactory
Modified: lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixNDTest.java
===================================================================
--- lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixNDTest.java 2009-03-03 10:26:11 UTC (rev 121)
+++ lutinmatrix/trunk/src/test/java/org/codelutin/math/matrix/MatrixNDTest.java 2009-03-03 14:01:27 UTC (rev 122)
@@ -543,7 +543,33 @@
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));
1
0