# Java in Science: Data Inter- and Extrapolation Using Numerical Methods of Polynomial Fittings, Part 2

**Review Part 1**

#### 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) are not the same for matrix**

*3 x 2***and**

*A***because**

*B***and**

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

*B(1,1) = 4**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 matrix the same**

*B**size*as

**or**

*A***with all entries as the sum of**

*B***a**and

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

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

*1*,*1*)**a**+

_{11}**b**= 3+4 = 7. The sum of two different size (dimemsion) matrices is

_{11}*undefined*such as

**+**

*A***, because**

*C**size*

**(**and

*A*) = 3 by 2*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 multiplication of**

*3*B***by a scaler value of**

*B***in which all entries are now**

*3***.**

*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 dimensions have to be the same for it to be defined, regardless of the outer dimensions. In this case size**

*C***(**and size

*A*) = [3 x 2]**(**; therefore, matrix multiplication is defined because they have the same inner dimensions of two, [3 x

*C*) = [2 x 2]**][**

*2***x 2]. The dimension of the result of multiplying two matrices would be the outer dimensions of the respective matrices.**

*2******

*A***would result in a matrix with size**

*C***(**= [

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

**2**], that is [

**x 2][2 x**

*3***]. Matrix multiplication results in all entries**

*2***(**equals the summation of element-wise multiplication of each

*i*,*j*)*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 matrices is not always**

*3**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 in contrast with its**

*C**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

**, which is the multiplication**

*1**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***matrix denoted by**

*m***, whose columns are formed from the corresponding rows of**

*E*^{T}**. 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, sometimes there is another [**

*n***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***. The inverse of**

*K***is denoted by**

*K***.**

*K*^{-1}

** K** *

**=**

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

*I***- is the**

*I**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***x**

*3***. If**

*5***is an [**

*P***x**

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

*n*
** P** =

*****

*Q***:**

*R*where

**is an [**

*Q***x**

*m***] whose columns form an orthonormal basis and**

*m***is an [**

*R***x**

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

*n*

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

Repeat a

*matrix*in a tiling manner. If matrix

**is to be tiled [**

*A***by**

*3***], then**

*2***would be tiled in a**

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

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.*

*This article was originally published on June 20, 2001*

*
*