#### Sums and Scalar Multiples

Two matrices would be *equal* if they have the same *size* (that

is, the same number of rows and the same number of columns) and also their

corresponding entries are equal. Matrix ** A** is not

*equal*

to matrix

**, although they have the same matrix**

*B**size*=

**. The corresponding entries at index (1,1)**

*3 x 2*are not the same for matrix

**and**

*A***because**

*B***and**

*A(1,1) = 3***, and so they are**

*B(1,1) = 4*different.

*Note*that the entry-index starts at zero.

double[][] arrayA = {{4.,0.},{5.,3.},{-2.,1.}};

//Instantiate matrix object A

Matrix A = new Matrix(arrayA);

double[][] arrayB = {{4.,0.},{5.,4.},{-2.,1.}};

//Instantiate matrix object B

Matrix B = new Matrix(arrayB);

double[][] arrayC = {{1.,7.},{7.,4.}};

//Instantiate matrix object C

Matrix C = Matrix(arrayC);

double[][] arrayD = {{1.,0.},{0.,1.}};

//Instantiate matrix object D

Matrix D = new Matrix(arrayD);

The additions (sums) and subtractions (difference) of *matrices* is done

element-wise. The sum of ** A**+

**would result in a**

*B*matrix the same

*size*as

**or**

*A***with all**

*B*entries as the sum of

**a**and

_{ij}**b**, for example, the resultant entry of

_{ij }**(**= 7,

*1*,*1*)which is

**a**+

_{11}**b**= 3+4 = 7.

_{11}The sum of two different size (dimemsion) matrices is

*undefined*such as

**+**

*A***, because**

*C**size*

**(**

*A*) = 3 by 2and

*size*

**(**. In multiplying a

*C*) = 2 by 2*matrix*by

a scaler results in a

*matrix*with all

the entries scaled by this scaler-value.

**is a**

*3*B*multiplication of

**by a scaler value of**

*B***in**

*3*which all entries are now

**.**

*3b*_{ij}

Matrix A_plus_B = A.plus(B) ;

//returned matrix has all its entries scaled by 3.

Matrix matrix_3_times_B = B.times(3.);

//This following line will throw an IllegalArgumentException because matrix dimensions do

//not agree, matrix A is a 4 by 2 but C is a 2 by 2 size matrix.

Matrix A_plus_C = A.plus(C);

#### Matrix Multiplication

Under matrix multiplication, such as ** A***

**, the inner**

*C*dimensions have to be the same for it to be defined, regardless of the outer

dimensions. In this case size

**(**and size

*A*) = [3 x 2]**(**

= [2 x 2]; therefore, matrix multiplication is defined because they have the

*C*)= [2 x 2]

same inner dimensions of two, [3 x

**][**

*2***x 2].**

*2*The dimension of the result of multiplying two matrices would be the outer

dimensions of the respective matrices.

*****

*A***would**

*C*result in a matrix with size

**(**= [

*A***C*)**3**x

**2**],

that is [

**x 2][2 x**

*3***].**

*2*Matrix multiplication results in all entries

**(**equals

*i*,*j*)the summation of element-wise multiplication of each

*row*of

**to each**

*A**column*of

**. Multiplication of**

*C******

*A***is undefined, because the**

*B**inner*dimensions do not agree:

*****

*A***is [3 x**

*B***]*[**

*2***x 2]. Multiplication of**

*3*matrices is not always

*commutative*(order of operation),

*****

*B***is defined, but reversing the order as**

*C******

*C***is undefined but**

*B******

*C***=**

*D******

*D***. Matrix multiplication is directly**

*C*in contrast with its

*elementary*counterpart,

**3**x

**4**=

**4**x

**3**=

**12**.

Matrix

**is a special type called**

*D**Identity*, and this

is the equivalent in

*elementary*arithmetic of the number

**,**

*1*which is the multiplication

*identity*,

**x**

*6*

*1*=

**.**

*6**Identity*is

a

*square-matrix*(number of

*rows*=

*columns*) with any

dimension and all its elements at the

*diagonal*, from

top-left to bottom-right are ones and zeros everywhere. Any

*matrix*

that is multiplied to the

*Identity*is always that matrix, examples are:

*****

*A***=**

*D***,**

*A******

*B***=**

*D***and**

*B******

*C***=**

*D***.**

*C*

Matrix A_times_C = A.times(C) ;

//The following line will throw an IllegalArgumentException because inner matrix

//dimensions do not agree.

Matrix A_times_B = A.times(B);

#### Transpose of a Matrix

Given an ** m** x

**matrix**

*n***, the**

*E***of**

*transpose***is the**

*E***x**

*n*

*m*matrix denoted by

**, whose columns are formed from**

*E*^{T}the corresponding rows of

**. General matrix**

*E**transposition*

operations:

1. (** F** +

**)**

*G*^{T}=

**+**

*F*^{T}

*G*^{T}

Matrix result1 = F.plus(G).transpose() ;

Matrix result2 = F.transpose().plus(G.transpose());

//Matrix result1 and result2 are equals (same value in all entries)

2. (** F^{T}**)

^{T}=

*F*

Matrix result3 = F.transpose().transpose() ;

//Matrix result3 and F are equals (same value in all entries)

3. (** F** *

**)**

*G*^{T}=

*****

*G*^{T}

*F*^{T}

Matrix result4 = F.times(G).transpose() ;

Matrix result5 = G.transpose().times(F.transpose()) ;

//Matrix result4 and result5 are equals (same value in all entries)

4. For any **scalar** ** r**, (

*****

*r***)**

*F*^{T}=

*****

*r*

*F*^{T}

Matrix result6 = F.times(r).transpose() ;

Matrix result7 = F.transpose().times(r) ;

//Matrix result6 and result7 are equals (same value in all entries)

