Index: lutinmatrix/src/java/org/codelutin/math/matrix/FloatBigVector.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/FloatBigVector.java:1.1 --- /dev/null Fri Oct 21 14:27:28 2005 +++ lutinmatrix/src/java/org/codelutin/math/matrix/FloatBigVector.java Fri Oct 21 14:27:23 2005 @@ -0,0 +1,118 @@ +/* *##% + * Copyright (C) 2005 + * Code Lutin, Cédric 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. + *##%*/ + +/* * + * FloatBigVector.java + * + * Created: 6 octobre 2005 02:54:36 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/10/21 14:27:23 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix; + +import java.util.Arrays; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class FloatBigVector implements Vector { // FloatBigVector + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(FloatBigVector.class); + + protected float data[] = null; + + public FloatBigVector(int capacity){ + data = new float[capacity]; + } + + public int size() { + return data.length; + } + + public double getMaxOccurence() { + return MatrixHelper.maxOccurence(data); + } + + public double getValue(int pos) { + return data[pos]; + } + + public void setValue(int pos, double value) { + data[pos] = (float)value; + } + + public boolean equals(Object o) { + boolean result = false; + if (o instanceof FloatBigVector) { + FloatBigVector other = (FloatBigVector)o; + result = Arrays.equals(this.data, other.data); + } else if (o instanceof Vector) { + Vector other = (Vector)o; + result = true; + for(int i=0; i + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/10/21 14:27:23 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix; + +import java.util.Arrays; +import org.apache.commons.collections.primitives.ArrayFloatList; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** +* Permet de stocker des données à une position lineair et de la redemander +* Cette classe ne gére que les données lineaire. +* L'avantage de cette classe est de ne conserver que les elements differents +* de la valeur par defaut, ce qui minimize la taille du tableau necessaire +* a conserver les données. +*/ +public class FloatVector implements Vector { // FloatVector + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(FloatVector.class); + + /** maximum number of element, maximum pos value */ + protected int capacity = 0; + + /** la valeur par defaut */ + protected float defaultValue = 0; + + /** contient la position de l'element, le tableau est trie */ + protected int [] position; + protected int positionSize = 0; + + /** contient la valeur de l'element */ + protected ArrayFloatList data = new ArrayFloatList(); + + public FloatVector(int capacity) { + this.capacity = capacity; + position = new int[8]; + Arrays.fill(position, Integer.MAX_VALUE); + } + + public FloatVector(int capacity, float defaultValue){ + this(capacity); + this.defaultValue = defaultValue; + } + + public int size() { + return capacity; + } + + public double getMaxOccurence() { + float result = defaultValue; + + float [] tmp = data.toArray(); + + // si potentiellement il y a plus d'element identique dans data + // que de valeur par defaut, on recherche la valeur possible + if (this.capacity < 2*tmp.length) { + Arrays.sort(tmp); + + // le nombre de fois que l'on a rencontrer la valeur la plus nombreuse + int max = 1; + // le nombre de fois que l'on a rencontrer la valeur courante + int count = 1; + // la valeur la plus rencontrer + result = tmp[0]; + // la valeur que l'on vient de traiter précédement + float old = tmp[0]; + // la valeur courante lu dans le tableaux + float current = tmp[0]; + // tant que l'on peut encore trouve un element plus nombreux dans le + // tableau on le parcours + for(int i=1; max max){ + max = count; + result = old; + } + count = 1; + old = current; + } + } + if(count > max){ + max = count; + result = current; + } + + if(max <= capacity - tmp.length) { + // en fin de compte, il n'y a pas plus d'element identique + // dans data que de defaultValue + result = defaultValue; + } + } + + return result; + } + + protected void checkPos(int pos){ + if(pos < 0 || pos >= capacity) { + throw new IllegalArgumentException("pos " + pos + " is not in [0, "+capacity+"]"); + } + } + + public double getValue(int pos) { + checkPos(pos); + + float result = defaultValue; + int index = findIndex(pos); + if (index >= 0) { + result = data.get(index); + } + return result; + } + + /** + * On ajoute dans l'o + */ + public void setValue(int pos, double dValue) { + checkPos(pos); + + float value = (float)dValue; + int index = findIndex(pos); + if (index >= 0) { + if (value == defaultValue) { + // il etait present, on supprime l'element + removeElementAt(index); + data.removeElementAt(index); + } else { + // il etait deja present, on modifie la valeur + data.set(index, value); + } + } else { + // il n'etait pas present + if (value != defaultValue) { + // il faut ajouter dans position et dans data + index = -index -1; + + addElementAt(index, pos); + data.add(index, value); + } + } + } + + public boolean equals(Object o) { + boolean result = false; + if (o instanceof FloatVector) { + FloatVector other = (FloatVector)o; + result = Arrays.equals(this.position, other.position) && data.equals(other.data); + } else if (o instanceof Vector) { + Vector other = (Vector)o; + result = true; + for(int i=0; i position.length) { + int newcap = (position.length * 3) / 2 + 1; + int olddata[] = position; + position = new int[newcap >= mincap ? newcap : mincap]; + System.arraycopy(olddata, 0, position, 0, positionSize); + for(int i=positionSize; i 0) { + System.arraycopy(position, index + 1, position, index, numtomove); + } + positionSize--; + position[positionSize] = Integer.MAX_VALUE; + return oldval; + } + + + public boolean isImplementedPaste(Vector v) { + return v instanceof FloatVector; + } + public boolean isImplementedAdd(Vector v) { + // FIXME une fois la methode implanter supprimer le false + return false && v instanceof FloatVector; + } + public boolean isImplementedMinus(Vector v) { + // FIXME une fois la methode implanter supprimer le false + return false && v instanceof FloatVector; + } + public boolean isImplementedMap() { + return true; + } + + /** + * On recopie tous les attributs pour que le vector ressemble exactement + * a celui passé en argument + */ + public void paste(Vector v) { + FloatVector fbv = (FloatVector)v; + this.capacity = fbv.capacity; + this.defaultValue = fbv.defaultValue; + this.positionSize = fbv.positionSize; + this.position = new int[fbv.position.length]; + System.arraycopy(fbv.position, 0, this.position, 0, this.position.length); + this.data.clear(); + this.data.addAll(fbv.data); + } + + // FIXME a faire + public void add(Vector v) { + FloatVector fbv = (FloatVector)v; + for(int i=0; i=0; i--) { + double value = f.apply(data.get(i)); + if (value == defaultValue) { + // il etait present, on supprime l'element + removeElementAt(i); + data.removeElementAt(i); + } else { + // il etait deja present, on modifie la valeur + data.set(i, (float)value); + } + } + } +} // FloatVector + Index: lutinmatrix/src/java/org/codelutin/math/matrix/MatrixFactory.java diff -u /dev/null lutinmatrix/src/java/org/codelutin/math/matrix/MatrixFactory.java:1.1 --- /dev/null Fri Oct 21 14:27:28 2005 +++ lutinmatrix/src/java/org/codelutin/math/matrix/MatrixFactory.java Fri Oct 21 14:27:23 2005 @@ -0,0 +1,131 @@ +/* *##% + * Copyright (C) 2005 + * Code Lutin, Cédric 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. + *##%*/ + +/* * + * MatrixFactory.java + * + * Created: 11 octobre 2005 20:15:20 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/10/21 14:27:23 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix; + +import java.lang.reflect.Constructor; +import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** +* Cette classe permet de creer des matrices, toutes les creations de matrice +* doivent etre faite a travers cette classe. +* Cette classe permet de modifier la representation interne des matrices +* de facon simple. +*

+* +*/ +public class MatrixFactory { // MatrixFactory + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(MatrixFactory.class); + + /** Valeur par defaut si aucun type de Vector n'est donné */ + static protected Class defaultVectorClass = FloatVector.class; + + protected Class vectorClass = null; + + protected MatrixFactory(Class vectorClass) { + this.vectorClass = vectorClass; + } + + static public void setDefaultVectorClass(Class vectorClass) { + defaultVectorClass = vectorClass; + } + + static public Class getDefaultVectorClass() { + return defaultVectorClass; + } + + /** + * Retourne une factory utilisant vectorClass comme classe de base + * a l'implantation des matrices + */ + static public MatrixFactory getInstance(Class vectorClass) { + return new MatrixFactory(vectorClass); + } + + /** + * Utilise par defaut {@link FloatBigVector} + */ + static public MatrixFactory getInstance() { + return getInstance(defaultVectorClass); + } + + public MatrixND create(int [] dim){ + return new MatrixNDImpl(this, dim); + } + 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, 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 + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/10/21 14:27:23 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.math.matrix; + +public interface Vector { // Vector + + public double getMaxOccurence(); + public double getValue(int pos); + public void setValue(int pos, double value); + public int size(); + + /** + * Permet de savoir si paste est implanté par ce vector + */ + public boolean isImplementedPaste(Vector v); + /** + * Permet de savoir si add est implanté par ce vector + */ + public boolean isImplementedAdd(Vector v); + /** + * Permet de savoir si minus est implanté par ce vector + */ + public boolean isImplementedMinus(Vector v); + /** + * Permet de savoir si map est implanté par ce vector + */ + public boolean isImplementedMap(); + + /** + * Copie les valeurs du vector passé en argument dans ce vector + */ + public void paste(Vector v); + /** + * Ajoute les valeurs du vector passé en argument a ce vector + */ + public void add(Vector v); + /** + * soustrai les valeurs du vector passé en argument a ce vector + */ + public void minus(Vector v); + /** + * applique a chaque valeur du vector la MapFunction + */ + public void map(MapFunction f); + +} // Vector +