標籤:res wait keyword author 運算 矩陣 pow file uil
python numpy array 與matrix 乘方 程式設計語言 waitig 1年前 (2017-04-18) 1272℃ 百度已收錄 0評論
數組array 的乘方(**為乘方運算子)是每個元素的乘方,而矩陣matrix的乘方遵循矩陣相乘,因此必須是方陣。
2*3的數組與矩陣
>>> from numpy import *>>> import operator>>> a = array([[1,2,3],[4,5,6]])>>> aarray([[1, 2, 3], [4, 5, 6]])>>> m = mat(a)>>> mmatrix([[1, 2, 3], [4, 5, 6]])>>> a ** 2array([[ 1, 4, 9], [16, 25, 36]])>>> m ** 2Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\anaconda\lib\site-packages\numpy\matrixlib\defmatrix.py", line 356, in __pow__ return matrix_power(self, other) File "D:\anaconda\lib\site-packages\numpy\matrixlib\defmatrix.py", line 173, in matrix_power raise ValueError("input must be a square array")ValueError: input must be a square array>>>
(mat()函數把array轉化為matrix)
3*3的數組與矩陣
>>> A = array([[1,2,3],[4,5,6],[7,8,9]])>>> Aarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]])>>> M = mat(A)>>> Mmatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])>>> A ** 2array([[ 1, 4, 9], [16, 25, 36], [49, 64, 81]])>>> M ** 2matrix([[ 30, 36, 42], [ 66, 81, 96], [102, 126, 150]])
python numpy array 與matrix 乘方