简体   繁体   中英

Calculate gradient of norm of a vector with respect to a vector in python

I want to calculate gradient (f(v(X))) where f(a) : second_norm(a);

X : a vector of 1*n dimension :: as an example for n == 2:  [x1, x2]
v(X) : (((x1)^m)*P + ((x2)^m)*Q)/(x1^m + x2^m);

where P and Q are vectors

So is there any function in python which can help me with this? If so, please elaborate.

I really need help! Thanks in advance!

If I understand your function P and Q should be two vectors of the same dimension. In this case it's enough to use numpy array. I put a very simple code that may help you:

import numpy as np
x1=2
x2=5
a=[x1,x2]
m=5

P=np.array([1,2,3,4])
Q=np.array([5,6,7,8])

print((  (a[0]**m)*P +(a[1]**m)*Q   )/(a[0]**m  + a[1]**m))

Output: array([4.95945518, 5.95945518, 6.95945518, 7.95945518])

In general if you want to multiply a vector with a scalar you need to use numpy array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM