1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
allsm [11]
2 years ago
5

Write a naive implementation (i.e. non-vectorized) of matrix multiplication, and then write an efficient implementation that uti

lizes Numpy's vectorization. When writing the function ensure that the matrix dimensions are correct (print the message Wrong dimensions! otherwise).

Engineering
1 answer:
erik [133]2 years ago
7 0

Answer:

import numpy as np  

import time  

def matrixMul(m1,m2):      

   if m1.shape[1] == m2.shape[0]:  

       

       t1 = time.time()

       r1 = np.zeros((m1.shape[0],m2.shape[1]))

       for i in range(m1.shape[0]):

           for j in range(m2.shape[1]):

               r1[i,j] = (m1[i]*m2.transpose()[j]).sum()

       t2 = time.time()

       print("Native implementation: ",r1)

       print("Time: ",t2-t1)

       

       t1 = time.time()

       r2 = m1.dot(m2)

       t2 = time.time()

       print("\nEfficient implementation: ",r2)

       print("Time: ",t2-t1)

       

   else:

       print("Wrong dimensions!")

Explanation:

We define a function (matrixMul) that receive two arrays representing the two matrices to be multiplied, then we verify is the dimensions are appropriated for matrix multiplication if so we proceed with the native implementation consisting of two for-loops and prints the result of the operation and the execution time, then we proceed with the efficient implementation using .dot method then we return the result with the operation time. As you can see from the image the execution time is appreciable just for large matrices, in such a case the execution time of the efficient implementation can be 1000 times faster than the native implementation.

You might be interested in
Before finishing and installing a shelved cabinet you just constructed, you need to check the
Greeley [361]

Answer:

Carpenter's square

Explanation:

The most common hand tool used to measure or set angles with its application extending to setting angles of roofs and rafters. Another name of a Carpenter's square is a framing square.

Other hand tools that are used to measure angles are;

  • The combination square that allows a user to set both 90°  and 45° angles
  • A Bevel that allows users to set any angle they like.
  • A Protractor that resembles a bevel but its marks are marked in an arc.
  • An electromagnetic angle finder which gives a reading according to the measure of the arms adjusted by the user.
7 0
2 years ago
A gas in a piston–cylinder assembly undergoes a compression process for which the relation between pressure and volume is given
viktelen [127]

Answer:

A.) P = 2bar, W = - 12kJ

B.) P = 0.8 bar, W = - 7.3 kJ

C.) P = 0.608 bar, W = - 6.4kJ

Explanation: Given that the relation between pressure and volume is

PV^n = constant.

That is, P1V1^n = P2V2^n

P1 = P2 × ( V2/V1 )^n

If the initial volume V1 = 0.1 m3,

the final volume V2 = 0.04 m3, and

the final pressure P2 = 2 bar. 

A.) When n = 0

Substitute all the parameters into the formula

(V2/V1)^0 = 1

Therefore, P2 = P1 = 2 bar

Work = ∫ PdV = constant × dV

Work = 2 × 10^5 × [ 0.04 - 0.1 ]

Work = 200000 × - 0.06

Work = - 12000J

Work = - 12 kJ

B.) When n = 1

P1 = 2 × (0.04/0.1)^1

P1 = 2 × 0.4 = 0.8 bar

Work = ∫ PdV = constant × ∫dV/V

Work = P1V1 × ln ( V2/V1 )

Work = 0.8 ×10^5 × 0.1 × ln 0.4

Work = - 7330.3J

Work = -7.33 kJ

C.) When n = 1.3

P1 = 2 × (0.04/0.1)^1.3

P1 = 0.6077 bar

Work = ∫ PdV

Work = (P2V2 - P1V1)/ ( 1 - 1.3 )

Work = (2×10^5×0.04) - (0.608 10^5×0.1)/ ( 1 - 1.3 )

Work = (8000 - 6080)/ -0.3

Work = -1920/0.3

Work = -6400 J

Work = -6.4 kJ

5 0
3 years ago
What would happen if an exposed film was accidentally placed in the fixer before being placed in the developer
Eduardwww [97]
Do you still want this answered
5 0
3 years ago
A car accelerates from rest with an acceleration of 5 m/s^2. The acceleration decreases linearly with time to zero in 15 s, afte
Tpy6a [65]

Answer: At time 18.33 seconds it will have moved 500 meters.

Explanation:

Since the acceleration of the car is a linear function of time it can be written as a function of time as

a(t)=5(1-\frac{t}{15})

a=\frac{d^{2}x}{dt^{2}}\\\\\therefore \frac{d^{2}x}{dt^{2}}=5(1-\frac{t}{15})

Integrating both sides we get

\int \frac{d^{2}x}{dt^{2}}dt=\int 5(1-\frac{t}{15})dt\\\\\frac{dx}{dt}=v=5t-\frac{5t^{2}}{30}+c

Now since car starts from rest thus at time t = 0 ; v=0 thus c=0

again integrating with respect to time we get

\int \frac{dx}{dt}dt=\int (5t-\frac{5t^{2}}{30})dt\\\\x(t)=\frac{5t^{2}}{2}-\frac{5t^{3}}{90}+D

Now let us assume that car starts from origin thus D=0

thus in the first 15 seconds it covers a distance of

x(15)=2.5\times 15^{2}-\farc{15^{3}}{18}=375m

Thus the remaining 125 meters will be covered with a constant speed of

v(15)=5\times 15-\frac{15^{2}}{6}=37.5m/s

in time equalling t_{2}=\frac{125}{37.5}=3.33seconds

Thus the total time it requires equals 15+3.33 seconds

t=18.33 seconds

3 0
2 years ago
Find the total amount of heat in Q lost through a wall 10' by 18' , with R value from q. 1. Inside temperature is 70 degrees F w
marissa [1.9K]

Answer:

Just think

Explanation:

6 0
3 years ago
Read 2 more answers
Other questions:
  • Two players find themselves in a legal battle over a patent. The patent is worth 20 for each player, so the winner would receive
    14·1 answer
  • The wheel and the attached reel have a combined weight of 50lb and a radius of gyration about their center of 6 A k in = . If pu
    9·1 answer
  • The sports car has a weight of 4900 lblb and center of gravity at GG. If it starts from rest it causes the rear wheels to slip a
    13·1 answer
  • Four eight-ohm speakers are connected in parallel to an audio power amplifier. The amplifier can supply a maximum driver output
    12·1 answer
  • Which of the following activities could be considered unethical?
    7·1 answer
  • What are difference between conic sectional and solids?
    15·1 answer
  • There are three different types of slings. What determines which type you use?
    13·1 answer
  • QUICK ASAP
    5·1 answer
  • In the case of a collision causing property damage, injury, or death, you are required to ____
    14·2 answers
  • Technician A says test lights are great for performing simple tests. Technician B says you can use a test light to check SRS cir
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!