{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Some basic linear algebra routines in Sage" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#Execute this line to activate (shift-enter)\n", "%display latex #Format output with LaTeX\n", "latex.matrix_delimiters(\"[\", \"]\") #Matrix delimeters square brackets instead of parentheses" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "## Generate random matrices with prescribed properties" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Generate a 4x5 random matrix with rational entries, rank 3, coefficients bounded whose \n", "# RREF is guaranteed to have integer values" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrr}\n", "1 & 4 & 13 & -6 & -10 \\\\\n", "1 & 5 & 17 & -7 & -12 \\\\\n", "0 & -1 & -4 & 2 & 4 \\\\\n", "-1 & 0 & 3 & 7 & 12\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 4 13 -6 -10]\n", "[ 1 5 17 -7 -12]\n", "[ 0 -1 -4 2 4]\n", "[ -1 0 3 7 12]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A=random_matrix(QQ,4,5,algorithm='echelonizable', rank=3, upper_bound=20); A" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Generate random 6x6 matrix which is diagonalizable with prescribed eigenvalues and multiplicities\n", "# Discussion of finding the eigenvectors is further below" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrr}\n", "-1144 & -256 & -272 & -4288 & 3552 & 828 \\\\\n", "-282 & -60 & -176 & -728 & 760 & -230 \\\\\n", "712 & 160 & 244 & 2448 & -2128 & -232 \\\\\n", "216 & 48 & 64 & 772 & -656 & -104 \\\\\n", "-72 & -16 & 0 & -320 & 244 & 120 \\\\\n", "2 & 0 & -16 & 56 & -24 & -62\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[-1144 -256 -272 -4288 3552 828]\n", "[ -282 -60 -176 -728 760 -230]\n", "[ 712 160 244 2448 -2128 -232]\n", "[ 216 48 64 772 -656 -104]\n", "[ -72 -16 0 -320 244 120]\n", "[ 2 0 -16 56 -24 -62]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = random_matrix(QQ, 6, algorithm='diagonalizable', eigenvalues=[-12,4,6],dimensions=[2,3,1]); B" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "## Generate random unimodular matrix with bounded entries" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\left[\\begin{array}{rrrr}\n", "0 & 0 & -1 & 3 \\\\\n", "0 & 1 & 1 & -7 \\\\\n", "-1 & 1 & -2 & -4 \\\\\n", "-1 & 2 & -3 & -6\n", "\\end{array}\\right], \\left[\\begin{array}{rrrr}\n", "-9 & -5 & -7 & 6 \\\\\n", "9 & 5 & 4 & -4 \\\\\n", "5 & 3 & 3 & -3 \\\\\n", "2 & 1 & 1 & -1\n", "\\end{array}\\right]\\right)\n", "\\end{math}" ], "text/plain": [ "(\n", "[ 0 0 -1 3] [-9 -5 -7 6]\n", "[ 0 1 1 -7] [ 9 5 4 -4]\n", "[-1 1 -2 -4] [ 5 3 3 -3]\n", "[-1 2 -3 -6], [ 2 1 1 -1]\n", ")" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B=random_matrix(QQ,4,algorithm='unimodular',upper_bound=10); (B, B.inverse())" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# To generate a random unitary matrix seems to be harder; there may be other ways" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrr}\n", "0 & 1 & -5 & 9 \\\\\n", "-1 & 1 & -1 & 3 \\\\\n", "0 & 1 & -4 & 7 \\\\\n", "0 & 1 & -4 & 8\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 0 1 -5 9]\n", "[-1 1 -1 3]\n", "[ 0 1 -4 7]\n", "[ 0 1 -4 8]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Start with a unimodular matrix\n", "B=random_matrix(QQ,4,algorithm='unimodular',upper_bound=10);B" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrr}\n", "0 & 1 & -5 & 9 \\\\\n", "-1 & \\frac{74}{107} & \\frac{58}{107} & \\frac{24}{107} \\\\\n", "\\frac{2}{39} & \\frac{7}{39} & -\\frac{4}{39} & -\\frac{1}{13} \\\\\n", "\\frac{1}{5} & \\frac{1}{10} & \\frac{1}{5} & \\frac{1}{10}\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 0 1 -5 9]\n", "[ -1 74/107 58/107 24/107]\n", "[ 2/39 7/39 -4/39 -1/13]\n", "[ 1/5 1/10 1/5 1/10]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use Gram-Schmidt to give orthogonal rows\n", "G,M=B.gram_schmidt();G" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\left(0,\\,\\frac{1}{107} \\, \\sqrt{107},\\,-\\frac{5}{107} \\, \\sqrt{107},\\,\\frac{9}{107} \\, \\sqrt{107}\\right), \\left(-\\frac{107}{195} \\, \\sqrt{\\frac{195}{107}},\\,\\frac{74}{195} \\, \\sqrt{\\frac{195}{107}},\\,\\frac{58}{195} \\, \\sqrt{\\frac{195}{107}},\\,\\frac{8}{65} \\, \\sqrt{\\frac{195}{107}}\\right), \\left(\\sqrt{\\frac{2}{39}},\\,\\frac{7}{2} \\, \\sqrt{\\frac{2}{39}},\\,-2 \\, \\sqrt{\\frac{2}{39}},\\,-\\frac{3}{2} \\, \\sqrt{\\frac{2}{39}}\\right), \\left(2 \\, \\sqrt{\\frac{1}{10}},\\,\\sqrt{\\frac{1}{10}},\\,2 \\, \\sqrt{\\frac{1}{10}},\\,\\sqrt{\\frac{1}{10}}\\right)\\right]\n", "\\end{math}" ], "text/plain": [ "[(0, 1/107*sqrt(107), -5/107*sqrt(107), 9/107*sqrt(107)),\n", " (-107/195*sqrt(195/107), 74/195*sqrt(195/107), 58/195*sqrt(195/107), 8/65*sqrt(195/107)),\n", " (sqrt(2/39), 7/2*sqrt(2/39), -2*sqrt(2/39), -3/2*sqrt(2/39)),\n", " (2*sqrt(1/10), sqrt(1/10), 2*sqrt(1/10), sqrt(1/10))]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Give a list of the rows of G normalized\n", "U=[G[i].normalized() for i in range(G.nrows())]; U" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrr}\n", "0 & \\frac{1}{107} \\, \\sqrt{107} & -\\frac{5}{107} \\, \\sqrt{107} & \\frac{9}{107} \\, \\sqrt{107} \\\\\n", "-\\frac{107}{195} \\, \\sqrt{\\frac{195}{107}} & \\frac{74}{195} \\, \\sqrt{\\frac{195}{107}} & \\frac{58}{195} \\, \\sqrt{\\frac{195}{107}} & \\frac{8}{65} \\, \\sqrt{\\frac{195}{107}} \\\\\n", "\\sqrt{\\frac{2}{39}} & \\frac{7}{2} \\, \\sqrt{\\frac{2}{39}} & -2 \\, \\sqrt{\\frac{2}{39}} & -\\frac{3}{2} \\, \\sqrt{\\frac{2}{39}} \\\\\n", "2 \\, \\sqrt{\\frac{1}{10}} & \\sqrt{\\frac{1}{10}} & 2 \\, \\sqrt{\\frac{1}{10}} & \\sqrt{\\frac{1}{10}}\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 0 1/107*sqrt(107) -5/107*sqrt(107) 9/107*sqrt(107)]\n", "[-107/195*sqrt(195/107) 74/195*sqrt(195/107) 58/195*sqrt(195/107) 8/65*sqrt(195/107)]\n", "[ sqrt(2/39) 7/2*sqrt(2/39) -2*sqrt(2/39) -3/2*sqrt(2/39)]\n", "[ 2*sqrt(1/10) sqrt(1/10) 2*sqrt(1/10) sqrt(1/10)]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Turn the list into a unitary matrix\n", "V=matrix(U);V" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\mathrm{True}\n", "\\end{math}" ], "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V.is_unitary()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(0,\\,\\frac{1}{107} \\, \\sqrt{107},\\,-\\frac{5}{107} \\, \\sqrt{107},\\,\\frac{9}{107} \\, \\sqrt{107}\\right)\n", "\\end{math}" ], "text/plain": [ "(0, 1/107*sqrt(107), -5/107*sqrt(107), 9/107*sqrt(107))" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V[0]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}0\n", "\\end{math}" ], "text/plain": [ "0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V[0].hermitian_inner_product(V[1])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "##" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "## Now some basic linear algebra computations" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrr}\n", "0 & 0 & 1 & 2 & 2 & -5 & 3 \\\\\n", "-1 & 5 & 2 & 2 & 1 & -7 & 5 \\\\\n", "0 & 0 & -2 & -3 & -3 & 8 & -5 \\\\\n", "-1 & 5 & 0 & -1 & -2 & 1 & 0\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 0 0 1 2 2 -5 3]\n", "[-1 5 2 2 1 -7 5]\n", "[ 0 0 -2 -3 -3 8 -5]\n", "[-1 5 0 -1 -2 1 0]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example stolen from Sage Reference Manual\n", "A = matrix(QQ, [[0, 0, 1, 2, 2, -5, 3],\n", "[-1, 5, 2, 2, 1, -7, 5],\n", "[0, 0, -2, -3, -3, 8, -5],\n", "[-1, 5, 0, -1, -2, 1, 0]]); A" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrr}\n", "1 & -5 & 0 & 0 & 1 & 1 & -1 \\\\\n", "0 & 0 & 1 & 0 & 0 & -1 & 1 \\\\\n", "0 & 0 & 0 & 1 & 1 & -2 & 1 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 0\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 -5 0 0 1 1 -1]\n", "[ 0 0 1 0 0 -1 1]\n", "[ 0 0 0 1 1 -2 1]\n", "[ 0 0 0 0 0 0 0]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Put the matrix in RREF\n", "A.rref()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(0, 2, 3\\right)\n", "\\end{math}" ], "text/plain": [ "(0, 2, 3)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Pivots (lists start with index 0)\n", "A.pivots()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(1, 4, 5, 6\\right)\n", "\\end{math}" ], "text/plain": [ "(1, 4, 5, 6)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.nonpivots()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\mathrm{RowSpan}_{\\Bold{Q}}\\left[\\begin{array}{rrrrrrr}\n", "5 & 1 & 0 & 0 & 0 & 0 & 0 \\\\\n", "-1 & 0 & 0 & -1 & 1 & 0 & 0 \\\\\n", "-1 & 0 & 1 & 2 & 0 & 1 & 0 \\\\\n", "1 & 0 & -1 & -1 & 0 & 0 & 1\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "Vector space of degree 7 and dimension 4 over Rational Field\n", "User basis matrix:\n", "[ 5 1 0 0 0 0 0]\n", "[-1 0 0 -1 1 0 0]\n", "[-1 0 1 2 0 1 0]\n", "[ 1 0 -1 -1 0 0 1]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Give a basis for the nullspace with a one in each non-pivot (free variable) position\n", "A.right_kernel(basis='pivot')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "# How to solve linear systems, first an inconsistent system, then a consistent one" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrr}\n", "1 & 1 & 5 & -6 & -14 & 18 & 1 \\\\\n", "0 & 1 & 4 & -7 & -15 & 16 & 6 \\\\\n", "-1 & -1 & -4 & 3 & 8 & -13 & 3 \\\\\n", "2 & 1 & 6 & -4 & -11 & 19 & -6 \\\\\n", "-1 & 0 & -3 & 0 & 1 & -7 & 7\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 1 5 -6 -14 18 1]\n", "[ 0 1 4 -7 -15 16 6]\n", "[ -1 -1 -4 3 8 -13 3]\n", "[ 2 1 6 -4 -11 19 -6]\n", "[ -1 0 -3 0 1 -7 7]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A=matrix(QQ,[[1, 1, 5,-6,-14,18,1],[0, 1, 4, -7, -15,16\n", ",6], [-1 ,-1 ,-4, 3, 8,-13, 3],\n", "[2, 1, 6, -4,-11, 19, -6], [-1, 0, -3, 0, 1, -7, 7]]);A" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\left(5,\\,3,\\,-5,\\,7,\\,3\\right), \\left(5,\\,3,\\,-5,\\,7,\\,-2\\right)\\right)\n", "\\end{math}" ], "text/plain": [ "((5, 3, -5, 7, 3), (5, 3, -5, 7, -2))" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b1=vector(QQ,[5,3,-5,7,3])\n", "b2=vector(QQ,[5,3,-5,7,-2])\n", "b1,b2" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrrr}\n", "1 & 0 & 0 & 0 & -1 & 1 & -1 & 0 \\\\\n", "0 & 1 & 0 & 0 & -1 & 1 & 0 & 0 \\\\\n", "0 & 0 & 1 & 0 & 0 & 2 & -2 & 0 \\\\\n", "0 & 0 & 0 & 1 & 2 & -1 & -2 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 0 0 0 -1 1 -1 0]\n", "[ 0 1 0 0 -1 1 0 0]\n", "[ 0 0 1 0 0 2 -2 0]\n", "[ 0 0 0 1 2 -1 -2 0]\n", "[ 0 0 0 0 0 0 0 1]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# First row-reduce the augmented matrix\n", "(A.augment(b1)).rref()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrrr}\n", "1 & 0 & 0 & 0 & -1 & 1 & -1 & 2 \\\\\n", "0 & 1 & 0 & 0 & -1 & 1 & 0 & 3 \\\\\n", "0 & 0 & 1 & 0 & 0 & 2 & -2 & 0 \\\\\n", "0 & 0 & 0 & 1 & 2 & -1 & -2 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 0 0 0 -1 1 -1 2]\n", "[ 0 1 0 0 -1 1 0 3]\n", "[ 0 0 1 0 0 2 -2 0]\n", "[ 0 0 0 1 2 -1 -2 0]\n", "[ 0 0 0 0 0 0 0 0]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(A.augment(b2)).rref()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(2,\\,3,\\,0,\\,0,\\,0,\\,0,\\,0\\right)\n", "\\end{math}" ], "text/plain": [ "(2, 3, 0, 0, 0, 0, 0)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Give one solution to Ax=b_2\n", "A.solve_right(b2)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\left(4, 5, 6\\right), \\mathrm{RowSpan}_{\\Bold{Q}}\\left[\\begin{array}{rrrrrrr}\n", "1 & 1 & 0 & -2 & 1 & 0 & 0 \\\\\n", "-1 & -1 & -2 & 1 & 0 & 1 & 0 \\\\\n", "1 & 0 & 2 & 2 & 0 & 0 & 1\n", "\\end{array}\\right]\\right)\n", "\\end{math}" ], "text/plain": [ "((4, 5, 6),\n", " Vector space of degree 7 and dimension 3 over Rational Field\n", " User basis matrix:\n", " [ 1 1 0 -2 1 0 0]\n", " [-1 -1 -2 1 0 1 0]\n", " [ 1 0 2 2 0 0 1])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find the nullspace of A\n", "A.nonpivots(),A.right_kernel(basis='pivot')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\mathrm{False}\n", "\\end{math}" ], "text/plain": [ "False" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b1 in A.column_space()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\mathrm{True}\n", "\\end{math}" ], "text/plain": [ "True" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b2 in A.column_space()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrrr}\n", "20 & -17 & -5 & 15 & -1 & 3 & -27 & -38 \\\\\n", "14 & -13 & 0 & 10 & 0 & 2 & -22 & -16 \\\\\n", "45 & 15 & -17 & 15 & -2 & 3 & -33 & 64 \\\\\n", "-20 & 60 & -5 & -27 & -1 & -4 & 48 & 136 \\\\\n", "-17 & -59 & 15 & 5 & 0 & 1 & -11 & -140 \\\\\n", "6 & -38 & 5 & 10 & 1 & -1 & -22 & -80 \\\\\n", "8 & 16 & -5 & 0 & -1 & 0 & -3 & 40 \\\\\n", "-7 & 11 & 0 & -5 & 0 & -1 & 11 & 17\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 20 -17 -5 15 -1 3 -27 -38]\n", "[ 14 -13 0 10 0 2 -22 -16]\n", "[ 45 15 -17 15 -2 3 -33 64]\n", "[ -20 60 -5 -27 -1 -4 48 136]\n", "[ -17 -59 15 5 0 1 -11 -140]\n", "[ 6 -38 5 10 1 -1 -22 -80]\n", "[ 8 16 -5 0 -1 0 -3 40]\n", "[ -7 11 0 -5 0 -1 11 17]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Characteristic polynomials, eigenspaces, etc \n", "B=random_matrix(ZZ,8,8,algorithm='diagonalizable')\n", "B" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}(x - 9) \\cdot (x + 9) \\cdot (x + 2)^{2} \\cdot (x + 3)^{2} \\cdot (x + 7)^{2}\n", "\\end{math}" ], "text/plain": [ "(x - 9) * (x + 9) * (x + 2)^2 * (x + 3)^2 * (x + 7)^2" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B.characteristic_polynomial().factor()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\left(9, \\mathrm{RowSpan}_{\\Bold{Q}}\\left[\\begin{array}{rrrrrrrr}\n", "1 & \\frac{1}{2} & 0 & -\\frac{5}{2} & 2 & \\frac{3}{2} & -\\frac{1}{2} & -\\frac{1}{2}\n", "\\end{array}\\right]\\right), \\left(-9, \\mathrm{RowSpan}_{\\Bold{Q}}\\left[\\begin{array}{rrrrrrrr}\n", "1 & \\frac{1}{3} & \\frac{5}{2} & 0 & -\\frac{11}{6} & -\\frac{1}{3} & \\frac{2}{3} & -\\frac{1}{6}\n", "\\end{array}\\right]\\right), \\left(-2, \\mathrm{RowSpan}_{\\Bold{Q}}\\left[\\begin{array}{rrrrrrrr}\n", "1 & 0 & 2 & 1 & -3 & -1 & 1 & 0 \\\\\n", "0 & 1 & -\\frac{3}{2} & -\\frac{7}{2} & 5 & \\frac{5}{2} & -\\frac{3}{2} & -\\frac{1}{2}\n", "\\end{array}\\right]\\right), \\left(-3, \\mathrm{RowSpan}_{\\Bold{Q}}\\left[\\begin{array}{rrrrrrrr}\n", "1 & 0 & 2 & 1 & -2 & -1 & 1 & 0 \\\\\n", "0 & 1 & -\\frac{3}{2} & -\\frac{7}{2} & \\frac{7}{2} & 2 & -\\frac{3}{2} & -\\frac{1}{2}\n", "\\end{array}\\right]\\right), \\left(-7, \\mathrm{RowSpan}_{\\Bold{Q}}\\left[\\begin{array}{rrrrrrrr}\n", "1 & 0 & 3 & 1 & -3 & -1 & 1 & 0 \\\\\n", "0 & 1 & -3 & -4 & 5 & \\frac{5}{2} & -\\frac{3}{2} & -\\frac{1}{2}\n", "\\end{array}\\right]\\right)\\right]\n", "\\end{math}" ], "text/plain": [ "[\n", "(9, Vector space of degree 8 and dimension 1 over Rational Field\n", "User basis matrix:\n", "[ 1 1/2 0 -5/2 2 3/2 -1/2 -1/2]),\n", "(-9, Vector space of degree 8 and dimension 1 over Rational Field\n", "User basis matrix:\n", "[ 1 1/3 5/2 0 -11/6 -1/3 2/3 -1/6]),\n", "(-2, Vector space of degree 8 and dimension 2 over Rational Field\n", "User basis matrix:\n", "[ 1 0 2 1 -3 -1 1 0]\n", "[ 0 1 -3/2 -7/2 5 5/2 -3/2 -1/2]),\n", "(-3, Vector space of degree 8 and dimension 2 over Rational Field\n", "User basis matrix:\n", "[ 1 0 2 1 -2 -1 1 0]\n", "[ 0 1 -3/2 -7/2 7/2 2 -3/2 -1/2]),\n", "(-7, Vector space of degree 8 and dimension 2 over Rational Field\n", "User basis matrix:\n", "[ 1 0 3 1 -3 -1 1 0]\n", "[ 0 1 -3 -4 5 5/2 -3/2 -1/2])\n", "]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B.eigenspaces_right()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\left(9, \\left[\\left(1,\\,\\frac{1}{2},\\,0,\\,-\\frac{5}{2},\\,2,\\,\\frac{3}{2},\\,-\\frac{1}{2},\\,-\\frac{1}{2}\\right)\\right], 1\\right), \\left(-9, \\left[\\left(1,\\,\\frac{1}{3},\\,\\frac{5}{2},\\,0,\\,-\\frac{11}{6},\\,-\\frac{1}{3},\\,\\frac{2}{3},\\,-\\frac{1}{6}\\right)\\right], 1\\right), \\left(-2, \\left[\\left(1,\\,0,\\,2,\\,1,\\,-3,\\,-1,\\,1,\\,0\\right), \\left(0,\\,1,\\,-\\frac{3}{2},\\,-\\frac{7}{2},\\,5,\\,\\frac{5}{2},\\,-\\frac{3}{2},\\,-\\frac{1}{2}\\right)\\right], 2\\right), \\left(-3, \\left[\\left(1,\\,0,\\,2,\\,1,\\,-2,\\,-1,\\,1,\\,0\\right), \\left(0,\\,1,\\,-\\frac{3}{2},\\,-\\frac{7}{2},\\,\\frac{7}{2},\\,2,\\,-\\frac{3}{2},\\,-\\frac{1}{2}\\right)\\right], 2\\right), \\left(-7, \\left[\\left(1,\\,0,\\,3,\\,1,\\,-3,\\,-1,\\,1,\\,0\\right), \\left(0,\\,1,\\,-3,\\,-4,\\,5,\\,\\frac{5}{2},\\,-\\frac{3}{2},\\,-\\frac{1}{2}\\right)\\right], 2\\right)\\right]\n", "\\end{math}" ], "text/plain": [ "[(9,\n", " [\n", " (1, 1/2, 0, -5/2, 2, 3/2, -1/2, -1/2)\n", " ],\n", " 1),\n", " (-9,\n", " [\n", " (1, 1/3, 5/2, 0, -11/6, -1/3, 2/3, -1/6)\n", " ],\n", " 1),\n", " (-2,\n", " [\n", " (1, 0, 2, 1, -3, -1, 1, 0),\n", " (0, 1, -3/2, -7/2, 5, 5/2, -3/2, -1/2)\n", " ],\n", " 2),\n", " (-3,\n", " [\n", " (1, 0, 2, 1, -2, -1, 1, 0),\n", " (0, 1, -3/2, -7/2, 7/2, 2, -3/2, -1/2)\n", " ],\n", " 2),\n", " (-7,\n", " [\n", " (1, 0, 3, 1, -3, -1, 1, 0),\n", " (0, 1, -3, -4, 5, 5/2, -3/2, -1/2)\n", " ],\n", " 2)]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B.eigenvectors_right()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\left[\\begin{array}{rrrrrrrr}\n", "9 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & -9 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & 0 & -2 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & -2 & 0 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & 0 & -3 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & -3 & 0 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & -7 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 0 & -7\n", "\\end{array}\\right], \\left[\\begin{array}{rrrrrrrr}\n", "1 & 1 & 1 & 0 & 1 & 0 & 1 & 0 \\\\\n", "\\frac{1}{2} & \\frac{1}{3} & 0 & 1 & 0 & 1 & 0 & 1 \\\\\n", "0 & \\frac{5}{2} & 2 & -\\frac{3}{2} & 2 & -\\frac{3}{2} & 3 & -3 \\\\\n", "-\\frac{5}{2} & 0 & 1 & -\\frac{7}{2} & 1 & -\\frac{7}{2} & 1 & -4 \\\\\n", "2 & -\\frac{11}{6} & -3 & 5 & -2 & \\frac{7}{2} & -3 & 5 \\\\\n", "\\frac{3}{2} & -\\frac{1}{3} & -1 & \\frac{5}{2} & -1 & 2 & -1 & \\frac{5}{2} \\\\\n", "-\\frac{1}{2} & \\frac{2}{3} & 1 & -\\frac{3}{2} & 1 & -\\frac{3}{2} & 1 & -\\frac{3}{2} \\\\\n", "-\\frac{1}{2} & -\\frac{1}{6} & 0 & -\\frac{1}{2} & 0 & -\\frac{1}{2} & 0 & -\\frac{1}{2}\n", "\\end{array}\\right]\\right)\n", "\\end{math}" ], "text/plain": [ "(\n", "[ 9 0 0 0 0 0 0 0]\n", "[ 0 -9 0 0 0 0 0 0]\n", "[ 0 0 -2 0 0 0 0 0]\n", "[ 0 0 0 -2 0 0 0 0]\n", "[ 0 0 0 0 -3 0 0 0]\n", "[ 0 0 0 0 0 -3 0 0]\n", "[ 0 0 0 0 0 0 -7 0]\n", "[ 0 0 0 0 0 0 0 -7],\n", "\n", "[ 1 1 1 0 1 0 1 0]\n", "[ 1/2 1/3 0 1 0 1 0 1]\n", "[ 0 5/2 2 -3/2 2 -3/2 3 -3]\n", "[ -5/2 0 1 -7/2 1 -7/2 1 -4]\n", "[ 2 -11/6 -3 5 -2 7/2 -3 5]\n", "[ 3/2 -1/3 -1 5/2 -1 2 -1 5/2]\n", "[ -1/2 2/3 1 -3/2 1 -3/2 1 -3/2]\n", "[ -1/2 -1/6 0 -1/2 0 -1/2 0 -1/2]\n", ")" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Retruns the pair of a diagonal matrix and a matrix of eigenvectors\n", "B.eigenmatrix_right()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrrr}\n", "1 & 1 & 1 & 0 & 1 & 0 & 1 & 0 \\\\\n", "\\frac{1}{2} & \\frac{1}{3} & 0 & 1 & 0 & 1 & 0 & 1 \\\\\n", "0 & \\frac{5}{2} & 2 & -\\frac{3}{2} & 2 & -\\frac{3}{2} & 3 & -3 \\\\\n", "-\\frac{5}{2} & 0 & 1 & -\\frac{7}{2} & 1 & -\\frac{7}{2} & 1 & -4 \\\\\n", "2 & -\\frac{11}{6} & -3 & 5 & -2 & \\frac{7}{2} & -3 & 5 \\\\\n", "\\frac{3}{2} & -\\frac{1}{3} & -1 & \\frac{5}{2} & -1 & 2 & -1 & \\frac{5}{2} \\\\\n", "-\\frac{1}{2} & \\frac{2}{3} & 1 & -\\frac{3}{2} & 1 & -\\frac{3}{2} & 1 & -\\frac{3}{2} \\\\\n", "-\\frac{1}{2} & -\\frac{1}{6} & 0 & -\\frac{1}{2} & 0 & -\\frac{1}{2} & 0 & -\\frac{1}{2}\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 1 1 0 1 0 1 0]\n", "[ 1/2 1/3 0 1 0 1 0 1]\n", "[ 0 5/2 2 -3/2 2 -3/2 3 -3]\n", "[ -5/2 0 1 -7/2 1 -7/2 1 -4]\n", "[ 2 -11/6 -3 5 -2 7/2 -3 5]\n", "[ 3/2 -1/3 -1 5/2 -1 2 -1 5/2]\n", "[ -1/2 2/3 1 -3/2 1 -3/2 1 -3/2]\n", "[ -1/2 -1/6 0 -1/2 0 -1/2 0 -1/2]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab the matrix of eigenvectors\n", "P=B.eigenmatrix_right()[1];P" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrrr}\n", "9 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & -9 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & 0 & -2 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & -2 & 0 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & 0 & -3 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & -3 & 0 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & -7 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 0 & -7\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 9 0 0 0 0 0 0 0]\n", "[ 0 -9 0 0 0 0 0 0]\n", "[ 0 0 -2 0 0 0 0 0]\n", "[ 0 0 0 -2 0 0 0 0]\n", "[ 0 0 0 0 -3 0 0 0]\n", "[ 0 0 0 0 0 -3 0 0]\n", "[ 0 0 0 0 0 0 -7 0]\n", "[ 0 0 0 0 0 0 0 -7]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "P.inverse()*B*P" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "#" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "# Invariant factors and canonical forms" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrrr}\n", "0 & -8 & 4 & -6 & -2 & 5 & -3 & 11 \\\\\n", "-2 & -4 & 2 & -4 & -2 & 4 & -2 & 6 \\\\\n", "5 & 14 & -7 & 12 & 3 & -8 & 6 & -27 \\\\\n", "-3 & 8 & 7 & -5 & 0 & 2 & 6 & 17 \\\\\n", "0 & 5 & 0 & 2 & 4 & -4 & 1 & 2 \\\\\n", "-3 & -7 & 5 & -6 & -1 & 5 & -4 & 14 \\\\\n", "6 & 18 & -10 & 14 & 4 & -10 & 10 & -28 \\\\\n", "-2 & -6 & 4 & -5 & -1 & 3 & -3 & 13\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 0 -8 4 -6 -2 5 -3 11]\n", "[ -2 -4 2 -4 -2 4 -2 6]\n", "[ 5 14 -7 12 3 -8 6 -27]\n", "[ -3 8 7 -5 0 2 6 17]\n", "[ 0 5 0 2 4 -4 1 2]\n", "[ -3 -7 5 -6 -1 5 -4 14]\n", "[ 6 18 -10 14 4 -10 10 -28]\n", "[ -2 -6 4 -5 -1 3 -3 13]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C=matrix(QQ,8,[[0,-8,4,-6,-2,5,-3,11], \\\n", "[-2,-4,2,-4,-2,4,-2,6], [5, 14, -7, 12, 3,-8,6,-27], \\\n", "[-3,8,7,-5,0,2,6,17], [0,5,0,2,4, -4, 1, 2], \\\n", "[-3, -7, 5, -6, -1, 5, -4, 14], \\\n", "[6, 18, -10, 14, 4, -10, 10, -28], \\\n", "[-2, -6, 4, -5, -1, 3, -3, 13]]);C" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}(x - 12) \\cdot (x - 2)^{5} \\cdot (x^{2} + 6 x - 20)\n", "\\end{math}" ], "text/plain": [ "(x - 12) * (x - 2)^5 * (x^2 + 6*x - 20)" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C.characteristic_polynomial().factor()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(x^{5} - 10 x^{4} - 64 x^{3} + 584 x^{2} - 1328 x + 960, (x - 12) \\cdot (x - 2)^{2} \\cdot (x^{2} + 6 x - 20)\\right)\n", "\\end{math}" ], "text/plain": [ "(x^5 - 10*x^4 - 64*x^3 + 584*x^2 - 1328*x + 960,\n", " (x - 12) * (x - 2)^2 * (x^2 + 6*x - 20))" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# minimal polynomial in raw and factored form\n", "m=C.minimal_polynomial()\n", "m,m.factor()" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\left[-2, 1\\right], \\left[4, -4, 1\\right], \\left[960, -1328, 584, -64, -10, 1\\right]\\right]\n", "\\end{math}" ], "text/plain": [ "[[-2, 1], [4, -4, 1], [960, -1328, 584, -64, -10, 1]]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Coefficients of the invariant factors\n", "C.rational_form(format='invariants')" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[(x - 2), (x - 2)^{2}, (x - 12) \\cdot (x - 2)^{2} \\cdot (x^{2} + 6 x - 20)\\right]\n", "\\end{math}" ], "text/plain": [ "[x - 2, (x - 2)^2, (x - 12) * (x - 2)^2 * (x^2 + 6*x - 20)]" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# List the invariant factors\n", "invariants=C.rational_form(format='invariants')\n", "R=PolynomialRing(QQ,'x')\n", "[R(p).factor() for p in invariants]" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{r|rr|rrrrr}\n", "2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & -4 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & 1 & 4 & 0 & 0 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 0 & 0 & 0 & 0 & -960 \\\\\n", "0 & 0 & 0 & 1 & 0 & 0 & 0 & 1328 \\\\\n", "0 & 0 & 0 & 0 & 1 & 0 & 0 & -584 \\\\\n", "0 & 0 & 0 & 0 & 0 & 1 & 0 & 64 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 1 & 10\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 2| 0 0| 0 0 0 0 0]\n", "[----+---------+------------------------]\n", "[ 0| 0 -4| 0 0 0 0 0]\n", "[ 0| 1 4| 0 0 0 0 0]\n", "[----+---------+------------------------]\n", "[ 0| 0 0| 0 0 0 0 -960]\n", "[ 0| 0 0| 1 0 0 0 1328]\n", "[ 0| 0 0| 0 1 0 0 -584]\n", "[ 0| 0 0| 0 0 1 0 64]\n", "[ 0| 0 0| 0 0 0 1 10]" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C.rational_form(format='right')" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\Bold{Q}[a]/(a^{2} + 6 a - 20)\n", "\\end{math}" ], "text/plain": [ "Number Field in a with defining polynomial x^2 + 6*x - 20" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# To produce a Jordan form, we must extend the field\n", "K.=NumberField(x^2+6*x-20);K" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{r|r|r|rr|rr|r}\n", "12 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & a & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & -a - 6 & 0 & 0 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 2 & 1 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & 0 & 2 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 0 & 0 & 2 & 1 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 2 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 2\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 12| 0| 0| 0 0| 0 0| 0]\n", "[------+------+------+-------------+-------------+------]\n", "[ 0| a| 0| 0 0| 0 0| 0]\n", "[------+------+------+-------------+-------------+------]\n", "[ 0| 0|-a - 6| 0 0| 0 0| 0]\n", "[------+------+------+-------------+-------------+------]\n", "[ 0| 0| 0| 2 1| 0 0| 0]\n", "[ 0| 0| 0| 0 2| 0 0| 0]\n", "[------+------+------+-------------+-------------+------]\n", "[ 0| 0| 0| 0 0| 2 1| 0]\n", "[ 0| 0| 0| 0 0| 0 2| 0]\n", "[------+------+------+-------------+-------------+------]\n", "[ 0| 0| 0| 0 0| 0 0| 2]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C.jordan_form(K)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrrrrrrr}\n", "0 & -8 & 4 & -6 & -2 & 5 & -3 & 11 \\\\\n", "-2 & -4 & 2 & -4 & -2 & 4 & -2 & 6 \\\\\n", "5 & 14 & -7 & 12 & 3 & -8 & 6 & -27 \\\\\n", "-3 & -8 & 7 & -5 & 0 & 2 & -6 & 17 \\\\\n", "0 & 5 & 0 & 2 & 4 & -4 & 1 & 2 \\\\\n", "-3 & -7 & 5 & -6 & -1 & 5 & -4 & 14 \\\\\n", "6 & 18 & -10 & 14 & 4 & -10 & 10 & -28 \\\\\n", "-2 & -6 & 4 & -5 & -1 & 3 & -3 & 13\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 0 -8 4 -6 -2 5 -3 11]\n", "[ -2 -4 2 -4 -2 4 -2 6]\n", "[ 5 14 -7 12 3 -8 6 -27]\n", "[ -3 -8 7 -5 0 2 -6 17]\n", "[ 0 5 0 2 4 -4 1 2]\n", "[ -3 -7 5 -6 -1 5 -4 14]\n", "[ 6 18 -10 14 4 -10 10 -28]\n", "[ -2 -6 4 -5 -1 3 -3 13]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Another nice example from the Sage Reference Manual\n", "D=matrix(QQ,8,[[0,-8,4,-6,-2,5,-3,11], \\\n", "[-2,-4,2,-4,-2,4,-2,6], [5, 14, -7, 12, 3,-8,6,-27], \\\n", "[-3,-8,7,-5,0,2,-6,17], [0,5,0,2,4, -4, 1, 2], \\\n", "[-3, -7, 5, -6, -1, 5, -4, 14], \\\n", "[6, 18, -10, 14, 4, -10, 10, -28], \\\n", "[-2, -6, 4, -5, -1, 3, -3, 13]]);D" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}(x - 2)^{8}\n", "\\end{math}" ], "text/plain": [ "(x - 2)^8" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D.characteristic_polynomial().factor()" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(x^{3} - 6 x^{2} + 12 x - 8, (x - 2)^{3}\\right)\n", "\\end{math}" ], "text/plain": [ "(x^3 - 6*x^2 + 12*x - 8, (x - 2)^3)" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m=D.minimal_polynomial()\n", "m,m.factor()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[(x - 2), (x - 2)^{2}, (x - 2)^{2}, (x - 2)^{3}\\right]\n", "\\end{math}" ], "text/plain": [ "[x - 2, (x - 2)^2, (x - 2)^2, (x - 2)^3]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invariants=D.rational_form(format='invariants')\n", "R=PolynomialRing(QQ,'x')\n", "[R(p).factor() for p in invariants]" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{r|rr|rr|rrr}\n", "2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & -4 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & 1 & 4 & 0 & 0 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 0 & -4 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & 1 & 4 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 8 \\\\\n", "0 & 0 & 0 & 0 & 0 & 1 & 0 & -12 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 1 & 6\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 2| 0 0| 0 0| 0 0 0]\n", "[---+-------+-------+-----------]\n", "[ 0| 0 -4| 0 0| 0 0 0]\n", "[ 0| 1 4| 0 0| 0 0 0]\n", "[---+-------+-------+-----------]\n", "[ 0| 0 0| 0 -4| 0 0 0]\n", "[ 0| 0 0| 1 4| 0 0 0]\n", "[---+-------+-------+-----------]\n", "[ 0| 0 0| 0 0| 0 0 8]\n", "[ 0| 0 0| 0 0| 1 0 -12]\n", "[ 0| 0 0| 0 0| 0 1 6]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D.rational_form(format='right')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrr|rr|rr|r}\n", "2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & 2 & 1 & 0 & 0 & 0 & 0 & 0 \\\\\n", "0 & 0 & 2 & 0 & 0 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 2 & 1 & 0 & 0 & 0 \\\\\n", "0 & 0 & 0 & 0 & 2 & 0 & 0 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 0 & 0 & 2 & 1 & 0 \\\\\n", "0 & 0 & 0 & 0 & 0 & 0 & 2 & 0 \\\\\n", "\\hline\n", " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 2\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[2 1 0|0 0|0 0|0]\n", "[0 2 1|0 0|0 0|0]\n", "[0 0 2|0 0|0 0|0]\n", "[-----+---+---+-]\n", "[0 0 0|2 1|0 0|0]\n", "[0 0 0|0 2|0 0|0]\n", "[-----+---+---+-]\n", "[0 0 0|0 0|2 1|0]\n", "[0 0 0|0 0|0 2|0]\n", "[-----+---+---+-]\n", "[0 0 0|0 0|0 0|2]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D.jordan_form()" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "#Inner Product Spaces" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\left[\\begin{array}{rrr}\n", "1 & 1 & -2 \\\\\n", "6 & 0 & 0\n", "\\end{array}\\right], \\left[\\begin{array}{rrr}\n", "1 & 1 & -2 \\\\\n", "5 & -1 & 2\n", "\\end{array}\\right], \\left[\\begin{array}{rr}\n", "1 & 0 \\\\\n", "1 & 1\n", "\\end{array}\\right], \\left[\\begin{array}{rrr}\n", "1 & 1 & -2 \\\\\n", "6 & 0 & 0\n", "\\end{array}\\right]\\right)\n", "\\end{math}" ], "text/plain": [ "(\n", "[ 1 1 -2] [ 1 1 -2] [1 0] [ 1 1 -2]\n", "[ 6 0 0], [ 5 -1 2], [1 1], [ 6 0 0]\n", ")" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Gram Schmidt\n", "# The rowws of A are the given vectors\n", "# The rows of G are output of G-S\n", "# A = MG\n", "A=matrix(QQbar, [[1,1,-2], [6,0,0]])\n", "G,M=A.gram_schmidt()\n", "(A,G,M, M*G)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(0,\\,-\\frac{2}{5},\\,\\frac{4}{5}\\right)\n", "\\end{math}" ], "text/plain": [ "(0, -2/5, 4/5)" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Orthogonal projection of a vector into the space spanned by the above two vectors by definition\n", "v=vector(QQbar,[0,0,1])\n", "A=matrix(QQbar, [G[0],G[1]])\n", "OP = vector(QQbar,[0,0,0])\n", "for i in range(G.nrows()):\n", " scalar = v.inner_product(G[i])/(G[i].inner_product(G[i]))\n", " OP = OP + scalar*G[i]\n", "OP" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrr}\n", "1 & 0 & 0 \\\\\n", "0 & \\frac{1}{5} & -\\frac{2}{5} \\\\\n", "0 & -\\frac{2}{5} & \\frac{4}{5}\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 0 0]\n", "[ 0 1/5 -2/5]\n", "[ 0 -2/5 4/5]" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The matrix of the orthogonal projection wrt the standard basis\n", "# Above we computed the orthogonal projection of the standard basis vector e_3\n", "# hence the last column of the matrix below\n", "A= A.transpose()\n", "A* (A.conjugate_transpose()*A).inverse() * A.conjugate_transpose()" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rr}\n", "1 & 3 \\\\\n", "2 & 2 \\\\\n", "3 & 1\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[1 3]\n", "[2 2]\n", "[3 1]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Some SVD tools\n", "B=matrix(QQbar,[[1,3],[2,2],[3,1]])\n", "B" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rr}\n", "14 & 10 \\\\\n", "10 & 14\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[14 10]\n", "[10 14]" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C=B.conjugate_transpose()*B;C" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}(x - 24) \\cdot (x - 4)\n", "\\end{math}" ], "text/plain": [ "(x - 24) * (x - 4)" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C.characteristic_polynomial().factor()\n" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\left[\\begin{array}{rr}\n", "24 & 0 \\\\\n", "0 & 4\n", "\\end{array}\\right], \\left[\\begin{array}{rr}\n", "1 & 1 \\\\\n", "1 & -1\n", "\\end{array}\\right]\\right)\n", "\\end{math}" ], "text/plain": [ "(\n", "[24 0] [ 1 1]\n", "[ 0 4], [ 1 -1]\n", ")" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#The diagonalized matrix and a matrix of eigenvectors\n", "C.eigenmatrix_right()" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rr}\n", "1 & 1 \\\\\n", "1 & -1\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 1]\n", "[ 1 -1]" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab the matrix of eigenvectors\n", "V=C.eigenmatrix_right()[1]\n", "V" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rr}\n", "0.7071067811865475? & 0.7071067811865475? \\\\\n", "0.7071067811865475? & -0.7071067811865475?\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 0.7071067811865475? 0.7071067811865475?]\n", "[ 0.7071067811865475? -0.7071067811865475?]" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# normalize the column vectoors\n", "for j in range(V.ncols()):\n", " w=V.column(j)\n", " if w.norm() != 0 :\n", " V[:,j] = w/w.norm()\n", "V" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrr}\n", "1 & 1 & 1 \\\\\n", "-1 & 0 & 1 \\\\\n", "0 & 0 & 0\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 1 1]\n", "[-1 0 1]\n", "[ 0 0 0]" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Filling out a linearly independent set to an orthogonal basis\n", "# Build a container for a third vector\n", "D= matrix(QQbar,[[1,1,1],[-1,0,1],[0,0,0]]);D" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrr}\n", "1 & 1 & 1 \\\\\n", "-1 & 0 & 1 \\\\\n", "0 & 1 & 2\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 1 1]\n", "[-1 0 1]\n", "[ 0 1 2]" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Try a vector known to be linearly dependent on the other two\n", "D[2]=[0,1,2];D" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrr}\n", "1 & 1 & 1 \\\\\n", "-1 & 0 & 1\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 1 1]\n", "[-1 0 1]" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Gram-Schmidt kicks it out\n", "G,M=D.gram_schmidt();G" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrr}\n", "1 & 1 & 1 \\\\\n", "-1 & 0 & 1 \\\\\n", "1 & 0 & 2\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 1 1]\n", "[-1 0 1]\n", "[ 1 0 2]" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Try something more intelligent\n", "D[2]=[1,0,2];D" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\begin{array}{rrr}\n", "1 & 1 & 1 \\\\\n", "-1 & 0 & 1 \\\\\n", "\\frac{1}{2} & -1 & \\frac{1}{2}\n", "\\end{array}\\right]\n", "\\end{math}" ], "text/plain": [ "[ 1 1 1]\n", "[ -1 0 1]\n", "[1/2 -1 1/2]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Better; a third orthognal vector\n", "G,M=D.gram_schmidt();G" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\left[\\begin{array}{rr}\n", "1 & 3 \\\\\n", "2 & 2 \\\\\n", "3 & 1\n", "\\end{array}\\right], \\left(\\left[\\begin{array}{rrr}\n", "-0.5773502691896257 & 0.7071067811865479 & 0.40824829046386346 \\\\\n", "-0.5773502691896257 & 8.326672684688674 \\times 10^{-17} & -0.816496580927726 \\\\\n", "-0.5773502691896258 & -0.7071067811865475 & 0.40824829046386296\n", "\\end{array}\\right], \\left[\\begin{array}{rr}\n", "4.898979485566356 & 0.0 \\\\\n", "0.0 & 2.0 \\\\\n", "0.0 & 0.0\n", "\\end{array}\\right], \\left[\\begin{array}{rr}\n", "-0.7071067811865476 & -0.7071067811865475 \\\\\n", "-0.7071067811865475 & 0.7071067811865476\n", "\\end{array}\\right]\\right)\\right)\n", "\\end{math}" ], "text/plain": [ "(\n", " ([ -0.5773502691896257 0.7071067811865479 0.40824829046386346] \n", " [ -0.5773502691896257 8.326672684688674e-17 -0.816496580927726] \n", " [ -0.5773502691896258 -0.7071067811865475 0.40824829046386296], [4.898979485566356 0.0]\n", "[1 3] [ 0.0 2.0] \n", "[2 2] [ 0.0 0.0], [-0.7071067811865476 -0.7071067811865475] \n", "[3 1], [-0.7071067811865475 0.7071067811865476]) \n", ")" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sage also has the ability to compute an SVD directly once the entries of the matrix \n", "# have been converted to RDF or CDF (Real or Complex double precision). \n", "# This conversion can be done on the fly or by direct definition; \n", "# we show both methods. The algorithm outputs the triple (U,Sigma,V)\n", "B=matrix(QQ,[[1,3],[2,2],[3,1]])\n", "B,B.change_ring(RDF).SVD()" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "\\begin{math}\n", "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left(\\left[\\begin{array}{rr}\n", "1.0 & 3.0 \\\\\n", "2.0 & 2.0 \\\\\n", "3.0 & 1.0\n", "\\end{array}\\right], \\left(\\left[\\begin{array}{rrr}\n", "-0.5773502691896257 & 0.7071067811865479 & 0.40824829046386346 \\\\\n", "-0.5773502691896257 & 8.326672684688674 \\times 10^{-17} & -0.816496580927726 \\\\\n", "-0.5773502691896258 & -0.7071067811865475 & 0.40824829046386296\n", "\\end{array}\\right], \\left[\\begin{array}{rr}\n", "4.898979485566356 & 0.0 \\\\\n", "0.0 & 2.0 \\\\\n", "0.0 & 0.0\n", "\\end{array}\\right], \\left[\\begin{array}{rr}\n", "-0.7071067811865476 & -0.7071067811865475 \\\\\n", "-0.7071067811865475 & 0.7071067811865476\n", "\\end{array}\\right]\\right)\\right)\n", "\\end{math}" ], "text/plain": [ "(\n", " ([ -0.5773502691896257 0.7071067811865479 0.40824829046386346] \n", " [ -0.5773502691896257 8.326672684688674e-17 -0.816496580927726] \n", " [ -0.5773502691896258 -0.7071067811865475 0.40824829046386296], [4.898979485566356 0.0]\n", "[1.0 3.0] [ 0.0 2.0] \n", "[2.0 2.0] [ 0.0 0.0], [-0.7071067811865476 -0.7071067811865475] \n", "[3.0 1.0], [-0.7071067811865475 0.7071067811865476]) \n", ")" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B=matrix(RDF,[[1,3],[2,2],[3,1]])\n", "B,B.SVD()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 9.2", "language": "sage", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }