Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a python code to perform the following tasks: 1. create a numpy matrix a o

ID: 3742254 • Letter: W

Question

Write a python code to perform the following tasks:

1. create a numpy matrix a of dimension 5 x 5 with diagnol entries from 0 to 4 using a single line of python

2. multiply (matrix-vector operation) this matric by a numpy array b with entries b[j] = (2^j)+j, j = 0,....,4, such that c = a * b

3. implement the same multiplication with a for loop and check your previous result

4. multiply elementwise the resulting array c by the numpy array d with components d[j] = j + [j*3.1654], j = 0,...,4. Use typecast to implement the operation [ . ]. This is the floor operation which return the integer part of any floating point number.

5. Sum all the components of the resulting array except the last one, using a single line of python code

Explanation / Answer

1. code to create a numpy matrix a of dimension 5 x 5 with diagnol entries from 0 to 4 using a single line of python

a = np.diag([1,2,3,4,5])

2. multiply (matrix-vector operation) this matric by a numpy array b with entries b[j] = (2^j)+j, j = 0,....,4, such that c = a * b

import numpy as np

a = np.diag([0,1,2,3,4])

b = np.array([2,4,2,4,10])

print a * b

c. implement the same multiplication with a for loop and check your previous result

X = [[0,0,0,0,0],
    [0 ,1,0,0,0],
    [0 ,0,2,0,0],
    [0,0,0,3,0],
    [0,0,0,0,4]]

Y = [[2,0,0,0,0],
    [0,4,0,0,0],
    [0,0,2,0,0],
    [0,0,0,4,0],
    [0,0,0,0,10]]
  
result = [[0,0,0,0,0],
         [0,0,0,0,0],
         [0,0,0,0,0],
         [0,0,0,0,0],
         [0,0,0,0,0]]

# iterate through rows of X
for i in range(len(X)):
   # iterate through columns of Y
   for j in range(len(Y[0])):
       # iterate through rows of Y
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]

for r in result:
   print(r)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote