2019年8月26日月曜日

学習環境

新装版 数学読本2 (松坂 和夫(著)、岩波書店)の第7章(急速・緩慢に変化する関係 - 指数関数・対数関数)、7.1(指数の拡張)、指数の拡張(1)の問5の解答を求めてみる。



    1. a 3 2

    2. a 2 3

    3. a 5 3

    4. a - 2 5

コード

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, sqrt, root, Rational
from unittest import TestCase, main

print('5.')


class MyTestCase(TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_symbol(self):
        a = symbols('a')
        spam = [sqrt(a ** 3, 2), root(a ** 2, 3),
                root(a ** 5, 3), root(1 / a ** 2, 5)]
        egg = [a ** Rational(3, 2), a ** Rational(2, 3), a **
               Rational(5, 3), a ** -Rational(2, 5)]
        for t, s in zip(spam, egg):
            self.assertEqual(t, s)

    def test_int(self):
        a = 2
        spam = [sqrt(a ** 3, 2), root(a ** 2, 3),
                root(a ** 5, 3), root(1 / a ** 2, 5)]
        egg = [a ** Rational(3, 2), a ** Rational(2, 3), a **
               Rational(5, 3), a ** -Rational(2, 5)]
        for t, s in zip(spam, egg):
            self.assertEqual(float(t), float(s))


if __name__ == '__main__':
    main()

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))

C:\Users\...>py sample5.py
5.
.F
======================================================================
FAIL: test_symbol (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 22, in test_symbol
    self.assertEqual(t, s)
AssertionError: sqrt(a**3) != a**(3/2)

----------------------------------------------------------------------
Ran 2 tests in 0.017s

FAILED (failures=1)

C:\Users\...>py sample5.py
5.
test_int (__main__.MyTestCase) ... ok
test_symbol (__main__.MyTestCase) ... FAIL

======================================================================
FAIL: test_symbol (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample5.py", line 22, in test_symbol
    self.assertEqual(t, s)
AssertionError: sqrt(a**3) != a**(3/2)

----------------------------------------------------------------------
Ran 2 tests in 0.016s

FAILED (failures=1)

C:\Users\...>

Symbolでは上手くいかないみたい。

0 コメント:

コメントを投稿