Can you multiply 2 arrays in python?

How to Multiply Matrices in Python

The dot function of the numpy library allows you to multiply two arrays in python through the product rows by columns.

import numpy as np
np.dot (m, n)

The arguments m and n are two matrix objects or vectors, previously defined with the array function.

The dot() function returns the product row by column of arrays.

Can you multiply 2 arrays in python?

Example

The dot function requires importing the numpy library into the python interpreter

import numpy as np

Give two arrays A and B defined with the array function.

A = np.array ([[1,2], [3,4], [5,6]])
B = np.array ([[1,2,3], [3,4,5]])

The number of rows in the first array must be equal to the number of columns in the second array.

The product is calculated row by column using the dot () function.

np.dot (A, B)

This is the result in output of the function

array ([[7, 10, 13],
[15, 22, 29],
[23, 34, 45]])

The output matrix is ​​the result of the product between the rows of A for the rows of B.

Can you multiply 2 arrays in python?

https://how.okpedia.org/en/python/how-to-multiply-two-arrays-matrix-in-python

In Python, we can implement a matrix as nested list (list inside a list).

We can treat each element as a row of the matrix.

For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2 matrix.

The first row can be selected as X[0]. And, the element in first row, first column can be selected as X[0][0].

Multiplication of two matrices X and Y is defined only if the number of columns in X is equal to the number of rows Y.

If X is a n x m matrix and Y is a m x l matrix then, XY is defined and has the dimension n x l (but YX is not defined). Here are a couple of ways to implement matrix multiplication in Python.

Source Code: Matrix Multiplication using Nested Loop

# Program to multiply two matrices using nested loops

# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
    [6,7,3,0],
    [4,5,9,1]]
# result is 3x4
result = [[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)

Output

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

In this program, we have used nested for loops to iterate through each row and each column. We accumulate the sum of products in the result.

This technique is simple but computationally expensive as we increase the order of the matrix.

For larger matrix operations we recommend optimized software packages like NumPy which is several (in the order of 1000) times faster than the above code.

Source Code: Matrix Multiplication Using Nested List Comprehension

# Program to multiply two matrices using list comprehension

# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

# 3x4 matrix
Y = [[5,8,1,2],
    [6,7,3,0],
    [4,5,9,1]]

# result is 3x4
result = [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*Y)] for X_row in X]

for r in result:
   print(r)

The output of this program is the same as above. To understand the above code we must first know about built-in function zip() and unpacking argument list using * operator.

We have used nested list comprehension to iterate through each element in the matrix. The code looks complicated and unreadable at first. But once you get the hang of list comprehensions, you will probably not go back to nested loops.

numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])=<ufunc 'multiply'>#

Multiply arguments element-wise.

Parametersx1, x2array_like

Input arrays to be multiplied. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

outndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

wherearray_like, optional

This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

**kwargs

For other keyword-only arguments, see the ufunc docs.

Returnsyndarray

The product of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.

Notes

Equivalent to x1 * x2 in terms of array broadcasting.

Examples

>>> np.multiply(2.0, 4.0)
8.0

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.multiply(x1, x2)
array([[  0.,   1.,   4.],
       [  0.,   4.,  10.],
       [  0.,   7.,  16.]])

The * operator can be used as a shorthand for np.multiply on ndarrays.

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> x1 * x2
array([[  0.,   1.,   4.],
       [  0.,   4.,  10.],
       [  0.,   7.,  16.]])

How do I multiply two array values in Python?

how to multiply two arrays in python.
list1 = [1, 2, 3].
list2 = [4, 5, 6].
products = [].
for num1, num2 in zip(list1, list2):.
products. append(num1 * num2).
print(products).

Can you multiply two arrays?

C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.

What happens when you multiply 2 NumPy arrays?

Multiply two numpy arrays It returns a numpy array of the same shape with values resulting from multiplying values in each array elementwise. Note that both the arrays need to have the same dimensions.

Can we multiply 2 list in Python?

Multiply two lists using for loop Through for loop, we can iterate through the list. Similarly, with every iteration, we can multiply the elements from both lists. For this purpose, we can use Zip Function. The zip() function in python can combine the contents of 2 or more iterables.