From aea0ab12424c527cdc167fd9d4db3bb3d9dfe338 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sat, 30 May 2009 18:36:18 +0000 Subject: [PATCH] added conversion utility functions for FieldMatrix to RealMatrix FieldMatrix to RealMatrix git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@780308 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/math/linear/MatrixUtils.java | 90 +++++++++++++++++++ .../commons/math/linear/MatrixUtilsTest.java | 25 ++++++ 2 files changed, 115 insertions(+) diff --git a/src/java/org/apache/commons/math/linear/MatrixUtils.java b/src/java/org/apache/commons/math/linear/MatrixUtils.java index 182d2bfed..a1c510193 100644 --- a/src/java/org/apache/commons/math/linear/MatrixUtils.java +++ b/src/java/org/apache/commons/math/linear/MatrixUtils.java @@ -24,6 +24,8 @@ import java.util.Arrays; import org.apache.commons.math.Field; import org.apache.commons.math.FieldElement; import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.fraction.BigFraction; +import org.apache.commons.math.fraction.Fraction; /** * A collection of static methods that operate on or return matrices. @@ -627,4 +629,92 @@ public class MatrixUtils { } } + /** + * Convert a {@link FieldMatrix}/{@link Fraction} matrix to a {@link RealMatrix}. + * @param m matrix to convert + * @return converted matrix + */ + public static RealMatrix fractionMatrixToRealMatrix(final FieldMatrix m) { + final FractionMatrixConverter converter = new FractionMatrixConverter(); + m.walkInOptimizedOrder(converter); + return converter.getConvertedMatrix(); + } + + /** Converter for {@link FieldMatrix}/{@link Fraction}. */ + private static class FractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor { + + /** Converted array. */ + private double[][] data; + + /** Simple constructor. */ + public FractionMatrixConverter() { + super(Fraction.ZERO); + } + + /** {@inheritDoc} */ + @Override + public void start(int rows, int columns, + int startRow, int endRow, int startColumn, int endColumn) { + data = new double[rows][columns]; + } + + /** {@inheritDoc} */ + @Override + public void visit(int row, int column, Fraction value) { + data[row][column] = value.doubleValue(); + } + + /** Get the converted matrix. + * @return converted matrix + */ + RealMatrix getConvertedMatrix() { + return new RealMatrixImpl(data, false); + } + + } + + /** + * Convert a {@link FieldMatrix}/{@link BigFraction} matrix to a {@link RealMatrix}. + * @param m matrix to convert + * @return converted matrix + */ + public static RealMatrix bigFractionMatrixToRealMatrix(final FieldMatrix m) { + final BigFractionMatrixConverter converter = new BigFractionMatrixConverter(); + m.walkInOptimizedOrder(converter); + return converter.getConvertedMatrix(); + } + + /** Converter for {@link FieldMatrix}/{@link BigFraction}. */ + private static class BigFractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor { + + /** Converted array. */ + private double[][] data; + + /** Simple constructor. */ + public BigFractionMatrixConverter() { + super(BigFraction.ZERO); + } + + /** {@inheritDoc} */ + @Override + public void start(int rows, int columns, + int startRow, int endRow, int startColumn, int endColumn) { + data = new double[rows][columns]; + } + + /** {@inheritDoc} */ + @Override + public void visit(int row, int column, BigFraction value) { + data[row][column] = value.doubleValue(); + } + + /** Get the converted matrix. + * @return converted matrix + */ + RealMatrix getConvertedMatrix() { + return new RealMatrixImpl(data, false); + } + + } + } diff --git a/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java b/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java index f1238ea7f..52d526d38 100644 --- a/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java +++ b/src/test/org/apache/commons/math/linear/MatrixUtilsTest.java @@ -21,6 +21,7 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.commons.math.fraction.BigFraction; import org.apache.commons.math.fraction.Fraction; import org.apache.commons.math.fraction.FractionConversionException; import org.apache.commons.math.fraction.FractionField; @@ -322,6 +323,30 @@ public final class MatrixUtilsTest extends TestCase { } } + public void testBigFractionConverter() { + BigFraction[][] bfData = { + { new BigFraction(1), new BigFraction(2), new BigFraction(3) }, + { new BigFraction(2), new BigFraction(5), new BigFraction(3) }, + { new BigFraction(1), new BigFraction(0), new BigFraction(8) } + }; + FieldMatrix m = new FieldMatrixImpl(bfData, false); + RealMatrix converted = MatrixUtils.bigFractionMatrixToRealMatrix(m); + RealMatrix reference = new RealMatrixImpl(testData, false); + assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0); + } + + public void testFractionConverter() { + Fraction[][] fData = { + { new Fraction(1), new Fraction(2), new Fraction(3) }, + { new Fraction(2), new Fraction(5), new Fraction(3) }, + { new Fraction(1), new Fraction(0), new Fraction(8) } + }; + FieldMatrix m = new FieldMatrixImpl(fData, false); + RealMatrix converted = MatrixUtils.fractionMatrixToRealMatrix(m); + RealMatrix reference = new RealMatrixImpl(testData, false); + assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0); + } + public static final Fraction[][] asFraction(double[][] data) { Fraction d[][] = new Fraction[data.length][]; try {