Python では、行列は 2D リストまたは 2D 配列として実装できます。後者から行列を形成すると、行列でさまざまな操作を実行するための追加機能が得られます。これらの操作と配列はモジュールで定義されます しこり 。
行列の演算:
- 1. add() :- この関数は以下を実行するために使用されます。 要素ごとの行列加算 。 2.subtract() :- この関数は以下を実行するために使用されます。 要素ごとの行列の減算 。 3. Division() :- この関数は次の実行に使用されます。 要素ごとの行列の除算 。
実装:
パイソン
# Python code to demonstrate matrix operations> # add(), subtract() and divide()> > # importing numpy for matrix operations> import> numpy> > # initializing matrices> x>=> numpy.array([[>1>,>2>], [>4>,>5>]])> y>=> numpy.array([[>7>,>8>], [>9>,>10>]])> > # using add() to add matrices> print> (>'The element wise addition of matrix is : '>)> print> (numpy.add(x,y))> > # using subtract() to subtract matrices> print> (>'The element wise subtraction of matrix is : '>)> print> (numpy.subtract(x,y))> > # using divide() to divide matrices> print> (>'The element wise division of matrix is : '>)> print> (numpy.divide(x,y))> |
>
ターボC++のダウンロード
>
出力:
The element wise addition of matrix is : [[ 8 10] [13 15]] The element wise subtraction of matrix is : [[-6 -6] [-5 -5]] The element wise division of matrix is : [[ 0.14285714 0.25 ] [ 0.44444444 0.5 ]]>
- 4. multiply() :- この関数は実行するために使用されます。 要素ごとの行列乗算 。 5. dot() :- この関数は、 要素ごとの乗算ではなく、行列の乗算 。
パイソン
# Python code to demonstrate matrix operations> # multiply() and dot()> > # importing numpy for matrix operations> import> numpy> > # initializing matrices> x>=> numpy.array([[>1>,>2>], [>4>,>5>]])> y>=> numpy.array([[>7>,>8>], [>9>,>10>]])> > # using multiply() to multiply matrices element wise> print> (>'The element wise multiplication of matrix is : '>)> print> (numpy.multiply(x,y))> > # using dot() to multiply matrices> print> (>'The product of matrices is : '>)> print> (numpy.dot(x,y))> |
>
>
CSSの折り返しテキスト
出力:
The element wise multiplication of matrix is : [[ 7 16] [36 50]] The product of matrices is : [[25 28] [73 82]]>
- 6. sqrt() :- この関数は、 各要素の平方根 マトリックスの。 7. sum(x,axis) :- この関数は次の目的で使用されます。 行列内のすべての要素を追加します 。オプションの軸引数は、 軸が 0 の場合の列の合計 そして 軸が 1 の場合の行の合計 。 8. T :- この引数は次の目的で使用されます。 転置 指定された行列。
実装:
パイソン
# Python code to demonstrate matrix operations> # sqrt(), sum() and 'T'> > # importing numpy for matrix operations> import> numpy> > # initializing matrices> x>=> numpy.array([[>1>,>2>], [>4>,>5>]])> y>=> numpy.array([[>7>,>8>], [>9>,>10>]])> > # using sqrt() to print the square root of matrix> print> (>'The element wise square root is : '>)> print> (numpy.sqrt(x))> > # using sum() to print summation of all elements of matrix> print> (>'The summation of all matrix element is : '>)> print> (numpy.>sum>(y))> > # using sum(axis=0) to print summation of all columns of matrix> print> (>'The column wise summation of all matrix is : '>)> print> (numpy.>sum>(y,axis>=>0>))> > # using sum(axis=1) to print summation of all columns of matrix> print> (>'The row wise summation of all matrix is : '>)> print> (numpy.>sum>(y,axis>=>1>))> > # using 'T' to transpose the matrix> print> (>'The transpose of given matrix is : '>)> print> (x.T)> |
>
Javaから印刷する
>
出力:
The element wise square root is : [[ 1. 1.41421356] [ 2. 2.23606798]] The summation of all matrix element is : 34 The column wise summation of all matrix is : [16 18] The row wise summation of all matrix is : [15 19] The transpose of given matrix is : [[1 4] [2 5]]>
ネストされたループの使用:
アプローチ:
- 行列 A と B を定義します。
- len() 関数を使用して行列の行数と列数を取得します。
- ネストされたループまたはリスト内包表記を使用して、行列 C、D、および E をゼロで初期化します。
- ネストされたループまたはリスト内包表記を使用して、行列の要素ごとの加算、減算、除算を実行します。
- 結果として得られる行列 C、D、および E を出力します。
Python3
A>=> [[>1>,>2>],[>4>,>5>]]> B>=> [[>7>,>8>],[>9>,>10>]]> rows>=> len>(A)> cols>=> len>(A[>0>])> > # Element wise addition> C>=> [[>0> for> i>in> range>(cols)]>for> j>in> range>(rows)]> for> i>in> range>(rows):> >for> j>in> range>(cols):> >C[i][j]>=> A[i][j]>+> B[i][j]> print>(>'Addition of matrices:
'>, C)> > # Element wise subtraction> D>=> [[>0> for> i>in> range>(cols)]>for> j>in> range>(rows)]> for> i>in> range>(rows):> >for> j>in> range>(cols):> >D[i][j]>=> A[i][j]>-> B[i][j]> print>(>'Subtraction of matrices:
'>, D)> > # Element wise division> E>=> [[>0> for> i>in> range>(cols)]>for> j>in> range>(rows)]> for> i>in> range>(rows):> >for> j>in> range>(cols):> >E[i][j]>=> A[i][j]>/> B[i][j]> print>(>'Division of matrices:
'>, E)> |
>
ビープラスツリー
>出力
Addition of matrices: [[8, 10], [13, 15]] Subtraction of matrices: [[-6, -6], [-5, -5]] Division of matrices: [[0.14285714285714285, 0.25], [0.4444444444444444, 0.5]]>
時間計算量: O(n^2)
空間複雑さ: O(n^2)