În python, matricea poate fi implementată ca listă 2D sau matrice 2D. Formarea matricei din acesta din urmă oferă funcționalități suplimentare pentru efectuarea diferitelor operații în matrice. Aceste operațiuni și matrice sunt definite în modul numpy .
Operație pe matrice:
- 1. add() :- Această funcție este folosită pentru a efectua adunarea matricei în funcție de elemente . 2. subtract() :- Această funcție este folosită pentru a efectua scăderea matricei în funcție de elemente . 3. divide() :- Această funcție este folosită pentru a efectua diviziune matriceală în funcție de elemente .
Implementare:
Piton
# 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))> |
>
vlc descărca videoclipuri youtube
>
Ieșire:
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. multiplicare() :- Această funcție este folosită pentru a efectua înmulțirea matriceală în funcție de elemente . 5. dot() :- Această funcție este folosită pentru a calcula înmulțirea matriceală, mai degrabă decât înmulțirea în funcție de elemente .
Piton
# 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))> |
>
>
Ieșire:
The element wise multiplication of matrix is : [[ 7 16] [36 50]] The product of matrices is : [[25 28] [73 82]]>
- 6. sqrt() :- Această funcție este folosită pentru a calcula rădăcina pătrată a fiecărui element de matrice. 7. sum(x,axis) :- Această funcție este folosită pentru adăugați toate elementele din matrice . Argumentul opțional al axei calculează suma coloanei dacă axa este 0 și suma rândurilor dacă axa este 1 . 8. T :- Acest argument este folosit pentru a transpune matricea specificată.
Implementare:
Piton
pauză java
# 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)> |
>
>
Ieșire:
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]]>
Folosind bucle imbricate:
Abordare:
- Definiți matricele A și B.
- Obțineți numărul de rânduri și coloane ale matricelor folosind funcția len().
- Inițializați matricele C, D și E cu zerouri folosind bucle imbricate sau înțelegerea listelor.
- Utilizați bucle imbricate sau înțelegerea listelor pentru a efectua adunarea, scăderea și împărțirea matricelor în funcție de elemente.
- Tipăriți matricele rezultate C, D și E.
Python3
repl în java
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)> |
>
>Ieșire
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]]>
Complexitatea timpului: O(n^2)
Complexitatea spațiului: O(n^2)