Math Methods
Anonymous contributor
Published May 1, 2024
Contribute to Docs
In NumPy, Math Methods are used to perform mathematical operations on arrays. These methods encompass arithmetic operations, trigonometric functions, exponential and logarithmic functions, and more. They play a crucial role in scientific computing, data analysis, and machine learning, making NumPy indispensable across scientific research, engineering, finance, and data analysis domains.
Syntax
The generic syntax for the NumPy math methods is as follows:
numpy.math_method()
Example
import numpy as np# Perform element-wise addition of two arrays using numpy.add() methodresult = np.add([1, 2, 3], [10, 20, 30])print("Result of addition:", result)
Result of addition: [11 22 33]
Basic Categories of Math Methods
1. Basic Arithmetic Operations
- Addition
numpy.add()
: Performs element-wise addition of arrays. - Subtraction
numpy.subtract()
: Performs element-wise subtraction of arrays. - Multiplication
numpy.multiply()
: Performs element-wise multiplication of arrays. - Division
numpy.divide()
: Performs element-wise division of arrays. - Power
numpy.power()
: Performs element-wise exponentiation of arrays.
2. Trigonometric Functions
- Sine
numpy.sin()
: Computes the sine of each element in the array. - Cosine
numpy.cos()
: Computes the cosine of each element in the array. - Tangent
numpy.tan()
: Computes the tangent of each element in the array. - Inverse Sine
numpy.arcsin()
: Computes the inverse sine of each element in the array. - Inverse Cosine
numpy.arccos()
: Computes the inverse cosine of each element in the array. - Inverse Tangent
numpy.arctan()
: Computes the inverse tangent of each element in the array.
3. Exponential and Logarithmic Functions
- Exponential
numpy.exp()
: Computes the exponential of each element in the array. - Natural Logarithm
numpy.log()
: Computes the natural logarithm of each element in the array. - Base-10 Logarithm
numpy.log10()
: Computes the base-10 logarithm of each element in the array.
4. Miscellaneous Functions
- Absolute Value
numpy.absolute()
: Computes the absolute value of each element in the array. - Square Root
numpy.sqrt()
: Computes the non-negative square root of each element in the array. - Ceiling
numpy.ceil()
: Rounds each element of the array to the nearest integer greater than or equal to that element. - Floor
numpy.floor()
: Rounds each element of the array to the nearest integer less than or equal to that element. - Rounding
numpy.round()
: Rounds each element of the array to the nearest integer.
Math Methods
- .abs()
- Calculates the absolute value of a given number or each element in an array.
- .arccos()
- Calculates the inverse cosine of each element in an array or a single value.
- .arcsin()
- Calculates the inverse sine of each element in an array.
- .arctan()
- Calculates the inverse tangent of the elements in an array.
- .around()
- Rounds each element of an array to the specified number of decimal places.
- .ceil()
- Rounds each element in an array to the nearest greater integer.
- .cos()
- Computes the cosine of each element in an array or a single value.
- .deg2rad()
- Converts angles from degrees to radians.
- .degrees()
- Converts angles expressed in radians into degrees.
- .diff()
- Calculates the n-th discrete difference along a given axis, effectively subtracting consecutive elements in an array.
- .exp()
- Computes the exponential of all elements in the input array.
- .floor()
- Rounds down a number or an array of numbers to the nearest smallest integer.
- .hypot()
- Returns the hypotenuse of a right triangle, given the legs.
- .log()
- Calculates the natural logarithm of each element in an array.
- .log10()
- Calculates the base-10 logarithm of each element in an array.
- .nanprod()
- Calculates the product of array elements.
- .nansum()
- Computes the sum of array elements, treating Not a Number (NaN) values as zero.
- .power()
- Raises each element in the first array to the power of the corresponding element in the second array.
- .prod()
- Calculates the product of array elements along a specified axis.
- .rad2deg()
- Converts angles from radians to degrees.
- .rint()
- Rounds each element of an array to the nearest integer and returns a new array with the rounded values.
- .round()
- Rounds a number or an array of numbers to a specified number of decimal places.
- .sin()
- Calculates the trigonometric sine of each element in an array.
- .sqrt()
- Calculates the square root of each element in an array.
- .square()
- Calculates the square of each element in an array.
- .tan()
- Calculates the tangent of each element in an array or a single value in radians.
- .trunc()
- Returns the truncated value of each element in an array, removing the fractional part and leaving the integer part.
- .unwrap()
- Unwraps a phase angle array by changing deltas greater than a threshold to their 2*pi complement.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours