This question already has answers here:
Speed of calculating powers (in python)
(6 answers)
Closed 7 years ago.
Read somewhere in my adventure in optimizing my mandelbrot program that if you are for example squaring something. Using the mandelbrot equation as the example that
z * z is faster than z**2. Is this true, and if it is why is this so? Is it due to the fact that z**2 still computes trivial cases such as 0?
I am one of the people who have suggested the optimization and yes, it is true. In the example below, squaring with * is over 3 times as fast as with **2.
from timeit import repeat
print(repeat('z*z', 'z=.54321 + .56789j'))
print(repeat('z**2', 'z=.54321 + .56789j'))
# prints
[0.07780417092306088, 0.05484807785182001, 0.05577646621573226]
[0.2081317598277747, 0.19060335713839965, 0.1913974058615736]
Since both operations give the same answer
>>> z*z
(-0.02742194800000003+0.6169670537999999j)
>>> z**2
(-0.02742194800000003+0.6169670537999999j)
the second must eventually do the first (plus whatever else it does.
Related
This question already has answers here:
How can I use "e" (Euler's number) and power operation?
(7 answers)
Closed 3 months ago.
What is the easiest/most optimal way of finding the exponential of a number, say x, in Python? i.e. how can I implement e^x?
The easiest and most optimal way to do e^x in Python is:
from math import exp
print(exp(4))
Output
>>> 54.598150033144236
You can use the math.exp() function from the math module (read the docs).
>>> import math
>>> x = 4
>>> print(math.exp(x))
54.598150033144236
This question already has answers here:
Repeating each element of a numpy array 5 times
(2 answers)
Closed 2 years ago.
I'm looking for a fast way to take a numpy array, such as [[1,2,3,4]]
and turn it into an extended version of itself with its elements repeated N times.
I.E. if N = 2, then [[1,2,3,4]] -> [[1,1,2,2,3,3,4,4]]
Obviously I can brute force it with an explicit for, but I'm wondering if this can be vectorized somehow to speed it up?
edit: i'm using [[1,2,3,4]] as shorthand for np.array([1,2,3,4]), sorry for the confusion. Also, thanks to those of you who mentioned np.repeat! That's just what I needed.
Try this:
from numpy import repeat
x = [[1,2,3,4]]
N = 3
y = repeat(x, N).reshape((1,-1))
print(y)
Edit: Quang's solution is shorter, I admit ...
y = repeat(x, N, axis=-1)
This question already has an answer here:
Running time/time complexity for while loop with square root
(1 answer)
Closed 4 years ago.
i=n
while i>1 :
i = int(math.sqrt(i))
print('*')
why is the time complixity of the code above log(log(n)) ??
(Assume sqrt() runs in O(1).)
Assume n can be coded with k bits.
After first iteration, i=√n and i can be coded on k/2 bits.
Hence the number of bits required to code i is divided by 2 at every step and the number of steps is log(k).
As k=log(n), complexity is log(log(n)), if we assume that sqrt complexity is one.
This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 6 years ago.
from math import pow
assert pow(-3, 2) == 9
assert -3 ** 2 == -9
why are the two above assertions valid?
in regular math, when a negative numbered is powered to 2, it becomes positive. which one of these is equal to the regular math I know?
is ignoring negative value the only difference between these two methods?
Its because of the order in which the operations are performed. In the first case, pow(-3,2) takes as inputs a -3 as first input and a 2 as the second input. In the second case, the ** has precedence over the -, so the order in which the operations are executed is
Calculate 3**2
Change the sign of the result
This leads to the result being -9.
Because python calculate the minus after it calculate the power.
In [2]: -3**2
Out[2]: -9
In [3]: (-3)**2
Out[3]: 9
This question already has answers here:
numpy convert number to byte
(2 answers)
Closed 4 years ago.
I have a numpy array and I want to force every element that is less than zero to be zero and every element above 255 will be forced down to 255.
eg. x = (-1,7,255,299) => (0,7,255,255)
Is there a not too complicated one-liner that can accomplish this?
The answer is numpy.clip
numpy.clip(x, 0, 255)
Regarding the question posted in your title: don't. You can apply the lambda function to every element, using vectorize but that's rarely the best choice.