**Table of Content**

**Search clouds**

**Licenses**

**Author's resources**

You are a guest. Restricted access. Read more.

The SCaVis contains many high-performance Java packages for linear algebra and matrix operations:

- LinearAlgebra - A Native jHPlot Linear Algebra package
- Jama - A Java Matrix Package
- ParallelColt - high-performance calculations on multiple cores
- EJML - Efficient Java Matrix Library
- La4J - La4J Matrix Library
- ND4J - N-dimensional arrays

For manipulations with vectors, use the following classes with useful static methods:

- ArrayMathUtils - manipulate with 1D arrays
- IntegerArray - to construct integer 1D arrays
- P0I - the standard jhplot 1D integer arrays with many methods
- P0D - the standard jhplot 1D double arrays with many methods

You can also use the Python list as a container to hold and manipulate with 1D data structures. P0I and P0D arrays have been considered in Sect.Data structures. Below we show how to use static methods by mixing Python lists with the static methods of the ArrayMathUtils Java class:

from jhplot.math.ArrayMathUtils import * a=[-1,-2,3,4,5,-6,7,10] # make a Python list print a b=invert(a) # invert it print b.tolist() c=scalarMultiply(10, b) # scalar multiply by 10 print c.tolist() print mean(a) print sumSquares(a) # sums the squares

This code generates the following output:

[-1, -2, 3, 4, 5, -6, 7, 10] [10, 7, -6, 5, 4, 3, -2, -1] [100.0, 70.0, -60.0, 50.0, 40.0, 30.0, -20.0, -10.0] 2.5 240

For matrix calculations, consider the package LinearAlgebra. A simple example below can illustrate how to get started:

from jhplot.math.LinearAlgebra import * array = [[1.,2.,3],[4.,5.,6.],[7.,8.,10.]] inverse=inverse(array) # calculate inverse matrix print inverse print trace(array) # calculate trace

While working with NxM matrices, consider another important library DoubleArray which helps to manipulate with double arrays. For example, this class has toString() method to print double arrays in a convenient format. Consider this example:

from jhplot.math.LinearAlgebra import * from jhplot.math.DoubleArray import * print dir() # list all imported methods array = [[1.,2.,3],[4.,5.,6.],[7.,8.,10.]] inverse=inverse(array) print toString("%7.3f", inverse.tolist()) # print the matrix

The above script prints all the methods for matrix manipulation and the inverse matrix itself:

-0.667 -1.333 1.000 -0.667 3.667 -2.000 1.000 -2.000 1.000

Below is a simple example of how to call Jama package to create a matrix to perform some manipulations.

from Jama import * array = [[1.,2.,3],[4.,5.,6.],[7.,8.,10.]] a = Matrix(array) b = Matrix.random(3,1) x = a.solve(b) Residual = a.times(x).minus(b); rnorm = Residual.normInf();

To print a matrix, one can make a simple function that converts a matrix to a string:

from Jama import * def toString(a): s="" for i in range(a.getRowDimension()): for j in range(a.getColumnDimension()): s=s+str(a.get(i,j))+" " s=s+ "\n" return s print toString(a) # print "a" (must be Matrix object)

This part is available for
full members.

For matrix manipulation, one can also use Apache Math Common Linear Algebra package: Look at the Linear Algebra Java package. Below we show a simple example of how to create and manipulate with matrices:

from org.apache.commons.math3.linear import * # Create a real matrix with two rows and three columns matrixData = [[1,2,3], [2,5,3]] m=Array2DRowRealMatrix(matrixData) # One more with three rows, two columns matrixData2 = [[1,2], [2,5], [1, 7]] n=Array2DRowRealMatrix(matrixData2) # Now multiply m by n p = m.multiply(n); print p.getRowDimension() # print 2 print p.getColumnDimension() # print 2 # Invert p, using LU decomposition inverse =LUDecompositionImpl(p).getSolver().getInverse();

