1
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.

関数の増減表作成プログラム_Markdown表(1/2) sympyで作ってみたい。

Last updated at Posted at 2022-07-05

(2023/07/02) バグあり。調査中。

csv形式も出力しています。csv形式→Markdown表へ変換しました。
おすすすめがあれば、教えて下さい。

会話記録(2022年6月梅雨時編) 

会話記録は誘導です。久しぶりに,中学生のA子さんとB子さん登場。(5月GW編自転車君以来)

中学生A子(以下A子)「この暑い時期にどうして私たちが登場しないとだめなの。」
中学生B子(以下B子)「何か増減表とか言っているわよ。」
私 「増減表は、関数のグラフを書くのに必要です。」
A子「スマホのwolframalphaで、グラフが簡単に書けるのに必要なの?」
B子「wolframalphaで、最大値、最小値に続けて関数を入力すれば、最大値、最小値も計算してくれるわよ。」
A子「wolframalphaで、極値に続けて関数を入力すれば、極大値、極小値も計算してくれるね。」
A子,B子「スマホで関数のグラフが書けるのに、めんどくさいわね。」
私 「それで、増減表作成アプリを探したけど、みつかりませんでした。」

sympyでVer0.11

online sympyでうごきませんでした。Error.
わかる方,教えて下さい。

# 関数の増減表作成プログラム_Markdown表 Ver0.11
from sympy import *
var('x')
def myZogenhyoCsv(y):
    dy=diff(y, x)
    ans=myList(dy)
    n=len(ans)
    m=n*2+1
    myHyo0 = [".... "]
    myHyo1 = [myFugo(dy, ans[0] - 3)]
    myHyo2 = [myYajirusi(myHyo1[0])]
    j=0
    for i in range(n):
        j=j+1
        myHyo0 = myHyo0 + [ans[i]]
        myHyo1 = myHyo1 + ["0"]
        myHyo2 = myHyo2 + [y.subs({x:ans[i]}).simplify()] #20220808
        if i<n-1:
           j = j + 1
           myHyo0 = myHyo0 + ["..."]
           myHyo1 = myHyo1 + [myFugo(dy,(ans[i]+ans[i+1])/2)]
           myHyo2 = myHyo2 + [myYajirusi(myHyo1[j])]
    j = j + 1
    myHyo0 = myHyo0 + ["...."]
    myHyo1 = myHyo1 + [myFugo(dy, ans[i] + 3)]
    myHyo2 = myHyo2 + [myYajirusi(myHyo1[j])]
    myHyo0 = ["x"]    +myHyo0
    myHyo1 = ["f'(x)"]+myHyo1
    myHyo2 = ["f(x)"] +myHyo2
    return myHyo0,myHyo1,myHyo2
def myList(y):
    l0 = solve(y)
    ll0 = []
    for i in range(len(l0)):
        ll0 = ll0 + [l0[i]]
    return ll0
def myFugo(z, xi):
    Fugo = "-"
    if (z.subs({x: xi}) > 0):
        Fugo = "+"
    return Fugo
def myYajirusi(PlusMinus):
    Yajirusi = ""
    if (PlusMinus == "+"):
        Yajirusi = ""
    return Yajirusi
def myCsv2Markdown(Gyo):
    Md=""
    for i in range(len(Gyo)):
        Md = Md + "|" +str(Gyo[i])
    return Md + "|"
def myDefMarkdown(nCol):
    ans='|'
    for i in range(nCol):
        ans=ans+':-:|'
    return ans
def myCsv(ans):
    print("[csv形式増減表]")
    for i in range(3):
        print(",".join(map(str,ans[i])))
    return
def myCsvMarkdown(y):
    ans = myZogenhyoCsv(y)
    print("y=", y)
    print("")
    myCsv(ans)
    print("")
    print("[markdown形式増減表]")
    print(myCsv2Markdown(ans[0]))
    print(myDefMarkdown(len(ans[0])))
    print(myCsv2Markdown(ans[1]))
    print(myCsv2Markdown(ans[2]))
    return
myCsvMarkdown(2*x**3-4*x**2+2*x)
plot         (2*x**3-4*x**2+2*x)
myCsvMarkdown(3*x**4+2*x**3-6*x**2-6*x-1)
plot         (3*x**4+2*x**3-6*x**2-6*x-1)

実行結果は、次ページです。

sympyでVer0.2

# 関数の増減表作成プログラム_Markdown表 Ver0.2(20220816)
from sympy import *
var('x')
def myZogenhyoCsvXminXmax(y,*XminXmax):
    dy=diff(y, x)
    ans=myList(dy)
    n=len(ans)
    myHyo0 = [XminXmax[0][1]]
    myHyo1 = [""]
    myHyo2 = [y.subs({x:XminXmax[0][1]})]
    myHyo0 = myHyo0 + ["... "]
    myHyo1 = myHyo1 + [myFugo(dy,(XminXmax[0][1]+ans[0])/2)]
    myHyo2 = myHyo2 + [myYajirusi(myHyo1[1])]
    j=0
    for i in range(n):
        j=j+1
        myHyo0 = myHyo0 + [ans[i]]
        myHyo1 = myHyo1 + ["0"]
        myHyo2 = myHyo2 + [y.subs({x:ans[i]})]
        if i<n-1:
           j = j + 1
           myHyo0 = myHyo0 + ["..."]
           myHyo1 = myHyo1 + [myFugo(dy,(ans[i]+ans[i+1])/2)]
           myHyo2 = myHyo2 + [myYajirusi(myHyo1[j])]
    j = j + 1
    myHyo0 = myHyo0 + ["..."]
    myHyo1 = myHyo1 + [myFugo(dy,(ans[i]+XminXmax[0][2])/2)]
    myHyo2 = myHyo2 + [myYajirusi(myHyo1[j+1])]
    myHyo0 = myHyo0 + [XminXmax[0][2]]
    myHyo1 = myHyo1 + [""]
    myHyo2 = myHyo2 + [y.subs({x:XminXmax[0][2]})]
    #
    myHyo0 = ["x"]    +myHyo0
    myHyo1 = ["f'(x)"]+myHyo1
    myHyo2 = ["f(x)"] +myHyo2
    return myHyo0,myHyo1,myHyo2
def myZogenhyoCsv(y):
    dy=diff(y, x)
    ans=myList(dy)
    n=len(ans)
    myHyo0 = [".... "]
    myHyo1 = [myFugo(dy, ans[0] - 3)]
    myHyo2 = [myYajirusi(myHyo1[0])]
    j=0
    for i in range(n):
        j=j+1
        myHyo0 = myHyo0 + [ans[i]]
        myHyo1 = myHyo1 + ["0"]
        myHyo2 = myHyo2 + [y.subs({x:ans[i]})]
        if i<n-1:
           j = j + 1
           myHyo0 = myHyo0 + ["..."]
           myHyo1 = myHyo1 + [myFugo(dy,(ans[i]+ans[i+1])/2)]
           myHyo2 = myHyo2 + [myYajirusi(myHyo1[j])]
    j = j + 1
    myHyo0 = myHyo0 + ["...."]
    myHyo1 = myHyo1 + [myFugo(dy, ans[i] + 3)]
    myHyo2 = myHyo2 + [myYajirusi(myHyo1[j])]
    #
    myHyo0 = ["x"]    +myHyo0
    myHyo1 = ["f'(x)"]+myHyo1
    myHyo2 = ["f(x)"] +myHyo2
    return myHyo0,myHyo1,myHyo2
def myList(y):
    l0 = solve(y)
    ll0 = []
    for i in range(len(l0)):
        ll0 = ll0 + [l0[i]]
    return ll0
def myFugo(z, xi):
    Fugo = "-"
    if (z.subs({x: xi}) > 0):
        Fugo = "+"
    return Fugo
def myYajirusi(PlusMinus):
    Yajirusi = ""
    if (PlusMinus == "+"):
        Yajirusi = ""
    return Yajirusi
def myCsv2Markdown(Gyo):
    Md=""
    for i in range(len(Gyo)):
        Md = Md + "|" +str(Gyo[i])
    return Md + "|"
def myDefMarkdown(nCol):
    ans='|'
    for i in range(nCol):
        ans=ans+':-:|'
    return ans
def myCsv(ans):
    print("csv形式増減表")
    for i in range(3):
        print(",".join(map(str,ans[i])))
    return
def myCsvMarkdown(y,*XminXmax):
    if XminXmax==():
       ans = myZogenhyoCsv(y)
    else:
       ans = myZogenhyoCsvXminXmax(y,*XminXmax)
    myCsv(ans)
    print("")
    print("markdown形式増減表")
    print(myCsv2Markdown(ans[0]))
    print(myDefMarkdown(len(ans[0])))
    print(myCsv2Markdown(ans[1]))
    print(myCsv2Markdown(ans[2]))
    return
myCsvMarkdown(2*x**3-3*x**2-12*x+6,(x,-2,4))

参考

増減表作成アプリを探しています。 - mrrclb48z 2022/06/26 (Sun) 13:06:34 <
第2導関数まで <

予定

第2導関数を追加したい。
xの範囲を入力したい。
三角関数、log,eでテストしてみたい。

(20220808)エラーがでます。
x^4+12x^2+16x+36

(2023/07/02) バグあり。調査中。 エラーがでます。
(1+sin(θ))/(2+cos(θ))

1
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
1
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?