Class: StairCar::PMatrix

Inherits:
Object
  • Object
show all
Includes:
Indicies, InitMethods, Inspect, Iteration, Methods, PMatrixCompare, PMatrixMatrixMath, PMatrixTransforms, PMatrixTypes
Defined in:
lib/stair_car/pmatrix/pmatrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Methods

#abs, #sqrt

Methods included from InitMethods

#array_dimensions, #from_array, included

Methods included from Indicies

#convert_indicies

Methods included from Inspect

#inspect, #max_char_width

Methods included from Iteration

#each, #each_column, #each_row, #each_with_index

Methods included from PMatrixCompare

#<, #<=, #==, #>, #>=, #any?, #find

Methods included from PMatrixTransforms

#count, #inv, #map, #map!, #map_non_zero, #map_non_zero!, #to_a, #transpose, #~

Methods included from PMatrixMatrixMath

#*, #**, #+, #-, #/, #max, #mean, #min, #perform, #std, #sum, #variance

Methods included from PMatrixTypes

#sparse?, #type, #type_class

Constructor Details

#initialize(rows_or_data = nil, cols = nil, type = :double, sparse = false, initialize_values = :zeros) ⇒ PMatrix

Returns a new instance of PMatrix.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 44

def initialize(rows_or_data=nil, cols=nil, type=:double, sparse=false, initialize_values=:zeros)
  if rows_or_data.is_a?(Array)
    klass = type_class(type, sparse, initialize_values)

    # Create the matrix from an array
    from_array(rows_or_data, klass)
  elsif rows_or_data.is_a?(Fixnum)
    raise MatrixDimensionsError, "Must specify columns and rows" unless rows_or_data && cols
    klass = type_class(type, sparse, initialize_values)
    if klass.is_a?(Method) || klass.is_a?(Proc)
      # A factory method was returned, call to build
      @data = klass.call(rows_or_data, cols)
    else
      # A class was returned, create new
      @data = klass.new(rows_or_data, cols)

      setup_default_values(initialize_values)
    end
  else
    # Passing in data directly
    @data = rows_or_data
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



26
27
28
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 26

def data
  @data
end

Instance Method Details

#[](rows, cols) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 90

def [](rows, cols)
  rows = convert_indicies(rows, self.rows)
  cols = convert_indicies(cols, self.cols)

  # Returns either the value in a cell or a subview
  # if rows && cols && rows.size == 1 && cols.size == 1 && rows.first.is_a?(Fixnum) && cols.first.is_a?(Fixnum)
  #   @data.get(rows.first, cols.first)
  # else
    # Get subview, also convert rows/cols to java arrays
    self.class.new(@data.view_selection(rows && rows.to_java(:int), cols && cols.to_java(:int)))
  # end
end

#[]=(rows, cols, value) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 103

def []=(rows, cols, value)
  rows = convert_indicies(rows, self.rows)
  cols = convert_indicies(cols, self.cols)

  if value.is_a?(PMatrix) && value.rows == 1 && value.cols == 1
    value = value.value_at(0,0)
  end

  # Set either the value in a cell or a subview with a matrix
  if rows && cols && rows.size == 1 && cols.size == 1 && rows.first.is_a?(Fixnum) && cols.first.is_a?(Fixnum)
    @data.set(rows.first, cols.first, value)
  else
    subview = @data.view_selection(rows && rows.to_java(:int), cols && cols.to_java(:int))

    # Assign a single array or a nested array
    if value.is_a?(Array)
      value_rows, value_cols = array_dimensions(value)

      # If one dimentional, and they want to set cols
      if value_rows == 1 && subview.columns == 1
        # Transpose so we can place an array vertically
        subview = subview.view_dice
      end

      # Check to make sure the sizes match
      if value_rows != subview.rows || value_cols != subview.columns
        raise MatrixDimensionsError, "the array you are trying to assign is not the correct size"
      end
    end

    if value.is_a?(PMatrix)
      value = value.data
    end

    if value.is_a?(Array)
      assign_values(subview, value)
    else
      subview.assign(value)
    end
  end
end

#assign_values(subview, value) ⇒ Object

Takes an array and assigns it to the right cells in the subview



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 157

def assign_values(subview, value)
  unless value.first.is_a?(Array)
    value = [value]
  end

  value.each_with_index do |row,row_index|
    row.each_with_index do |val,col_index|
      subview.set(row_index, col_index, val.to_java(:double))
    end
  end
end

#colsObject



35
36
37
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 35

def cols
  @data.columns
end

#dupObject



169
170
171
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 169

def dup
  PMatrix.new(@data.copy)
end

#each_non_zero(&block) ⇒ Object

Loop through each non-zero value, pass in the value, row, column



147
148
149
150
151
152
153
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 147

def each_non_zero(&block)
  @data.for_each_non_zero do |row, col, value|
    yield(value, row, col)

    value
  end
end

#setup_default_values(initialize_values) ⇒ Object



68
69
70
71
72
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 68

def setup_default_values(initialize_values)
  if initialize_values == :ones
    @data.assign(1.0)
  end
end

#shapeObject



39
40
41
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 39

def shape
  [rows, cols]
end

#to_iObject

Takes a 1x1 matrix and converts it to an integer, raises an exception if the matrix is not 1x1



76
77
78
79
80
81
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 76

def to_i
  if rows != 1 || cols != 1
    raise IncorrectMatrixDimensions, "to_i should only be called on 1x1 matricies"
  end
  return value_at(0, 0)
end

#value_at(row, col) ⇒ Object

Gets the value at the row and column



84
85
86
87
88
# File 'lib/stair_car/pmatrix/pmatrix.rb', line 84

def value_at(row, col)
  row = convert_indicies(row, self.rows)
  col = convert_indicies(col, self.cols)
  return @data.get(row.first, col.first)
end