Skip to content

Instantly share code, notes, and snippets.

@fehiepsi
Last active April 15, 2018 15:58
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 fehiepsi/807d5135c79681e49d3f275bfff3e47e to your computer and use it in GitHub Desktop.
Save fehiepsi/807d5135c79681e49d3f275bfff3e47e to your computer and use it in GitHub Desktop.
use python 2.7
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from permanent import permanent"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def det_and_per(n, k, p):\n",
" k_matrices = np.random.randint(-p+1, p+1, size=(k, n, n))\n",
" det_list = np.zeros(2*p + 1)\n",
" per_list = np.zeros(2*p + 1)\n",
" for matrix in np.split(k_matrices, k):\n",
" matrix = matrix.reshape(n, n)\n",
" det = int(np.linalg.det(matrix)) % (2*p+1)\n",
" per = int(permanent(matrix.astype(complex)).real) % (2*p+1)\n",
" det_list[det] = det_list[det] + 1\n",
" per_list[per] = per_list[per] + 1\n",
" print(\"Determinent distribution: {}\".format((det_list / k).tolist()))\n",
" print(\"Permanent distribution: {}\".format((per_list / k).tolist()))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Determinent distribution: [0.3929, 0.2984, 0.3087]\n",
"Permanent distribution: [0.3311, 0.3278, 0.3411]\n",
"CPU times: user 4min 16s, sys: 39.2 ms, total: 4min 16s\n",
"Wall time: 4min 16s\n"
]
}
],
"source": [
"%%time\n",
"det_and_per(15, 10000, 1)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (py27)",
"language": "python",
"name": "py27"
},
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment