13,048,406 members (148,388 online)

#### Stats

16.7K views
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 (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.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()); } } } ```

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.