Hướng dẫn dùng np.multiply python

Phép nhân ma trận là một phép toán tạo ra một ma trận duy nhất bằng cách lấy hai ma trận làm đầu vào và nhân các hàng của ma trận thứ nhất với cột của ma trận thứ hai. Lưu ý rằng chúng ta phải đảm bảo rằng số hàng trong ma trận đầu tiên phải bằng số cột trong ma trận thứ hai.

Các bài viết liên quan:

Hướng dẫn dùng np.multiply python

Trong Python, quá trình nhân ma trận bằng NumPy được gọi là vectơ hóa. Mục tiêu chính của vectơ hóa là loại bỏ hoặc giảm các vòng lặp for mà chúng tôi đang sử dụng một cách rõ ràng. Bằng cách giảm vòng lặp ‘for’ từ các chương trình giúp tính toán nhanh hơn. Gói tích hợp NumPy được sử dụng để thao tác và xử lý mảng.

Đây là ba phương pháp mà qua đó chúng ta có thể thực hiện phép nhân ma trận numpy.

  • Đầu tiên là việc sử dụng hàm nhân (), thực hiện phép nhân theo phần tử của ma trận.
  • Thứ hai là việc sử dụng hàm matmul (), hàm này thực hiện tích ma trận của hai mảng.
  • Cuối cùng là sử dụng hàm dot (), hàm này thực hiện tích số chấm của hai mảng.

Ví dụ 1: Phép nhân ma trận theo phần tử

import numpy as np array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3) array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3) result=np.multiply(array1,array2) result

Trong đoạn code trên

  • Chúng tôi đã tạo một array1 và array2 bằng cách sử dụng hàm numpy.array () với kích thước 3.
  • Chúng tôi đã tạo một kết quả biến và gán giá trị trả về của hàm np.multiply ().
  • Chúng tôi đã chuyển cả mảng array1 và array2 trong np.multiply ().
  • Cuối cùng, chúng tôi đã cố gắng in giá trị của kết quả.

Trong đầu ra, một ma trận ba chiều đã được hiển thị có các phần tử là kết quả của phép nhân khôn ngoan phần tử của cả hai phần tử array1 và array2.

Output:

Ví dụ 2: Sản phẩm ma trận

import numpy as np array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3) array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3) result=np.matmul(array1,array2) result

Output:

Trong đoạn code trên

  • Chúng tôi đã tạo array1 và array2 bằng cách sử dụng hàm numpy.array () với kích thước 3.
  • Chúng tôi đã tạo một kết quả biến và gán giá trị trả về của hàm np.matmul ().
  • Chúng tôi đã chuyển cả mảng array1 và array2 trong np.matmul ().
  • Cuối cùng, chúng tôi đã cố gắng in giá trị của kết quả.

Trong đầu ra, một ma trận ba chiều đã được hiển thị có các phần tử là tích của cả phần tử array1 và array2.

Ví dụ 3: Dot product

Đây là các thông số kỹ thuật sau cho numpy.dot:

  • Khi cả a và b đều là mảng 1-D (một chiều )-> Tích bên trong của hai vectơ
  • Khi cả a và b đều là mảng 2-D (hai chiều) -> Phép nhân ma trận
  • Khi a hoặc b là 0-D (còn được gọi là vô hướng) -> Nhân bằng cách sử dụng numpy.multiply (a, b) hoặc a * b.
  • Khi a là mảng N-D và b là mảng 1-D -> Tính tổng trên trục cuối cùng của a và b.
  • Khi a là mảng N-D và b là mảng M-D với điều kiện M> = 2 -> Tính tổng tích trên trục cuối cùng của a và trục thứ hai đến cuối của b:
  • Ngoài ra, dấu chấm (a, b) [i, j, k, m] = sum (a [i, j,:] * b [k,:, m])

import numpy as np array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3) array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3) result=np.dot(array1,array2) result

