0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

lra_theoryのリファクタリング (1)

0
Posted at

やったこと

内容

sympy.logic.algorithms.lra_theory.pyにある内部関数をキレイにした。

list_terms

list_termsは、$3x+4y$を$[3x, 4y]$のように項毎にする内部関数である。足し算でない場合、例えば$5x$のような式が入力された場合は$[5x]$として、入力をリストにして返す。同等の機能はAdd.make_args(expr)で実現できるので、車輪の再発明を避けた。

_sep_const_coeff, _sep_const_terms

_sep_const_coeff_sep_const_termsはどちらも自由変数を含む部分とそうでない部分に分離する関数である。free_symbolsで自由変数を確認できる。前段の処理は、list_termsと同様にMul.make_argsAdd.make_argsでできるし、後続の処理もsympy.utilities.iterables.siftというユーティリティ関数が用意されているので、こちらを使った方が短く書ける。

まとめ

既に実装されているならそれを使うというのは、言うは易しである。こういう関数があることを知っているかは、単なる知識でしかない。私も、utilitiesを読んだことがあったから知っていたのであって、言う程SymPyについて精通しているわけではない。

lra_theoryは他にも直したいところはあるが、どう手を付けてよいか判断が付きかねているので、気長に修正していきたい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?