stair_car 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,10 +24,19 @@ module StairCar
24
24
  @data.cardinality > 0
25
25
  end
26
26
 
27
-
28
27
  # Compares this matrix to another to see if they are the same (in values)
29
28
  def ==(matrix2)
30
- self.data.equals(matrix2.data)
29
+ if matrix2.is_a?(Fixnum) || matrix2.is_a?(Float)
30
+ # See if its a 1x1
31
+ if rows != 1 || cols != 1
32
+ return false
33
+ else
34
+ # Check if the value matches
35
+ return self.to_i == matrix2
36
+ end
37
+ else
38
+ return self.data.equals(matrix2.data)
39
+ end
31
40
  end
32
41
 
33
42
  end
@@ -71,17 +71,33 @@ module StairCar
71
71
  end
72
72
  end
73
73
 
74
+ # Takes a 1x1 matrix and converts it to an integer, raises an exception
75
+ # if the matrix is not 1x1
76
+ def to_i
77
+ if rows != 1 || cols != 1
78
+ raise IncorrectMatrixDimensions, "to_i should only be called on 1x1 matricies"
79
+ end
80
+ return value_at(0, 0)
81
+ end
82
+
83
+ # Gets the value at the row and column
84
+ def value_at(row, col)
85
+ row = convert_indicies(row, self.rows)
86
+ col = convert_indicies(col, self.cols)
87
+ return @data.get(row.first, col.first)
88
+ end
89
+
74
90
  def [](rows, cols)
75
91
  rows = convert_indicies(rows, self.rows)
76
92
  cols = convert_indicies(cols, self.cols)
77
93
 
78
94
  # Returns either the value in a cell or a subview
79
- if rows && cols && rows.size == 1 && cols.size == 1 && rows.first.is_a?(Fixnum) && cols.first.is_a?(Fixnum)
80
- @data.get(rows.first, cols.first)
81
- else
95
+ # if rows && cols && rows.size == 1 && cols.size == 1 && rows.first.is_a?(Fixnum) && cols.first.is_a?(Fixnum)
96
+ # @data.get(rows.first, cols.first)
97
+ # else
82
98
  # Get subview, also convert rows/cols to java arrays
83
99
  self.class.new(@data.view_selection(rows && rows.to_java(:int), cols && cols.to_java(:int)))
84
- end
100
+ # end
85
101
  end
86
102
 
87
103
  def []=(rows, cols, value)
@@ -4,4 +4,8 @@ module StairCar
4
4
 
5
5
  class InverseMatrixIsSignular < RuntimeError
6
6
  end
7
+
8
+ class IncorrectMatrixDimensions < RuntimeError
9
+ end
10
+
7
11
  end
@@ -76,7 +76,7 @@ module StairCar
76
76
  max_cell_width = 0
77
77
  rows.times do |row|
78
78
  cols.times do |column|
79
- cell_value = self[row, column]
79
+ cell_value = self.value_at(row, column)
80
80
  width = formatter.call(cell_value).size + 1
81
81
 
82
82
  # Track the biggest cell width we've seen so far
@@ -97,11 +97,11 @@ module StairCar
97
97
  def row_line_text(row_number, formatter, max_columns, screen_width, cell_width, hidden_columns)
98
98
  line = ''
99
99
  [self.cols-1, max_columns].min.times do |column|
100
- cell_value = self[row_number, column]
100
+ cell_value = self.value_at(row_number, column)
101
101
  line << formatter.call(cell_value).ljust(cell_width)
102
102
  end
103
103
 
104
- last_cell = formatter.call(self[row_number, -1]).ljust(cell_width-1)
104
+ last_cell = formatter.call(self.value_at(row_number, -1)).ljust(cell_width-1)
105
105
  padding_size = screen_width - line.size - last_cell.size - 1
106
106
  line << ('.' * padding_size) + ' ' if hidden_columns
107
107
 
@@ -3,7 +3,7 @@ module StairCar
3
3
  def each(&block)
4
4
  rows.times do |row|
5
5
  cols.times do |col|
6
- yield(self[row,col])
6
+ yield(self.value_at(row,col))
7
7
  end
8
8
  end
9
9
  end
@@ -11,7 +11,7 @@ module StairCar
11
11
  def each_with_index(&block)
12
12
  rows.times do |row|
13
13
  cols.times do |col|
14
- yield(self[row,col], row, col)
14
+ yield(self.value_at(row,col), row, col)
15
15
  end
16
16
  end
17
17
  end
@@ -24,10 +24,19 @@ module StairCar
24
24
  # @data.cardinality > 0
25
25
  # end
26
26
 
27
-
28
27
  # Compares this matrix to another to see if they are the same (in values)
29
28
  def ==(matrix2)
30
- self.data.equalsContent(matrix2.data)
29
+ if matrix2.is_a?(Fixnum) || matrix2.is_a?(Float)
30
+ # See if its a 1x1
31
+ if rows != 1 || cols != 1
32
+ return false
33
+ else
34
+ # Check if the value matches
35
+ return self.to_i == matrix2
36
+ end
37
+ else
38
+ self.data.equalsContent(matrix2.data)
39
+ end
31
40
  end
32
41
 
33
42
  end
@@ -87,29 +87,43 @@ module StairCar
87
87
  return shape[1]
88
88
  end
89
89
 
90
+ # Takes a 1x1 matrix and converts it to an integer, raises an exception
91
+ # if the matrix is not 1x1
92
+ def to_i
93
+ if rows != 1 || cols != 1
94
+ raise IncorrectMatrixDimensions, "to_i should only be called on 1x1 matricies"
95
+ end
96
+ return value_at(0, 0)
97
+ end
98
+
99
+ # Gets the value at the row and column
100
+ def value_at(row, col)
101
+ row = convert_indicies(row, self.rows)
102
+ col = convert_indicies(col, self.cols)
103
+
104
+ # Returns either the value in a cell or a subview
105
+ # From jruby we have to call this way for doubles
106
+ if @data.is_a?(Java::org.ujmp.core.objectmatrix.impl.ObjectCalculationMatrix)
107
+ @data.getObject(row.first.to_java(:int), col.first.to_java(:int)) || 0.0
108
+ else
109
+ # From jruby we have to call this way for doubles
110
+ @data.java_send(:getObject, [Java::long, Java::long], row.first, col.first) || 0.0
111
+ end
112
+ end
113
+
90
114
  def [](rows, cols)
91
115
  rows = convert_indicies(rows, self.rows)
92
116
  cols = convert_indicies(cols, self.cols)
93
117
 
94
- # Returns either the value in a cell or a subview
95
- if rows && cols && rows.size == 1 && cols.size == 1 && rows.first.is_a?(Fixnum) && cols.first.is_a?(Fixnum)
96
- if @data.is_a?(Java::org.ujmp.core.objectmatrix.impl.ObjectCalculationMatrix)
97
- @data.getObject(rows.first.to_java(:int), cols.first.to_java(:int)) || 0.0
98
- else
99
- # From jruby we have to call this way for doubles
100
- @data.java_send(:getObject, [Java::long, Java::long], rows.first, cols.first) || 0.0
101
- end
118
+ # Get subview, also convert rows/cols to java arrays
119
+ if !rows && !cols
120
+ return self.class.new(@data)
102
121
  else
103
- # Get subview, also convert rows/cols to java arrays
104
- if !rows && !cols
105
- return self.class.new(@data)
122
+ if rows
123
+ return self.class.new(@data.select(Java::OrgUjmpCoreCalculation::Calculation.LINK, rows, cols))
106
124
  else
107
- if rows
108
- return self.class.new(@data.select(Java::OrgUjmpCoreCalculation::Calculation.LINK, rows, cols))
109
- else
110
- # If row is nil, we need to lookup by columns directly
111
- return self.class.new(@data.select_columns(Java::OrgUjmpCoreCalculation::Calculation.LINK, cols))
112
- end
125
+ # If row is nil, we need to lookup by columns directly
126
+ return self.class.new(@data.select_columns(Java::OrgUjmpCoreCalculation::Calculation.LINK, cols))
113
127
  end
114
128
  end
115
129
  end
@@ -156,7 +170,7 @@ module StairCar
156
170
  # Loop through each non-zero value, pass in the value, row, column
157
171
  def each_non_zero(&block)
158
172
  @data.available_coordinates.each do |row, col|
159
- val = self[row,col]
173
+ val = self.value_at(row,col)
160
174
  yield(val, row, col)
161
175
  end
162
176
  end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "stair_car"
7
- gem.version = "0.1.1"
7
+ gem.version = "0.1.2"
8
8
  gem.authors = ["Ryan Stout"]
9
9
  gem.email = ["ryanstout@gmail.com"]
10
10
  gem.description = "StairCar is a fully featured matrix library for jruby (think matlab or numpy)"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stair_car
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -136,4 +136,3 @@ test_files:
136
136
  - spec/umatrix/matrix_math_spec.rb
137
137
  - spec/umatrix/transform_spec.rb
138
138
  - spec/umatrix/umatrix_spec.rb
139
- has_rdoc: