differentiation

Partial Derivative using Autograd

天大地大妈咪最大 提交于 2020-01-01 06:55:07
问题 I have a function that takes in a multivariate argument x. Here x = [x1,x2,x3]. Let's say my function looks like: f(x,T) = np.dot(x,T) + np.exp(np.dot(x,T) where T is a constant. I am interested in finding df/dx1, df/dx2 and df/dx3 functions. I have achieved some success using scipy diff, but I am a bit skeptical because it uses numerical differences. Yesterday, my colleague pointed me to Autograd (github). Since it seems to be a popular package, I am hoping someone here knows how to get

Calculating the coefficients of a differentiated polynomial using re.compile and searching in python

。_饼干妹妹 提交于 2019-12-24 23:07:49
问题 Testing the polynomial ' 2x^3+4x^2+8x-16 ' my code below outputs [6, 8] as the coefficients of the differentiated polynomial. However, the output should be [6, 8, 8] . Why is the function getNewCoefficients producing the wrong result? What would be a good way to produce the correct result? def getNumbers(polynomial): regex = re.compile(r"[+-]?\d+(?:\.\d+)?") return regex.findall(polynomial) def formatNumbers(numbers): formattedNumbers = [] for e in numbers: if (e[0] == '+'): formattedNumbers

Function gradients with gather operations in tensorflow

喜欢而已 提交于 2019-12-24 20:37:49
问题 I am trying to write a complicated computational graph using tensorflow and compute symbolic gradients with respect to function parameters. However I am struggling with this when my function/graph involves gather operations of some of the parameters. The problem is that gradient returned by the Session.run is not just a tensor but the IndexedSlices object. And I don't know how to properly convert it to a tensor. Here is a toy example that illustrates the issue import tensorflow as tf import

USE DIFFERENTIAL MATRIX OPERATOR TO SOLVE ODE

旧城冷巷雨未停 提交于 2019-12-24 13:11:13
问题 We were asked to define our own differential operators on MATLAB, and I did it following a series of steps, and then we should use the differential operators to solve a boundary value problem: -y'' + 2y' - y = x, y(0) = y(1) =0 my code was as follows, it was used to compute this (first and second derivative) h = 2; x = 2:h:50; y = x.^2 ; n=length(x); uppershift = 1; U = diag(ones(n-abs(uppershift),1),uppershift); lowershift = -1; L= diag(ones(n-abs(lowershift),1),lowershift); % the code above

`numpy.diff` and `scipy.fftpack.diff` giving different results when differentiating

谁说我不能喝 提交于 2019-12-24 00:42:27
问题 I am trying to compute the derivative of some data and I was trying to compare the output of a finite difference and a spectral method output. But the results are very different and I can't figure out exactly why. Consider the example code below import numpy as np from scipy import fftpack as sp from matplotlib import pyplot as plt x = np.arange(-100,100,1) y = np.sin(x) plt.plot(np.diff(y)/np.diff(x)) plt.plot(sp.diff(y)) plt.show() This outputs the following result The orange output being

Output of numpy.diff is wrong

一笑奈何 提交于 2019-12-12 02:13:54
问题 here's my problem: I am trying to use numpy to compute (numerical) derivatives, but I am finding some problems in the value the function numpy.diff returns (and numpy.gradient as well). What I find is that the values are totally wrong! here's a code sample: import numpy as np x = np.linspace(-5, 5, 1000) y = x**2 yDiff = np.diff(y) print y[0], yDiff[0] The output of this script is: 25.0 -0.0999998997997 where the first value is right, the second is exactly 100 times smaller than the one it

Pendulum simulation in Python using odeint() not quite working as a pendulum

流过昼夜 提交于 2019-12-11 06:28:08
问题 I have built a pendulum simulation using fourth order Runge-Kutta differentiation where everything is done step by step: from scipy import * from matplotlib.pyplot import * ##A pendulum simulation using fourth order ##Runge-Kutta differentiation ts=.05 #time step size td=20 #trial duration te=int(td/ts) #no of timesteps mu=0.1 #friction factor m=1 #mass g=9.81 #grav. acceleration l=1 #length th=[((rand()*2)-1)*pi] #initial angle om=[0] #initial angular velocity u=0 #torque for j in range(te):

Multi-touch detecting & differentiating - Cocos2d for iPhone

时光怂恿深爱的人放手 提交于 2019-12-09 07:03:39
问题 I would like to know how to detect and differentiate between touches in a multi-touch view. I have read about a "hash" code, but I don't understand how to use it. I want to know when two of my Sprites are touched at the same time, like as if pressing a chord on two keys of a piano. [EDIT] Here is an example of what I have for my ccTouchesBegan: - (void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { NSSet *allTouches = [event allTouches]; int validTouchCount = 0; for (UITouch*

Maxima - differentiating a piecewise function

百般思念 提交于 2019-12-07 19:03:16
问题 Suppose you have a function defined by intervals, such as f(x):=block(if x<0 then x^2 else x^3); When we differentiate it with diff(f(x),x); we get d/dx (if x<0 then x^2 else x^3) whereas I'd like to get (if x<0 then 2*x else 3*x^2) Is there a way to obtain such result? 回答1: This may help in a simple case: (%i1) f(x):= charfun(x<0)*x^2 + charfun(x>=0)*x^3$ (%i2) gradef(charfun(y), 0)$ (%i3) diff(f(x),x); 2 (%o3) 2 x charfun(x < 0) + 3 x charfun(x >= 0) charfun, gradef You can try also Pw.mac

Derivative of a conjugate in sympy

我怕爱的太早我们不能终老 提交于 2019-12-07 18:58:13
问题 When I try to differentiate a symbol with SymPy I get the following In : x=Symbol('x') In : diff(x,x) Out: 1 When I differentiate the symbol respect to its conjugate the result is In [55]: diff(x,x.conjugate()) Out[55]: 0 However, when I try to differentiate the conjugate of the symbol SymPy doesn't do it In : diff(x.conjugate(),x) Out: Derivative(conjugate(x), x) This is still correct, but the result should be zero. How can I make SimPy perform the derivative of a conjugate? 回答1: I'm not