#### Inverse of a Matrix

If ** K** is an [

**x**

*n***] matrix,**

*n*sometimes there is another [

**x**

*n***] matrix**

*n***such that:**

*L*
** K** *

**=**

*L***and**

*I******

*L***=**

*K*

*I*where

**is an [**

*I***x**

*n***]**

*n**Identity*matrix

We say ** K** is invertible and

**is an inverse of**

*L***.**

*K*The inverse of

**is denoted by**

*K***.**

*K*^{-1}
** K** *

**=**

*K*^{-1}**, where**

*I***–**

*I*is the

*Identity*matrix

*****

*K*^{-1}**=**

*K*

*I*

Matrix inverseMatrix = K.inverse() ;

//Matrix inverseMatrix is an inverse of matrix K

#### QR Matrix Factorization

Factorizing a matrix follows a similar concept in elementary arithmetics. The

number ** 30** has prime factors of =

**x**

*2*

*3*x

**. If**

*5***is an [**

*P***x**

*m***]**

*n*matrix with linearly independent columns, then it can be factored as:

** P** =

*****

*Q***:**

*R*where

**is an [**

*Q***x**

*m***] whose columns form**

*m*an orthonormal basis and

**is an [**

*R***x**

*m***]**

*n*upper triangular invertible matrix with positive entries on its diagonal.

double[][] arrayP = {{1.,0.},{1.,1.},{1.,1.},{1.,1.}};

Matrix P = new Matrix(arrayP);

//Create a QR factorisation object from matrix P.

QRDecomposition QR = new QRDecomposition(P);

Matrix Q = QR.getQ();

Matrix R = QR.getR();

//Now that we have two matrix factors for matrix P which: Q*R = P

#### Elementary Matrix Function and Special Matrices

Jamlab package

Repeat a

*matrix*in a tiling manner. If matrix

**is to**

*A*be tiled [

**by**

*3***], then**

*2*

*A*would be tiled in a

**-rows by**

*3***-columns pattern.**

*2*

double[][] arrayA = {{7,5},{2,3}};

Matrix A = new Matrix(arrayA);

//Create matrix object B by tiling matrix A in a 3 by 2 pattern.

Matrix B = JElmat.repmat(A,3,2);

Create a

*Vandermonde*matrix in which the second to last column are the elements of

array a[].

The matrix has

*numCol*number of columns.

//Create Vandermonde-matrix

double[] arrayA = {5,-1,7,6};

//vander is also overloaded with one input argument a double[] array, where columns = rows

Matrix A = JElmat.vander(arrayA);

double[] arrayB = {5,-1,7,6,-3,4};

//Create a Vandermonde-matrix with only four columns.

Matrix B = JElmat.vander(arrayB,4);

sum all the elements of the column of a matrix and return a single row matrix.

double arrayA = {{19,18,16,18},{5,15,9,15},{12,9,12,4},{10,0,16,8}};

Matrix A = new Matrix(arrayA);

//Sum all the columns of matrix A.

Matrix B = JDatafun.sum(A);

class

Linear spaced matrix row vector. Generates

*nPoints*between

*leftBound*and

*rightBound*.

//row matrix A, starting at -8. and ending at 12. with eleven points equally spaced.

//internal array A = (-8 -6 -4 -2 0 2 4 6 8 10 12)

Matrix A = JElmat.linspace(-8.,12.,11);

returns the M-by-N matrix whose elements are taken columnwise from A.

double[][] arrayA = {{1.,2.,3.,4.},{5.,6.,7.,8.},{9.,10.,11.,12.}};

Matrix A = new Matrix(arrayA);

//reshape matrix A from 3 x 4 dimension into 6 x 2

Matrix B = JElmat.reshape(A,6,2);

returns an m x n matrix of zeros.

returns an m x n matrix of ones.

//Create a matrix of ones with 3 x 4 size.

Matrix X = JElmat.ones(3,4);

//Create a matrix of zeros with 2 x 2 size.

Matrix Y = JElmat.zeros(2,2);

We will continue with polynomial fittings and Java code in Part 3.

Download ** Jama** and

**and**

*Jamlab***.**

*Jamlab documentation*#### References

*Java for Engineers and Scientists*by Stephen J. Chapman, Prentice Hall, 1999.*Introductory Java for Scientists and Engineers*by Richard J. Davies, Addison-Wesley Pub. Co., 1999.*Applied Numerical Analysis (Sixth Edition)*by Curtis F. Gerald and Patrick O. Wheatly, Addison-Wesley Pub. Co., 1999.*Linear Algebra and Its Applications (Second Edition)*, by David C. Lay, Addison-Wesley Pub. Co.*Numerical Recipes in Fortran 77, the Art of Scientific Computing (Volume 1)*by William H. Press, Saul A. Teukolsky, William T. Vetterling, and Brian P. Flannery, Cambridge University Press, 1997.*Mastering MATLAB 5: A Comprehensive Tutorial and Reference*by Duane Hanselman and Bruce Littlefield, Prentice Hall, 1997.*Advanced Mathematics and Mechanics Applications using MATLAB*by Louis H. Turcotte and Howard B. Wilson, CRC Press, 1998.

#### Related Articles

- Java as a Scientific Programming Language (Part 1): More Issues for Scientific Programming in Java
- Scientific Computing in Java (Part 2): Writing Scientific Programs in Java
- Using Java in Scientific Research: An Introduction

#### About the Author

*Sione Palu is a Java developer at Datacom in Auckland, New Zealand, currently involved in a Web application development project. Palu graduated from the University of Auckland, New Zealand, double majoring in mathematics and computer science. He has a personal interest in applying Java and mathematics in the fields of mathematical modeling and simulations, expert systems, neural and soft computation, wavelets, digital signal processing, and control systems.*