Café du jeudi: exemple 5

132 days ago by thierry.chappuis

Algèbre linéaire

Sage propose de nombreuses possibilité pour l'algèbre linéaire

A = Matrix(QQ, [[0, -1, -1, 1], [1, 1, 1, 1], [2, 4, 1, -2], [3, 1, -2, 2]]) print("Matrix A:") show(A) # Getting elements of a matrix print("A[0] = {0}".format(A[0])) print("A[1, 2] = {0}".format(A[1, 2])) print("A[2, 1] = {0}".format(A[2, 1])) print("A[0:2]") show(A[0:2]) print("A[0, 2:4] = {0}".format(A[0, 2:4])) print("A[:,0]:") show(A[:,0]) # Getting parts of a matrix print("Third row:") print(A.row(2)) print("Second column:") print(A.column(1)) print("Lower right submatrix:") show(A.submatrix(2, 2, 2, 2)) 
       
Matrix A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 0 & -1 & -1 & 1 \\ 1 & 1 & 1 & 1 \\ 2 & 4 & 1 & -2 \\ 3 & 1 & -2 & 2 \end{array}\right)
A[0] = (0, -1, -1, 1) A[1, 2] = 1 A[2, 1] = 4 A[0:2]
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 0 & -1 & -1 & 1 \\ 1 & 1 & 1 & 1 \end{array}\right)
A[0, 2:4] = [-1 1] A[:,0]:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 0 \\ 1 \\ 2 \\ 3 \end{array}\right)
Third row: (2, 4, 1, -2) Second column: (-1, 1, 4, 1) Lower right submatrix:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} 1 & -2 \\ -2 & 2 \end{array}\right)
Matrix A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 0 & -1 & -1 & 1 \\ 1 & 1 & 1 & 1 \\ 2 & 4 & 1 & -2 \\ 3 & 1 & -2 & 2 \end{array}\right)
A[0] = (0, -1, -1, 1) A[1, 2] = 1 A[2, 1] = 4 A[0:2]
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrr} 0 & -1 & -1 & 1 \\ 1 & 1 & 1 & 1 \end{array}\right)
A[0, 2:4] = [-1 1] A[:,0]:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 0 \\ 1 \\ 2 \\ 3 \end{array}\right)
Third row: (2, 4, 1, -2) Second column: (-1, 1, 4, 1) Lower right submatrix:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} 1 & -2 \\ -2 & 2 \end{array}\right)
A = Matrix(QQ, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("Matrix A:") show(A) # Elementary row operations print("Scaling second row by two:") A.rescale_row(1, 2) show(A) print("Swapping first and second rows:") A.swap_rows(0, 1) show(A) print("Adding 3*(row 1) to row 0:") A.add_multiple_of_row(0, 1 ,3) show(A) print("A in echelon form:") show(A.echelon_form()) 
       
Matrix A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\right)
Scaling second row by two:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & 2 & 3 \\ 8 & 10 & 12 \\ 7 & 8 & 9 \end{array}\right)
Swapping first and second rows:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 8 & 10 & 12 \\ 1 & 2 & 3 \\ 7 & 8 & 9 \end{array}\right)
Adding 3*(row 1) to row 0:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 11 & 16 & 21 \\ 1 & 2 & 3 \\ 7 & 8 & 9 \end{array}\right)
A in echelon form:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & 0 & -1 \\ 0 & 1 & 2 \\ 0 & 0 & 0 \end{array}\right)
Matrix A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\right)
Scaling second row by two:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & 2 & 3 \\ 8 & 10 & 12 \\ 7 & 8 & 9 \end{array}\right)
Swapping first and second rows:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 8 & 10 & 12 \\ 1 & 2 & 3 \\ 7 & 8 & 9 \end{array}\right)
Adding 3*(row 1) to row 0:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 11 & 16 & 21 \\ 1 & 2 & 3 \\ 7 & 8 & 9 \end{array}\right)
A in echelon form:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 1 & 0 & -1 \\ 0 & 1 & 2 \\ 0 & 0 & 0 \end{array}\right)
M3 = MatrixSpace(QQ, 3, 2) A = M3.matrix([[3, 2, 1], [4, 5, 6]]) B = M3.matrix([[2, 2, 2], [1, 2, 3]]) print("Matrix addition:") show(A + B) print("Scalar multiplication:") show(1/2 * A) var('a b c d e f') C = Matrix(QQ, [[4, 2, 1], [5, 3, 7]]) D = Matrix(SR, [[a, b], [c, d], [e, f]]) print("Matrix multiplication:") show(C * D) var('x1 x2 x3') X = vector([x1,x2,x3]) print("Multiplying a matrix and a vector:") show(C * X) 
       
Matrix addition:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} 5 & 4 \\ 3 & 5 \\ 7 & 9 \end{array}\right)
Scalar multiplication:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} \frac{3}{2} & 1 \\ \frac{1}{2} & 2 \\ \frac{5}{2} & 3 \end{array}\right)
Matrix multiplication:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} 4 \, a + 2 \, c + e & 4 \, b + 2 \, d + f \\ 5 \, a + 3 \, c + 7 \, e & 5 \, b + 3 \, d + 7 \, f \end{array}\right)
Multiplying a matrix and a vector:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(4 \, x_{1} + 2 \, x_{2} + x_{3},\,5 \, x_{1} + 3 \, x_{2} + 7 \, x_{3}\right)
Matrix addition:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} 5 & 4 \\ 3 & 5 \\ 7 & 9 \end{array}\right)
Scalar multiplication:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} \frac{3}{2} & 1 \\ \frac{1}{2} & 2 \\ \frac{5}{2} & 3 \end{array}\right)
Matrix multiplication:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rr} 4 \, a + 2 \, c + e & 4 \, b + 2 \, d + f \\ 5 \, a + 3 \, c + 7 \, e & 5 \, b + 3 \, d + 7 \, f \end{array}\right)
Multiplying a matrix and a vector:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(4 \, x_{1} + 2 \, x_{2} + x_{3},\,5 \, x_{1} + 3 \, x_{2} + 7 \, x_{3}\right)
A = matrix(QQ, [[2, 5, 4], [3, 1, 2], [5, 4, 6]]) print("Matrix A:") show(A) # Scalar operations print("Determinant of A: {0}".format(A.det())) print("Rank of A: {0}".format(A.rank())) print("Euclidean norm: {0}".format(A.norm())) print("Frobenius norm: {0}".format(A.norm('frob'))) # Matrix operations print("Transpose of A:") show(A.transpose()) print("Inverse of A:") show(A.inverse()) print("Adjoint of A:") show(A.adjoint()) print("Testing adj(A)/det(A) == inverse(A)") A.adjoint()/A.det() == A.inverse() 
       
Matrix A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 2 & 5 & 4 \\ 3 & 1 & 2 \\ 5 & 4 & 6 \end{array}\right)
Determinant of A: -16 Rank of A: 3 Euclidean norm: 11.3469601386 Frobenius norm: 11.6619037897 Transpose of A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 2 & 3 & 5 \\ 5 & 1 & 4 \\ 4 & 2 & 6 \end{array}\right)
Inverse of A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} \frac{1}{8} & \frac{7}{8} & -\frac{3}{8} \\ \frac{1}{2} & \frac{1}{2} & -\frac{1}{2} \\ -\frac{7}{16} & -\frac{17}{16} & \frac{13}{16} \end{array}\right)
Adjoint of A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} -2 & -14 & 6 \\ -8 & -8 & 8 \\ 7 & 17 & -13 \end{array}\right)
Testing adj(A)/det(A) == inverse(A) True
Matrix A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 2 & 5 & 4 \\ 3 & 1 & 2 \\ 5 & 4 & 6 \end{array}\right)
Determinant of A: -16 Rank of A: 3 Euclidean norm: 11.3469601386 Frobenius norm: 11.6619037897 Transpose of A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} 2 & 3 & 5 \\ 5 & 1 & 4 \\ 4 & 2 & 6 \end{array}\right)
Inverse of A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} \frac{1}{8} & \frac{7}{8} & -\frac{3}{8} \\ \frac{1}{2} & \frac{1}{2} & -\frac{1}{2} \\ -\frac{7}{16} & -\frac{17}{16} & \frac{13}{16} \end{array}\right)
Adjoint of A:
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrr} -2 & -14 & 6 \\ -8 & -8 & 8 \\ 7 & 17 & -13 \end{array}\right)
Testing adj(A)/det(A) == inverse(A) True