Help us shape the future of Go! Take the 2026 Go Developer Survey

mtx

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package mtx reads and writes the Matrix Market exchange format (https://math.nist.gov/MatrixMarket/formats.html): coordinate and array layouts; real, integer, pattern and complex fields; general, symmetric, skew-symmetric and hermitian symmetry. Gzip-compressed input is detected by its magic bytes. Equivalents: scipy.io.mmread / mmwrite, MATLAB's mmread.m.

Index

Constants

View Source
const (
	Coordinate Layout = "coordinate"
	Array      Layout = "array"

	Real    Field = "real"
	Integer Field = "integer"
	Pattern Field = "pattern"
	Complex Field = "complex"

	General       Symmetry = "general"
	Symmetric     Symmetry = "symmetric"
	SkewSymmetric Symmetry = "skew-symmetric"
	Hermitian     Symmetry = "hermitian"
)

Constants naming the Matrix Market vocabulary.

Variables

View Source
var DefaultLimits = Limits{MaxDim: 1 << 27, MaxEntries: 1 << 28, MaxLine: 1 << 16, MaxBytes: 1 << 34, MaxComments: 1 << 12}

DefaultLimits are generous for real matrices (2.7e8 entries) and refuse absurd headers.

View Source
var ErrFormat = errors.New("mtx: malformed file")

ErrFormat reports a malformed Matrix Market file.

Functions

func Write

func Write[T sparse.Scalar](w io.Writer, a *sparse.CSR[T], o Options) error

Write stores a in Matrix Market format. With a symmetry other than General, the matrix must have that symmetry exactly.

Types

type Field

type Field string

Field is the type of the stored values.

type Header struct {
	Layout   Layout
	Field    Field
	Symmetry Symmetry
	Rows     int
	Cols     int
	Entries  int // stored entries (coordinate) or stored values (array)
	Comments []string
}

Header is the information of the banner and size lines.

type Layout

type Layout string

Layout is the storage layout of a Matrix Market file.

type Limits

type Limits struct {
	MaxDim      int   // rows and columns
	MaxEntries  int   // declared coordinate entries; rows*cols of an array file
	MaxLine     int   // bytes in one line
	MaxBytes    int64 // bytes read (after decompression)
	MaxComments int   // comment lines kept in Header.Comments
}

Limits bound what Read accepts from an untrusted stream. A zero field takes its DefaultLimits value. Nothing is allocated from a declared size before the data that backs it has been read, but the Matrix is later converted with O(Rows+Cols) arrays, hence MaxDim.

type Matrix

type Matrix struct {
	Header
	Real    *sparse.COO[float64]
	Complex *sparse.COO[complex128]
}

Matrix is a file read by Read. Exactly one of Real and Complex is set: Complex for the complex field, Real otherwise (pattern entries are 1). Symmetry is expanded: the COO matrices hold the full matrix, and Header.Symmetry records what the file declared.

func Read

func Read(r io.Reader) (*Matrix, error)

Read parses a Matrix Market stream with DefaultLimits.

func ReadFile

func ReadFile(path string) (*Matrix, error)

ReadFile reads a Matrix Market file (optionally gzip-compressed).

func ReadLimited

func ReadLimited(r io.Reader, lim Limits) (*Matrix, error)

ReadLimited parses a Matrix Market stream, refusing input beyond lim.

func (*Matrix) ComplexCSR

func (m *Matrix) ComplexCSR() *sparse.CSR[complex128]

ComplexCSR returns the matrix as a complex CSR matrix (real files are widened).

func (*Matrix) RealCSR

func (m *Matrix) RealCSR() (*sparse.CSR[float64], error)

RealCSR returns the matrix as a real CSR matrix; it fails for complex files.

type Options

type Options struct {
	Layout   Layout   // Coordinate (default) or Array
	Field    Field    // default: real for float64 data, complex for complex128
	Symmetry Symmetry // default General; others write only the lower triangle
	Comments []string
}

Options controls Write. The zero value writes a general coordinate file whose field is real (float64) or complex (complex128).

type Symmetry

type Symmetry string

Symmetry is the symmetry structure of the stored matrix.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL