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 / _dok / dok_matrix

ABCMeta

scipy.sparse._dok:dok_matrix

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

Signature

def dok_matrix(*args, **kwargs)

Members

Summary

Dictionary Of Keys based sparse matrix.

Extended Summary

This is an efficient structure for constructing sparse matrices incrementally.

This can be instantiated in several ways:

dok_matrix(D)

where D is a 2-D ndarray

dok_matrix(S)

with another sparse array or matrix S (equivalent to S.todok())

dok_matrix((M,N), [dtype])

create the matrix with initial shape (M,N) dtype is optional, defaulting to dtype='d'

Attributes

dtype : dtype

Data type of the matrix

shape : 2-tuple

Shape of the matrix

ndim : int

Number of dimensions (this is always 2)

format : str

Three letter code for the format of the matrix storage, e.g. 'dok'

nnz : int

Number of values stored in the matrix

size : int

Number of values stored in the matrix

T : dok_matrix

The transpose of the matrix

mT : dok_matrix

The matrix transpose

Notes

Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power.

  • Allows for efficient O(1) access of individual elements.

  • Duplicates are not allowed.

  • Can be efficiently converted to a coo_matrix once constructed.

Examples

import numpy as np
from scipy.sparse import dok_matrix
S = dok_matrix((5, 5), dtype=np.float32)
for i in range(5):
    for j in range(5):
        S[i, j] = i + j    # Update element
✓

Aliases

  • scipy.sparse.dok_matrix

Referenced by