calculate the gradient of the matrix with apply_map
I wrote this code to calculate the gradient of the matrix, but I got the wrong answer
W = matrix(SR, 2, var(','.join([f'w{i}' for i in range(4)]), domain=RR))
x = vector(SR, 2, var('x1,x2', domain=RR))
y = sum(W*x)
f(x) = y.diff(x)
W.apply_map(f)
output:
[0 0]
[0 0]
what I expect is
[x1 x2]
[x1 x2]
Welcome to Ask Sage!
Thank you for your question!
To display blocks of code or error messages in Ask Sage, skip a line above and below, and do one of the following (all give the same result):
For instance, typing
produces:
Please edit your question to do that.
I don't understand what you want to achieve with your code. Are you looking for the gradient of
y
as a function ofx
?In any case, does this question help?