0% found this document useful (0 votes)
19 views7 pages

Lab 05

Uploaded by

dvfrdrfg rfgdeg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Lab 05

Uploaded by

dvfrdrfg rfgdeg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Islamic University of Gaza

Engineering Faculty

Computer Engineering Department

Discrete Mathematics
Matrices

October 20, 2022


2

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]

Inversion of 2×2 mat rices


Inversion of 2x2 matrices can be done easily as follow s:

Solve linear equat ions


Let ax + by = e
cx + dy = f

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.

2. W rite a program that solve tw o linear equations.


import java.util.*;

public class Task1 {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);

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("Enter the first matrix:");


double[][] A = new double[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
A[i][j] = in.nextInt();
System.out.println("Enter the second matrix:");
double[][] B = new double[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
B[i][j] = 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();

System.out.println("Enter the first matrix:");


double[][] A = new double[m][k];
for (int i = 0; i < m; i++)
for (int j = 0; j < k; j++)
A[i][j] = in.nextDouble();
System.out.println("Enter the second matrix:");
double[][] B = new double[k][n];
for (int i = 0; i < k; i++)
for (int j = 0; j < n; j++)
5

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

for (int i = 0; i < mat.length; i++)


res.append(Arrays.toString(mat[i]) + "\n");

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
}

public static String solve(double[][] A, double[][] B) {


// Code here
}
}
6

Expect ed out put :


7

References
[1] Discrete Mathematics and Its Applications by Kenneth H. Rosen, Eighth Edition, page
188.
[2] Previous reference, page 189.

You might also like