Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One possibility:

def f(a, b, sigma=1, l=1): return sigma^2*exp(-1/(l^2)*sum(map(lambda u,v:(u-v)^2, a, b)))

Example of use :

sage: q=4
sage: X=vector([var("x_{}".format(u)) for u in range(q)])
sage: Y=vector([var("y_{}".format(u)) for u in range(q)])
sage: f(X,Y)
e^(-(x_0 - y_0)^2 - (x_1 - y_1)^2 - (x_2 - y_2)^2 - (x_3 - y_3)^2)
sage: f(X,Y).diff(x_1)
-2*(x_1 - y_1)*e^(-(x_0 - y_0)^2 - (x_1 - y_1)^2 - (x_2 - y_2)^2 - (x_3 - y_3)^2)
# Gradient of f(X,Y) as a function of X
sage: vector([f(X,Y).diff(u) for u in X])
(-2*(x_0 - y_0)*e^(-(x_0 - y_0)^2 - (x_1 - y_1)^2 - (x_2 - y_2)^2 - (x_3 - y_3)^2), -2*(x_1 - y_1)*e^(-(x_0 - y_0)^2 - (x_1 - y_1)^2 - (x_2 - y_2)^2 - (x_3 - y_3)^2), -2*(x_2 - y_2)*e^(-(x_0 - y_0)^2 - (x_1 - y_1)^2 - (x_2 - y_2)^2 - (x_3 - y_3)^2), -2*(x_3 - y_3)*e^(-(x_0 - y_0)^2 - (x_1 - y_1)^2 - (x_2 - y_2)^2 - (x_3 - y_3)^2))

Is that what you seek ?

A "real-world" use might check for equality of lengths of a and b and raise a convenient exception if not satisfied...