why is complex exponentiation so fast in python 3?

  • Last Update :
  • Techknowledgy :

You can explain your observation by looking at the bytecode generated by CPython using the dis module. Lets take a look.

** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
from match
import sin;
sin(1)
1 0 LOAD_CONST 0(0)
2 LOAD_CONST 1(('sin', ))
4 IMPORT_NAME 0(match)
6 IMPORT_FROM 1(sin)
8 STORE_NAME 1(sin)
10 POP_TOP
12 LOAD_NAME 1(sin)
14 LOAD_CONST 2(1)
16 CALL_FUNCTION 1
18 POP_TOP
20 LOAD_CONST 3(None)
22 RETURN_VALUE
   **
   ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
   from math
import e;
(e ** 1 j).imag
1 0 LOAD_CONST 0(0)
2 LOAD_CONST 1(('e', ))
4 IMPORT_NAME 0(math)
6 IMPORT_FROM 1(e)
8 STORE_NAME 1(e)
10 POP_TOP
12 LOAD_NAME 1(e)
14 LOAD_CONST 2(1 j)
16 BINARY_POWER
18 LOAD_ATTR 2(imag)
20 POP_TOP
22 LOAD_CONST 3(None)
24 RETURN_VALUE
   **
   ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
   from cmath
import exp;
exp(1 j).imag
1 0 LOAD_CONST 0(0)
2 LOAD_CONST 1(('exp', ))
4 IMPORT_NAME 0(cmath)
6 IMPORT_FROM 1(exp)
8 STORE_NAME 1(exp)
10 POP_TOP
12 LOAD_NAME 1(exp)
14 LOAD_CONST 2(1 j)
16 CALL_FUNCTION 1
18 LOAD_ATTR 2(imag)
20 POP_TOP
22 LOAD_CONST 3(None)
24 RETURN_VALUE
   **
   ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
   (2.718281828459045 ** 1 j).imag
1 0 LOAD_CONST 0((0.5403023058681398 + 0.8414709848078965 j))
2 LOAD_ATTR 0(imag)
4 RETURN_VALUE

Suggestion : 2

math.sqrt(x) is faster than math.pow(x, 0.5) or x ** 0.5 but the precision of the results is the same. The cmath module is extremely similar to the math module, except for the fact it can compute complex numbers and all of its results are in the form of a + bi. It can also use .sqrt():,Before python 3.0.0, this raised a ValueError.,For most (all in Python 2.x) arithmetic operations the result’s type will be that of the wider operand. This is not true for **; the following cases are exceptions from this rule:,The math.sqrt() function raises a ValueError if the result would be complex:

Exponentiation can be used by using the builtin pow-function or the ** operator:

2 ** 3 # 8 pow(2, 3) # 8
2._
Base: int, exponent: int < 0:
Base: int, exponent: int < 0:
2 ** -3

The operator module contains two functions that are equivalent to the **-operator:

import operatoroperator.pow(4, 2) # 16 operator.pow(4, 3) # 64
6._
val1, val2 = 4, 2 val1.pow(val2) # 16 val2.rpow(val1) # 16

Suggestion : 3

November 23, 2021

The first way to calculate exponents in Python is by using loops. Loops can help us run the code block over and over, to gain its benefits for finding the exponential value of Python.

base = 2
exponent = 5

output = base

if exponent != 0:
   for i in range(exponent - 1):
   output = output * base
else:
   output == 1

print("Exponent using python loops is : ", output) # 32

In the previous example we used base 2 and the exponent of 5. Then, 2 is multiplied by 5 times. This is the easiest method of calculating the value of the exponential function in Python.

base = 2
exponent = 5

print("Exponential value using Operator is : ", base ** exponent) #32

We can also use floating-point numbers when calculating exponential value. When we apply any operand to represent a floating point number, the result is in the floating point number as illustrated in the code fragment.

print(5.1 ** 2.3) #42.404447108809826

It is suggested to make use of pow(5,3,2) in place of pow(5,3)%2 since the efficiency is higher in calculating the exponential modulo value.

print("Pow method using 3 arguments:", pow(10, 5, 6)) # Example 3: 4

These operations can be performed with negative numbers, too:

print(pow(-2, 3)) # - 8
print(pow(-5, -3)) # - 0.008

Suggestion : 4

A quick way of getting the square root of a value is using the exponentiation operator ** with 0.5 as the second parameter. See below for a quick example of this:,Using the exponentiation operator ** is an easy way of getting the square root of a number. This operator raises the first operand to the power of the second operand.,Using ** isn't the only way, or most accurate way, to calculate the square root in Python. We'll look at how you can calculate the square root of a value using exponentiation, along with the math and numpy sqrt() functions, and also consider the advantages of each option.,An alternative way of calculating the square root of a value is by using the math.sqrt() function from the math library. The example below demonstrates how we can apply the math.sqrt() function to the example used in the **0.5 section:

4 ** 0.5
2.0
values = [16, 25, 36, 49, 64]
for x in values:
   x_sqrt = x ** 0.5
print(f 'value: {x} square root {x_sqrt}')
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
-4 ** 0.5
-2.0

Suggestion : 5

Return e raised to the power x, where e is the base of natural logarithms.,The modulus (absolute value) of a complex number x can be computed using the built-in abs() function. There is no separate cmath module function for this operation.,Returns the logarithm of x to the given base. If the base is not specified, returns the natural logarithm of x. There is one branch cut, from 0 along the negative real axis to -∞, continuous from above.,Return the representation of x in polar coordinates. Returns a pair (r, phi) where r is the modulus of x and phi is the phase of x. polar(x) is equivalent to (abs(x), phase(x)).

z == z.real + z.imag * 1 j
>>> phase(complex(-1.0, 0.0))
3.141592653589793
   >>>
   phase(complex(-1.0, -0.0)) -
   3.141592653589793

Suggestion : 6

Last Updated : 30 Jun, 2022

What is the minimum time complexity to find n’th Fibonacci Number? 
We can find n’th Fibonacci Number in O(Log n) time using Matrix Exponentiation. Refer method 4 of this for details. In this post, a general implementation of Matrix Exponentiation is discussed. 

For solving the matrix exponentiation we are assuming a
linear recurrence equation like below:

   F(n) = a * F(n - 1) + b * F(n - 2) + c * F(n - 3) for n >= 3
   .....Equation(1)
where a, b and c are constants.

For this recurrence relation, it depends on three previous values.
Now we will
try to represent Equation(1) in terms of the matrix.

[First Matrix] = [Second matrix] * [Third Matrix] |
F(n) | = Matrix 'C' * | F(n - 1) |
   |
   F(n - 1) | | F(n - 2) |
   |
   F(n - 2) | | F(n - 3) |

   Dimension of the first matrix is 3 x 1 .
Dimension of the third matrix is also 3 x 1.

So the dimension of the second matrix must be 3 x 3[For multiplication rule to be satisfied.]

Now we need to fill the Matrix 'C'.

So according to our equation.
F(n) = a * F(n - 1) + b * F(n - 2) + c * F(n - 3)
F(n - 1) = F(n - 1)
F(n - 2) = F(n - 2)

C = [a b c
   1 0 0
   0 1 0
]

Now the relation between matrix becomes: [First Matrix][Second matrix][Third Matrix] |
   F(n) | = | a b c | * | F(n - 1) |
   |
   F(n - 1) | | 1 0 0 | | F(n - 2) |
   |
   F(n - 2) | | 0 1 0 | | F(n - 3) |

   Lets assume the initial values
for this
case: -
F(0) = 0
F(1) = 1
F(2) = 1

So, we need to get F(n) in terms of these values.

So,
for n = 3 Equation(1) changes to |
   F(3) | = | a b c | * | F(2) |
   |
   F(2) | | 1 0 0 | | F(1) |
   |
   F(1) | | 0 1 0 | | F(0) |

   Now similarly
for n = 4 |
   F(4) | = | a b c | * | F(3) |
   |
   F(3) | | 1 0 0 | | F(2) |
   |
   F(2) | | 0 1 0 | | F(1) |

   - - - -2 times - - -
   |
   F(4) | = | a b c | * | a b c | * | F(2) |
   |
   F(3) | | 1 0 0 | | 1 0 0 | | F(1) |
   |
   F(2) | | 0 1 0 | | 0 1 0 | | F(0) |

   So
for n, the Equation(1) changes to

   -
   - - - - - - -n - 2 times - - - - -
   |
   F(n) | = | a b c | * | a b c | * ... * | a b c | * | F(2) |
   |
   F(n - 1) | | 1 0 0 | | 1 0 0 | | 1 0 0 | | F(1) |
   |
   F(n - 2) | | 0 1 0 | | 0 1 0 | | 0 1 0 | | F(0) |

   |
   F(n) | = [ | a b c | ] ^ (n - 2) * | F(2) |
   |
   F(n - 1) | [ | 1 0 0 | ] | F(1) |
   |
   F(n - 2) | [ | 0 1 0 | ] | F(0) |

Let us consider the problem of finding n’th term of a series defined using below recurrence. 

n 'th term,
F(n) = F(n - 1) + F(n - 2) + F(n - 3), n >= 3
Base Cases:
   F(0) = 0, F(1) = 1, F(2) = 1

We can find n’th term using following : 

Putting a = 1, b = 1 and c = 1 in above formula

   |
   F(n) | = [ | 1 1 1 | ] ^ (n - 2) * | F(2) |
   |
   F(n - 1) | [ | 1 0 0 | ] | F(1) |
   |
   F(n - 2) | [ | 0 1 0 | ] | F(0) |

F(5) is 7