You are on page 1of 4

06/11/2023, 16:17 Assignment8.

ipynb - Colaboratory

Q1 Consider the following two vectors


u = (0.5, 0.4, 0.4, 0.5, 0.1, 0.4, 0.1) and v = (-1,-2, 1,-2, 3, 1,-5).

1. Check if u or v is a unit vector.


2. Calculate the dot product, <u, v>
3. Are u and v orthogonal ?

import numpy as np
import cmath
U = np.array([0.5, 0.4, 0.4, 0.5, 0.1, 0.4, 0.1])
V= np.array([-1,-2, 1,-2, 3, 1,-5])
Umag = np.linalg.norm(U)
Vmag = np.linalg.norm(V)
print('Magnitude of U', Umag)
print('Magnitude of V', Vmag)

Magnitude of U 1.0
Magnitude of V 6.708203932499369

# Calculate the dot product


dot_product = np.dot(U,V)

# Print the result


print("Dot Product:", dot_product)

Dot Product: -1.6999999999999997

Q.2. Consider the following three vectors

v = (1, 2, 5, 2,-3, 1, 2, 6, 2)
u = (-4, 3, -2, 2, 1, -3, 4, 1, -2)
w = (3, 3, 3, -1, 6,-1, 2,-5,-7)

1. Evaluate dot product <v,w>


2. Are any pair of vectors orthogonal, and if so which ones?

import numpy as np
import cmath
U= np.array([-4, 3, -2, 2, 1, -3, 4, 1, -2])
V = np.array([1, 2, 5, 2,-3, 1, 2, 6, 2])
W= np.array([3, 3, 3, -1, 6,-1, 2,-5,-7])
Umag = np.linalg.norm(U)
Vmag = np.linalg.norm(V)
# Calculate the dot product
dot_product1 = np.dot(V,W)
dot_product2 = np.dot(U,V)
dot_product3 = np.dot(W,U)
# Print the result
print("Dot Product of V and W:" dot product1)
https://colab.research.google.com/drive/1DKH4FlQCWyxGAm7HRIXi-7oAmmaXUteO?auth… 1/4
06/11/2023, 16:17 Assignment8.ipynb - Colaboratory
print( Dot Product of V and W: , dot_product1)
print("Dot Product of U and V:", dot_product2)
print("Dot Product of W and U:", dot_product3)

if(dot_product1==0):
print("V and W are orthogonal vectors")
if(dot_product2==0):
print("U and V are orthogonal vectors")
if(dot_product3==0):
print("w and u are orthogonal vectors")

Dot Product of V and W: -37


Dot Product of U and V: 0
Dot Product of W and U: 15
U and V are orthogonal vectors

## Q2. Consider the following three matric


Q2. Consider the following three
Evaluate the following
1. A T B matrices A,B and C
2. C + B
3. Which matrices are full rank? Evaluate the following
4. B -1
1. A T B
2. C + B
3. Which matrices are full rank?
4. B -1

Code Text

import numpy as np
import cmath

# Define matrices A, B, and C with your specific values


A = np.array([[2,-2],
[-3,1],
[5,-3]])

B = np.array([[4,4,4],
[-2,3,-7],
[2,5,-7]])

C = np.array([[4,-1,2],
[-8,2,-4],
[2,1,-4]])

# i. Evaluate ATB
result1 = np.dot(np.transpose(A), B)
print("Transpose of A ")
https://colab.research.google.com/drive/1DKH4FlQCWyxGAm7HRIXi-7oAmmaXUteO?auth… 2/4
06/11/2023, 16:17 Assignment8.ipynb - Colaboratory
print( Transpose of A )
print(np.transpose(A))
print()
# ii. Evaluate C + B
result2 = C + B

# iii. Check for full rank matrices


def is_full_rank(matrix):
return np.linalg.matrix_rank(matrix) == min(matrix.shape)

full_rank_A = is_full_rank(A)
full_rank_B = is_full_rank(B)
full_rank_C = is_full_rank(C)

# iv. Calculate the inverse of B if it's full rank


B_inv = np.linalg.inv(B)

# Print the results


print("ATB:")
print(result1)
print()
print("C + B:")
print(result2)
print()
print("Full rank matrices:")
print("Matrix A is full rank:", full_rank_A)
print("Matrix B is full rank:", full_rank_B)
print("Matrix C is full rank:", full_rank_C)
print()
print("Inverse of B:")
print(B_inv)

Transpose of A
[[ 2 -3 5]
[-2 1 -3]]

ATB:
[[ 24 24 -6]
[-16 -20 6]]

C + B:
[[ 8 3 6]
[-10 5 -11]
[ 4 6 -11]]

Full rank matrices:


Matrix A is full rank: True
Matrix B is full rank: True
Matrix C is full rank: False

Inverse of B:
[[-0.11666667 -0.4 0.33333333]
[ 0.23333333 0.3 -0.16666667]
[ 0.13333333 0.1 -0.16666667]]

https://colab.research.google.com/drive/1DKH4FlQCWyxGAm7HRIXi-7oAmmaXUteO?auth… 3/4
06/11/2023, 16:17 Assignment8.ipynb - Colaboratory

https://colab.research.google.com/drive/1DKH4FlQCWyxGAm7HRIXi-7oAmmaXUteO?auth… 4/4

You might also like