当前位置: 首页>>代码示例>>Python>>正文


Python Matrix.reshape方法代码示例

本文整理汇总了Python中sympy.Matrix.reshape方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.reshape方法的具体用法?Python Matrix.reshape怎么用?Python Matrix.reshape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sympy.Matrix的用法示例。


在下文中一共展示了Matrix.reshape方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: printnp

# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import reshape [as 别名]
def printnp(m):
    """
    Prints a sympy matrix in the same format as a numpy array
    """
    m = Matrix(m)
    if m.shape[1] == 1:
        m = m.reshape(1, m.shape[0])
    elem_len = max(num_chars(d) for d in m.vec())
    if m.shape[0] > 1:
        outstr = '[['
    else:
        outstr = '['
    for i in xrange(m.shape[0]):
        if i:
            outstr += ' ['
        char_count = 0
        for j, elem in enumerate(m[i, :]):
            char_count += elem_len
            if char_count > 77:
                outstr += '\n '
                char_count = elem_len + 2
            # Add spaces
            outstr += ' ' * (elem_len - num_chars(elem) +
                             int(j != 0)) + str(elem)

        if i < m.shape[0] - 1:
            outstr += ']\n'
    if m.shape[0] > 1:
        outstr += ']]'
    else:
        outstr += ']'
    print outstr
开发者ID:RamaAlvim,项目名称:Diophantine,代码行数:34,代码来源:diophantine.py

示例2: solve

# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import reshape [as 别名]
def solve(A, b):
    """
    Finds small solutions to systems of diophantine equations, A x = b, where A
    is a M x N matrix of coefficents, b is a M x 1 vector and x is the
    N x 1 solution vector, e.g.

    >>> from sympy import Matrix
    >>> from diophantine import solve
    >>> A = Matrix([[1, 0, 0, 2], [0, 2, 3, 5], [2, 0, 3, 1], [-6, -1, 0, 2],
                    [0, 1, 1, 1], [-1, 2, 0,1], [-1, -2, 1, 0]]).T
    >>> b = Matrix([1, 1, 1, 1])
    >>> solve(A, b)
    [Matrix([
    [-1],
    [ 1],
    [ 0],
    [ 0],
    [-1],
    [-1],
    [-1]])]

    The returned solution vector will tend to be one with the smallest norms.
    If multiple solutions with the same norm are found they will all be
    returned. If there are no solutions the empty list will be returned.
    """
    A = Matrix(A)
    b = Matrix(b)
    if b.shape != (A.shape[0], 1):
        raise Exception("Length of b vector ({}) does not match number of rows"
                        " in A matrix ({})".format(b.shape[0], A.shape[0]))
    G = zeros(A.shape[1] + 1, A.shape[0] + 1)
    G[:-1, :-1] = A.T
    G[-1, :-1] = b.reshape(1, b.shape[0])
    G[-1, -1] = 1
    # A is m x n, b is m x 1, solving AX=b, X is n x 1+
    # Ab is the (n+1) x m transposed augmented matrix. G=[A^t|0] [b^t]1]
    hnf, P, rank = lllhermite(G)
    r = rank - 1  # For convenience
    if not any(chain(hnf[:r, -1], hnf[r, :-1])) and hnf[r, -1] == 1:
        nullity = hnf.shape[0] - rank
        if nullity:
            basis = P[rank:, :-1].col_join(-P[r, :-1])
            solutions = get_solutions(basis)
        else:
            raise NotImplementedError("Ax=B has unique solution in integers")
    else:
        solutions = []
    return solutions
开发者ID:tclose,项目名称:Diophantine,代码行数:50,代码来源:diophantine.py

示例3: test_reshape

# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import reshape [as 别名]
def test_reshape():
    m0 = eye(3)
    assert m0.reshape(1,9) == Matrix(1,9,(1,0,0,0,1,0,0,0,1))
    m1 = Matrix(3,4, lambda i,j: i+j)
    assert m1.reshape(4,3) == Matrix(((0,1,2), (3,1,2), (3,4,2), (3,4,5)))
    assert m1.reshape(2,6) == Matrix(((0,1,2,3,1,2), (3,4,2,3,4,5)))
开发者ID:Lucaweihs,项目名称:sympy,代码行数:8,代码来源:test_matrices.py


注:本文中的sympy.Matrix.reshape方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。