Optimization in scipy from sympy
<p>I have four functions symbolically computed with Sympy and then lambdified:</p>

<pre><code>deriv_log_s_1 = sym.lambdify((z, m_1, m_2, s_1, s_2), deriv_log_sym_s_1, modules=['numpy', 'sympy'])
deriv_log_s_2 = sym.lambdify((z, m_1, m_2, s_1, s_2), deriv_log_sym_s_2, modules=['numpy', 'sympy'])
deriv_log_m_1 = sym.lambdify((z, m_1, m_2, s_1, s_2), deriv_log_sym_m_1, modules=['numpy', 'sympy'])
deriv_log_m_2 = sym.lambdify((z, m_1, m_2, s_1, s_2), deriv_log_sym_m_2, modules=['numpy', 'sympy'])
</code></pre>

<p>From these functions, I define a cost function to optimize:</p>

<pre><code>def cost_function(x, *args):

    m_1, m_2, s_1, s_2 = x     

    print(args[0])    

    T1 = np.sum([deriv_log_m_1(y, m_1, m_2, s_1, s_2) for y in args[0]])   
    T2 = np.sum([deriv_log_m_2(y, m_1, m_2, s_1, s_2) for y in args[0]]) 


    T3 = np.sum([deriv_log_m_1(y, m_1, m_2, s_1, s_2) for y in args[0]])   
    T4 = np.sum([deriv_log_m_1(y, m_1, m_2, s_1, s_2) for y in args[0]])   

    return T1 + T2 + T3 + T4
</code></pre>

<p>My function <code>cost_function</code> works as expected:    </p>

<pre><code>a = 48.7161
b = 16.3156
c = 17.0882
d = 7.0556
z = [0.5, 1, 2, 1.2, 3]

test = cost_function(np.array([a, b, c, d]).astype(np.float32), z)
</code></pre>

<p>However, when I try to optimize it:</p>

<pre><code>from scipy.optimize import fmin_powell

res = fmin_powell(cost_function, x0=np.array([a, b, c, d], dtype=np.float32), args=(z, ))
</code></pre>

<p>It raises the following error:</p>

<pre><code>AttributeError: 'Float' object has no attribute 'sqrt'
</code></pre>

<p>I do not understand why such an error appears as my <code>cost_function</code> alone does not raise any error.</p>

<p>The solution was, and I do not know why, to cast inputs to numpy.float:</p>

<pre><code>m_1 = np.float32(m_1)
m_2 = np.float32(m_2)
s_1 = np.float32(s_1)
s_2 = np.float32(s_2)
</code></pre>

python numpy scipy sympy