I'm trying to use fsolve in combination with the mpmath package.
However, I get the error cannot create mpf from array([mpf('1.0')], dtype=object).
Here is a minimal example reproducing the error. For this example, I technically do not need the mpmath package, but my actual function contains hyperconfluent functions that do.
from scipy.optimize import fsolve
#from mpmath import hyp1f1 as hyp1f1mp
#from mpmath import gamma as gammamp
import mpmath as mp
#import numpy as np
mp.dps = 250; mp.pretty = True
def cosFunc(p):
vn = p
output = mp.sin(vn)
return output
estimate = mp.mpf(1)
value = fsolve(cosFunc,estimate)
print value
I found a similar question suggesting to use np.frompyfunc (How to mpf an array?), but it tells me that the function is not callable (when I apply it on vn).
The trick is to apply np.frompyfunc to a function instead of a value. I think the following modification would make your function work:
def cosFunc(p):
vn = p
np_sin = np.frompyfunc(mp.sin, 1, 1)
output = np_sin(vn)
return float(output)
value = fsolve(cosFunc, 1)
print value
The specific cause of the error you is this:
(Pdb) x0
array([mpf('1.0')], dtype=object)
(Pdb) mp.sin(x0)
*** TypeError: cannot create mpf from array([mpf('1.0')], dtype=object)
What happens is that fsolve tries to convert your estimate to array and numpy does not know how to handle mpmath objects.
>>> np.asarray(mp.mpf(1))
>>> array(mpf('1.0'), dtype=object)
Changing how fsolve works is not very productive, so your best bet seems to be to teach your function to handle arrays of mpmath objects
def cos_func(p):
vn = p
if isinstance(p, np.ndarray):
if p.size == 0:
vn = p[0]
else:
raise ValueError # or whatever you want to do here"
return mp.sin(vn)
Related
I am supposed to use python to solve a Matrix A^1000. I have tried numpy's built in matrix_power function to compute the output, but when I try to compute the result step by step according to the formula P*(D^1000) *P^-1 , I get an incomplete result. I am trying to figure out if my precision options are just too tight or if I am doing something else wrong completely
I have tried the matrix_power function to definitely get the result I want. But I need to be able to show how the calculation is done, and when I do so, I get an incomplete result
import numpy as np
np.set_printoptions(precision=6) # set the precision of the output
np.set_printoptions(suppress=True) # suppress the use of scientific notation
from numpy import diag, allclose, corrcoef
from numpy.random import randint, randn
from numpy.linalg import eig, matrix_rank, inv, cholesky, qr, norm, matrix_power
from sympy import Matrix, init_printing, matrix2numpy
A = np.array([[0.9,0.15,0.25],[0.075,0.8,0.25],[0.025,0.05,0.5]])
A
#python way
A_1000 = matrix_power(A,1000)
A_1000
D , P = eig(A)
P * np.diag(D**1000) * np.linalg.inv(p)
According to Python Documentation a TypeError is defined as
Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.
exception TypeError
The reason I got this Error was because my code looked like this:
import math as m
import pylab as pyl
import numpy as np
#normal distribution function
def normal(x,mu,sigma):
P=(1/(m.sqrt(2*m.pi*sigma**2)))*(m.exp((-(x-mu)**2)/2*sigma**2))
return P
#solution
x = np.linspace(-5,5,1000)
P = normal(x,0,1)
#plotting the function
pyl.plot(x,P)
pyl.show()
P=(1/(m.sqrt(2***m**.pisigma2)))(**m.exp((-(x-mu)2)/2*sigma2))
Notice the m. - This is incorrect, because math. can only handle scalars. And the Error said that a TypeError had occurred.
np. (Numpy) can handle scalers as well as arrays and the problem is solved.
The right code looks like this:
import math as m
import pylab as pyl
import numpy as np
# normal distribution function
def normal(x,mu,sigma):
P = (1/(np.sqrt(2*np.pi*sigma**2))) * (np.exp((-(x-mu)**2)/2*sigma**2))
return P
# solution
x = np.linspace(-5,5,1000)
P = normal(x,0,1)
# plotting the function
pyl.plot(x,P)
pyl.show()
In the end we get a great normal distribution function that looks like this:
This Error occurred in Spyder IDE.
I have a rather complicated function H(x), and I'm trying to solve for the value of x such that H(x) = constant. I would like to do this with an interpolation object generated from a discrete interval and the corresponding output of H(interval), where other inputs are held constant. I denote the interpolation object f.
My problem is that the call function of the interpolation object accepts an array_like, so passing a symbol to f(x) to use sage's solver method is out of the question. Any ideas of how to get around this?
I have interpolation function f. I would like to solve the equation f(x) == sageconstant forx.
from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
import numpy as np
#Generating my interpolation object
xint = srange(30,200,step=.1)
val = [H(i,1,.1,0,.2,.005,40) for i in srange(30,299,step=.1)]
f = IUspline(xint,val,k=4)
#This will yield a sage constant
eq_G(x) = freeB - x
#relation that I would like to solve
eq_m(x) = eq_G(39.9) == f(x)
m = solve(eq_m(x),x)
The above code (f(x) to be more specific) generates
"TypeError: Cannot cast array data from dtype('0') to dtype('float64')
according to the rule 'safe'.
edit: Any function H(x) will result in the same error, hence it doesn't matter what H(x) is. For simplicity (I wasn't kidding when I said H is complicated), try H(x) = x. Then the block will read:
from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
import numpy as np
#Generating my interpolation object
xint = srange(30,200,step=.1)
H(x) = x
val = [H(i) for i in srange(30,299,step=.1)]
f = IUspline(xint,val,k=4)
#This will yield a sage constant
eq_G(x) = freeB - x
#relation that I would like to solve
eq_m(x) = eq_G(39.9) == f(x)
m = solve(eq_m(x),x)
When working with numpy and scipy, prefer Python types to Sage types.
Instead of Sage Integers and Reals, use Python ints and floats.
Maybe you can fix your code like this.
from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
import numpy as np
# Generate interpolation object
xint = srange(30,200,step=.1)
xint = [float(x) for x in xint]
val = [float(H(i,1,.1,0,.2,.005,40)) for i in srange(30,299,step=.1)]
f = IUspline(xint,val,k=4)
# This will yield a Sage constant
eq_G(x) = freeB - x
# relation that I would like to solve
eq_m(x) = eq_G(39.9) == f(x)
m = solve(eq_m(x),x)
I'm trying to generate random variables according to a certain ugly distribution, in Python. I have an explicit expression for the PMF, but it involves some products which makes it unpleasant to obtain and invert the CDF (see below code for explicit form of PMF).
In essence, I'm trying to define a random variable in Python by its PMF and then have built-in code do the hard work of sampling from the distribution. I know how to do this if the support of the RV is finite, but here the support is countably infinite.
The code I am currently trying to run as per #askewchan's advice below is:
import scipy as sp
import numpy as np
class x_gen(sp.stats.rv_discrete):
def _pmf(self,k,param):
num = np.arange(1+param, k+param, 1)
denom = np.arange(3+2*param, k+3+2*param, 1)
p = (2+param)*(np.prod(num)/np.prod(denom))
return p
pa_limit = limitrv_gen()
print pa_limit.rvs(alpha,n=1)
However, this returns the error while running:
File "limiting_sim.py", line 42, in _pmf
num = np.arange(1+param, k+param, 1)
TypeError: only length-1 arrays can be converted to Python scalars
Basically, it seems that the np.arange() list isn't working somehow inside the def _pmf() function. I'm at a loss to see why. Can anyone enlighten me here and/or point out a fix?
EDIT 1: cleared up some questions by askewchan, edits reflected above.
EDIT 2: askewchan suggested an interesting approximation using the factorial function, but I'm looking more for an exact solution such as the one that I'm trying to get work with np.arange.
You should be able to subclass rv_discrete like so:
class mydist_gen(rv_discrete):
def _pmf(self, n, param):
return yourpmf(n, param)
Then you can create a distribution instance with:
mydist = mydist_gen()
And generate samples with:
mydist.rvs(param, size=1000)
Or you can then create a frozen distribution object with:
mydistp = mydist(param)
And finally generate samples with:
mydistp.rvs(1000)
With your example, this should work, since factorial automatically broadcasts. But, it might fail for large enough alpha:
import scipy as sp
import numpy as np
from scipy.misc import factorial
class limitrv_gen(sp.stats.rv_discrete):
def _pmf(self, k, alpha):
#num = np.prod(np.arange(1+alpha, k+alpha))
num = factorial(k+alpha-1) / factorial(alpha)
#denom = np.prod(np.arange(3+2*alpha, k+3+2*alpha))
denom = factorial(k + 2 + 2*alpha) / factorial(2 + 2*alpha)
return (2+alpha) * num / denom
pa_limit = limitrv_gen()
alpha = 100
pa_limit.rvs(alpha, size=10)
Dear Stackoverflow Community,
I am very new to Python and to programming in general, so please don't get mad when I don't get your answers and ask again.
I am trying to fit a curve to experimental data with scipy.optimization.curve_fit. This is my code:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as nm
from __future__ import division
import cantera as ct
from matplotlib.backends.backend_pdf import PdfPages
import math as ma
import scipy.optimize as so
R = 8.314
T = nm.array([700, 900, 1100, 1300, 1400, 1500, 1600, 1700])
k = nm.array([289, 25695, 763059, 6358040, 14623536, 30098925, 56605969, 98832907])
def func(A, E, T):
return A*ma.exp(-E/(R*T))
popt, pcov = so.curve_fit(func, T, k)
Now this code works for me, but if I change the function to:
def func(T, A, E)
and keep the rest I get:
TypeError: only length-1 arrays can be converted to Python scalars
Also I am not really convinced by the Parameter solution of the first one.
Can anyone tell me what happens when you change the variable order?
I got the same problem and found the cause and its solution:
The problem lies on the implementation of Scipy. After the optimal parameter has been found, Scipy calls your function with the input array xdata as first argument. That is, it calls func(xdata, *args), and the function complains with a type error because xdata is not an scalar. For example:
from math import erf
erf([1, 2]) # TypeError
erf(np.array([1, 2])) # TypeError
To avoid the error, you can add custom code for supporting arrays, or better, as suggested in the answer of Joris, use numpy functions because they have support for scalars and arrays.
If the math function is not in numpy , like erf or any custom function you coded, then I recommend you instead of doing from math import erf, to do as follows:
from math import erf as math_erf # only supports scalars
import numpy as np
erf = np.vectorize(math_erf) # adds array support
def fit_func(t,s):
return 0.5*(1.0-erf(t/(np.sqrt(2)*s)))
X = np.linspace(-5,5,1000)
Y = np.array([fit_func(x,1) for x in X])
curve_fit(fit_func, X, Y)
The curve_fit function from scipy does not handle very well embedded functions from the math module. When you change the exponential function to the numpy exponential function you don't get the error:
def func(A, E, T):
return A*np.exp(-E/(R*T))
I wonder whether you data shows an exponential decay of rate. The mathematical model may not be the most suitable one.
See the doc string of curve_fit
f : callable
The model function, f(x, ...). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.
since your formula is essentially: k=A*ma.exp(-E/(R*T)), the right order of parameters in func should be (T, A, E) or (T, E, A).
Regarding the order of A and E, they don't really matter. If you flip them, the result will get flipped as well:
>>> def func(T, A, E):
return A*ma.exp(-E/(R*T))
>>> so.curve_fit(func, T, k)
(array([ 8.21449078e+00, -5.86499656e+04]), array([[ 6.07720215e+09, 4.31864058e+12],
[ 4.31864058e+12, 3.07102992e+15]]))
>>> def func(T, E, A):
return A*ma.exp(-E/(R*T))
>>> so.curve_fit(func, T, k)
(array([ -5.86499656e+04, 8.21449078e+00]), array([[ 3.07102992e+15, 4.31864058e+12],
[ 4.31864058e+12, 6.07720215e+09]]))
I didn't get your typeerror at all.