How to correct solve trigonometric functions equation by sympy? - python

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?

Related

Out of sample forecasting

I have the following code to perform an out-of-sample assessment of a time series. The idea is to perform a recursive and rolling method to calculate MAPE and MSPE.
The code is as follows:
long = len(y)
n_estimation = 83
real = y[(n_estimation):len(y)]
n_forecasting = long - n_estimation
horizontes = 2
predicc = np.zeros((horizontes,n_forecasting))
MSFE = np.zeros((horizontes, 1))
MAPE = np.zeros((horizontes, 1))
for Periods_ahead in range(horizontes):
for i in range(0,n_forecasting):
aux_y = y[0:(n_estimation - Periods_ahead + i)]
model = SARIMAX(endog = aux_y, order = (1,1,0), seasonal_order = (1,1,0,4))
model_fit=model.fit(disp=0)
y_pred = fit.forecast(Periods_ahead + 1)
predicc[Periods_ahead][i] = y_pred[0][Periods_ahead]
error = np.array(real) - predicc[Periods_ahead]
MSFE[Periods_ahead] = np.mean(error**2)
MAPE[Periods_ahead] = np.mean(np.abs(error/np.array(real))) * 100
df_pred = pd.DataFrame({"V1":predicc[0], "V2":predicc[1]})
print("MSFE",MSFE)
print("MAPE %",MAPE)
I am getting the following error, most likely related to using a newer version of SARIMAX.
ValueError Traceback (most recent call last)
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\indexes\range.py:392, in RangeIndex.get_loc(self, key, method, tolerance)
391 try:
--> 392 return self._range.index(new_key)
393 except ValueError as err:
ValueError: 0 is not in range
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
c:\Users\dianaf\OneDrive - Microsoft\Documents\GitHub\big_data_operations\Homework2.ipynb Cell 36 in <cell line: 13>()
17 model_fit=model.fit(disp=0)
18 y_pred = fit.forecast(Periods_ahead + 1)
---> 19 predicc[Periods_ahead][i] = y_pred[0][Periods_ahead]
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\series.py:982, in Series.__getitem__(self, key)
979 return self._values[key]
981 elif key_is_scalar:
--> 982 return self._get_value(key)
984 if is_hashable(key):
985 # Otherwise index.get_value will raise InvalidIndexError
986 try:
987 # For labels that don't resolve as scalars like tuples and frozensets
...
--> 394 raise KeyError(key) from err
395 self._check_indexing_error(key)
396 raise KeyError(key)
KeyError: 0
Any idea how to fix it without downgrading to previous versions of statsmodel?
Thank you!

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).

Pandas - TypeError: Cannot perform 'rand_' with a dtyped [bool] array and scalar of type [bool]

I wanted to change a value of a cell with the conditions of another cell value and used this code dfT.loc[dfT.state == "CANCELLED" & (dfT.Activity != "created"), "Activity"] = "cancelled"
This is an Example Table:
ID
Activity
state
1
created
CANCELLED
1
completed
CANCELLED
2
created
FINNISHED
2
completed
FINISHED
3
created
REJECTED
3
rejected
REJECTED
and There is a Type Error like this:
TypeError Traceback (most recent call last)
~\miniconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op)
264 # (xint or xbool) and (yint or bool)
--> 265 result = op(x, y)
266 except TypeError:
~\miniconda3\lib\site-packages\pandas\core\ops\roperator.py in rand_(left, right)
51 def rand_(left, right):
---> 52 return operator.and_(right, left)
53
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~\miniconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op)
278 try:
--> 279 result = libops.scalar_binop(x, y, op)
280 except (
pandas\_libs\ops.pyx in pandas._libs.ops.scalar_binop()
ValueError: Buffer dtype mismatch, expected 'Python object' but got 'bool'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-6-350c55a06fa7> in <module>
4 # dfT2 = dfT1[dfT1.Activity != 'created']
5 # df.loc[(df.state == "CANCELLED") & (df.Activity != "created"), "Activity"] = "cancelled"
----> 6 dfT.loc[dfT.state == "CANCELLED" & (dfT.Activity != "created"), "Activity"] = "cancelled"
7 dfT
~\miniconda3\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
63 other = item_from_zerodim(other)
64
---> 65 return method(self, other)
66
67 return new_method
~\miniconda3\lib\site-packages\pandas\core\arraylike.py in __rand__(self, other)
61 #unpack_zerodim_and_defer("__rand__")
62 def __rand__(self, other):
---> 63 return self._logical_method(other, roperator.rand_)
64
65 #unpack_zerodim_and_defer("__or__")
~\miniconda3\lib\site-packages\pandas\core\series.py in _logical_method(self, other, op)
4987 rvalues = extract_array(other, extract_numpy=True)
4988
-> 4989 res_values = ops.logical_op(lvalues, rvalues, op)
4990 return self._construct_result(res_values, name=res_name)
4991
~\miniconda3\lib\site-packages\pandas\core\ops\array_ops.py in logical_op(left, right, op)
353 filler = fill_int if is_self_int_dtype and is_other_int_dtype else fill_bool
354
--> 355 res_values = na_logical_op(lvalues, rvalues, op)
356 # error: Cannot call function of unknown type
357 res_values = filler(res_values) # type: ignore[operator]
~\miniconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op)
286 ) as err:
287 typ = type(y).__name__
--> 288 raise TypeError(
289 f"Cannot perform '{op.__name__}' with a dtyped [{x.dtype}] array "
290 f"and scalar of type [{typ}]"
If anyone understand what's my mistake is please help.
Thanks in advance
-Alde
You need to wrap your conditions inside ()
Use:
dfT.loc[(dfT.state == "CANCELLED") & (dfT.Activity != "created"), "Activity"] = "cancelled"

Google AdManager getCurrentNetwork() error

I have set up ad manager credentials. I'm trying to access the Admanager API, im getting the following error.
from googleads import ad_manager
client = ad_manager.AdManagerClient.LoadFromStorage()
network_service = client.GetService('NetworkService', version='v201902')
current_network = network_service.getCurrentNetwork()
the error im facing is:
Fault Traceback (most recent call last)
~\Anaconda3\lib\site-packages\googleads\common.py in MakeSoapRequest(*args)
1381 return soap_service_method(
-> 1382 *packed_args, _soapheaders=soap_headers)['body']['rval']
1383 except zeep.exceptions.Fault as e:
~\Anaconda3\lib\site-packages\zeep\proxy.py in __call__(self, *args, **kwargs)
41 self._proxy._client, self._proxy._binding_options,
---> 42 self._op_name, args, kwargs)
43
~\Anaconda3\lib\site-packages\zeep\wsdl\bindings\soap.py in send(self, client, options, operation, args, kwargs)
131
--> 132 return self.process_reply(client, operation_obj, response)
133
~\Anaconda3\lib\site-packages\zeep\wsdl\bindings\soap.py in process_reply(self, client, operation, response)
193 if response.status_code != 200 or fault_node is not None:
--> 194 return self.process_error(doc, operation)
195
~\Anaconda3\lib\site-packages\zeep\wsdl\bindings\soap.py in process_error(self, doc, operation)
287 actor=None,
--> 288 detail=etree_to_string(doc))
289
Fault: Unknown fault occured
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-16-991c0839fc99> in <module>()
----> 1 current_network = network_service.getCurrentNetwork()
~\Anaconda3\lib\site-packages\googleads\common.py in MakeSoapRequest(*args)
1385 if e.detail is not None:
1386 underlying_exception = e.detail.find(
-> 1387 '{%s}ApiExceptionFault' % self._GetBindingNamespace())
1388 fault_type = self.zeep_client.get_element(
1389 '{%s}ApiExceptionFault' % self._GetBindingNamespace())
TypeError: a bytes-like object is required, not 'str'
You are probably missing permissions with your configured service account. Make sure the account has access to ad-manager and scopes are configured properly.
I suggest to do it this way:
class Adx:
def __init__(self):
self.GOOGLEADS_YAML = 'googleads.yaml'
self.GOOGLEADS_VERSION = 'v202111'
self.google_keys = self.GOOGLEADS_YAML
def activate(self):
ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage(self.google_keys)

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

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

Categories

Resources