Using minimise function ('SLSQP' method) in sympy with free and fixed parameters - python

I am still a beginner in python, so I am sorry if this is too trivial. I want to calculate the minimum value of a function which has 12 variables in total. Of these 12 variables, 10 are fixed at a given value and the remaining 2 is left free to compute the minimum. Here is an example of my code.
import numpy as np
from sympy import *
from scipy.optimize import minimize
init_printing(use_unicode=True)
X_1,X_2,Y_1,Y_2,X_c1,X_c2,Y_c1,Y_c2,a_1,a_2,b_1,b_2,t_1,t_2,psi_1,psi_2= symbols('X_1 X_2 Y_1 Y_2 X_c1 X_c2 Y_c1 Y_c2 a_1 a_2 b_1 b_2 t_1 t_2 psi_1 psi_2')
X_1=X_c1 + (a_1 * cos(t_1) * cos(psi_1)) - ((b_1) * sin(t_1)* sin(psi_1))
X_2=X_c2 + (a_2 * cos(t_2) * cos(psi_2)) - ((b_2) * sin(t_2)* sin(psi_2))
Y_1=Y_c1 + (a_1 * cos(t_1) * sin(psi_1)) + ((b_1) * sin(t_1)* cos(psi_1))
Y_2=Y_c2 + (a_2 * cos(t_2) * sin(psi_2)) + ((b_2) * sin(t_2)* sin(psi_2))
param=(t_1,t_2,X_c1,X_c2,Y_c1,Y_c2,a_1,a_2,b_1,b_2,psi_1,psi_2) #12 parameters, 10 are fixed and 2 are free.
free_param=(t_1,t_2) #These are my two free parameters
D=((X_2-X_1)**2 + (Y_2-Y_1)**2)**0.5 #Expression to be minimised
distance=lambdify(param, D, modules='numpy')
Following piece of code has been based on this link:
Want to do multi-variation minimize with sympy
#Build Jacobian:
jac_D=[D.diff(x) for x in param]
jac_distance=[lambdify(param, jf, modules='numpy') for jf in jac_D]
def vector_distance(zz):
""" Helper for receiving vector parameters """
return distance(zz[0], zz[1], zz[2], zz[3], zz[4], zz[5], zz[6], zz[7], zz[8], zz[9], zz[10], zz[11])
def jac_vector_distance(zz):
""" Jacobian Helper for receiving vector parameters """
return np.array([jfn(zz[0], zz[1], zz[2], zz[3], zz[4], zz[5], zz[6], zz[7], zz[8], zz[9], zz[10], zz[11]) for jfn in jac_distance])
zz0 = np.array([np.pi/2, np.p1/2]) #Guess values for t_1 and t_2
Now I want to fix the values of the other 10 variables. I thought of using constrains. (I want X_c1=150, X_c2=2.03 and so on as shown below)
cons=({'type': 'eq',
'fun' : lambda x: np.array([X_c1-150])},
{'type': 'eq',
'fun' : lambda x:np.array([X_c2-2.03)]},
{'type': 'eq',
'fun': lambda x:np.array([Y_c1-152])},
{'type': 'eq',
'fun' : lambda x: np.array([Y_c2-2.31])},
{'type': 'eq',
'fun' : lambda x:np.array([a_1-5])},
{'type': 'eq',
'fun': lambda x:np.array([a_2-3])},
{'type': 'eq',
'fun' : lambda x: np.array([b_1-9])},
{'type': 'eq',
'fun' : lambda x:np.array([b_2-4])},
{'type': 'eq',
'fun': lambda x:np.array([psi_1-np.pi/2])},
{'type': 'eq',
'fun' : lambda x: np.array([psi_2-np.pi/4])},
)
bnds=((0,np.2pi), (0,np.2pi)) # My free parameters can take values between 0 and 2pi.
rslts = minimize(vector_distance, zz0, method='SLSQP', jac=jac_vector_distance, constraints=cons, bounds=bnds)
This returns the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: can't convert expression to float
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
SystemError: <built-in function hasattr> returned a result with an error set
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
SystemError: <built-in function hasattr> returned a result with an error set
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
SystemError: <built-in function hasattr> returned a result with an error set
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
SystemError: <built-in function hasattr> returned a result with an error set
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
SystemError: <built-in function hasattr> returned a result with an error set
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
SystemError: <built-in function hasattr> returned a result with an error set
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
SystemError: <built-in function hasattr> returned a result with an error set
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
SystemError: <built-in function hasattr> returned a result with an error set
During handling of the above exception, another exception occurred:
SystemError Traceback (most recent call last)
<ipython-input-18-fc64da7d0cae> in <module>()
----> 1 rslts = minimize(vector_distance, zz0, method='SLSQP', jac=jac_vector_distance, constraints=cons)
/users/vishnu/anaconda3/lib/python3.5/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
453 elif meth == 'slsqp':
454 return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 455 constraints, callback=callback, **options)
456 elif meth == 'dogleg':
457 return _minimize_dogleg(fun, x0, args, jac, hess,
/users/vishnu/anaconda3/lib/python3.5/site-packages/scipy/optimize/slsqp.py in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, **unknown_options)
404
405 # Call SLSQP
--> 406 slsqp(m, meq, x, xl, xu, fx, c, g, a, acc, majiter, mode, w, jw)
407
408 # call callback if major iteration has incremented
/users/vishnu/anaconda3/lib/python3.5/site-packages/sympy/core/expr.py in __float__(self)
219 # to fail, and if it is we still need to check that it evalf'ed to
220 # a number.
--> 221 result = self.evalf()
222 if result.is_Number:
223 return float(result)
/users/vishnu/anaconda3/lib/python3.5/site-packages/sympy/core/evalf.py in evalf(self, n, subs, maxn, chop, strict, quad, verbose)
1359
1360 """
-> 1361 from sympy import Float, Number
1362 n = n if n is not None else 15
1363
/users/vishnu/anaconda3/lib/python3.5/importlib/_bootstrap.py in _handle_fromlist(module, fromlist, import_)
SystemError: <built-in function hasattr> returned a result with an error set

It seems that you are minimizing distance between two ellipse. You don't need sympy to do this. Here is an example:
from math import sin, cos, hypot, pi
from scipy import optimize
import numpy as np
def ellipse(xc, yc, a, b, psi):
a_cos_p = a * cos(psi)
a_sin_p = a * sin(psi)
b_cos_p = b * cos(psi)
b_sin_p = b * sin(psi)
def f(t):
cos_t = cos(t)
sin_t = sin(t)
x = xc + cos_t * a_cos_p - sin_t * b_sin_p
y = yc + cos_t * a_sin_p + sin_t * b_cos_p
return x, y
return f
def min_dist_between_ellipses(el1, el2):
def dist(pars):
t1, t2 = pars.tolist()
x1, y1 = el1(t1)
x2, y2 = el2(t2)
return hypot(x1 - x2, y1 - y2)
r = optimize.minimize(dist, (0, 0))
return r.x.tolist(), dist(r.x)
xc1 = 150
xc2 = 2.03
yc1 = 152
yc2 = 2.31
a1 = 5
a2 = 3
b1 = 9
b2 = 4
psi1 = pi / 2
psi2 = pi / 4
elpars1 = xc1, yc1, a1, b1, psi1
elpars2 = xc2, yc2, a2, b2, psi2
el1 = ellipse(*elpars1)
el2 = ellipse(*elpars2)
print((min_dist_between_ellipses(el1, el2)))
x1, y1 = np.array([el1(t) for t in np.linspace(0, 2*np.pi, 100)]).T
x2, y2 = np.array([el2(t) for t in np.linspace(0, 2*np.pi, 100)]).T
print(np.hypot(x1[:, None] - x2[None, :], y1[:, None] - y2[None, :]).min())
outputs:
([2.098535986219504, 0.03199718973020122], 200.25805791197473)
200.259630185

Related

How to correct solve trigonometric functions equation by sympy?

I have an equation with trigonometric functions as below:
eq = Eq(cos(theta_3), a_2*a_3*(-a_2**2/2 - a_3**2/2 + b**2/2 + z_4**2/2))
Then I try solve θ by sympy and code as below:
solve([eq, theta_3 < pi ], theta_3)
But it raise a exception and part of the information as follows:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File E:\conda\lib\site-packages\sympy\polys\polyutils.py:211, in _parallel_dict_from_expr_if_gens(exprs, opt)
209 base, exp = decompose_power_rat(factor)
--> 211 monom[indices[base]] = exp
212 except KeyError:
KeyError: cos(_theta_3)
During handling of the above exception, another exception occurred:
PolynomialError Traceback (most recent call last)
File E:\conda\lib\site-packages\sympy\solvers\inequalities.py:809, in _solve_inequality(ie, s, linear)
808 try:
--> 809 p = Poly(expr, s)
810 if p.degree() == 0:
File E:\conda\lib\site-packages\sympy\polys\polytools.py:182, in Poly.__new__(cls, rep, *gens, **args)
181 else:
--> 182 return cls._from_expr(rep, opt)
File E:\conda\lib\site-packages\sympy\polys\polytools.py:311, in Poly._from_expr(cls, rep, opt)
310 """Construct a polynomial from an expression. """
--> 311 rep, opt = _dict_from_expr(rep, opt)
312 return cls._from_dict(rep, opt)
Why does such an exception raise?
How to correct solve trigonometric functions equation by sympy?

Error: _print_LambertW() got an unexpected keyword argument 'exp'

I am trying multiply the following equation with its derivative using Python, I made the following code
from sympy.abc import x, y, z, a,b
from sympy import *
a, b, n, t,N_0,x = symbols('a b n t N_0 x')
f=-LambertW(-N_0*b**(-a)*log(b)*log(t + 1))/((t + 1)*log(b)*log(t + 1))
f_a=diff(f,a)
d=f*f_a
d
When I try to run the code:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/IPython/core/formatters.py in __call__(self, obj)
336 method = get_real_method(obj, self.print_method)
337 if method is not None:
--> 338 return method()
339 return None
340 else:
10 frames
/usr/local/lib/python3.7/dist-packages/sympy/printing/printer.py in _print(self, expr, **kwargs)
327 printmethod = '_print_' + cls.__name__
328 if hasattr(self, printmethod):
--> 329 return getattr(self, printmethod)(expr, **kwargs)
330 # Unknown object, fall back to the emptyPrinter.
331 return self.emptyPrinter(expr)
TypeError: _print_LambertW() got an unexpected keyword argument 'exp'
This error is no longer raised in the most recent version of SymPy (or at least the development version).

Python throws error when I call integrate( vector expression, t )

I wrote a Python program to calculate the Magnetic Field from a given time-varying Electric field. I used CoordSys3d from sympy.vector
I got it working this afternoon. It was giving correct answers. Then, I upgraded to the newest version of Anaconda (which updated the SymPy library), and now it throws an error when I call integrate().
Here is the error traceback:
>>> integrate( jimmy, t )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/integrals.py", line 1573, in integrate
new_args = [a.doit(**doit_flags) if isinstance(a, Integral) else a
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/integrals.py", line 1573, in <listcomp>
new_args = [a.doit(**doit_flags) if isinstance(a, Integral) else a
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/integrals.py", line 597, in doit
antideriv = self._eval_integral(
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/integrals.py", line 941, in _eval_integral
result, i = risch_integrate(f, x, separate_integral=True,
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/risch.py", line 1831, in risch_integrate
ans, i, b = integrate_hyperexponential(fa, fd, DE, conds=conds)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/risch.py", line 1564, in integrate_hyperexponential
qa, qd, b = integrate_hyperexponential_polynomial(pp, DE, z)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/risch.py", line 1516, in integrate_hyperexponential_polynomial
va, vd = rischDE(iDta, iDtd, Poly(aa, DE.t), Poly(ad, DE.t), DE)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/rde.py", line 774, in rischDE
_, (fa, fd) = weak_normalizer(fa, fd, DE)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/integrals/rde.py", line 126, in weak_normalizer
r = (a - Poly(z, DE.t)*derivation(d1, DE)).as_poly(DE.t).resultant(
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/polys/polytools.py", line 65, in wrapper
return func(f, g)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/polys/polytools.py", line 4104, in __sub__
return f.sub(g)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/polys/polytools.py", line 1464, in sub
_, per, F, G = f._unify(g)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/polys/polytools.py", line 465, in _unify
dom, lev = f.rep.dom.unify(g.rep.dom, gens), len(gens) - 1
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/polys/domains/domain.py", line 686, in unify
return K0.unify_with_symbols(K1, symbols)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/polys/domains/domain.py", line 666, in unify_with_symbols
return K0.unify(K1)
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/polys/domains/domain.py", line 725, in unify
domain = domain.get_ring()
File "/opt/anaconda3/lib/python3.8/site-packages/sympy/polys/domains/complexfield.py", line 98, in get_ring
raise DomainError("there is no ring associated with %s" % self)
sympy.polys.polyerrors.DomainError: there is no ring associated with CC
Here is the code:
import numpy as np
import sympy as smp
import matplotlib.pyplot as plt
from sympy import *
from sympy import symbols
from sympy.vector import divergence
from sympy.vector import curl
from sympy import diff
from sympy import exp
from sympy import integrate
from sympy.vector import CoordSys3D
R = CoordSys3D('R')
# Variables
X, Y, Z = symbols('X Y Z')
X = R.x
Y = R.y
Z = R.z
# Basis Unit Vectors
i,j,k = symbols('i j k')
i = R.i
j = R.j
k = R.k
# Symbols
x,t = symbols('x t')
Ex = smp.Function('Ex')(x,t)
Ey = smp.Function('Ey')(x,t)
Ez = smp.Function('Ez')(x,t)
wavenumber = symbols('k')
E_0 = symbols('E_0') # Amplitude of E field
w = symbols('w' , real=True, positive=True)
# Define Ey(x,t)
Ey = E_0 * smp.exp( 1j * (wavenumber*X - w*t ))
# The Electric Field
E = Ex*i + Ey*j + Ez*k
init_printing(use_unicode=True, wrap_line=False)
# curl E = - dB/dt
# integrate( (curl E) , t ) = - B
jimmy = curl( E )
B = -integrate( jimmy, t )
pprint( B )
B = -integrate( jimmy, t ).doit()
pprint( B )
Usually we ask users to include the full traceback when asking about errors.
In a sympy context I expect to see "won't integrate" description when result is an expression containing the integral sign. Similarly for "can't solve" results, where the result is the original expression, or a barely simplified one.
Your case is an actual error.
In [6]: jimmy = curl( E, R )
In [7]: jimmy
Out[7]:
1.0⋅ⅈ⋅(Rₓ⋅k - t⋅w)
1.0⋅ⅈ⋅E₀⋅k⋅ℯ r_z
In [8]: smp.integrate( jimmy, t )
/usr/local/lib/python3.8/dist-packages/sympy/core/sympify.py:456: SymPyDeprecationWarning:
String fallback in sympify has been deprecated since SymPy 1.6. Use
sympify(str(obj)) or sympy.core.sympify.converter or obj._sympy_
instead. See https://github.com/sympy/sympy/issues/18066 for more
info.
SymPyDeprecationWarning(
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ValueError: Error from parse_expr with transformed code: "Float ('1.0' )*I *Symbol ('E_0' )*Symbol ('k' )*exp (Float ('1.0' )*I *(Symbol ('R_x' )*Symbol ('k' )-Symbol ('t' )*Symbol ('w' )))*Symbol ('R' ).z "
The above exception was the direct cause of the following exception:
AttributeError Traceback (most recent call last)
<ipython-input-8-8d40c4ce949f> in <module>
----> 1 smp.integrate( jimmy, t )
/usr/local/lib/python3.8/dist-packages/sympy/integrals/integrals.py in integrate(meijerg, conds, risch, heurisch, manual, *args, **kwargs)
1566 'manual': manual
1567 }
-> 1568 integral = Integral(*args, **kwargs)
1569
1570 if isinstance(integral, Integral):
/usr/local/lib/python3.8/dist-packages/sympy/integrals/integrals.py in __new__(cls, function, *symbols, **assumptions)
89 useinstead="the as_expr or integrate methods of Poly").warn()
90
---> 91 obj = AddWithLimits.__new__(cls, function, *symbols, **assumptions)
92 return obj
93
/usr/local/lib/python3.8/dist-packages/sympy/concrete/expr_with_limits.py in __new__(cls, function, *symbols, **assumptions)
496
497 def __new__(cls, function, *symbols, **assumptions):
--> 498 pre = _common_new(cls, function, *symbols, **assumptions)
499 if type(pre) is tuple:
500 function, limits, orientation = pre
/usr/local/lib/python3.8/dist-packages/sympy/concrete/expr_with_limits.py in _common_new(cls, function, *symbols, **assumptions)
23 (function, limits, orientation). This code is common to
24 both ExprWithLimits and AddWithLimits."""
---> 25 function = sympify(function)
26
27 if isinstance(function, Equality):
/usr/local/lib/python3.8/dist-packages/sympy/core/sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
477 try:
478 a = a.replace('\n', '')
--> 479 expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
480 except (TokenError, SyntaxError) as exc:
481 raise SympifyError('could not parse %r' % a, exc)
....
AttributeError: 'Symbol' object has no attribute 'z'
So while:
In [9]: R.z
Out[9]: r_z
it has replaced that with:
In [10]: parse_expr(Symbol ('R' ).z)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-60a85cf12db4> in <module>
----> 1 parse_expr(Symbol ('R' ).z)
AttributeError: 'Symbol' object has no attribute 'z'
I don't know enough sympy to understand the issue, but it is more informative than "won't integrate".
I discovered the problem was being caused by me using "1j" for "i". I changed it to "I" and now it works.
Causes error:
Ey = E_0 * smp.exp( 1j * (wavenumber*X - w*t ))
Works fine:
Ey = E_0 * smp.exp( I * (wavenumber*X - w*t ))

