2016年4月6日水曜日

開発環境

Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (Amit Saha (著)、No Starch Press)のChapter 7.(Solving Calculus Problems)、Programming Challenges #1: Verify the Continuity of a Function at Point, (No. 4919)を取り組んでみる。

Programming Challenges #1: Verify the Continuity of a Function at Point, (No. 4919)

コード(Emacs)

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

import sympy

expr = input('Enter a function in one variable: ')
var = input('enter the variable: ')
point = float(input('enter the point to check the continuity at: '))

expr = sympy.sympify(expr)
var = sympy.Symbol(var)

l_plus = sympy.Limit(expr, var, point)
l_minus = sympy.Limit(expr, var, point, dir='-')
if l_plus.doit() == l_minus.doit():
    print('{0} is continuous at {1}'.format(expr, point))
else:
    print('{0} is not continuous at {1}'.format(expr, point))

入出力結果(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 コメント:

コメントを投稿