0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python でMaxPlus代数の計算法則を実装してみる

Posted at

はじめに

MaxPlus代数とは,我々が普段扱う世界とは別の線形な世界です.筆者の勉強不足により詳細についてはここで述べませんが,基本的な演算自体は簡単なのでそれを実装してみることにします.

MaxPlus代数の演算

極めて単純で,以下の二つのみです.

  1. 加法: $a\oplus b=\mathrm{max}(a,b)$
  2. 乗法: $a\otimes b = a+b$

この世界でも線形代数でおなじみの固有値,固有ベクトルを議論できるのですが,そこまでは踏み込まず,一旦実装をメインに扱います.

クラスによる実装

MaxPlus代数の数を扱えるクラス MaxPlus を実装します.

class MaxPlus():
    def __init__(self, x):
        self.x = x

    def __str__(self):
        return f"{self.x}"

    def __add__(self, other):
        return MaxPlus(max(self.x, other.x))

    def __mul__(self, other):
        return MaxPlus(self.x + other.x)

可視化用の __str__ の他,足し算と掛け算を実装しています.

数値実験

MaxPlusの世界では,次の演算が成り立ちます.

$$
\begin{align}
5\oplus 2 &= \mathrm{max}(5,2) = 5 \\
5\otimes 2 &= 5+2 = 7
\end{align}
$$

この演算が成立しているか確認します.

a = MaxPlus(5)
b = MaxPlus(2)

print(a+b)
# >>> 5

print(a*b)
# >>> 7

たしかにMaxPlusの演算ができています!!

最近まで筆者はMaxPlus代数を知らなかったのですが,別の世界の線形代数を学ぶことで,線形代数の基礎的な枠組みの理解が深まりました.

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?