Module: StairCar::PMatrixTypes

Included in:
PMatrix
Defined in:
lib/stair_car/pmatrix/types.rb

Instance Method Summary collapse

Instance Method Details

#sparse?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/stair_car/pmatrix/types.rb', line 18

def sparse?
  klass = @data.class
  if klass == Java::CernColtMatrixTdoubleImpl::DenseDoubleMatrix2D
    return false
  elsif klass == Java::CernColtMatrixTdoubleImpl::SparseDoubleMatrix2D
    return true
  elsif klass == Java::CernColtMatrixTfloatImpl::DenseFloatMatrix2D
    return false
  elsif klass == Java::CernColtMatrixTfloatImpl::SparseFloatMatrix2D
    return true
  end
end

#typeObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/stair_car/pmatrix/types.rb', line 5

def type
  klass = @data.class
  if klass == Java::CernColtMatrixTdoubleImpl::DenseDoubleMatrix2D
    return :double
  elsif klass == Java::CernColtMatrixTdoubleImpl::SparseDoubleMatrix2D
    return :double
  elsif klass == Java::CernColtMatrixTfloatImpl::DenseFloatMatrix2D
    return :float
  elsif klass == Java::CernColtMatrixTfloatImpl::SparseFloatMatrix2D
    return :float
  end
end

#type_class(type, sparse, initialize_values) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/stair_car/pmatrix/types.rb', line 32

def type_class(type, sparse, initialize_values)
  types = {
    :zeros => {
      false => {
        # dense
        :double => Java::cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D,
        :float => Java::cern.colt.matrix.tfloat.impl.DenseFloatMatrix2D
      },
      true => {
        # sparse
        :double => Java::cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D,
        # :double => Proc.new {|rows,cols| Java::cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D.new(rows,cols,10000.to_java(:int),0.0.to_java(:double),0.999999999.to_java(:double)) },
        :float => Java::cern.colt.matrix.tfloat.impl.SparseFloatMatrix2D
      }
    },
    :ones => {
      false => {
        # dense
        :double => Java::cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D,
        :float => Java::cern.colt.matrix.tfloat.impl.DenseFloatMatrix2D
      },
      true => {
        # sparse
        :double => Java::cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D,
        :float => Java::cern.colt.matrix.tfloat.impl.SparseFloatMatrix2D
      }
    },
    :rand => {
      false => {
        # dense
        :double => Java::cern.colt.matrix.tdouble.DoubleFactory2D.dense.method(:random),
        :float => Java::cern.colt.matrix.tfloat.FloatFactory2D.dense.method(:random)
      },
      true => {
        # sparse
        :double => Java::cern.colt.matrix.tdouble.DoubleFactory2D.sparse.method(:random),
        :float => Java::cern.colt.matrix.tfloat.FloatFactory2D.sparse.method(:random)
      }
    },
    :desc => {
      false => {
        # dense
        :double => Java::cern.colt.matrix.tdouble.DoubleFactory2D.dense.method(:descending),
        :float => Java::cern.colt.matrix.tfloat.FloatFactory2D.dense.method(:descending)
      },
      true => {
        # sparse
        :double => Java::cern.colt.matrix.tdouble.DoubleFactory2D.sparse.method(:descending),
        :float => Java::cern.colt.matrix.tfloat.FloatFactory2D.sparse.method(:descending)
      }
    },
    :asc => {
      false => {
        # dense
        :double => Java::cern.colt.matrix.tdouble.DoubleFactory2D.dense.method(:ascending),
        :float => Java::cern.colt.matrix.tfloat.FloatFactory2D.dense.method(:ascending)
      },
      true => {
        # sparse
        :double => Java::cern.colt.matrix.tdouble.DoubleFactory2D.sparse.method(:ascending),
        :float => Java::cern.colt.matrix.tfloat.FloatFactory2D.sparse.method(:ascending)
      }
    }
  }

  klass = types[initialize_values][sparse][type]

  unless klass
    raise "Could not make a #{sparse ? 'sparse' : 'dense'} #{type} matrix"
  end

  return klass
end