exponentiation

Using exponentiation **0.5 less efficient than math.sqrt?

做~自己de王妃 提交于 2019-11-30 02:23:36
问题 A quote from "Python Programming: An Introduction to Computer Science" We could have taken the square root using exponentiation **. Using math.sqrt is somewhat more efficient. "Somewhat", but to what extent, and how? 回答1: Theoretically, hammar's answer and duffymo's answer are good guesses. But in practice, on my machine, it's not more efficient: >>> import timeit >>> timeit.timeit(stmt='[n ** 0.5 for n in range(100)]', setup='import math', number=10000) 0.15518403053283691 >>> timeit.timeit

Why does **0.5 appear to be more efficient than sqrt() [closed]

醉酒当歌 提交于 2019-11-29 12:34:37
I have tried measuring the speed of these two ways for taking square root: > system.time(expr = replicate(10000, 1:10000 ** (1/2))) ## user system elapsed ## 0.027 0.001 0.028 > system.time(expr = replicate(10000, sqrt(1:10000))) ## user system elapsed ## 3.722 0.665 4.494 If the sqrt() function cannot compete with ** 0.5 , why do we need such a function? (system is OS X Yusemite, and R version is 3.1.2) You forgot important parentheses. Here are the timings after correcting that: system.time(expr = replicate(10000, (1:10000) ** (1/2))) #user system elapsed #4.76 0.32 5.12 system.time(expr =

Exponentiation with negative base

泄露秘密 提交于 2019-11-29 11:05:20
So, the R expression and its output is as follows: > (4-7)^1.3 [1] NaN Any ideas how to solve this in R? The answer is a complex number, so you need to give it a complex argument: > (4-7+0i)^1.3 [1] -2.451751-3.374545i but remember this is only one root... I quote from Wikipedia, especially the bold text (http://en.wikipedia.org/wiki/Exponentiation): The IEEE 754-2008 floating point standard is used in the design of most > floating point libraries. It recommends a number of different functions for computing a power:[19] pow treats 00 as 1. This is the oldest defined version. If the power is an

Exponentiation in Python - should I prefer ** operator instead of math.pow and math.sqrt? [duplicate]

放肆的年华 提交于 2019-11-29 10:32:12
问题 This question already has answers here : Which is faster in Python: x**.5 or math.sqrt(x)? (14 answers) Closed 6 years ago . In my field it's very common to square some numbers, operate them together, and take the square root of the result. This is done in pythagorean theorem, and the RMS calculation, for example. In numpy, I have done the following: result = numpy.sqrt(numpy.sum(numpy.pow(some_vector, 2))) And in pure python something like this would be expected: result = math.sqrt(math.pow

Exponentiation operator in Swift

北城余情 提交于 2019-11-28 19:59:59
I don't see an exponentiation operator defined in the base arithmetic operators in the Swift language reference. Is there really no predefined integer or float exponentiation operator in the language? There isn't an operator but you can use the pow function like this: return pow(num, power) If you want to, you could also make an operator call the pow function like this: infix operator ** { associativity left precedence 170 } func ** (num: Double, power: Double) -> Double{ return pow(num, power) } 2.0**2.0 //4.0 chanceoperation If you happen to be raising 2 to some power, you can use the

Why is exponentiation applied right to left?

♀尐吖头ヾ 提交于 2019-11-28 14:09:27
I am reading an Intro to Python textbook and came across this line: Operators on the same row have equal precedence and are applied left to right, except for exponentiation, which is applied right to left. I understand most of this, but I do not understand why they say exponentiation is applied right to left. They do not provide any examples either. Also, am I allowed to ask general questions like this, or are only problem solving questions preferred? The ** operator follows normal mathematical conventions ; it is right-associative: In the usual computer science jargon, exponentiation in

Prolog predicate - infinite loop

喜你入骨 提交于 2019-11-28 13:47:18
I need to create a Prolog predicate for power of 2, with the natural numbers. Natural numbers are: 0, s(0), s(s(0)) ans so on.. For example: ?- pow2(s(0),P). P = s(s(0)); false. ?- pow2(P,s(s(0))). P = s(0); false. This is my code: times2(X,Y) :- add(X,X,Y). pow2(0,s(0)). pow2(s(N),Y) :- pow2(N,Z), times2(Z,Y). And it works perfectly with the first example, but enters an infinite loop in the second.. How can I fix this? This happends because the of evaluation order of pow2. If you switch the order of pow2, you'll have the first example stuck in infinite loop.. So you can check first if Y is a

how to calculate 2^n modulo 1000000007 , n = 10^9

蹲街弑〆低调 提交于 2019-11-28 12:28:38
what is the fastest method to calculate this, i saw some people using matrices and when i searched on the internet, they talked about eigen values and eigen vectors (no idea about this stuff)...there was a question which reduced to a recursive equation f(n) = (2*f(n-1)) + 2 , and f(1) = 1, n could be upto 10^9.... i already tried using DP, storing upto 1000000 values and using the common fast exponentiation method, it all timed out im generally weak in these modulo questions, which require computing large values f(n) = (2*f(n-1)) + 2 with f(1)=1 is equivalent to (f(n)+2) = 2 * (f(n-1)+2) = ...

Integer exponentiation in OCaml

為{幸葍}努か 提交于 2019-11-28 11:00:33
Is there a function for integer exponentiation in OCaml? ** is only for floats. Although it seems to be mostly accurate, isn't there a possibility of precision errors, something like 2. ** 3. = 8. returning false sometimes? Is there a library function for integer exponentiation? I could write my own, but efficiency concerns come into that, and also I'd be surprised if there isn't such a function already. Regarding the floating-point part of your question: OCaml calls the underlying system's pow() function. Floating-point exponentiation is a difficult function to implement, but it only needs to

Efficient Exponentiation For HUGE Numbers (I'm Talking Googols)

Deadly 提交于 2019-11-28 05:50:07
问题 I am in the midst of solving a simple combination problem whose solution is 2^(n-1). The only problem is 1 <= n <= 2^31 -1 (max value for signed 32 bit integer) I tried using Java's BigInteger class but It times out for numbers 2^31/10^4 and greater, so that clearly doesn't work out. Furthermore, I am limited to using only built-in classes for Java or C++. Knowing I require speed, I chose to build a class in C++ which does arithmetic on strings. Now, when I do multiplication, my program