2019年8月27日火曜日

学習環境

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



    1. a

    2. a 4 3

    3. 1.75 = 175 100 = 7 4 a 3 4 = a 3 4

    4. a - 5 4

コード

Python 3

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

print('6.')


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

    def tearDown(self):
        pass

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

    def test_int(self):
        spam = [Rational(1, 2), Rational(4, 3), 1.75, -Rational(5, 4)]
        egg = [(2, 1), (3, 4), (4, 7), (4, -5)]
        for _ in range(10):
            a = random.randrange(1, 101)
            for s, (t, u) in zip(spam, egg):
                self.assertEqual(float((a ** s)), float(root(a ** u, t)))

    def test_int_almost(self):
        spam = [Rational(1, 2), Rational(4, 3), 1.75, -Rational(5, 4)]
        egg = [(2, 1), (3, 4), (4, 7), (4, -5)]
        for _ in range(10):
            a = random.randrange(1, 101)
            for s, (t, u) in zip(spam, egg):
                self.assertAlmostEqual(float((a ** s)), float(root(a ** u, t)))


if __name__ == '__main__':
    main()

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

C:\Users\...>py sample6.py
6.
test_int (__main__.MyTestCase) ... FAIL
test_int_almost (__main__.MyTestCase) ... ok
test_symbol (__main__.MyTestCase) ... FAIL

======================================================================
FAIL: test_int (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample6.py", line 29, in test_int
    self.assertEqual(float((a ** s)), float(root(a ** u, t)))
AssertionError: 0.012180114016663296 != 0.012180114016663295

======================================================================
FAIL: test_symbol (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./sample6.py", line 21, in test_symbol
    self.assertEqual(simplify((a ** s)), simplify(root(a ** u, t)))
AssertionError: a**(4/3) != (a**4)**(1/3)

----------------------------------------------------------------------
Ran 3 tests in 0.251s

FAILED (failures=2)

C:\Users\...>

0 コメント:

コメントを投稿