# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/KielKielKielKiel/LinearAlgebra_2ndSem/blob/main/Assignment10.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="c7JgmgJR8mVB"
#

# + [markdown] id="3ALQ2PJiT1gV"
# ## Laboratory 10 : Linear Combination and Vector Spaces

# + id="n648vCMcTzFH"
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline

# + [markdown] id="prbnOyGQT5sl"
# ## Linear Combination

# + [markdown] id="aICO-RIqT81b"
# It is said that a linear combination is the combination of linear scaling and addition of a vector its bases/components

# + [markdown] id="f4_09ihcT_MD"
# We will try to visualize the vectors and their linear combinations by plotting a sample of real number values for the scalars for the vectors. Let's first try the vectors below:

# + [markdown] id="DIOYegLaUAxN"
# $$X = \begin{bmatrix} 2\\5 \\\end{bmatrix} , Y = \begin{bmatrix} 7\\9 \\\end{bmatrix} $$

# + id="QhsnzsioT8Y3"
vectX =  np.array([2,5])
vectY =  np.array([7,9])

# + [markdown] id="kxhFct-nUgFq"
# Span of single vectors

# + [markdown] id="qMXFGaIXUgAI"
# As discussed in the lecture, the span of individual vectors can be represented by a line span. Let's take vector $X$ as an example.

# + id="Dyw8XwsrUkR7"
c = np.arange(-10,10,0.125)

plt.scatter(c*vectX[0],c*vectX[1])

plt.xlim(-5,15)
plt.ylim(5,-15)
plt.axhline(y=0, color='k')
plt.axvline(x=0, color='k')
plt.grid()
plt.show()

# + id="Cqv6wmFSUlly"
c = np.arange(-10,10,0.125)

plt.scatter(c*vectX[0],c*vectX[1])

plt.xlim(-5,15)
plt.ylim(5,-15)
plt.axhline(y=0, color='k')
plt.axvline(x=0, color='k')
plt.grid()
plt.show()

# + [markdown] id="5_g8ZEa7UpfO"
# #Span of a linear combination of vectors
# So what if we are to plot the span of a linear combination of vectors? We can visualize as a plane on the 2-dimensional coordinate system. Let's take the span of the linear combination below:

# + [markdown] id="saTQ2YxnUsqh"
# $$S = \begin{Bmatrix} c_1 \cdot\begin{bmatrix} 1\\0 \\\end{bmatrix}, 
# c_2 \cdot \begin{bmatrix} 1\\-1 \\\end{bmatrix}\end{Bmatrix} $$

# + id="l0QpNPATUnFL"
vectA = np.array([1,0])
vectB = np.array([1,-1])

R = np.arange(-10,10,1)

c1, c2 = np.meshgrid(R,R)

vectR = vectA + vectB
spanRx = c1*vectA[0] + c2*vectB[0]
spanRy = c1*vectA[1] + c2*vectB[1]
##plt.scatter(R*vectA[0],R*vectA[1])
##plt.scatter(R*vectB[0],R*vectB[1])
plt.scatter(spanRx,spanRy, s=5, alpha=0.75)

plt.axhline(y=0, color='k')
plt.axvline(x=0, color='k')
plt.grid()
plt.show()

# + id="KqvxSIgUVAVk" colab={"base_uri": "https://localhost:8080/", "height": 265} outputId="a7e6e01b-f3dd-416d-a53f-818ea8afa66e"
vectP = np.array([2,1])
vectQ = np.array([4,3])

R = np.arange(-10,10,1)

c1, c2 = np.meshgrid(R,R)

vectR = vectP + vectQ
spanRx = c1*vectP[0] + c2*vectQ[0]
spanRy = c1*vectP[1] + c2*vectQ[1]
##plt.scatter(R*vectA[0],R*vectA[1])
##plt.scatter(R*vectB[0],R*vectB[1])
plt.scatter(spanRx,spanRy, s=5, alpha=0.75)

plt.axhline(y=0, color='k')
plt.axvline(x=0, color='k')
plt.grid()
plt.show()

# + [markdown] id="GhK16RtJVEpd"
# Take note that if vectors are seen to be as a 2-dimensional span we can say it has a Rank of 2 or  R2 . But if the span of the linear combination of vectors are seen to be like a line, they are said to be linearly dependent and they have a rank of 1 or  R1 .
#
# #Activity
# ##Task 1
# Try different linear combinations using different scalar values. In your methodology discuss the different functions that you have used, the linear equation and vector form of the linear combination, and the flowchart for declaring and displaying linear combinations. Please make sure that your flowchart has only few words and not putting the entire code as it is bad practice. In your results, display and discuss the linear combination visualization you made. You should use the cells below for displaying the equation markdows using LaTeX and your code.

# + [markdown] id="rny8z1QDj6Q7"
# $$ Vector1 = -10\hat{i} + 2\hat{j}  \\
# Vector 2 = 13\hat{x} - 8\hat{k}\\ $$
#
#
#
#

# + [markdown] id="RE20rAi0j-_Q"
# $$Span = \begin{Bmatrix} Vector1 \cdot\begin{bmatrix} -10\\2 \\\end{bmatrix}, 
# vector2 \cdot \begin{bmatrix} 13\\-8 \\\end{bmatrix}\end{Bmatrix} $$

# + id="ok9NGzrFVK3n" colab={"base_uri": "https://localhost:8080/", "height": 265} outputId="9f60a41e-cbdb-4b6a-9379-68fef4db2ff1"
vector1 = np.array([-10,2])
vector2 = np.array([13,-8])

R = np.arange(-10,10,1)

c1, c2 = np.meshgrid(R,R)

vectR = vectP + vectQ
spanRx = c1*vectP[0] + c2*vectQ[0]
spanRy = c1*vectP[1] + c2*vectQ[1]
##plt.scatter(R*vectA[0],R*vectA[1])
##plt.scatter(R*vectB[0],R*vectB[1])
plt.scatter(spanRx,spanRy, s=5, alpha=0.75)

plt.axhline(y=0, color='red')
plt.axvline(x=0, color='red')
plt.grid()
plt.show()

# + [markdown] id="XqPHCi4gWyb8"
# $$ X = -5\hat{i} - 7\hat{j}  \\
# Y = 3\hat{x} - 4\hat{k}\\ $$

# + [markdown] id="JEddNRAWW5G2"
# $$X = c\cdot \begin{bmatrix} 14\\6 \\\end{bmatrix} $$
# $$Y = c\cdot \begin{bmatrix} 22\\13 \\\end{bmatrix} $$

# + colab={"base_uri": "https://localhost:8080/", "height": 269} id="wHsgiP9oXd7T" outputId="0ddb179e-23a8-4a2a-b444-412cb7b8ef10"
vectX =  np.array([14,6])
vectY =  np.array([22,13])

c = np.arange(-25,25,0.335)

plt.scatter(c*vectX[0],c*vectX[1])


plt.xlim(-30,30)
plt.ylim(-30,30)
plt.axhline(y=0, color='blue')
plt.axvline(x=0, color='blue')
plt.grid()
plt.show()

StarcoderdataJupyterScriptsDedupFiltered