#C2329. Basic Matrix Operations for 2x2 Matrices
Basic Matrix Operations for 2x2 Matrices
Basic Matrix Operations for 2x2 Matrices
You are required to implement a Matrix class that performs basic operations on 2x2 matrices. The supported operations are:
-
Addition: Given two 2x2 matrices (A = \begin{pmatrix} a_{11} & a_{12}\ a_{21} & a_{22} \end{pmatrix}) and (B = \begin{pmatrix} b_{11} & b_{12}\ b_{21} & b_{22} \end{pmatrix}), compute ( A+B = \begin{pmatrix} a_{11}+b_{11} & a_{12}+b_{12}\ a_{21}+b_{21} & a_{22}+b_{22} \end{pmatrix}).
-
Subtraction: Compute (A-B) in a similar manner.
-
Multiplication: There are two cases:
- When multiplying two matrices, perform standard matrix multiplication: ( (A \times B){ij} = \sum{k=1}^{2} a_{ik} \times b_{kj}).
- When multiplying a matrix by a scalar (k): Multiply each element by (k).
-
Transpose: Compute the transpose (A^T) such that the element at row (i) and column (j) becomes the element at row (j) and column (i).
-
Determinant: For a 2x2 matrix (A = \begin{pmatrix} a & b\ c & d \end{pmatrix}), compute (\det(A) = ad - bc).
-
Equality: Check if two matrices are identical element-wise.
-
String Representation: Return the string representation in a Python list style, e.g. "[[1, 2], [3, 4]]".
Input/Output Requirement: All input is via standard input (stdin) and all output via standard output (stdout).
Input Format:
The first line contains an integer (T) representing the number of operations to perform. Then for each operation, the first line is a string indicating the operation type. The supported operation types are: add
, sub
, mul
, smul
, trans
, det
, eq
, and str
.
For operations add
, sub
, mul
, and eq
, the next 4 lines provide two matrices. Each matrix is given by two lines containing two space-separated integers.
For operations trans
, det
, and str
, the next 2 lines describe a single matrix.
For operation smul
(scalar multiplication), the next 2 lines describe a matrix, followed by one line containing the scalar.
Output Format: For each operation, output its result as follows:
- For matrix operations (
add
,sub
,mul
,smul
,trans
,str
): print the 2x2 matrix in two lines (each line with two space-separated integers or exactly as the string representation forstr
). - For
det
: print a single integer. - For
eq
: print eitherTrue
orFalse
.
Make sure your solution passes all test cases.
inputFormat
The first line contains an integer (T) representing the number of operations. Then, for each operation:
• The first line is a string denoting the operation type (one of: add, sub, mul, smul, trans, det, eq, str).
• For operations requiring two matrices (add, sub, mul, eq):
- The next two lines contain the first matrix (each line with 2 integers).
- The following two lines contain the second matrix (each line with 2 integers).
• For operations requiring one matrix (det, trans, str):
- The next two lines contain the matrix.
• For scalar multiplication (smul):
- The next two lines contain the matrix, followed by one line with a scalar integer.
outputFormat
For each operation, output the result as follows:
• For matrix results (add, sub, mul, smul, trans, str):
- If the operation is not
str
, print the matrix in 2 lines with each row's entries separated by a space. - For
str
, output the string representation, e.g. "[[1, 2], [3, 4]]".
• For det
: output a single integer representing the determinant.
• For eq
: output either "True" or "False".## sample
2
add
1 2
3 4
4 3
2 1
smul
1 2
3 4
2
5 5
5 5
2 4
6 8
</p>