Reputation: 105
Consider, for instance, the following formula:
$$f_{nm}(x) = \sin(n \ m \ x^2/\pi)$$
where $n$ and $m$ both are integers $\in [1,4]$. Forming a matrix with this information would result in:
$$\mathbf{F} = \begin{pmatrix}\ f_{11} & f_{12} & f_{13} & f_{14} \\ f_{21} & f_{22} & f_{23} & f_{24} \\ f_{31} & f_{32} & f_{33} & f_{34} \\ f_{41} &f_{42}&f_{43}&f_{44}\end{pmatrix}$$
How to create such a $4\times4$ matrix in Python and fill it with the provided formula for a given $x$ please. I know this question may be off-topic here, but the $LaTeX$ style of the equations did not work for me in the StackOverflow, that is why posted it here.
Upvotes: 0
Views: 89
Reputation: 877
A faster implementation avoiding Python loops (if I understood your question correctly) that assumes that also x
is an array from 0 to 1 and exploits Numpy Broadcasting
import numpy as np
n = np.arange(1, 5)
m = n.reshape(4, 1)
x = np.linspace(0, 1, 100).reshape(100, 1, 1)
result = np.sin(n * m * x**2 / np.pi)
print(result.shape)
This gives a result array of dimensions:
(100, 4, 4)
where the first dimensions indexes the x
value. That is:
result[0, ...]
is the 4x4 matrix corresponding to x = x[0] = 0
Upvotes: 0
Reputation: 26
For a fixed $x$ you should be able to do this
import numpy as np
f = np.zeros((4,4))
for n in range(4):
for m in range(4):
f[n][m] = np.sin(n*m*(x**2)/np.pi)
Upvotes: 1