|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.ObjectMatrices
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
| 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
| 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 |
public Matrices()
| Method Detail |
public static void main(java.lang.String[] args)
public static void printArray(java.lang.String title,
int[][] A)
title - printed before the array on a separate line.A - double array, size m X n
public static void printArray(java.lang.String title,
double[][] A)
title - printed before the array on a separate line.A - double array, size m X n
public static int[][] multiply(int[][] A,
int[][] B)
| 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
A - double array, size m X k of ints.B - double array, size k X n of ints
public static double[][] multiply(double[][] A,
double[][] B)
A - double array, size m X k of doubles.B - double array, size k X n of doubles
public static double[][] multiply(double[][] A,
int[][] B)
A - double array, size m X k of doubles.B - double array, size k X n of ints
public static double[][] multiply(int[][] A,
double[][] B)
A - double array, size m X k of ints.B - double array, size k X n of doubles
public static int[][] transpose(int[][] A)
A - double array of ints, matrix A, an m X n matrix.
public static double[][] transpose(double[][] A)
A - double array of doubles, matrix A, an m X n matrix.
public static int[][] identity(int n)
n - the order of this identity matrix.
public static double[][] identity(double n)
n - the order of this identity matrix.
public static int[][] add(int[][] A,
int[][] B)
| 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
A - double array, size m X n of ints.B - double array, size m X n of ints
public static double[][] add(double[][] A,
double[][] B)
A - double array, size m X n of doubles.B - double array, size m X n of doubles
public static double[][] add(double[][] A,
int[][] B)
A - double array, size m X n of doubles.B - double array, size m X n of ints
public static double[][] add(int[][] A,
double[][] B)
A - double array, size m X n of ints.B - double array, size m X n of doubles
public static int[][] subtract(int[][] A,
int[][] B)
A - double array, size m X n of ints.B - double array, size m X n of ints
public static double[][] subtract(double[][] A,
double[][] B)
A - double array, size m X n of doubles.B - double array, size m X n of doubles
public static double[][] subtract(double[][] A,
int[][] B)
A - double array, size m X n of doubles.B - double array, size m X n of ints
public static double[][] subtract(int[][] A,
double[][] B)
A - double array, size m X n of ints.B - double array, size m X n of doubles
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||