How to create a function that takes elements of a list as parameters to graph a function [closed]

How to create a function that takes elements of a list as parameters to graph a function [closed]



I have written the following code:


m = np.arange(-5, 5, 0.1)
q = np.arange(-500, 500, 0.1)
m, q = np.meshgrid(m, q)



Next, say:


a = [[1,2],[2,3]]



I want:


y = np.sqrt((a[0][0]*m + q - a[0][1])**2 + (a[1][0]*m + q - a[1][1])**2)



However I want to write it as:


y = np.sqrt((x[0]*m + q - x[1])**2 + (x[0]*m + q - x[1])**2)



where x in a, (i.e. x = [1,2] in the first case and x = [2,3])



and in general I am looking for a way to generalise this, so that I can write something like:


y = np.sqrt(sum((m*x[0]+q - x[1])**2 for x in a))



How can I do this?



(In the particular example it should return, but it needs to work for any a that is a list of a list of two numbers):
y = np.sqrt((1*m + q - 2)**2 + (2*m + q - 3)**2)



EDIT:


y = np.sqrt(sum((m*x[0]+q - x[1])**2 for x in a))



is actually correct code, my mistake was somewhere else.



This question appears to be off-topic. The users who voted to close gave this specific reason:





Could you give an example of an a that does not work? Seems like your code does what you want.
– Kevin Fang
Aug 27 at 1:02


a





@KevinFang Thank you! Yes, it does, the error was something else! Sorry!
– user
Aug 27 at 1:25





@KevinFang You should post the answer, your code is correct, and I would mark it as the right answer. Otherwise, not sure if I should close it.
– user
Aug 27 at 1:26





@user It's not necessarily an answer. Don't bother it too much. I'm glad you finally solved the problem!
– Kevin Fang
Aug 27 at 1:31




2 Answers
2



You can do exactly what you describe


>>> def y(x):
... return np.sqrt((x[0]*m + q - x[1])**2 + (x[0]*m + q - x[1])**2)
>>> y(a[0])
array([[ 717.00627612, 716.86485477, 716.72343341, ..., 703.28840457,
703.14698321, 703.00556186],
[ 716.86485477, 716.72343341, 716.58201205, ..., 703.14698321,
703.00556186, 702.8641405 ],
[ 716.72343341, 716.58201205, 716.4405907 , ..., 703.00556186,
702.8641405 , 702.72271914],
...,
[ 696.78302218, 696.92444354, 697.06586489, ..., 710.50089374,
710.64231509, 710.78373645],
[ 696.92444354, 697.06586489, 697.20728625, ..., 710.64231509,
710.78373645, 710.92515781],
[ 697.06586489, 697.20728625, 697.34870761, ..., 710.78373645,
710.92515781, 711.06657916]])
>>> y(a[1])
array([[ 725.4915575 , 725.20871478, 724.92587207, ..., 698.05581439,
697.77297167, 697.49012896],
[ 725.35013614, 725.06729343, 724.78445072, ..., 697.91439303,
697.63155032, 697.34870761],
[ 725.20871478, 724.92587207, 724.64302936, ..., 697.77297167,
697.49012896, 697.20728625],
...,
[ 688.29774081, 688.58058352, 688.86342623, ..., 715.73348392,
716.01632663, 716.29916934],
[ 688.43916216, 688.72200488, 689.00484759, ..., 715.87490527,
716.15774799, 716.4405907 ],
[ 688.58058352, 688.86342623, 689.14626894, ..., 716.01632663,
716.29916934, 716.58201205]])

>>> Y = [y(x) for x in a]
>>> for yy in Y:
... print('do something')



try


m = np.arange(-5, 5, 0.1)
q = np.arange(-500, 500, 0.1)
m, q = np.meshgrid(m, q)

a = np.array([[1,2],[2,3]])

y = np.sqrt(np.power(m[..., np.newaxis] * a[:, 0] + q[..., np.newaxis] - a[:, 1], 2).sum(axis=-1))

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