Trong đoạn code trên

  • Chúng tôi đã tạo array1 và array2 bằng cách sử dụng hàm numpy.array () với kích thước 3.
  • Chúng tôi đã tạo một kết quả biến và gán giá trị trả về của hàm np.dot ().
  • Chúng tôi đã chuyển cả mảng array1 và array2 trong np.dot ().
  • Cuối cùng, chúng tôi đã cố gắng in giá trị của kết quả.

Trong đầu ra, một ma trận ba chiều đã được hiển thị có các phần tử là tích số chấm của cả phần tử array1 và array2.

Output:

Phép nhân ma trận là một phép toán tạo ra một ma trận duy nhất bằng cách lấy hai ma trận làm đầu vào và nhân các hàng của ma trận thứ nhất với cột của ma trận thứ hai. Lưu ý rằng chúng ta phải đảm bảo rằng số hàng trong ma trận đầu tiên phải bằng số cột trong ma trận thứ hai.

Nội dung chính

  • Ví dụ 1: Phép nhân ma trận theo phần tử
  • Ví dụ 2: Sản phẩm ma trận
  • Ví dụ 3: Dot product
  • Multiplying Integers In Python
  • Multiplying Complex Numbers In Python
  • Multiplying String With Integers In Python
  • Use Of Functions To Multiply Numbers In Python
  • Multiplying Lists In Python
  • Multiplying One List By Another In Python
  • Multiplying Elements Of A List
  • Use Of Traversal Python In Multiplying Elements Of A List

Nội dung chính

  • Ví dụ 1: Phép nhân ma trận theo phần tử
  • Ví dụ 2: Sản phẩm ma trận
  • Ví dụ 3: Dot product
  • Multiplying Integers In Python
  • Multiplying Complex Numbers In Python
  • Multiplying String With Integers In Python
  • Use Of Functions To Multiply Numbers In Python
  • Multiplying Lists In Python
  • Multiplying One List By Another In Python
  • Multiplying Elements Of A List
  • Use Of Traversal Python In Multiplying Elements Of A List

Các bài viết liên quan:

Trong Python, quá trình nhân ma trận bằng NumPy được gọi là vectơ hóa. Mục tiêu chính của vectơ hóa là loại bỏ hoặc giảm các vòng lặp for mà chúng tôi đang sử dụng một cách rõ ràng. Bằng cách giảm vòng lặp ‘for’ từ các chương trình giúp tính toán nhanh hơn. Gói tích hợp NumPy được sử dụng để thao tác và xử lý mảng.

Đây là ba phương pháp mà qua đó chúng ta có thể thực hiện phép nhân ma trận numpy.

  • Đầu tiên là việc sử dụng hàm nhân (), thực hiện phép nhân theo phần tử của ma trận.
  • Thứ hai là việc sử dụng hàm matmul (), hàm này thực hiện tích ma trận của hai mảng.
  • Cuối cùng là sử dụng hàm dot (), hàm này thực hiện tích số chấm của hai mảng.

Ví dụ 1: Phép nhân ma trận theo phần tử

import numpy as np array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3) array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3) result=np.multiply(array1,array2) result

Trong đoạn code trên

  • Chúng tôi đã tạo một array1 và array2 bằng cách sử dụng hàm numpy.array () với kích thước 3.
  • Chúng tôi đã tạo một kết quả biến và gán giá trị trả về của hàm np.multiply ().
  • Chúng tôi đã chuyển cả mảng array1 và array2 trong np.multiply ().
  • Cuối cùng, chúng tôi đã cố gắng in giá trị của kết quả.

Trong đầu ra, một ma trận ba chiều đã được hiển thị có các phần tử là kết quả của phép nhân khôn ngoan phần tử của cả hai phần tử array1 và array2.

Output:

Ví dụ 2: Sản phẩm ma trận

import numpy as np array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3) array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3) result=np.matmul(array1,array2) result

Output:

