<jupyter_start><jupyter_text>Linear Algebra for ECE Laboratory 4 : Matrices![image](https://drive.google.com/uc?export=view&id=1xJLUKtpiXA1nezyfhoSLOtdcqgZn4JnX)$$[1]$$ Now that you have a 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.  Methods![image](https://drive.google.com/uc?export=view&id=1pjAZ9KYD4b6KjWqH_JJ1GPZPJ6WPs5Gz)$$Task 1 Flowchart$$ ![image](https://drive.google.com/uc?export=view&id=15feMktAJLl4DKVMo6Io9V42M_9uYUtBD)![image](https://drive.google.com/uc?export=view&id=1plBqZkKflEjDwNcEhOGKOkvai2Ktw5Hn)  Introduction to Matrices > A matrix is a two-dimensional array of numbers with rows and columns arranged in rows and columns. Matrices are a type of matrix that can be used to organize, store, and manipulate mathematical data. In the actual world, matrices have a wide range of applications and uses. Matrices are useful when working with models that are based on systems of linear equations [1].>> For example, matrix *A* has two rows and three columns. $$A=\begin{bmatrix} -2 & 5 & 6\\ 5 & 2 & 7\end{bmatrix} \\$$ $$B=\begin{bmatrix} -8 & -4 \\ 23 & 12\\ 18 & 10\end{bmatrix}\\$$ > * In contrast, matrix *B* has three rows and two columns, so it is a 3 x 2 matrix. \Keep in mind that: Rows x Columns!  Matrix Dimensions > Khan Academy provided a general introduction into matrices so we can further understand its concept. The dimensions of a matrix tells its size: the number of rows and columns of the matrix, in that order. Since matrix *A* has two rows and three columns, we write its dimensions as 2 x 3, pronounced "two by three" [2].$$A=\begin{bmatrix} -2 & 5 & 6\\ 5 & 2 & 7\end{bmatrix} \\$$  Representing a linear system with matrices >  Matrices can be used to solve systems of equations. However, we must learn how to represent systems with matrices [3]. First and foremost, the equation must be arranged properly in the standard form. Then we take its constants and put it into its matrix form from left to right (including the constant on the right side) Let's say for example you have $A$ and $B$ as system of equation. $$A = \left\{    \begin{array}\        -2x + 5y + 6z \\         5x + 2y +7z    \end{array}\\right. \\$$ $$B = \left\{    \begin{array}\        -8x - 4y\\         23x + 12y \\        18x + 10y     \end{array}\\right. \\$$ We could see that $A$ is a system of 2 equations with 3 parameters. While $B$ is a system of 3 equations with 2 parameters. We can represent them as matrices by: $$A=\begin{bmatrix} -2 & 5 & 6\\ 5 & 2 & 7\end{bmatrix} \\$$ $$B=\begin{bmatrix} -8 & -4 \\ 23 & 12\\ 18 & 10\end{bmatrix}\\$$  Declaring Matrices > The entities or numbers in matrices are called the elements of a matrix. These elements are arranged and ordered in rows and columns which form the list/array-like structure of matrices. And just like arrays, these elements are indexed according to their position with respect to their rows and columns. This can be reprsented just like the equation below. Whereas  A  is a matrix consisting of elements denoted by  ai,j . Denoted by  i  is the number of rows in the matrix while  j  stands for the number of columns.Do note that the  size  of a matrix is  i×j .$$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}$$ > There are several ways of classifying matrices. Once could be according to their shape and another is according to their element values. We'll try to go through them.  Activity  Task 1 Create 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 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.<jupyter_code>## Function area
import numpy as np
import matplotlib.pyplot as plt
import scipy.linalg as la
%matplotlib inline
def desc_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')
## Matrix declarations
T= np.array([
    [1,2,3],
    [4,5,5],
    [8,7,9]
])
E= np.array([
    [8,9,7],
    [6,5,4],
    [1,2,3]
])

A = np.array([])

S = np.eye(69)

U = np.ones((6,9))

R = np.zeros((3,3))
## Test Areas
desc_mat(T)
desc_mat(E)
desc_mat(A)
desc_mat(S)
desc_mat(U)
desc_mat(R)<jupyter_output>Matrix:
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

Shape:	(3, 3)
Rank:	2
Is Square: True<jupyter_text>Task 2 Create 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 difference of the matrices. 4. Returns the element-wise multiplication of the matrices. 5. Returns the element-wise division of the matrices.Use at least 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.<jupyter_code>## Function area
import numpy as np
import matplotlib.pyplot as plt
import scipy.linalg as la
%matplotlib inline
def mat_operations(np,matA,matB):
  if M1.size > 0 or M2.size > 0:
    if M1.shape == M2.shape:
      if np == 'Add':
        sum = M1 + M2
        print(f'\nThe Sum of \n{M1} \n\n+\n\n {M2} \n\n is equal to:\n\n {sum}')
      if np == 'Subtract':
        sub = M1 - M2
        print(f'\nThe Difference of \n{M1} \n\n-\n\n {M2} \n\n is equal to:\n\n {sub}')
      if np == 'Multiply':
        mul = M1 * M2
        print(f'\nThe Product of \n{M1} \n\nx\n\n {M2} \n\n is equal to:\n\n {mul}')
      if np == 'Divide':
        div = M1 / M2
        print(f'\nThe Result of \n{M1} \n\n/\n\n {M2} \n\n is equal to:\n\n {div}')
      else:
        if np == '0':
          print(f'Operation Not Found!')
    else:
      print(f'Syntax Error! Invalid Matrices detected.')
  else:
    print(f'One or Both of the Matrices are NULL')
## Matrix declarations
M1= np.array([
    [1,2,3],
    [4,5,5],
    [8,7,9]
])
M2= np.array([
    [8,9,7],
    [6,5,4],
    [1,2,3]
])
## Test Areas
mat_operations('Add',M1,M2)
mat_operations('Subtract',M1,M2)
mat_operations('Multiply',M1,M2)
mat_operations('Divide',M1,M2)<jupyter_output>The Result of 
[[1 2 3]
 [4 5 5]
 [8 7 9]] 

/

 [[8 9 7]
 [6 5 4]
 [1 2 3]] 

 is equal to:

 [[0.125      0.22222222 0.42857143]
 [0.66666667 1.         1.25      ]
 [8.         3.5        3.        ]]
{'fraction_non_alphanumeric': 0.12241261137765592, 'fraction_numerical': 0.03728581220013708, 'mean_word_length': 3.4084592145015105, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '498017', 'license': 'Apache-2.0', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2368, 'n_words': 961, 'path': 'Assignment4_NUISA.ipynb', 'repo_name': 'acsnuisa/LinAlg-2021', 'size': '7295'}