def RoboTrajectory(thst, then, T, dt, ts): try: thst, then = np.array(thst), np.array(then) thst.shape, then.shape = (1, np.size(thst)), (1, np.size(then)) n = thst.shape[1] N = int(T / dt) robotraj = [] thlistset = JointTrajectory(thst, then, T, N + 1, ts) robotraj.append(thlistset) thdotlistlist = [] thdotdotlistlist = [] ti = 0 for i in range(0, N + 1): if ts == 3: thdotlist = symath.diff(lambda t: thst + CubicTimeScaling(t, T) * (then - thst), ti) thdotdotlist = symath.diff(lambda t: thst + CubicTimeScaling(t, T) * (then - thst), ti, 2) elif ts == 5: thdotlist = symath.diff(lambda t: thst + QuinticTimeScaling(t, T) * (then - thst), ti) thdotdotlist = symath.diff(lambda t: thst + QuinticTimeScaling(t, T) * (then - thst), ti, 2) ti += dt thdotlistlist.append(thdotlist.astype(np.float64)) thdotdotlistlist.append(thdotdotlist.astype(np.float64)) thdotlistset = thdotlistlist[0] thdotdotlistset = thdotdotlistlist[0] for i in range(1, N + 1): thdotlistset = np.vstack((thdotlistset, thdotlistlist[i])) thdotdotlistset = np.vstack((thdotdotlistset, thdotdotlistlist[i])) robotraj.append(thdotlistset) robotraj.append(thdotdotlistset) return robotraj except: print "Please check input parameters!"
def getObjectiveFunctionDiff(word, document_counter, vocab, smooth, ratio, X_plus, X_minus, sum_Xplus, sum_Xminus, vocab_T, Nt_plus, Nt_minus): from sympy import mpmath import numpy as np import warnings import sys mpmath.mp.pretty = True warnings.filterwarnings("error") #INDEPENDENT word fraction_nplus = document_counter[word] / (smooth + X_plus[word]) fraction_nminus = document_counter[word] / (smooth + X_minus[word]) di = sum(document_counter.itervalues()) g = ((vocab * smooth + sum_Xplus) / (vocab * smooth + sum_Xminus))**di product_pi = 1 for key in document_counter.keys(): #WARNING: divided by zero try: product_pi = product_pi * ( ((smooth + X_minus[key]) / (smooth + X_plus[key]))**document_counter[key]) except Warning: print "smooth " + str(smooth) + " key " + str(key) + " " + str( X_plus[key]) + " " + str(X_minus[key]) g_diffpos = mpmath.diff( lambda x: ((smooth * vocab + sum_Xplus - X_plus[word] + x) / (smooth * vocab + sum_Xminus))**di, X_plus[word]) g_diffneg = mpmath.diff( lambda x: ((smooth * vocab + sum_Xplus) / (smooth * vocab + sum_Xminus - X_minus[word] + x))**di, X_minus[word]) sum_penalty_T = 0 #INDEPENDENT word for w in vocab_T: sum_penalty_T = sum_penalty_T + (X_plus[w] - Nt_plus[w])**2 + ( X_minus[w] - Nt_minus[w])**2 penalty_T_diffpos = mpmath.diff( lambda x: sum_penalty_T - (X_plus[word] - Nt_plus[word])**2 + (x - Nt_plus[word])**2, X_plus[word]) penalty_T_diffneg = mpmath.diff( lambda x: sum_penalty_T - (X_minus[word] - Nt_minus[word])**2 + (x - Nt_minus[word])**2, X_minus[word]) #FUNCTION regularization penalty terms here #FUNCTION check vocabulary here Fplus = (fraction_nplus + (ratio**-1) * product_pi * g_diffpos) / ( 1 + (ratio**-1) * product_pi * g) - fraction_nplus + penalty_T_diffpos import sys try: Fminus = (fraction_nminus * g + g_diffneg) / (ratio * (product_pi**-1) + g) + penalty_T_diffneg except ZeroDivisionError: mlog( getObjectiveFunctionDiff.__name__, ZeroDivisionError.message + ' word=' + word + ' product_pi = ' + str(product_pi)) product_pi = 1 Fminus = (fraction_nminus * g + g_diffneg) / (ratio * (product_pi**-1) + g) return (np.float64(Fplus), np.float64(Fminus))
def checar_criterio(despeje,punto): from sympy.mpmath import diff, log, sin, tan, cos criterio = False #se evalua el criterio de convergencia y se deja que el usuario decida si continua con el msimo despeje print "g'(",punto,") = ",abs(diff(lambdify(x,sympify(despeje),"sympy"), punto)) while abs(diff(lambdify(x,sympify(despeje),"sympy"), punto)) > 1.0: print"Tu despeje no cumple el criterio de convergencia para este intervalo" respuesta = raw_input("Quieres arriesgarte? responde si/no\n") while respuesta != "si" and respuesta != "no": respuesta = raw_input("Quieres arriesgarte? responde si/no?") if respuesta == "si": return criterio else: despeje = ingresar_funcion(despeje = True) print"Tu despeje cumple el criterio de covergencia, solucion garantizada!" criterio = True return criterio
def RoboTrajectory(thst, then, T, dt, ts): try: thst, then = np.array(thst), np.array(then) thst.shape, then.shape = (1, np.size(thst)), (1, np.size(then)) n = thst.shape[1] N = int(T / dt) robotraj = [] thlistset = JointTrajectory(thst, then, T, N + 1, ts) robotraj.append(thlistset) thdotlistlist = [] thdotdotlistlist = [] ti = 0 for i in range(0, N + 1): if ts == 3: thdotlist = symath.diff( lambda t: thst + CubicTimeScaling(t, T) * (then - thst), ti) thdotdotlist = symath.diff( lambda t: thst + CubicTimeScaling(t, T) * (then - thst), ti, 2) elif ts == 5: thdotlist = symath.diff( lambda t: thst + QuinticTimeScaling(t, T) * (then - thst), ti) thdotdotlist = symath.diff( lambda t: thst + QuinticTimeScaling(t, T) * (then - thst), ti, 2) ti += dt thdotlistlist.append(thdotlist.astype(np.float64)) thdotdotlistlist.append(thdotdotlist.astype(np.float64)) thdotlistset = thdotlistlist[0] thdotdotlistset = thdotdotlistlist[0] for i in range(1, N + 1): thdotlistset = np.vstack((thdotlistset, thdotlistlist[i])) thdotdotlistset = np.vstack((thdotdotlistset, thdotdotlistlist[i])) robotraj.append(thdotlistset) robotraj.append(thdotdotlistset) return robotraj except: print "Please check input parameters!"
def getObjectiveFunctionDiff(word,document_counter,vocab,smooth,ratio,X_plus,X_minus, sum_Xplus, sum_Xminus,vocab_T,Nt_plus,Nt_minus): from sympy import mpmath import numpy as np import warnings import sys mpmath.mp.pretty = True warnings.filterwarnings("error") #INDEPENDENT word fraction_nplus = document_counter[word] / (smooth + X_plus[word]) fraction_nminus = document_counter[word] / (smooth + X_minus[word]) di = sum(document_counter.itervalues()) g = ((vocab * smooth + sum_Xplus) / (vocab * smooth + sum_Xminus)) ** di product_pi = 1 for key in document_counter.keys(): #WARNING: divided by zero try: product_pi = product_pi * (((smooth + X_minus[key]) / (smooth + X_plus[key])) ** document_counter[key]) except Warning: print "smooth "+str(smooth)+" key "+str(key)+ " "+str(X_plus[key])+" "+str(X_minus[key]) g_diffpos = mpmath.diff(lambda x: ((smooth * vocab + sum_Xplus - X_plus[word] + x) / (smooth * vocab + sum_Xminus)) ** di,X_plus[word]) g_diffneg = mpmath.diff(lambda x: ((smooth * vocab + sum_Xplus) / (smooth * vocab + sum_Xminus - X_minus[word] + x)) ** di,X_minus[word]) sum_penalty_T = 0 #INDEPENDENT word for w in vocab_T: sum_penalty_T = sum_penalty_T + (X_plus[w]-Nt_plus[w])**2 + (X_minus[w]-Nt_minus[w])**2 penalty_T_diffpos = mpmath.diff(lambda x:sum_penalty_T-(X_plus[word]-Nt_plus[word])**2+(x-Nt_plus[word])**2,X_plus[word]) penalty_T_diffneg = mpmath.diff(lambda x:sum_penalty_T-(X_minus[word]-Nt_minus[word])**2+(x-Nt_minus[word])**2,X_minus[word]) #FUNCTION regularization penalty terms here #FUNCTION check vocabulary here Fplus = (fraction_nplus + (ratio ** -1) * product_pi * g_diffpos) / (1 + (ratio ** -1) * product_pi * g) - fraction_nplus + penalty_T_diffpos import sys try: Fminus = (fraction_nminus * g + g_diffneg) / (ratio * (product_pi ** -1) + g) + penalty_T_diffneg except ZeroDivisionError: mlog(getObjectiveFunctionDiff.__name__,ZeroDivisionError.message + ' word=' + word + ' product_pi = ' + str(product_pi)) product_pi = 1 Fminus = (fraction_nminus * g + g_diffneg) / (ratio * (product_pi ** -1) + g) return (np.float64(Fplus),np.float64(Fminus))
def doit_numerically(self, z0): """ Evaluate the derivative at z numerically. When we can represent derivatives at a point, this should be folded into the normal evalf. For now, we need a special method. """ from sympy import mpmath from sympy.core.expr import Expr if len(self.free_symbols) != 1 or len(self.variables) != 1: raise NotImplementedError('partials and higher order derivatives') z = list(self.free_symbols)[0] def eval(x): f0 = self.expr.subs(z, Expr._from_mpmath(x, prec=mpmath.mp.prec)) f0 = f0.evalf(mlib.libmpf.prec_to_dps(mpmath.mp.prec)) return f0._to_mpmath(mpmath.mp.prec) return Expr._from_mpmath(mpmath.diff(eval, z0._to_mpmath(mpmath.mp.prec)), mpmath.mp.prec)
def getObjectiveFunctionDiff(word,document_counter,vocab,smooth,ratio,X_plus,X_minus, sum_Xplus, sum_Xminus,vocab_T,pos_count,neg_count,di,vocab_S,sum_penalty_Vs,R_Mkb,X_zeroplus,X_zerominus): from sympy import mpmath import numpy as np mpmath.mp.pretty = True #np.seterr(all='print') fraction_nplus = document_counter[word] / (smooth + X_plus[word]) fraction_nminus = document_counter[word] / (smooth + X_minus[word]) #INDEPENDENT g = ((vocab * smooth + sum_Xplus) / (vocab * smooth + sum_Xminus)) ** di product_pi = 1 for key in document_counter.keys(): product_pi = product_pi * (((smooth + X_minus[key]) / (smooth + X_plus[key])) ** document_counter[key]) g_diffpos = mpmath.diff(lambda x: ((smooth * vocab + sum_Xplus - X_plus[word] + x) / (smooth * vocab + sum_Xminus)) ** di,X_plus[word]) g_diffneg = mpmath.diff(lambda x: ((smooth * vocab + sum_Xplus) / (smooth * vocab + sum_Xminus - X_minus[word] + x)) ** di,X_minus[word]) sum_penalty_Vt = 0 #regularization term 1 #INDEPENDENT for w in vocab_T: sum_penalty_Vt = sum_penalty_Vt + (X_plus[w]-pos_count[w])**2 +(X_minus[w]-neg_count[w])**2 #FIX vocab limitation if word in vocab_T: penalty_Vt_plus = mpmath.diff(lambda x: 0.5*0.1*(sum_penalty_Vt -( X_plus[word]-pos_count[word])**2+(x-pos_count[word])**2),X_plus[word]) penalty_Vt_minus = mpmath.diff(lambda x: 0.5*0.1*(sum_penalty_Vt -( X_minus[word]-neg_count[word])**2+(x-neg_count[word])**2),X_minus[word]) else: penalty_Vt_plus = 0 penalty_Vt_minus = 0 #regularization term 2 if word in vocab_S: penalty_Vs_plus = mpmath.diff(lambda x:0.5*0.1*(sum_penalty_Vs-(X_plus[word]-R_Mkb[word]*X_zeroplus[word])**2),X_plus[word]) penalty_Vs_minus = mpmath.diff(lambda x:0.5*0.1*(sum_penalty_Vs-(X_minus[word]-(1-R_Mkb[word])*X_zerominus[word])**2),X_minus[word]) else: penalty_Vs_plus = 0 penalty_Vs_minus = 0 #dF+, dF- Fplus = (fraction_nplus + (ratio ** -1) * product_pi * g_diffpos) / (1 + (ratio ** -1) * product_pi * g) - fraction_nplus + penalty_Vt_plus + penalty_Vs_plus Fminus = (fraction_nminus * g + g_diffneg) / (ratio * (product_pi ** -1) + g) + penalty_Vt_minus + penalty_Vs_minus return (np.float64(Fplus),np.float64(Fminus))
def contour_int(f, c, t0, t1): """ Evaluate the integral of the function 'f' parameterized by the function 'c' with initial and final parameter values 't0' and 't1'. """ return complex(mp.quad(lambda t: f(c(t)) * mp.diff(c, t), (t0, t1)))