Numpy and numdifftools multidimensional function - python

I'm using the package numddifftools to calculate the hessians of a multidimensional function (from R^n to R^n). While changing the code to use numpy arrays instead of lists, I discovered that doing so broke the code. Specifically:
import numpy as np
import numdifftools as nd
def function(x):
out = np.zeros(2)
out[0] = x[0] - x[1]**2/2.0
return float(out[0])
tempHessian = nd.Hessian(function, method='complex')
tempHessian([0.4,0.4])
Produces the error:
...\Continuum\Anaconda3\lib\site-packages\ipykernel_launcher.py:8: ComplexWarning: Casting complex values to real discards the imaginary part
and gives a zero hessian.
However, this one works fine:
import numpy as np
import numdifftools as nd
def function(x):
return x[0] - x[1]**2/2.0
tempHessian = nd.Hessian(function, method='complex')
tempHessian([0.4,0.4])
Any ideas what could be the problem? Thanks!

When out is created like this:
out = np.zeros(2)
it has type numpy.float64. You can't assign a complex value to such an array. Depending on which version of numpy that you are using, you'll get a warning or an error.
The complex step method for numerical differentiation requires that your function works with complex values. Forcing out to be numpy.float64 breaks that method. Converting the return value to floating point with float(out[0]) also breaks the method.
You can try something like this:
def function(x):
out = np.zeros(2, dtype=x.dtype)
out[0] = x[0] - x[1]**2/2.0
return out[0]
This creates out with the same data type as x. So if x is complex, the return value is also complex, as required by the complex step method.
(Of course, we don't know why you create out to have size 2, when only the first element is used. Presumably this is a simplification of the actual code that you are working with.)

Related

Can I skip the complex(imaginary results) in a numpy array?

Suppose I have a function like sqrt(x) and I am passing a list of numbers through it [-1,0,1].
This is going to give me an error saying that a negative argument is passed inside the square root function and the program is going to be halted after that without even checking for the 0 and 1 for which I would have gotten the real and "allowed" results in the first place.
Is there a way I can command python to ignore the values for which it would give errors and move on to the next part of the list?
Perhaps an algorithm like:
Start
x = [-1,0,1]
Pass this list through the function sqrt(x)
Ignore the values for which the function is going to the complex regime
Give the results ([-,0,1])
End
?
Thanks.
#matszwecja's answer assumes you can tell a priori which arguments give complex-valued answers. This is true for your sqrt example, but not in the general case. To filter after execution, a slight remix would be:
import numpy as np
arguments = [-1, 0, 1]
results = [x ** 0.5 for x in arguments] # stand in for your more general function
# use numpy for the filtering, for convenience
real_valued_args = np.array(arguments)[~np.iscomplex(results)]
import numpy as np
x = [-1, 0, 1]
print(np.sqrt(x))
numpys sqrt function is not throwing an error and returning only valid results.
Just do an explicit filter of your data:
import numpy as np
arr = np.array([-1,0,1])
filtered_arr = arr[arr >= 0]
print(np.sqrt(filtered_arr))

How to create a loop to evaluate a conditional function

I am attempting to create a loop that will evaluate a function at discrete values. Normally this will be okay, however, this is proving to be difficult as my function is conditional (it has an adittional piecewise function which is dependent on the main variable.) This is an attempt I made below;
import math
import numpy as np
for i in np.arange(4000,8000,1000):
def f(λ,a,u,o1,o2):
o = o1 if (λ <= u) else o2
return a*math.exp((λ-u)^2/(-2*o^2))
print(f(i,1.056,5998,379,310))
I expected the code to evaluate the function at i=4000, then i=5000 etc. The output I recieve is as follows;
TypeError: ufunc 'bitwise_xor' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
I can't seem to find any specific examples for implementing a conditional function like this. Another attempt was made without a loop but I ran into value errors where I believe I was evaluating arrays as if they were one variable.
I wish to integrate and plot sums of the function later. Will I run into any issues with an approach involving a loop?
Python uses ** for exponentiation. ^ is bitwise xor.
Assuming your function is correct apart from the power operator **, a possible implementation with numpy would look like this
# import math # does not work well with numpy
import numpy as np
def f(l,a,u,o1,o2): # change 'λ' to 'l' (it's easier to type)
o = np.where(l <= u, o1, o2) # vectorize piecewise definition
return a*np.exp((l-u)**2/(-2*o**2)) # change '^' to '**' operator, use np.exp
x = np.arange(4000,8000,1000) # define value range
print(f(x,1.056,5998,379,310)) # call your function one time with entire range
Output
[9.74560013e-07 3.29586202e-02 1.05597802e+00 5.68878883e-03]
To reuse your function you can change it into a parameterized version
import numpy as np
def f(a,u,o1,o2):
return lambda x: a*np.exp((x-u)**2/(-2*np.where(x <= u, o1, o2)**2))
x = np.arange(4000,8000,1000)
f_p = f(1.056,5998,379,310) # parameterize your function
print(f_p(x)) # call the parameterized function with your value range
Output
[9.74560013e-07 3.29586202e-02 1.05597802e+00 5.68878883e-03]
import math
import numpy as np
def f(λ,a,u,o1,o2):
o = o1 if (λ <= u) else o2
return a*math.exp((λ-u)^2/(-2*o^2))
for i in np.arange(4000,8000,1000):
print(f(i,1.056,5998,379,310))
Try this code. This will work

Why do I keep getting this error 'RuntimeWarning: overflow encountered in int_scalars'

I am trying to multiply all the row values and column values of a 2 dimensional numpy array with an explicit for-loop:
product_0 = 1
product_1 = 1
for x in arr:
product_0 *= x[0]
product_1 *= x[1]
I realize the product will blow up to become an extremely large number but from my previous experience python has had no memory problem dealing very very extremely large numbers.
So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.
Any idea how to fix this?
Using non inplace multiplication hasn't helped product_0 = x[0]*product_0
Python int are represented in arbitrary precision, so they cannot overflow. But numpy uses C++ under the hood, so the highest long signed integer is with fixed precision, 2^63 - 1. Your number is far beyond this value, having in average ((716-1)/2)^86507).
When you, in the for loop, extract the x[0] this is still a numpy object. To use the full power of python integers you need to clearly assign it as python int, like this:
product_0 = 1
product_1 = 1
for x in arr:
t = int(x[0])
product_0 = product_0 * t
and it will not overflow.
Following your comment, which makes your question more specific, your original problem is to calculate the geometric mean of the array for each row/column. Here the solution:
I generate first an array that has the same properties of your array:
arr = np.resize(np.random.randint(1,716,86507*2 ),(86507,2))
Then, calculate the geometric mean for each column/row:
from scipy import stats
gm_0 = stats.mstats.gmean(arr, axis = 0)
gm_1 = stats.mstats.gmean(arr, axis = 1)
gm_0 will be an array that contains the geometric mean of the xand y coordinates. gm_1 instead contains the geometric mean of the rows.
Hope this solves your problem!
You say
So from what I can tell this is a problem with numpy except I am not storing the gigantic product in a numpy array or any numpy data type for that matter its just a normal python variable.
Your product may not be a NumPy array, but it is using a NumPy data type. x[0] and x[1] are NumPy scalars, and multiplying a Python int by a NumPy scalar produces a NumPy scalar. NumPy integers have a finite range.
While you technically could call int on x[0] and x[1] to get a Python int, it'd probably be better to avoid needing such huge ints. You say you're trying to perform this multiplication to compute a geometric mean; in that case, it'd be better to compute the geometric mean by transforming to and from logarithms, or to use scipy.stats.mstats.gmean, which uses logarithms under the hood.
Numpy is compiled for 32 bit and not 64 bit , so while Python can handle this numpy will overflow for smaller values , if u want to use numpy then I recommend that you build it from source .
Edit
After some testing with
import numpy as np
x=np.abs(np.random.randn(1000,2)*1000)
np.max(x)
prod1=np.dtype('int32').type(1)
prod2=np.dtype('int32').type(1)
k=0
for i,j in x:
prod1*=i
prod2*=j
k+=1
print(k," ",prod1,prod2)
1.797693134e308 is the max value (to this many digits my numpy scalar was able to take)
if you run this you will see that numpy is able to handle quite a large value but when you said your max value is around 700 , even with a 1000 values my scalar overflowed.
As for how to fix this , rather than doing this manually the answer using scipy seems more viable now and is able to get the answer so i suggest that you go forward with that
from scipy.stats.mstats import gmean
x=np.abs(np.random.randn(1000,2)*1000)
print(gmean(x,axis=0))
You can achieve what you want with the following command in numpy:
import numpy as np
product_0 = np.prod(arr.astype(np.float64))
It can still reach np.inf if your numbers are large enough, but that can happen for any type.

