Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* *##% NuitonMatrix
* 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
* . ##%*/
package org.nuiton.math.matrix;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.collections.primitives.ArrayIntList;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.util.ArrayUtil;
/**
* AbstractMatrixND.
*
* Created: 29 oct. 2004
*
* @author Benjamin Poussin
* @version $Revision: 199 $
*
* Mise a jour: $Date: 2009-11-04 15:22:06 +0100 (mer., 04 nov. 2009) $
* par : $Author: echatellier $
*/
public abstract class AbstractMatrixND implements MatrixND { // AbstractMatrixND
/** serialVersionUID. */
private static final long serialVersionUID = -6838751468730930727L;
/** to use log facility, just put in your code: log.info(\"...\"); */
private static Log log = LogFactory.getLog(AbstractMatrixND.class);
@Override
public abstract MatrixIterator iterator();
@Override
public abstract double getValue(int[] coordinates);
@Override
public abstract void setValue(int[] coordinates, double d);
protected transient DimensionHelper dimHelper = new DimensionHelper();
protected transient MatrixFactory factory = null;
protected String name = "";
protected String[] dimNames = null;
protected int[] dim = null;
protected List>[] semantics = null;
protected double defaultValue = 0;
/**
* Separateur CSV par défaut le point virgule.
*/
public static final char CSV_SEPARATOR = ';';
protected static final Pattern NUMBER = Pattern
.compile(" *[+-]?[0-9]*\\.?[0-9]+([eE][+-]?[0-9]+)? *");
protected void init(int[] dim) {
this.dim = new int[dim.length];
System.arraycopy(dim, 0, this.dim, 0, dim.length);
semantics = new List>[dim.length];
dimNames = new String[dim.length];
// par defaut chaine vide pour le nom des dimensions
Arrays.fill(dimNames, "");
}
protected AbstractMatrixND(MatrixFactory factory) {
this.factory = factory;
}
public AbstractMatrixND(MatrixFactory factory, int[] dim) {
this(factory);
init(dim);
for (int i = 0; i < getDimCount(); i++) {
// par defaut les listes des semantiques contiennent des nulls
semantics[i] = Collections.nCopies(dim[i], null);
}
}
public AbstractMatrixND(MatrixFactory factory, List>[] semantics) {
this(factory);
int[] dim = new int[semantics.length];
for (int i = 0; i < dim.length; i++) {
if (semantics[i] == null) {
dim[i] = 0;
} else {
dim[i] = semantics[i].size();
}
}
init(dim);
for (int i = 0; i < getDimCount(); i++) {
setSemantic(i, semantics[i]);
}
}
public AbstractMatrixND(MatrixFactory factory, String name, int[] dim) {
this(factory, dim);
setName(name);
}
public AbstractMatrixND(MatrixFactory factory, String name, int[] dim,
String[] dimNames) {
this(factory, dim);
setName(name);
for (int i = 0; dimNames != null && i < dimNames.length; i++) {
setDimensionName(i, dimNames[i]);
}
}
public AbstractMatrixND(MatrixFactory factory, String name,
List>[] semantics) {
this(factory, semantics);
setName(name);
}
public AbstractMatrixND(MatrixFactory factory, String name,
List>[] semantics, String[] dimNames) {
this(factory, name, semantics);
for (int i = 0; dimNames != null && i < dimNames.length; i++) {
setDimensionName(i, dimNames[i]);
}
}
/*
* @see org.nuiton.math.matrix.MatrixND#copy()
*/
@Override
public MatrixND copy() {
MatrixND result = getFactory().create(this);
return result;
}
/*
* @see java.lang.Object#clone()
*/
@Override
public MatrixND clone() {
return copy();
}
/**
* Retourne la factory utilisée pour créer cette matrice, la factory
* peut-être réutilisé pour créer d'autre matrice si besoin.
*/
@Override
public MatrixFactory getFactory() {
return factory;
}
@Override
public List>[] getSemantics() {
return semantics;
}
/**
* {@inheritDoc}
*
* @deprecated Use #getSemantics(dim)
*/
@Deprecated
@Override
public List> getSemantics(int dim) {
return getSemantic(dim);
}
@Override
public List> getSemantic(int dim) {
return semantics[dim];
}
/**
* {@inheritDoc}
*
* @deprecated Use #setSemantic(dim, List)
*/
@Deprecated
@Override
public void setSemantics(int dim, List sem) {
setSemantic(dim, sem);
}
@Override
public void setSemantic(int dim, List sem) {
if (!(sem instanceof SemanticList>)) {
sem = new SemanticList(sem);
}
// else SemanticList is immutable and can be used in many matrix in
// same time this permit to used same indexOf optimization
semantics[dim] = sem;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public String[] getDimensionNames() {
return dimNames;
}
@Override
public void setDimensionNames(String[] names) {
for (int i = 0; names != null && i < names.length; i++) {
setDimensionName(i, names[i]);
}
}
/**
* {@inheritDoc}
*
* @deprecated Use #getDimensionNames()
*/
@Deprecated
@Override
public String[] getDimensionName() {
return getDimensionNames();
}
/**
* {@inheritDoc}
*
* @deprecated Use #setDimensionName(String[])
*/
@Deprecated
@Override
public void setDimensionName(String[] names) {
setDimensionNames(names);
}
@Override
public void setDimensionName(int dim, String name) {
dimNames[dim] = name;
}
@Override
public String getDimensionName(int dim) {
return dimNames[dim];
}
@Override
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
// un tableau il ne faut pas le recréer, on peut le passer directement
int nbelem = 1;
for (int i = 0; i < getDimCount(); i++) {
nbelem *= getDim(i);
}
double[] data = new double[nbelem];
int i = 0;
for (MatrixIterator mi = iterator(); mi.next();) {
data[i++] = mi.getValue();
}
return MatrixHelper.maxOccurence(data);
}
/**
* {@inheritDoc}
*
* @deprecated use #getDimCount() instead
*/
@Override
public int getNbDim() {
return getDimCount();
}
@Override
public int getDimCount() {
return dim.length;
}
@Override
public int[] getDim() {
return dim;
}
@Override
public int getDim(int d) {
return dim[d];
}
/**
* Retourne la matrice elle meme. Les modifications sont faites directement
* dessus
*/
@Override
public MatrixND map(MapFunction f) {
for (MatrixIterator i = iterator(); i.next();) {
i.setValue(f.apply(i.getValue()));
}
return this;
}
@Override
public double getValue(Object[] coordinates) {
return getValue(MatrixHelper.semanticsToDimension(getSemantics(),
coordinates));
}
@Override
public double getValue(Object x) {
// on peut utiliser dimHelper car le get ne le reutilisera pas en
// interne
return getValue(dimHelper.get(x));
}
@Override
public double getValue(Object x, Object y) {
return getValue(dimHelper.get(x, y));
}
@Override
public double getValue(Object x, Object y, Object z) {
return getValue(dimHelper.get(x, y, z));
}
@Override
public double getValue(Object x, Object y, Object z, Object t) {
return getValue(dimHelper.get(x, y, z, t));
}
@Override
public double getValue(int x) {
// on peut utiliser dimHelper car le get ne le reutilisera pas en
// interne
return getValue(dimHelper.get(x));
}
@Override
public double getValue(int x, int y) {
return getValue(dimHelper.get(x, y));
}
@Override
public double getValue(int x, int y, int z) {
return getValue(dimHelper.get(x, y, z));
}
@Override
public double getValue(int x, int y, int z, int t) {
return getValue(dimHelper.get(x, y, z, t));
}
@Override
public void setValue(Object[] coordinates, double d) {
setValue(
MatrixHelper.semanticsToDimension(getSemantics(), coordinates),
d);
}
@Override
public void setValue(Object x, double d) {
setValue(dimHelper.get(x), d);
}
@Override
public void setValue(Object x, Object y, double d) {
setValue(dimHelper.get(x, y), d);
}
@Override
public void setValue(Object x, Object y, Object z, double d) {
setValue(dimHelper.get(x, y, z), d);
}
@Override
public void setValue(Object x, Object y, Object z, Object t, double d) {
setValue(dimHelper.get(x, y, z, t), d);
}
@Override
public void setValue(int x, double d) {
setValue(dimHelper.get(x), d);
}
@Override
public void setValue(int x, int y, double d) {
setValue(dimHelper.get(x, y), d);
}
@Override
public void setValue(int x, int y, int z, double d) {
setValue(dimHelper.get(x, y, z), d);
}
@Override
public void setValue(int x, int y, int z, int t, double d) {
setValue(dimHelper.get(x, y, z, t), d);
}
// TODO peut-etre faire une variante de equals qui regarde par rapport au
// coordonnées sémantique
@Override
public boolean equals(Object o) {
return o instanceof MatrixND && equals((MatrixND) o);
}
public boolean equals(MatrixND mat) {
boolean result = true;
// le nom doit être le même
result = result && getName().equals(mat.getName());
result = result && equalsValues(mat);
// les sémantiques doivent-être identique
for (int i = 0; result && i < getDimCount(); i++) {
String dimName1 = getDimensionName(i);
String dimName2 = mat.getDimensionName(i);
result = ObjectUtils.equals(dimName1, dimName2);
if (log.isTraceEnabled()) {
log.trace("dimName1(" + dimName1 + ")==dimName2(" + dimName2
+ ")=" + result);
}
// System.out.println("dimName1("+dimName1+")==dimName2("+dimName2+
// ")="+result);
List> sem1 = getSemantic(i);
List> sem2 = mat.getSemantic(i);
result = result && ObjectUtils.equals(sem1, sem2);
if (log.isTraceEnabled()) {
log.trace("sem1(" + sem1 + ")==sem2(" + sem2 + ")=" + result);
}
// System.out.println("sem1("+sem1+")==sem1("+sem2+ ")="+result);
}
if (log.isTraceEnabled()) {
log.trace("result=" + result);
}
// System.out.println("result="+result);
return result;
}
/**
* Verifie si les matrices sont egales en ne regardant que les valeurs et
* pas les semantiques
*
* @param mat
* @return equality on values
*/
@Override
public boolean equalsValues(MatrixND mat) {
boolean result = true;
// les dimensions doivent-être identique
result = result && MatrixHelper.sameDimension(getDim(), mat.getDim());
// toutes les données doivent être identique
for (MatrixIterator i = mat.iterator(); result && i.next();) {
double v1 = i.getValue();
double v2 = getValue(i.getCoordinates());
result = v1 == v2;
if (log.isTraceEnabled()) {
log.trace("v1(" + v1 + ")==v2(" + v2 + ")=" + result);
}
}
return result;
}
@Override
public String toString() {
StringBuffer result = new StringBuffer();
result.append("dimensions = [\n");
for (int i = 0; i < getDim().length; i++) {
result.append(getDim()[i] + ",");
}
result.append("\n]\nmatrice = [\n");
for (MatrixIterator i = this.iterator(); i.next();) {
result.append(i.getValue() + ",");
}
result.append("\n]\n");
return result.toString();
}
@Override
public List> toList() {
List> result = new ArrayList