This is a pre-release version (2.0.0.dev0+git20260907.41eeb59).Go to latest (1.18.0)
{ } Raw JSON

bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / sparse / _construct / kronsum

function

scipy.sparse._construct:kronsum

source: build-install/usr/lib/python3.14/site-packages/scipy/sparse/_construct.py:929

Signature

def kronsum(A, B, format = None)

Summary

Kronecker sum of square sparse matrices A and B.

Extended Summary

Kronecker sum of two sparse matrices is a sum of two Kronecker products kron(I_n,A) + kron(B,I_m) where A has shape (m, m) and B has shape (n, n) and I_m and I_n are identity matrices of shape (m, m) and (n, n), respectively.

Parameters

A : sparse matrix or array

Square matrix

B : sparse array or array

Square matrix

format : str

format of the result (e.g. "csr")

Returns

: sparse matrix or array

kronecker sum in a sparse format. Returns a sparse matrix unless either A or B is a sparse array in which case returns a sparse array.

Examples

`kronsum` can be used to construct a finite difference discretization of the 2D Laplacian from a 1D discretization.
from scipy.sparse import diags_array, kronsum
from matplotlib import pyplot as plt
import numpy as np
ex = np.ones(10)
D_x = diags_array([ex, -ex[1:]], offsets=[0, -1])  # 1D first derivative
D_xx = D_x.T @ D_x  # 1D second derivative
L = kronsum(D_xx, D_xx)  # 2D Laplacian
✓
plt.spy(L.toarray())
✗
<matplotlib.image.AxesImage object at 0x7f3f60b63890>
plt.show()
✓
fig-3434b044f0843cdd.png

Aliases

  • scipy.sparse.kronsum

Referenced by