fast_matrix 0.1.7 → 0.1.66
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.dockerignore +1 -0
- data/.travis.yml +1 -2
- data/README.md +8 -3
- data/ext/fast_matrix/errors.c +1 -7
- data/ext/fast_matrix/errors.h +1 -3
- data/ext/fast_matrix/matrix.c +47 -82
- data/ext/fast_matrix/vector.c +0 -47
- data/fast_matrix.gemspec +1 -1
- data/lib/fast_matrix/version.rb +1 -1
- data/lib/matrix/matrix.rb +4 -58
- data/lib/vector/vector.rb +0 -20
- 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: cfb8428a0aae303ecb09400b55429bc1d388d1f8d168f7583318b3de93cd16f0
|
4
|
+
data.tar.gz: b8c1bafdc23dbfcd02b801fb47f4bcfd7c865b4a545fe813327d3adaf2f41e64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7088df3f843e518dddd2dcabf92a5f8c7fe85fdd390dceb756c8e89f386e8b962e373790605246ab06bdae84be09ce09f92f9c1001a189c18d349f719161497
|
7
|
+
data.tar.gz: 8784f020358a09ffe6aba60391e43cead8d18990e0c0a85841c13390caad7a856aa886ac6051034f16d6fac6b6f05683703253e39ac8d7c6b249a4e4a8681810
|
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 MMCS students.
|
7
|
+
Ruby wrapper around C matrices implementation written as exercise by two MMCS students.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -22,9 +22,14 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
$ gem install fast_matrix
|
24
24
|
|
25
|
-
## Usage
|
25
|
+
## Usage
|
26
26
|
|
27
|
-
|
27
|
+
TODO: Write usage instructions here
|
28
|
+
|
29
|
+
### Differences with the standard matrix
|
30
|
+
* Supported only double values
|
31
|
+
* Can't be empty
|
32
|
+
* Some faster
|
28
33
|
|
29
34
|
## Development
|
30
35
|
|
data/ext/fast_matrix/errors.c
CHANGED
@@ -28,16 +28,10 @@ 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
|
-
|
37
31
|
void init_fm_errors()
|
38
32
|
{
|
39
33
|
VALUE mod = rb_define_module("FastMatrix");
|
40
34
|
|
41
35
|
fm_eTypeError = rb_define_class_under(mod, "TypeError", rb_eTypeError);
|
42
36
|
fm_eIndexError = rb_define_class_under(mod, "IndexError", rb_eIndexError);
|
43
|
-
}
|
37
|
+
}
|
data/ext/fast_matrix/errors.h
CHANGED
@@ -12,9 +12,7 @@ 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);
|
17
15
|
|
18
16
|
void init_fm_errors();
|
19
17
|
|
20
|
-
#endif /* FAST_MATRIX_ERRORS_H */
|
18
|
+
#endif /* FAST_MATRIX_ERRORS_H */
|
data/ext/fast_matrix/matrix.c
CHANGED
@@ -125,9 +125,8 @@ 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
|
-
|
130
|
-
p_c[i] += d_a * p_b[i];
|
128
|
+
for(int i = 0; i < m; ++i)
|
129
|
+
p_c[i] += d_a * p_b[i];
|
131
130
|
}
|
132
131
|
}
|
133
132
|
}
|
@@ -191,13 +190,10 @@ void strassen_iteration(int n, int k, int m, const double* A, const double* B, d
|
|
191
190
|
|
192
191
|
bool check_strassen(int m, int n, int k)
|
193
192
|
{
|
194
|
-
return n > 2 && m > 2 && k > 2 && (double)m * (double)n * (double)k >
|
193
|
+
return n > 2 && m > 2 && k > 2 && (double)m * (double)n * (double)k > 100000000;
|
195
194
|
}
|
196
195
|
|
197
|
-
|
198
|
-
// [x x x] [x x x]
|
199
|
-
// [x x x] => [x x x]
|
200
|
-
// [x x x] [x x x]
|
196
|
+
|
201
197
|
void strassen_copy(int m, int n, const double* A, double* B, int s_a, int s_b)
|
202
198
|
{
|
203
199
|
for(int i = 0; i < n; ++i)
|
@@ -209,41 +205,6 @@ void strassen_copy(int m, int n, const double* A, double* B, int s_a, int s_b)
|
|
209
205
|
}
|
210
206
|
}
|
211
207
|
|
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
|
-
|
247
208
|
void strassen_sum_to_first(int m, int n, double* A, const double* B, int s_a, int s_b)
|
248
209
|
{
|
249
210
|
for(int i = 0; i < n; ++i)
|
@@ -292,6 +253,8 @@ void recursive_strassen(int n, int k, int m, const double* A, const double* B, d
|
|
292
253
|
double* P6 = P5 + m1 * n1;
|
293
254
|
double* P7 = P6 + m1 * n1;
|
294
255
|
fill_d_array(7 * m1 * n1, P1, 0);
|
256
|
+
fill_d_array(k1 * n1, termA, 0);
|
257
|
+
fill_d_array(m1 * k1, termB, 0);
|
295
258
|
|
296
259
|
// -----------P1-----------
|
297
260
|
strassen_copy(k1, n1, A, termA, k, k1);
|
@@ -301,47 +264,55 @@ void recursive_strassen(int n, int k, int m, const double* A, const double* B, d
|
|
301
264
|
strassen_sum_to_first(m2, k2, termB, B + m1 + m * k1, m1, m);
|
302
265
|
|
303
266
|
recursive_strassen(n1, k1, m1, termA, termB, P1);
|
267
|
+
fill_d_array(k1 * n1, termA, 0);
|
304
268
|
// -----------P2-----------
|
305
|
-
|
269
|
+
strassen_copy(k1, n2, A + k * n1, termA, k, k1);
|
306
270
|
strassen_sum_to_first(k2, n2, termA, A + k1 + k * n1, k1, k);
|
307
271
|
|
308
272
|
strassen_copy(m1, k1, B, termB, m, m1);
|
309
273
|
|
310
274
|
recursive_strassen(n1, k1, m1, termA, termB, P2);
|
275
|
+
fill_d_array(m1 * k1, termB, 0);
|
311
276
|
// -----------P3-----------
|
312
277
|
strassen_copy(k1, n1, A, termA, k, k1);
|
313
278
|
|
314
|
-
|
279
|
+
strassen_copy(m2, k1, B + m1, termB, m, m1);
|
315
280
|
strassen_sub_to_first(m2, k2, termB, B + m1 + m * k1, m1, m);
|
316
281
|
|
317
282
|
recursive_strassen(n1, k1, m1, termA, termB, P3);
|
283
|
+
fill_d_array(k1 * n1, termA, 0);
|
284
|
+
fill_d_array(m1 * k1, termB, 0);
|
318
285
|
// -----------P4-----------
|
319
|
-
|
320
|
-
|
321
|
-
|
286
|
+
strassen_copy(k2, n2, A + k1 + k * n1, termA, k, k1);
|
287
|
+
|
288
|
+
strassen_copy(m1, k2, B + m * k1, termB, m, m1);
|
322
289
|
strassen_sub_to_first(m1, k1, termB, B, m1, m);
|
323
290
|
|
324
291
|
recursive_strassen(n1, k1, m1, termA, termB, P4);
|
292
|
+
fill_d_array(m1 * k1, termB, 0);
|
325
293
|
// -----------P5-----------
|
326
294
|
strassen_copy(k1, n1, A, termA, k, k1);
|
327
295
|
strassen_sum_to_first(k2, n1, termA, A + k1, k1, k);
|
328
296
|
|
329
|
-
|
330
|
-
|
297
|
+
strassen_copy(m2, k2, B + m1 + m * k1, termB, m, m1);
|
298
|
+
|
331
299
|
recursive_strassen(n1, k1, m1, termA, termB, P5);
|
300
|
+
fill_d_array(k1 * n1, termA, 0);
|
332
301
|
// -----------P6-----------
|
333
|
-
|
302
|
+
strassen_copy(k1, n2, A + k * n1, termA, k, k1);
|
334
303
|
strassen_sub_to_first(k1, n1, termA, A, k1, k);
|
335
304
|
|
336
305
|
strassen_copy(m1, k1, B, termB, m, m1);
|
337
306
|
strassen_sum_to_first(m2, k1, termB, B + m1, m1, m);
|
338
307
|
|
339
308
|
recursive_strassen(n1, k1, m1, termA, termB, P6);
|
309
|
+
fill_d_array(k1 * n1, termA, 0);
|
310
|
+
fill_d_array(m1 * k1, termB, 0);
|
340
311
|
// -----------P7-----------
|
341
|
-
|
312
|
+
strassen_copy(k2, n1, A + k1, termA, k, k1);
|
342
313
|
strassen_sub_to_first(k2, n2, termA, A + k1 + k * n1, k1, k);
|
343
314
|
|
344
|
-
|
315
|
+
strassen_copy(m1, k2, B + k1 * m, termB, m, m1);
|
345
316
|
strassen_sum_to_first(m2, k2, termB, B + m1 + m * k1, m1, m);
|
346
317
|
|
347
318
|
recursive_strassen(n1, k1, m1, termA, termB, P7);
|
@@ -441,7 +412,7 @@ VALUE matrix_multiply(VALUE self, VALUE v)
|
|
441
412
|
|| RB_TYPE_P(v, T_BIGNUM))
|
442
413
|
return matrix_multiply_mn(self, v);
|
443
414
|
if(RBASIC_CLASS(v) == cMatrix)
|
444
|
-
return
|
415
|
+
return matrix_multiply_mm(self, v);
|
445
416
|
if(RBASIC_CLASS(v) == cVector);
|
446
417
|
return matrix_multiply_mv(self, v);
|
447
418
|
rb_raise(fm_eTypeError, "Invalid klass for multiply");
|
@@ -491,7 +462,6 @@ VALUE transpose(VALUE self)
|
|
491
462
|
|
492
463
|
VALUE matrix_add_with(VALUE self, VALUE value)
|
493
464
|
{
|
494
|
-
raise_check_rbasic(value, cMatrix, "matrix");
|
495
465
|
struct matrix* A;
|
496
466
|
struct matrix* B;
|
497
467
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
@@ -514,7 +484,6 @@ VALUE matrix_add_with(VALUE self, VALUE value)
|
|
514
484
|
|
515
485
|
VALUE matrix_add_from(VALUE self, VALUE value)
|
516
486
|
{
|
517
|
-
raise_check_rbasic(value, cMatrix, "matrix");
|
518
487
|
struct matrix* A;
|
519
488
|
struct matrix* B;
|
520
489
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
@@ -531,9 +500,9 @@ VALUE matrix_add_from(VALUE self, VALUE value)
|
|
531
500
|
return self;
|
532
501
|
}
|
533
502
|
|
503
|
+
|
534
504
|
VALUE matrix_sub_with(VALUE self, VALUE value)
|
535
505
|
{
|
536
|
-
raise_check_rbasic(value, cMatrix, "matrix");
|
537
506
|
struct matrix* A;
|
538
507
|
struct matrix* B;
|
539
508
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
@@ -554,25 +523,6 @@ VALUE matrix_sub_with(VALUE self, VALUE value)
|
|
554
523
|
return result;
|
555
524
|
}
|
556
525
|
|
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
|
-
|
576
526
|
double determinant(int n, const double* A)
|
577
527
|
{
|
578
528
|
double* M = malloc(n * n * sizeof(double));
|
@@ -609,6 +559,7 @@ VALUE matrix_determinant(VALUE self)
|
|
609
559
|
struct matrix* A;
|
610
560
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
611
561
|
|
562
|
+
|
612
563
|
int m = A->m;
|
613
564
|
int n = A->n;
|
614
565
|
if(m != n)
|
@@ -617,6 +568,24 @@ VALUE matrix_determinant(VALUE self)
|
|
617
568
|
return DBL2NUM(determinant(n, A->data));
|
618
569
|
}
|
619
570
|
|
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
|
+
|
620
589
|
VALUE matrix_fill(VALUE self, VALUE value)
|
621
590
|
{
|
622
591
|
double d = raise_rb_value_to_double(value);
|
@@ -630,15 +599,13 @@ VALUE matrix_fill(VALUE self, VALUE value)
|
|
630
599
|
|
631
600
|
VALUE matrix_equal(VALUE self, VALUE value)
|
632
601
|
{
|
633
|
-
if(RBASIC_CLASS(value) != cMatrix)
|
634
|
-
return Qfalse;
|
635
602
|
struct matrix* A;
|
636
603
|
struct matrix* B;
|
637
604
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
638
605
|
TypedData_Get_Struct(value, struct matrix, &matrix_type, B);
|
639
606
|
|
640
607
|
if(A->n != B->n || A->m != B->m)
|
641
|
-
|
608
|
+
return Qfalse;
|
642
609
|
|
643
610
|
int n = A->n;
|
644
611
|
int m = B->m;
|
@@ -667,8 +634,6 @@ VALUE matrix_abs(VALUE self)
|
|
667
634
|
|
668
635
|
VALUE matrix_greater_or_equal(VALUE self, VALUE value)
|
669
636
|
{
|
670
|
-
if(RBASIC_CLASS(value) != cMatrix)
|
671
|
-
rb_raise(fm_eTypeError, "Expected class matrix");
|
672
637
|
struct matrix* A;
|
673
638
|
struct matrix* B;
|
674
639
|
TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
|
@@ -705,7 +670,7 @@ void init_fm_matrix()
|
|
705
670
|
rb_define_method(cMatrix, "-", matrix_sub_with, 1);
|
706
671
|
rb_define_method(cMatrix, "-=", matrix_sub_from, 1);
|
707
672
|
rb_define_method(cMatrix, "fill!", matrix_fill, 1);
|
708
|
-
|
673
|
+
rb_define_method(cMatrix, "strassen", strassen, 1);
|
709
674
|
rb_define_method(cMatrix, "abs", matrix_abs, 0);
|
710
675
|
rb_define_method(cMatrix, ">=", matrix_greater_or_equal, 1);
|
711
676
|
rb_define_method(cMatrix, "determinant", matrix_determinant, 0);
|
data/ext/fast_matrix/vector.c
CHANGED
@@ -102,7 +102,6 @@ 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");
|
106
105
|
struct vector* A;
|
107
106
|
struct vector* B;
|
108
107
|
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
@@ -125,7 +124,6 @@ VALUE vector_add_with(VALUE self, VALUE value)
|
|
125
124
|
|
126
125
|
VALUE vector_add_from(VALUE self, VALUE value)
|
127
126
|
{
|
128
|
-
raise_check_rbasic(value, cVector, "vector");
|
129
127
|
struct vector* A;
|
130
128
|
struct vector* B;
|
131
129
|
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
@@ -141,51 +139,8 @@ VALUE vector_add_from(VALUE self, VALUE value)
|
|
141
139
|
return self;
|
142
140
|
}
|
143
141
|
|
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
|
-
|
185
142
|
VALUE vector_equal(VALUE self, VALUE value)
|
186
143
|
{
|
187
|
-
if(RBASIC_CLASS(value) != cVector)
|
188
|
-
return Qfalse;
|
189
144
|
struct vector* A;
|
190
145
|
struct vector* B;
|
191
146
|
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
@@ -316,8 +271,6 @@ void init_fm_vector()
|
|
316
271
|
rb_define_method(cVector, "size", c_vector_size, 0);
|
317
272
|
rb_define_method(cVector, "+", vector_add_with, 1);
|
318
273
|
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);
|
321
274
|
rb_define_method(cVector, "eql?", vector_equal, 1);
|
322
275
|
rb_define_method(cVector, "clone", vector_copy, 0);
|
323
276
|
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"
|
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,25 +19,13 @@ module FastMatrix
|
|
19
19
|
alias element []
|
20
20
|
alias component []
|
21
21
|
|
22
|
-
def collect
|
23
|
-
collected_rows = []
|
24
|
-
rows.each do |i|
|
25
|
-
collected_rows.push(yield i)
|
26
|
-
end
|
27
|
-
collected_rows
|
28
|
-
end
|
29
|
-
|
30
|
-
#
|
31
|
-
# Overrides Object#to_s
|
32
|
-
#
|
33
22
|
def to_s
|
34
|
-
|
35
|
-
'[' + row.join(', ') + ']'
|
36
|
-
end.join(', ')}]"
|
23
|
+
convert.to_s
|
37
24
|
end
|
38
25
|
|
39
|
-
|
40
|
-
|
26
|
+
def inspect
|
27
|
+
convert.inspect
|
28
|
+
end
|
41
29
|
|
42
30
|
#
|
43
31
|
# Create fast matrix from standard matrix
|
@@ -52,42 +40,14 @@ module FastMatrix
|
|
52
40
|
fast_matrix
|
53
41
|
end
|
54
42
|
|
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
|
-
#
|
79
43
|
def each_with_index
|
80
|
-
raise NotSupportedError unless block_given?
|
81
|
-
|
82
44
|
(0...row_count).each do |i|
|
83
45
|
(0...column_count).each do |j|
|
84
46
|
yield self[i, j], i, j
|
85
47
|
end
|
86
48
|
end
|
87
|
-
self
|
88
49
|
end
|
89
50
|
|
90
|
-
# don't use (Issue#1)
|
91
51
|
def each_with_index!
|
92
52
|
(0...row_count).each do |i|
|
93
53
|
(0...column_count).each do |j|
|
@@ -116,19 +76,5 @@ module FastMatrix
|
|
116
76
|
end
|
117
77
|
result
|
118
78
|
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
|
133
79
|
end
|
134
80
|
end
|
data/lib/vector/vector.rb
CHANGED
@@ -21,32 +21,12 @@ 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
|
-
|
42
24
|
def each_with_index
|
43
25
|
(0...size).each do |i|
|
44
26
|
yield self[i], i
|
45
27
|
end
|
46
|
-
self
|
47
28
|
end
|
48
29
|
|
49
|
-
# don't use (Issue#1)
|
50
30
|
def each_with_index!
|
51
31
|
(0...size).each do |i|
|
52
32
|
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.66
|
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-10-29 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
|
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
|
125
125
|
source_code_uri: https://github.com/mmcs-ruby/fast_matrix
|
126
126
|
post_install_message:
|
127
127
|
rdoc_options: []
|