2016年4月8日金曜日

開発環境

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 #3: Area Between Two Curves, (No. 4942)を取り組んでみる。

Programming Challenges #3: Area Between Two Curves, (No. 4942)

コード(Emacs)

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

import sympy

print('Enter two single-variable functions of x')
f = input()
g = input()
try:
    f = sympy.sympify(f)
    g = sympy.sympify(g)
except sympy.SympifyError as err:
    print('Invalid function entered: {0}'.format(err))
else:
    expr = f - g
    a = sympy.solve(expr)
    x = sympy.Symbol('x')
    print('area: {0}'.format(
        abs(sympy.Integral(f - g, (x, a[0], a[1])).doit())))

入出力結果(Terminal, IPython)

$ ./sample3.py
Enter two single-variable functions of x
x ** 2
x
area: 1/6
$ ./sample3.py
Enter two single-variable functions of x
x
x ** 2
area: 1/6
$

0 コメント:

コメントを投稿