Class: VectorSSE::Mat

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_sse.rb

Constant Summary collapse

MIN_ROW_COL_COUNT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, rows, cols, data = nil) ⇒ Mat

Returns a new instance of Mat.



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
# File 'lib/vector_sse.rb', line 60

def initialize( type, rows, cols, data=nil )

   if VectorSSE::valid_type( type )
      @type = type
   else
      raise ArgumentError.new( "invalid SSE matrix type for argument 0" )
   end

   if rows < MIN_ROW_COL_COUNT
      raise ArgumentError.new( "row count must be greater than zero for argument 1" )
   end

   if cols < MIN_ROW_COL_COUNT
      raise ArgumentError.new( "column count must be greater than zero for argument 2" )
   end

   if data && ( data.class != ::Array )
      raise ArgumentError.new( "expected value of type Array for argument 3" )
   end

   @rows = rows
   @cols = cols
   @linear_size = @rows * @cols

   @data = ::Array.new( @linear_size, 0 )

   fill( data ) if data
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



58
59
60
# File 'lib/vector_sse.rb', line 58

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



57
58
59
# File 'lib/vector_sse.rb', line 57

def rows
  @rows
end

#typeObject (readonly)

Returns the value of attribute type.



56
57
58
# File 'lib/vector_sse.rb', line 56

def type
  @type
end

Instance Method Details

#*(other) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/vector_sse.rb', line 135

def *( other )

   scalar_mul = false

   if [ Integer, Float ].include? other.class

      scalar_mul = true
      scalar_value = other
      other = Mat.new( @type, @rows, @cols )
      other.data.replace( ::Array.new( @linear_size, scalar_value ) )

   elsif other.class == self.class

      if @cols != other.rows
         raise "invalid matrix dimensions"
      end

   else
      raise ArgumentError.new(
         "expected argument of type #{self.class} for argument 0" )
   end
   

   result = Mat.new( @type, @rows, other.cols )

   case @type
   when Type::S32
      if scalar_mul
         result.data.replace( VectorSSE::vec_mul_s32( self.data, other.data ) )
      else
         result.data.replace( VectorSSE::mul_s32(
            @data, @rows, @cols, other.data, other.rows, other.cols ) )
      end
   when Type::S64
      if scalar_mul
         result.data.replace( VectorSSE::vec_mul_s32( self.data, other.data ) )
      else
         result.data.replace( VectorSSE::mul_s64(
            @data, @rows, @cols, other.data, other.rows, other.cols ) )
      end
   when Type::F32
      if scalar_mul
         result.data.replace( VectorSSE::vec_mul_f32( self.data, other.data ) )
      else
         result.data.replace( VectorSSE::mul_f32(
            @data, @rows, @cols, other.data, other.rows, other.cols ) )
      end
   when Type::F64
      if scalar_mul
         result.data.replace( VectorSSE::vec_mul_f64( self.data, other.data ) )
      else
         result.data.replace( VectorSSE::mul_f64(
            @data, @rows, @cols, other.data, other.rows, other.cols ) )
      end
   end

   result
end

#+(other) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/vector_sse.rb', line 194

def +( other )

   if [ Integer, Float ].include? other.class

      scalar_value = other
      other = Mat.new( @type, @rows, @cols )
      other.data.replace( ::Array.new( @linear_size, scalar_value ) )

   elsif other.class == self.class

      if ( @rows != other.rows ) || ( @cols != other.cols )
         raise ArgumentError.new(
            "matrix addition requires operands of equal size")
      end

   else

      raise ArgumentError.new(
         "expect argument of type #{self.class}, Integer, or Float for argument 0" )

   end

   result = Mat.new( @type, @rows, @cols )

   case @type
   when Type::S32
      result.data.replace( VectorSSE::add_s32( self.data, other.data ) )
   when Type::S64
      result.data.replace( VectorSSE::add_s64( self.data, other.data ) )
   when Type::F32
      result.data.replace( VectorSSE::add_f32( self.data, other.data ) )
   when Type::F64
      result.data.replace( VectorSSE::add_f64( self.data, other.data ) )
   end

   result
end

#-(other) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/vector_sse.rb', line 232

def -( other )

   if [ Integer, Float ].include? other.class

      scalar_value = other
      other = Mat.new( @type, @rows, @cols )
      other.data = ::Array.new( @linear_size, scalar_value )

   elsif other.class == self.class

      if ( @rows != other.rows ) || ( @cols != other.cols )
         raise ArgumentError.new(
            "matrix subtraction requires operands of equal size")
      end

   else

      raise ArgumentError.new(
         "expect argument of type #{self.class}, Integer, or Float for argument 0" )

   end

   result = Mat.new( @type, @rows, @cols )

   case @type
   when Type::S32
      result.data.replace( VectorSSE::sub_s32( self.data, other.data ) )
   when Type::S64
      result.data.replace( VectorSSE::sub_s64( self.data, other.data ) )
   when Type::F32
      result.data.replace( VectorSSE::sub_f32( self.data, other.data ) )
   when Type::F64
      result.data.replace( VectorSSE::sub_f64( self.data, other.data ) )
   end

   result
end

#[](pos) ⇒ Object



112
113
114
115
# File 'lib/vector_sse.rb', line 112

def []( pos )
   valid_linear_index( pos )
   @data[ pos ]
end

#[]=(pos, val) ⇒ Object



117
118
119
120
121
# File 'lib/vector_sse.rb', line 117

def []=( pos, val )
   valid_linear_index( pos )
   valid_data_type( val )
   @data[ pos ] = val
end

#at(row, col) ⇒ Object



89
90
91
92
# File 'lib/vector_sse.rb', line 89

def at( row, col )
   valid_row_col( row, col )
   @data[ linear_index( row, col ) ]
end

#fill(data) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vector_sse.rb', line 100

def fill( data )
   if data.length != @rows * @cols
      raise ArgumentError.new( "size does not match matrix size" )
   end

   data.each do |value|
      valid_data_type( value )
   end

   @data = data
end

#reshape(rows, cols) ⇒ Object



274
275
276
# File 'lib/vector_sse.rb', line 274

def reshape( rows, cols )
   raise "unimplemented"
end

#set(row, col, val) ⇒ Object



94
95
96
97
98
# File 'lib/vector_sse.rb', line 94

def set( row, col, val )
   valid_row_col( row, col )
   valid_data_type( val )
   @data[ linear_index( row, col ) ] = val
end

#to_sObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/vector_sse.rb', line 123

def to_s
   text = ""
   @rows.times do |r|
      vals = []
      @cols.times do |c|
         vals << @data[r*@cols + c]
      end
      text << "|#{vals.join(' ')}|\n"
   end
   text
end

#transposeObject



270
271
272
# File 'lib/vector_sse.rb', line 270

def transpose
   raise "unimplemented"
end