kmat 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  static int
4
4
  mod(int a, int b)
5
5
  {
6
- int r = a % b;
6
+ const int r = a % b;
7
7
  if ( r < 0 ) {
8
8
  return r+b;
9
9
  } else {
@@ -16,10 +16,9 @@ VALUE
16
16
  kmm_mat_get_value(VALUE self, VALUE vi, VALUE vj)
17
17
  {
18
18
  SMAT *smat = km_mat2smat(self);
19
- int i = NUM2INT(vi);
20
- int j = NUM2INT(vj);
21
- if ( i < 0 || smat->m <= i || j < 0 || smat->n <= j ) {
22
- rb_raise(rb_eIndexError, "index (%d, %d) is out of range (%d, %d)", i, j, smat->m, smat->n);
19
+ const size_t i = NUM2ZU(vi), j = NUM2ZU(vj);
20
+ if ( smat->m <= i || smat->n <= j ) {
21
+ rb_raise(rb_eIndexError, "index (%zu, %zu) is out of range (%zu, %zu)", i, j, smat->m, smat->n);
23
22
  }
24
23
  VT_SWITCH( smat->vtype,
25
24
  return rb_float_new(ENTITY(smat, d, i, j));,
@@ -35,7 +34,7 @@ kmm_mat_get_value(VALUE self, VALUE vi, VALUE vj)
35
34
  VALUE
36
35
  kmm_mat_get_ssub(VALUE self, VALUE vi, VALUE vj, VALUE vm, VALUE vn)
37
36
  {
38
- return km_Mat_ssub(NUM2INT(vi), NUM2INT(vj), NUM2INT(vm), NUM2INT(vn), self);
37
+ return km_Mat_ssub(NUM2ZU(vi), NUM2ZU(vj), NUM2ZU(vm), NUM2ZU(vn), self);
39
38
  }
40
39
 
41
40
  // make a random-submatrix by :ivec indecies `vi', `vj'
@@ -44,8 +43,7 @@ kmm_mat_get_ssub(VALUE self, VALUE vi, VALUE vj, VALUE vm, VALUE vn)
44
43
  VALUE
45
44
  kmm_mat_get_rsub(VALUE self, VALUE vi, VALUE vj)
46
45
  {
47
- SMAT *si = km_mat2smat(vi);
48
- SMAT *sj = km_mat2smat(vj);
46
+ SMAT *si = km_mat2smat(vi), *sj = km_mat2smat(vj);
49
47
  if ( si->vtype != VT_INT || sj->vtype != VT_INT ) {
50
48
  rb_raise(km_eVT, "index arguments must be int vectors");
51
49
  }
@@ -58,17 +56,17 @@ kmm_mat_get_rsub(VALUE self, VALUE vi, VALUE vj)
58
56
  if ( sj->stype != ST_FULL ) {
59
57
  sj = km_mat2smat(rb_obj_dup(vj));
60
58
  }
61
- int *is = si->ibody, *js = sj->ibody;
59
+ const int *is = si->ibody, *js = sj->ibody;
62
60
  bool flg = true;
63
- int m = LENGTH(si), n = LENGTH(sj);
64
- for (int i=0; i<m-1; i++) {
61
+ const size_t m = LENGTH(si), n = LENGTH(sj);
62
+ for (size_t i=0; i<m-1; i++) {
65
63
  if ( is[i+1]-is[i] != 1 ) {
66
64
  flg = false;
67
65
  break;
68
66
  }
69
67
  }
70
68
  if ( flg ) {
71
- for (int i=0; i<n-1; i++) {
69
+ for (size_t i=0; i<n-1; i++) {
72
70
  if ( js[i+1]-js[i] != 1 ) {
73
71
  flg = false;
74
72
  break;
@@ -76,7 +74,7 @@ kmm_mat_get_rsub(VALUE self, VALUE vi, VALUE vj)
76
74
  }
77
75
  }
78
76
  if ( flg ) {
79
- return km_Mat_ssub(is[0], js[0], m, n, self);
77
+ return km_Mat_ssub(i2s(is[0]), i2s(js[0]), m, n, self);
80
78
  } else {
81
79
  return km_Mat_rsub1(m, n, is, js, self);
82
80
  }
@@ -92,20 +90,20 @@ kmm_mat_get_rsub2(VALUE self, VALUE vi)
92
90
  if ( vmat->vtype != VT_VALUE ) {
93
91
  rb_raise(km_eVT, "index argument must be an object matrix");
94
92
  }
95
- int m = vmat->m, n = vmat->n, idx;
96
- int m2 = smat->m, n2 = smat->n;
97
- int *is, *js;
98
- is = ALLOCA_N(int, int2size_t(n*m));
99
- js = ALLOCA_N(int, int2size_t(n*m));
93
+ size_t m = vmat->m, n = vmat->n, idx;
94
+ size_t m2 = smat->m, n2 = smat->n;
95
+ size_t *is, *js;
96
+ is = ALLOCA_N(size_t, n*m);
97
+ js = ALLOCA_N(size_t, n*m);
100
98
  VALUE ij;
101
- for ( int i=0; i<m; i++ ) { for ( int j=0; j<n; j++ ) {
99
+ for ( size_t i=0; i<m; i++ ) { for ( size_t j=0; j<n; j++ ) {
102
100
  ij = ENTITY(vmat, v, i, j);
103
101
  if ( (TYPE(ij) != T_ARRAY) || (RARRAY_LEN(ij) != 2) ) {
104
102
  rb_raise(rb_eIndexError, "value of vmat-index must be an Array with 2 elements");
105
103
  }
106
104
  idx = i+j*m;
107
- is[idx] = mod( NUM2INT(rb_ary_entry(ij, 0)), m2 );
108
- js[idx] = mod( NUM2INT(rb_ary_entry(ij, 1)), n2 );
105
+ is[idx] = i2s(mod( NUM2INT(rb_ary_entry(ij, 0)), s2i(m2) ));
106
+ js[idx] = i2s(mod( NUM2INT(rb_ary_entry(ij, 1)), s2i(n2) ));
109
107
  } }
110
108
  return km_Mat_rsub2(m, n, is, js, self);
111
109
  }
@@ -117,10 +115,10 @@ kmm_mat_get_rsub2(VALUE self, VALUE vi)
117
115
  static void
118
116
  km_gbb_len(bool *eb, void *data)
119
117
  {
120
- if ( *eb ) { *((int *)data) += 1; };
118
+ if ( *eb ) { *((size_t *)data) += 1; };
121
119
  }
122
120
  struct km_gbb_arg {
123
- int l;
121
+ size_t l;
124
122
  union {
125
123
  double **dpbody;
126
124
  COMPLEX **zpbody;
@@ -208,9 +206,9 @@ kmm_mat_set_value(VALUE self, VALUE vi, VALUE vj, VALUE val)
208
206
  {
209
207
  km_check_frozen(self);
210
208
  SMAT *smat = km_mat2smat(self);
211
- int i = NUM2INT(vi), j = NUM2INT(vj);
212
- if ( i<0 || smat->m<i || j<0 || smat->n<j ) {
213
- rb_raise(rb_eIndexError, "index (%d, %d) is out of range (%d, %d)", i, j, smat->m, smat->n);
209
+ size_t i = NUM2ZU(vi), j = NUM2ZU(vj);
210
+ if ( smat->m<i || smat->n<j ) {
211
+ rb_raise(rb_eIndexError, "index (%zu, %zu) is out of range (%zu, %zu)", i, j, smat->m, smat->n);
214
212
  }
215
213
  if ( smat->stype == ST_RSUB ) {
216
214
  VT_SWITCH( smat->vtype,
@@ -262,10 +260,10 @@ kmm_mat_tcopy_from(VALUE self, VALUE other)
262
260
  km_check_frozen(self);
263
261
  SMAT *dest = km_mat2smat(self), *src = km_mat2smat(other);
264
262
  if ( dest->m!=src->n || dest->n!=src->m ) {
265
- rb_raise(km_eDim, "transposed sizes must be the same, (%d, %d) != (%d, %d)", (dest)->m, (dest)->n, (src)->n, (src)->m);
263
+ rb_raise(km_eDim, "transposed sizes must be the same, (%zu, %zu) != (%zu, %zu)", (dest)->m, (dest)->n, (src)->n, (src)->m);
266
264
  }
267
265
  dest->trans = !dest->trans;
268
- SWAP(int, dest->m, dest->n);
266
+ SWAP(size_t, dest->m, dest->n);
269
267
  if ( dest->vtype == src->vtype ) {
270
268
  if ( ( dest->stype != ST_FULL && ( dest->parent == src->parent || dest->parent == other ) ) || src->parent == self ) {
271
269
  km_smat_copy(dest, km_mat2smat(rb_obj_dup(other)));
@@ -282,7 +280,7 @@ kmm_mat_tcopy_from(VALUE self, VALUE other)
282
280
  );
283
281
  }
284
282
  dest->trans = !dest->trans;
285
- SWAP(int, dest->m, dest->n);
283
+ SWAP(size_t, dest->m, dest->n);
286
284
  return self;
287
285
  }
288
286
 
@@ -295,24 +293,24 @@ kmm_mat__diag(VALUE self)
295
293
  SMAT *sr = km_mat2smat(ret);
296
294
  km_smat_alloc_pbody(sr, MIN(smat->m, smat->n), 1, smat->vtype);
297
295
  if ( smat->stype == ST_RSUB ) {
298
- for ( int i=0; i<sr->m; i++ ) {
296
+ for ( size_t i=0; i<sr->m; i++ ) {
299
297
  sr->pbody[i] = smat->pbody[i+i*(smat->ld)];
300
298
  }
301
299
  } else {
302
300
  VT_SWITCH( smat->vtype,
303
- for ( int i=0; i<sr->m; i++ ) {
301
+ for ( size_t i=0; i<sr->m; i++ ) {
304
302
  sr->pbody[i] = smat->dbody+(i+i*smat->ld);
305
303
  },
306
- for ( int i=0; i<sr->m; i++ ) {
304
+ for ( size_t i=0; i<sr->m; i++ ) {
307
305
  sr->pbody[i] = smat->zbody+(i+i*smat->ld);
308
306
  },
309
- for ( int i=0; i<sr->m; i++ ) {
307
+ for ( size_t i=0; i<sr->m; i++ ) {
310
308
  sr->pbody[i] = smat->ibody+(i+i*smat->ld);
311
309
  },
312
- for ( int i=0; i<sr->m; i++ ) {
310
+ for ( size_t i=0; i<sr->m; i++ ) {
313
311
  sr->pbody[i] = smat->bbody+(i+i*smat->ld);
314
312
  },
315
- for ( int i=0; i<sr->m; i++ ) {
313
+ for ( size_t i=0; i<sr->m; i++ ) {
316
314
  sr->pbody[i] = smat->vbody+(i+i*smat->ld);
317
315
  }
318
316
  );
@@ -333,55 +331,57 @@ kmm_mat__diag_ul(VALUE self, VALUE vk)
333
331
  SMAT *smat = km_mat2smat(self);
334
332
  VALUE ret = km_Mat_alloc(km_cMat);
335
333
  SMAT *sr = km_mat2smat(ret);
336
- int k = NUM2INT(vk);
334
+ const long k = NUM2LONG(vk);
337
335
  if ( k == 0 ) { return kmm_mat__diag(self); }
338
- int len, i_s, j_s;
336
+ long len;
337
+ size_t i_s, j_s;
339
338
  if ( 0 < k ) {
340
- if ( k < (smat->n)-(smat->m) ) {
341
- len = smat->m;
339
+ if ( k < s2l(smat->n)-s2l(smat->m) ) {
340
+ len = s2l(smat->m);
342
341
  } else {
343
- len = smat->n-k;
342
+ len = s2l(smat->n)-k;
344
343
  }
345
344
  if ( smat->trans ) {
346
- i_s = k; j_s = 0;
345
+ i_s = l2s(k); j_s = 0;
347
346
  } else {
348
- i_s = 0; j_s = k;
347
+ i_s = 0; j_s = l2s(k);
349
348
  }
350
349
  } else {
351
- if ( k < (smat->n)-(smat->m) ) {
352
- len = smat->m+k;
350
+ if ( k < s2l(smat->n)-s2l(smat->m) ) {
351
+ len = s2l(smat->m)+k;
353
352
  } else {
354
- len = smat->n;
353
+ len = s2l(smat->n);
355
354
  }
356
355
  if ( smat->trans ) {
357
- i_s = 0; j_s = -k;
356
+ i_s = 0; j_s = l2s(-k);
358
357
  } else {
359
- i_s = -k; j_s = 0;
358
+ i_s = l2s(-k); j_s = 0;
360
359
  }
361
360
  }
362
361
  if ( len <= 0 ) {
363
- rb_raise(rb_eArgError, "given offset %d exceeds range [-%d, %d]", k, smat->m-1, smat->n-1);
362
+ rb_raise(rb_eArgError, "given offset %ld exceeds range [-%ld, %ld]", k, s2l(smat->m)-1, s2l(smat->n)-1);
364
363
  }
365
- km_smat_alloc_pbody(sr, len, 1, smat->vtype);
364
+ const size_t len_s = l2s(len);
365
+ km_smat_alloc_pbody(sr, len_s, 1, smat->vtype);
366
366
  if ( smat->stype == ST_RSUB ) {
367
- for ( int i=0; i<len; i++ ) {
367
+ for ( size_t i=0; i<len_s; i++ ) {
368
368
  sr->pbody[i] = smat->pbody[i_s+i+(j_s+i)*smat->ld];
369
369
  }
370
370
  } else {
371
371
  VT_SWITCH( smat->vtype,
372
- for ( int i=0; i<len; i++ ) {
372
+ for ( size_t i=0; i<len_s; i++ ) {
373
373
  sr->pbody[i] = smat->dbody+(i_s+i+(j_s+i)*smat->ld);
374
374
  },
375
- for ( int i=0; i<len; i++ ) {
375
+ for ( size_t i=0; i<len_s; i++ ) {
376
376
  sr->pbody[i] = smat->zbody+(i_s+i+(j_s+i)*smat->ld);
377
377
  },
378
- for ( int i=0; i<len; i++ ) {
378
+ for ( size_t i=0; i<len_s; i++ ) {
379
379
  sr->pbody[i] = smat->ibody+(i_s+i+(j_s+i)*smat->ld);
380
380
  },
381
- for ( int i=0; i<len; i++ ) {
381
+ for ( size_t i=0; i<len_s; i++ ) {
382
382
  sr->pbody[i] = smat->bbody+(i_s+i+(j_s+i)*smat->ld);
383
383
  },
384
- for ( int i=0; i<len; i++ ) {
384
+ for ( size_t i=0; i<len_s; i++ ) {
385
385
  sr->pbody[i] = smat->vbody+(i_s+i+(j_s+i)*smat->ld);
386
386
  }
387
387
  );
@@ -408,10 +408,10 @@ typedef enum {
408
408
  IT_END
409
409
  } ITSYM;
410
410
  static void
411
- km_index_treatment(VALUE *oidx, ITSYM *itsym, VALUE iidx, int size, bool convert)
411
+ km_index_treatment(VALUE *oidx, ITSYM *itsym, VALUE iidx, size_t size, bool convert)
412
412
  {
413
413
  if ( iidx == Qnil ) {
414
- *oidx = rb_ary_new3(2, INT2NUM(0), INT2NUM(size));
414
+ *oidx = rb_ary_new3(2, INT2NUM(0), ZU2NUM(size));
415
415
  *itsym = IT_SERIAL;
416
416
  } else if ( rb_obj_is_kind_of(iidx, km_cMat) ) {
417
417
  SMAT *si = km_mat2smat(iidx);
@@ -430,9 +430,9 @@ km_index_treatment(VALUE *oidx, ITSYM *itsym, VALUE iidx, int size, bool convert
430
430
  } else if ( si->vtype == VT_BOOL ) {
431
431
  if ( convert && VECTOR_P(si) ) {
432
432
  if ( LENGTH(si) == size ) {
433
- int oi_size = 0;
433
+ size_t oi_size = 0;
434
434
  if ( si->stype == ST_RSUB ) {
435
- for ( int i=0; i<size; i++ ) {
435
+ for ( size_t i=0; i<size; i++ ) {
436
436
  if ( *((si->bpbody)[i]) ) {
437
437
  oi_size++;
438
438
  }
@@ -440,24 +440,24 @@ km_index_treatment(VALUE *oidx, ITSYM *itsym, VALUE iidx, int size, bool convert
440
440
  *oidx = km_Mat(oi_size,1, VT_INT);
441
441
  SMAT *oi = km_mat2smat(*oidx);
442
442
  oi_size = 0;
443
- for ( int i=0; i<size; i++ ) {
443
+ for ( size_t i=0; i<size; i++ ) {
444
444
  if ( *((si->bpbody)[i]) ) {
445
- oi->ibody[oi_size] = i;
445
+ oi->ibody[oi_size] = s2i(i);
446
446
  oi_size++;
447
447
  }
448
448
  }
449
449
  } else {
450
- for ( int i=0; i<size; i++ ) {
450
+ for ( size_t i=0; i<size; i++ ) {
451
451
  if ( (si->bbody)[i] ) {
452
452
  oi_size++;
453
453
  }
454
454
  }
455
- *oidx = km_Mat(oi_size,1, VT_INT);
455
+ *oidx = km_Mat(oi_size, 1, VT_INT);
456
456
  SMAT *oi = km_mat2smat(*oidx);
457
457
  oi_size = 0;
458
- for ( int i=0; i<size; i++ ) {
458
+ for ( size_t i=0; i<size; i++ ) {
459
459
  if ( (si->bbody)[i] ) {
460
- oi->ibody[oi_size] = i;
460
+ oi->ibody[oi_size] = s2i(i);
461
461
  oi_size++;
462
462
  }
463
463
  }
@@ -477,14 +477,14 @@ km_index_treatment(VALUE *oidx, ITSYM *itsym, VALUE iidx, int size, bool convert
477
477
  rb_raise(km_eVT, "float or complex matrix cannot be an index");
478
478
  }
479
479
  } else if ( rb_obj_is_kind_of(iidx, rb_cArray) ) {
480
- int ii_size = (int)RARRAY_LEN(iidx);
480
+ const long ii_size = RARRAY_LEN(iidx), size_l = s2l(size);
481
481
  if ( ii_size == 0 ) {
482
- *oidx = rb_ary_new3(2, INT2NUM(0), INT2NUM(size));
482
+ *oidx = rb_ary_new3(2, INT2NUM(0), ZU2NUM(size));
483
483
  *itsym = IT_SERIAL;
484
484
  } else if ( rb_ary_entry(iidx, 0) == Qtrue || rb_ary_entry(iidx, 0) == Qfalse ) {
485
- if ( ii_size == size ) {
486
- int oi_size = 0;
487
- for ( int i=0; i<size; i++ ) {
485
+ if ( ii_size == size_l ) {
486
+ size_t oi_size = 0;
487
+ for ( long i=0; i<size_l; i++ ) {
488
488
  if ( RTEST(rb_ary_entry(iidx, i)) ) {
489
489
  oi_size++;
490
490
  }
@@ -492,9 +492,9 @@ km_index_treatment(VALUE *oidx, ITSYM *itsym, VALUE iidx, int size, bool convert
492
492
  *oidx = km_Mat(oi_size, 1, VT_INT);
493
493
  SMAT *oi = km_mat2smat(*oidx);
494
494
  oi_size = 0;
495
- for ( int i=0; i<size; i++ ) {
495
+ for ( long i=0; i<size_l; i++ ) {
496
496
  if ( RTEST(rb_ary_entry(iidx, i)) ) {
497
- oi->ibody[oi_size] = i;
497
+ oi->ibody[oi_size] = (int)i;
498
498
  oi_size++;
499
499
  }
500
500
  }
@@ -503,23 +503,23 @@ km_index_treatment(VALUE *oidx, ITSYM *itsym, VALUE iidx, int size, bool convert
503
503
  rb_raise(km_eDim, "boolean-Array-index length must match with object size");
504
504
  }
505
505
  } else {
506
- *oidx = km_Mat(ii_size, 1, VT_INT);
506
+ *oidx = km_Mat(l2s(ii_size), 1, VT_INT);
507
507
  SMAT *oi = km_mat2smat(*oidx);
508
- for ( int i=0; i<ii_size; i++ ) {
509
- (oi->ibody)[i] = mod(NUM2INT(rb_ary_entry(iidx, i)), size);
508
+ for ( long i=0; i<ii_size; i++ ) {
509
+ (oi->ibody)[i] = mod(NUM2INT(rb_ary_entry(iidx, i)), s2i(size));
510
510
  }
511
511
  *itsym = IT_IVEC;
512
512
  }
513
513
  } else if ( rb_obj_is_kind_of(iidx, rb_cInteger) ) {
514
- *oidx = INT2NUM(mod(NUM2INT(iidx), size));
514
+ *oidx = INT2NUM(mod(NUM2INT(iidx), s2i(size)));
515
515
  *itsym = IT_INT;
516
516
  } else if ( rb_obj_is_kind_of(iidx, rb_cRange) ) {
517
- int f = mod(NUM2INT(rb_funcall(iidx, id_first, 0)), size);
517
+ const int f = mod(NUM2INT(rb_funcall(iidx, id_first, 0)), s2i(size));
518
518
  int l = NUM2INT(rb_funcall(iidx, id_last, 0));
519
519
  if ( RTEST(rb_funcall(iidx, id_exclude_end_p, 0)) ) {
520
520
  l--;
521
521
  }
522
- l = mod(l, size);
522
+ l = mod(l, s2i(size));
523
523
  *oidx = rb_ary_new3(2, INT2NUM(f), INT2NUM(l-f+1));
524
524
  *itsym = IT_SERIAL;
525
525
  } else {
@@ -544,7 +544,7 @@ kmm_mat_bracket(int argc, VALUE *argv, VALUE self)
544
544
  if ( cit == IT_INT ) {
545
545
  return kmm_mat_get_value(self, ri, ci);
546
546
  } else if ( cit == IT_SERIAL ) {
547
- return km_Mat_ssub(NUM2INT(ri), NUM2INT(rb_ary_entry(ci, 0)), 1, NUM2INT(rb_ary_entry(ci, 1)), self);
547
+ return km_Mat_ssub(NUM2ZU(ri), NUM2ZU(rb_ary_entry(ci, 0)), 1, NUM2ZU(rb_ary_entry(ci, 1)), self);
548
548
  } else if ( cit == IT_IVEC ) {
549
549
  VALUE riv = km_Mat(1, 1, VT_INT);
550
550
  (km_mat2smat(riv)->ibody)[0] = NUM2INT(ri);
@@ -552,16 +552,16 @@ kmm_mat_bracket(int argc, VALUE *argv, VALUE self)
552
552
  }
553
553
  } else if ( rit == IT_SERIAL ) {
554
554
  if ( cit == IT_INT ) {
555
- return km_Mat_ssub(NUM2INT(rb_ary_entry(ri, 0)), NUM2INT(ci), NUM2INT(rb_ary_entry(ri, 1)), 1, self);
555
+ return km_Mat_ssub(NUM2ZU(rb_ary_entry(ri, 0)), NUM2ZU(ci), NUM2ZU(rb_ary_entry(ri, 1)), 1, self);
556
556
  } else if ( cit == IT_SERIAL ) {
557
557
  return kmm_mat_get_ssub(self, rb_ary_entry(ri, 0), rb_ary_entry(ci, 0), rb_ary_entry(ri, 1), rb_ary_entry(ci, 1));
558
558
  } else if ( cit == IT_IVEC ) {
559
- int ri1 = NUM2INT(rb_ary_entry(ri, 1));
559
+ size_t ri1 = NUM2ZU(rb_ary_entry(ri, 1));
560
560
  VALUE riv = km_Mat(ri1, 1, VT_INT);
561
- int ri0 = NUM2INT(rb_ary_entry(ri, 0));
561
+ size_t ri0 = NUM2ZU(rb_ary_entry(ri, 0));
562
562
  SMAT *sri = km_mat2smat(riv);
563
- for ( int i=0; i<ri1; i++ ) {
564
- (sri->ibody)[i] = i+ri0;
563
+ for ( size_t i=0; i<ri1; i++ ) {
564
+ (sri->ibody)[i] = s2i(i+ri0);
565
565
  }
566
566
  return kmm_mat_get_rsub(self, riv, ci);
567
567
  }
@@ -571,23 +571,23 @@ kmm_mat_bracket(int argc, VALUE *argv, VALUE self)
571
571
  (km_mat2smat(civ)->ibody)[0] = NUM2INT(ci);
572
572
  return kmm_mat_get_rsub(self, ri, civ);
573
573
  } else if ( cit == IT_SERIAL ) {
574
- int ci1 = NUM2INT(rb_ary_entry(ci, 1));
574
+ size_t ci1 = NUM2ZU(rb_ary_entry(ci, 1));
575
575
  VALUE civ = km_Mat(ci1, 1, VT_INT);
576
- int ci0 = NUM2INT(rb_ary_entry(ci, 0));
576
+ size_t ci0 = NUM2ZU(rb_ary_entry(ci, 0));
577
577
  SMAT *sci = km_mat2smat(civ);
578
- for ( int i=0; i<ci1; i++ ) {
579
- (sci->ibody)[i] = i+ci0;
578
+ for ( size_t i=0; i<ci1; i++ ) {
579
+ (sci->ibody)[i] = s2i(i+ci0);
580
580
  }
581
581
  return kmm_mat_get_rsub(self, ri, civ);
582
582
  } else if ( cit == IT_IVEC ) {
583
583
  return kmm_mat_get_rsub(self, ri, ci);
584
584
  }
585
585
  } else if ( rit == IT_ZERO ) {
586
- int cs;
586
+ size_t cs;
587
587
  if ( cit == IT_INT ) {
588
588
  cs = 1;
589
589
  } else if ( cit == IT_SERIAL ) {
590
- cs = NUM2INT(rb_ary_entry(ci, 1));
590
+ cs = NUM2ZU(rb_ary_entry(ci, 1));
591
591
  } else if ( cit == IT_IVEC ) {
592
592
  cs = LENGTH(km_mat2smat(ci));
593
593
  } else if ( cit == IT_ZERO ) {
@@ -607,7 +607,7 @@ kmm_mat_bracket(int argc, VALUE *argv, VALUE self)
607
607
  if ( SAME_SIZE(smat, si) ) {
608
608
  return kmm_mat_get_by_bmat(self, i);
609
609
  } else {
610
- rb_raise(km_eDim, "boolean index size (%d, %d) must be the same as matrix size (%d, %d)", si->m, si->n, smat->m, smat->n);
610
+ rb_raise(km_eDim, "boolean index size (%zu, %zu) must be the same as matrix size (%zu, %zu)", si->m, si->n, smat->m, smat->n);
611
611
  }
612
612
  } else if ( it == IT_INT ) {
613
613
  if ( smat->n == 1 ) {
@@ -615,15 +615,15 @@ kmm_mat_bracket(int argc, VALUE *argv, VALUE self)
615
615
  } else if ( smat->m == 1 ) {
616
616
  return kmm_mat_get_value(self, INT2NUM(0), i);
617
617
  } else {
618
- rb_raise(rb_eArgError, "single integer index is available only for vectors, not for (%d, %d) matricies", smat->m, smat->n);
618
+ rb_raise(rb_eArgError, "single integer index is available only for vectors, not for (%zu, %zu) matricies", smat->m, smat->n);
619
619
  }
620
620
  } else if ( it == IT_SERIAL ) {
621
621
  if ( smat->n == 1 ) {
622
- return km_Mat_ssub(NUM2INT(rb_ary_entry(i, 0)), 0, NUM2INT(rb_ary_entry(i, 1)), 1, self);
622
+ return km_Mat_ssub(NUM2ZU(rb_ary_entry(i, 0)), 0, NUM2ZU(rb_ary_entry(i, 1)), 1, self);
623
623
  } else if ( smat->m == 1 ) {
624
- return km_Mat_ssub(0, NUM2INT(rb_ary_entry(i, 0)), 1, NUM2INT(rb_ary_entry(i, 1)), self);
624
+ return km_Mat_ssub(0, NUM2ZU(rb_ary_entry(i, 0)), 1, NUM2ZU(rb_ary_entry(i, 1)), self);
625
625
  } else {
626
- rb_raise(rb_eArgError, "single serial index is available only for vectors, not for (%d, %d) matricies", smat->m, smat->n);
626
+ rb_raise(rb_eArgError, "single serial index is available only for vectors, not for (%zu, %zu) matricies", smat->m, smat->n);
627
627
  }
628
628
  } else if ( it == IT_IVEC ) {
629
629
  if ( smat->n == 1 ) {
@@ -635,7 +635,7 @@ kmm_mat_bracket(int argc, VALUE *argv, VALUE self)
635
635
  (km_mat2smat(i2)->ibody)[0] = 0;
636
636
  return kmm_mat_get_rsub(self, i2, i);
637
637
  } else {
638
- rb_raise(rb_eArgError, "single ivec index is available only for vectors, not for (%d, %d) matricies", smat->m, smat->n);
638
+ rb_raise(rb_eArgError, "single ivec index is available only for vectors, not for (%zu, %zu) matricies", smat->m, smat->n);
639
639
  }
640
640
  } else if ( it == IT_ZERO ) {
641
641
  if ( smat->n == 1 ) {
@@ -643,7 +643,7 @@ kmm_mat_bracket(int argc, VALUE *argv, VALUE self)
643
643
  } else if ( smat->m == 1 ) {
644
644
  return km_Mat(0, 1, smat->vtype);
645
645
  } else {
646
- rb_raise(rb_eArgError, "single serial index is available only for vectors, not for (%d, %d) matricies", smat->m, smat->n);
646
+ rb_raise(rb_eArgError, "single serial index is available only for vectors, not for (%zu, %zu) matricies", smat->m, smat->n);
647
647
  }
648
648
  } else if ( it == IT_VMAT ) {
649
649
  return kmm_mat_get_rsub2(self, i);
@@ -667,9 +667,9 @@ kmm_mat_bracket_set(int argc, VALUE *argv, VALUE self)
667
667
  }
668
668
  if ( argc == 3 ) {
669
669
  if ( rb_obj_is_kind_of(argv[0], rb_cInteger) && rb_obj_is_kind_of(argv[1], rb_cInteger) ) {
670
- kmm_mat_set_value(self, INT2NUM(mod(NUM2INT(argv[0]), smat->m)), INT2NUM(mod(NUM2INT(argv[1]), smat->n)), argv[2]);
670
+ kmm_mat_set_value(self, INT2NUM(mod(NUM2INT(argv[0]), s2i(smat->m))), INT2NUM(mod(NUM2INT(argv[1]), s2i(smat->n))), argv[2]);
671
671
  } else {
672
- bool flg = smat->may_have_sub;
672
+ const bool flg = smat->may_have_sub;
673
673
  VALUE target = kmm_mat_bracket(2, argv, self);
674
674
  if ( rb_obj_is_kind_of(argv[2], km_cMat) ) {
675
675
  kmm_mat_copy_from(target, argv[2]);
@@ -682,19 +682,19 @@ kmm_mat_bracket_set(int argc, VALUE *argv, VALUE self)
682
682
  } else if ( argc == 2 ) {
683
683
  if ( rb_obj_is_kind_of(argv[0], rb_cInteger) ) {
684
684
  if ( smat->n == 1 ) {
685
- kmm_mat_set_value(self, INT2NUM(mod(NUM2INT(argv[0]), smat->m)), INT2NUM(0), argv[1]);
685
+ kmm_mat_set_value(self, INT2NUM(mod(NUM2INT(argv[0]), s2i(smat->m))), INT2NUM(0), argv[1]);
686
686
  } else if ( smat->m == 1 ) {
687
- kmm_mat_set_value(self, INT2NUM(0), INT2NUM(mod(NUM2INT(argv[0]), smat->n)), argv[1]);
687
+ kmm_mat_set_value(self, INT2NUM(0), INT2NUM(mod(NUM2INT(argv[0]), s2i(smat->n))), argv[1]);
688
688
  } else {
689
- rb_raise(rb_eArgError, "setting value with single integer index is available only for vectors, not (%d, %d) matricies", smat->m, smat->n);
689
+ rb_raise(rb_eArgError, "setting value with single integer index is available only for vectors, not (%zu, %zu) matricies", smat->m, smat->n);
690
690
  }
691
691
  } else {
692
- bool flg = smat->may_have_sub;
692
+ const bool flg = smat->may_have_sub;
693
693
  VALUE target = kmm_mat_bracket(1, argv, self);
694
694
  if ( rb_obj_is_kind_of(argv[1], km_cMat) ) {
695
695
  if ( rb_obj_is_kind_of(argv[0], km_cMat) && km_mat2smat(argv[0])->vtype == VT_BOOL ) {
696
696
  SMAT *smat2 = km_mat2smat(argv[1]);
697
- bool flg2 = smat2->may_have_sub;
697
+ const bool flg2 = smat2->may_have_sub;
698
698
  VALUE vsrc = kmm_mat_bracket(1, argv, argv[1]);
699
699
  kmm_mat_copy_from(target, vsrc);
700
700
  kmm_mat__kill(vsrc);
@@ -1,20 +1,20 @@
1
1
  #include "../kmat.h"
2
2
 
3
3
  static VALUE
4
- km_block(VALUE idx, VALUE ary, VALUE self)
4
+ km_block(RB_BLOCK_CALL_FUNC_ARGLIST(idx, ary))
5
5
  {
6
6
  VALUE row = rb_ary_entry(ary, NUM2LONG(rb_ary_entry(idx, 0)));
7
7
  return rb_ary_entry(row, NUM2LONG(rb_ary_entry(idx, 1)));
8
8
  }
9
9
  static VALUE
10
- km_block_vector(VALUE idx, VALUE ary, VALUE self)
10
+ km_block_vector(RB_BLOCK_CALL_FUNC_ARGLIST(idx, ary))
11
11
  {
12
12
  return rb_ary_entry(ary, NUM2LONG(rb_ary_entry(idx, 0)));
13
13
  }
14
14
  static long
15
15
  km_check_col_size(VALUE ary)
16
16
  {
17
- long ret = RARRAY_LEN(rb_ary_entry(ary, 0));
17
+ const long ret = RARRAY_LEN(rb_ary_entry(ary, 0));
18
18
  for ( long i=1; i<RARRAY_LEN(ary); i++ ) {
19
19
  if ( RARRAY_LEN(rb_ary_entry(ary, i)) != ret ) {
20
20
  rb_raise(km_eDim, "the length of rows must be the same");
@@ -12,8 +12,8 @@ km_rand_normal(VALUE random)
12
12
  return NUM2DBL(rb_ivar_get(random, id_iv_kmat_stored_value));
13
13
  } else {
14
14
  rb_ivar_set(random, id_iv_kmat_stored, Qtrue);
15
- double sqrt_m2_log_x = sqrt(-2.0*log1p(-NUM2DBL(rb_funcall(random, id_rand, 0))));
16
- double two_pi_y = M_2PI*NUM2DBL(rb_funcall(random, id_rand, 0));
15
+ const double sqrt_m2_log_x = sqrt(-2.0*log1p(-NUM2DBL(rb_funcall(random, id_rand, 0))));
16
+ const double two_pi_y = M_2PI*NUM2DBL(rb_funcall(random, id_rand, 0));
17
17
  rb_ivar_set(random, id_iv_kmat_stored_value, rb_float_new(sqrt_m2_log_x*sin(two_pi_y)));
18
18
  return sqrt_m2_log_x*cos(two_pi_y);
19
19
  }
@@ -22,9 +22,9 @@ km_rand_normal(VALUE random)
22
22
  // replace ary[0..(len-1)] by random numbers which follow N(0, 1)
23
23
  // `random' is an instance of Random
24
24
  void
25
- km_fill_normal(int len, double *ary, VALUE random)
25
+ km_fill_normal(size_t len, double *ary, VALUE random)
26
26
  {
27
- int i;
27
+ size_t i;
28
28
  double sqrt_m2_log_x, two_pi_y;
29
29
  len--;
30
30
  if (RTEST(rb_ivar_get(random, id_iv_kmat_stored))) {
@@ -66,7 +66,7 @@ km_random_randn(int argc, VALUE *argv, VALUE random)
66
66
  void
67
67
  km_Mat_rand_init(void)
68
68
  {
69
- kmgv_mat_random = rb_const_get(rb_cRandom, id_DEFAULT);
69
+ kmgv_mat_random = rb_funcall(rb_cRandom, id_new, 0);
70
70
  rb_define_variable("$MatRandom", &kmgv_mat_random);
71
71
  rb_define_method(rb_cRandom, "randn", km_random_randn, -1);
72
72
  }