GithubHelp home page GithubHelp logo

ograsdijk / eigenshuffle Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 376 KB

Keep order of eigenvalues and eigenvectors of a series of matrices based on initial ordering

License: MIT License

Python 100.00%

eigenshuffle's Introduction

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.float_]:
    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.

eigenshuffle's People

Contributors

ograsdijk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.