<jupyter_start><jupyter_text>Lab 4 - Plotting Vector using NumPy and MatPlotLib In this laboratory we will be discussing the basics of numerical and scientific programming by working with Vectors using NumPy and MatPlotLib. ObjectivesAt the end of this activity you will be able to:1. Be familiar with the libraries in Python for numerical and scientific programming.2. Visualize vectors through Python programming.3. Perform simple vector operations through code.4. Illustrate the vector in a graph through Python programming.  Discussion  NumPy NumPy or Numerical Python, is mainly used for matrix and vector operations. It is capable of declaring computing and representing matrices. Most Python scientific programming libraries uses NumPy as the basic code.Scalars - Value the is only one dimensional \ represents only the magnitudeVectors - Valeus that is multidimensional \ represent magnitude with direction  Representing VectorsNow that you know how to represent vectors using their component and matrix form we can now hard-code them in Python. Let's say that you have the vectors: $$ BTS = 10\hat{x} + 78\hat{y} -41\hat{z}\\BP = 32\hat{x} - 25\hat{y}+22\hat{z}\\EXO = 5ax + 19ay - 3az + 13xy - 12xz\\ITZY = \hat{i} - 20\hat{j} + 8\hat{k}- 5\hat{l} + 14\hat{m}$$ In which it's matrix equivalent is:$$ BTS = \begin{bmatrix} 10 \\ 78\\-41\end{bmatrix} , BP = \begin{bmatrix} 32 \\ -25\\22\end{bmatrix} , EXO = \begin{bmatrix} 5\\ 19 \\  -3 \\13\\-12\end{bmatrix}, ITZY = \begin{bmatrix} 1 \\ -20 \\ 8\\-5\\14\end{bmatrix}$$$$ BTS = \begin{bmatrix} 10 & 78&-41\end{bmatrix} , BP = \begin{bmatrix} 32 & -25&22\end{bmatrix} , EXO = \begin{bmatrix} 5 & 19 & -3&13&-12\end{bmatrix} , ITZY = \begin{bmatrix} 1 & -20 & 8&-5&14\end{bmatrix} $$ We can then start doing numpy code with this by:<jupyter_code>## Importing necessary libraries
import numpy as np ## 'np' here is short-hand name of the library (numpy) or a nickname.
BTS = np.array([10, 78, -41])
BP = np.array([32, -25, 22])
EXO = np.array([
              [5],
              [19],
              [-3],
              [13],
              [-12]
])
ITZY = np.array ([[1],
              [-20],
              [8],
              [-5],
              [14]
              ])
print('Vector BTS is ', BTS, "\n")
print('Vector BP is ', BP, "\n")
print('Vector EXO is ', EXO, "\n")
print('Vector ITZY is ', ITZY, "\n")<jupyter_output>Vector BTS is  [ 10  78 -41] 

Vector BP is  [ 32 -25  22] 

Vector EXO is  [[  5]
 [ 19]
 [ -3]
 [ 13]
 [-12]] 

Vector ITZY is  [[  1]
 [-20]
 [  8]
 [ -5]
 [ 14]]<jupyter_text>Describing vectors in NumPyDescribing vectors is identifying its magnitude along with its direction. There are several ways to identify its direction either through graphical, resultant vector, or conventions. It is very important to describe the vector if we want to perform basic to advanced operations with them. The fundamental ways in describing vectors are knowing their shape, size and dimensions.<jupyter_code>### Checking shapes
### Shapes tells us how many elements are there on each row and column

BTS.shape
kpop = np.array([13, 0, 9, 25, -18.9, 2])
kpop.shape
EXO.shape
### Checking size
### Array/Vector sizes tells us many total number of elements are there in the vector

BP.size
### Checking dimensions
### The dimensions or rank of a vector tells us how many dimensions are there for the vector.

ITZY.ndim<jupyter_output><empty_output><jupyter_text>Great! Now let's try to explore in performing operations with these vectors. AdditionThe addition rule is simple, the we just need to add the elements of the matrices according to their index. So in this case if we add vector  and vector  we will have a resulting vector: $$ggs = 42\hat{x} +53\hat{y} -19\hat{z} \\ \\or \\ \\ ggs = \begin{bmatrix} 42 \\ 53\\-19\end{bmatrix} $$So let's try to do that in NumPy in several number of ways:<jupyter_code>ggs = np.add(BTS, BP) ## this is the functional method using the numpy library
snsd = np.add(EXO, ITZY)
ror = BTS + BP   ## this is the explicit method, since Python does a value-reference so it can 
                 ## know that these variables would need to do array operations.
print(ror, "\n")

plop = EXO + ITZY

print(plop)
from numpy.lib.index_tricks import AxisConcatenator
svt = np.array([12,8,4,2])
ace = np.array([9,6,3,1])
gf = np.array([10,9,8,7])
rv = np.array([-8,-3,-3,-8])

#ror = svt + ace + gf + rv
#ror = np.multiply(gf, rv)

ror = svt/ace
ror<jupyter_output><empty_output><jupyter_text>Try for yourself!Try to implement subtraction, multiplication, and division with vectors *BTS* and *BP*<jupyter_code>an = BTS - BP   

print("When vectors bts and bp are subtracted >>>", an, "\n")

ny = BTS*BP   

print("When vectors bts and bp are multiplied >>>", ny, "\n")

eo = BTS/BP  

print("When vectors bts and bp are divied >>>", eo, "\n")<jupyter_output>When vectors bts and bp are subtracted >>> [-22 103 -63] 

