Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Go to top

la4j - Linear Algebra for Java

, 14 Nov 2011
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.vector;

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.VectorException;
import la4j.factory.Factory;
import la4j.io.MMInputStream;
import la4j.io.MMOutputStream;

public abstract class AbstractVectorTest extends TestCase {

	public abstract Factory factory();  

	public void testFactory() {

		double array[] = new double[] { 0.0, 0.0, 3.0, 0.0, 0.0 };
		
		Vector a = factory().createVector(array);
		Vector b = factory().createVector(array);
		
		assertEquals(a, b);
		assertEquals(a.hashCode(), b.hashCode());
	}
	
	public void testAccess() {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 3.0, 0.0, 0.0 });

		a.set(0, a.get(2) + 10);
		assertEquals(13.0, a.get(0));
	}
	
	public void testNonzero() {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 0.0, 0.0, 1.0 });
		
		assertEquals(1, a.nonzero());
	}
	
	public void testToArray() {
		
		double array[] = new double[] { 0.0, 0.0, 0.0, 0.0, 1.0 };

		Vector a = factory().createVector(array);
		
		assertTrue(Arrays.equals(array, a.toArray()));
		assertTrue(Arrays.equals(array, a.toArrayCopy()));
	}
	
	public void testResize() {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 1.0 });
		Vector b = factory().createVector(new double[] { 0.0, 0.0, 1.0, 0.0, 0.0 });
		Vector c = factory().createVector(new double[] { 0.0, 0.0 });
		
		a.resize(5);
		assertEquals(b, a);
		
		a.resize(2);
		assertEquals(c, a);
	}
	
	public void testSwap() {
		
		Vector a = factory().createVector(new double[] { 1.0, 0.0, 0.0, 0.0, 3.0 });
		Vector b = factory().createVector(new double[] { 3.0, 0.0, 0.0, 0.0, 1.0 });
		
		a.swap(0, 4);
		assertEquals(b, a);
	}
	
	public void testNorm() {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 0.0, 4.0 });
		Vector b = factory().createVector(new double[] { 0.0, 0.0, 0.0, 1.0 });
		
		double norm = a.norm();
		assertEquals(4.0, norm);
		
		Vector c = a.asNormalized();
		assertEquals(b, c);
	}
	
	public void testAdd() throws VectorException {
	
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 3.0 });
		Vector b = factory().createVector(new double[] { 0.0, 5.0, 0.0 });
		
		Vector c = a.add(7.0);
		assertEquals(factory().createVector(new double[] { 7.0, 7.0, 10.0 }), c);
		
		Vector d = a.add(b);
		assertEquals(factory().createVector(new double[] { 0.0, 5.0, 3.0 }), d);
		
	}
	
	public void testSubtract() throws VectorException {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 3.0 });
		Vector b = factory().createVector(new double[] { 4.0, 0.0, 0.0 });
		
		Vector c = a.subtract(7.0);
		assertEquals(factory().createVector(new double[] { -7.0, -7.0, -4.0 }), c);
		
		Vector d = a.subtract(b);
		assertEquals(factory().createVector(new double[] { -4.0, 0.0, 3.0 }), d);
		
	}
	
	public void testMultiply() throws VectorException {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 1.0 });
		Vector b = factory().createVector(new double[] { 0.0, 5.0, 0.0 });
		
		Vector c = a.multiply(10.0);
		assertEquals(factory().createVector(new double[] { 0.0, 0.0, 10.0 }), c);
		
		Vector d = a.multiply(b);
		assertEquals(factory().createVector(new double[] { 0.0, 0.0, 0.0 }), d);
	}
	
	public void testDiv() throws VectorException {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 3.0 });
		
		Vector b = a.div(10);
		assertEquals(factory().createVector(new double[] { 0.0, 0.0, 0.3 }), b);
	}
	
	public void testClone() throws VectorException {
		
		Vector a = factory().createVector(new double[] { 0.0, 2.0, 0.0 });
		Vector b = (Vector) a.clone();
		
		assertTrue(a.equals(b));
		
		a.set(1, -1.0);
		
		assertTrue(!a.equals(b));
	}
	
	public void testCopy() throws VectorException {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 0.0, 0.0, 1.0 });

		Vector b = a.copy();
		
		assertEquals(a, b);
	}
	
	public void testBlank() {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 0.0, 0.0, 1.0 });
		Vector b = factory().createVector(new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 });
		
		Vector c = a.blank();

		assertEquals(b, c);
	}
	
	public void testSerialization() throws VectorException, IOException, ClassNotFoundException {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 0.0, 0.0, 5.0 });
		
		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);
		Vector b = (Vector) in.readObject();
		in.close();
		
		assertEquals(a, b);
	}
	
	public void testIO() throws IOException, ClassNotFoundException {
		
		Vector a = factory().createVector(new double[] { 0.0, 0.0, 0.0, 0.0, 5.0 });

		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);
		Vector b = (Vector) 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)

Share

About the Author

Vladimir Kostyukov
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.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 14 Nov 2011
Article Copyright 2011 by Vladimir Kostyukov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid