fast_matrix 0.1.6 → 0.1.7
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.
- checksums.yaml +4 -4
- data/.dockerignore +0 -1
- data/.travis.yml +2 -1
- data/README.md +3 -8
- data/ext/fast_matrix/errors.c +7 -1
- data/ext/fast_matrix/errors.h +3 -1
- data/ext/fast_matrix/matrix.c +82 -47
- data/ext/fast_matrix/vector.c +47 -0
- data/fast_matrix.gemspec +1 -1
- data/lib/fast_matrix/version.rb +1 -1
- data/lib/matrix/matrix.rb +58 -4
- data/lib/vector/vector.rb +20 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f561247c42ab2329857835462917a1a1a3ca520cb262ab89b7cc968c903f5d55
|
4
|
+
data.tar.gz: e606f0ca2ce438c5f0656203fb71ec70d747c8be82efc5090dd9f35fc735204c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3bbb8b3e6c3d1dfaff7de547dadbc559d86318b63e2321303bf821c8b1ed3e350034798cacb47f802b86712f7b7b1bf33cce7d71a0d46bb9a3d36c1687a83fe
|
7
|
+
data.tar.gz: 31c9b5884b15bdcf1d82ade4090f256f30ec0dba5f1394e4b46e390ea4b5dd0df4c829823f668381253122f6cc1e9c8f19c45cfa6e7ce7bb483eec2ed51cbed1
|
data/.dockerignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
# FastMatrix
|
6
6
|
|
7
|
-
Ruby wrapper around C matrices implementation written as exercise by
|
7
|
+
Ruby wrapper around C matrices implementation written as exercise by MMCS students.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -22,14 +22,9 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
$ gem install fast_matrix
|
24
24
|
|
25
|
-
## Usage
|
25
|
+
## Usage and documentation
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
### Differences with the standard matrix
|
30
|
-
* Supported only double values
|
31
|
-
* Can't be empty
|
32
|
-
* Some faster
|
27
|
+
See our [GitHub Wiki](https://github.com/mmcs-ruby/fast_matrix/wiki).
|
33
28
|
|
34
29
|
## Development
|
35
30
|
|
data/ext/fast_matrix/errors.c
CHANGED
@@ -28,10 +28,16 @@ void raise_check_range(int v, int min, int max)
|
|
28
28
|
rb_raise(fm_eIndexError, "Index out of range");
|
29
29
|
}
|
30
30
|
|
31
|
+
void raise_check_rbasic(VALUE v, VALUE rBasic, const char* rbasic_name)
|
32
|
+
{
|
33
|
+
if(RBASIC_CLASS(v) != rBasic)
|
34
|
+
rb_raise(fm_eTypeError, "Expected class %s", rbasic_name);
|
35
|
+
}
|
36
|
+
|
31
37
|
void init_fm_errors()
|
32
38
|
{
|
33
39
|
VALUE mod = rb_define_module("FastMatrix");
|
34
40
|
|
35
41
|
fm_eTypeError = rb_define_class_under(mod, "TypeError", rb_eTypeError);
|
36
42
|
fm_eIndexError = rb_define_class_under(mod, "IndexError", rb_eIndexError);
|
37
|
-
}
|
43
|
+
}
|
data/ext/fast_matrix/errors.h
CHANGED
@@ -12,7 +12,9 @@ double raise_rb_value_to_double(VALUE v);
|
|
12
12
|
int raise_rb_value_to_int(VALUE v);
|
13
13
|
// check if the value is in range and raise an error if not
|
14
14
|
void raise_check_range(int v, int min, int max);
|
15
|
+
// check if the basic class of value is rBasic and raise an error if not
|
16
|
+
void raise_check_rbasic(VALUE v, VALUE rBasic, const char* rbasic_name);
|
15
17
|
|
16
18
|
void init_fm_errors();
|
17
19
|
|
18
|
-
#endif /* FAST_MATRIX_ERRORS_H */
|
20
|
+
#endif /* FAST_MATRIX_ERRORS_H */
|
data/ext/fast_matrix/matrix.c
CHANGED
@@ -125,8 +125,9 @@ void c_matrix_multiply(int n, int k, int m, const double* A, const double* B, do
|
|
125
125
|
{
|
126
126
|
const double* p_b = B + m * t;
|
127
127
|
double d_a = p_a[t];
|
128
|
-
|
129
|
-
|
128
|
+
if(d_a != 0)
|
129
|
+
for(int i = 0; i < m; ++i)
|
130
|
+
p_c[i] += d_a * p_b[i];
|
130
131
|
}
|
131
132
|
}
|
132
133
|
}
|
@@ -190,10 +191,13 @@ void strassen_iteration(int n, int k, int m, const double* A, const double* B, d
|
|
190
191
|
|
191
192
|
bool check_strassen(int m, int n, int k)
|
192
193
|
{
|
193
|
-
return n > 2 && m > 2 && k > 2 && (double)m * (double)n * (double)k >
|
194
|
+
return n > 2 && m > 2 && k > 2 && (double)m * (double)n * (double)k > 1000000;
|
194
195
|
}
|
195
196
|
|
196
|
-
|
197
|
+
// A B
|
198
|
+
// [x x x] [x x x]
|
199
|
+
// [x x x] => [x x x]
|
200
|
+
// [x x x] [x x x]
|
197
201
|
void strassen_copy(int m, int n, const double* A, double* B, int s_a, int s_b)
|
198
202
|
{
|
199
203
|
for(int i = 0; i < n; ++i)
|
@@ -205,6 +209,41 @@ void strassen_copy(int m, int n, const double* A, double* B, int s_a, int s_b)
|
|
205
209
|
}
|
206
210
|
}
|
207
211
|
|
212
|
+
// if right = false and down = false | if right = true and down = false:
|
213
|
+
// A B | A B
|
214
|
+
// [x x x] [x x x] | [x x x] [x x x 0]
|
215
|
+
// [x x x] => [x x x] | [x x x] => [x x x 0]
|
216
|
+
// [x x x] [x x x] | [x x x] [x x x 0]
|
217
|
+
// |
|
218
|
+
// if right = false and down = true | if right = true and down = true:
|
219
|
+
// A B | A B
|
220
|
+
// [x x x] [x x x] | [x x x] [x x x 0]
|
221
|
+
// [x x x] => [x x x] | [x x x] => [x x x 0]
|
222
|
+
// [x x x] [x x x] | [x x x] [x x x 0]
|
223
|
+
// [0 0 0] | [0 0 0 0]
|
224
|
+
void strassen_copy_with_zero(int m, int n, const double* A, double* B, int s_a, int s_b, bool right, bool down)
|
225
|
+
{
|
226
|
+
double* p_B;
|
227
|
+
for(int i = 0; i < n; ++i)
|
228
|
+
{
|
229
|
+
const double* p_A = A + i * s_a;
|
230
|
+
p_B = B + i * s_b;
|
231
|
+
for(int j = 0; j < m; ++j)
|
232
|
+
p_B[j] = p_A[j];
|
233
|
+
if(right)
|
234
|
+
p_B[m] = 0;
|
235
|
+
}
|
236
|
+
|
237
|
+
if(down)
|
238
|
+
{
|
239
|
+
p_B = B + n * s_b;
|
240
|
+
for(int j = 0; j < m; ++j)
|
241
|
+
p_B[j] = 0;
|
242
|
+
if(right)
|
243
|
+
p_B[m] = 0;
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
208
247
|
void strassen_sum_to_first(int m, int n, double* A, const double* B, int s_a, int s_b)
|
209
248
|
{
|
210
249
|
for(int i = 0; i < n; ++i)
|
@@ -253,8 +292,6 @@ void recursive_strassen(int n, int k, int m, const double* A, const double* B, d
|
|
253
292
|
double* P6 = P5 + m1 * n1;
|
254
293
|
double* P7 = P6 + m1 * n1;
|
255
294
|
fill_d_array(7 * m1 * n1, P1, 0);
|
256
|
-
fill_d_array(k1 * n1, termA, 0);
|
257
|
-
fill_d_array(m1 * k1, termB, 0);
|
258
295
|
|
259
296
|
// -----------P1-----------
|
260
297
|
strassen_copy(k1, n1, A, termA, k, k1);
|
@@ -264,55 +301,47 @@ void recursive_strassen(int n, int k, int m, const double* A, const double* B, d
|
|
264
301
|
strassen_sum_to_first(m2, k2, termB, B + m1 + m * k1, m1, m);
|
265
302
|
|
266
303
|
recursive_strassen(n1, k1, m1, termA, termB, P1);
|
267
|
-
fill_d_array(k1 * n1, termA, 0);
|
268
304
|
// -----------P2-----------
|
269
|
-
|
305
|
+
strassen_copy_with_zero(k1, n2, A + k * n1, termA, k, k1, false, n1 != n2);
|
270
306
|
strassen_sum_to_first(k2, n2, termA, A + k1 + k * n1, k1, k);
|
271
307
|
|
272
308
|
strassen_copy(m1, k1, B, termB, m, m1);
|
273
309
|
|
274
310
|
recursive_strassen(n1, k1, m1, termA, termB, P2);
|
275
|
-
fill_d_array(m1 * k1, termB, 0);
|
276
311
|
// -----------P3-----------
|
277
312
|
strassen_copy(k1, n1, A, termA, k, k1);
|
278
313
|
|
279
|
-
|
314
|
+
strassen_copy_with_zero(m2, k1, B + m1, termB, m, m1, m1 != m2, false);
|
280
315
|
strassen_sub_to_first(m2, k2, termB, B + m1 + m * k1, m1, m);
|
281
316
|
|
282
317
|
recursive_strassen(n1, k1, m1, termA, termB, P3);
|
283
|
-
fill_d_array(k1 * n1, termA, 0);
|
284
|
-
fill_d_array(m1 * k1, termB, 0);
|
285
318
|
// -----------P4-----------
|
286
|
-
|
287
|
-
|
288
|
-
|
319
|
+
strassen_copy_with_zero(k2, n2, A + k1 + k * n1, termA, k, k1, k1 != k2, n1 != n2);
|
320
|
+
|
321
|
+
strassen_copy_with_zero(m1, k2, B + m * k1, termB, m, m1, false, k1 != k2);
|
289
322
|
strassen_sub_to_first(m1, k1, termB, B, m1, m);
|
290
323
|
|
291
324
|
recursive_strassen(n1, k1, m1, termA, termB, P4);
|
292
|
-
fill_d_array(m1 * k1, termB, 0);
|
293
325
|
// -----------P5-----------
|
294
326
|
strassen_copy(k1, n1, A, termA, k, k1);
|
295
327
|
strassen_sum_to_first(k2, n1, termA, A + k1, k1, k);
|
296
328
|
|
297
|
-
|
298
|
-
|
329
|
+
strassen_copy_with_zero(m2, k2, B + m1 + m * k1, termB, m, m1, m1 != m2, k1 != k2);
|
330
|
+
|
299
331
|
recursive_strassen(n1, k1, m1, termA, termB, P5);
|
300
|
-
fill_d_array(k1 * n1, termA, 0);
|
301
332
|
// -----------P6-----------
|
302
|
-
|
333
|
+
strassen_copy_with_zero(k1, n2, A + k * n1, termA, k, k1, false, n1 != n2);
|
303
334
|
strassen_sub_to_first(k1, n1, termA, A, k1, k);
|
304
335
|
|
305
336
|
strassen_copy(m1, k1, B, termB, m, m1);
|
306
337
|
strassen_sum_to_first(m2, k1, termB, B + m1, m1, m);
|
307
338
|
|
308
339
|
recursive_strassen(n1, k1, m1, termA, termB, P6);
|
309
|
-
fill_d_array(k1 * n1, termA, 0);
|
310
|
-
fill_d_array(m1 * k1, termB, 0);
|
311
340
|
// -----------P7-----------
|
312
|
-
|
341
|
+
strassen_copy_with_zero(k2, n1, A + k1, termA, k, k1, k1 != k2, false);
|
313
342
|
strassen_sub_to_first(k2, n2, termA, A + k1 + k * n1, k1, k);
|
314
343
|
|
315
|
-
|
344
|
+
strassen_copy_with_zero(m1, k2, B + k1 * m, termB, m, m1, false, k1 != k2);
|
316
345
|
strassen_sum_to_first(m2, k2, termB, B + m1 + m * k1, m1, m);
|
317
346
|
|
318
347
|
recursive_strassen(n1, k1, m1, termA, termB, P7);
|
@@ -412,7 +441,7 @@ VALUE matrix_multiply(VALUE self, VALUE v)
|
|
412
441
|
|| RB_TYPE_P(v, T_BIGNUM))
|
413
442
|
return matrix_multiply_mn(self, v);
|
414
443
|
if(RBASIC_CLASS(v) == cMatrix)
|
415
|
-
return
|
444
|
+
return strassen(self, v);
|
416
445
|
if(RBASIC_CLASS(v) == cVector);
|
417
446
|
return matrix_multiply_mv(self, v);
|
418
447
|
rb_raise(fm_eTypeError, "Invalid klass for multiply");
|
@@ -462,6 +491,7 @@ VALUE transpose(VALUE self)
|
|
462
491
|
|
463
492
|
VALUE matrix_add_with(VALUE self, VALUE value)
|
464
493
|
{
|
494
|
+
raise_check_rbasic(value, cMatrix, "matrix");
|
465
495
|
struct matrix* A;
|
466
496
|
struct matrix* B;
|
467
497
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
@@ -484,6 +514,7 @@ VALUE matrix_add_with(VALUE self, VALUE value)
|
|
484
514
|
|
485
515
|
VALUE matrix_add_from(VALUE self, VALUE value)
|
486
516
|
{
|
517
|
+
raise_check_rbasic(value, cMatrix, "matrix");
|
487
518
|
struct matrix* A;
|
488
519
|
struct matrix* B;
|
489
520
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
@@ -500,9 +531,9 @@ VALUE matrix_add_from(VALUE self, VALUE value)
|
|
500
531
|
return self;
|
501
532
|
}
|
502
533
|
|
503
|
-
|
504
534
|
VALUE matrix_sub_with(VALUE self, VALUE value)
|
505
535
|
{
|
536
|
+
raise_check_rbasic(value, cMatrix, "matrix");
|
506
537
|
struct matrix* A;
|
507
538
|
struct matrix* B;
|
508
539
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
@@ -523,6 +554,25 @@ VALUE matrix_sub_with(VALUE self, VALUE value)
|
|
523
554
|
return result;
|
524
555
|
}
|
525
556
|
|
557
|
+
VALUE matrix_sub_from(VALUE self, VALUE value)
|
558
|
+
{
|
559
|
+
raise_check_rbasic(value, cMatrix, "matrix");
|
560
|
+
struct matrix* A;
|
561
|
+
struct matrix* B;
|
562
|
+
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
563
|
+
TypedData_Get_Struct(value, struct matrix, &matrix_type, B);
|
564
|
+
|
565
|
+
if(A->m != B->m && A->n != B->n)
|
566
|
+
rb_raise(fm_eIndexError, "Different sizes matrices");
|
567
|
+
|
568
|
+
int m = B->m;
|
569
|
+
int n = A->n;
|
570
|
+
|
571
|
+
sub_d_arrays_to_first(n * m, A->data, B->data);
|
572
|
+
|
573
|
+
return self;
|
574
|
+
}
|
575
|
+
|
526
576
|
double determinant(int n, const double* A)
|
527
577
|
{
|
528
578
|
double* M = malloc(n * n * sizeof(double));
|
@@ -559,7 +609,6 @@ VALUE matrix_determinant(VALUE self)
|
|
559
609
|
struct matrix* A;
|
560
610
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
561
611
|
|
562
|
-
|
563
612
|
int m = A->m;
|
564
613
|
int n = A->n;
|
565
614
|
if(m != n)
|
@@ -568,24 +617,6 @@ VALUE matrix_determinant(VALUE self)
|
|
568
617
|
return DBL2NUM(determinant(n, A->data));
|
569
618
|
}
|
570
619
|
|
571
|
-
VALUE matrix_sub_from(VALUE self, VALUE value)
|
572
|
-
{
|
573
|
-
struct matrix* A;
|
574
|
-
struct matrix* B;
|
575
|
-
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
576
|
-
TypedData_Get_Struct(value, struct matrix, &matrix_type, B);
|
577
|
-
|
578
|
-
if(A->m != B->m && A->n != B->n)
|
579
|
-
rb_raise(fm_eIndexError, "Different sizes matrices");
|
580
|
-
|
581
|
-
int m = B->m;
|
582
|
-
int n = A->n;
|
583
|
-
|
584
|
-
sub_d_arrays_to_first(n * m, A->data, B->data);
|
585
|
-
|
586
|
-
return self;
|
587
|
-
}
|
588
|
-
|
589
620
|
VALUE matrix_fill(VALUE self, VALUE value)
|
590
621
|
{
|
591
622
|
double d = raise_rb_value_to_double(value);
|
@@ -599,13 +630,15 @@ VALUE matrix_fill(VALUE self, VALUE value)
|
|
599
630
|
|
600
631
|
VALUE matrix_equal(VALUE self, VALUE value)
|
601
632
|
{
|
633
|
+
if(RBASIC_CLASS(value) != cMatrix)
|
634
|
+
return Qfalse;
|
602
635
|
struct matrix* A;
|
603
636
|
struct matrix* B;
|
604
637
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
605
638
|
TypedData_Get_Struct(value, struct matrix, &matrix_type, B);
|
606
639
|
|
607
640
|
if(A->n != B->n || A->m != B->m)
|
608
|
-
|
641
|
+
return Qfalse;
|
609
642
|
|
610
643
|
int n = A->n;
|
611
644
|
int m = B->m;
|
@@ -634,6 +667,8 @@ VALUE matrix_abs(VALUE self)
|
|
634
667
|
|
635
668
|
VALUE matrix_greater_or_equal(VALUE self, VALUE value)
|
636
669
|
{
|
670
|
+
if(RBASIC_CLASS(value) != cMatrix)
|
671
|
+
rb_raise(fm_eTypeError, "Expected class matrix");
|
637
672
|
struct matrix* A;
|
638
673
|
struct matrix* B;
|
639
674
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
@@ -670,7 +705,7 @@ void init_fm_matrix()
|
|
670
705
|
rb_define_method(cMatrix, "-", matrix_sub_with, 1);
|
671
706
|
rb_define_method(cMatrix, "-=", matrix_sub_from, 1);
|
672
707
|
rb_define_method(cMatrix, "fill!", matrix_fill, 1);
|
673
|
-
rb_define_method(cMatrix, "strassen", strassen, 1);
|
708
|
+
//rb_define_method(cMatrix, "strassen", strassen, 1);
|
674
709
|
rb_define_method(cMatrix, "abs", matrix_abs, 0);
|
675
710
|
rb_define_method(cMatrix, ">=", matrix_greater_or_equal, 1);
|
676
711
|
rb_define_method(cMatrix, "determinant", matrix_determinant, 0);
|
data/ext/fast_matrix/vector.c
CHANGED
@@ -102,6 +102,7 @@ VALUE c_vector_size(VALUE self)
|
|
102
102
|
|
103
103
|
VALUE vector_add_with(VALUE self, VALUE value)
|
104
104
|
{
|
105
|
+
raise_check_rbasic(value, cVector, "vector");
|
105
106
|
struct vector* A;
|
106
107
|
struct vector* B;
|
107
108
|
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
@@ -124,6 +125,7 @@ VALUE vector_add_with(VALUE self, VALUE value)
|
|
124
125
|
|
125
126
|
VALUE vector_add_from(VALUE self, VALUE value)
|
126
127
|
{
|
128
|
+
raise_check_rbasic(value, cVector, "vector");
|
127
129
|
struct vector* A;
|
128
130
|
struct vector* B;
|
129
131
|
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
@@ -139,8 +141,51 @@ VALUE vector_add_from(VALUE self, VALUE value)
|
|
139
141
|
return self;
|
140
142
|
}
|
141
143
|
|
144
|
+
VALUE vector_sub_with(VALUE self, VALUE value)
|
145
|
+
{
|
146
|
+
raise_check_rbasic(value, cVector, "vector");
|
147
|
+
struct vector* A;
|
148
|
+
struct vector* B;
|
149
|
+
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
150
|
+
TypedData_Get_Struct(value, struct vector, &vector_type, B);
|
151
|
+
|
152
|
+
if(A->n != B->n)
|
153
|
+
rb_raise(fm_eIndexError, "Different sizes matrices");
|
154
|
+
|
155
|
+
int n = A->n;
|
156
|
+
|
157
|
+
struct vector* C;
|
158
|
+
VALUE result = TypedData_Make_Struct(cVector, struct vector, &vector_type, C);
|
159
|
+
|
160
|
+
c_vector_init(C, n);
|
161
|
+
sub_d_arrays_to_result(n, A->data, B->data, C->data);
|
162
|
+
|
163
|
+
return result;
|
164
|
+
}
|
165
|
+
|
166
|
+
|
167
|
+
VALUE vector_sub_from(VALUE self, VALUE value)
|
168
|
+
{
|
169
|
+
raise_check_rbasic(value, cVector, "vector");
|
170
|
+
struct vector* A;
|
171
|
+
struct vector* B;
|
172
|
+
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
173
|
+
TypedData_Get_Struct(value, struct vector, &vector_type, B);
|
174
|
+
|
175
|
+
if(A->n != B->n)
|
176
|
+
rb_raise(fm_eIndexError, "Different sizes matrices");
|
177
|
+
|
178
|
+
int n = A->n;
|
179
|
+
|
180
|
+
sub_d_arrays_to_first(n, A->data, B->data);
|
181
|
+
|
182
|
+
return self;
|
183
|
+
}
|
184
|
+
|
142
185
|
VALUE vector_equal(VALUE self, VALUE value)
|
143
186
|
{
|
187
|
+
if(RBASIC_CLASS(value) != cVector)
|
188
|
+
return Qfalse;
|
144
189
|
struct vector* A;
|
145
190
|
struct vector* B;
|
146
191
|
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
@@ -271,6 +316,8 @@ void init_fm_vector()
|
|
271
316
|
rb_define_method(cVector, "size", c_vector_size, 0);
|
272
317
|
rb_define_method(cVector, "+", vector_add_with, 1);
|
273
318
|
rb_define_method(cVector, "+=", vector_add_from, 1);
|
319
|
+
rb_define_method(cVector, "-", vector_sub_with, 1);
|
320
|
+
rb_define_method(cVector, "-=", vector_sub_from, 1);
|
274
321
|
rb_define_method(cVector, "eql?", vector_equal, 1);
|
275
322
|
rb_define_method(cVector, "clone", vector_copy, 0);
|
276
323
|
rb_define_method(cVector, "*", vector_multiply, 1);
|
data/fast_matrix.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
|
11
11
|
spec.summary = %q{Ruby wrapper around C matrices implementation}
|
12
12
|
spec.description = %q{Ruby wrapper around C matrices implementation}
|
13
|
-
spec.homepage = "https://github.com/mmcs-ruby/fast_matrix"
|
13
|
+
spec.homepage = "https://github.com/mmcs-ruby/fast_matrix/wiki"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.metadata["homepage_uri"] = spec.homepage
|
data/lib/fast_matrix/version.rb
CHANGED
data/lib/matrix/matrix.rb
CHANGED
@@ -19,14 +19,26 @@ module FastMatrix
|
|
19
19
|
alias element []
|
20
20
|
alias component []
|
21
21
|
|
22
|
-
def
|
23
|
-
|
22
|
+
def collect
|
23
|
+
collected_rows = []
|
24
|
+
rows.each do |i|
|
25
|
+
collected_rows.push(yield i)
|
26
|
+
end
|
27
|
+
collected_rows
|
24
28
|
end
|
25
29
|
|
26
|
-
|
27
|
-
|
30
|
+
#
|
31
|
+
# Overrides Object#to_s
|
32
|
+
#
|
33
|
+
def to_s
|
34
|
+
"#{self.class}[#{collect do |row|
|
35
|
+
'[' + row.join(', ') + ']'
|
36
|
+
end.join(', ')}]"
|
28
37
|
end
|
29
38
|
|
39
|
+
alias to_str to_s
|
40
|
+
alias inspect to_str
|
41
|
+
|
30
42
|
#
|
31
43
|
# Create fast matrix from standard matrix
|
32
44
|
#
|
@@ -40,14 +52,42 @@ module FastMatrix
|
|
40
52
|
fast_matrix
|
41
53
|
end
|
42
54
|
|
55
|
+
#
|
56
|
+
# Yields all elements of the matrix, starting with those of the first row
|
57
|
+
#
|
58
|
+
# Matrix[ [1,2], [3,4] ].each { |e| puts e }
|
59
|
+
# # => prints the numbers 1 to 4
|
60
|
+
def each(&block)
|
61
|
+
raise NotSupportedError unless block_given?
|
62
|
+
|
63
|
+
each_with_index { |elem, _, _| block.call(elem) }
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Same as #each, but the row index and column index in addition to the element
|
69
|
+
#
|
70
|
+
# Matrix[ [1,2], [3,4] ].each_with_index do |e, row, col|
|
71
|
+
# puts "#{e} at #{row}, #{col}"
|
72
|
+
# end
|
73
|
+
# # => Prints:
|
74
|
+
# # 1 at 0, 0
|
75
|
+
# # 2 at 0, 1
|
76
|
+
# # 3 at 1, 0
|
77
|
+
# # 4 at 1, 1
|
78
|
+
#
|
43
79
|
def each_with_index
|
80
|
+
raise NotSupportedError unless block_given?
|
81
|
+
|
44
82
|
(0...row_count).each do |i|
|
45
83
|
(0...column_count).each do |j|
|
46
84
|
yield self[i, j], i, j
|
47
85
|
end
|
48
86
|
end
|
87
|
+
self
|
49
88
|
end
|
50
89
|
|
90
|
+
# don't use (Issue#1)
|
51
91
|
def each_with_index!
|
52
92
|
(0...row_count).each do |i|
|
53
93
|
(0...column_count).each do |j|
|
@@ -76,5 +116,19 @@ module FastMatrix
|
|
76
116
|
end
|
77
117
|
result
|
78
118
|
end
|
119
|
+
|
120
|
+
private
|
121
|
+
|
122
|
+
def rows
|
123
|
+
rows = []
|
124
|
+
(0...row_count).each do |i|
|
125
|
+
row = []
|
126
|
+
(0...column_count).each do |j|
|
127
|
+
row.push(element(i, j))
|
128
|
+
end
|
129
|
+
rows.push(row)
|
130
|
+
end
|
131
|
+
rows
|
132
|
+
end
|
79
133
|
end
|
80
134
|
end
|
data/lib/vector/vector.rb
CHANGED
@@ -21,12 +21,32 @@ module FastMatrix
|
|
21
21
|
Array.new(size) { |i| self[i] }
|
22
22
|
end
|
23
23
|
|
24
|
+
def to_s
|
25
|
+
"#{self.class}[#{to_ary.join(', ')}]"
|
26
|
+
end
|
27
|
+
alias to_str to_s
|
28
|
+
alias inspect to_str
|
29
|
+
|
30
|
+
#
|
31
|
+
# Iterate over the elements of this vector
|
32
|
+
#
|
33
|
+
def each
|
34
|
+
raise NotSupportedError unless block_given?
|
35
|
+
|
36
|
+
(0...size).each do |i|
|
37
|
+
yield self[i]
|
38
|
+
end
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
24
42
|
def each_with_index
|
25
43
|
(0...size).each do |i|
|
26
44
|
yield self[i], i
|
27
45
|
end
|
46
|
+
self
|
28
47
|
end
|
29
48
|
|
49
|
+
# don't use (Issue#1)
|
30
50
|
def each_with_index!
|
31
51
|
(0...size).each do |i|
|
32
52
|
self[i] = yield self[i], i
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_matrix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mmcs_ruby
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -117,11 +117,11 @@ files:
|
|
117
117
|
- lib/scalar.rb
|
118
118
|
- lib/vector/constructors.rb
|
119
119
|
- lib/vector/vector.rb
|
120
|
-
homepage: https://github.com/mmcs-ruby/fast_matrix
|
120
|
+
homepage: https://github.com/mmcs-ruby/fast_matrix/wiki
|
121
121
|
licenses:
|
122
122
|
- MIT
|
123
123
|
metadata:
|
124
|
-
homepage_uri: https://github.com/mmcs-ruby/fast_matrix
|
124
|
+
homepage_uri: https://github.com/mmcs-ruby/fast_matrix/wiki
|
125
125
|
source_code_uri: https://github.com/mmcs-ruby/fast_matrix
|
126
126
|
post_install_message:
|
127
127
|
rdoc_options: []
|