In [1]:
import sympy as sym
import numpy as np
from IPython.display import display, Math
In [2]:
A = [[1,2],[3,4]]
print(A)
type(A)
[[1, 2], [3, 4]]
Out[2]:
list
In [3]:
A = np.array([[1,2],[3,4]])
print(A)
display(Math(sym.latex(sym.sympify(A))))
type(A)
[[1 2]
 [3 4]]
$\displaystyle \left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]$
Out[3]:
numpy.ndarray
In [4]:
mat = np.zeros([4,6])
mat
Out[4]:
array([[0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.]])
In [5]:
mat[0,1] = 2
mat[2,4] = 7
mat
Out[5]:
array([[0., 2., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 7., 0.],
       [0., 0., 0., 0., 0., 0.]])
In [6]:
numrange = range(0,4)

for rowi in numrange:
    for coli in numrange:
        mat[rowi,coli] = (-1)**(rowi+coli)
        
mat
Out[6]:
array([[ 1., -1.,  1., -1.,  0.,  0.],
       [-1.,  1., -1.,  1.,  0.,  0.],
       [ 1., -1.,  1., -1.,  7.,  0.],
       [-1.,  1., -1.,  1.,  0.,  0.]])
In [7]:
x,y = sym.symbols('x y')
Fxy = (4+x)*(2-y)

xyset = range(0,3)

outmat = np.zeros([len(xyset),len(xyset)])
outmat
Out[7]:
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
In [8]:
xyset
Out[8]:
range(0, 3)
In [9]:
for i in xyset:
    for j in xyset:
        outmat[i,j] = Fxy.subs({x:i,y:j})

display(Math(sym.latex(sym.sympify(outmat))))
$\displaystyle \left[\begin{matrix}8.0 & 4.0 & 0.0\\10.0 & 5.0 & 0.0\\12.0 & 6.0 & 0.0\end{matrix}\right]$