EinSum: Matrix projection

Perform matrix projection using the generic EinSum module

This example demonstrates how to use the pymoto.EinSum module to perform a matrix projection operation, specifically calculating \(\mathbf{A}_p = \mathbf{V}^T \mathbf{A} \mathbf{V}\), where \(\mathbf{A}\) is a square matrix and \(\mathbf{V}\) is a block-vector. This is useful for generation of reduced model with Galerkin projection.

12 import pymoto as pym
13 import numpy as np
14
15 if __name__ == "__main__":
16     print(__doc__)
17
18     # --- INITIALIZATION ---
19
20     A = pym.Signal("A")
21     V = pym.Signal("V")
22
23     N = 5  # Size
24     M = 2
25     A.state = np.random.rand(N, N)
26     V.state = np.random.rand(N, M)
27     print(f"{A.tag} = {A.state}")
28     print(f"{V.tag} = {V.state}")
29
30     # --- FORWARD ANALYSIS ---
31     with pym.Network() as fn:
32         # Matrix projection V^T A V
33         Ap = pym.EinSum("ji, jk, kl -> il")(V, A, V)
34         Ap.tag = "A_p"
35
36     print(f"\nThe response is {Ap.tag} =\n {Ap.state}\n")
37
38     # --- BACKPROPAGATION ---
39     dgdAp = np.ones_like(Ap.state)  # Seed the output sensitivity
40     Ap.sensitivity = dgdAp
41     fn.sensitivity()  # Run the backpropagation
42     print("\nThe sensitivities are:")
43     print(f"dg/d{A.tag} = \n{A.sensitivity}")
44     print(f"dg/d{V.tag} = \n{V.sensitivity}")
45
46     # --- Finite difference check ---
47     pym.finite_difference([A, V], Ap)

Gallery generated by Sphinx-Gallery