Professional Documents
Culture Documents
Lab 05
Lab 05
Engineering Faculty
Discrete Mathematics
Matrices
Overview
Matrices 3
Addition 3
Product 3
Inversion of 2 ×2 matrices 3
Solve linear equations 3
Labw ork 4
References 7
3
Mat rices
A mat rix is a rectangular array of numbers. A matrix w ith m row s and n columns is called
an m × n matrix. The plural of matrix is matrices. A matrix w ith the same number of row s
as columns is called square. Tw o matrices are equal if they have the same number of
row s and the same number of columns and the corresponding entries in every position
are equal. [1]
Addit ion
Let A = [aij], B = [bij] be m × n matrices. The sum of A and B, denoted by A + B, is the m
× n matrix that has aij + bij as its (i, j)th element. In other w ords, A + B = [aij + bij]. [2]
Product
Let A be an m × k matrix and B be a k × n matrix. The product of A and B, denoted by
AB, is the m × n matrix w ith its (i, j)th entry equal to the sum of the products of the
corresponding elements from the ith row of A and the jth column of B. In other w ords, if
AB = [cij], then cij = ai1b1j + ai2b2j + ⋯ + aikbkj [2]
You can see that the previous equations can be w ritten as:
𝑎𝑎 𝑏𝑏 𝑥𝑥 𝑒𝑒 𝑎𝑎 𝑏𝑏 −1
� � x [𝑦𝑦] = [𝑓𝑓], that mean you can find x, and y values by multiplying by � � at
𝑐𝑐 𝑑𝑑 𝑐𝑐 𝑑𝑑
both sides, thus:
𝑥𝑥 𝑎𝑎 𝑏𝑏 −1 𝑒𝑒
[𝑦𝑦] = � � x [𝑓𝑓]
𝑐𝑐 𝑑𝑑
4
Labw ork
1. W rite a program to print the addition of tw o mxn matrices, and the product of mxk
and kxn matrices.
while (true) {
System.out.print("1- Addition\n"
+ "2- Product\n"
+ "3- Solve linear equations\n"
+ "Choose: ");
int choice = in.nextInt();
if (choice == 1) {
System.out.print("Enter m, and n: ");
int m = in.nextInt(), n = in.nextInt();
System.out.println("Result: ");
System.out.println(addition(A, B));
} else if (choice == 2) {
System.out.print("Enter m, k, and n: ");
int m = in.nextInt(), k = in.nextInt(), n = in.nextInt();
B[i][j] = in.nextDouble();
System.out.println("Result: ");
System.out.println(product(A, B));
} else if (choice == 3) {
System.out.println("Enter coefficient matrix: ");
double[][] A = new double[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
A[i][j] = in.nextDouble();
System.out.println("Enter results: ");
double e = in.nextDouble(), f = in.nextDouble();
System.out.println("[x, y] = ");
System.out.println(solve(A, new double[][] {
{ e },
{ f },
}));
} else {
System.out.println("Invalid input!");
break;
}
}
}
public static String matrixToString(double[][] mat) {
StringBuilder res = new StringBuilder();
return res.toString();
}
public static String addition(double[][] A, double[][] B) {
double[][] res = new double[A.length][A[0].length];
// Code here
return matrixToString(res);
}
public static String product(double[][] A, double[][] B) {
double[][] res = new double[A.length][B[0].length];
// Code here
return matrixToString(res);
}
public static double[][] inverse(double[][] A) {
// Code here
}
References
[1] Discrete Mathematics and Its Applications by Kenneth H. Rosen, Eighth Edition, page
188.
[2] Previous reference, page 189.