### Table of Contents

# Linear Algebra

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

# Vectors

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

# Matrices

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

## Scripting using Jama

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)

## Summary of JAMA Capabilities

## Linear Algebra with Apache Math

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();

# Dense and sparse matrices

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:

1: from org.la4j.matrix.dense import * 2: from org.la4j.matrix.sparse import * 3: from org.la4j.matrix import * 4: from java.io import FileInputStream 5: 6: 7: print "Dense matrix" 8: m=Basic2DMatrix( [[1.0, 2.0, 3.0 ], 9: [4.0, 5.0, 6.0], 10: [7.0, 8.0, 9.0]]) 11: 12: print "Sparse matrix" 13: m=CRSMatrix( [[1.0, 2.0, 3.0 ], 14: [4.0, 5.0, 6.0], 15: [7.0, 8.0, 9.0]]) 16: 17: print "Matrix from CSV" 18: file=open("matrix.csv","w") 19: file.write("1.0, 2.0, 3.0\n") 20: file.write("1.0, 2.0, 3.0\n") 21: file.write("1.0, 2.0, 3.0\n") 22: file.close() 23: 24: a = Basic2DMatrix(Matrices.asSymbolSeparatedSource( 25: FileInputStream("matrix.csv"))) 26: 27: print "We want to take first row of the matrix 'a' as sparse vector 'b'" 28: b = a.toRowVector(); 29: print c.toString() 30: 31: print "We want to take first column of the matrix 'a' as sparse vector 'c'" 32: c = a.toColumnVector(); 33: 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:

# Dense matrices in EJML

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 can test various features of a matrix using this MatrixFeatures API. For example, let's check “SkewSymmetric” feature of a given matrix:

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

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.

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])

# Input and output

You can save arrays and matrices in a compressed serialized form, as well as using XML form. Look at the Section (Input and output).

# Solving linear equations

Please refer Sect.Linear equations for this topic.

# Matrix operations using multiple cores

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.