Skip to main content

Keep consistent order of eigenvalues and eigenvectors of successive eigenvalue/vector problems based on the inital ordering of eigenvalues from low to high by matching closest eigenvectors and eigenvalues.

Project description

Python versions on PyPI CeNTREX-TlF version on PyPI Code style: black

eigenshuffle

Adapted from code by bmachiel, which in turn was based on matlab eigenshuffle.

Consistently sort eigenvalues and eigenvectors of a series of matrices based on initial ordering from low to high.

Includes eigenshuffle_eig and eigenshuffle_eigh for non-hermitian and hermitian matrices, respectively.

Installation

Install from pypi with:

pip install eigenshuffle

or clone repo and install with pip or directly install from GitHub with:

pip install git+https://github.com/ograsdijk/eigenshuffle
pip install git+https://github.com/ograsdijk/eigenshuffle

Example

import numpy as np
import numpy.typing as npt
import matplotlib.pyplot as plt

from eigenshuffle import eigenshuffle_eig

def eigenvalue_function(
    t: float,
) -> npt.NDArray[np.floating]:
    return np.array(
        [
            [1, 2 * t + 1, t**2, t**3],
            [2 * t + 1, 2 - t, t**2, 1 - t**3],
            [t**2, t**2, 3 - 2 * t, t**2],
            [t**3, 1 - t**3, t**2, 4 - 3 * t],
        ]
    )

tseq = np.arange(-1, 1.1, 0.1)
Aseq = np.array([eigenvalue_function(ti) for ti in tseq])

e, v = np.linalg.eig(Aseq)

es, vs = eigenshuffle_eig(Aseq)

# sorting original eig result from low to high
v[np.argsort(e)]
e = np.sort(e)

fig, ax = plt.subplots()
lines = ax.plot(tseq, e)

for i in range(ei.shape[-1]):
    ax.plot(tseq, ei.real[:, i], "--", color=lines[i].get_color())

# for generating the legend
line1 = plt.Line2D([0, 1], [0, 1], linestyle="-", color="black")
line2 = plt.Line2D([0, 1], [0, 1], linestyle="--", color="black")

ax.set_xlabel("t")
ax.set_ylabel("eigenvalue")
ax.legend([line1, line2], ["sorted", "eigenshuffle"])
ax.grid()

consistenly sorted eigenvalues
Here the eigenvalues are consistently ordered, and are not switching positions after a level crossing (around t=0.3) when using eigenshuffle.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eigenshuffle-0.1.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eigenshuffle-0.1.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file eigenshuffle-0.1.2.tar.gz.

File metadata

  • Download URL: eigenshuffle-0.1.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Windows/11

File hashes

Hashes for eigenshuffle-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e2d8a2c12e5e73e1ec1dfab1394f66678f802ab81f7983075943699cb4cc657c
MD5 7784c9418b7e4c9c4a0e6f80ff6d81cb
BLAKE2b-256 c807d322fb9629b589d3b49ec69ec59ec8b446c19ad7ef7d149cbf923ecccc6f

See more details on using hashes here.

File details

Details for the file eigenshuffle-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: eigenshuffle-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Windows/11

File hashes

Hashes for eigenshuffle-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e37788131f9703619deb623b91d90d6e70054d6c254aa2096ec9d15121040b6f
MD5 a8a1f95e2b8dedb46536882d4a1b5b6c
BLAKE2b-256 5c1a4d43a3559807a1f473494adf9b1152eb0a69e0ab32ebc0c2931610407f9f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page