/*
 * 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.math.linear;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.math.linear.DecompositionSolver;
import org.apache.commons.math.linear.InvalidMatrixException;
import org.apache.commons.math.linear.MatrixUtils;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.ArrayRealVector;
import org.apache.commons.math.linear.SingularValueDecompositionImpl;

public class SingularValueSolverTest extends TestCase {

	private double[][] testSquare = { { 24.0 / 25.0, 43.0 / 25.0 }, { 57.0 / 25.0, 24.0 / 25.0 } };

	private static final double normTolerance = 10e-14;

	public SingularValueSolverTest(String name) {
		super(name);
	}

	public static Test suite() {
		TestSuite suite = new TestSuite(SingularValueSolverTest.class);
		suite.setName("SingularValueSolver Tests");
		return suite;
	}

	/** test solve dimension errors */
	public void testSolveDimensionErrors() {
		DecompositionSolver solver = new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare))
				.getSolver();
		RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
		try {
			solver.solve(b);
			fail("an exception should have been thrown");
		} catch (IllegalArgumentException iae) {
			// expected behavior
		} catch (Exception e) {
			fail("wrong exception caught");
		}
		try {
			solver.solve(b.getColumn(0));
			fail("an exception should have been thrown");
		} catch (IllegalArgumentException iae) {
			// expected behavior
		} catch (Exception e) {
			fail("wrong exception caught");
		}
		try {
			solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
			fail("an exception should have been thrown");
		} catch (IllegalArgumentException iae) {
			// expected behavior
		} catch (Exception e) {
			fail("wrong exception caught");
		}
	}

	/** test solve singularity errors */
	public void testSolveSingularityErrors() {
		RealMatrix m = MatrixUtils.createRealMatrix(new double[][] { { 1.0, 0.0 }, { 0.0, 0.0 } });
		DecompositionSolver solver = new SingularValueDecompositionImpl(m).getSolver();
		RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
		try {
			solver.solve(b);
			fail("an exception should have been thrown");
		} catch (InvalidMatrixException ime) {
			// expected behavior
		} catch (Exception e) {
			fail("wrong exception caught");
		}
		try {
			solver.solve(b.getColumn(0));
			fail("an exception should have been thrown");
		} catch (InvalidMatrixException ime) {
			// expected behavior
		} catch (Exception e) {
			fail("wrong exception caught");
		}
		try {
			solver.solve(b.getColumnVector(0));
			fail("an exception should have been thrown");
		} catch (InvalidMatrixException ime) {
			// expected behavior
		} catch (Exception e) {
			fail("wrong exception caught");
		}
		try {
			solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
			fail("an exception should have been thrown");
		} catch (InvalidMatrixException ime) {
			// expected behavior
		} catch (Exception e) {
			fail("wrong exception caught");
		}
	}

	/** test solve */
	public void testSolve() {
		DecompositionSolver solver = new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare))
				.getSolver();
		RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { { 1, 2, 3 }, { 0, -5, 1 } });
		RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] { { -8.0 / 25.0, -263.0 / 75.0, -29.0 / 75.0 },
				{ 19.0 / 25.0, 78.0 / 25.0, 49.0 / 25.0 } });

		// using RealMatrix
		assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), normTolerance);

		// using double[]
		for (int i = 0; i < b.getColumnDimension(); ++i) {
			assertEquals(0,
					new ArrayRealVector(solver.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
					1.0e-13);
		}

		// using Array2DRowRealMatrix
		for (int i = 0; i < b.getColumnDimension(); ++i) {
			assertEquals(0, solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13);
		}

		// using RealMatrix with an alternate implementation
		for (int i = 0; i < b.getColumnDimension(); ++i) {
			ArrayRealVectorTest.RealVectorTestImpl v = new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
			assertEquals(0, solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13);
		}

	}

	/** test condition number */
	public void testConditionNumber() {
		SingularValueDecompositionImpl svd = new SingularValueDecompositionImpl(
				MatrixUtils.createRealMatrix(testSquare));
		assertEquals(3.0, svd.getConditionNumber(), 1.0e-15);
	}

}
