Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can create them like this

sage: V = [var(['v_'+str(i)+'_'+str(j) for j in xrange(4)]) for i in xrange(5)] 
sage: V
[(v_0_0, v_0_1, v_0_2, v_0_3),
 (v_1_0, v_1_1, v_1_2, v_1_3),
 (v_2_0, v_2_1, v_2_2, v_2_3),
 (v_3_0, v_3_1, v_3_2, v_3_3),
 (v_4_0, v_4_1, v_4_2, v_4_3)]
sage: X = var(['x'+str(i) for i in xrange(5)])   
sage: X
(x0, x1, x2, x3, x4)

And now, your indices will match the variable names:

sage: V[0][1]
v_0_1
sage: V[2][3]
v_2_3

And you can do derivatives:

sage: f = sum(V[i][j] * X[i] * X[j] for i in xrange(3) for j in xrange(3))
sage: f
v_0_0*x0^2 + v_0_1*x0*x1 + v_1_0*x0*x1 + v_1_1*x1^2 + v_0_2*x0*x2 + v_2_0*x0*x2 + v_1_2*x1*x2 + v_2_1*x1*x2 + v_2_2*x2^2
sage: f.derivative(V[0][0])
x0^2

You If your p and k are variables, then I don't know any way of doing it. But if they are fixed, then you can create them like this

sage: V = [var(['v_'+str(i)+'_'+str(j) for j in xrange(4)]) for i in xrange(5)] 
sage: V
[(v_0_0, v_0_1, v_0_2, v_0_3),
 (v_1_0, v_1_1, v_1_2, v_1_3),
 (v_2_0, v_2_1, v_2_2, v_2_3),
 (v_3_0, v_3_1, v_3_2, v_3_3),
 (v_4_0, v_4_1, v_4_2, v_4_3)]
sage: X = var(['x'+str(i) for i in xrange(5)])   
sage: X
(x0, x1, x2, x3, x4)

And now, your indices will match the variable names:

sage: V[0][1]
v_0_1
sage: V[2][3]
v_2_3

And you can do derivatives:

sage: f = sum(V[i][j] * X[i] * X[j] for i in xrange(3) for j in xrange(3))
sage: f
v_0_0*x0^2 + v_0_1*x0*x1 + v_1_0*x0*x1 + v_1_1*x1^2 + v_0_2*x0*x2 + v_2_0*x0*x2 + v_1_2*x1*x2 + v_2_1*x1*x2 + v_2_2*x2^2
sage: f.derivative(V[0][0])
x0^2