<jupyter_start><jupyter_text>Linear Algebra for CpE Laboratory 4 : Vector Operations Now that you have a fundamental knowledge about linear combination, we'll try to apply the fundamental operations  ObjectivesAt the end of this activity you will be able to:1. Referesh knowledge on vector operations while being familiar with new operations such as products.2. Visualize vector operations.3. Perform vector operations using Python.  Discussion<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>We have dealt with some of the vector operations in the last module, now we will dwell into more operations. In this laboratory, we will tackle addition,multiplication,division and the inner product of a vector.  Vector Addition We have encountered vector before especially with your last activity. Vector addition is simply the element-wise addition of the scalar values of the vectors. Let's take the following vectors as a sample:$$A = \begin{bmatrix}1\\2\\0\end{bmatrix}, B= \begin{bmatrix}3\\1\\-2 \end{bmatrix}$$So if do a vector addition of these two vectors we'll get:$$A + B = \begin{bmatrix}4\\3\\-2\end{bmatrix}$$We can progammatically solve this using `np.add()` or simply using `+`.<jupyter_code>A = np.array([1,2,0])
B = np.array([3,1,-2])
A+B
np.add(A,B)<jupyter_output><empty_output><jupyter_text>Vector Subtraction Vector subtraction is similar to your vector addition but you would need to scale the second vector using a negative scalar that is usuall $-1$. So if we subtract vector $B$ from vector $A$ we get:$$A-B = \begin{bmatrix}-2\\1\\2\end{bmatrix}$$In Python, this can be achieved by using `np.subtract()` or `-`<jupyter_code>np.subtract(A,B)
A-B<jupyter_output><empty_output><jupyter_text>Vector Multiplication Vector multiplication, like addition does its operations element-wise. So basic vector multiplication can be achieved by multiplying the elements or the scalars of the vectors individually. So:$$A * B = \begin{bmatrix}3\\2\\0\end{bmatrix}$$We can implement this in code by using `np.multiply()` or simply operating with `*`.<jupyter_code>np.multiply(A,B)
A*B<jupyter_output><empty_output><jupyter_text>Vector Division Dividing a vector by another is uncommon, but if the situation specifies that each elements or scalars of the vector would be divided individually we can perform this by:$$A./B = \begin{bmatrix}\frac{1}{3}\\2\\0\end{bmatrix}$$Take note that the notation we used here is $./$ for element-wise division, this notation is based in a MATLab notation for element-wise division. This can be achieved in Python using `np.divide()` or `/`.<jupyter_code>np.divide(A,B)
A/B<jupyter_output><empty_output><jupyter_text>Modulus of a Vector The modulus of a vector or the magnitude of a vector can be determined using the Pythagorean theorem. Given the vector $A$ and its scalars denoted as $a_n$ where $n$ is the index of the scalar. So if we have:$$A = \begin{bmatrix}1\\2\end{bmatrix}$$We can compute the magnitude as:$$||A|| = \sqrt{a_1^2 + a_2^2} = \sqrt{1^2 + 2^2} = \sqrt{5}$$So if we have a matrix with more parameters such as:$$B=\begin{bmatrix}2\\5\\-1\\0\end{bmatrix}$$We can generalize the Pythagorean theorem to compute for the magnitude as:$$||B|| = \sqrt{b_1^2 + b_2^2 + b_3^2 + ... +b_n^2} = \sqrt{\sum_{n=1}^{N}b_n^2}$$And this equation is now called a Euclidian distance or the Euclidean Norm. We can implement this explicitly by for loops or using `np.linalg.norm()` to get the Euclidian Norm.<jupyter_code>A = np.array([1,2])
B = np.array([2,5,-1,0])
np.linalg.norm(A)
np.linalg.norm(B)<jupyter_output><empty_output><jupyter_text>Vector Dot Product / Inner Product The inner product of a vector is the sum of the products of each elements of the vectors. So given vectors $H$ and $G$ below:$$H=\begin{bmatrix}1\\3\\6\end{bmatrix}, G = \begin{bmatrix}5\\2\\1\end{bmatrix}$$We first take the element-wise product of the vectors:$$H*G = \begin{bmatrix}5\\6\\6\end{bmatrix}$$Then we take the sum of the products, making it the inner product of a vector:$$H\cdot G = 17$$You can solve for the inner product using an explicit function, `np.inner()` or the `@` operator.<jupyter_code>H = np.array([1,3,6])
G = np.array([5,2,1])
np.inner(H,G)
H @ G<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11805716941668604, 'fraction_numerical': 0.017894492214733906, 'mean_word_length': 5.192805755395684, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '68822', 'license': 'Apache-2.0', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1324, 'n_words': 514, 'path': 'Lab 04 - Vector Products/LinAlg Lab 4.ipynb', 'repo_name': 'krissydolor/Linear-Algebra', 'size': '4303'}