Skip to content

Instantly share code, notes, and snippets.

@tarassh
Created August 22, 2023 14:37
Show Gist options
  • Save tarassh/12159cf5275b8e1ea247af08f38e0b6b to your computer and use it in GitHub Desktop.
Save tarassh/12159cf5275b8e1ea247af08f38e0b6b 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": 3,
"id": "c4fc2b48",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 9)\n",
"(3, 3)\n",
"(9, 33)\n",
"False\n"
]
}
],
"source": [
"x = 3\n",
"v1 = x*x\n",
"out = v1*x + x + 5\n",
"\n",
"F73 = GF(73)\n",
"\n",
"w = vector(F73,[1, out, x, v1])\n",
"\n",
"A = Matrix(F73, [\n",
" [0, 0, 1, 0],\n",
" [0, 0, 0, 1]\n",
"])\n",
"\n",
"B = Matrix(F73, [\n",
" [0, 0, 1, 0],\n",
" [0, 0, 1, 0]\n",
"])\n",
"\n",
"C = Matrix(F73, [\n",
" [0, 0, 0, 1],\n",
" [-5, 1, 1, 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": 5,
"id": "129cb758",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A\n",
"[ 0 0]\n",
"[ 0 0]\n",
"[ 2 72]\n",
"[72 1]\n",
"\n",
"B\n",
"[0 0]\n",
"[0 0]\n",
"[1 0]\n",
"[0 0]\n",
"\n",
"C\n",
"[ 5 68]\n",
"[72 1]\n",
"[72 1]\n",
"[ 2 72]\n",
"\n"
]
}
],
"source": [
"R73.<x> = PolynomialRing(F73)\n",
"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": 6,
"id": "70224c7a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ax = 6*x + 70\n",
"Bx = 3\n",
"Cx = 24*x + 58\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": 7,
"id": "b5163ac0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T = 67*x + 6\n",
"Polynomial roots\n",
"T(1) = 0\n",
"T(2) = 67\n",
"T(3) = 61\n",
"T(4) = 55\n",
"Z = 24\n",
"Quotient of Z/T = 18*x + 55\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