<jupyter_start><jupyter_text>Linear ALgebra for ECE Laboratory 4 : MatricesNow that you have fundamental knowledge about Python, we'll try to look into greater dimensions. ObjectivesAt the end of this activity you will be able to:1.   Be familiar with matrices and their relation to linear equations.2.   Perform basic matrix operations.3.    Program and translate matrix equations and operations using Python. Discussion<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import scipy.linalg as la
%matplotlib inline<jupyter_output><empty_output><jupyter_text>$$A = \left\{    \begin{array}\        x_1 + y \\         4x - 10y    \end{array}\right. \\B = \left\{    \begin{array}\        x+y+z \\         3x -2y -z \\        -x + 4y +2z    \end{array}\right. $$ $$A=\begin{bmatrix}a_{(0,0)}&a_{(0,1)}&\dots&a_{(0,j-1)}\\a_{(1,0)}&a_{(1,1)}&\dots&a_{(1,j-1)}\\\vdots&\vdots&\ddots&\vdots&\\a_{(i-1,0)}&a_{(i-1,1)}&\dots&a_{(i-1,j-1)}\end{bmatrix}$$<jupyter_code>def describe_mat(matrix):
    print(f'Matrix:\n{matrix}\n\nShape:\t{matrix.shape}\nRank:\t{matrix.ndim}\n')
A = np.array([
      [1, 2],
      [3, 10]        
])
describe_mat(A)
B = np.array([[
      [8, 2],
      [5, 4],
      [7, 7]        
]])
describe_mat(B)
def describe_mat(matrix):
    is_square = True if matrix.shape[0] == matrix.shape[1] else False
    print(f'Matrix:\n{matrix}\n\nShape:\t{matrix.shape}\nRank:\t{matrix.ndim}\nIs Square:{is_square}\n')
square_mat = np.array([
    [1,2,5],
    [3,3,8],
    [6,1,2]
])

non_square_mat = np.array([
     [1,2,5],
     [3,3,8]                       
])
describe_mat(square_mat)
describe_mat(non_square_mat)<jupyter_output>Matrix:
[[1 2 5]
 [3 3 8]
 [6 1 2]]

Shape:	(3, 3)
Rank:	2
Is Square:True

Matrix:
[[1 2 5]
 [3 3 8]]

Shape:	(2, 3)
Rank:	2
Is Square:False<jupyter_text>Null Matrix<jupyter_code>def describe_mat(matrix):
  if matrix.size < 0:
    is_square = True if matrix.shape[0] == matrix.shape[1] else False
    print(f'Matrix:\n{matrix}\n\nShape:\t{matrix.shape}\nRank:\t{matrix.ndim}\nIs Square:{is_square}\n')
  else:
    print('Matrix is Null')
null_mat = np.array([])
describe_mat(null_mat)<jupyter_output>Matrix is Null<jupyter_text>Zero Matrix<jupyter_code>zero_mat_row = np.zeros((1,2))
zero_mat_sqr = np.zeros((2,2))
zero_mat_rct = np.zeros((3,2))

print(f'zero Row Matrix: \n{zero_mat_row}')
print(f'zero Row Matrix: \n{zero_mat_sqr}')
print(f'zero Row Matrix: \n{zero_mat_rct}')<jupyter_output>zero Row Matrix: 
[[0. 0.]]
zero Row Matrix: 
[[0. 0.]
 [0. 0.]]
zero Row Matrix: 
[[0. 0.]
 [0. 0.]
 [0. 0.]]<jupyter_text>Ones Matrix<jupyter_code>zero_mat_row = np.ones((1,2))
zero_mat_sqr = np.ones((2,2))
zero_mat_rct = np.ones((3,2))

print(f'zero Row Matrix: \n{zero_mat_row}')
print(f'zero Row Matrix: \n{zero_mat_sqr}')
print(f'zero Row Matrix: \n{zero_mat_rct}')<jupyter_output>zero Row Matrix: 
[[1. 1.]]
zero Row Matrix: 
[[1. 1.]
 [1. 1.]]
zero Row Matrix: 
[[1. 1.]
 [1. 1.]
 [1. 1.]]<jupyter_text>Diagonal Matrix<jupyter_code>np.array([
      [2,0,0],
      [0,3,0],
      [0,0,5]
])<jupyter_output><empty_output><jupyter_text>Identity Matrix<jupyter_code>np.eye(5)
np.identity(5)<jupyter_output><empty_output><jupyter_text>Upper Triangular Matrix<jupyter_code>np.array([
      [2,1,3],
      [0,3,1],
      [0,0,5]
])<jupyter_output><empty_output><jupyter_text>Lower Triangular Matrix<jupyter_code>np.array([
      [5,0,0],
      [1,3,0],
      [1,2,3]
])<jupyter_output><empty_output><jupyter_text>Practice<jupyter_code>np.array([
    [1, 4, -7]
])<jupyter_output><empty_output><jupyter_text>$$*   List item*   List itemG = \left\{    \begin{array}\        x_1 + 7x_2 + 8x_3\\         2x_1 + 2x_2 + 2x_3\\        4x_1 + 6x_2 + 7_3    \end{array}\right.$$ G = \begin{bmatrix} 1 & 7 & 8 \\ 2 & 2 & 2 \\ 4 & 6 & 7\end{bmatrix} Matrix Algebra<jupyter_code>A = np.array([
    [1,2],
    [2,3],
    [4,1]
])

B = np.array([
    [2,2],
    [0,0],
    [1,1]          
])

A+B
2+A 
2*np.ones(A.shape)+A
2*np.ones(A.shape)+A == 2+A 
3-B<jupyter_output><empty_output><jupyter_text>Element Wise Multiplacation<jupyter_code>A*B
2*A
A@B
A/B
alpha = 10**-10
A/(alpha+B)
np.add(A,B)<jupyter_output><empty_output><jupyter_text>Activity Task 1Create a function named `desc_mat()` that througouhly describes a matrix, it should: 1. Displays the shape, size, and rank of the matrix. 2. Displays whether the matrix is square or non-square. 3. Displays whether the matrix is an empty matrix. 4. Displays if the matrix is an identity, ones, or zeros matrix    Use 5 sample matrices in which their shapes are not lower than $(3,3)$.In your methodology, create a flowchart discuss the functions and methods you have done. Present your results in the results section showing the description of each matrix you have declared. Function Area<jupyter_code>def desc_mat(matrix):
    print(f'Matrix:\n{matrix}\n\nShape:\t{matrix.shape}\nRank:\t{matrix.ndim}\n')<jupyter_output><empty_output><jupyter_text>Matrix Declaration<jupyter_code>## Null Matrix
null_mat = np.array([])

## Zero Matrix
zero_mat_sqr = np.zeros((3,3))
## Ones Matrix
ones_mat_sqr = np.ones((3,3))
## Diagonal Matrix
A = np.array([
    [1, 0, 0],
    [0, 8, 0],
    [0, 0, 4]
])
## Identity Matrix
# np.identity(3)

## Upper Triangular Matrix
B = np.array([
    [1, 2, 3],
    [0, 6, 4],
    [0, 0, 6]
])
#Lower Triangular Matrix
C = np.array([
    [1, 0, 0],
    [8, 6, 0],
    [4, 5, 6]
])<jupyter_output><empty_output><jupyter_text>Test Areas<jupyter_code>## Null Matrix
desc_mat(null_mat)
## Zero Matrix
desc_mat(zero_mat_sqr)

## Ones Matrix
desc_mat(ones_mat_sqr)
## Diagonal Matrix
desc_mat(A)
## Identity Matrix
np.identity(3)
## Upper Triangular Matrix
desc_mat(B)
#Lower Triangular Matrix
desc_mat(C)<jupyter_output>Matrix:
[[1 0 0]
 [8 6 0]
 [4 5 6]]

Shape:	(3, 3)
Rank:	2<jupyter_text>Task 2Create a function named `mat_operations()` that takes in two matrices a input parameters it should: 1. Determines if the matrices are viable for operation and returns your own error message if they are not viable. 2. Returns the sum of the matrices. 3. Returns the differen of the matrices. 4. Returns the element-wise multiplication of the matrices. 5. Returns the element-wise division of the matrices.Use 5 sample matrices in which their shapes are not lower than $(3,3)$.In your methodology, create a flowchart discuss the functions and methods you have done. Present your results in the results section showing the description of each matrix you have declared.Task 2 Function Area<jupyter_code>def mat_operations(matrix):
    print(f'Matrix:\n{matrix}\n\nShape:\t{matrix.shape}\nRank:\t{matrix.ndim}\n')<jupyter_output><empty_output><jupyter_text>Matrix Declaration<jupyter_code>## Addition
A = np.array([
    [1,3,6],
    [1,2,8],
    [3,2,9]
])
B = np.array([
    [1,3,4],
    [3,1,2],
    [6,9,8]
])
## Subtraction
D = np.array([
    [4,2,3],
    [1,8,6],
    [9,6,0]
])
E = np.array([
    [1,4,5],
    [4,2,6],
    [1,9,8]
])
## element-wise multiplication
F = np.array([
    [3,1,2],
    [2,4,5],
    [6,9,8]
])
G = np.array([
    [2,3,5],
    [1,4,9],
    [0,3,7]
])
## element-wise division
H = np.array([
    [11,12,15],
    [13,19,8],
    [22,18,16]
])
I = np.array([
    [1,6,5],
    [3,3,3],
    [7,10,2]
])<jupyter_output><empty_output><jupyter_text>Test Areas<jupyter_code>## Addition
C = A+B
mat_operations(C)
np.add(A,B)
## Subtraction
S = D-E
mat_operations(S)
np.subtract(D,E)
## element-wise multiplication
M = F*G
mat_operations(M)
np.multiply(F,G)
4*G
## element-wise division
V = H/I
mat_operations(V)
np.divide(H,I)
np.divide(H,I)
H/5
1/I<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1721915285451197, 'fraction_numerical': 0.04538279400157853, 'mean_word_length': 3.995400788436268, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '317330', 'license': 'Apache-2.0', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3099, 'n_words': 714, 'path': 'Assignment4.ipynb', 'repo_name': 'jbvrstg/linealgebra', 'size': '7602'}