File size: 1,168 Bytes
ad91e86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Row-normalized state transitions and fixed-target identity dynamics."""

from __future__ import annotations

import numpy as np


class IdentityTransition:
    dynamic = False

    def matrix(self, states: list[int], elapsed_s: float) -> np.ndarray:
        return np.eye(len(states), dtype=float)


class MatrixTransition:
    """A supplied row-stochastic transition, useful for deterministic integration tests."""

    dynamic = True

    def __init__(self, matrix: np.ndarray) -> None:
        self.values = np.asarray(matrix, dtype=float)
        if self.values.ndim != 2 or self.values.shape[0] != self.values.shape[1]:
            raise ValueError("transition must be square")
        if np.any(self.values < 0) or not np.allclose(self.values.sum(axis=1), 1):
            raise ValueError("transition rows must sum to one")

    def matrix(self, states: list[int], elapsed_s: float) -> np.ndarray:
        if elapsed_s < 0:
            raise ValueError("time cannot go backwards")
        if len(states) != len(self.values):
            raise ValueError("transition state count mismatch")
        return np.eye(len(states)) if elapsed_s == 0 else self.values