Trong đoạn code trên

  • Chúng tôi đã tạo array1 và array2 bằng cách sử dụng hàm numpy.array () với kích thước 3.
  • Chúng tôi đã tạo một kết quả biến và gán giá trị trả về của hàm np.matmul ().
  • Chúng tôi đã chuyển cả mảng array1 và array2 trong np.matmul ().
  • Cuối cùng, chúng tôi đã cố gắng in giá trị của kết quả.

Trong đầu ra, một ma trận ba chiều đã được hiển thị có các phần tử là tích của cả phần tử array1 và array2.

Ví dụ 3: Dot product

Đây là các thông số kỹ thuật sau cho numpy.dot:

  • Khi cả a và b đều là mảng 1-D (một chiều )-> Tích bên trong của hai vectơ
  • Khi cả a và b đều là mảng 2-D (hai chiều) -> Phép nhân ma trận
  • Khi a hoặc b là 0-D (còn được gọi là vô hướng) -> Nhân bằng cách sử dụng numpy.multiply (a, b) hoặc a * b.
  • Khi a là mảng N-D và b là mảng 1-D -> Tính tổng trên trục cuối cùng của a và b.
  • Khi a là mảng N-D và b là mảng M-D với điều kiện M> = 2 -> Tính tổng tích trên trục cuối cùng của a và trục thứ hai đến cuối của b:
  • Ngoài ra, dấu chấm (a, b) [i, j, k, m] = sum (a [i, j,:] * b [k,:, m])

import numpy as np array1=np.array([[1,2,3],[4,5,6],[7,8,9]],ndmin=3) array2=np.array([[9,8,7],[6,5,4],[3,2,1]],ndmin=3) result=np.dot(array1,array2) result

Trong đoạn code trên

  • Chúng tôi đã tạo array1 và array2 bằng cách sử dụng hàm numpy.array () với kích thước 3.
  • Chúng tôi đã tạo một kết quả biến và gán giá trị trả về của hàm np.dot ().
  • Chúng tôi đã chuyển cả mảng array1 và array2 trong np.dot ().
  • Cuối cùng, chúng tôi đã cố gắng in giá trị của kết quả.

Trong đầu ra, một ma trận ba chiều đã được hiển thị có các phần tử là tích số chấm của cả phần tử array1 và array2.

Output:

"

This article is part of in the series

Published: Monday 20th March 2017

Last Updated: Thursday 30th December 2021

Multiplication is a fundamental operation in the arithmetic and programming world. We can find its use in every program(or in the logic behind every code), no matter how basic it is. Thus a programmer must know how to multiply integers, decimals, complex numbers, and strings in Python to create an efficient and accurate code. In this article, we will learn how to perform all these operations with coding examples. 

But first, let’s see what the syntax is to denote multiplication operation. We use the asterisk character ‘*’ to perform multiplication. Let’s consider an example, suppose we have to multiply two numbers, 171 and 899. Then the code for this operation is :

The output for the code mentioned above is :

We can see that we use the asterisk operator for writing multiplication codes in Python.

Multiplying Integers In Python

Integers are a data type consisting of only whole numbers, i.e., there are no fractional parts in integers. For example 117, 1, 2 ,10 are integers. The syntax for multiplying integers is straightforward. We write the numbers to be multiplied and separate them by the asterisk operator.

We can see the multiplication of the numbers 90 and 17 in the complete code snippet given above.

The basic definition of the float number data type is that it contains numbers comprising of fractions. It can store numbers having up to seventeen significant digits. Examples of float numbers are 17.003, 5.0098, 70.0007, and many more such numbers.

The syntax for multiplying float numbers is the same as that of integers; we separate the numbers we have to multiply by the asterisk operator and print it.

In this code snippet, we have multiplied two float numbers, 91.007 and 87.3333, using the asterisk operator and printed it. We can note here that the output of the multiplication is also a float number.

Multiplying Complex Numbers In Python

