Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
3

How to make a symbolic function of a matrix ?

asked 8 years ago

Babacool51 gravatar image

updated 1 year ago

FrédéricC gravatar image

Hi,

I'm beginning with Sage. I've got two matrix Qf and Xf defined by :

Qf = 1000000*matrix([[0,0],[0,1]]);
Xf = matrix([1],[1675]);

I would like to write a symbolic function "f" which would take a matrix X with 2 rows & 1 column.

X = var('X');
f(X) = ((X-Xf).transpose()*Qf*(X-Xf));

I easily wrote it with python non-symbolic function syntax, but i didn't find a way to make it symbolic. Because I'll need his gradient later (which is easy to calculate by hand, that I conceed ^^).

Maybe, it's related with SR matrix, no idea, i'm beginning with Sage and that's why I'm asking for help x)

Thank you in advance.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 8 years ago

mforets gravatar image

updated 8 years ago

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(x21675)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 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,2000000x23350000000).

Preview: (hide)
link

Comments

Ty so much :)

Babacool51 gravatar imageBabacool51 ( 8 years ago )
1

Just one last thing, is there any way to make elegant evaluation of f, as something like :

M = matrix([[1,2],[3,4],[5,6],[7,8]]);
print f(M);

Or eventually :

M = matrix([[1,2],[3,4],[5,6],[7,8]]);
print f(xij = M[i][j]);

You see the idea ^^

Babacool51 gravatar imageBabacool51 ( 8 years ago )

going back to the example of above, and if v = vector([1, 2]), then f(X=v) will unfortunately not work, but with f.substitute([X[i][0] == v[i] for i in range(2)]) it evaluates f at the point (1,2). I'm not sure if this is what you want to do, so don't hesitate to post a new question!

mforets gravatar imagemforets ( 8 years ago )

I works perfectly i'm actually gratefull, believe me :)

There is my code, if eventually it can serve to someone :

Qf = 1000000*matrix([[0,0],[0,1]]);
Xf = matrix([[1],[1675]]);
X = matrix(SR, [[var('X'+str(1+i)+str(1+j)) for j in range(1)] for i in range(2)]);
f = ((X-Xf).transpose()*Qf*(X-Xf))[0,0];

Xt = matrix([[1],[1]]);
print f;
print f.substitute([X[i,0]==Xt[i,0] for i in range(2)]);

-1000000.0*(X21 - 1659.0)*(-1.0*X21 + 1659.0)
-0.0
Babacool51 gravatar imageBabacool51 ( 8 years ago )

may i ask you something babacool51, in which context you are using this code? like course of math (linear algebra?) or another? thanks for any feedback!

mforets gravatar imagemforets ( 7 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 8 years ago

Seen: 882 times

Last updated: Feb 21 '17