Problem with sending argument using dblquad integrator

from scipy import integrate
def g(y,x,a):
return x*y**2 + a
a= 13
integrate.dblquad(g, 0, 2, lambda x: 0, lambda x: x, args=(a))
TypeError Traceback (most recent call last)
in
5 a= 13
6
----> 7 integrate.dblquad(g, 0, 2, lambda x: 0, lambda x: x, args=(a))
~\anaconda3\lib\site-packages\scipy\integrate\quadpack.py in dblquad(func, a, b, gfun, hfun, args, epsabs, epsrel)
599 hfun(args[0]) if callable(hfun) else hfun]
600
--> 601 return nquad(func, [temp_ranges, [a, b]], args=args,
602 opts={"epsabs": epsabs, "epsrel": epsrel})
603
~\anaconda3\lib\site-packages\scipy\integrate\quadpack.py in nquad(func, ranges, args, opts, full_output)
824 else:
825 opts = [opt if callable(opt) else _OptFunc(opt) for opt in opts]
--> 826 return _NQuad(func, ranges, opts, full_output).integrate(*args)
827
828
TypeError: integrate() argument after * must be an iterable, not int
You could just define a inside (or even outside) of g:
from scipy import integrate
a = 13
def g(y, x):
return x * y ** 2 + a
integrate.dblquad(g, 0, 2, lambda x: 0, lambda x: x)
(I was never a big of the args argument.)

