Skip to content

Instantly share code, notes, and snippets.

@tarassh
Created August 22, 2023 14:19
Show Gist options
  • Save tarassh/c7dc6744666e4b09a79b9ee5debc7722 to your computer and use it in GitHub Desktop.
Save tarassh/c7dc6744666e4b09a79b9ee5debc7722 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "f885a6f3",
"metadata": {},
"source": [
"Evaluate: $ x^{3} + x + 5 $"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "c4fc2b48",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 9, 30, 35)\n",
"(3, 3, 1, 1)\n",
"(9, 27, 30, 35)\n",
"True\n"
]
}
],
"source": [
"x = 3\n",
"v1 = x*x\n",
"v2 = v1*x\n",
"v3 = v2 + x\n",
"out = v3 + 5\n",
"\n",
"F73 = GF(73)\n",
"\n",
"w = vector(F73,[1, out, x, v1, v2, v3])\n",
"\n",
"A = Matrix(F73, [\n",
" [0, 0, 1, 0, 0, 0],\n",
" [0, 0, 0, 1, 0, 0],\n",
" [0, 0, 1, 0, 1, 0],\n",
" [5, 0, 0, 0, 0, 1]\n",
"])\n",
"\n",
"B = Matrix(F73, [\n",
" [0, 0, 1, 0, 0, 0],\n",
" [0, 0, 1, 0, 0, 0],\n",
" [1, 0, 0, 0, 0, 0],\n",
" [1, 0, 0, 0, 0, 0],\n",
"])\n",
"\n",
"C = Matrix(F73, [\n",
" [0, 0, 0, 1, 0, 0],\n",
" [0, 0, 0, 0, 1, 0],\n",
" [0, 0, 0, 0, 0, 1],\n",
" [0, 1, 0, 0, 0, 0]\n",
"])\n",
"\n",
"Aw = A*w\n",
"Bw = B*w\n",
"Cw = C*w\n",
"\n",
"print(Aw)\n",
"print(Bw)\n",
"print(Cw)\n",
"\n",
"# Compute the Hadamard product\n",
"AwBw = vector([Aw[i]*Bw[i] for i in range(len(Aw))])\n",
"\n",
"print(Cw == AwBw)\n"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "129cb758",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A\n",
"[68 70 68 13]\n",
"[ 0 0 0 0]\n",
"[ 8 13 5 48]\n",
"[67 46 69 37]\n",
"[ 4 66 40 36]\n",
"[72 14 72 61]\n",
"\n",
"B\n",
"[ 3 7 39 24]\n",
"[ 0 0 0 0]\n",
"[71 66 34 49]\n",
"[ 0 0 0 0]\n",
"[ 0 0 0 0]\n",
"[ 0 0 0 0]\n",
"\n",
"C\n",
"[ 0 0 0 0]\n",
"[72 14 72 61]\n",
"[ 0 0 0 0]\n",
"[ 4 20 38 12]\n",
"[67 46 69 37]\n",
"[ 4 66 40 36]\n",
"\n"
]
}
],
"source": [
"M = [A, B, C]\n",
"PolyMtx = []\n",
"\n",
"for m in M:\n",
" PolyList = []\n",
" for i in range(m.ncols()):\n",
" points = []\n",
" for j in range(m.nrows()):\n",
" points.append([j+1, m[j, i]])\n",
" \n",
" Poly = R73.lagrange_polynomial(points).coefficients()\n",
" \n",
" if (len(Poly) < m.nrows()):\n",
" diff = m.nrows() - len(Poly)\n",
" for c in range(diff):\n",
" Poly.append(0)\n",
" \n",
" PolyList.append(Poly)\n",
" \n",
" PolyMtx.append(Matrix(F73, PolyList))\n",
" \n",
"print(f'''A\n",
"{PolyMtx[0]}\n",
"''')\n",
"\n",
"print(f'''B\n",
"{PolyMtx[1]}\n",
"''')\n",
"\n",
"print(f'''C\n",
"{PolyMtx[2]}\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "70224c7a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ax = 7*x^3 + 2*x^2 + 24*x + 43\n",
"Bx = 25*x^3 + 68*x^2 + 59*x + 70\n",
"Cx = 15*x^3 + 12*x^2 + 23*x + 32\n"
]
}
],
"source": [
"Ax = R73(list(w*PolyMtx[0]))\n",
"Bx = R73(list(w*PolyMtx[1]))\n",
"Cx = R73(list(w*PolyMtx[2]))\n",
"\n",
"print(\"Ax = \", Ax)\n",
"print(\"Bx = \", Bx)\n",
"print(\"Cx = \", Cx)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "b5163ac0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T = 29*x^6 + 15*x^5 + 54*x^4 + 15*x^3 + 15*x^2 + 33*x + 58\n",
"Polynomial roots\n",
"T(1) = 0\n",
"T(2) = 0\n",
"T(3) = 0\n",
"T(4) = 0\n",
"Z = 24\n",
"Quotient of Z/T = 59*x^6 + 28*x^5 + 57*x^4 + 28*x^3 + 28*x^2 + 47*x + 45\n",
"Remainder of Z/T = 0\n"
]
}
],
"source": [
"T = Ax*Bx - Cx\n",
"\n",
"print(\"T = \", T)\n",
"print(\"Polynomial roots\")\n",
"print(\"T(1) =\", T(1))\n",
"print(\"T(2) =\", T(2))\n",
"print(\"T(3) =\", T(3))\n",
"print(\"T(4) =\", T(4))\n",
"\n",
"x = 5\n",
"Z = R73((x-1)*(x-2)*(x-3)*(x-4))\n",
"\n",
"print(\"Z =\", Z)\n",
"\n",
"H = T.quo_rem(Z)\n",
"print(\"Quotient of Z/T = \", end=\"\")\n",
"print(H[0])\n",
"print(\"Remainder of Z/T = \", end=\"\")\n",
"print(H[1])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a11e86e1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 10.0",
"language": "sage",
"name": "sagemath-10.0"
},
"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.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment