![]() | 1 | initial version |
Yes, SR matrices come in handy for that type of calculations. This:
# data
Qf = 1000000*matrix([[0,0],[0,1]]);
Xf = matrix([[1],[1675]]);
# matrix with symbolic coefficients
X = matrix([[var('x1')], [var('x2')]]);
# quadratic function
f = ((X-Xf).transpose()*Qf*(X-Xf));
# see result
Qf, Xf, X, f
produces
((0001000000),(11675),(x1x2),(1000000(x2−1675)2)).
To evaluate f, do f(x1=1,x2=1)
.
More generally, to define your X it can be useful to do something like:
# create a coefficients matrix of m rows and n columns
m = 4; n = 2;
xij = [[var('x'+str(1+i)+str(1+j)) for j in range(n)] for i in range(m)]
X = matrix(SR, xij)
X
(x11x12x21x22x31x32x41x42).
The quadratic function f defined above is a 1×1 matrix (convince yourself, for instance by reading to output of type(f)
). To take the gradient this is one possible way:
# passing from a 1x1 matrix to a scalar
f = f[0, 0]
grad_f = f.gradient([x1, x2])
# see result
grad_f
(0,2000000x2−3350000000).
![]() | 2 | No.2 Revision |
Yes, SR matrices come in handy for that type of calculations. This:calculations.
# data
Qf = 1000000*matrix([[0,0],[0,1]]);
Xf = matrix([[1],[1675]]);
# matrix with symbolic coefficients
X = matrix([[var('x1')], [var('x2')]]);
# quadratic function
f = ((X-Xf).transpose()*Qf*(X-Xf));
# see result
Qf, Xf, X, f
produces
((0001000000),(11675),(x1x2),(1000000(x2−1675)2)).
To evaluate f, do f(x1=1,x2=1)
.
More generally, to define your X it can be useful to do something like:
# create a coefficients matrix of m rows and n columns
m = 4; n = 2;
xij = [[var('x'+str(1+i)+str(1+j)) for j in range(n)] for i in range(m)]
X = matrix(SR, xij)
X
(x11x12x21x22x31x32x41x42).
The quadratic function f defined above is a 1×1 matrix (convince yourself, for instance by reading to output of type(f)
). To take the gradient this is one possible way:
# passing from a 1x1 matrix to a scalar
f = f[0, 0]
grad_f = f.gradient([x1, x2])
# see result
grad_f
(0,2000000x2−3350000000).
![]() | 3 | No.3 Revision |
Yes, SR matrices come in handy for that type of calculations.
# data
Qf = 1000000*matrix([[0,0],[0,1]]);
Xf = matrix([[1],[1675]]);
# matrix with symbolic coefficients
X = matrix([[var('x1')], [var('x2')]]);
# quadratic function
f = ((X-Xf).transpose()*Qf*(X-Xf));
# see result
Qf, Xf, X, f
produces
((0001000000),(11675),(x1x2),(1000000(x2−1675)2)).
To evaluate f, do f(x1=1,x2=1)
.
More generally, to define your X it can be useful to do something like:
# create a coefficients coefficient matrix of m rows and n columns
m = 4; n = 2;
xij = [[var('x'+str(1+i)+str(1+j)) for j in range(n)] for i in range(m)]
X = matrix(SR, xij)
X
(x11x12x21x22x31x32x41x42).
The quadratic function f defined above is a 1×1 matrix (convince yourself, for instance by reading to output of type(f)
). To take the gradient this is one possible way:
# passing from a 1x1 matrix to a scalar
f = f[0, 0]
grad_f = f.gradient([x1, x2])
# see result
grad_f
(0,2000000x2−3350000000).
![]() | 4 | No.4 Revision |
Yes, SR matrices come in handy for that type of calculations.
# data
Qf = 1000000*matrix([[0,0],[0,1]]);
Xf = matrix([[1],[1675]]);
# matrix with symbolic coefficients
X = matrix([[var('x1')], [var('x2')]]);
# quadratic function
f = ((X-Xf).transpose()*Qf*(X-Xf));
# see result
Qf, Xf, X, f
produces
((0001000000),(11675),(x1x2),(1000000(x2−1675)2)).
To evaluate f, do f(x1=1,x2=1)
.
More generally, to define your X it can be useful to do something like:
# create a coefficient matrix of m rows and n columns
m = 4; n = 2;
xij = [[var('x'+str(1+i)+str(1+j)) for j in range(n)] for i in range(m)]
X = matrix(SR, xij)
X
(x11x12x21x22x31x32x41x42).
The quadratic function f defined above is a 1×1 matrix (convince yourself, for instance by reading to the output of type(f)
). To take the gradient this is one possible way:
# passing from a 1x1 matrix to a scalar
f = f[0, 0]
grad_f = f.gradient([x1, x2])
# see result
grad_f
(0,2000000x2−3350000000).