2020年6月17日水曜日

学習環境

続 解析入門 (原書第2版) (S.ラング(著)、松坂 和夫(翻訳)、片山 孝次(翻訳)、岩波書店)の第1章(ベクトル)、5(パラメーター表示された直線)の練習問題4の解答を求めてみる。



    1. (1,3,-1)+12(-5,2,3)=(-32,4,12)

    2. (1,3,-1)+13(-5,2,3)=(-23,113,0)(1,3,-1)+23(-5,2,3)=(-73,133,1)

    3. (1,3,-1)+15(-5,2,3)=(0,175,-25)

    4. (1,3,-1)+25(-5,2,3)=(-1,195,15)

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Matrix, Rational
from sympy.abc import t

print('4.')

p = Matrix([1, 3, -1])
q = Matrix([-4, 5, 2])
qp = q - p
l = p + t * qp


class Test(TestCase):
    def test_a(self):
        self.assertEqual(
            l.subs({t: Rational(1, 2)}),
            Matrix([-Rational(3, 2), 4, Rational(1, 2)])
        )

    def test_b(self):
        self.assertEqual(
            l.subs({t: Rational(1, 3)}),
            Matrix([-Rational(2, 3), Rational(11, 3), 0])
        )
        self.assertEqual(
            l.subs({t: Rational(2, 3)}),
            Matrix([-Rational(7, 3), Rational(13, 3), 1])
        )

    def test_c(self):
        self.assertEqual(
            l.subs({t: Rational(1, 5)}),
            Matrix([0, Rational(17, 5), -Rational(2, 5)])
        )

    def test_d(self):
        self.assertEqual(
            l.subs({t: Rational(2, 5)}),
            Matrix([-1, Rational(19, 5), Rational(1, 5)])
        )


if __name__ == "__main__":
    main()

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% ./sample4.py -v
4.
test_a (__main__.Test) ... ok
test_b (__main__.Test) ... ok
test_c (__main__.Test) ... ok
test_d (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.005s

OK
%

0 コメント:

コメントを投稿