Click here to Skip to main content
15,885,874 members
Articles / Programming Languages / Java

la4j - Linear Algebra for Java

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 Nov 2011CPOL2 min read 30.4K   365   9  
Elegant and pure Java matrix library
/*
 * Copyright 2011, Vladimir Kostyukov
 * 
 * This file is part of la4j project (http://la4j.googlecode.com)
 * 
 * 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 la4j.matrix;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Arrays;

import junit.framework.TestCase;
import la4j.err.MatrixException;
import la4j.factory.Factory;
import la4j.io.MMInputStream;
import la4j.io.MMOutputStream;
import la4j.vector.Vector;

public abstract class AbstractMatrixTest extends TestCase {
	
	public abstract Factory factory();
	
	public void testFactory() {
		
		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		Matrix b = factory().createMatrix(array);

		assertEquals(b, a);
		assertEquals(a.hashCode(), b.hashCode());
	}
	
	public void testAccess() {
		
		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		
		a.set(0, 1, a.get(1, 1) * 2);
		assertEquals(10.0, a.get(0, 1));
	}
	
	public void testNonzero() {
		
		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		
		assertEquals(3, a.nonzero());
	}
	
	public void testToArray() {
		
		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		
		double[][] toArray = a.toArray();
		double[][] toArrayCopy = a.toArrayCopy();

		for (int i = 0; i < a.rows(); i++) {
			assertTrue(Arrays.equals(array[i], toArray[i]));
			assertTrue(Arrays.equals(array[i], toArrayCopy[i]));
		}
	}
	
	public void testResize() {

		double arrayA[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {1.0, 0.0, 0.0, 0.0}, {0.0, 5.0, 0.0, 0.0}, {0.0, 0.0, 9.0, 0.0}, {0.0, 0.0, 0.0, 0.0} };
		double arrayC[][] = new double[][] { {1.0, 0.0}, {0.0, 5.0} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		Matrix c = factory().createMatrix(arrayC);

		a.resize(a.rows() + 1, a.columns() + 1);
		assertEquals(b, a);
		
		a.resize(a.rows() - 2, a.columns() - 2);
		assertEquals(c, a);
	}
	
	public void testSwap() {
		
		double arrayA[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {0.0, 0.0, 9.0}, {0.0, 5.0, 0.0}, {1.0, 0.0, 0.0} };
		double arrayC[][] = new double[][] { {0.0, 0.0, 9.0}, {5.0, 0.0, 0.0}, {0.0, 1.0, 0.0} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		Matrix c = factory().createMatrix(arrayC);
		
		a.swapRows(0, 2);
		assertEquals(b, a);
		
		a.swapColumns(0, 1);
		assertEquals(c, a);
	}
	
	public void testTranspose() {
		
		double arrayA[][] = new double[][] { {0.0, 1.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 9.0, 0.0} };
		double arrayB[][] = new double[][] { {0.0, 0.0, 0.0}, {1.0, 5.0, 9.0}, {0.0, 0.0, 0.0} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		
		Matrix c = a.transpose();
		assertEquals(b, c);
		
		Matrix d = c.transpose();
		assertEquals(a, d);
	}
	
	public void testAdd() throws MatrixException {

		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };

		Matrix a = factory().createMatrix(array);
		Matrix b = factory().createMatrix(array);

		Matrix c = a.add(10.0);
		assertEquals(factory().createMatrix(new double[][] { {11.0, 10.0, 10.0}, {10.0, 15.0, 10.0}, {10.0, 10.0, 19.0} }), c);
		
		
		Matrix d = a.add(b);
		assertEquals(factory().createMatrix(new double[][] { {2.0, 0.0, 0.0}, {0.0, 10.0, 0.0}, {0.0, 0.0, 18.0} }), d);
	}
	
	public void testSubtract() throws MatrixException {

		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		Matrix b = factory().createMatrix(array);
		
		Matrix c = a.subtract(10.0);
		assertEquals(factory().createMatrix(new double[][] { {-9.0, -10.0, -10.0}, {-10.0, -5.0, -10.0}, {-10.0, -10.0, -1.0} }), c);
		
		Matrix d = a.subtract(b);
		assertEquals(factory().createMatrix(new double[][] { {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0} }), d);
	}
	
	public void testMultiply() throws MatrixException {
	
		double arrayA[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {10.0, 0.0, 0.0}, {0.0, 50.0, 0.0}, {0.0, 0.0, 90.0} };
		double arrayC[][] = new double[][] { {10.0, 0.0, 0.0}, {0.0, 250.0, 0.0}, {0.0, 0.0, 810.0} };
		double arrayD[] = new double[] {1.0, 0.0, 0.0 };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		Matrix c = factory().createMatrix(arrayC);
		
		Matrix e = a.multiply(10.0);
		assertEquals(b, e);
		
		Matrix f = a.multiply(b);
		assertEquals(c, f);
		
		Vector g = a.multiply(a.getRow(0));
		assertTrue(Arrays.equals(arrayD, g.toArray()));
	}
	
	public void testDiv() throws MatrixException {
		
		double arrayA[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {0.1, 0.0, 0.0}, {0.0, 0.5, 0.0}, {0.0, 0.0, 0.9} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		
		Matrix c = a.div(10.0);
		
		assertEquals(b, c);
	}
	
	public void testTrace() {
		
		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		
		assertTrue(Math.abs(a.determinant() - 45.0) < Matrix.EPS);
	}
	
	public void testDeterminant() {
		
		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		
		assertTrue(Math.abs(a.determinant() - 45.0) < Matrix.EPS);
	}
	
	public void testRowAccess() {

		double arrayA[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {1.0, 0.0, 0.0} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		
		a.setRow(2, a.getRow(0));
		
		assertEquals(b, a);
	}
	
	public void testColumnAccess() {
		
		double arrayA[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {1.0, 0.0, 1.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 0.0} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		
		a.setColumn(2, a.getColumn(0));
		
		assertEquals(b, a);
	}
	
	public void testSymmetric() {
		
		double arrayA[][] = new double[][] { {1.0, 0.0, 0.0}, {4.0, 0.0, 6.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {1.0, 0.0, 3.0}, {0.0, 5.0, 0.0}, {3.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		
		assertFalse(a.isSymmetric());
		assertTrue(b.isSymmetric());
	}
	
	public void testTriangle() {
		
		double arrayA[][] = new double[][] { {1.0, 0.0, 0.0}, {4.0, 0.0, 6.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {1.0, 0.0, 3.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		
		assertFalse(a.isTriangle());
		assertTrue(b.isTriangle());
		
		// TODO: add triangularize
	}
	
	public void testClone() {
		
		double array[][] = new double[][] { {1.0, 0.0, 0.0}, {0.0, 5.0, 0.0}, {0.0, 0.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		
		Matrix b = a.clone();
		assertTrue(a.equals(b));
		
		b.set(0, 0, b.get(0, 0) * 10);
		assertFalse(a.equals(b));
	}
	
	public void testCopy() {
		
		double array[][] = new double[][] { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0} };
		
		Matrix a = factory().createMatrix(array);
		Matrix b = a.copy();
		
		assertEquals(a, b);
	}
	
	public void testBlank() {
		
		double arrayA[][] = new double[][] { {0.0, 0.0, 3.0}, {0.0, 0.0, 6.0}, {0.0, 0.0, 9.0} };
		double arrayB[][] = new double[][] { {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 0.0} };
		
		Matrix a = factory().createMatrix(arrayA);
		Matrix b = factory().createMatrix(arrayB);
		
		Matrix c = a.blank();
		
		assertEquals(b, c);
	}
	
	public void testSerialization() throws IOException, ClassNotFoundException {
		
		double array[][] = new double[][] { {0.0, 0.0, 3.0}, {0.0, 0.0, 6.0}, {0.0, 0.0, 9.0} };

		Matrix a = factory().createMatrix(array);
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutput out = new ObjectOutputStream(bos);
		out.writeObject(a);
		out.close();

		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		ObjectInput in = new ObjectInputStream(bis);
		Matrix b = (Matrix) in.readObject();
		in.close();
		
		assertEquals(a, b);
	}
	
	public void testIO() throws IOException, ClassNotFoundException {
		
		double array[][] = new double[][] { {0.0, 0.0, 3.0}, {0.0, 0.0, 6.0}, {0.0, 0.0, 9.0} };
		Matrix a = factory().createMatrix(array);
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutput mmos = new MMOutputStream(bos);
		mmos.writeObject(a);
		mmos.close();
		
		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		ObjectInput mmis = new MMInputStream(bis);
		Matrix b = (Matrix) mmis.readObject();
		
		assertEquals(a, b);	
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Intel
Russian Federation Russian Federation
I've received a Master's Degree in Computer Science at the IT Faculty of Altai State Technical University, Russia.

Currently, I am Software Engineer at Intel Corporation.

Comments and Discussions