简单 ipython 脚本中的错误结果

Wrong result in a simple ipython script

我定义了一个对 3x3 矩阵的第 1 行和第 3 行进行操作的简单函数:

In [1]: from numpy import *    
     from sympy import Symbol

In [2]: def ifsin(mat): 
     mat[2,:]+=1
     mat[0,:]=mat[0,:]/pow(mat[2,:],mat[2,:]>0)
     return mat

In [3]:Ay=Symbol('Ay')
     By=Symbol('By')
     q=array([[Ay,-10 ,By],[0,0.4,1],[-1 ,-1, -1]])
     q

Out[3]:

 array([[Ay, -10, By],
        [0, 0.4, 1],
        [-1, -1, -1]], dtype=object)

In [4]:V=ifsin(q)
        q

Out[4]: array([[Ay, -10.0, By],
        [0, 0.4, 1],
        [0, 0, 0]], dtype=object)

为什么要更新矩阵 q 的第 3 行?

此外,如果我评估以下内容:

In [5]:M=ifsin(V)
       q

Out[5]: array([[Ay, -10.0, By],
            [0, 0.4, 1],
            [1, 1, 1]], dtype=object)

q 的第 3 行再次更新!!

我在 "Computable"(ipad 应用程序)和 ipython 笔记本上 ubuntu 14.04(python 2.7.6)上尝试了这个脚本结果

在此先感谢您的帮助。

您实际上是在要求这一行的更新:

mat[2,:]+=1

在 numpy 语法中,这意味着 "update row 2 of the array named mat by incrementing each value by 1"。