尝试用pylab做线性代数

时间:2012-09-15 18:22:08

标签: python matplotlib algebra linear

我正在尝试用pylab做一些基本的线性代数并将结果可视化,特别是在点网格上的变换y = Ax。我认为我有它的工作,但我相信有更好的方法(更好,更好)然后我做了。我的代码如下所示,关于如何改进它的任何建议,无论是将变换应用于网格还是绘制结果,都将非常感激。我的代码在..........

之下
from pylab import *
## Linear algebra
# y = Ax

A = array([[2, 1], [1.5, 2]]) # np array definition
x = array([[0.75], [0.25]])
y = dot(A,x)                  # matrix multiplication for np array

plot(x[0], x[1], 'ob', y[0], y[1], 'or')    # plot points
plot([x[0],y[0]],[x[1],y[1]])               # plot line
suptitle('The matrix A transforms the point (0.75, 0.25) to (1.75, 1.625)')
show()

figure()
p = [1,2,3,4,5]
q = [1,2,3,4,5]
m=meshgrid(p,q)        # Create grid for plotting
plot(m[0],m[1], 'ob')
for i in range(1,6): hlines(i, 1, 5)
for i in range(1,6): vlines(i, 1, 5)

# Apply transform to every point in grid.
ygrid = zeros(shape(m))
for i in range(5):
  for j in range(5):
    x = array([m[0][i][j], m[1][i][j]])
    y = dot(A,x)         
    ygrid[0][i][j] = y[0]
    ygrid[1][i][j] = y[1]
    plot(x[0], x[1], 'ob', y[0], y[1], 'or')

suptitle('Using the transform y=Mx on a grid of points')
show()

感谢, d

0 个答案:

没有答案