Mahesh Joshi > Math-SparseMatrix-0.01 > Math::SparseMatrix



Annotate this POD

View/Report Bugs
Module Version: 0.01   Source   Latest Release: Math-SparseMatrix-0.03


Math::SparseMatrix - A Perl module for basic sparse matrix operations such as creation, reading from file, reading transpose from file and writing to file.


  # include this module for use in your program
  use Math::SparseMatrix;
  # create a new sparse matrix with 10 rows and 15 columns
  my $spmatrix = Math::SparseMatrix->new(10, 15);

  # set the value at row 5, column 3 to 10
  $spmatrix->set(5, 3, 10);

  # get the value at row 6, column 5 if present, or zero
  $val = $spmatrix->get(6, 5);

  # write out the sparse matrix to the file "matrix.txt"

  # create a matrix object by reading the file "matrix.txt"
  my $spmatrix = Math::SparseMatrix->createFromFile("matrix.txt");

  # create the transpose of the matrix stored in "matrix.txt"
  my $spmatrix = Math::SparseMatrix->createTransposeFromFile("matrix.txt");
  # write out the created transpose to another file "transpose.txt"


Math::SparseMatrix module provides simple sparse matrix functionality such as creation of sparse matrices, writing them out to a file, reading matrices from files and reading transpose of a matrix stored in a file.

The sparse matrix file format that Math::SparseMatrix expects is described below with an example.

The first line (or the header line) of the file should contain 3 number separated by a single space. The first number is the number of rows in the sparse matrix, the second number is the number of columns and the third number is the number of non-zero elements present in the stored matrix.

Each subsequent line represents one row of the sparse matrix, therefore there should be as many number of lines after the header line as the number of rows mentioned in the header line. In every line representing a row, there should be as many pairs of numbers as the number of non-zero elements in that row. The first number in the pair represents the column number of the non-zero element (column numbers start with 1). The row number is implicitly provided by the line number in the file. The second number in the pair is the actual non-zero matrix element. Numbers in a pair and multiple pairs should all be separated by single spaces. If a row does not contain any non-zero element, then an empty line should be present in the file.

NOTE: There should be no empty lines except those representing empty rows, neither should there be any comment lines. Commenting is not supported.

Consider the sparse matrix of 5 rows and 4 columns below:

  10    0    0    0
   0    0    6    8
   0    0    0    0
   0   21    0    0
   7    0    0    9

The sparse file representation for the same is:

  5 4 6
  1 10
  3 6 4 8

  2 21
  1 7 4 9

Notice the empty line in between for the third row.




Ted Pedersen, <>

Mahesh Joshi, <>


Copyright (c) 2006,

Ted Pedersen, University of Minnesota, Duluth.

Mahesh Joshi, University of Minnesota, Duluth.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to

The Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

syntax highlighting: