1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*
* Copyright (c) 2009-2018, Peter Abeles. All Rights Reserved.
*
* This file is part of Efficient Java Matrix Library (EJML).
*
* Licensed 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.ejml.sparse.csc.linsol;
import org.ejml.EjmlUnitTests;
import org.ejml.UtilEjml;
import org.ejml.data.DMatrixRMaj;
import org.ejml.data.DMatrixSparseCSC;
import org.ejml.dense.row.RandomMatrices_DDRM;
import org.ejml.interfaces.linsol.LinearSolverSparse;
import org.ejml.sparse.FillReducing;
import org.ejml.sparse.csc.CommonOps_DSCC;
import org.ejml.sparse.csc.RandomMatrices_DSCC;
import org.junit.Test;
import java.util.Random;
import static org.junit.Assert.*;
/**
* Generic tests for linear solvers
*
* @author Peter Abeles
*/
// TODO Add a test that makes sure identify permutation produces identical results to no permutation
public abstract class GenericLinearSolverSparseTests_DSCC {
protected Random rand = new Random(234);
protected FillReducing permutationTests[] = new FillReducing[]
{FillReducing.NONE, FillReducing.IDENTITY};
// used to adjust tolerance threshold
protected double equalityTolerance = UtilEjml.TEST_F64;
protected boolean canHandleTall = true;
protected boolean canHandleWide = true;
protected boolean canDecomposeZeros = true;
protected boolean canLockStructure = true;
public abstract LinearSolverSparse<DMatrixSparseCSC,DMatrixRMaj> createSolver(FillReducing permutation);
/**
* Create a random matrix. The exact shape is determined by the implementation but all allowable shapes for this
* size should be randomized
*/
public abstract DMatrixSparseCSC createA(int size);
public DMatrixRMaj create( int rows , int cols ) {
return RandomMatrices_DDRM.rectangle(rows,cols,rand);
}
public DMatrixSparseCSC createSparse( int rows , int cols ) {
return RandomMatrices_DSCC.rectangle(rows,cols,rows*cols,rand);
}
@Test
public void randomSolveable() {
for( FillReducing perm : permutationTests ) {
// System.out.println("perm = "+perm);
LinearSolverSparse<DMatrixSparseCSC, DMatrixRMaj> solver = createSolver(perm);
for (int N : new int[]{1, 2, 5, 10, 20}) {
for (int mc = 0; mc < 30; mc++) {
// System.out.println("-=-=-=-=-=-=-=-= "+N+" mc "+mc);
DMatrixSparseCSC A = createA(N);
DMatrixSparseCSC A_cpy = A.copy();
DMatrixRMaj X = create(A.numCols, 3);
DMatrixRMaj foundX = create(A.numCols, 3);
DMatrixRMaj B = new DMatrixRMaj(A.numRows, 3);
// compute the solution
CommonOps_DSCC.mult(A, X, B);
DMatrixRMaj B_cpy = B.copy();
assertTrue(solver.setA(A));
solver.solve(B, foundX);
EjmlUnitTests.assertRelativeEquals(X, foundX, equalityTolerance);
if( !solver.modifiesA() ) {
EjmlUnitTests.assertEquals(A, A_cpy, equalityTolerance);
}
if( !solver.modifiesB() ) {
EjmlUnitTests.assertEquals(B, B_cpy, equalityTolerance);
}
}
}
}
}
@Test
public void randomSolveable_Sparse() {
for( FillReducing perm : permutationTests ) {
// System.out.println("perm = "+perm);
LinearSolverSparse<DMatrixSparseCSC, DMatrixRMaj> solver = createSolver(perm);
for (int N : new int[]{1, 2, 5, 10, 20}) {
for (int mc = 0; mc < 20; mc++) {
// System.out.println("-=-=-=-=-=-=-=-= "+N+" mc "+mc);
DMatrixSparseCSC A = createA(N);
DMatrixSparseCSC A_cpy = A.copy();
DMatrixSparseCSC X = createSparse(A.numCols, 3);
DMatrixSparseCSC foundX = createSparse(A.numCols, 3);
DMatrixSparseCSC B = new DMatrixSparseCSC(A.numRows, 3,1);
// compute the solution
CommonOps_DSCC.mult(A, X, B);
DMatrixSparseCSC B_cpy = B.copy();
// System.out.println("--- A");
// A.print();
assertTrue(solver.setA(A));
solver.solveSparse(B, foundX);
assertTrue(CommonOps_DSCC.checkStructure(foundX));
EjmlUnitTests.assertRelativeEquals(X, foundX, equalityTolerance);
// should never be modified
EjmlUnitTests.assertEquals(A, A_cpy, equalityTolerance);
EjmlUnitTests.assertEquals(B, B_cpy, equalityTolerance);
}
}
}
}
/**
* Give it a matrix that's all zeros and see if it fails.
*/
@Test
public void handleAllZeros() {
DMatrixSparseCSC A = new DMatrixSparseCSC(10,10,0);
LinearSolverSparse<DMatrixSparseCSC, DMatrixRMaj> solver = createSolver(FillReducing.NONE);
assertTrue(canDecomposeZeros == solver.setA(A));
}
/**
* Provides wide or tall matrices and see if it throws an exception
*/
@Test
public void checkFailByShape_Tall() {
if( canHandleTall ) {
return;
}
try {
LinearSolverSparse<DMatrixSparseCSC, DMatrixRMaj> solver = createSolver(FillReducing.NONE);
DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(10,5,50,rand);
solver.setA(A);
fail("Should have thrown an exception");
} catch( IllegalArgumentException ignore ){}
}
@Test
public void checkFailByShape_Wide() {
if( canHandleWide ) {
return;
}
try {
LinearSolverSparse<DMatrixSparseCSC, DMatrixRMaj> solver = createSolver(FillReducing.NONE);
DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(5,10,50,rand);
solver.setA(A);
fail("Should have thrown an exception");
} catch( IllegalArgumentException ignore ){}
}
@Test
public void quality() {
DMatrixSparseCSC A_good = CommonOps_DSCC.diag(4,3,2,1);
DMatrixSparseCSC A_bad = CommonOps_DSCC.diag(4, 3, 2, 0.1);
LinearSolverSparse<DMatrixSparseCSC, DMatrixRMaj> solver = createSolver(FillReducing.NONE);
assertTrue(solver.setA(A_good));
double q_good;
try {
q_good = (double)solver.quality();
} catch( IllegalArgumentException e ) {
// quality is not supported
return;
}
assertTrue(solver.setA(A_bad));
double q_bad = (double)solver.quality();
assertTrue(q_bad < q_good);
}
@Test
public void ifCanNotLockThrowException() {
LinearSolverSparse<DMatrixSparseCSC,DMatrixRMaj> d = createSolver(FillReducing.NONE);
if( canLockStructure ) {
d.setStructureLocked(true);
} else {
try {
d.setStructureLocked(true);
fail("RuntimeException should have been thrown");
} catch (RuntimeException ignore) {
}
}
}
@Test
public void lockingDoesNotChangeSolution() {
if( !canLockStructure )
return;
LinearSolverSparse<DMatrixSparseCSC,DMatrixRMaj> d = createSolver(FillReducing.NONE);
DMatrixSparseCSC A = createA(10);
DMatrixRMaj B = RandomMatrices_DDRM.rectangle(A.numRows,5,rand);
DMatrixRMaj X0 = new DMatrixRMaj(A.numCols,5);
DMatrixRMaj X1 = new DMatrixRMaj(A.numCols,5);
assertTrue(d.setA((DMatrixSparseCSC)A.copy()));
d.solve(B.copy(),X0);
assertFalse(d.isStructureLocked());
d.setStructureLocked(true);
assertTrue(d.isStructureLocked());
assertTrue(d.setA(A));
d.solve(B,X1);
EjmlUnitTests.assertEquals(X0,X1,UtilEjml.TEST_F64);
}
}
|