Skip to content

Instantly share code, notes, and snippets.

@reesepathak
Created January 18, 2018 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reesepathak/b38c09b3f46cec3b7c41c088c6ea082a to your computer and use it in GitHub Desktop.
Save reesepathak/b38c09b3f46cec3b7c41c088c6ea082a to your computer and use it in GitHub Desktop.
Eigenvalues
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from sympy import var, Matrix\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def get_eigenvalues(n):\n",
" p = var('p', positive=True)\n",
" q = 1 - 2*p \n",
" row = [q, p]\n",
" row.extend(np.zeros(n - 3))\n",
" row.append(p)\n",
" m = Matrix([list(np.roll(row, i)) for i in range(n)])\n",
" return m.eigenvals()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"def print_eigenlist(eigendict):\n",
" n = sum(eigendict.values())\n",
" print(\"n = {}. \".format(n) + \"=\"*70)\n",
" for (k, v) in eigendict.items():\n",
" print(\"Multiplicity: {} \\t \\t Eigenvalue: {}\".format(v, k))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = 4. ======================================================================\n",
"Multiplicity: 1 \t \t Eigenvalue: 1\n",
"Multiplicity: 1 \t \t Eigenvalue: -4*p + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -2*p + 1\n",
"n = 5. ======================================================================\n",
"Multiplicity: 1 \t \t Eigenvalue: 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -5*p/2 + sqrt(5)*p/2 + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -5*p/2 - sqrt(5)*p/2 + 1\n",
"n = 6. ======================================================================\n",
"Multiplicity: 1 \t \t Eigenvalue: 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -3*p + 1\n",
"Multiplicity: 1 \t \t Eigenvalue: -4*p + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -p + 1\n",
"n = 7. ======================================================================\n",
"Multiplicity: 1 \t \t Eigenvalue: 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -7*p/3 - (-42*p**2 + 42*p + (7*p - 3)**2 - 9)/(3*(-1/2 + sqrt(3)*I/2)*(189*p**3/2 - 189*p**2 + 189*p/2 + (7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3)/2 + sqrt(-4*(-42*p**2 + 42*p + (7*p - 3)**2 - 9)**3 + (189*p**3 - 378*p**2 + 189*p + 2*(7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3) - 27)**2)/2 - 27/2)**(1/3)) - (-1/2 + sqrt(3)*I/2)*(189*p**3/2 - 189*p**2 + 189*p/2 + (7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3)/2 + sqrt(-4*(-42*p**2 + 42*p + (7*p - 3)**2 - 9)**3 + (189*p**3 - 378*p**2 + 189*p + 2*(7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3) - 27)**2)/2 - 27/2)**(1/3)/3 + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -7*p/3 - (-42*p**2 + 42*p + (7*p - 3)**2 - 9)/(3*(-1/2 - sqrt(3)*I/2)*(189*p**3/2 - 189*p**2 + 189*p/2 + (7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3)/2 + sqrt(-4*(-42*p**2 + 42*p + (7*p - 3)**2 - 9)**3 + (189*p**3 - 378*p**2 + 189*p + 2*(7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3) - 27)**2)/2 - 27/2)**(1/3)) - (-1/2 - sqrt(3)*I/2)*(189*p**3/2 - 189*p**2 + 189*p/2 + (7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3)/2 + sqrt(-4*(-42*p**2 + 42*p + (7*p - 3)**2 - 9)**3 + (189*p**3 - 378*p**2 + 189*p + 2*(7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3) - 27)**2)/2 - 27/2)**(1/3)/3 + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -7*p/3 - (-42*p**2 + 42*p + (7*p - 3)**2 - 9)/(3*(189*p**3/2 - 189*p**2 + 189*p/2 + (7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3)/2 + sqrt(-4*(-42*p**2 + 42*p + (7*p - 3)**2 - 9)**3 + (189*p**3 - 378*p**2 + 189*p + 2*(7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3) - 27)**2)/2 - 27/2)**(1/3)) - (189*p**3/2 - 189*p**2 + 189*p/2 + (7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3)/2 + sqrt(-4*(-42*p**2 + 42*p + (7*p - 3)**2 - 9)**3 + (189*p**3 - 378*p**2 + 189*p + 2*(7*p - 3)**3 - (63*p - 27)*(14*p**2 - 14*p + 3) - 27)**2)/2 - 27/2)**(1/3)/3 + 1\n",
"n = 8. ======================================================================\n",
"Multiplicity: 1 \t \t Eigenvalue: 1\n",
"Multiplicity: 1 \t \t Eigenvalue: -4*p + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -2*p - sqrt(2)*p + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -2*p + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -2*p + sqrt(2)*p + 1\n",
"n = 9. ======================================================================\n",
"Multiplicity: 1 \t \t Eigenvalue: 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -2*p - (-27*p**2 + 36*p + (6*p - 3)**2 - 9)/(3*(-1/2 + sqrt(3)*I/2)*(81*p**3/2 - 243*p**2/2 + 81*p + (6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3)/2 + sqrt(-4*(-27*p**2 + 36*p + (6*p - 3)**2 - 9)**3 + (81*p**3 - 243*p**2 + 162*p + 2*(6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3) - 27)**2)/2 - 27/2)**(1/3)) - (-1/2 + sqrt(3)*I/2)*(81*p**3/2 - 243*p**2/2 + 81*p + (6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3)/2 + sqrt(-4*(-27*p**2 + 36*p + (6*p - 3)**2 - 9)**3 + (81*p**3 - 243*p**2 + 162*p + 2*(6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3) - 27)**2)/2 - 27/2)**(1/3)/3 + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -3*p + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -2*p - (-27*p**2 + 36*p + (6*p - 3)**2 - 9)/(3*(81*p**3/2 - 243*p**2/2 + 81*p + (6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3)/2 + sqrt(-4*(-27*p**2 + 36*p + (6*p - 3)**2 - 9)**3 + (81*p**3 - 243*p**2 + 162*p + 2*(6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3) - 27)**2)/2 - 27/2)**(1/3)) - (81*p**3/2 - 243*p**2/2 + 81*p + (6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3)/2 + sqrt(-4*(-27*p**2 + 36*p + (6*p - 3)**2 - 9)**3 + (81*p**3 - 243*p**2 + 162*p + 2*(6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3) - 27)**2)/2 - 27/2)**(1/3)/3 + 1\n",
"Multiplicity: 2 \t \t Eigenvalue: -2*p - (-27*p**2 + 36*p + (6*p - 3)**2 - 9)/(3*(-1/2 - sqrt(3)*I/2)*(81*p**3/2 - 243*p**2/2 + 81*p + (6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3)/2 + sqrt(-4*(-27*p**2 + 36*p + (6*p - 3)**2 - 9)**3 + (81*p**3 - 243*p**2 + 162*p + 2*(6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3) - 27)**2)/2 - 27/2)**(1/3)) - (-1/2 - sqrt(3)*I/2)*(81*p**3/2 - 243*p**2/2 + 81*p + (6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3)/2 + sqrt(-4*(-27*p**2 + 36*p + (6*p - 3)**2 - 9)**3 + (81*p**3 - 243*p**2 + 162*p + 2*(6*p - 3)**3 - (54*p - 27)*(9*p**2 - 12*p + 3) - 27)**2)/2 - 27/2)**(1/3)/3 + 1\n"
]
}
],
"source": [
"for i in range(4, 10):\n",
" print_eigenlist(get_eigenvalues(i))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment