{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Symbolic Tensor Notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sp\n",
    "from IPython.display import Markdown\n",
    "\n",
    "\n",
    "from mechpy.core.symbolic.tensor import (\n",
    "    SymbolicTensor,\n",
    "    SymbolicThreeByThreeTensor,\n",
    "    SymbolicSymmetricThreeByThreeTensor,\n",
    "    SymbolicSixBySixTensor,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Symbolic Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, c, d, e, f, g, h, i = sp.symbols(\"a b c d e f g h i\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Init Method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b\\\\c & d\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b], [c, d]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = sp.ImmutableDenseNDimArray([[a, b], [c, d]])\n",
    "A = SymbolicTensor(data)\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### From List method"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Symbolic Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b\\\\c & d\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b], [c, d]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([a, b, c, d], shape=(2, 2))\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c\\\\d & e & f\\\\g & h & i\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b, c], [d, e, f], [g, h, i]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([a, b, c, d, e, f, g, h, i], shape=(3, 3))\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Symbolic 3x3 Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c\\\\d & e & f\\\\g & h & i\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b, c], [d, e, f], [g, h, i]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicThreeByThreeTensor.from_list([a, b, c, d, e, f, g, h, i])\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Symbolic Symmetric 3x3 Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_notation_standard = [a, b, c, b, d, e, c, e, f]\n",
    "list_notation_voigt = [a, f, e, f, b, d, e, d, c]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### List 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Default notation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicSymmetricThreeByThreeTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c & d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, b, c, d, e, f]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'standard'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.from_list(list_notation_standard)\n",
    "display(A.__class__)\n",
    "display(A.data)\n",
    "display(A.notation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c\\\\b & d & e\\\\c & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b, c], [b, d, e], [c, e, f]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = A.to_general()\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notation 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c & d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, b, c, d, e, f]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'voigt'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.from_list(list_notation_voigt, notation=\"voigt\")\n",
    "display(A.data)\n",
    "display(A.notation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & f & e\\\\f & b & d\\\\e & d & c\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, f, e], [f, b, d], [e, d, c]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = A.to_general()\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### List 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Default notation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & f & e & b & d & c\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, f, e, b, d, c]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'standard'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.from_list(list_notation_voigt)\n",
    "display(A.data)\n",
    "display(A.notation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & f & e\\\\f & b & d\\\\e & d & c\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, f, e], [f, b, d], [e, d, c]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = A.to_general()\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notation 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c & d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, b, c, d, e, f]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'voigt'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.from_list(list_notation_voigt, notation=\"voigt\")\n",
    "display(A.data)\n",
    "display(A.notation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & f & e\\\\f & b & d\\\\e & d & c\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, f, e], [f, b, d], [e, d, c]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = A.to_general()\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list of 6 elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "default notation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c & d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, b, c, d, e, f]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'standard'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.from_list([a, b, c, d, e, f])\n",
    "display(A.data)\n",
    "display(A.notation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c\\\\b & d & e\\\\c & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b, c], [b, d, e], [c, e, f]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = A.to_general()\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notation 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c & d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, b, c, d, e, f]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'voigt'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.from_list([a, b, c, d, e, f], notation=\"voigt\")\n",
    "display(A.data)\n",
    "display(A.notation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & f & e\\\\f & b & d\\\\e & d & c\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, f, e], [f, b, d], [e, d, c]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = A.to_general()\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Symbolic 6x6 Tensor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List 36 elements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 2 & 3 & 4 & 5 & 6\\\\7 & 8 & 9 & 10 & 11 & 12\\\\13 & 14 & 15 & 16 & 17 & 18\\\\19 & 20 & 21 & 22 & 23 & 24\\\\25 & 26 & 27 & 28 & 29 & 30\\\\31 & 32 & 33 & 34 & 35 & 36\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data_list = list(range(1,37))\n",
    "A = SymbolicSixBySixTensor.from_list(data_list)\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List 6x6 elemnts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 2 & 3 & 4 & 5 & 6\\\\7 & 8 & 9 & 10 & 11 & 12\\\\13 & 14 & 15 & 16 & 17 & 18\\\\19 & 20 & 21 & 22 & 23 & 24\\\\25 & 26 & 27 & 28 & 29 & 30\\\\31 & 32 & 33 & 34 & 35 & 36\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data_list = [[i * 6 + j + 1 for j in range(6)] for i in range(6)]\n",
    "A = SymbolicSixBySixTensor.from_list(data_list)\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create Method"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This method is a simple way to create a tensor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x_{11} & x_{12}\\\\x_{21} & x_{22}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[x_11, x_12], [x_21, x_22]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.create(shape=(2, 2), name=\"x\")\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}X_{11} & X_{12} & X_{13}\\\\X_{21} & X_{22} & X_{23}\\\\X_{31} & X_{32} & X_{33}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[X_11, X_12, X_13], [X_21, X_22, X_23], [X_31, X_32, X_33]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicThreeByThreeTensor.create(name=\"X\")\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Custom name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a_{11} & a_{12} & a_{13} & a_{14} & a_{15} & a_{16}\\\\a_{21} & a_{22} & a_{23} & a_{24} & a_{25} & a_{26}\\\\a_{31} & a_{32} & a_{33} & a_{34} & a_{35} & a_{36}\\\\a_{41} & a_{42} & a_{43} & a_{44} & a_{45} & a_{46}\\\\a_{51} & a_{52} & a_{53} & a_{54} & a_{55} & a_{56}\\\\a_{61} & a_{62} & a_{63} & a_{64} & a_{65} & a_{66}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a_11, a_12, a_13, a_14, a_15, a_16], [a_21, a_22, a_23, a_24, a_25, a_26], [a_31, a_32, a_33, a_34, a_35, a_36], [a_41, a_42, a_43, a_44, a_45, a_46], [a_51, a_52, a_53, a_54, a_55, a_56], [a_61, a_62, a_63, a_64, a_65, a_66]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSixBySixTensor.create(name=\"a\")\n",
    "display(A.data)\n",
    "display(A.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x_{1} & x_{2} & x_{3} & x_{4} & x_{5} & x_{6}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[x_1, x_2, x_3, x_4, x_5, x_6]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'x'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'standard'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x_{1} & x_{2} & x_{3}\\\\x_{2} & x_{4} & x_{5}\\\\x_{3} & x_{5} & x_{6}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[x_1, x_2, x_3], [x_2, x_4, x_5], [x_3, x_5, x_6]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.create(name=\"x\")\n",
    "display(A.data)\n",
    "display(A.name)\n",
    "display(A.notation)\n",
    "display(A.to_general().data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x_{11} & x_{22} & x_{33} & x_{23} & x_{13} & x_{12}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[x_11, x_22, x_33, x_23, x_13, x_12]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'x'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'voigt'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}x_{11} & x_{12} & x_{13}\\\\x_{12} & x_{22} & x_{23}\\\\x_{13} & x_{23} & x_{33}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[x_11, x_12, x_13], [x_12, x_22, x_23], [x_13, x_23, x_33]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.create(name=\"x\", notation=\"voigt\")\n",
    "display(A.data)\n",
    "display(A.name)\n",
    "display(A.notation)\n",
    "display(A.to_general().data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & a c\\\\b & b d\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, a*c], [b, b*d]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([a, 0, 0, b], shape=(2, 2))\n",
    "B = SymbolicTensor.from_list([1, c, 1, d], shape=(2, 2))\n",
    "C = A @ B\n",
    "display(C.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Converting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tensor to 3x3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicThreeByThreeTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([a, b, c, b, d, e, c, e, f], shape=(3,3))\n",
    "display(type(A))\n",
    "B = A.to_3x3()\n",
    "display(type(B))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### General to Symmetric 3x3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicSymmetricThreeByThreeTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c & d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, b, c, d, e, f]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([a, b, c, b, d, e, c, e, f], shape=(3,3))\n",
    "display(type(A))\n",
    "display(A.is_symmetric())\n",
    "B = A.to_sym_3x3()\n",
    "display(type(B))\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3x3 to Symmetric"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicThreeByThreeTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c\\\\b & d & e\\\\c & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b, c], [b, d, e], [c, e, f]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicSymmetricThreeByThreeTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c & d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, b, c, d, e, f]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicThreeByThreeTensor.from_list([a, b, c, b, d, e, c, e, f])\n",
    "display(type(A))\n",
    "display(A.data)\n",
    "display(A.is_symmetric())\n",
    "B = A.to_symmetric()\n",
    "display(type(B))\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Converting to general"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicSymmetricThreeByThreeTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c & d & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[a, b, c, d, e, f]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicSymmetricThreeByThreeTensor.from_list([a, b, c, d, e, f])\n",
    "display(type(A))\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mechpy.core.symbolic.tensor.SymbolicThreeByThreeTensor"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & c\\\\b & d & e\\\\c & e & f\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b, c], [b, d, e], [c, e, f]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = A.to_general()\n",
    "display(type(B))\n",
    "display(B.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiplication"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting Tensor Components"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b\\\\c & d\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b], [c, d]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([a, b, c, d], shape=(2, 2))\n",
    "display(A.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[a, b], [c, d]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display([[A[0, 0], A[0, 1]], [A[1, 0], A[1, 1]]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[a, b], [c, d]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display([[A[0][0], A[0][1]], [A[1][0], A[1][1]]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Eigenvalues and Eigenvectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(a,\n",
       "  1,\n",
       "  [Matrix([\n",
       "   [1],\n",
       "   [0]])]),\n",
       " (b,\n",
       "  1,\n",
       "  [Matrix([\n",
       "   [0],\n",
       "   [1]])])]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([[a, 0], [0, b]], shape=(2, 2))\n",
    "eigenvectors = A.eigenvectors()\n",
    "display(eigenvectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(a,\n",
       "  2,\n",
       "  [Matrix([\n",
       "   [1],\n",
       "   [0],\n",
       "   [0]]),\n",
       "   Matrix([\n",
       "   [0],\n",
       "   [1],\n",
       "   [0]])]),\n",
       " (b,\n",
       "  1,\n",
       "  [Matrix([\n",
       "   [0],\n",
       "   [0],\n",
       "   [1]])])]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([[a, 0, 0], [0, a, 0], [0, 0, b]], shape=(3, 3))\n",
    "eigenvectors = A.eigenvectors()\n",
    "display(eigenvectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{4}{17}\\\\- \\frac{8}{17}\\\\1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 4/17],\n",
       "[-8/17],\n",
       "[    1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{2} - \\frac{\\sqrt{15} i}{6}\\\\1\\\\0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1/2 - sqrt(15)*I/6],\n",
       "[                 1],\n",
       "[                 0]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}\\frac{1}{2} + \\frac{\\sqrt{15} i}{6}\\\\1\\\\0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1/2 + sqrt(15)*I/6],\n",
       "[                 1],\n",
       "[                 0]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([[4, -2, 0], [3, 1, -4], [0, 0, 8]], shape=(3, 3))\n",
    "eigenvectors = A.eigenvectors()\n",
    "display(*[_[2][0] for _ in eigenvectors])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle b$"
      ],
      "text/plain": [
       "b"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0\\\\1\\\\0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[0],\n",
       "[1],\n",
       "[0]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{a}{2} + \\frac{b}{2} - \\frac{\\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}$"
      ],
      "text/plain": [
       "a/2 + b/2 - sqrt(a**2 - 2*a*b + 4*a*c + b**2)/2"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}- \\frac{b}{a} + \\frac{\\frac{a}{2} + \\frac{b}{2} - \\frac{\\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}}{a}\\\\\\frac{- b + c}{c} + \\frac{b \\left(\\frac{a}{2} + \\frac{b}{2} - \\frac{\\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}\\right)}{a c}\\\\1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[            -b/a + (a/2 + b/2 - sqrt(a**2 - 2*a*b + 4*a*c + b**2)/2)/a],\n",
       "[(-b + c)/c + b*(a/2 + b/2 - sqrt(a**2 - 2*a*b + 4*a*c + b**2)/2)/(a*c)],\n",
       "[                                                                     1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{a}{2} + \\frac{b}{2} + \\frac{\\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}$"
      ],
      "text/plain": [
       "a/2 + b/2 + sqrt(a**2 - 2*a*b + 4*a*c + b**2)/2"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}- \\frac{b}{a} + \\frac{\\frac{a}{2} + \\frac{b}{2} + \\frac{\\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}}{a}\\\\\\frac{- b + c}{c} + \\frac{b \\left(\\frac{a}{2} + \\frac{b}{2} + \\frac{\\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}\\right)}{a c}\\\\1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[            -b/a + (a/2 + b/2 + sqrt(a**2 - 2*a*b + 4*a*c + b**2)/2)/a],\n",
       "[(-b + c)/c + b*(a/2 + b/2 + sqrt(a**2 - 2*a*b + 4*a*c + b**2)/2)/(a*c)],\n",
       "[                                                                     1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = SymbolicTensor.from_list([[a, 0, c], [a, b, b], [a, 0, b]], shape=(3, 3))\n",
    "eigenvectors = A.eigenvectors()\n",
    "for eig in eigenvectors:\n",
    "    eig_val = eig[0]\n",
    "    multiplicity = eig[1]\n",
    "    eig_vects = eig[2][0]\n",
    "    display(eig_val)\n",
    "    display(multiplicity)\n",
    "    display(eig_vects)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & \\frac{a - b - \\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2 a} & \\frac{a - b + \\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2 a}\\\\1 & \\frac{a \\left(- b + c\\right) + \\frac{b \\left(a + b - \\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}\\right)}{2}}{a c} & \\frac{a \\left(- b + c\\right) + \\frac{b \\left(a + b + \\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}\\right)}{2}}{a c}\\\\0 & 1 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[0,                    (a - b - sqrt(a**2 - 2*a*b + 4*a*c + b**2))/(2*a),                    (a - b + sqrt(a**2 - 2*a*b + 4*a*c + b**2))/(2*a)],\n",
       "[1, (a*(-b + c) + b*(a + b - sqrt(a**2 - 2*a*b + 4*a*c + b**2))/2)/(a*c), (a*(-b + c) + b*(a + b + sqrt(a**2 - 2*a*b + 4*a*c + b**2))/2)/(a*c)],\n",
       "[0,                                                                    1,                                                                    1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}b & 0 & 0\\\\0 & \\frac{a}{2} + \\frac{b}{2} - \\frac{\\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2} & 0\\\\0 & 0 & \\frac{a}{2} + \\frac{b}{2} + \\frac{\\sqrt{a^{2} - 2 a b + 4 a c + b^{2}}}{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[b,                                               0,                                               0],\n",
       "[0, a/2 + b/2 - sqrt(a**2 - 2*a*b + 4*a*c + b**2)/2,                                               0],\n",
       "[0,                                               0, a/2 + b/2 + sqrt(a**2 - 2*a*b + 4*a*c + b**2)/2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "P, D = A.diagonalize()\n",
    "display(P)\n",
    "display(D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}a & b & b\\\\b & 0 & c\\\\b & c & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "[[a, b, b], [b, 0, c], [b, c, 0]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = SymbolicSymmetricThreeByThreeTensor.from_list([a, b, b, 0, c, 0])\n",
    "display(B.to_general().data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & \\frac{a - c - \\sqrt{a^{2} - 2 a c + 8 b^{2} + c^{2}}}{2 b} & \\frac{a - c + \\sqrt{a^{2} - 2 a c + 8 b^{2} + c^{2}}}{2 b}\\\\-1 & 1 & 1\\\\1 & 1 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[ 0, (a - c - sqrt(a**2 - 2*a*c + 8*b**2 + c**2))/(2*b), (a - c + sqrt(a**2 - 2*a*c + 8*b**2 + c**2))/(2*b)],\n",
       "[-1,                                                  1,                                                  1],\n",
       "[ 1,                                                  1,                                                  1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}- c & 0 & 0\\\\0 & \\frac{a}{2} + \\frac{c}{2} - \\frac{\\sqrt{a^{2} - 2 a c + 8 b^{2} + c^{2}}}{2} & 0\\\\0 & 0 & \\frac{a}{2} + \\frac{c}{2} + \\frac{\\sqrt{a^{2} - 2 a c + 8 b^{2} + c^{2}}}{2}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[-c,                                                0,                                                0],\n",
       "[ 0, a/2 + c/2 - sqrt(a**2 - 2*a*c + 8*b**2 + c**2)/2,                                                0],\n",
       "[ 0,                                                0, a/2 + c/2 + sqrt(a**2 - 2*a*c + 8*b**2 + c**2)/2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "P, D = B.diagonalize()\n",
    "display(P)\n",
    "display(D)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}