Skip to content

Commit

Permalink
update en code, and fix tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
AmazingAng committed Aug 5, 2024
1 parent b29c583 commit 24f9c03
Show file tree
Hide file tree
Showing 22 changed files with 2,219 additions and 24 deletions.
2 changes: 1 addition & 1 deletion 24_Polynomial/Polynomial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
" Y = list(data)\n",
"\n",
"# 使用 lagrange 函数构造拉格朗日插值多项式\n",
"interpolation_polynomial = interpolating_poly(len(data_points), x, X, Y)\n",
"interpolation_polynomial = interpolating_poly(len(data), x, X, Y)\n",
"\n",
"# 输出插值多项式\n",
"print(\"拉格朗日多项式:\", interpolation_polynomial)\n",
Expand Down
4 changes: 2 additions & 2 deletions 27_GaloisField/GaloisField.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"print(\"有限域 GF(4) 的元素\", GF4.elements)\n",
"\n",
"print(\"有限域 GF(4) 的加法表\")\n",
"print(GF7.arithmetic_table(\"+\"))\n",
"print(GF4.arithmetic_table(\"+\"))\n",
"print(\"有限域 GF(4) 的乘法表\")\n",
"print(GF7.arithmetic_table(\"*\"))\n",
"print(GF4.arithmetic_table(\"*\"))\n",
"\n",
"## 输出示例\n",
"# 有限域 GF(4) 的性质 Galois Field:\n",
Expand Down
4 changes: 2 additions & 2 deletions 27_GaloisField/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ print("有限域 GF(4) 的性质", GF4.properties)
print("有限域 GF(4) 的元素", GF4.elements)

print("有限域 GF(4) 的加法表")
print(GF7.arithmetic_table("+"))
print(GF4.arithmetic_table("+"))
print("有限域 GF(4) 的乘法表")
print(GF7.arithmetic_table("*"))
print(GF4.arithmetic_table("*"))

## 输出示例
# 有限域 GF(4) 的性质 Galois Field:
Expand Down
1 change: 1 addition & 0 deletions 32_ECC/ECC.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"# EC ElGamal\n",
"\n",
"from py_ecc.secp256k1 import secp256k1\n",
"from random import randint\n",
"\n",
"def elgamal_encrypt(G, Y, M):\n",
" k = randint(1, secp256k1.N - 1)\n",
Expand Down
1 change: 1 addition & 0 deletions 32_ECC/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Bob 收到密文 $(C_1, C_2)$ 后,需要使用私钥 $x$ 进行解密:

```python
from py_ecc.secp256k1 import secp256k1
from random import randint

def elgamal_encrypt(G, Y, M):
k = randint(1, secp256k1.N - 1)
Expand Down
115 changes: 115 additions & 0 deletions Languages/en/24_Polynomial/Polynomial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "a20a2420",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original polynomial: x**3 - 10*x**2 + 31*x - 30\n",
"Factorization of polynomial: (x - 5)*(x - 3)*(x - 2)\n"
]
}
],
"source": [
"# Prime Factorization of polynomials\n",
"from sympy import symbols, factor\n",
"from sympy.abc import x\n",
"\n",
"polynomial = x**3 -10*x**2 + 31*x - 30\n",
"\n",
"factored_polynomial = factor(polynomial)\n",
"\n",
"print(\"Original polynomial:\", polynomial)\n",
"print(\"Factorization of polynomial:\", factored_polynomial)\n",
"\n",
"# Output\n",
"# Original polynomial: x**3 - 10*x**2 + 31*x - 30\n",
"# Factorization of polynomial: (x - 5)*(x - 3)*(x - 2)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "89ff714b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Lagrange Polynomial: (x - 3)*(x - 2) - 3*(x - 3)*(x - 1) + 4*(x - 2)*(x - 1)\n",
"Simplified Polynomial: 2*x**2 - 5*x + 5\n",
"Value at x=4: 8\n"
]
}
],
"source": [
"# Lagrange Interpolation Method\n",
"from sympy import symbols\n",
"from sympy.abc import x\n",
"from sympy.polys.polyfuncs import interpolating_poly\n",
"\n",
"# Given interpolation points and their corresponding function values\n",
"data = [(1, 2), (2, 3), (3, 8)]\n",
"\n",
"if isinstance(data, dict):\n",
" X, Y = list(zip(*data.items()))\n",
"else:\n",
" if isinstance(data[0], tuple):\n",
" X, Y = list(zip(*data))\n",
" else:\n",
" X = list(range(1, n + 1))\n",
" Y = list(data)\n",
"\n",
"# Use the interpolating_poly function to construct the Lagrange interpolation polynomial\n",
"interpolation_polynomial = interpolating_poly(len(data), x, X, Y)\n",
"\n",
"# Output the interpolation polynomial\n",
"print(\"Lagrange Polynomial:\", interpolation_polynomial)\n",
"print(\"Simplified Polynomial:\", interpolation_polynomial.expand())\n",
"# Calculate the value at x=4 using the interpolation polynomial\n",
"result = interpolation_polynomial.subs(x, 3)\n",
"print(\"Value at x=4:\", result)\n",
"\n",
"# Sample Output\n",
"# Lagrange Polynomial: (x - 3)*(x - 2) - 3*(x - 3)*(x - 1) + 4*(x - 2)*(x - 1)\n",
"# Simplified Polynomial: 2*x**2 - 5*x + 5\n",
"# Value at x=4: 8\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f1b05a6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
111 changes: 111 additions & 0 deletions Languages/en/25_PolyRing/PolyRing.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "a20a2420",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Polynomial p1: Poly(2*x**3 - 2*x**2 + x + 1, x, modulus=5)\n",
"Polynomial p2: Poly(x**2 - x - 1, x, modulus=5)\n",
"Addition result: Poly(2*x**3 - x**2, x, modulus=5)\n",
"Subtraction result: Poly(2*x**3 + 2*x**2 + 2*x + 2, x, modulus=5)\n",
"Multiplication result: Poly(2*x**5 + x**4 + x**3 + 2*x**2 - 2*x - 1, x, modulus=5)\n",
"Modulo operation: Poly(-2*x + 1, x, modulus=5)\n",
"Quotient: Poly(2*x, x, modulus=5)\n",
"Greatest common divisor: Poly(x + 2, x, modulus=5)\n",
"Result of substituting x = 2 into polynomial p1: 1\n"
]
}
],
"source": [
"from sympy import symbols, Poly, GF, gcd\n",
"\n",
"# Define symbolic variables\n",
"x = symbols('x')\n",
"\n",
"# Define polynomials with coefficients in the ring of integers modulo 5\n",
"p1 = Poly(2*x**3 + 3*x**2 + x + 1, x, domain=GF(5))\n",
"p2 = Poly(x**2 + 4*x + 4, x, domain=GF(5))\n",
"\n",
"# Print the polynomials\n",
"print(\"Polynomial p1:\", p1)\n",
"print(\"Polynomial p2:\", p2)\n",
"\n",
"\n",
"# Polynomial addition\n",
"add_result = p1 + p2\n",
"print(\"Addition result:\", add_result)\n",
"\n",
"# Polynomial subtraction\n",
"sub_result = p1 - p2\n",
"print(\"Subtraction result:\", sub_result)\n",
"\n",
"# Polynomial multiplication\n",
"mul_result = p1 * p2\n",
"print(\"Multiplication result:\", mul_result)\n",
"\n",
"# Note: In the ring of integers modulo n, polynomial division is not always possible\n",
"# However, modulo operation can be performed\n",
"mod_result = p1 % p2\n",
"print(\"Modulo operation:\", mod_result)\n",
"\n",
"# Quotient\n",
"quotient_result = p1 // p2\n",
"print(\"Quotient:\", quotient_result)\n",
"\n",
"# Greatest common divisor\n",
"gcd_result = gcd(p1, p2)\n",
"print(\"Greatest common divisor:\", gcd_result)\n",
"\n",
"# Substituting values to solve\n",
"value_result = p1.subs(x, 2)\n",
"print(\"Result of substituting x = 2 into polynomial p1:\", value_result)\n",
"\n",
"## Output Example\n",
"# Polynomial p1: Poly(2*x**3 - 2*x**2 + x + 1, x, modulus=5)\n",
"# Polynomial p2: Poly(x**2 - x - 1, x, modulus=5)\n",
"# Addition result: Poly(2*x**3 - x**2, x, modulus=5)\n",
"# Subtraction result: Poly(2*x**3 + 2*x**2 + 2*x + 2, x, modulus=5)\n",
"# Multiplication result: Poly(2*x**5 + x**4 + x**3 + 2*x**2 - 2*x - 1, x, modulus=5)\n",
"# Modulo operation: Poly(-2*x + 1, x, modulus=5)\n",
"# Quotient: Poly(2*x, x, modulus=5)\n",
"# Greatest common divisor: Poly(x + 2, x, modulus=5)\n",
"# Result of substituting x = 2 into polynomial p1: 1\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f1b05a6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
78 changes: 78 additions & 0 deletions Languages/en/26_FieldExtension/FieldExtension.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "04209408",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extension Field: QQ<sqrt(2)>\n",
"a = ANP([MPQ(1,2)], [MPQ(1,1), MPQ(0,1), MPQ(-2,1)], QQ)\n",
"b = ANP([MPQ(3,1)], [MPQ(1,1), MPQ(0,1), MPQ(-2,1)], QQ)\n",
"a + b = ANP([MPQ(7,2)], [MPQ(1,1), MPQ(0,1), MPQ(-2,1)], QQ)\n",
"a * b = ANP([MPQ(3,2)], [MPQ(1,1), MPQ(0,1), MPQ(-2,1)], QQ)\n"
]
}
],
"source": [
"from sympy import symbols, QQ, RootOf\n",
"\n",
"# Define symbolic variables\n",
"x = symbols('x')\n",
"\n",
"# Construct the algebraic extension QQ/(sqrt(2)) (which is a root of x^2 - 2 = 0) over the rational number field QQ\n",
"ext_field = QQ.algebraic_field(RootOf(x**2 - 2, 1))\n",
"\n",
"# Output the extension field and algebraic element\n",
"print(\"Extension Field: \", ext_field)\n",
"# QQ<sqrt(2)>\n",
"\n",
"\n",
"\n",
"# Calculation on extention field\n",
"a = ext_field(QQ(1, 2))\n",
"b = ext_field(3)\n",
"c = a + b\n",
"d = a * b\n",
"\n",
"print(\"a =\", a)\n",
"print(\"b =\", b)\n",
"print(\"a + b =\", c)\n",
"print(\"a * b =\", d)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c678725",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 24f9c03

Please sign in to comment.