I'm trying to perform implicit differentiation to the function Lrdot which is -2*rdot/(1 - 1/r(t)), wrt an affine parameter s, where rdot is dr/ds. The error below pops up and i'm not sure how to debug.
from sympy import *
from sympy.physics.mechanics import *
r = dynamicsymbols('r')
rdot = dynamicsymbols('r', 1)
t = dynamicsymbols('t')
tdot = dynamicsymbols('t', 1)
phi = dynamicsymbols('phi')
phidot = dynamicsymbols('phi', 1)
s = symbols('s')
def F(x):
return 1-(1/x)
# Largangian
def L(a,b,c, adot, bdot, cdot, photon = true): #r,t,phi
return F(a)*(bdot)**2 - adot**2/F(a) - (a*cdot)**2
L = L(r, t, phi, rdot, tdot, phidot, photon = true)
Lr = diff(L, r)
Lrdot = diff(L, rdot)
diffLrdot = idiff(-2*rdot/(1-1/r), r, s)
print(diffLrdot)
Traceback (most recent call last):
File "/Users/myname/PycharmProjects/untitled/.idea/14.1.py", line 40, in <module>
diffLrdot = idiff(-2*rdot/(1-1/r), r, s)
File "/Users/myname/PycharmProjects/untitled/venv/lib/python3.6/site-packages/sympy/geometry/util.py", line 578, in idiff
f = {s: Function(s.name)(x) for s in eq.free_symbols
File "/Users/myname/PycharmProjects/untitled/venv/lib/python3.6/site-packages/sympy/geometry/util.py", line 579, in <dictcomp>
if s != x and s in dep}
NameError: free variable 'dep' referenced before assignment in enclosing scope
Related
When using CVXOPT to optimize my SVM problem, I receive the error given below.
Traceback (most recent call last):
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\misc.py", line 1429, in factor
lapack.potrf(F['S'])
ArithmeticError: 25
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\coneprog.py", line 2065, in coneqp
try: f = kktsolver(W)
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\coneprog.py", line 1981, in kktsolver
return factor(W, P)
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\misc.py", line 1444, in factor
lapack.potrf(F['S'])
ArithmeticError: 25
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\mospic\Desktop\EXP2_files\src1\main.py", line 28, in <module>
test_svm(1, 'Linear', 1e-4)
File "C:\Users\mospic\Desktop\EXP2_files\src1\main.py", line 21, in test_svm
model.fit(train_features, train_labels)
File "C:\Users\mospic\Desktop\EXP2_files\src1\SVM.py", line 66, in fit
sol = cvxopt.solvers.qp(P, q, G, h, A, b)
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\coneprog.py", line 4485, in qp
return coneqp(P, q, G, h, None, A, b, initvals, kktsolver = kktsolver, options = options)
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\coneprog.py", line 2067, in coneqp
raise ValueError("Rank(A) < p or Rank([P; A; G]) < n")
ValueError: Rank(A) < p or Rank([P; A; G]) < n
And the Code given below:
n = train_data.shape[0]
K = np.zeros((n, n))
for i in range(n):
for j in range(n):
K[i][j] = train_label[i] * train_label[j] * SupportVectorMachine().KERNEL(train_data[i], train_data[j])
#Calculate Kernal Matrix with label i and label j
# 1/2x^TPx + q^Tx
P = cvxopt.matrix(K)
q = cvxopt.matrix(-1 * np.ones(n))
# Gx <= h
temp_G1 = np.identity(n)
temp_G2 = -1 * (np.identity(n))
G = cvxopt.matrix(np.vstack((temp_G1, temp_G2)))
temp_h1 = self.C * np.ones(n)
temp_h2 = np.zeros(n)
h = cvxopt.matrix(np.hstack((temp_h1, temp_h2)).reshape(-1, 1))
# Ax = b
A = cvxopt.matrix(train_label.astype(np.double), (1, n))
b = cvxopt.matrix(0.0)
sol = cvxopt.solvers.qp(P, q, G, h, A, b)
alpha = np.array(sol['x'])
n is the length of training data, and sol is the optimal solution of the SVM question.But the Error ValueError: Rank(A) < p or Rank([P; A; G]) < n is confusing me.
I am trying to do a simple reaction network and see the sensitivity of reaction rates k1 and k2. So my reaction goes A --> B --> C with k1 and k2, respectively.
import numpy as np
import pymc3 as pm
from matplotlib.pyplot import figure, scatter, legend, plot
from scipy.integrate import solve_ivp
from sys import exit
Nt = 11
time = 10
tt = np.linspace(0,time,Nt+1)
y0 = [1,0,0]
k1_0, k2_0 = 1, 0.5
def equat(t,c):
da_dt = -k1_0*c[0]
db_dt = k1_0*c[0] - k2_0*c[1]
dc_dt = k2_0*c[1]
return da_dt, db_dt, dc_dt
c_est = solve_ivp(equat, t_span = [0,time], t_eval = tt, y0 = y0)
niter = 10
with pm.Model() as reak_model:
k1 = pm.Normal('k1', mu = 0, sd = 100)
k2 = pm.Normal('k2', mu=0, sd=100, shape = 1)
sigma = pm.HalfNormal('sigma', sd=1)
def equat_2(t,c):
da_dt = -k1*c[0]
db_dt = k1*c[0] - k1*c[1]
dc_dt = k1*c[1]
return da_dt, db_dt, dc_dt
c = solve_ivp(equat_2, t_span = [0,time], t_eval = tt, y0 = y0)
likelihood = pm.Normal('y', mu=c.y, sd=sigma, observed=c_est.y)
trace = pm.sample(niter, chains = 1)
pm.traceplot(trace, varnames=['k1','k2'])
ValueError: setting an array element with a sequence.
Im getting this error. Since I am new to Bayesian, I am wondering if its something with the distributions?
Traceback (most recent call last):
File "<ipython-input-95-26ff8f25faea>", line 1, in <module>
runfile('/Daft regresion.py', wdir='/Final code')
File "/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "Daft regression.py", line 53, in <module>
c = solve_ivp(equat_2, t_span = [0,time], t_eval = tt, y0 = y0)
File "/python3.7/site-packages/scipy/integrate/_ivp/ivp.py", line 454, in solve_ivp
solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
File "/python3.7/site-packages/scipy/integrate/_ivp/rk.py", line 99, in __init__
self.f = self.fun(self.t, self.y)
File "/python3.7/site-packages/scipy/integrate/_ivp/base.py", line 139, in fun
return self.fun_single(t, y)
File "/python3.7/site-packages/scipy/integrate/_ivp/base.py", line 21, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
File "/python3.7/site-packages/numpy/core/numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
Edit: Apparently I need to implement Theano package instead of solve_ivp. Still help appreciated.
Background:
I am trying to get a pool of workers going to solve a task. My issue is that I am trying to pass it a shared variable, but I get an error. I have written an initializer method for the workers that expect my variables, but I can't seem to get it to work.
Here is my code:
from matplotlib import pyplot
import time
import multiprocessing
#initialize some multiprocessing stuff
num_processes = 8
y = multiprocessing.Array('d', 1000, lock=False)
new_y = multiprocessing.Array('d', 1000, lock=False)
dt = multiprocessing.Value('d',0, lock=False)
y_len = multiprocessing.Value('i',len(y), lock=False)
def init(y_to_share, new_y_to_share):
global y, new_y
y = y_to_share
new_y = new_y_to_share
y[480:520] = [1] * 40
dt.value = 0.01
# our rule for reaction-diffusion
def advance():
global y, new_y
n = len(y)
new_y = list(y)
for j in xrange(n):
new_y[j] += dt * (20 * (y[j - 1] - 2 * y[j] + y[(j + 1) % n])
- y[j] * (1 - y[j]) * (0.3 - y[j]))
y = new_y
return y
# advance through t (t = i * dt) is at least 100; plot
# every 20
chunks = len(y)/num_processes
y_range = range(len(y))
y_range = [y_range[i:i+chunks] for i in range(0, len(y_range), chunks)]
p = multiprocessing.Pool(num_processes, initializer=init, initargs=(y, new_y))
i = 0
start = time.time()
while i * dt.value <= 100:
if i * dt.value % 20 == 0:
pyplot.plot(y, label='t = %g' % (i * dt.value))
arr = p.map(advance, (y, new_y))#hand in an array of indices
i += 1
#print i * dt.value
end = time.time()
elapsed = end-start
print elapsed
pyplot.legend()
pyplot.show()
Edit: Post the actual error
The error:
runfile('/home/kevin/Downloads/cbb750_parallel_hw/propagating-signal-parallel.py', wdir='/home/kevin/Downloads/cbb750_parallel_hw')
Traceback (most recent call last):
File "<ipython-input-64-5ad3fdf93b59>", line 1, in <module>
runfile('/home/kevin/Downloads/cbb750_parallel_hw/propagating-signal-parallel.py', wdir='/home/kevin/Downloads/cbb750_parallel_hw')
File "/usr/local/lib/python2.7/dist-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/usr/local/lib/python2.7/dist-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/home/kevin/Downloads/cbb750_parallel_hw/propagating-signal-parallel.py", line 45, in <module>
arr = p.map(advance, (y, new_y))#hand in an array of indices
File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get
raise self._value
PicklingError: Can't pickle <class 'multiprocessing.sharedctypes.c_double_Array_1000'>: attribute lookup multiprocessing.sharedctypes.c_double_Array_1000 failed
Can anyone help me solve this? I am not sure what I am doing wrong, but I would like to use pool.map .
Why does this code
user_f = lambda a, b: a + b
user_x = lambda u: 2 * u
import sympy
from sympy.abc import t
from sympy.utilities.lambdify import implemented_function
x = implemented_function(sympy.Function('x'), user_x)
f = implemented_function(sympy.Function('f'), user_f)
dx = sympy.diff(f(x(t), t), t, 1)
print(dx)
fl = sympy.lambdify((x(t), t), dx)
print(fl(x(t), t))
give me the output below? (Shouldn't it have sufficient information to fully evaluate the derivatives?)
How do I fix (or work around) this error? Assume I am given user_f and user_x as inputs.
Derivative(x(t), t)*Subs(Derivative(f(_xi_1, t), _xi_1), (_xi_1,), (x(t),)) + Subs(Derivative(f(x(t), _xi_2), _xi_2), (_xi_2,), (t,))
Traceback (most recent call last):
File <path>, line 12, in <module>
print(fl(x(t), t))
File "<string>", line 1, in <lambda>
NameError: global name 'Derivative' is not defined
Providing the modules argument of sympy.lambdify() eliminates the problem:
>>> fl = sympy.lambdify((x(t), t), dx, modules=sympy)
>>> print(fl(x(t), t))
Derivative(x(t), t)*Subs(Derivative(_xi_1 + t, _xi_1), (_xi_1,), (x(t),)) + Subs(Derivative(_xi_2 + x(t), _xi_2), (_xi_2,), (t,))
Hi I am trying to use scipy for optimization. The minimize function with method as 'COBYLA' is working fine for small array size but errors out for larger sized arrays. I tried with 'COBYLA' and 'SLSQP' methods since I have a constrained optimization problem for non-linear functions.
Code snippet:
import scipy as sp
import random
def mytest7obj(x, x_d, y_d, z_d, power):
for x_i in x:
if x_i < 0:
return 0.
sum = 0.
for i in range(x_d):
for j in range(z_d):
term = 1.
for k in range(y_d):
term *= (x[i*(y_d*z_d)+j*(y_d)+k] ** power[k])
sum += term
return 0. - sum
def mytest7():
x_d = 30
y_d = 10
z_d = 100
goal = 1000000.
constraints = []
power = []
for i in range(y_d):
power.append(random.uniform(0.,0.3))
constraints.append({'type':'ineq', 'fun': lambda x: goal - sum(x)})
print 'power: %s\n' % (power,)
result = sp.optimize.minimize(fun = mytest7obj, x0 = [30.] * (x_d*y_d*z_d), method = 'COBYLA', args = (x_d, y_d, z_d, power), jac=False, constraints=constraints, options={'disp':True, 'rhobeg':3., 'maxiter': 10000})
print 'goal attained: %s'% (sum(result.x),)
if __name__ == “__main__”:
mytest7()
The traceback of the error with method 'COBYLA' is:
Traceback (most recent call last):
File "opt_test.py", line 584, in <module>
print 'mytest7'; mytest7()
File "opt_test.py", line 571, in mytest7
result = sp.optimize.minimize(fun = mytest7obj, x0 = [30.] * (x_d*y_d*z_d), method = 'COBYLA', args = (x_d, y_d, z_d, power), jac=False, constraints=constraints, options={'disp':True, 'rhobeg':3., 'maxiter': 10000})
File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 385, in minimize
return _minimize_cobyla(fun, x0, args, constraints, **options)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/cobyla.py", line 238, in _minimize_cobyla
dinfo=info)
ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (-1594577286,)
With 'SLSQP', the error is:
File "opt_test.py", line 586, in <module>
print 'test'; test()
File "opt_test.py", line 454, in test
x = get_optimal(base, budget, initial_values, x_elas, y_elas, x_history, y_history, constraint_coeffs, opt_method = 'SLSQP')
File "opt_test.py", line 355, in get_optimal
constraints=constraints, options=opts)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 388, in minimize
constraints, **options)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py", line 316, in _minimize_slsqp
w = zeros(len_w)
MemoryError
I am using python 2.7.5,
scipy version: 0.14.0rc1,
numpy version: 1.8.1