2017年6月3日土曜日

学習環境

ラング線形代数学(上)(S.ラング (著)、芹沢 正三 (翻訳)、ちくま学芸文庫)の3章(行列)、3(行列の乗法)、練習問題5、6、7、8、9.を取り組んでみる。


  1. AB=( 4 2 5 1 ) BA=( 2 4 4 1 )

  2. CA=( 7 14 21 7 ) AC=( 7 14 21 7 ) CB=( 14 0 7 7 ) BC=( 14 0 7 7 ) C=a I n DC=CD=aD

  3. XA=( 3 1 5 )

  4. (0,1,0) A 2 (0,0,1) A 3 n×n (0,, a i ,,0) a i =1 A i

  5. ( AB ) t =( 1 2 2 3 ) B t =( 1 1 1 0 ) A t =( 2 3 1 1 ) B t A t =( 1 2 2 3 ) ( AB ) t =( 1 11 3 1 ) B t =( 1 2 3 1 0 1 ) A t =( 2 3 1 1 1 2 ) B t A t =( 1 11 3 1 ) ( AB ) t =( 13 0 7 2 1 5 ) B t =( 1 2 3 1 1 1 0 1 5 ) A t =( 2 3 4 0 1 1 ) B t A t =( 13 0 7 2 1 5 ) ( ABC ) t = ( ( AB )C ) t = C t ( AB ) t = C t B t A t

コード(Emacs)

Python 3

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

from sympy import pprint, Matrix, randMatrix, symbols
import random
import functools

print('5.')
a = Matrix([[1, 2],
            [3, -1]])
b = Matrix([[2, 0],
            [1, 1]])

pprint(a * b)
pprint(b * a)

print('6.')
c = Matrix([[7, 0],
            [0, 7]])
for m in [c * a, a * c, c * b, b * c]:
    pprint(m)

n = 10
d = randMatrix(n, n)

a = symbols('a')
i = Matrix([[1 if i == j else 0 for j in range(n)]
            for i in range(n)])

c = a * i
print(d * c == c * d == a * d)

print('7.')
x = Matrix([[1, 0, 0]])
a = Matrix([[3, 1, 5],
            [2, 0, 1],
            [1, 1, 7]])
pprint(x * a)

print('8.')
x = Matrix([[0, 1, 0]])
a = randMatrix(3, 3)
pprint(x * a == a[1, :])

x = Matrix([[0, 0, 1]])
pprint(x * a == a[2, :])

print('9.')
m = random.randrange(1, 10)
n = random.randrange(1, 10)
l = random.randrange(1, 10)
a = randMatrix(m, n)
b = randMatrix(n, l)
c = randMatrix(l, random.randrange(1, 10))

print((a * b * c).transpose() == functools.reduce(lambda x, y: x * y,
                                                  map(lambda x: x.transpose(),
                                                      [c, b, a])))

入出力結果(Terminal, IPython)

$ ./sample5.py
5.
⎡4  2 ⎤
⎢     ⎥
⎣5  -1⎦
⎡2  4⎤
⎢    ⎥
⎣4  1⎦
6.
⎡7   14⎤
⎢      ⎥
⎣21  -7⎦
⎡7   14⎤
⎢      ⎥
⎣21  -7⎦
⎡14  0⎤
⎢     ⎥
⎣7   7⎦
⎡14  0⎤
⎢     ⎥
⎣7   7⎦
True
7.
[3  1  5]
8.
True
True
9.
True
$

0 コメント:

コメントを投稿

Comments on Google+: