{ "cells": [ { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Diagonal matrix A (non-negative elements):\n", " [[ 5 0 0 0 0 0]\n", " [ 0 3 0 0 0 0]\n", " [ 0 0 8 0 0 0]\n", " [ 0 0 0 1 0 0]\n", " [ 0 0 0 0 5 0]\n", " [ 0 0 0 0 0 12]] \n", "\n", "C1 (A + A.T)/2: \n", " [[ 5. 0. 0. 0. 0. 0.]\n", " [ 0. 3. 0. 0. 0. 0.]\n", " [ 0. 0. 8. 0. 0. 0.]\n", " [ 0. 0. 0. 1. 0. 0.]\n", " [ 0. 0. 0. 0. 5. 0.]\n", " [ 0. 0. 0. 0. 0. 12.]] \n", "\n", "C2 (A.T@A):\n", " [[ 25 0 0 0 0 0]\n", " [ 0 9 0 0 0 0]\n", " [ 0 0 64 0 0 0]\n", " [ 0 0 0 1 0 0]\n", " [ 0 0 0 0 25 0]\n", " [ 0 0 0 0 0 144]] \n", "\n", "C1 = sqrt(C2)\n", "\n", "A.T@A simply squares the diagonal elements of A because the transpose is the same as A.\n", "\n", "A.T + A doubles the diagonal elements of A and then divides them in half, so A is unchanged.\n", "\n", "Hence, C2 is simply the elements of C1 squared, or alternately C1 is the square root of C2.\n" ] } ], "source": [ "# D. Scott\n", "# Linear Algebra: TCI\n", "# Code Challenge 6.3\n", "# Creation of symmetric diagonal matrices\n", "\n", "import random\n", "import numpy as np\n", "\n", "A = np.empty((6,6),dtype=int)\n", "\n", "# generate diagonal 6 x 6 matrix\n", "for i in range(0,6):\n", " for j in range(0,6):\n", " r_num = 0\n", " while (r_num <= 0):\n", " r_num = random.randint(-4,12) \n", " if (i == j):\n", " A[i][j] = r_num\n", " else:\n", " A[i][j] = 0\n", "\n", "print(\"Diagonal matrix A (non-negative elements):\\n\",A,\"\\n\")\n", "\n", "C1 = (A + A.transpose())/2\n", "C2 = A.transpose()@A\n", "print(\"C1 (A + A.T)/2: \\n\",C1,\"\\n\")\n", "print(\"C2 (A.T@A):\\n\",C2,\"\\n\")\n", "print(\"C1 = sqrt(C2)\\n\")\n", "print(\"A.T@A simply squares the diagonal elements of A because the transpose is the same as A.\\n\")\n", "print(\"A.T + A doubles the diagonal elements of A and then divides them in half, so A is unchanged.\\n\")\n", "print(\"Hence, C2 is simply the elements of C1 squared, or alternately C1 is the square root of C2.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "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.9.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }