# DMelt:Numeric/4 Linear Equations

# Linear Equations

Linear_equation is an algebraic equation in which each term is either a constant or the product of a constant and (the first power of) a single variable.
A linear system is any system than can be expressed in the format **Ax = b**.
where **A** is m by n,** x** is n by o, and **b** is m by o. Most of the time o=1. There are numerous ways to solve such system using DMelt.

- jhplot/math/LinearAlgebra - A Native jHPlot Linear Algebra package
- Jama/package-summary - A Java Matrix Package
- ParallelColt - high-performance calculations on multiple cores
- EJML - Efficient Java Matrix Library

# Solving linear systems

First we consider the Apache math library.
Consider a linear systems of equations of the form **AX=B**. For example, consider

2x + 3y - 2z = 1 x + 7y + 6x = -2 4x - 3y - 5z = 1

We will solve this using DecompositionSolver of the Apache Common Math package:

from org.apache.commons.math3.linear import * # get the coefficient matrix A using LU decomposition coeff= Array2DRowRealMatrix([[2,3,-2],[-1,7,6],[4,-3,-5]]) solver =LUDecomposition(coeff).getSolver() # use solve(RealVector) to solve the system constants = ArrayRealVector([1, -2, 1 ]) solution = solver.solve(constants); print "Solution: x=",solution.getEntry(0), "y=",solution.getEntry(1),"z=",solution.getEntry(2)

The execution of this code prints:

Solution: x= -0.369863013699 y= 0.178082191781 z= -0.602739726027

Read more for different types of decomposition here.

## Using EJML library

A generic way to solve linear equations using EJML is shown below:

A = DenseMatrix64F(m,n); x = DenseMatrix64F(n,1); b = DenseMatrix64F(m,1); .... code to fill matrices .... if !CommonOps.solve(A,b,x) : print "Singular matrix";

Linear solvers will in general fail (with some notable exceptions) to produce a meaning full solution if the 'A' matrix is singular. When 'A' is singular then there is an infinite number of solutions. Below is a Jython code which solves a system of linear equations:

No access to this part. DataMelt members can view this part after login to member area. |

## Solving equation using multithreaded approach

You can solve linear equations using multiple cores. This addresses the needs of high performance computing (HPC) community. In this approach, the calculations are significantly faster (roughly proportional to the number of available cores of the computer).

- Solving a system of linear equations using QR factorization.
- Solving a system of linear equations using LU factorization.
- Solving a system of linear equations using Cholesky factorization.

Here is the example for solving a system of linear equations using LU factorization using 4 threads (cores). If you do not specify the numbers of cores, it the program will use the maximal number of available cores seen by JVM.

Example for solving a system of linear equations using LU factorization using multiple cores. We will use Dplasma class.

This is example for the Cholesky method on multiple cores:

No access to this part. DataMelt members can view this part after login to member area. |