Author: jcouteau Date: 2009-03-24 18:09:10 +0000 (Tue, 24 Mar 2009) New Revision: 2032 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/domain/MatrixContinuousDomain.java isis-fish/trunk/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java Log: Fully working MatrixContinuousDomain : - New implementation of getValueForIdentifier. - Unit tests Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/domain/MatrixContinuousDomain.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/domain/MatrixContinuousDomain.java 2009-03-24 17:05:50 UTC (rev 2031) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/sensitivity/domain/MatrixContinuousDomain.java 2009-03-24 18:09:10 UTC (rev 2032) @@ -20,22 +20,23 @@ import java.io.Serializable; +import org.codelutin.math.matrix.MapFunction; import org.codelutin.math.matrix.MatrixND; /** * Domaine continu portant sur une matrice. * * Composé d'un borne min et max et d'une matrice. - * - * @param <E> type des valeurs gérées par le domaine * + * @param <E> + * type des valeurs gérées par le domaine + * * @author chatellier * @version $Revision: 1.0 $ * - * Last update : $Date: 24 févr. 2009 $ - * By : $Author: chatellier $ + * Last update : $Date: 24 févr. 2009 $ By : $Author: chatellier $ */ -public class MatrixContinuousDomain<E extends Serializable> extends +public class MatrixContinuousDomain <E extends Serializable> extends ContinuousDomain<E> { /** serialVersionUID. */ @@ -69,7 +70,8 @@ /** * Set matrix. * - * @param matrix the matrix to set + * @param matrix + * the matrix to set */ public void setMatrix(MatrixND matrix) { this.matrix = matrix; @@ -83,7 +85,8 @@ } /** - * @param coefficient the coefficient to set + * @param coefficient + * the coefficient to set */ public void setCoefficient(double coefficient) { this.coefficient = coefficient; @@ -101,7 +104,8 @@ /** * Set operator. * - * @param operator the operator to set + * @param operator + * the operator to set */ public void setOperator(String operator) { this.operator = operator; @@ -110,18 +114,58 @@ /** * {@inheritDoc}. * + * In matrix continuous domain, always return 0 + */ + @Override + public double getMinBound() { + return 0; + } + + /** + * {@inheritDoc}. + * + * In matrix continuous domain, always return 1 + */ + @Override + public double getMaxBound() { + return 1; + } + + /** + * {@inheritDoc}. + * * In continuous domain, just return identifier */ - public E getValueForIdentifier(Serializable identifier) { + public E getValueForIdentifier(final Serializable identifier) { if (operator.equals("+")) { - this.matrix.adds((Double) identifier); + this.matrix.map(new MapFunction() { + public double apply(double val) { + return (val + ((val + coefficient) * ((2 * Double + .valueOf((String) identifier)) - 1))); + } + }); } else if (operator.equals("-")) { - this.matrix.minuss((Double) identifier); + this.matrix.map(new MapFunction() { + public double apply(double val) { + return (val + ((val - coefficient) * ((2 * Double + .valueOf((String) identifier)) - 1))); + } + }); } else if (operator.equals("*")) { - this.matrix.mults((Double) identifier); + this.matrix.map(new MapFunction() { + public double apply(double val) { + return (val + ((val * coefficient) * ((2 * Double + .valueOf((String) identifier)) - 1))); + } + }); } else if (operator.equals("/")) { - this.matrix.divs((Double) identifier); + this.matrix.map(new MapFunction() { + public double apply(double val) { + return (val + ((val / coefficient) * ((2 * Double + .valueOf((String) identifier)) - 1))); + } + }); } - return (E) identifier; + return (E) this.matrix; } } Modified: isis-fish/trunk/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java =================================================================== --- isis-fish/trunk/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java 2009-03-24 17:05:50 UTC (rev 2031) +++ isis-fish/trunk/src/test/java/fr/ifremer/isisfish/simulator/sensitivity/FactorTest.java 2009-03-24 18:09:10 UTC (rev 2032) @@ -25,18 +25,17 @@ import org.junit.Assert; import org.junit.Test; -import fr.ifremer.isisfish.simulator.sensitivity.Factor; import fr.ifremer.isisfish.simulator.sensitivity.domain.ContinuousDomain; import fr.ifremer.isisfish.simulator.sensitivity.domain.DiscreteDomain; +import fr.ifremer.isisfish.simulator.sensitivity.domain.MatrixContinuousDomain; /** * Factors test. - * + * * @author chatellier * @version $Revision: 1.0 $ * - * Last update : $Date: 23 févr. 2009 $ - * By : $Author: chatellier $ + * Last update : $Date: 23 févr. 2009 $ By : $Author: chatellier $ */ public class FactorTest { @@ -98,7 +97,8 @@ domain.getValues().put("m1", matrix1); domain.getValues().put("m2", matrix2); factor.setDomain(domain); - factor.setPath("org.codelutin.math.matrix.MatrixND#563456293453#2.456347646#dim"); + factor + .setPath("org.codelutin.math.matrix.MatrixND#563456293453#2.456347646#dim"); factor.setValueForIdentifier("m2"); Assert.assertEquals(matrix2, factor.getValue()); @@ -116,4 +116,134 @@ log.info("factor#toString() = " + factor); } } + + /** + * Test factor with matrix. + * + * @see MatrixND + */ + @Test + public void testMatrixContinuousFactor() { + + // matrix 1 + MatrixND matrix1 = MatrixFactory.getInstance().create("test1", + new int[] { 3, 2 }, new String[] { "col1", "col2" }); + matrix1.setValue(new int[] { 0, 0 }, 1); + matrix1.setValue(new int[] { 0, 1 }, -14); + matrix1.setValue(new int[] { 1, 0 }, 21); + matrix1.setValue(new int[] { 1, 1 }, 2); + matrix1.setValue(new int[] { 2, 0 }, 3); + matrix1.setValue(new int[] { 2, 1 }, -1); + + // factor + Factor<MatrixND> factor = new Factor<MatrixND>("testmatrix"); + MatrixContinuousDomain<MatrixND> domain = new MatrixContinuousDomain<MatrixND>(); + domain.setMatrix(matrix1); + domain.setCoefficient(0.1); + domain.setOperator("*"); + factor.setDomain(domain); + factor + .setPath("org.codelutin.math.matrix.MatrixND#563456293453#2.456347646#dim"); + factor.setValueForIdentifier("0.1"); + + Assert.assertEquals(0.92, factor.getValue().getValue(new int[] { 0, 0 }),0.0000001); + Assert.assertEquals(2.76, factor.getValue().getValue(new int[] { 2, 0 }),0.0000001); + Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); + Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); + + // matrix 2 + MatrixND matrix2 = MatrixFactory.getInstance().create("test1", + new int[] { 3, 2 }, new String[] { "col1", "col2" }); + matrix2.setValue(new int[] { 0, 0 }, 1); + matrix2.setValue(new int[] { 0, 1 }, -14); + matrix2.setValue(new int[] { 1, 0 }, 21); + matrix2.setValue(new int[] { 1, 1 }, 2); + matrix2.setValue(new int[] { 2, 0 }, 3); + matrix2.setValue(new int[] { 2, 1 }, -1); + + + // factor 2 + Factor<MatrixND> factor2 = new Factor<MatrixND>("testmatrix"); + MatrixContinuousDomain<MatrixND> domain2 = new MatrixContinuousDomain<MatrixND>(); + domain2.setMatrix(matrix2); + domain2.setCoefficient(0.1); + domain2.setOperator("/"); + factor2.setDomain(domain2); + factor2 + .setPath("org.codelutin.math.matrix.MatrixND#563456293453#2.456347646#dim"); + factor2.setValueForIdentifier("0.1"); + + // val + ((val / coefficient) * ((2 * Double + // .valueOf((String) identifier)) - 1)) + + Assert.assertEquals(-7, factor2.getValue().getValue(new int[] { 0, 0 }),0.0000001); + Assert.assertEquals(-21, factor2.getValue().getValue(new int[] { 2, 0 }),0.0000001); + Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); + Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); + + // matrix 3 + MatrixND matrix3 = MatrixFactory.getInstance().create("test1", + new int[] { 3, 2 }, new String[] { "col1", "col2" }); + matrix3.setValue(new int[] { 0, 0 }, 1); + matrix3.setValue(new int[] { 0, 1 }, -14); + matrix3.setValue(new int[] { 1, 0 }, 21); + matrix3.setValue(new int[] { 1, 1 }, 2); + matrix3.setValue(new int[] { 2, 0 }, 3); + matrix3.setValue(new int[] { 2, 1 }, -1); + + // factor 3 + Factor<MatrixND> factor3 = new Factor<MatrixND>("testmatrix"); + MatrixContinuousDomain<MatrixND> domain3 = new MatrixContinuousDomain<MatrixND>(); + domain3.setMatrix(matrix3); + domain3.setCoefficient(0.1); + domain3.setOperator("-"); + factor3.setDomain(domain3); + factor3 + .setPath("org.codelutin.math.matrix.MatrixND#563456293453#2.456347646#dim"); + factor3.setValueForIdentifier("0.1"); + + Assert.assertEquals(0.28, factor3.getValue().getValue(new int[] { 0, 0 }),0.0000001); + Assert.assertEquals(0.68, factor3.getValue().getValue(new int[] { 2, 0 }),0.0000001); + Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); + Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); + + // matrix 3 + MatrixND matrix4 = MatrixFactory.getInstance().create("test1", + new int[] { 3, 2 }, new String[] { "col1", "col2" }); + matrix4.setValue(new int[] { 0, 0 }, 1); + matrix4.setValue(new int[] { 0, 1 }, -14); + matrix4.setValue(new int[] { 1, 0 }, 21); + matrix4.setValue(new int[] { 1, 1 }, 2); + matrix4.setValue(new int[] { 2, 0 }, 3); + matrix4.setValue(new int[] { 2, 1 }, -1); + + // factor 3 + Factor<MatrixND> factor4 = new Factor<MatrixND>("testmatrix"); + MatrixContinuousDomain<MatrixND> domain4 = new MatrixContinuousDomain<MatrixND>(); + domain4.setMatrix(matrix4); + domain4.setCoefficient(0.1); + domain4.setOperator("+"); + factor4.setDomain(domain4); + factor4 + .setPath("org.codelutin.math.matrix.MatrixND#563456293453#2.456347646#dim"); + factor4.setValueForIdentifier("0.1"); + + Assert.assertEquals(0.12, factor4.getValue().getValue(new int[] { 0, 0 }),0.0000001); + Assert.assertEquals(0.52, factor4.getValue().getValue(new int[] { 2, 0 }),0.0000001); + Assert.assertEquals(0,((ContinuousDomain)factor.getDomain()).getMinBound(),0); + Assert.assertEquals(1,((ContinuousDomain)factor.getDomain()).getMaxBound(),0); + + try { + factor.setValueForIdentifier("blah blah"); + Assert.fail("Can't set identifier not present in domain"); + } catch (IllegalArgumentException e) { + if (log.isInfoEnabled()) { + log.info("Exception normally thrown"); + } + } + + if (log.isInfoEnabled()) { + log.info("factor#toString() = " + factor); + } + } }