Python에서 행렬은 2D 목록 또는 2D 배열로 구현될 수 있습니다. 후자로부터 행렬을 형성하면 행렬에서 다양한 작업을 수행하기 위한 추가 기능이 제공됩니다. 이러한 작업과 배열은 모듈에서 정의됩니다. 멍청하다 .
매트릭스에서의 작업:
- 1. add() :- 이 함수는 다음을 수행하는 데 사용됩니다. 요소별 행렬 덧셈 . 2. subtract() :- 이 함수는 다음을 수행하는 데 사용됩니다. 요소별 행렬 빼기 . 3. Divide() :- 이 함수는 다음을 수행하는 데 사용됩니다. 요소별 행렬 분할 .
구현:
파이썬
# 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))> |
>
>
출력 :
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))> |
>
>
출력 :
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)> |
>
>
출력 :
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를 0으로 초기화합니다.
- 중첩 루프 또는 목록 이해를 사용하여 행렬의 요소별 덧셈, 뺄셈 및 나눗셈을 수행합니다.
- 결과 행렬 C, D, E를 인쇄합니다.
파이썬3
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)