PyMC3 & Theano - Theano code that works stop working after pymc3 import

Some simple theano code that works perfectly, stop working when I import pymc3
Here some snipets in order to reproduce the error:
#Initial Theano Code (this works)
import theano.tensor as tsr
x = tsr.dscalar('x')
y = tsr.dscalar('y')
z = x + y
#Snippet 1
import pymc3 as pm
import theano.tensor as tsr
x = tsr.dscalar('x')
y = tsr.dscalar('y')
z = x + y
#Snippet 2
import theano.tensor as tsr
import pymc3 as pm
x = tsr.dscalar('x')
y = tsr.dscalar('y')
z = x + y
#Snippet 3
import pymc3 as pm
x = pm.theano.tensor.dscalar('x')
y = pm.theano.tensor.dscalar('y')
z = x + y
And I get the following error for each of the previous snippets:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Users/tom/anaconda/lib/python3.4/site-packages/theano/gof/op.py in __call__(self, *inputs, **kwargs)
516 try:
--> 517 storage_map[ins] = [self._get_test_value(ins)]
518 compute_map[ins] = [True]
/Users/tom/anaconda/lib/python3.4/site-packages/theano/gof/op.py in _get_test_value(cls, v)
478
--> 479 raise AttributeError('%s has no test value' % v)
480
AttributeError: x has no test value
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-2-ef8582b040f8> in <module>()
3 x = pm.theano.tensor.dscalar('x')
4 y = pm.theano.tensor.dscalar('y')
----> 5 z = x + y
/Users/tom/anaconda/lib/python3.4/site-packages/theano/tensor/var.py in __add__(self, other)
126 def __add__(self, other):
127 try:
--> 128 return theano.tensor.basic.add(self, other)
129 # We should catch the minimum number of exception here.
130 # Otherwise this will convert error when Theano flags
/Users/tom/anaconda/lib/python3.4/site-packages/theano/gof/op.py in __call__(self, *inputs, **kwargs)
523 run_perform = False
524 elif config.compute_test_value == 'raise':
--> 525 raise ValueError('Cannot compute test value: input %i (%s) of Op %s missing default value' % (i, ins, node))
526 elif config.compute_test_value == 'ignore':
527 # silently skip test
ValueError: Cannot compute test value: input 0 (x) of Op Elemwise{add,no_inplace}(x, y) missing default value
Any Ideas ?
Thanks in advance
I think this is related to pymc3 setting theano.config.compute_test_value = 'raise': https://github.com/pymc-devs/pymc3/blob/master/pymc3/model.py#L395
You can explicitly set theano.config.compute_test_value back to 'ignore' to get rid of the error.
Solution proposed here lasts a bit longer than setting the flag. In your shell type:
theano-cache purge

Categories

Resources