在同义词中对矩阵进行逐列(或逐行)操作

时间:2015-09-19 01:12:02

标签: python matrix linear-algebra sympy

有没有办法在同义词中对矩阵进行逐列(或逐行)操作?例如,将矩阵的每一列除以其范数,或将矩阵的每一行乘以其范数?

1 个答案:

答案 0 :(得分:2)

您可以使用row_opcol_op。来自row_op的文档:

row_op(i, f) method of sympy.matrices.dense.MutableDenseMatrix instance
    In-place operation on row ``i`` using two-arg functor whose args are
    interpreted as ``(self[i, j], j)``.

这些方法就地采取行动:

>>> a = Matrix([[1, 2], [3, 4]])
>>> a.row_op(1, lambda i, j: i*2)
>>> a
Matrix([
[1, 2],
[6, 8]])