Matrix最適化とメソッド追加
@@ -126,6 +126,7 @@ | ||
126 | 126 | if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) |
127 | 127 | { |
128 | 128 | int i, j; |
129 | + float num = NUM2FLOAT( varg ); | |
129 | 130 | |
130 | 131 | vresult = Matrix_allocate( cMatrix ); |
131 | 132 | result = DXRUBY_GET_STRUCT( Matrix, vresult ); |
@@ -135,7 +136,7 @@ | ||
135 | 136 | { |
136 | 137 | for( j = 0; j < mat_d->x; j++ ) |
137 | 138 | { |
138 | - result->m[i][j] = mat_d->m[i][j] * NUM2FLOAT( varg ); | |
139 | + result->m[i][j] = mat_d->m[i][j] * num; | |
139 | 140 | } |
140 | 141 | } |
141 | 142 | } |
@@ -147,7 +148,7 @@ | ||
147 | 148 | DXRUBY_CHECK_TYPE( Matrix, varg ); |
148 | 149 | mat_s = DXRUBY_GET_STRUCT( Matrix, varg ); |
149 | 150 | |
150 | - if( mat_d->x != mat_s->y || mat_d->y != mat_s->x ) rb_raise( eDXRubyError, "要素数が一致していません。 - Matrix_*"); | |
151 | + if( mat_d->x != mat_s->y || mat_d->y != mat_s->x ) rb_raise( eDXRubyError, "Number of elements does not match - Matrix_*"); | |
151 | 152 | vresult = Matrix_allocate( cMatrix ); |
152 | 153 | result = DXRUBY_GET_STRUCT( Matrix, vresult ); |
153 | 154 | result->x = mat_s->x; |
@@ -167,7 +168,133 @@ | ||
167 | 168 | return vresult; |
168 | 169 | } |
169 | 170 | |
171 | +static VALUE Matrix_div( VALUE self, VALUE varg ) | |
172 | +{ | |
173 | + struct DXRubyMatrix *mat_d = DXRUBY_GET_STRUCT( Matrix, self ); | |
174 | + struct DXRubyMatrix *result; | |
175 | + VALUE vresult; | |
170 | 176 | |
177 | + if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) | |
178 | + { | |
179 | + int i, j; | |
180 | + float num = NUM2FLOAT( varg ); | |
181 | + | |
182 | + vresult = Matrix_allocate( cMatrix ); | |
183 | + result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
184 | + result->x = mat_d->x; | |
185 | + result->y = mat_d->y; | |
186 | + for( i = 0; i < mat_d->y; i++ ) | |
187 | + { | |
188 | + for( j = 0; j < mat_d->x; j++ ) | |
189 | + { | |
190 | + result->m[i][j] = mat_d->m[i][j] / num; | |
191 | + } | |
192 | + } | |
193 | + } | |
194 | + else | |
195 | + { | |
196 | + rb_raise( eDXRubyError, "Argument is wrong - Matrix_/"); | |
197 | + } | |
198 | + return vresult; | |
199 | +} | |
200 | + | |
201 | +static VALUE Matrix_add( VALUE self, VALUE varg ) | |
202 | +{ | |
203 | + struct DXRubyMatrix *mat_d = DXRUBY_GET_STRUCT( Matrix, self ); | |
204 | + struct DXRubyMatrix *result; | |
205 | + VALUE vresult; | |
206 | + | |
207 | + if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) | |
208 | + { | |
209 | + int i, j; | |
210 | + float num = NUM2FLOAT( varg ); | |
211 | + | |
212 | + vresult = Matrix_allocate( cMatrix ); | |
213 | + result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
214 | + result->x = mat_d->x; | |
215 | + result->y = mat_d->y; | |
216 | + for( i = 0; i < mat_d->y; i++ ) | |
217 | + { | |
218 | + for( j = 0; j < mat_d->x; j++ ) | |
219 | + { | |
220 | + result->m[i][j] = mat_d->m[i][j] + num; | |
221 | + } | |
222 | + } | |
223 | + } | |
224 | + else | |
225 | + { | |
226 | + int i, j, k; | |
227 | + struct DXRubyMatrix *mat_s; | |
228 | + | |
229 | + DXRUBY_CHECK_TYPE( Matrix, varg ); | |
230 | + mat_s = DXRUBY_GET_STRUCT( Matrix, varg ); | |
231 | + | |
232 | + if( mat_d->x != mat_s->y || mat_d->y != mat_s->x ) rb_raise( eDXRubyError, "Number of elements does not match - Matrix_+"); | |
233 | + vresult = Matrix_allocate( cMatrix ); | |
234 | + result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
235 | + result->x = mat_s->x; | |
236 | + result->y = mat_d->y; | |
237 | + | |
238 | + for( i = 0; i < mat_d->y; i++ ) | |
239 | + { | |
240 | + for( j = 0; j < mat_s->x; j++ ) | |
241 | + { | |
242 | + result->m[i][j] += mat_d->m[i][j] + mat_s->m[i][j]; | |
243 | + } | |
244 | + } | |
245 | + } | |
246 | + return vresult; | |
247 | +} | |
248 | + | |
249 | +static VALUE Matrix_sub( VALUE self, VALUE varg ) | |
250 | +{ | |
251 | + struct DXRubyMatrix *mat_d = DXRUBY_GET_STRUCT( Matrix, self ); | |
252 | + struct DXRubyMatrix *result; | |
253 | + VALUE vresult; | |
254 | + | |
255 | + if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM ) | |
256 | + { | |
257 | + int i, j; | |
258 | + float num = NUM2FLOAT( varg ); | |
259 | + | |
260 | + vresult = Matrix_allocate( cMatrix ); | |
261 | + result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
262 | + result->x = mat_d->x; | |
263 | + result->y = mat_d->y; | |
264 | + for( i = 0; i < mat_d->y; i++ ) | |
265 | + { | |
266 | + for( j = 0; j < mat_d->x; j++ ) | |
267 | + { | |
268 | + result->m[i][j] = mat_d->m[i][j] - num; | |
269 | + } | |
270 | + } | |
271 | + } | |
272 | + else | |
273 | + { | |
274 | + int i, j, k; | |
275 | + struct DXRubyMatrix *mat_s; | |
276 | + | |
277 | + DXRUBY_CHECK_TYPE( Matrix, varg ); | |
278 | + mat_s = DXRUBY_GET_STRUCT( Matrix, varg ); | |
279 | + | |
280 | + if( mat_d->x != mat_s->y || mat_d->y != mat_s->x ) rb_raise( eDXRubyError, "Number of elements does not match - Matrix_-"); | |
281 | + vresult = Matrix_allocate( cMatrix ); | |
282 | + result = DXRUBY_GET_STRUCT( Matrix, vresult ); | |
283 | + result->x = mat_s->x; | |
284 | + result->y = mat_d->y; | |
285 | + | |
286 | + for( i = 0; i < mat_d->y; i++ ) | |
287 | + { | |
288 | + for( j = 0; j < mat_s->x; j++ ) | |
289 | + { | |
290 | + result->m[i][j] += mat_d->m[i][j] - mat_s->m[i][j]; | |
291 | + } | |
292 | + } | |
293 | + } | |
294 | + return vresult; | |
295 | +} | |
296 | + | |
297 | + | |
171 | 298 | static VALUE Matrix_to_s( VALUE self ) |
172 | 299 | { |
173 | 300 | struct DXRubyMatrix *mat = DXRUBY_GET_STRUCT( Matrix, self ); |
@@ -643,7 +770,7 @@ | ||
643 | 770 | } |
644 | 771 | else |
645 | 772 | { |
646 | - rb_raise( eDXRubyError, "引数が異常です - Vector_mul"); | |
773 | + rb_raise( eDXRubyError, "Argument is wrong - Vector_mul"); | |
647 | 774 | } |
648 | 775 | |
649 | 776 | return vresult; |
@@ -1183,8 +1310,13 @@ | ||
1183 | 1310 | /* Matrixクラスにインスタンスメソッド登録*/ |
1184 | 1311 | rb_define_private_method( cMatrix, "initialize", Matrix_initialize, -1 ); |
1185 | 1312 | rb_define_method( cMatrix, "*", Matrix_mul, 1 ); |
1313 | + rb_define_method( cMatrix, "/", Matrix_div, 1 ); | |
1314 | + rb_define_method( cMatrix, "+", Matrix_add, 1 ); | |
1315 | + rb_define_method( cMatrix, "-", Matrix_sub, 1 ); | |
1186 | 1316 | rb_define_method( cMatrix, "to_s", Matrix_to_s, 0 ); |
1317 | + rb_define_method( cMatrix, "inspect", Matrix_to_s, 0 ); | |
1187 | 1318 | rb_define_method( cMatrix, "to_a", Matrix_to_a, 0 ); |
1319 | + rb_define_method( cMatrix, "to_ary", Matrix_to_a, 0 ); | |
1188 | 1320 | rb_define_method( cMatrix, "inverse", Matrix_inverse, 0 ); |
1189 | 1321 | |
1190 | 1322 | /* Matrixオブジェクトを生成した時にinitializeの前に呼ばれるメモリ割り当て関数登録 */ |
@@ -1209,7 +1341,9 @@ | ||
1209 | 1341 | rb_define_method( cVector, "-@", Vector_minus, 0 ); |
1210 | 1342 | rb_define_method( cVector, "/", Vector_div, 1 ); |
1211 | 1343 | rb_define_method( cVector, "to_s", Vector_to_s, 0 ); |
1344 | + rb_define_method( cVector, "inspect", Vector_to_s, 0 ); | |
1212 | 1345 | rb_define_method( cVector, "to_a", Vector_to_a, 0 ); |
1346 | + rb_define_method( cVector, "to_ary", Vector_to_a, 0 ); | |
1213 | 1347 | rb_define_method( cVector, "x", Vector_get_x, 0 ); |
1214 | 1348 | rb_define_method( cVector, "y", Vector_get_y, 0 ); |
1215 | 1349 | rb_define_method( cVector, "z", Vector_get_z, 0 ); |
@@ -1218,6 +1352,7 @@ | ||
1218 | 1352 | rb_define_method( cVector, "xyz", Vector_get_xyz, 0 ); |
1219 | 1353 | rb_define_method( cVector, "size", Vector_get_size, 0 ); |
1220 | 1354 | rb_define_method( cVector, "normalize", Vector_normalize, 0 ); |
1355 | + rb_define_method( cVector, "length", Vector_norm, 0 ); | |
1221 | 1356 | rb_define_method( cVector, "norm", Vector_norm, 0 ); |
1222 | 1357 | rb_define_method( cVector, "norm2", Vector_norm2, 0 ); |
1223 | 1358 | rb_define_method( cVector, "==", Vector_equal, 1 ); |