Skip to main content

A Python package for solving ordinary differential equations evolving on non-linear manifolds

Project description

PyLie

A Python package for solving ordinary differential equations evolving on non-linear manifolds.

This package is distributed with the Python package index. To install it, use

$ pip install pylie

In order to solve an ODE, the differential must first be described in its canonical Lie form – that is, as a mapping from the manifold to the corresponding Lie algebra. For examples, please see below.

Example: Equation evolving on the unit sphere

The complete code is listed at the bottom of this section if you want to copy-paste it, including a definition of A(t, y).

The unit sphere has Lie algebra so(3), consisting of 3-by-3 skew-symmetric matrices (i.e. matrices which satisfy the equation transpose(A) = -A). Ordinary differential equations where the solution space is the unit sphere may be formulated in the form

dy / dt = A(t, y) · y

where A is a skew-symmetric matrix. In order to solve the above equation, you must define the function

def A(t, y):
    # return a 3-by-3 skew-symmetric matrix of type np.ndarray

For instance:

import numpy as np


def A(t, y):
    return np.array(
            [
                [0,                  t,           -0.4 * np.cos(t)],
                [-t,                 0,                0.1 * t    ],
                [0.4 * np.cos(t), -0.1 * t,                0      ]
            ]
        )

You must also decide which numerical scheme you would like to use to solve the equation. Higher-order methods provide a more accurate solution, but are more computationally expensive. For a list of available methods, see available numerical schemes. In this example, we will use the Lie group method corresponding to the fourth order Runge-Kutta method.

To solve the problem, we use the following code:

import numpy as np
import pylie

### Code defining or importing A(t, y) ###

y0 = [0.0, 0.0, 1.0]
t_start = 0
t_end = 5
step_length = 0.1
manifold = "hmnsphere"
method = "RKMK4"
solution = pylie.solve(A, y0, t_start, t_end, step_length, manifold, method)

The variable solution is now a Flow object with two attributes: T, a one-dimensional numpy array containing the times at which the solution is computed, Y, a 3-by-n numpy array where column Y[i, :] is the solution at time T[i]. It is also possible to use indexing directly on the object: solution[i, j] is equivalent to solution.Y[i, j]. If you wish you may also extract the variables Y and T directly by using

# If solution is not yet computed:
Y, T = pylie.solve(A, y0, t_start, t_end, step_length, manifold, method)

# Or, if you followed the example above
Y, T = solution

The following is a suggestion in order to plot the solution:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.plot(solution[0, :], solution[1, :], solution[2, :])
plt.show()

Full example

This file is also avaiable in /docs/unit_sphere_example.py.

from numpy.testing import assert_almost_equal
import pylie
import numpy as np
import matplotlib.pyplot as plt


def A(t, y):
    return np.array(
        [[0, t, -0.4 * np.cos(t)], [-t, 0, 0.1 * t], [0.4 * np.cos(t), -0.1 * t, 0]]
    )


if __name__ == "__main__":
    y0 = [0.0, 0.0, 1.0]
    t_start = 0
    t_end = 5
    step_length = 0.01
    manifold = "hmnsphere"
    method = "RKMK4"
    solution = pylie.solve(A, y0, t_start, t_end, step_length, manifold, method)

    # Verify that the solution is indeed on the unit sphere
    solution_norm = [np.linalg.norm(solution[:, i]) for i in range(len(solution.T))]
    for val in solution_norm:
        assert_almost_equal(val, 1.0)
    print("Passed test, plotting ...")

    fig = plt.figure()
    ax = fig.add_subplot(projection="3d")
    ax.plot(solution[0, :], solution[1, :], solution[2, :])
    plt.show()

Example: The heavy top

A more detailed explanation is in progress. Until then, the code for the full example is avaible below and in the file /docs/heavy_top_example.py.

from numpy.testing import assert_almost_equal
import pylie
import numpy as np
import matplotlib.pyplot as plt


def heavy_top(
    t, y, principal_moments=np.array([2, 2, 1]), m=1, g=1, chi=np.array([0, 0, 1])
):
    """A formulation of the problem exploiting the Lie-group structure"""
    mu, beta = np.split(y, 2)
    mu_dot = -mu / principal_moments
    beta_dot = -m * g * chi
    return np.hstack((mu_dot, beta_dot))


if __name__ == "__main__":
    y0 = np.array([np.sin(1.1), 0, np.cos(1.1), 1, 0.2, 3])
    t_start = 0
    t_end = 5
    step_length = 0.01
    manifold = "heavytop"
    method = "RKMK4"
    solution = pylie.solve(heavy_top, y0, t_start, t_end, step_length, manifold, method)

    # Verify that the solution is indeed on the manifold
    expexted_norm = np.linalg.norm(y0[3:])
    solution_norm = [np.linalg.norm(solution[3:, i]) for i in range(len(solution.T))]
    for val in solution_norm:
        assert_almost_equal(val, expexted_norm)
    print("Passed test, plotting ...")

    fig = plt.figure()
    ax = fig.add_subplot(projection="3d")
    ax.plot(solution[0, :], solution[1, :], solution[2, :])
    plt.show()

Available numerical schemes

  • "E1": Explicit Euler, 1st order
  • "RKMK4": Runge-Kutta Munthe-Kaas 4, 4th order

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

pylie-0.3.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

pylie-0.3.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file pylie-0.3.0.tar.gz.

File metadata

  • Download URL: pylie-0.3.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.4

File hashes

Hashes for pylie-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cbf87afacaf29af5494ab45fada6f430fd66f22d9bf7def630bc5acf9037e7de
MD5 4c474dbba876d31029e99a7f1354f054
BLAKE2b-256 04af68f83578797180752a9bdc9a9ac2badf703fc9357468d6367143e0e56642

See more details on using hashes here.

File details

Details for the file pylie-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pylie-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.4

File hashes

Hashes for pylie-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7318b442de530afe66ded7a296125978fac850611508bdfb0436050c5769533d
MD5 4d85fd60d165e562e6c0605cd8767612
BLAKE2b-256 29d4c30d506989c9d21fd3db5f7c33736348adc2c93c0d83941b54f970b759bf

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