📄 matrixtestabstract.java
字号:
/* * Copyright (C) 2003-2006 Bjørn-Ove Heimsund * * This file is part of MTJ. * * This library 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 2.1 of the License, or (at your * option) any later version. * * This library 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 Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package no.uib.cipr.matrix;import no.uib.cipr.matrix.Matrices;import no.uib.cipr.matrix.Matrix;import no.uib.cipr.matrix.MatrixEntry;import no.uib.cipr.matrix.MatrixNotSPDException;import no.uib.cipr.matrix.MatrixSingularException;import no.uib.cipr.matrix.Vector;import junit.framework.TestCase;/** * Tests a matrix */public abstract class MatrixTestAbstract extends TestCase { /** * Matrix to test */ protected Matrix A; /** * Jagged array version of A */ protected double[][] Ad; /** * Matrix of the same size as A, dense and non-dense */ protected Matrix Bdense, B; /** * Contents of B */ protected double[][] Bd; /** * Non-dense vectors with size equal the number of rows in A */ protected Vector xR, yR; /** * Non-dense vectors with size equal the number of columns in A */ protected Vector xC, yC; /** * Dense vectors with size equal the number of rows in A */ protected Vector xDenseR, yDenseR; /** * Dense vectors with size equal the number of columns in A */ protected Vector xDenseC, yDenseC; /** * Contents of the vectors */ protected double[] xdR, ydR, xdC, ydC; /** * Tolerance for floating-point comparisons */ protected double tol = 1e-5; /** * Maximum matrix size, to avoid too slow tests */ protected int max = 100; /** * Constructor for MatrixTestAbstract */ public MatrixTestAbstract(String arg0) { super(arg0); } @Override protected void setUp() throws Exception { createPrimary(); createAuxillerary(); } protected abstract void createPrimary() throws Exception; @Override protected void tearDown() throws Exception { A = B = Bdense = null; Ad = Bd = null; xC = xDenseC = xDenseR = xR = yC = yDenseC = yDenseR = yR = null; xdC = xdR = ydC = ydR = null; } /** * Called after setUp() to create additional datastructures */ protected void createAuxillerary() { Bdense = Matrices.random(A.numRows(), A.numColumns()); B = Matrices.synchronizedMatrix(Bdense.copy()); Bd = Matrices.getArray(B); xDenseC = Matrices.random(A.numColumns()); yDenseC = Matrices.random(A.numColumns()); xDenseR = Matrices.random(A.numRows()); yDenseR = Matrices.random(A.numRows()); xC = Matrices.synchronizedVector(xDenseC); yC = Matrices.synchronizedVector(yDenseC); xR = Matrices.synchronizedVector(xDenseR); yR = Matrices.synchronizedVector(yDenseR); xdC = Matrices.getArray(xC); ydC = Matrices.getArray(yC); xdR = Matrices.getArray(xR); ydR = Matrices.getArray(yR); } public void testMatrixRank2Dense() { if (A.isSquare()) { int n = Utilities.getInt(1, max); Matrix B = Matrices.random(A.numRows(), n), C = Matrices.random(A .numRows(), n); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); A = A.rank2(alpha, B, C); rank2(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } } public void testMatrixRank2() { if (A.isSquare()) { int n = Utilities.getInt(1, max); Matrix B = Matrices.synchronizedMatrix(Matrices.random(A.numRows(), n)), C = Matrices.synchronizedMatrix(Matrices.random(A .numRows(), n)); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); A = A.rank2(alpha, B, C); rank2(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } } public void testMatrixTransRank2Dense() { if (A.isSquare()) { int n = Utilities.getInt(1, max); Matrix B = Matrices.random(n, A.numColumns()), C = Matrices.random( n, A.numColumns()); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); A = A.transRank2(alpha, B, C); transRank2(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } } public void testMatrixTransRank2() { if (A.isSquare()) { int n = Utilities.getInt(1, max); Matrix B = Matrices.synchronizedMatrix(Matrices.random(n, A .numColumns())), C = Matrices.synchronizedMatrix(Matrices .random(n, A.numColumns())); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); A = A.transRank2(alpha, B, C); transRank2(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } } public void testMatrixRank1Dense() { if (A.isSquare()) { Matrix C = Matrices.random(A.numRows(), A.numColumns()); double[][] Cd = Matrices.getArray(C); double alpha = Math.random(); A = A.rank1(alpha, C); rank1(Ad, alpha, Cd); assertEquals(Ad, A); assertEquals(Cd, C); } } public void testMatrixRank1() { if (A.isSquare()) { Matrix C = Matrices.synchronizedMatrix(Matrices.random(A.numRows(), A.numColumns())); double[][] Cd = Matrices.getArray(C); double alpha = Math.random(); A = A.rank1(alpha, C); rank1(Ad, alpha, Cd); assertEquals(Ad, A); assertEquals(Cd, C); } } public void testMatrixTransRank1Dense() { if (A.isSquare()) { Matrix C = Matrices.random(A.numRows(), A.numColumns()); double[][] Cd = Matrices.getArray(C); double alpha = Math.random(); A = A.transRank1(alpha, C); transRank1(Ad, alpha, Cd); assertEquals(Ad, A); assertEquals(Cd, C); } } public void testMatrixTransRank1() { if (A.isSquare()) { Matrix C = Matrices.synchronizedMatrix(Matrices.random(A.numRows(), A.numColumns())); double[][] Cd = Matrices.getArray(C); double alpha = Math.random(); A = A.transRank1(alpha, C); transRank1(Ad, alpha, Cd); assertEquals(Ad, A); assertEquals(Cd, C); } } public void testMatrixMultDense() { int m = A.numRows(), k = A.numColumns(), n = Utilities.getInt(1, max); Matrix B = Matrices.random(k, n), C = Matrices.random(m, n); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.mult(alpha, B, C); Cd = mult(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixMult() { int m = A.numRows(), k = A.numColumns(), n = Utilities.getInt(1, max); Matrix B = Matrices.synchronizedMatrix(Matrices.random(k, n)), C = Matrices .synchronizedMatrix(Matrices.random(m, n)); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.mult(alpha, B, C); Cd = mult(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixTransAmultDense() { int m = A.numColumns(), k = A.numRows(), n = Utilities.getInt(1, max); Matrix B = Matrices.random(k, n), C = Matrices.random(m, n); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.transAmult(alpha, B, C); Cd = transAmult(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixTransAmult() { int m = A.numColumns(), k = A.numRows(), n = Utilities.getInt(1, max); Matrix B = Matrices.synchronizedMatrix(Matrices.random(k, n)), C = Matrices .synchronizedMatrix(Matrices.random(m, n)); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.transAmult(alpha, B, C); Cd = transAmult(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixTransABmultDense() { int m = A.numColumns(), k = A.numRows(), n = Utilities.getInt(1, max); Matrix B = Matrices.random(n, k), C = Matrices.random(m, n); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.transABmult(alpha, B, C); Cd = transABmult(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixTransABmult() { int m = A.numColumns(), k = A.numRows(), n = Utilities.getInt(1, max); Matrix B = Matrices.synchronizedMatrix(Matrices.random(n, k)), C = Matrices .synchronizedMatrix(Matrices.random(m, n)); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.transABmult(alpha, B, C); Cd = transABmult(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixTransBmultDense() { int m = A.numRows(), k = A.numColumns(), n = Utilities.getInt(1, max); Matrix B = Matrices.random(n, k), C = Matrices.random(m, n); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.transBmult(alpha, B, C); Cd = transBmult(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixTransBmult() { int m = A.numRows(), k = A.numColumns(), n = Utilities.getInt(1, max); Matrix B = Matrices.synchronizedMatrix(Matrices.random(n, k)), C = Matrices .synchronizedMatrix(Matrices.random(m, n)); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.transBmult(alpha, B, C); Cd = transBmult(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixMultAddDense() { int m = A.numRows(), k = A.numColumns(), n = Utilities.getInt(1, max); Matrix B = Matrices.random(k, n), C = Matrices.random(m, n); double[][] Bd = Matrices.getArray(B), Cd = Matrices.getArray(C); double alpha = Math.random(); C = A.multAdd(alpha, B, C); Cd = multAdd(Ad, alpha, Bd, Cd); assertEquals(Ad, A); assertEquals(Bd, B); assertEquals(Cd, C); } public void testMatrixMultAdd() { int m = A.numRows(), k = A.numColumns(), n = Utilities.getInt(1, max); Matrix B = Matrices.synchronizedMatrix(Matrices.random(k, n)), C = Matrices .synchronizedMatrix(Matrices.random(m, n));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -