exponentiation

How to do exponentiation in clojure?

回眸只為那壹抹淺笑 提交于 2019-12-02 15:41:20
How can I do exponentiation in clojure? For now I'm only needing integer exponentiation, but the question goes for fractions too. classic recursion (watch this, it blows stack) (defn exp [x n] (if (zero? n) 1 (* x (exp x (dec n))))) tail recursion (defn exp [x n] (loop [acc 1 n n] (if (zero? n) acc (recur (* x acc) (dec n))))) functional (defn exp [x n] (reduce * (repeat n x))) sneaky (also blows stack, but not so easily) (defn exp-s [x n] (let [square (fn[x] (* x x))] (cond (zero? n) 1 (even? n) (square (exp-s x (/ n 2))) :else (* x (exp-s x (dec n)))))) library (require 'clojure.contrib.math

Vectorized exponentiation

爱⌒轻易说出口 提交于 2019-12-02 04:02:00
I have two vectors, X of bases and N of exponents. I want to get the matrix of all values e = x n for each x in X and n in N . For example, the following input: X = [2 3 4]' N = [1 2 3] should produce: ans = [2 4 8; 3 9 27; 4 16 64] Is there a way to get this without looping (just like you can get all values of x×n by using the column by row product)? Use bsxfun : bsxfun(@power, X, N) This assumes that X is a column vector and N is a row vector. If you want to guarantee that, use the following syntax which is more robust: bsxfun(@power, X(:), N(:).') This is probably a bit sloppier than the

JavaScript exponentiation unary operator design decision

人走茶凉 提交于 2019-12-01 19:44:40
So I was fooling around with the new exponentiation operator and I discovered you cannot put a unary operator immediately before the base number. let result = -2 ** 2; // syntax error let result = -(2 ** 2); // -4 let x = 3; let result = --x ** 2; // 4 From the documentation on MDN : In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator ( + / - / ~ / ! / delete / void / typeof ) immediately before the base number. In most languages like PHP and Python and others that have an exponentiation operator (typically ^ or ** ), the

Why does numpy.power return 0 for small exponents while math.pow returns the correct answer?

回眸只為那壹抹淺笑 提交于 2019-12-01 15:34:49
In [25]: np.power(10,-100) Out[25]: 0 In [26]: math.pow(10,-100) Out[26]: 1e-100 I would expect both the commands to return 1e-100. This is not a precision issue either, since the issue persists even after increasing precision to 500. Is there some setting which I can change to get the correct answer? Oh, it's much "worse" than that: In [2]: numpy.power(10,-1) Out[2]: 0 But this is a hint to what's going on: 10 is an integer, and numpy.power doesn't coerce the numbers to floats. But this works: In [3]: numpy.power(10.,-1) Out[3]: 0.10000000000000001 In [4]: numpy.power(10.,-100) Out[4]: 1e-100

power function in prolog

不问归期 提交于 2019-12-01 04:07:36
What is wrong with my power function? pow(_,0,1). pow(X,Y,Z) :- pow(X,Y-1,X*Z). ?- pow(2,3,Z). ERROR: Out of global stack Your Y does not get decremented, you can not use predicates like functions. You also have to unify Z with the result of the multiplication. pow(_,0,1). pow(X,Y,Z) :- Y1 is Y - 1, pow(X,Y1,Z1), Z is Z1*X. There is also a builtin power function which will be much faster: pow2(X,Y,Z) :- Z is X**Y. Also note that pow is not a last call and can not be optimized to use only one stack frame. You should reformulate it to: pow3(X,Y,Z) :- powend(X,Y,1,Z),!. powend(_,0,A,Z) :- Z is A.

Numpy matrix exponentiation gives negative value

℡╲_俬逩灬. 提交于 2019-12-01 03:55:41
问题 I wanted to use NumPy in a Fibonacci question because of its efficiency in matrix multiplication. You know that there is a method for finding Fibonacci numbers with the matrix [[1, 1], [1, 0]] . I wrote some very simple code but after increasing n , the matrix is starting to give negative numbers. import numpy def fib(n): return (numpy.matrix("1 1; 1 0")**n).item(1) print fib(90) # Gives -1581614984 What could be the reason for this? Note: linalg.matrix_power also gives negative values. Note2

Element-wise power of scipy.sparse matrix

五迷三道 提交于 2019-12-01 02:31:08
How do I raise a scipy.sparse matrix to a power, element-wise? numpy.power should, according to its manual , do this, but it fails on sparse matrices: >>> X <1353x32100 sparse matrix of type '<type 'numpy.float64'>' with 144875 stored elements in Compressed Sparse Row format> >>> np.power(X, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> File ".../scipy/sparse/base.py", line 347, in __pow__ raise TypeError('matrix is not square') TypeError: matrix is not square Same problem with X**2 . Converting to a dense array works, but wastes precious seconds. I've had the same

power function in prolog

时光怂恿深爱的人放手 提交于 2019-12-01 01:50:40
问题 What is wrong with my power function? pow(_,0,1). pow(X,Y,Z) :- pow(X,Y-1,X*Z). ?- pow(2,3,Z). ERROR: Out of global stack 回答1: Your Y does not get decremented, you can not use predicates like functions. You also have to unify Z with the result of the multiplication. pow(_,0,1). pow(X,Y,Z) :- Y1 is Y - 1, pow(X,Y1,Z1), Z is Z1*X. There is also a builtin power function which will be much faster: pow2(X,Y,Z) :- Z is X**Y. Also note that pow is not a last call and can not be optimized to use only

Element-wise power of scipy.sparse matrix

我是研究僧i 提交于 2019-11-30 22:06:44
问题 How do I raise a scipy.sparse matrix to a power, element-wise? numpy.power should, according to its manual, do this, but it fails on sparse matrices: >>> X <1353x32100 sparse matrix of type '<type 'numpy.float64'>' with 144875 stored elements in Compressed Sparse Row format> >>> np.power(X, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> File ".../scipy/sparse/base.py", line 347, in __pow__ raise TypeError('matrix is not square') TypeError: matrix is not square Same

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

落爺英雄遲暮 提交于 2019-11-30 18:03:46
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? senderle 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(stmt='[math.sqrt(n) for n in range(100)]', setup='import math', number=10000) 0.17707490921020508