Python STL Math&cmath

Source: Internet
Author: User
Tags acos asin cos hypot natural logarithm sin square root cmath


The mathematical constants provided by the Python standard library Mathmath
Pi Mathematical constant Pi, the variable space belongs to the standard library math
E The mathematical constant e,e is the natural constant, and the variable space belongs to the standard library math
Function trigonometric functions commonly used in the math library
Name of function format function
Sin Sin (x) The sine of the returnedxradian value
Cos COS (x) x radians the cosine value of the returned
Tan Tan (x) x radian the tangent value of the returned
Asin ASIN (x) Returnedxby arcsine radians.
ACOs ACOs (x) Returnedxby arccosine radians.
Atan Atan (x) Returnedxby arctangent radian value.
Atan2 atan2 (x, y) ReturnsXYthe inverse tangent value of the given and coordinate values.
Degrees Degrees (x) degrees () Converts the radian x to an angle.
Radians Radians (x) Returns one angle of the radian value.
numeric functions
Name of function format function
Exp EXP (x) Returns the exponent \ (e^x\)of X.
Pow Pow (x, y) The POW () method returns the value of (x's y-square).
sqrt sqrt (x) The sqrt () method returns the square root of the number x.
Hypot Hypot (x, y) Hypot () returns the Euclidean normsqrt(x*x + y*y).
Modf MODF(x) modf () method returns the X integer part and decimal part
Fabs Fabs (x) fabs () method returns the absolute value of the number X
Log Log (x) Returned by the log () methodx natural logarithm.
Log10 LOG10 (x) The LOG10 () method returns the X logarithm of radix 10.
Copysign Copysign (x, y) Returns the | x | value of the symbol as modulosign (y)
Factorial Factorial (x) Calculationx!
Fmod Fmod (x, y) x modulo y, the implementation is toC library return a more accurate floating-point number to the bottom
Frexp Frexp (x) Returns a two-tuple, respectively, with the exponent part x mantissa part (m, e),
x == m * 2 ** e
Fsum Fsum (x) Returns a sum to be obtained floating point number.
The other functions
    • Math.isinf (x)

X is not positive or negative infinity.

    • Math.isnan (x)

X is not a Nan (not a number),

    • Math.ldexp (x, i)

Return x * (2**i). The opposite of Frexp ().

    • Math.trunc (x)

Integer part of Return x (truncated truncated) int (x)

    • MATH.EXPM1 (x)

Return e**x-1. When x is very small, it is more accurate.

    • MATH.LOG1P (x)

Return log (1+X) based on E, x approximately 0 o'clock more accurate

(Hyperbola method) Hyperbolic functions
    • Math.acosh (x)

Return the inverse hyperbolic cosine of x.

    • Math.asinh (x)

Return the inverse hyperbolic sine of x.

    • Math.atanh (x)

Return the inverse hyperbolic tangent of x.

    • Math.cosh (x)

Return the hyperbolic cosine of X.

    • Math.sinh (x)

Return the hyperbolic sine of X.

    • Math.tanh (x)

Return the hyperbolic tangent of X.

(Special method) special functions
    • Math.erf (x)

    • MATH.ERFC (x)

    • Math.gamma (x)

    • Math.lgamma (x)

Python Standard library Cmath

Standard library Math lacks the operation of a complex number, and it is time to introduce another python built-in standard library, Cmath.


As follows:


 
>>> import math  
>>> math.sqrt(-1)  
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
ValueError: math domain error  
>>> import cmath  
>>> cmath.sqrt(-1)  
1j  

You can see that the math.sqrt passed in a negative number, because the operation range in the real number will be error; Cmath.sqrt operation range in plural so the incoming parameter is-1 returns an imaginary number.

Cmath has most of the same functions as math, except that the operating range is within the complex number field, and there are still several functions worth remembering:

    • Cmath.phase (x):

Equal to Math.atan2 (X.imag, x.real).

    • Cmath.polar (x):

Equal to (ABS (x), phase (x)).

    • Cmath.rect (R, Phi):

Equal to R * (Math.Cos (PHI) + Math.sin (PHI) *1j).

Return (X.IMAG+X.REAL*J)


Python STL Math&cmath


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.