Class Matrices

java.lang.Object
  extended byMatrices

public class Matrices
extends java.lang.Object

Matrices provides an assortment of Matrix utilities - add, subtract, multiply, transpose, identity.
Adds two matrices of the same order.

A + B = C
0 1 2   2 0 1   2 1 3
1 2 3 + 0 1 0 = 1 3 3
3 4 5   2 0 3   5 4 8

C[r][c] = A[r][c] + B[r][c]
C[0][0] = 0 + 2 = 2, C[0][1] = 1 + 0 = 1, C[0][2] = 2 + 1 = 3
C[1][0] = 1 + 0 = 1, C[1][1] = 2 + 1 = 3, C[1][2] = 3 + 0 = 3
C[2][0] = 3 + 2 = 5, C[2][1] = 4 + 0 = 4, C[2][2] = 5 + 3 = 8
 

Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.
A * B = C
0 1 2   2 0 1   4 1 6
1 2 3 * 0 1 0 = 8 2 10
3 4 5   2 0 3   16 4 18

C[r][c] = A[r][0] * B[0][c] + A[r][1] * B[1][c] + ... + A[r][k] * B[k][c]
C[0][0] = 0*2 + 1*0 + 2*2 = 4,   C[0][1] = 0*0 + 1*1 + 2*0 = 1,  C[0][2] = 0*1 + 1*0 + 2*3 = 6
C[1][0] = 1*2 + 2*0 + 3*2 = 8,   C[1][1] = 1*0 + 2*1 + 3*0 = 2,  C[1][2] = 1*1 + 2*0 + 3*3 = 10
C[2][0] = 3*2 + 4*0 + 5*2 = 16, C[2][1] = 3*0 + 4*1 + 5*0 = 4,  C[2[2] = 3*1 + 4*0 + 5*3 = 18

D (3 x 2) * E (2 x 3) = F (3 x 3)
0 1     2 0 1   0 1 0
1 2   * 0 1 0 = 2 2 1
3 4             6 4 3

F[r][c] = D[r][0] * E[0][c] + D[r][1] * E[1][c] + ... + D[r][k] * E[k][c]
F[0][0] = 0*2 + 1*0 = 0,  F[0][1] = 0*0 + 1*1 = 1, F[0][2] = 0*1 + 1*0 = 0
F[1][0] = 1*2 + 2*0 = 2,  F[1][1] = 1*0 + 2*1 = 2, F[1][2] = 1*1 + 2*0 = 1
F[2][0] = 3*2 + 4*0 = 6,  F[2][1] = 3*0 + 4*1 = 4, F[2][2] = 3*1 + 4*0 = 3

Author:
Jack Tompkins
Source Code: Matrices.java

Constructor Summary
Matrices()
           
 
Method Summary
static double[][] add(double[][] A, double[][] B)
          Adds two matrices of the same order.
static double[][] add(double[][] A, int[][] B)
          Adds two matrices of the same order.
static double[][] add(int[][] A, double[][] B)
          Adds two matrices of the same order.
static int[][] add(int[][] A, int[][] B)
          Adds two matrices of the same order.
static double[][] identity(double n)
          The identity matrix of order n.
static int[][] identity(int n)
          The identity matrix of order n.
static void main(java.lang.String[] args)
           
static double[][] multiply(double[][] A, double[][] B)
          Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.
static double[][] multiply(double[][] A, int[][] B)
          Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.
static double[][] multiply(int[][] A, double[][] B)
          Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.
static int[][] multiply(int[][] A, int[][] B)
          Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.
static void printArray(java.lang.String title, double[][] A)
          Prints each element of a double array, one row at a time.
static void printArray(java.lang.String title, int[][] A)
          Prints each element of a double array, one row at a time.
static double[][] subtract(double[][] A, double[][] B)
          Subtracts two matrices of the same order.
static double[][] subtract(double[][] A, int[][] B)
          Subtracts two matrices of the same order.
static double[][] subtract(int[][] A, double[][] B)
          Subtracts two matrices of the same order.
static int[][] subtract(int[][] A, int[][] B)
          Subtracts two matrices of the same order.
static double[][] transpose(double[][] A)
          interchanges the rows and columns of matrix A
static int[][] transpose(int[][] A)
          interchanges the rows and columns of matrix A
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Matrices

public Matrices()
Method Detail

main

public static void main(java.lang.String[] args)

printArray

public static void printArray(java.lang.String title,
                              int[][] A)
Prints each element of a double array, one row at a time.

Parameters:
title - printed before the array on a separate line.
A - double array, size m X n

printArray

public static void printArray(java.lang.String title,
                              double[][] A)
Prints each element of a double array, one row at a time.

Parameters:
title - printed before the array on a separate line.
A - double array, size m X n

multiply

public static int[][] multiply(int[][] A,
                               int[][] B)
Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.
A * B = C
0 1 2   2 0 1   4 1 6
1 2 3 * 0 1 0 = 8 2 10
3 4 5   2 0 3   16 4 18

C[r][c] = A[r][0] * B[0][c] + A[r][1] * B[1][c] + ... + A[r][k] * B[k][c]
C[0][0] = 0*2 + 1*0 + 2*2 = 4,   C[0][1] = 0*0 + 1*1 + 2*0 = 1,  C[0][2] = 0*1 + 1*0 + 2*3 = 6
C[1][0] = 1*2 + 2*0 + 3*2 = 8,   C[1][1] = 1*0 + 2*1 + 3*0 = 2,  C[1][2] = 1*1 + 2*0 + 3*3 = 10
C[2][0] = 3*2 + 4*0 + 5*2 = 16, C[2][1] = 3*0 + 4*1 + 5*0 = 4,  C[2[2] = 3*1 + 4*0 + 5*3 = 18

D (3 x 2) * E (2 x 3) = F (3 x 3)
0 1     2 0 1   0 1 0
1 2   * 0 1 0 = 2 2 1
3 4             6 4 3

F[r][c] = D[r][0] * E[0][c] + D[r][1] * E[1][c] + ... + D[r][k] * E[k][c]
F[0][0] = 0*2 + 1*0 = 0,  F[0][1] = 0*0 + 1*1 = 1, F[0][2] = 0*1 + 1*0 = 0
F[1][0] = 1*2 + 2*0 = 2,  F[1][1] = 1*0 + 2*1 = 2, F[1][2] = 1*1 + 2*0 = 1
F[2][0] = 3*2 + 4*0 = 6,  F[2][1] = 3*0 + 4*1 = 4, F[2][2] = 3*1 + 4*0 = 3

Parameters:
A - double array, size m X k of ints.
B - double array, size k X n of ints
Returns:
The product AB, a matrix size m X n

multiply

public static double[][] multiply(double[][] A,
                                  double[][] B)
Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.

Parameters:
A - double array, size m X k of doubles.
B - double array, size k X n of doubles
Returns:
The product AB, a matrix size m X n

multiply

public static double[][] multiply(double[][] A,
                                  int[][] B)
Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.

Parameters:
A - double array, size m X k of doubles.
B - double array, size k X n of ints
Returns:
The product AB, a matrix size m X n

multiply

public static double[][] multiply(int[][] A,
                                  double[][] B)
Multiplies compatible matrices -the number of columns of the first matches the number of rows of the second.

Parameters:
A - double array, size m X k of ints.
B - double array, size k X n of doubles
Returns:
The product AB, a matrix size m X n

transpose

public static int[][] transpose(int[][] A)
interchanges the rows and columns of matrix A

Parameters:
A - double array of ints, matrix A, an m X n matrix.
Returns:
double array of ints, matrix At, an n X m matrix - the transpose of A

transpose

public static double[][] transpose(double[][] A)
interchanges the rows and columns of matrix A

Parameters:
A - double array of doubles, matrix A, an m X n matrix.
Returns:
double array of doubles, matrix At, an n X m matrix - the transpose of A

identity

public static int[][] identity(int n)
The identity matrix of order n.

Parameters:
n - the order of this identity matrix.
Returns:
a double array, order n, of ints - identity matrix - ones along the main diagonal, zeros elsewhere

identity

public static double[][] identity(double n)
The identity matrix of order n.

Parameters:
n - the order of this identity matrix.
Returns:
a double array, order n, of doubles - identity matrix - ones along the main diagonal, zeros elsewhere

add

public static int[][] add(int[][] A,
                          int[][] B)
Adds two matrices of the same order.
A + B = C
0 1 2   2 0 1   2 1 3
1 2 3 + 0 1 0 = 1 3 3
3 4 5   2 0 3   5 4 8

C[r][c] = A[r][c] + B[r][c]
C[0][0] = 0 + 2 = 2, C[0][1] = 1 + 0 = 1, C[0][2] = 2 + 1 = 3
C[1][0] = 1 + 0 = 1, C[1][1] = 2 + 1 = 3, C[1][2] = 3 + 0 = 3
C[2][0] = 3 + 2 = 5, C[2][1] = 4 + 0 = 4, C[2][2] = 5 + 3 = 8
 

Parameters:
A - double array, size m X n of ints.
B - double array, size m X n of ints
Returns:
the sum A + B, a double array of ints

add

public static double[][] add(double[][] A,
                             double[][] B)
Adds two matrices of the same order.

Parameters:
A - double array, size m X n of doubles.
B - double array, size m X n of doubles
Returns:
the sum A + B, a double array of doubles

add

public static double[][] add(double[][] A,
                             int[][] B)
Adds two matrices of the same order.

Parameters:
A - double array, size m X n of doubles.
B - double array, size m X n of ints
Returns:
the sum A + B, a double array of doubles

add

public static double[][] add(int[][] A,
                             double[][] B)
Adds two matrices of the same order.

Parameters:
A - double array, size m X n of ints.
B - double array, size m X n of doubles
Returns:
the sum A + B, a double array of doubles

subtract

public static int[][] subtract(int[][] A,
                               int[][] B)
Subtracts two matrices of the same order.

Parameters:
A - double array, size m X n of ints.
B - double array, size m X n of ints
Returns:
the difference A - B, a double array of ints

subtract

public static double[][] subtract(double[][] A,
                                  double[][] B)
Subtracts two matrices of the same order.

Parameters:
A - double array, size m X n of doubles.
B - double array, size m X n of doubles
Returns:
the difference A - B, a double array of doubles

subtract

public static double[][] subtract(double[][] A,
                                  int[][] B)
Subtracts two matrices of the same order.

Parameters:
A - double array, size m X n of doubles.
B - double array, size m X n of ints
Returns:
the difference A - B, a double array of doubles

subtract

public static double[][] subtract(int[][] A,
                                  double[][] B)
Subtracts two matrices of the same order.

Parameters:
A - double array, size m X n of ints.
B - double array, size m X n of doubles
Returns:
the difference A - B, a double array of doubles