2016年9月6日火曜日

開発環境

Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の7章(初等解析問題を解く)、7.10(プログラミングチャレンジ)、問題7-1(ある点での関数の連続性を検証する)を取り組んでみる。

問題7-1(ある点での関数の連続性を検証する)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sympy


def is_continuous(f, x, n):
    limit1 = sympy.Limit(f, x, n, dir='-').doit()
    limit2 = sympy.Limit(f, x, n, dir='+').doit()
    y = f.subs({x: n})
    return limit1 == limit2 == y

if __name__ == '__main__':
    f = input('Enter a function in one variable: ')
    try:
        f = sympy.sympify(f)
        x = input('Enter the variable: ')
        n = float(input('Enter the point to check the continuity at: '))
        x = sympy.Symbol(x)
    except sympy.SympifyError as err:
        print(err)
    except Exception as err:
        print(err)
    else:
        if is_continuous(f, x, n):
            sympy.pprint('{0} is continuous at {1}'.format(f, n))
        else:
            sympy.pprint('{0} is not continuous at {1}'.format(f, n))

入出力結果(Terminal, IPython)

$ ./sample1.py
Enter a function in one variable: 1/x
Enter the variable: x
Enter the point to check the continuity at: 1
1/x is continuous at 1.0
$ ./sample1.py
Enter a function in one variable: 1/x
Enter the variable: x
Enter the point to check the continuity at: 0
1/x is not continuous at 0.0
$ 

0 コメント:

コメントを投稿