<jupyter_start><jupyter_text>巡回符号・ガロア体上の多項式環における剰余 参考 https://stackoverflow.com/questions/14173007/polynomial-with-modular-coefficients-library-in-pythonhttps://numpy.org/doc/stable/reference/generated/numpy.poly1d.htmlnumpy.poly1dhttps://stackoverflow.com/questions/28646336/pretty-printing-polynomials-in-ipython-notebook (表記)<jupyter_code># import numpy.polynomial.polynomial
import numpy as np
# 表示だけ．計算には不要
import sympy
from sympy.abc import x
sympy.init_printing()
def printp(poly):
  return sympy.Poly(poly.coef,x).as_expr()<jupyter_output><empty_output><jupyter_text>実数体上の多項式 $\mathbb{R}[x]$ とその演算を Python + NumPy で行う方法．表示にはSymPyこの計算は，（教科書で出てくる）整数環上の多項式 $\mathbb{Z}[x]$ の演算ではない<jupyter_code>A = np.poly1d([1,2,3,4])
print(A)
printp(A)
# 加算，減算，乗算
A = np.poly1d([1,2])
B = np.poly1d([3,1])
printp( A ), printp( B ), printp( A + B ), printp( A - B ), printp( A * B )
# 除算
A = np.poly1d([1,2,3])
B = np.poly1d([1,1])
printp( A ), printp( B )
A / B
# 商
printp( (A / B)[0] )
# 剰余
printp( (A / B)[1] )<jupyter_output><empty_output><jupyter_text>A/Bは商と剰余をリストで返す．(A/B)[1]とすると剰余だけ  有限体上の多項式 $\mathbb{F}_2[x]$ の演算係数を(0,1)に限って$\mathbb{R}[x]$ の演算を行った後で mod 2すればよい（例外的なものがあるかもしれない）<jupyter_code># 情報ビット
X = np.poly1d([1,1,1,0])
# 生成多項式
G = np.poly1d([1,0,1,1])
printp(X), printp(G)
xm = np.poly1d( [1] + [0] * G.order )
printp(xm)<jupyter_output><empty_output><jupyter_text>$ x^m = x^{\mathrm{deg}(G(x))}$ $ C(x) = X(x) \, x^{\mathrm{deg}(G(x))} \bmod G(x) $ $ \mathbb{R}[x]$ 上の剰余<jupyter_code>printp( (X*xm/G)[1] )<jupyter_output><empty_output><jupyter_text>$ \mathbb{F}_2[x] $上の剰余<jupyter_code>np.remainder( (X*xm/G)[1], 2 )
printp( np.poly1d( np.remainder( (X*xm/G)[1], 2 ) ) )<jupyter_output><empty_output><jupyter_text>np.remainderは正の値，np.fmodはdividendと同じ符号<jupyter_code>np.remainder(-5,3), np.fmod(-5,3)
## 4週目．巡回ハミング符号．単一誤りパターンに対するシンドロームの計算
for i in range(7):
  E = np.poly1d( [1] + [0] * i )
  S = np.poly1d( np.remainder( (E/G)[1], 2 ) )
  
  print("i = %d" % i), print("%s = E(x)" % E), print("%s = S(x)" % S), print('\n===\n')<jupyter_output>i = 0
 
1 = E(x)
 
1 = S(x)

===

i = 1
 
1 x = E(x)
 
1 x = S(x)

===

i = 2
   2
1 x = E(x)
   2
1 x = S(x)

===

i = 3
   3
1 x = E(x)
 
1 x + 1 = S(x)

===

i = 4
   4
1 x = E(x)
   2
1 x + 1 x = S(x)

===

i = 5
   5
1 x = E(x)
   2
1 x + 1 x + 1 = S(x)

===

i = 6
   6
1 x = E(x)
   2
1 x + 1 = S(x)

===<jupyter_text>forループの中ではprintpは動かないみたい  参考SymPyに，$ \mathbb{F}_2[x]$ をちゃんと計算する方法はあるただし，上記の方が筆算の途中経過等が分かりやすいと思われるhttps://docs.sympy.org/latest/modules/polys/internals.htmlhttps://stackoverflow.com/questions/48065360/interpolate-polynomial-over-a-finite-field?rq=1<jupyter_code>from sympy.polys.domains import ZZ
from sympy.polys.galoistools import gf_div, gf_add_mul
print(gf_div(ZZ.map([1,1,1,0,0,0,0]), ZZ.map([1,0,1,1]), 2, ZZ)[1])
P = ZZ.map([1,1,1,0,0,0,0])
sympy.Poly(P,x).as_expr()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2115518441196938, 'fraction_numerical': 0.04871259568545581, 'mean_word_length': 4.86734693877551, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '302108', 'license': 'MIT', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1636, 'n_words': 225, 'path': 'it3.ipynb', 'repo_name': 'kojiyam/information-theory', 'size': '2874'}