When vectors bts and bp are multiplied >>> [  320 -1950  -902] 

When vectors bts and bp are divied >>> [ 0.3125     -3.12       -1.86363636]<jupyter_text>ScalingScaling or scalar multiplication takes a scalar value and performs multiplication with a vector. Let's take the example below: $$ sassagirl= 2*BTS $$ We can do this in numpy through:<jupyter_code>#sasagirl = 2 * BTS
sassagirl = np.multiply(2,BTS)
sassagirl<jupyter_output><empty_output><jupyter_text>Try to implement scaling with two vectors.<jupyter_code>mj= np.array ([[1,7,3,94,0],
              [91,2,5,-10,8],
              [13,56,7,9,-41],
              [3,-4,8,9,13,],
              [3,30,1,11,25]])

jozel= np.array ([[0,7,54,-2,5],
                 [19,-7,18,5,2],
                 [25,9,54,4,0],
                 [-3,16,1,3,5],
                 [16,7,3,4,1]])

eugene= mj * 3
print("When mj matrix is scaled to 2, the answer would be represented as: \n \n eugene= \n",eugene)

print('\n _____________________________________________________________________________________________\n')

embalzado= jozel * 5
print("When jozel matrix  is scaled to 5, the answer would be represented as: \n \n embalzado= \n",embalzado)<jupyter_output>When mj matrix is scaled to 2, the answer would be represented as: 
 
 eugene= 
 [[   3   21    9  282    0]
 [ 273    6   15  -30   24]
 [  39  168   21   27 -123]
 [   9  -12   24   27   39]
 [   9   90    3   33   75]]

 _____________________________________________________________________________________________

When jozel matrix  is scaled to 5, the answer would be represented as: 
 
 embalzado= 
 [[  0  35 270 -10  25]
 [ 95 -35  90  25  10]
 [125  45 270  20   0]
 [-15  80   5  15  25]
 [ 80  35  15  20   5]]<jupyter_text>MatPlotLibMatPlotLib or MATLab Plotting library is Python's take on MATLabs plotting feature. It is a library cor creating interactive, static, and animated visualization in Python. In addition, MatPlotLib can be used vastly from graping values to visualizing several dimensions of data - it makes things easier and what seems impossible to be possible. Visualizing DataIt's not enough just solving these vectors so might need to visualize them. So we'll use MatPlotLib for that. We'll need to import it first.<jupyter_code>import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
A = [8, -2, 4]
B = [6, 6, 5]
plt.scatter(A[0], A[1], label='A', c='black')
plt.scatter(B[0], B[1], label='B', c='pink')
plt.grid()
plt.legend()
plt.show()
A = np.array([3, -6])
B = np.array([2, 4])
plt.xlim(-5, 10)
plt.ylim(-8, 8)
plt.quiver(0, 0, A[0], A[1], angles='xy', scale_units='xy', scale=1, color='purple')
plt.quiver(A[0], A[1], B[0], B[1], angles='xy', scale_units='xy', scale=1, color='yellow')
R = A + B
plt.quiver(0, 0, R[0], R[1], angles='xy', scale_units='xy', scale=1, color='black')
plt.grid()
plt.show()
print(R)
Magnitude = np.sqrt(np.sum(R**2))
plt.title("Resultant Vector\nMagnitude:{}".format(Magnitude))
print(Magnitude)
Slope = R[1]/R[0]
print(Slope)
Angle = (np.arctan(Slope))*(180/np.pi)
print(Angle)
n = A.shape[0]
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.quiver(0,0, A[0], A[1], angles='xy', scale_units='xy',scale=1)
plt.quiver(A[0],A[1], B[0], B[1], angles='xy', scale_units='xy',scale=1)
plt.quiver(0,0, R[0], R[1], angles='xy', scale_units='xy',scale=1)
plt.show()<jupyter_output><empty_output><jupyter_text>Try plotting Three Vectors and show the Resultant Vector as a result. Use Head to Tail Method.<jupyter_code>A = np.array([3, -4])
B = np.array([4, 8])
C = np.array([2, -2])
D = A + B
plt.xlim(0, 15)
plt.ylim(-8, 8)
plt.quiver(0, 0, A[0], A[1], angles='xy', scale_units='xy', scale=1, color='cyan')
plt.quiver(A[0], A[1], B[0], B[1], angles='xy', scale_units='xy', scale=1, color='orange')
plt.quiver(D[0], D[1], C[0], C[1], angles='xy', scale_units='xy', scale=1, color='red')
R = A + B + C
plt.quiver(0, 0, R[0], R[1], angles='xy', scale_units='xy', scale=1, color='black')
plt.grid()
plt.show()
print(R)
Magnitude = np.sqrt(np.sum(R**2))
plt.title("Resultant Vector\nMagnitude:{}".format(Magnitude))
print(Magnitude)
Slope = R[1]/R[0]
print(Slope)
Angle = (np.arctan(Slope))*(180/np.pi)
print(Angle)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14553836022390518, 'fraction_numerical': 0.05334211392821864, 'mean_word_length': 3.7041817243159527, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 48, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '323233', 'license': 'Apache-2.0', 'n_tokens_mistral': 3527, 'n_tokens_neox': 3149, 'n_words': 1072, 'path': 'Assignment_9_Arriola_Valenzuela.ipynb', 'repo_name': 'jooeeeiy/LinearAlgebra_2ndSem', 'size': '9111'}