Python Numba Polynomial Root Lower Error with Sympy

I have created a function which, given ranges of coefficients, constructs polynomials with such coefficients and outputs a list of all their roots. However, Numba doesn't like it. It's like this:
import math
import numpy as np
import itertools
from numba import jit
from sympy.solvers import solve
from sympy import Symbol
from sympy import Poly
#jit
def polyn(ranges=[[-20,20],[-20,20],[-20,20],[-20,20]],step=4):
l = []
x = Symbol('x')
rangl = [np.linspace(i[0],i[1],math.floor((i[1]-i[0])/step)) for i in ranges]
coeffl = iter(itertools.product(*rangl))
leng = 1
for i in rangl:
leng *= len(i)
for i in range(0, leng):
a = solve(Poly(list(next(coeffl)),x),x)
for j in a:
l.append(j)
return np.array(l)
When I try to run this, it outputs a cryptic:
AssertionError: Failed at object (object mode frontend)
which I do not understand... Can anyone help?
There are a number of things in your code which Numba can't cope with currently. The first is the list comprehension where you build rangl:
[np.linspace(i[0],i[1],math.floor((i[1]-i[0])/step)) for i in ranges]
You should replace this with a NumPy solution like:
rangl = np.empty((len(ranges), step))
for i in ranges:
rangl[i] = np.linspace(i[0],i[1],math.floor((i[1]-i[0])/step))
The second thing Numba can't cope with is itertools.product. You can replace that with NumPy and a for loop as well.
In general, try to reduce your code by commenting out the lower part of it until you get Numba to accept it, then work from the top down and see which parts it can't compile. Be methodical, go step by step, and try to stick to simple constructs like simple for loops and arrays.

