Click here to Skip to main content
13,048,406 members (148,388 online)
Click here to Skip to main content

Tagged as


7 bookmarked
Posted 14 Nov 2011

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 (
 * 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
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package la4j.linear;

import la4j.err.LinearSystemException;
import la4j.err.MatrixException;
import la4j.err.VectorException;
import la4j.factory.DenseFactory;
import la4j.factory.Factory;
import la4j.matrix.Matrix;
import la4j.vector.Vector;

public class LinearSystem {
	public static final double EPS = 10e-7;

	private int equations;
	private int variables;
	private Matrix a;
	private Vector b;
	private LinearSystemSolver solver;
	private Factory factory;
	public LinearSystem(Matrix a, Vector b) {
		this.a = a;
		this.b = b;
		this.equations = a.rows();
		this.variables = a.columns();
		this.solver = new GaussianSolver();
		this.factory = new DenseFactory();
	public int equations() {
		return equations;

	public int variables() {
		return variables;

	public Matrix coefficientsMatrix() {
		return a;
	public Vector rightHandVector() {
		return b;
	public Vector solve() throws LinearSystemException {
		return solve(solver, factory);
	public Vector solve(Factory factory) throws LinearSystemException {
		return solve(solver, factory);
	public Vector solve(LinearSystemSolver solver) throws LinearSystemException {
		return solve(solver, factory);
	public Vector solve(LinearSystemSolver solver, Factory factory) throws LinearSystemException {
		return solver.solve(this, factory);
	public boolean isSolution(Vector vector) {
		if (vector == null) return false;
		if (vector.length() != variables) return false;
		try {
			Vector r = innacary(vector);
			boolean result = true;
			for (int i = 0; i < r.length(); i++) {
				result = result && (Math.abs(r.get(i)) < EPS);
			return result;
		} catch (LinearSystemException ignored) { 
			return false;

	private Vector innacary(Vector vector) throws LinearSystemException {
		try {
			return a.multiply(vector).subtract(b);
		} catch (MatrixException ex) {
			throw new LinearSystemException(ex.getMessage());
		} catch (VectorException ex) {
			throw new LinearSystemException(ex.getMessage());

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.


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


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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 14 Nov 2011
Article Copyright 2011 by Vladimir Kostyukov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid