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.



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

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.



60
61
62
# File 'lib/vector_sse.rb', line 60

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#*(other) ⇒ Object



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

def *( other )

   scalar_mul = false

   if [ Fixnum, 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



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

def +( other )

   if [ Fixnum, 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}, Fixnum, 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



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

def -( other )

   if [ Fixnum, 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}, Fixnum, 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



114
115
116
117
# File 'lib/vector_sse.rb', line 114

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

#[]=(pos, val) ⇒ Object



119
120
121
122
123
# File 'lib/vector_sse.rb', line 119

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

#at(row, col) ⇒ Object



91
92
93
94
# File 'lib/vector_sse.rb', line 91

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

#fill(data) ⇒ Object



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

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



276
277
278
# File 'lib/vector_sse.rb', line 276

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

#set(row, col, val) ⇒ Object



96
97
98
99
100
# File 'lib/vector_sse.rb', line 96

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

#to_sObject



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

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



272
273
274
# File 'lib/vector_sse.rb', line 272

def transpose
   raise "unimplemented"
end