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?

More than 1 year has passed since last update.

「sympyのRational」を調べました。

Last updated at Posted at 2023-10-17

目的:ラプラス変換の結果の小数の部分は、分数のはずです。

オリジナル。以下のコメントより。ありがとうございました。

DOC

limit_denominator(max_denominator=1000000)
https://docs.sympy.org/latest/modules/core.html#sympy.core.numbers.Rational.limit_denominator

sympyで

from sympy import *
print("#",1/6)
print("#",Rational(0.16666666666666666).limit_denominator(10**12) )
print("#",Rational(1/6)                .limit_denominator(10**12) )
print("#",Rational(1,6)                .limit_denominator(10**12) )
# 0.16666666666666666
# 1/6
# 1/6
# 1/6

sympyの自作myRld関数(小数を分数変換)で

・結果が、1/6*t**3 + t になっていません。
・1個のみです。←n個の時はどうするの?
 再帰?またはループの勉強中です。

from sympy import *
import re
var('t',positive=True)
def myRld(f): # ver0.1
     li = re.findall(r'[.0-9]+',str(f))
     sorted_li=sorted(li ,key=lambda s:len(s))
     syosu    =sorted_li[-1]
     bunsu=Rational(sympify(syosu)).limit_denominator(10**12)
     return sympify( str(f).replace(str(syosu),str(bunsu)) )
f=0.166666666666667*t**3 + t
print("#",      f )
print("#",myRld(f))
# 0.166666666666667*t**3 + t
# t**3/6 + t

いつもの? sympyの実行環境と 参考のおすすめです。

(テンプレート)

いつもと違うおすすめです。

0
0
1

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?