added conversion utility functions for
FieldMatrix<Fraction> to RealMatrix FieldMatrix<BigFraction> to RealMatrix git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@780308 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
525418ab1c
commit
aea0ab1242
|
@ -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<Fraction> m) {
|
||||
final FractionMatrixConverter converter = new FractionMatrixConverter();
|
||||
m.walkInOptimizedOrder(converter);
|
||||
return converter.getConvertedMatrix();
|
||||
}
|
||||
|
||||
/** Converter for {@link FieldMatrix}/{@link Fraction}. */
|
||||
private static class FractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor<Fraction> {
|
||||
|
||||
/** 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<BigFraction> m) {
|
||||
final BigFractionMatrixConverter converter = new BigFractionMatrixConverter();
|
||||
m.walkInOptimizedOrder(converter);
|
||||
return converter.getConvertedMatrix();
|
||||
}
|
||||
|
||||
/** Converter for {@link FieldMatrix}/{@link BigFraction}. */
|
||||
private static class BigFractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor<BigFraction> {
|
||||
|
||||
/** 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<BigFraction> m = new FieldMatrixImpl<BigFraction>(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<Fraction> m = new FieldMatrixImpl<Fraction>(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 {
|
||||
|
|
Loading…
Reference in New Issue