class NMatrix::IO::HarwellBoeing::Reader

Public Class Methods

new(file_name) click to toggle source
# File lib/nmatrix/io/harwell_boeing.rb, line 80
def initialize file_name
  raise(IOError, "Unsupported file format. Specify file as \
    file_name.rua.") if !file_name.match(/.*\.[rR][uU][aA]/)

  @file_name   = file_name
  @header      = {}
  @body        = nil
end

Public Instance Methods

header() click to toggle source
# File lib/nmatrix/io/harwell_boeing.rb, line 89
def header
  return @header if !@header.empty?
  @file = File.open @file_name, "r"

  line = @file.gets

  @header[:title] = line[0...72].strip
  @header[:key]   = line[72...80].strip

  line = @file.gets

  @header[:totcrd] = line[0...14] .strip.to_i
  @header[:ptrcrd] = line[14...28].strip.to_i
  @header[:indcrd] = line[28...42].strip.to_i
  @header[:valcrd] = line[42...56].strip.to_i
  @header[:rhscrd] = line[56...70].strip.to_i

  raise(IOError, "Right hand sides not supported.") if @header[:rhscrd] > 0

  line = @file.gets

  @header[:mxtype] = line[0...3]

  raise(IOError, "Currently supports only real, assembled, unsymmetric \
    matrices.") if !@header[:mxtype].match(/RUA/)

  @header[:nrow]   = line[13...28].strip.to_i
  @header[:ncol]   = line[28...42].strip.to_i
  @header[:nnzero] = line[42...56].strip.to_i
  @header[:neltvl] = line[56...70].strip.to_i

  line = @file.gets

  fortran_reader = NMatrix::IO::FortranFormat::Reader

  @header[:ptrfmt] = fortran_reader.new(line[0...16].strip) .parse
  @header[:indfmt] = fortran_reader.new(line[16...32].strip).parse
  @header[:valfmt] = fortran_reader.new(line[32...52].strip).parse
  @header[:rhsfmt] = fortran_reader.new(line[52...72].strip).parse

  @header
end
values() click to toggle source
# File lib/nmatrix/io/harwell_boeing.rb, line 132
def values
  @header      = header if @header.empty?
  @file.lineno = 5      if @file.lineno != 5
  @matrix      = NMatrix.new([ @header[:nrow], @header[:ncol] ], 
                              0, dtype: :float64)

  read_column_pointers
  read_row_indices
  read_values

  @file.close
  
  assemble_matrix

  @matrix
end