la4j package provides a simple API to handle sparse and dense matrices. According to the La4j authors, the package has Linear systems solving (Gaussian, Jacobi, Zeidel, Square Root, Sweep and other), Matrices decomposition (Eigenvalues/Eigenvectors, SVD, QR, LU, Cholesky and other), and useful I/O (CSV and MatrixMarket format).

Let us consider how we define such matrices in this package:

Download for this example is disabled for non-members

1: # Linear Algebra/Matrices | C | 1.7 | S.Chekanov | Create new dense, sparse matrix from double array aand CSV file 2: 3: """ 4: <h1>Lightweight Java sparse/dense matrix library</h1> 5: 6: See <a href="http://la4j.org/">La4j</a> for detail 7: 8: 9: <p> 10: The <strong>la4j</strong> is open source and 100% Java library that provides <a href="http://mathworld.wolfram.com/LinearAlgebra.html">Linear Algebra</a> primitives (matrices and vectors) and algorithms. The la4j was initially designed to be lightweight and simple tool for passionate Java developers. It has been started as student project and turned into one of the most popular Java packages for matrices and vectors. 11: </p> 12: <p> 13: The key features of the la4j are listed bellow: 14: </p> 15: <ul> 16: <li>Great performance allied with beautiful design</li> 17: <li>No dependencies and tiny size (~150kb jar)</li> 18: <li>Fluent object-oriented/functional API</li> 19: <li>Sparse (<a href="http://netlib.org/linalg/html_templates/node91.html#SECTION00931100000000000000">CRS</a>, <a href="http://netlib.org/linalg/html_templates/node92.html#SECTION00931200000000000000">CCS</a>) and dense (1D/2D arrays) matrices</li> 20: <li>Linear systems solving (Gaussian, Jacobi, Zeidel, Square Root, Sweep and other)</li> 21: <li>Matrices decomposition (Eigenvalues/Eigenvectors, SVD, QR, LU, Cholesky and other)</li> 22: <li><a href="http://math.nist.gov/MatrixMarket/formats.html">MatrixMarket</a>/<a href="http://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> IO formats support for matrices and vectors</li> 23: </ul> 24: <br /> 25: <hr /> 26: 27: """ 28: 29: 30: from org.la4j.matrix.dense import * 31: from org.la4j.matrix.sparse import * 32: from org.la4j.matrix import * 33: from java.io import FileInputStream 34: 35: 36: print "Dense matrix" 37: m=Basic2DMatrix( [[1.0, 2.0, 3.0 ], 38: [4.0, 5.0, 6.0], 39: [7.0, 8.0, 9.0]]) 40: 41: print "Sparse matrix" 42: m=CRSMatrix( [[1.0, 2.0, 3.0 ], 43: [4.0, 5.0, 6.0], 44: [7.0, 8.0, 9.0]]) 45: 46: print "Matrix from CSV" 47: file=open("matrix.csv","w") 48: file.write("1.0, 2.0, 3.0\n") 49: file.write("1.0, 2.0, 3.0\n") 50: file.write("1.0, 2.0, 3.0\n") 51: file.close() 52: 53: a = Basic2DMatrix(Matrices.asSymbolSeparatedSource( 54: FileInputStream("matrix.csv"))) 55: 56: 57: 58: 59: print "We want to take first row of the matrix 'a' as sparse vector 'b'" 60: b = a.toRowVector(); 61: print c.toString() 62: 63: 64: print "We want to take first column of the matrix 'a' as sparse vector 'c'" 65: c = a.toColumnVector(); 66: print c.toString()

Let us show how to perform manipulations with such matrices. In the example shown below, we multiply matrices and then perform a transformation of matrices using an arbitrary function:

You have a limited access to this part.
Unlock this text after becoming a full member.

The EJML package provides 2 types of matrices:

