2020年2月15日土曜日

学習環境

新装版 数学読本3 (松坂 和夫(著)、岩波書店)の第9章(図形と代数の交錯する世界 - 平面上のベクトル)、9.1(ベクトルとその演算)、平行四辺形の面積の問22の解答を求めてみる。



    1. S = 1 2 - 6 - 4 = 5

    2. a = - 1 , 3 - 2 , 1 = - 3 , 2 b = 3 , 6 - 2 , 1 = 1 , 5 S = 1 2 - 3 · 5 - 2 · 1 = 17 2

コード

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

print('22.')


def s(a, b, c):
    v = b - a
    w = c - a
    return Rational(abs(v[0] * w[1] - v[1] * w[0]), 2)


class MyTestCase(TestCase):
    def test1(self):
        a = Matrix([0, 0])
        b = Matrix([3, 4])
        c = Matrix([1, -2])
        self.assertEqual(s(a, b, c), 5)

    def test2(self):
        a = Matrix([2, 1])
        b = Matrix([-1, 3])
        c = Matrix([3, 6])
        self.assertEqual(s(a, b, c), Rational(17, 2))


if __name__ == "__main__":
    main()

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

% ./sample22.py -v
22.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK
%

0 コメント:

コメントを投稿