Module: NMatrix::IO::Market

Defined in:
lib/nmatrix/io/market.rb

Overview

Matrix Market is a repository of test data for use in studies of algorithms for numerical linear algebra. There are 3 file formats used:

  • Matrix Market Exchange Format.

  • Harwell-Boeing Exchange Format.

  • Coordinate Text File Format. (to be phased out)

This module can load and save the first format. We might support Harwell-Boeing in the future.

The MatrixMarket format is documented in:

Constant Summary collapse

CONVERTER_AND_DTYPE =
{
  :real => [:to_f, :float64],
  :complex => [:to_c, :complex128],
  :integer => [:to_i, :int64],
  :pattern => [:to_i, :byte]
}
ENTRY_TYPE =

:nodoc:

{
  :byte => :integer, :int8 => :integer, :int16 => :integer, :int32 => :integer, :int64 => :integer,
  :float32 => :real, :float64 => :real, :complex64 => :complex, :complex128 => :complex
}

Class Method Summary collapse

Class Method Details

.load(filename) ⇒ Object

call-seq:

load(filename) -> NMatrix

Load a MatrixMarket file. Requires a filename as an argument.

  • Arguments :

    • filename -> String with the filename to be saved.

  • Raises :

    • IOError -> expected type code line beginning with ‘%%MatrixMarket matrix’

Raises:

  • (IOError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nmatrix/io/market.rb', line 65

def load(filename)

  f = File.new(filename, "r")

  header = f.gets
  header.chomp!
  raise(IOError, "expected type code line beginning with '%%MatrixMarket matrix'") if header !~ /^\%\%MatrixMarket\ matrix/

  header = header.split

  entry_type = header[3].downcase.to_sym
  symmetry   = header[4].downcase.to_sym
  converter, default_dtype = CONVERTER_AND_DTYPE[entry_type]

  if header[2] == 'coordinate'
    load_coordinate f, converter, default_dtype, entry_type, symmetry
  else
    load_array f, converter, default_dtype, entry_type, symmetry
  end
end

.save(matrix, filename, options = {}) ⇒ Object

call-seq:

save(matrix, filename, options = {}) -> true

Can optionally set :symmetry to :general, :symmetric, :hermitian; and can set :pattern => true if you’re writing a sparse matrix and don’t want values stored.

  • Arguments :

    • matrix -> NMatrix with the data to be saved.

    • filename -> String with the filename to be saved.

  • Raises :

    • DataTypeError -> MatrixMarket does not support Ruby objects.

    • ArgumentError -> Expected two-dimensional NMatrix.

Raises:

  • (ArgumentError)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nmatrix/io/market.rb', line 99

def save(matrix, filename, options = {})
  options = {:pattern => false,
    :symmetry => :general}.merge(options)

  mode = matrix.stype == :dense ? :array : :coordinate
  if [:object].include?(matrix.dtype)
    raise(DataTypeError, "MatrixMarket does not support Ruby objects")
  end
  entry_type = options[:pattern] ? :pattern : ENTRY_TYPE[matrix.dtype]

  raise(ArgumentError, "expected two-dimensional NMatrix") if matrix.dim != 2

  f = File.new(filename, 'w')

  f.puts "%%MatrixMarket matrix #{mode} #{entry_type} #{options[:symmetry]}"

  if matrix.stype == :dense
    save_array matrix, f, options[:symmetry]
  elsif [:list,:yale].include?(matrix.stype)
    save_coordinate matrix, f, options[:symmetry], options[:pattern]
  end

  f.close

  true
end