|
Java example source code file (MatrixUtilsTest.java)
The MatrixUtilsTest.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.linear; import java.math.BigDecimal; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.fraction.BigFraction; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionConversionException; import org.apache.commons.math3.fraction.FractionField; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link MatrixUtils} class. * */ public final class MatrixUtilsTest { protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} }; protected double[][] testData3x3Singular = { { 1, 4, 7, }, { 2, 5, 8, }, { 3, 6, 9, } }; protected double[][] testData3x4 = { { 12, -51, 4, 1 }, { 6, 167, -68, 2 }, { -4, 24, -41, 3 } }; protected double[][] nullMatrix = null; protected double[] row = {1,2,3}; protected BigDecimal[] bigRow = {new BigDecimal(1),new BigDecimal(2),new BigDecimal(3)}; protected String[] stringRow = {"1", "2", "3"}; protected Fraction[] fractionRow = {new Fraction(1),new Fraction(2),new Fraction(3)}; protected double[][] rowMatrix = {{1,2,3}}; protected BigDecimal[][] bigRowMatrix = {{new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)}}; protected String[][] stringRowMatrix = {{"1", "2", "3"}}; protected Fraction[][] fractionRowMatrix = {{new Fraction(1), new Fraction(2), new Fraction(3)}}; protected double[] col = {0,4,6}; protected BigDecimal[] bigCol = {new BigDecimal(0),new BigDecimal(4),new BigDecimal(6)}; protected String[] stringCol = {"0","4","6"}; protected Fraction[] fractionCol = {new Fraction(0),new Fraction(4),new Fraction(6)}; protected double[] nullDoubleArray = null; protected double[][] colMatrix = {{0},{4},{6}}; protected BigDecimal[][] bigColMatrix = {{new BigDecimal(0)},{new BigDecimal(4)},{new BigDecimal(6)}}; protected String[][] stringColMatrix = {{"0"}, {"4"}, {"6"}}; protected Fraction[][] fractionColMatrix = {{new Fraction(0)},{new Fraction(4)},{new Fraction(6)}}; @Test public void testCreateRealMatrix() { Assert.assertEquals(new BlockRealMatrix(testData), MatrixUtils.createRealMatrix(testData)); try { MatrixUtils.createRealMatrix(new double[][] {{1}, {1,2}}); // ragged Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createRealMatrix(new double[][] {{}, {}}); // no columns Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createRealMatrix(null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testcreateFieldMatrix() { Assert.assertEquals(new Array2DRowFieldMatrix<Fraction>(asFraction(testData)), MatrixUtils.createFieldMatrix(asFraction(testData))); Assert.assertEquals(new Array2DRowFieldMatrix<Fraction>(FractionField.getInstance(), fractionColMatrix), MatrixUtils.createFieldMatrix(fractionColMatrix)); try { MatrixUtils.createFieldMatrix(asFraction(new double[][] {{1}, {1,2}})); // ragged Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createFieldMatrix(asFraction(new double[][] {{}, {}})); // no columns Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createFieldMatrix((Fraction[][])null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCreateRowRealMatrix() { Assert.assertEquals(MatrixUtils.createRowRealMatrix(row), new BlockRealMatrix(rowMatrix)); try { MatrixUtils.createRowRealMatrix(new double[] {}); // empty Assert.fail("Expecting NotStrictlyPositiveException"); } catch (NotStrictlyPositiveException ex) { // expected } try { MatrixUtils.createRowRealMatrix(null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCreateRowFieldMatrix() { Assert.assertEquals(MatrixUtils.createRowFieldMatrix(asFraction(row)), new Array2DRowFieldMatrix<Fraction>(asFraction(rowMatrix))); Assert.assertEquals(MatrixUtils.createRowFieldMatrix(fractionRow), new Array2DRowFieldMatrix<Fraction>(fractionRowMatrix)); try { MatrixUtils.createRowFieldMatrix(new Fraction[] {}); // empty Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createRowFieldMatrix((Fraction[]) null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCreateColumnRealMatrix() { Assert.assertEquals(MatrixUtils.createColumnRealMatrix(col), new BlockRealMatrix(colMatrix)); try { MatrixUtils.createColumnRealMatrix(new double[] {}); // empty Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createColumnRealMatrix(null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCreateColumnFieldMatrix() { Assert.assertEquals(MatrixUtils.createColumnFieldMatrix(asFraction(col)), new Array2DRowFieldMatrix<Fraction>(asFraction(colMatrix))); Assert.assertEquals(MatrixUtils.createColumnFieldMatrix(fractionCol), new Array2DRowFieldMatrix<Fraction>(fractionColMatrix)); try { MatrixUtils.createColumnFieldMatrix(new Fraction[] {}); // empty Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createColumnFieldMatrix((Fraction[]) null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } /** * Verifies that the matrix is an identity matrix */ protected void checkIdentityMatrix(RealMatrix m) { for (int i = 0; i < m.getRowDimension(); i++) { for (int j =0; j < m.getColumnDimension(); j++) { if (i == j) { Assert.assertEquals(m.getEntry(i, j), 1d, 0); } else { Assert.assertEquals(m.getEntry(i, j), 0d, 0); } } } } @Test public void testCreateIdentityMatrix() { checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(3)); checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(2)); checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(1)); try { MatrixUtils.createRealIdentityMatrix(0); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } /** * Verifies that the matrix is an identity matrix */ protected void checkIdentityFieldMatrix(FieldMatrix<Fraction> m) { for (int i = 0; i < m.getRowDimension(); i++) { for (int j =0; j < m.getColumnDimension(); j++) { if (i == j) { Assert.assertEquals(m.getEntry(i, j), Fraction.ONE); } else { Assert.assertEquals(m.getEntry(i, j), Fraction.ZERO); } } } } @Test public void testcreateFieldIdentityMatrix() { checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 3)); checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 2)); checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 1)); try { MatrixUtils.createRealIdentityMatrix(0); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } @Test 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 Array2DRowFieldMatrix Other Java examples (source code examples)Here is a short list of links related to this Java MatrixUtilsTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.