Logo

Tests Coverage Status

GitHub PyPi libraries.io

PyKronecker is a library for manipulating matrices which have a Kronecker product structure. Systems involving Kronecker products arise in many areas of applied mathematics and statistics. The aim of this library is to provide a clean interface for dealing with such systems, combining lazy evaluation and algebraic tricks to deliver large savings in terms of both memory and execution time.

Quickstart

Create a KroneckerProduct operator from two or more square arrays. These may be real or complex-valued.

import numpy as np
from pykronecker import KroneckerProduct

Ns = [30, 40, 50]
As = [np.random.normal(size=(Ni, Ni)) for Ni in Ns]

A = KroneckerProduct(As)

This object can be multiplied onto a vector of shape (30 * 40 * 50, ) or a tensor of shape (30, 40, 50) using the @ syntax for matrix multiplication. The returned array will be the same size as the input array.

X = np.random.normal(size=(30 * 40 * 50))
x = np.random.normal(size=(30, 40, 50))

print(A @ X, A @ x)

A KroneckerSum operator can be created and used in much the same way.

from pykronecker import KroneckerSum

B = KroneckerSum(As)

print(A @ X, A @ x)

These objects can be added, scaled, and transposed arbitrarily to create new composite operators.

C = 2 * A - 0.5 * B.T

print(C @ X, C @ x)