Complex numbers are imaginary numbers of the form ‘a+ bi’, where 'a' represents the real number and 'b' is the coefficient of the imaginary number. Also,' i' represents 'iota,' which is the square root of -1. Before going for the code, let us first discuss how we multiply complex numbers. For example let's consider the complex numbers (3 + 4i) and (5 + 6i).

The result of this multiplication is as follows: 

(3*5) + (3*6i) + (4*5i) + (4i*6i)

=15+ 18i+20i+ 24(i^2)

= -9+38i [ since (i^2 =-1) ]

We use the complex() method for the multiplication of complex numbers in Python

In the complex() method, we write the real part first and then the imaginary part, separated by commas. It is good to store each complex number in a variable and then perform multiplication using the asterisk operator on the variables. Let's perform multiplication on the numbers that were considered in the example above.

We have written the complex numbers using the complex() method. The numbers are stored in variables num1 and num2, and then the multiplication is performed.

Now let’s see what the output is:

We can see the output matches our calculation in the previous example.

Multiplying String With Integers In Python

What does 'string multiplied with an integer' mean? Suppose we want to display a string(a data type of characters)  multiple times, then instead of writing it again and again, we can multiply the string by the number of times it has to be shown, and hence we can obtain the desired output. 

To multiply a string with an integer in Python, we use the def()function. In the def()function, we create another function in which we mention the string variable to be repeated, followed by the number of times it has to be repeated. Then we return the multiplied value. Let's take an example here, we take the string "Hello World!" and repeat it five times.

Let's see the syntax of complete code and its output:

We have created the row() function using the def() function. We have mentioned the string "Hello World!" as the first parameter of the row() function and then multiplication number as the second parameter. 

We can see that the output displays the string five times.

Use Of Functions To Multiply Numbers In Python

Functions make a program compact and easy to understand. If your program has many multiplication operations, then to save time and prevent any confusion, you can use functions to multiply numbers. The steps to define a function are as follows:

  1. Declare the parameters of the function.
  2. Define the operation to be performed using the function.
  3. Specify the return value of the function.

We declare a function using the def keyword.

Let's see what the syntax of the code is:

Here we have defined a function mult() using the def keyword. The parameters of this function are x and y, and the operation is the multiplication of x and y, and the value returned is the product of x and y. Now let's see the output of this code:

We can see that the code provides the product of the entered numbers as output.

Multiplying Lists In Python

Multiplying One List By Another In Python

We are going to use the zip()method to multiply two lists in Python. The zip() method extracts the elements of the list. Then we multiply the elements obtained and append them into a new list. However, we have to make sure that the lists are of equal length, i.e., both the lists have an equal number of elements.

Let’s see how we write the code for this operation:

Here, our two lists are 'list1' and 'list2'. Multiply is an empty list in which the compiler will append the product. Using the for loop and zip()method, we obtain the elements of each list. After multiplying the elements, we append the products in the empty list 'multiply.'

The output obtained is :

We can see the products appended in the result.

Multiplying Elements Of A List

The math module of Python has a prod() function to make our multiplication easier. We import the module first and then write the code as follows:

We will get the product of elements of the list as output:

Use Of Traversal Python In Multiplying Elements Of A List

The traversal method is an iterative method to multiply elements of a list. Iterative methods comprise of looping statements. Let's see how we can use looping statements to obtain the product of list elements:

Here we have defined a function Multiplylist() in which we have used a looping statement 'for.' We have initialized the variable r as one to multiply the elements one by one using the loop quickly. Finally, we have printed the product of the list. Let's see the output:

You can learn more about the multiplication of lists in Python here.

Conclusion

Python offers various ways in which we can multiply numbers. These numbers can be integers or decimals or can be contained in lists. For every multiplication problem, Python has a solution. You can make your code efficient and accurate by including functions. We hope that this article helped you understand the different ways you can multiply numbers and use them effortlessly in your coding practices.

  1. Home
  2. Python How To's