Python -- numpy matrix operations

Author: The Little Dream, Created: 2017-01-12 12:47:58, Updated:

Python's numpy matrix operation

Note: NumPy is the successor to Numarray and is used to replace NumArray. SAGE is an integrated mathematics software package based on NumPy and several other tools, aiming to replace tools such as Magma, Maple, Mathematica and Matlab. Today I wanted to look online for some information about NumPy and when I tried to use NumPy to reverse the matrix, I couldn't find any information in Chinese, and some people in the forum asked me how to do it in Python.

  • 1 matrix object

    The matrix type inherits from the ndarray type and therefore contains all the data properties and methods of the ndarray. There are six important differences between the matrix type and the ndarray type, which can lead to unintended results when you operate the matrix object as an array.

    • 1) Matrix objects can be created using a Matlab-style string, which is a string separated by a space column separated by a decimal point.

    • 2) Matrix objects are always two-dimensional. This has far-reaching implications, for example, the return value of m.ravel (()) is two-dimensional, and the return value of the member selection is also two-dimensional, so the behavior of the sequence will be substantially different from that of the array.

    • 3) Multiplications of matrix type cover the multiplication of arrays, using the multiplication operation of the matrix. When you receive the return values of the matrix, make sure you understand the meaning of these functions. In particular, the fact that the function asanarray ((m)) returns a matrix if it is an mmatrix.

    • 4) The array of matrix types also covers the previous array, using the array of the matrix. Based on this fact, it should be recalled that if using the array of a matrix as an argument, the call assanarray[...] is the same as above.

    • 5) The default array_priority of the matrix is 10.0, so the operation of mixing narray and matrix objects always returns the matrix.

    • 6) The matrix has several unique properties that make calculation easier, these properties are:

      • (a).T -- returns to its own position

      • (b).H -- returns to its own resonance shift

      • ©.I -- Returns its own inverse matrix

      • (d).A -- a view of a 2-dimensional array that returns its own data ((without making any copies)

        The matrix class is a Python subclass of ndarray, and you can also learn this implementation to construct your own ndarray subclass. Matrix objects can also be constructed using other Matrix objects, characters, strings, or other parameters that can be converted to a ndarray. In addition, in NumPy, the matrix matrix is an alias for the matrix matrix.

  • Example 1: Using strings to construct a matrix

    import numpy as np
    a=np.mat('1 2 3; 4 5 3')
    print (a*a.T).I
    [[ 0.29239766 -0.13450292]
    [-0.13450292  0.08187135]]
    
  • Example 2: Matrix construction using nested sequences

    np.matrix([[  1.+0.j,   5.+0.j,  10.+0.j],
          [  1.+0.j,   3.+0.j,   0.+4.j]])
    
  • Example 3: Using an array to construct a matrix

    np.mat( np.random.rand(3,3) ).T
    np.matrix([[ 0.81541602,  0.73987459,  0.03509142],
          [ 0.14767449,  0.60539483,  0.05641679],
          [ 0.43257759,  0.628695  ,  0.47413553]])
    

    Matrix ((data, dtype=None, copy=True)) Converts data passed in with parameter data to a matrix. If dtype is None, the data type is determined by the data content. If copy is True, the data in the data is copied, otherwise the original data buffer is used. If the buffer area of the data is not found, of course the data is copied.候会调用matrix.new(matrix, data, dtype, copy) Matt, what's going on? It's just an alias for the matrix. Asmatrix ((data, dtype=None) is a data type that can be used for any number of data types. Returns data that has not been duplicated. Equivalent to matrix ((data, dtype, copy=False) ). Bmat (obj, ldict=None, gdict=None) Build a matrix using a string, nested sequence, or array. This command allows you to build a matrix from other objects. The arguments ldict and gdict are used when obj is a string.

    A=np.mat('2 2; 2 2'); B=np.mat('1 1; 1 1');
    print(np.bmat('A B; B A'))
      [[2 2 1 1]
       [2 2 1 1]
       [1 1 2 2]
       [1 1 2 2]]
    

Translated by su frank


More