The time complexity of any algorithm is very important in deciding whether an application is reliable or not. Running a large algorithm as quickly as possible is very important when it comes to applying real-time output. To do this, Python has several standard math functions for quickly performing operations on entire arrays of data without having to write loops. One such library that contains such a function is ** numpy **. Let`s see how we can use this standard function in the case of vectorization.

** What is vectorization? **

Vectorization is used to speed up Python code without using a loop. Using such a function can help minimize your code execution time. Various operations are performed on a vector, such as the * dot product of vectors, * which is also known as the * dot product, * because it produces a single output, outer products that result in a square measurement matrix. equal to the length X of the length of vectors, * multiplication by an element, * which produces the product The element of the indices and the dimension of the matrix remain unchanged.

We will see how classical methods take longer than ordinary functions, calculating their processing time.

outer (a, b):Compute the outer product of two vectors.

multiply (a, b) :Matrix product of two arrays.

dot (a, b):Dot product of two arrays.

zeros ((n, m)) :Return a matrix of given shape and type, filled with zeros.

process_time ():Return the value (in fractional seconds) of the sum of the system and user CPU time of the c urrent process. It does not include time elapsed during sleep.

** Dot product: **

Dot product — it is an algebraic operation in which two vectors of equal length are multiplied so that it produces one number. The Dot Product is often referred to as a ** Inner Product **. This product results in a scalar number. Let`s consider two matrices

** Visual representation of the dot product — **

Below here is the Python code:

```
``` |

** Exit:**

dot_product = 833323333350000.0 Computation time = 35.59449199999999ms n_dot_product = 833323333350000 Computation time = 0.1559900000000225ms

** Outdoor product: **

* Tensor product * of two coordinate vectors is called * External work * . Consider two vectors * a * and * b * with dimensions ` nx 1 `

and ` mx 1 `

then the outer product of the vector leads to a rectangular matrix ** nxm **. If two vectors have the same dimension, then the resulting matrix will be a square matrix, as shown in the figure.

** Visual representation of the external product — **

Below is the Python code:

```
``` |

** Exit:**

outer_product = [[0. 0. 0. ..., 0. 0. 0.] [200.201.202. ..., 397. 398. 399.] [400. 402. 404. ..., 794. 796. 798.] ..., [39400. 39597. 39794. ..., 78209. 78406. 78603. ] [39600. 39798. 39996. ..., 78606. 78804. 79002.] [39800. 39999. 40198. ..., 79202. 79401.]] Computation time = 39.821617ms outer_product = [[0 0 0 ..., 0 0 0] [200 201 202 ..., 397 398 399] [400 402 404 ..., 794 79 6 798] ..., [39400 39597 39794 ..., 78209 78406 78603] [39600 39798 39996 ..., 78606 78804 79002] [39800 39999 40198 ..., 79003 79202 79401]] Computation time = 0.2809480000000031ms

Element wise product:

Elementwise multiplication of two matrices — it is an algebraic operation in which each element of the first matrix is multiplied by the corresponding element in the later matrix. The dimensions of the matrices must be the same.

Consider two matricesaandb, the element index ina— these areiandj,thena (i, j)is multiplied byb (i, j), respectively, as shown in the picture below.

Visual representation of the wise product Element —Below is the Python code:

`# Element-wise multiplication`

`import`

`time`

`import`

`numpy`

`import`

`array`

`a`

`=`

`array.array (`

``i``

`)`

`for`

`i`

`in`

`range`

`(`

`50000`

`):`

`a.append (i);`

`b`

`=`

`array.array (`

`` i``

`)`

`for`

`i`

`in`

`range`

`(`

`50000`

`,`

`100000`

`):`

`b.append (i)`

`# classic item-by-item product vector implementations`

`vector`

`=`

`numpy.zeros ((`

`50000`

`))`

`tic`

`=`

`time.process_time ()`

`for`

`i`

`in`

`range`

`(`

`len`

`(a)):`

`vector [i]`

`=`

`a [i]`

`*`

`b [i]`

`toc`

`=`

`time.process_time ()`

`(`

`"Element wise Product ="`

`+`

`str`

`(vector));`

`(`

`"Computation time ="`

`+`

`str`

`(`

`1000`

`*`

`(toc`

`-`

`tic))`

`+`

`"ms"`

`)`

`n_tic`

`=`

`time.process_time ()`

`vector`

`=`

`numpy.multiply (a, b)`

`n_toc`

`=`

`time.process_time ()`

`(`

`"Element wise Product ="`

`+`

`str`

`(vector));`

`(`

`"Computation time ="`

`+`

`str`

`(`

`1000`

`*`

`(n_toc`

`-`

`n_tic))`

`+`

`"ms"`

`)`

Exit:Element wise Product = [0.00000000e + 00 5.00010000e + 04 1.00004000e + 05 ..., 4.99955001e + 09 4.99970000e + 09 4.99985000e + 09] Computation time = 23.516678000000013ms Element wise Product = [0 50001 100004 ... , 704582713 704732708 704882705] Computation time = 0.2250640000000248ms

X
# Submit new EBook