• R/O
  • SSH
  • HTTPS

dxruby: Commit


Commit MetaInfo

Revision547 (tree)
Time2017-03-12 18:05:08
Authormirichi

Log Message

Matrix最適化とメソッド追加

Change Summary

Incremental Difference

--- branches/1.5dev/matrix.c (revision 546)
+++ branches/1.5dev/matrix.c (revision 547)
@@ -126,6 +126,7 @@
126126 if( FIXNUM_P( varg ) || TYPE( varg ) == T_FLOAT || TYPE( varg ) == T_BIGNUM )
127127 {
128128 int i, j;
129+ float num = NUM2FLOAT( varg );
129130
130131 vresult = Matrix_allocate( cMatrix );
131132 result = DXRUBY_GET_STRUCT( Matrix, vresult );
@@ -135,7 +136,7 @@
135136 {
136137 for( j = 0; j < mat_d->x; j++ )
137138 {
138- result->m[i][j] = mat_d->m[i][j] * NUM2FLOAT( varg );
139+ result->m[i][j] = mat_d->m[i][j] * num;
139140 }
140141 }
141142 }
@@ -147,7 +148,7 @@
147148 DXRUBY_CHECK_TYPE( Matrix, varg );
148149 mat_s = DXRUBY_GET_STRUCT( Matrix, varg );
149150
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_*");
151152 vresult = Matrix_allocate( cMatrix );
152153 result = DXRUBY_GET_STRUCT( Matrix, vresult );
153154 result->x = mat_s->x;
@@ -167,7 +168,133 @@
167168 return vresult;
168169 }
169170
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;
170176
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+
171298 static VALUE Matrix_to_s( VALUE self )
172299 {
173300 struct DXRubyMatrix *mat = DXRUBY_GET_STRUCT( Matrix, self );
@@ -643,7 +770,7 @@
643770 }
644771 else
645772 {
646- rb_raise( eDXRubyError, "引数が異常です - Vector_mul");
773+ rb_raise( eDXRubyError, "Argument is wrong - Vector_mul");
647774 }
648775
649776 return vresult;
@@ -1183,8 +1310,13 @@
11831310 /* Matrixクラスにインスタンスメソッド登録*/
11841311 rb_define_private_method( cMatrix, "initialize", Matrix_initialize, -1 );
11851312 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 );
11861316 rb_define_method( cMatrix, "to_s", Matrix_to_s, 0 );
1317+ rb_define_method( cMatrix, "inspect", Matrix_to_s, 0 );
11871318 rb_define_method( cMatrix, "to_a", Matrix_to_a, 0 );
1319+ rb_define_method( cMatrix, "to_ary", Matrix_to_a, 0 );
11881320 rb_define_method( cMatrix, "inverse", Matrix_inverse, 0 );
11891321
11901322 /* Matrixオブジェクトを生成した時にinitializeの前に呼ばれるメモリ割り当て関数登録 */
@@ -1209,7 +1341,9 @@
12091341 rb_define_method( cVector, "-@", Vector_minus, 0 );
12101342 rb_define_method( cVector, "/", Vector_div, 1 );
12111343 rb_define_method( cVector, "to_s", Vector_to_s, 0 );
1344+ rb_define_method( cVector, "inspect", Vector_to_s, 0 );
12121345 rb_define_method( cVector, "to_a", Vector_to_a, 0 );
1346+ rb_define_method( cVector, "to_ary", Vector_to_a, 0 );
12131347 rb_define_method( cVector, "x", Vector_get_x, 0 );
12141348 rb_define_method( cVector, "y", Vector_get_y, 0 );
12151349 rb_define_method( cVector, "z", Vector_get_z, 0 );
@@ -1218,6 +1352,7 @@
12181352 rb_define_method( cVector, "xyz", Vector_get_xyz, 0 );
12191353 rb_define_method( cVector, "size", Vector_get_size, 0 );
12201354 rb_define_method( cVector, "normalize", Vector_normalize, 0 );
1355+ rb_define_method( cVector, "length", Vector_norm, 0 );
12211356 rb_define_method( cVector, "norm", Vector_norm, 0 );
12221357 rb_define_method( cVector, "norm2", Vector_norm2, 0 );
12231358 rb_define_method( cVector, "==", Vector_equal, 1 );
Show on old repository browser