前提
DjangoとSympy, Mathjaxを併用して、ランダムな計算プリントを出力するシステムを作っています。そのシステム上で簡単な対数の計算を行えるようにしたいと思い試作を行っている最中です。初めはPythonの組み込みモジュールmathや、外部ライブラリのSympyをそのまま利用しようとしたのですが、いずれも指定した底を保ちながら計算するようにはなっていなかったため、クラスを自作することにしました。
実現したいこと
自作クラス同士の足し算と引き算、自作クラスと整数とのかけ算までは真数部分の計算を行うことで実装ができたのですが、以下の計算の実装にてこずっています。
- 底が異なるため、そのままの結果になる足し算と引き算
log_2(3) + log_3(5) = log_2(3) + log_3(5)
log_2(3) - log_3(5) = log_2(3) - log_3(5)
- 自作クラス同士のかけ算と割り算
log_2(3) * log_5(4) = log_2(3) * log_5(4)
log_2(3) / log_5(4) = log_2(3) / log_5(4)
log_2(3) / log_2(3) = 1
イメージとしては、計算ができるときは実行して、できないときはそのまま値を保有しておいて欲しいのですが、それを実装するための方法が思い浮かびません。自作クラスの修正案でもよいですし、前述のmath.logやsympy.logを使って底を保って計算する方法でも構いません。何かしらのご教示をいただけると幸いです。
該当のソースコード
python
1import sympy as sy 2 3class Log: 4 def __init__(self, base_numerator, antilog_numerator, base_denominator=1, antilog_denominator=1): 5 base = sy.Rational(base_numerator, base_denominator) 6 antilog = sy.Rational(antilog_numerator, antilog_denominator) 7 if (base <= 0) or (antilog < 0): 8 raise ValueError(f"base must be more than 0, and antilog must be 0 or more than 0.") 9 10 self.base = base 11 self.antilog = antilog 12 self.other_log_part = 0 # = 0 13 14 def __str__(self): 15 return f"\log_{{{self.base}}} {self.antilog}" 16 17 def __add__(self, other_num): 18 if isinstance(other_num, Log): 19 if self.base == other_num.base: 20 return Log(self.base, self.antilog * other_num.antilog) 21 else: 22 new_antilog = self.base ** other_num 23 return Log(self.base, self.antilog * new_antilog) 24 25 def __radd__(self, other_num): 26 new_antilog = self.base ** other_num 27 return Log(self.base, new_antilog * self.antilog) 28 29 def __sub__(self, other_num): 30 if isinstance(other_num, Log): 31 if self.base == other_num.base: 32 return Log(self.base, self.antilog / other_num.antilog) 33 else: 34 new_antilog = self.base ** other_num 35 return Log(self.base, self.antilog / new_antilog) 36 37 def __rsub__(self, other_num): 38 new_antilog = self.base ** other_num 39 return Log(self.base, new_antilog / self.antilog) 40 41 def __mul__(self, other_num): 42 if not(isinstance(other_num, Log)): 43 new_antilog = self.antilog ** other_num 44 return Log(self.base, new_antilog) 45 46 def __rmul__(self, other_num): 47 new_antilog = self.antilog ** other_num 48 return Log(self.base, new_antilog)
動作確認とその結果
python
1num1 = Log(2, 3) 2num2 = Log(2, 5) 3print(num1 + num2) # \log_{2} 15 4print(num1 - num2) # \log_{2} 3/5 5print(num1 + 2) # \log_{2} 12 6print(2 + num1) # \log_{2} 12 7print(num1 - 2) # \log_{2} 3/4 8print(2 - num1) # \log_{2} 4/3 9print(2 * num1) # \log_{2} 9 10print(num1 * 2) # \log_{2} 9
補足情報(FW/ツールのバージョンなど)
python 3.8.12
sympy 1.9

回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/07/11 01:02