SoFunction
Updated on 2025-03-03

A detailed explanation of the implementation of NumPy's simple arithmetic and other operations

Simple arithmetic

You can use arithmetic operators directly between NumPy arrays+ - * /, but this section discusses an extension where we have functions that can accept any array-like objects such as lists, tuples, etc. and perform arithmetic operations based on conditions.

Conditional arithmetic: means we can define the conditions in which arithmetic operations should occur.

All the discussed arithmetic functions accept onewhereParameters, where we can specify the conditions.

addition

add()The function sums the contents of two arrays and returns the result to a new array.

Example:arr1The value inarr2Among the values:

import numpy as np

arr1 = ([10, 11, 12, 13, 14, 15])
arr2 = ([20, 21, 22, 23, 24, 25])

newarr = (arr1, arr2)

print(newarr)

The above example will return[30 32 34 36 38 40],This is10+2011+2112+22and so on.

Subtraction

subtract()The function subtracts the value in one array from the value in another array and returns the result to a new array.

Example: Fromarr1Subtract the value inarr2Values ​​in:

import numpy as np

arr1 = ([10, 20, 30, 40, 50, 60])
arr2 = ([20, 21, 22, 23, 24, 25])

newarr = (arr1, arr2)

print(newarr)

The above example will return[-10 -1 8 17 26 35],This is10-2020-2130-22The result of etc.

multiplication

multiply()The function multiplies the value in one array with the value in another array and returns the result to a new array.

Example:arr1The value inarr2Multiply the values ​​in:

import numpy as np

arr1 = ([10, 20, 30, 40, 50, 60])
arr2 = ([20, 21, 22, 23, 24, 25])

newarr = (arr1, arr2)

print(newarr)

The above example will return[200 420 660 920 1200 1500],This is10*2020*2130*22The result of etc.

division

divide()The function divides the value in one array by the value in another array and returns the result to a new array.

Example:arr1The value in divided byarr2Values ​​in:

import numpy as np

arr1 = ([10, 20, 30, 40, 50, 60])
arr2 = ([3, 5, 10, 8, 2, 33])

newarr = (arr1, arr2)

print(newarr)

The above example will return[3.33333333 4. 3. 5. 25. 1.81818182],This is10/320/530/10The result of etc.

power

power()The function raises the value in the first array to the power of the value in the second array and returns the result to a new array.

Example:arr1The value inarr2The power of the value in:

import numpy as np

arr1 = ([10, 20, 30, 40, 50, 60])
arr2 = ([3, 5, 6, 8, 2, 33])

newarr = (arr1, arr2)

print(newarr)

The above example will return[1000 3200000 729000000 6553600000000 2500 0],This is10^320^530^6The result of etc.

Remaining number

mod()andremainder()All functions return the remainder of the value in the first array corresponding to the value in the second array and return the result to a new array.

Example: Return the remainder:

import numpy as np

arr1 = ([10, 20, 30, 40, 50, 60])
arr2 = ([3, 7, 9, 8, 2, 33])

newarr = (arr1, arr2)

print(newarr)

The above example will return[1 6 3 0 0 27],This is10 divided by 3The remainder of  (10%3)、20 divided by 7The remainder of  (20%7)、30 divided by 9The remainder of  (30%9) wait.

When usedremainder()When function, the result is the same:

Example: Return the remainder:

import numpy as np

arr1 = ([10, 20, 30, 40, 50, 60])
arr2 = ([3, 7, 9, 8, 2, 33])

newarr = (arr1, arr2)

print(newarr)

Quotation and remainder

divmod()The function returns the quotient and remainder. The return value is two arrays, the first array contains the quotient and the second array contains the remainder.

Example: Return quotient and remainder:

import numpy as np

arr1 = ([10, 20, 30, 40, 50, 60])
arr2 = ([3, 7, 9, 8, 2, 33])

newarr = (arr1, arr2)

print(newarr)

The above example will return:

(array([3, 2, 3, 5, 25, 1]), array([1, 6, 3, 0, 0, 27]))

The first array represents the quotient, (when you will10Divide by320Divided by `7

30Divide by9` The integer value obtained in isochronous. The second array represents the remainder of the same division.

Absolute value

absolute()andabs()Functions all perform the same absolute value operation on each element, but we should useabsolute()To avoid built-in with Python()Confused.

Example: Return to the merchandise

and remainder:

import numpy as np

arr = ([-1, -2, 1, 2, 3, -4])

newarr = (arr)

print(newarr)

The above example will return[1 2 1 2 3 4]

at last

This is the article about this article explaining the implementation of NumPy simple arithmetic (addition, subtraction, multiplication, division and other operations) in detail. For more related content on NumPy simple arithmetic, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!