GuidesJava in Science: Data Inter- and Extrapolation Using Numerical Methods of Polynomial...

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

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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 B, although they have the same matrix
size = 3 x 2. The corresponding entries at index (1,1)
are not the same for matrix A and B because
A(1,1) = 3 and B(1,1) = 4, and so they are
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+B would result in a
matrix the same size as A or B with all
entries as the sum of aij and bij
, for example, the resultant entry of (1,1) = 7,
which is a11+b11 = 3+4 = 7.
The sum of two different size (dimemsion) matrices is undefined such as
A+C, because size(A) = 3 by 2
and size(C) = 2 by 2. In multiplying a matrix by
a scaler results in a matrix with all
the entries scaled by this scaler-value. 3*B is a
multiplication of B by a scaler value of 3 in
which all entries are now 3bij.



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*C, the inner
dimensions have to be the same for it to be defined, regardless of the outer
dimensions. In this case size(A) = [3 x 2] and size(C)
= [2 x 2]
; therefore, matrix multiplication is defined because they have the
same inner dimensions of two, [3 x 2][2 x 2].
The dimension of the result of multiplying two matrices would be the outer
dimensions of the respective matrices. A*C would
result in a matrix with size(A*C) = [3 x 2],
that is [3 x 2][2 x 2].
Matrix multiplication results in all entries(i,j) equals
the summation of element-wise multiplication of each row of A
to each column of C. Multiplication of A
*B is undefined, because the inner dimensions do not agree:
A*
B
is [3 x 2]*[3 x 2]. Multiplication of
matrices is not always commutative (order of operation), B
*C is defined, but reversing the order as
C*B is undefined but C*
D
= D*C. Matrix multiplication is directly
in contrast with its elementary counterpart, 3 x 4 =
4 x 3 = 12.
Matrix D is a special type called Identity, and this
is the equivalent in elementary arithmetic of the number 1,
which is the multiplication identity, 6 x 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
= B and 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 n matrix E, the
transpose
of E is the n x m
matrix denoted by ET, whose columns are formed from
the corresponding rows of E. General matrix transposition
operations:

1. (F + G)T =
FT + GT



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. (FT)T = F



Matrix result3 = F.transpose().transpose() ;
//Matrix result3 and F are equals (same value in all entries)

3. (F * G)T =
GT * FT



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



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 [n x n] matrix,
sometimes there is another [n x n] matrix
L such that:

K * L = I and L *
K = I
where I is an [n x
n] Identity matrix

We say K is invertible and L is an inverse of K.
The inverse of K is denoted by K-1.

K * K-1 = I, where 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 = 2 x 3
x 5. If P is an [m x n]
matrix with linearly independent columns, then it can be factored as:

P = Q * R:
where Q is an [m x m] whose columns form
an orthonormal basis and R is an [m x 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

  • repmat (Matrix mat, int row, int col) – static method from JElmat class of
    Jamlab package
    Repeat a matrix in a tiling manner. If matrix A is to
    be tiled [3 by 2], then A
    would be tiled in a 3-rows by 2-columns pattern.


    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);
  • vander (double[] a,int numCol) – static method from JElmat.
    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 (Matrix matrix) – static method from JDatafun class.
    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);

  • linespace (double leftBound, double rightBound, int nPoints) – static method from JElmat
    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);

  • reshape (Matrix A, int M, int N) – static method from JElmat class.
    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);

  • zeros (int m, int n) – static method from JElmat class.
    returns an m x n matrix of zeros.

  • ones (int m, int n) – static method from JElmat class.
    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
    Jamlab and
    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

    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.

    Get the Free Newsletter!

    Subscribe to Developer Insider for top news, trends & analysis

    Latest Posts

    Related Stories