Numeric Matrix Processor (Java). Stage 3/6

Matrix by matrix multiplication

Report a typo

Description

The next stage is the multiplication of matrices. This operation is a little more complex because it’s not enough to simply multiply the corresponding elements.

Unlike with addition, the sizes of the matrices can be different: the only restriction is that the number of columns in the first matrix should equal the number of rows of the second matrix.

The multiplication of a matrix AA with nn rows and mm columns and a matrix BB with mm rows and kk columns is Cn,k=An,m×Bm,kC_{n , k} = A_{n , m} \times B_{m, k}.

The resulting matrix has nn rows and kk columns, where every element is a sum of the multiplication of mm elements across the rows of matrix AA by mm elements down the columns of matrix BB.

Another really important thing is that Ai,j×Bj,kA_{i , j} \times B_{j , k} is not equal to Bj,k×Ai,jB_{j, k} \times A_{i , j}. In fact, these are not even possible to multiply if ki. k \ne i. If k=i k = i , the resulting matrices would still be different.

Take a look at this example of matrix multiplication:

(177664421)×(3245598010)=(1×3+7×5+7×81×2+7×5+7×01×4+7×9+7×106×3+6×5+4×86×2+6×5+4×06×4+6×9+4×104×3+2×5+1×84×2+2×5+1×04×4+2×9+1×10)=(94371378042118301844)\begin{pmatrix} 1 & 7 & 7 \\ 6 & 6 & 4 \\ 4 & 2 & 1 \end{pmatrix} \times \begin{pmatrix} 3 & 2 & 4 \\ 5 & 5 & 9 \\ 8 & 0 & 10 \end{pmatrix} = \begin{pmatrix} 1\times3+7\times5+7\times8 & 1\times2+7\times5+7\times0 & 1\times4+7\times9+7\times10 \\ 6\times3+6\times5+4\times8 & 6\times2+6\times5+4\times0 & 6\times4+6\times9+4\times10 \\ 4\times3+2\times5+1\times8 & 4\times2+2\times5+1\times0 & 4\times4+2\times9+1\times10 \end{pmatrix} = \begin{pmatrix} 94 & 37 & 137 \\ 80 & 42 & 118 \\ 30 & 18 & 44 \end{pmatrix}

Objectives

In this stage, you should write a program that can do all operations on matrices that you've learned.

Write a program that does the following:

  1. Prints a menu consisting of 4 options. The example shows what the menu should look like.

  2. Reads the user's choice.

  3. Reads all data (matrices, constants) needed to perform the chosen operation. The example shows the input format in each case.

  4. Calculates the result and outputs it. The example shows what your output should look like.

  5. Repeats all these steps.

The program should keep repeating this until the "Exit" option is chosen.

If some operation cannot be performed, output a warning message.

Also, you should support floating-point numbers.

Example

The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.

The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.

Example 1: the user chooses the addition of matrices

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice: > 1
Enter size of first matrix: > 4 5
Enter first matrix:
> 1 2 3 4 5
> 3 2 3 2 1
> 8 0 9 9 1
> 1 3 4 5 6
Enter size of second matrix: > 4 5
Enter second matrix:
> 1 1 4 4 5
> 4 4 5 7 8
> 1 2 3 9 8
> 1 0 0 0 1
The result is:
2 3 7 8 10
7 6 8 9 9
9 2 12 18 9
2 3 4 5 7

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice: >

Example 2: the user chooses the multiplication of a matrix by a constant

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice: > 2
Enter size of matrix: > 2 2
Enter matrix:
> 1.5 7.0
> 6.0 5.0
Enter constant: > 0.5
The result is:
0.75 3.5
3.0 2.5

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice: >

Example 3: the user chooses the multiplication of matrices

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice: > 3
Enter size of first matrix: > 3 3
Enter first matrix:
> 1 7 7
> 6 6 4
> 4 2 1
Enter size of second matrix: > 3 3
Enter second matrix:
> 3 2 4
> 5 5 9
> 8 0 10
The result is:
94 37 137
80 42 118
30 18 44

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice: >

Example 4: the user chooses the addition of matrices; the operation cannot be performed and they quit the program

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice: > 1
Enter size of first matrix: > 2 2
Enter first matrix:
> 1 2
> 3 2
Enter size of second matrix: > 1 1
Enter second matrix:
> 1
The operation cannot be performed.

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
0. Exit
Your choice: > 0
Write a program
package processor;

public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

___

Create a free account to access the full topic