Converting expression involving tranpose of vector to numerical function with lambdify

I have written a script in python that uses sympy to compute a couple of vector/matrix formulas. However, when I try to convert those to functions that I can evaluate with sympy.lambdify, I get a
SyntaxError : EOL while scanning string literal
Here's some code with the same error, so that you can see what I mean.
import sympy
x = sympy.MatrixSymbol('x',3,1)
f = sympy.lambdify(x, x.T*x)
So, the syntax error has to do with the expression "x'.dot(x)" and the conversion of ".T" to '.
How can I work around this to correctly define f from the above lambdify?
Found a work around, although not the cleanest looking solution... but it works.
Use the implemented_function() method from sympy to define your function. Read full documentation here: http://docs.sympy.org/latest/modules/utilities/lambdify.html
Here is the code:
import sympy
import numpy as np
from sympy.utilities.lambdify import implemented_function
x = sympy.MatrixSymbol('x',3,1)
f = implemented_function(sympy.Function('f'), lambda x: x.T*x)
lam_f= sympy.lambdify(x, f(x))
Hope this solves your problem :)
It has been solved in sympy version >= 1.1
Edit:
Example
when u define this
x = sympy.MatrixSymbol('x',3,1)
you are creating a matrix,
you can check its indexing and shape using
print(sympy.Matrix(x))
Now that you want to multiply Transpose of x to x, you will have to give x a matrix of same shape that you have defined before
here try this:
from sympy import MatrixSymbol, lambdify, Matrix
x = MatrixSymbol('x', 3, 1)
f = lambdify(x, x.T*x)
a = Matrix([[1], [2], [3]])
print(f(a))
you can check this link out to understand lambdify better:
http://docs.sympy.org/latest/modules/utilities/lambdify.html

Categories