Sha256: 8cac87a629691bf2a0d2969e99ed6d5e26d24ba9065af18bbf8eb88841476853

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module SVMKit
  # Module for utility methods.
  module Utils
    class << self
      # Dump an NMatrix object converted to a Ruby Hash.
      # # call-seq:
      #   dump_nmatrix(mat) -> Hash
      #
      # * *Arguments* :
      #   - +mat+ -- An NMatrix object converted to a Ruby Hash.
      # * *Returns* :
      #   - A Ruby Hash containing matrix information.
      def dump_nmatrix(mat)
        return nil if mat.class != NMatrix
        { shape: mat.shape, array: mat.to_flat_a, dtype: mat.dtype, stype: mat.stype }
      end

      # Return the results of converting the dumped data into an NMatrix object.
      #
      # call-seq:
      #   restore_nmatrix(dumped_mat) -> NMatrix
      #
      # * *Arguments* :
      #   - +dumpted_mat+ -- A Ruby Hash about NMatrix object created with SVMKit::Utils.dump_nmatrix method.
      # * *Returns* :
      #   - An NMatrix object restored from the given Hash.
      def restore_nmatrix(dmp = {})
        return nil unless dmp.class == Hash && %i[shape array dtype stype].all?(&dmp.method(:has_key?))
        NMatrix.new(dmp[:shape], dmp[:array], dtype: dmp[:dtype], stype: dmp[:stype])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
svmkit-0.1.1 lib/svmkit/utils.rb
svmkit-0.1.0 lib/svmkit/utils.rb