19Mathematic Functions

This section summarizes mathematic functions.

19.1Complex Number

A number literal followed by suffix j becomes an imaginary part of a complex value.

>>> (1 - 2j) * (3 + 1j)
5 - 5j

19.2Rational Number

A number literal followed by suffix r becomes a rational value with which you can do faction calculations.

>>> 2 / 3r + 1 / 2r
7/6r

19.3Big Number

Importing gmp module would add following suffixes:

  • Suffix L creates a gmp.mpz or gmp.mpf instances that can calculate numbers with variable-length digits.
  • Suffix Lr creates a gmp.mpq instance that can calculate rational value with variable-length digits.

19.4Differentiation Formula

When a function is declared with a body that contains math calculation, you can get a differentiation formula from it using function#mathdiff() method. Assumes that you have the following function:

>>> f(x) = math.sin(x ** 2)

Then, you can call function#mathdiff() method for it like following:

>>> g = f.mathdiff()

The newly created function g(x) is one that does differential calculation of f(x). You can examine what body it has by seeing function#expr property.

>>> g.expr
`(math.cos(x ** 2) * (2 * x))

The table below shows what differentiation formulas are obtained from original math functions:

Original Differentiation Forumula
x ** 2 2 * x
x ** 3 3 * x ** 2
x ** 4 4 * x ** 3
a ** x math.log(a) * a ** x
math.sin(x) math.cos(x)
math.cos(x) -math.sin(x)
math.tan(x) 1 / math.cos(x) ** 2
math.exp(x) math.exp(x)
math.log(x) 1 / x
math.log10(x) 1 / (x * math.log(10))
math.asin(x) 1 / math.sqrt(1 - x ** 2)
math.acos(x) (-1) / math.sqrt(1 - x ** 2)
math.atan(x) 1 / (1 + x ** 2)
math.sqrt(x) 1 / (2 * math.sqrt(x))
math.sin(x) ** 2 math.cos(x) * 2 * math.sin(x)
math.sin(x ** 2) math.cos(x ** 2) * (2 * x)
math.log(math.sin(x)) math.cos(x) / math.sin(x)
x ** 2 * math.sin(x) 2 * x * math.sin(x) + x ** 2 * math.cos(x)
math.sin(x) / (x ** 2) (math.cos(x) * x ** 2 - math.sin(x) * (2 * x)) / (x ** 4)
3 ** (2 * x) 2 * math.log(3) * 3 ** (2 * x)
math.log(x ** 2 + 1) 2 * x / (x ** 2 + 1)
((x - 1) ** 2 * (x - 2) ** 3) / ((x - 5) ** 2) (((2 * (x - 1) * (x - 2) ** 3 + (x - 1) ** 2 * (3 * (x - 2) ** 2)) * (x - 5) ** 2 - (x - 1) ** 2 * (x - 2) ** 3 * (2 * (x - 5))) / (x - 5) ** 4)
math.sin(2 * x - 3) math.cos(2 * x - 3) * 2
math.cos(x) ** 2 -(math.sin(x) * 2 * math.cos(x))
(2 * x - 1) ** 3 6 * (2 * x - 1) ** 2
math.sqrt(x ** 2 + 2 * x + 3) (2 * x + 2) / (2 * math.sqrt(x ** 2 + 2 * x + 3))
1 / x (-1) / x ** 2
math.exp(x) + math.exp(-x) math.exp(x) - math.exp(-x)
math.exp(x) - math.exp(-x) math.exp(x) + math.exp(-x)
(math.sin(x + 2) + x + 2) * (math.sin(x + 3) + x + 3) (math.cos(x + 2) + 1) * (math.sin(x + 3) + x + 3) + (math.sin(x + 2) + x + 2) * (math.cos(x + 3) + 1)
math.sin(math.sin(x ** 2 / 3)) math.cos(math.sin(x ** 2 / 3)) * (math.cos(x ** 2 / 3) * (2 * x * 3 / 9))
(2 * x - 1) / x ** 2 (2 * x ** 2 - (2 * x - 1) * (2 * x)) / x ** 4