- DenseMatrix64F - a dense matrix with elements that are 64-bit floats (doubles)
- SimpleMatrix - a wrapper around DenseMatrix64F that provides an easy to use object oriented interface for performing matrix operations.

EJML library provides the following operations:

- Basic Matrix Operators (addition, multiplication … )
- Matrix Manipulation (extract, insert, combine… )
- Linear Solvers (linear, least squares, incremental… )
- Matrix Decompositions (LU, QR, Cholesky, SVD, Eigenvalue …)
- Matrix Features (rank, symmetric, definitiveness … )
- Creating random Matrices (covariance, orthogonal, symmetric … )
- Different Internal Formats (row-major, block)
- Unit Testing
- Saving matrices into CSV files

Let us give a simple example using Jython: We create a few matrices and perform some algebra (multiplication, inverse etc). We also computes the eigen value decomposition and will print the answer:

You have a limited access to this part.
Unlock this text after becoming a full member.

You can test various features of a matrix using this MatrixFeatures API. For example, let's check “SkewSymmetric” feature of a given matrix:

You have a limited access to this part.
Unlock this text after becoming a full member.

You can save matrices in CVS files or binary formats. The example below shows how to do this:

You have a limited access to this part.
Unlock this text after becoming a full member.

Finally, you can visualize the matrices. The example below creates a matrix and then shows it's state in a window. Block means an element is zero. Red positive and blue negative. More intense the color larger the element's absolute value is.

You have a limited access to this part.
Unlock this text after becoming a full member.

The above example shows a graphic representation of the matrix defined as:

A=DenseMatrix64F(4,4,True,[0,2,3,4,-2,0,2,3,-3,-2,0,2,-4,-3,-2,0])

SCaVis supports multidimensional matrices and operations similar to Numpy.
The difference, however, you can use native Java and plus other scripting languages, such as Python or Groovy.
Let us build a matrix in 4 dimensions in Python using `org.nd4j.linalg.factory.Nd4j`

factory:

from org.nd4j.linalg.api.ndarray import INDArray from org.nd4j.linalg.factory import Nd4j; n = Nd4j.create(Nd4j.ones(81).data(), [3,3,3,3]) print n

Here we build a matrix 3x3x3x3 and filled it with 1. The last arguments specifies the dimension of the matrix (3x3x3x3), while the first argument its values. In a more general approach, you can assign any values at the initialization step:

nd = Nd4j.create([1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.], [2, 6]) # 2x6 matrix with values nd = Nd4j.create([2, 2]) # empty 2x2

Now let us show how to manipulate and transform with multidimensional matrices.

You have a limited access to this part.
Unlock this text after becoming a full member.

Read more on ND4J web page on how to use more methods and how to program in Java.
You can save arrays and matrices in a compressed serialized form, as well as using XML form. Look at the Section (Input and output).

Please refer Sect.Linear equations for this topic.

Matrix manipulation can be performed on multiple cores taking the advantage of parallel processing supported by Java virtual machine. In this approach, all processing cores of your computer will be used for calculations (or only a certain number of core as you have specified). Below we give a simple example. In the example below we create a large matrix 2000×2000 and calculate various characteristics of such matrix (cardinality, vectorize). We compare single threaded calculations with multithreaded ones (in this case, we set the number of cores to 2, but feel free to set to a large value).

To build random 2D matrices use DoubleFactory2D. Here is a short example to create 1000×1000 matrix and fill it with random numbers:

from cern.colt.matrix import * from edu.emory.mathcs.utils import ConcurrencyUtils ConcurrencyUtils.setNumberOfThreads(4) # set 4 numbers of threads M=tdouble.DoubleFactory2D.dense.random(1000, 1000) # random matrix

Below is a simple example which shows matrix operations on several cores. We set 2 cores, but you should remove the method “ConcurrencyUtils.setNumberOfThreads()” if you what to let the machine to determine the core automatically.

You are not full member and have a limited access to this section.
One can unlock this part after becoming a full member.