/
nummatrix2d.pmc
1279 lines (990 loc) · 35.6 KB
/
nummatrix2d.pmc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pla_matrix_types.h"
#include <math.h>
#include <cblas.h>
#define ALLOCATE_STORAGE(s) (FLOATVAL *)mem_sys_allocate_zeroed(s * sizeof (FLOATVAL))
#define PLATYPENAME "NumMatrix2D"
#define DECLATTRS(p, a) Parrot_NumMatrix2D_attributes * const (a) = \
(Parrot_NumMatrix2D_attributes *)((p)->data)
/* Relative error between two large numbers */
#define FLOATVAL_EPSILON_REL 0.0001
/* Absolute difference between two small numbers */
#define FLOATVAL_EPSILON_ABS 0.00001
/* Above we do relative error, below we use abs */
#define FLOATVAL_REL_THRESHOLD 10
/* Resize the matrix internal storage to be able to hold a point at position
(x, y). The matrix grows but does not shrink. New spaces in the matrix
are initialized to 0.0. Parameters x and y are the indices that are trying
to be accessed, so we must resize the matrix to be able to accomodate those
indices. Notice that the matrix type is zero-indexed, so the size is one
plus the highest index that we need to access.
This function will not shrink the matrix, only grow it. After the call,
the matrix will be at least large enough to hold an item at the given
index. To shrink the matrix, use the get_block() method, or that
algorithm. */
static void
resize_matrix(PARROT_INTERP, PMC * self, INTVAL row, INTVAL col)
{
DECLATTRS(self, attrs);
/* Store the old values */
const INTVAL old_rows = attrs->rows;
const INTVAL old_cols = attrs->cols;
FLOATVAL * old_s = attrs->storage;
/* rows and cols are indices, not sizes. Resize the matrix to accomodate
this new point without shrinking by taking the max. */
const INTVAL new_rows = INDEX_MAX(old_rows, row + 1);
const INTVAL new_cols = INDEX_MAX(old_cols, col + 1);
const INTVAL newsize = new_rows * new_cols;
FLOATVAL * new_s = ALLOCATE_STORAGE(newsize);
INTVAL i, j;
for (i = 0; i < old_rows; i++) {
for (j = 0; j < old_cols; j++) {
ITEM_XY_ROWMAJOR(new_s, new_rows, new_cols, i, j) =
ITEM_XY_ROWMAJOR(old_s, old_rows, old_cols, i, j);
}
}
attrs->storage = new_s;
attrs->rows = new_rows;
attrs->cols = new_cols;
if (old_s)
mem_sys_free(old_s);
}
/* If the matrix is lazily transposed, actually transpose the physical memory
layout. This is necessary for calculations, especially BLAS calculations,
which aren't lazy-transpose-aware. */
static void
normalize_lazy_transpose(PARROT_INTERP, PMC * self)
{
DECLATTRS(self, attrs);
if (IS_TRANSPOSED(attrs->flags)) {
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
const INTVAL size = rows_size * cols_size;
FLOATVAL * const new_s = ALLOCATE_STORAGE(size);
FLOATVAL * const old_s = attrs->storage;
INTVAL i, j;
for (i = 0; i < rows_size; ++i) {
for (j = 0; j < cols_size; ++j) {
ITEM_XY_COLMAJOR(new_s, rows_size, cols_size, i, j) =
ITEM_XY_ROWMAJOR(old_s, rows_size, cols_size, i, j);
}
}
attrs->storage = new_s;
mem_sys_free(old_s);
FLAG_CLEAR(attrs->flags, FLAG_TRANSPOSED);
}
}
/* Initialize the PMC from an array, filling the matrix row-at-a-time */
static void
init_from_pmc_array(PARROT_INTERP, PMC * self, INTVAL rows_size,
INTVAL cols_size, PMC * values)
{
DECLATTRS(self, attrs);
FLOATVAL * s;
INTVAL self_rows, self_cols, i, j, num = 0;
const INTVAL init_elems = VTABLE_elements(interp, values);
resize_matrix(interp, self, rows_size - 1, cols_size - 1);
self_rows = attrs->rows;
self_cols = attrs->cols;
s = attrs->storage;
for (i = 0; i < rows_size; i++) {
for (j = 0; j < cols_size; j++) {
const FLOATVAL value = VTABLE_get_number_keyed_int(interp, values, num);
num++;
ITEM_XY_ROWMAJOR(s, self_rows, self_cols, i, j) = value;
if (num >= init_elems)
return;
}
}
}
/* Add a scalar to every element of the matrix */
static void
add_scalar_float(PARROT_INTERP, PMC * self, FLOATVAL v)
{
DECLATTRS(self, attrs);
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
FLOATVAL * const s = attrs->storage;
INTVAL i, j;
for (i = 0; i < rows_size; i++) {
for (j = 0; j < cols_size; j++) {
ITEM_XY_ROWMAJOR(s, rows_size, cols_size, i, j) += v;
}
}
}
/* Multiply every entry by a scalar */
static void
multiply_scalar_float(PARROT_INTERP, PMC * self, FLOATVAL v)
{
DECLATTRS(self, attrs);
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
FLOATVAL * const s = attrs->storage;
INTVAL i, j;
for (i = 0; i < rows_size; i++) {
for (j = 0; j < cols_size; j++) {
ITEM_XY_ROWMAJOR(s, rows_size, cols_size, i, j) *= v;
}
}
}
/* item-by-item addition or subtraction
A = A + B */
static void
add_matrices(PARROT_INTERP, PMC * A, PMC * B, INTVAL sub)
{
DECLATTRS(A, attrs_a);
DECLATTRS(B, attrs_b);
const INTVAL rows = attrs_a->rows;
const INTVAL cols = attrs_a->cols;
FLOATVAL * const s_a = attrs_a->storage;
FLOATVAL * const s_b = attrs_b->storage;
INTVAL i, j;
if (attrs_b->rows != rows || attrs_b->cols != cols)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": sizes do not match");
if (sub) {
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
ITEM_XY_ROWMAJOR(s_a, rows, cols, i, j) -=
ITEM_XY_ROWMAJOR(s_b, rows, cols, i, j);
}
}
}
else {
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
ITEM_XY_ROWMAJOR(s_a, rows, cols, i, j) +=
ITEM_XY_ROWMAJOR(s_b, rows, cols, i, j);
}
}
}
}
/* Wrapper to call the dgemm function from BLAS with PMC arguments. Assumes
A, B, and C are all NumMatrix2D. */
static void
call_gemm(PARROT_INTERP, FLOATVAL alpha, PMC * A, PMC *B, FLOATVAL beta, PMC *C)
{
DECLATTRS(A, attrs_a);
DECLATTRS(B, attrs_b);
DECLATTRS(C, attrs_c);
const INTVAL M = attrs_a->rows;
const INTVAL N = attrs_b->cols;
const INTVAL K = attrs_a->cols;
if (attrs_c->rows != M)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": A, C indices do not match in gemm");
if (attrs_c->cols != N)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": B, C indices do not match in gemm");
if (attrs_b->rows != K)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": A, B indeces do not match in gemm");
cblas_dgemm(CblasRowMajor,
IS_TRANSPOSED_BLAS(attrs_a->flags),
IS_TRANSPOSED_BLAS(attrs_b->flags),
M,
N,
K,
alpha,
attrs_a->storage,
M,
attrs_b->storage,
N,
beta,
attrs_c->storage,
M
);
}
/* Check if two floats are equalish. Above a certain threshold we want to do a
relative error calculation because precision between subsequent large
numbers can be much larger than standard epsilon values. Below the threshold
we want to do an absolute calculation because as the values approach zero the
error value quickly begins to diverge, even for very similar values. The
values for absolute difference, relative error, and threshold need to be
tuned. */
static INTVAL
floats_are_equal(FLOATVAL a, FLOATVAL b)
{
FLOATVAL diff = fabs(a - b);
if (a == b)
return 1;
if (diff > FLOATVAL_EPSILON_ABS)
return 0;
if (a > FLOATVAL_REL_THRESHOLD) {
FLOATVAL err = diff / b;
if (err > FLOATVAL_EPSILON_REL)
return 0;
}
return 1;
}
pmclass NumMatrix2D dynpmc auto_attrs provides matrix provides numericmatrix {
ATTR FLOATVAL * storage;
ATTR INTVAL rows;
ATTR INTVAL cols;
ATTR INTVAL flags;
/*
=head1 VTABLEs
=over 4
=item* init
Create a new NumMatrix2D
=item* destroy
Destrow the matrix and free it's allocated storage
=cut
*/
VTABLE void init() {
DECLATTRS(SELF, a);
a->storage = NULL;
a->rows = 0;
a->cols = 0;
a->flags = 0;
PObj_custom_destroy_SET(SELF);
}
VTABLE void destroy() {
DECLATTRS(SELF, a);
FLOATVAL * const s = a->storage;
if (s)
mem_sys_free(s);
}
/*
=item* get_number_keyed
Get the number at the location X, Y. The key must have two elements.
=item* get_integer_keyed
Get the integer at the location X, Y. The key must have two elements.
=item* get_string_keyed
Get a string representation of the number at the location X, Y. The key must
have two elements.
=item* get_pmc_keyed
Get a Float PMC of the number at the location X, Y. The key must have two
elements.
=cut
*/
VTABLE FLOATVAL get_number_keyed(PMC * key) {
DECLATTRS(SELF, attrs);
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
INTVAL rows, cols;
GET_KEY_INDICES_ROWMAJOR(INTERP, key, rows, cols);
if (rows >= rows_size || cols >= cols_size)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": indices out of bounds");
return ITEM_XY(attrs->storage, attrs->flags, rows_size, cols_size, rows, cols);
}
VTABLE INTVAL get_integer_keyed(PMC * key) {
const FLOATVAL f = VTABLE_get_number_keyed(INTERP, SELF, key);
return (INTVAL)f;
}
VTABLE STRING * get_string_keyed(PMC * key) {
const FLOATVAL f = VTABLE_get_number_keyed(INTERP, SELF, key);
return Parrot_str_from_num(INTERP, f);
}
VTABLE PMC * get_pmc_keyed(PMC * key) {
const FLOATVAL f = VTABLE_get_number_keyed(INTERP, SELF, key);
PMC * const item = Parrot_pmc_new(INTERP, enum_class_Float);
VTABLE_set_number_native(INTERP, item, f);
return item;
}
/*
=item* get_number_keyed_int
Treating the matrix memory block like a C array, get the Nth number. The
array is arranged in memory row by row.
=item* get_integer_keyed_int
Treating the matrix memory block like a C array, get the Nth integer. The
array is arranged in memory row by row.
=item* get_string_keyed_int
Treating the matrix memory block like a C array, get the string representation
of the Nth number. The array is arranged in memory row by row.
=item* get_pmc_keyed_int
Treating the matrix memory block like a C array, get a Float PMC for the
Nth number. The array is arranged in memory row by row.
=cut
*/
VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
DECLATTRS(SELF, attrs);
const INTVAL total_size = attrs->rows * attrs->cols;
if (key >= total_size) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": Matrix dimensions must match in add.");
}
return attrs->storage[key];
}
VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
const FLOATVAL f = VTABLE_get_number_keyed_int(INTERP, SELF, key);
return (INTVAL)f;
}
VTABLE STRING * get_string_keyed_int(INTVAL key) {
const FLOATVAL f = VTABLE_get_number_keyed_int(INTERP, SELF, key);
return Parrot_str_from_num(INTERP, f);
}
VTABLE PMC * get_pmc_keyed_int(INTVAL key) {
const FLOATVAL f = VTABLE_get_number_keyed_int(INTERP, SELF, key);
PMC * const item = Parrot_pmc_new(INTERP, enum_class_Float);
VTABLE_set_number_native(INTERP, item, f);
return item;
}
/*
=item* set_number_keyed
Set the number at position (X, Y), growing the matrix if necessary and padding
empty spaces with 0.0. The key must have two elements.
=item* set_integer_keyed
Set the integer at position (X, Y), growing the matrix if necessary and
padding empty spaces with 0.0. The key must have two elements.
=item* set_pmc_keyed
Get the numeric value from the PMC argument and set it at position (X, Y),
growing the matrix if necessary and padding empty spaces with 0.0. The key
must have two elements.
=cut
*/
VTABLE void set_number_keyed(PMC * key, FLOATVAL value) {
DECLATTRS(SELF, attrs);
INTVAL rows, cols, rows_size = attrs->rows, cols_size = attrs->cols;
GET_KEY_INDICES_ROWMAJOR(INTERP, key, rows, cols);
if (rows >= rows_size || cols >= cols_size) {
resize_matrix(INTERP, SELF, rows, cols);
rows_size = attrs->rows;
cols_size = attrs->cols;
}
ITEM_XY(attrs->storage, attrs->flags, rows_size, cols_size, rows, cols) = value;
}
VTABLE void set_integer_keyed(PMC * key, INTVAL value) {
VTABLE_set_number_keyed(INTERP, SELF, key, (FLOATVAL)value);
}
VTABLE void set_pmc_keyed(PMC * key, PMC * value) {
const FLOATVAL v = VTABLE_get_number(INTERP, value);
VTABLE_set_number_keyed(INTERP, SELF, key, v);
}
/*
=item* get_string
Get a string representation of the matrix.
=cut
*/
VTABLE STRING *get_string() {
DECLATTRS(SELF, attrs);
INTVAL i, j;
PMC * const builder = Parrot_pmc_new(INTERP, enum_class_StringBuilder);
STRING * const newline = Parrot_str_new(INTERP, "\n", 1);
STRING * const tab = Parrot_str_new(INTERP, "\t", 1);
FLOATVAL * const s = attrs->storage;
const INTVAL rows = attrs->rows;
const INTVAL cols = attrs->cols;
const INTVAL flags = attrs->flags;
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
const FLOATVAL f = ITEM_XY(s, flags, rows, cols, i, j);
STRING * const item = Parrot_str_from_num(INTERP, f);
VTABLE_push_string(INTERP, builder, tab);
VTABLE_push_string(INTERP, builder, item);
}
VTABLE_push_string(INTERP, builder, newline);
}
return VTABLE_get_string(INTERP, builder);
}
/*
=item* add(NumMatrix)
Add two matrices together, element-by-element.
=item* add(DEFAULT)
Take the number representation of the argument PMC and add it to every element
in the matrix.
=item* add_float
Add the float value to every element in the matrix.
=item* add_int
Add the integer value to every element in the matrix.
=cut
*/
MULTI PMC *add(NumMatrix2D *value, PMC *dest) {
dest = VTABLE_clone(INTERP, SELF);
add_matrices(INTERP, dest, value, 0);
return dest;
}
MULTI PMC *add(DEFAULT *value, PMC *dest) {
const FLOATVAL v = VTABLE_get_number(INTERP, value);
dest = VTABLE_clone(INTERP, SELF);
add_scalar_float(INTERP, dest, v);
return dest;
}
VTABLE PMC *add_float(FLOATVAL v, PMC *dest) {
dest = VTABLE_clone(INTERP, SELF);
add_scalar_float(INTERP, dest, v);
return dest;
}
VTABLE PMC *add_int(INTVAL v, PMC *dest) {
dest = VTABLE_clone(INTERP, SELF);
add_scalar_float(INTERP, dest, (FLOATVAL)v);
return dest;
}
/*
=item* i_add(NumMatrix2D)
=item* i_add(DEFAULT)
=item* i_add_int
=item* i_add_float
=cut
*/
MULTI void i_add(NumMatrix2D* value) {
add_matrices(INTERP, SELF, value, 0);
}
MULTI void i_add(DEFAULT* value) {
const FLOATVAL v = VTABLE_get_number(INTERP, value);
add_scalar_float(INTERP, SELF, v);
}
void i_add_int(INTVAL v) {
add_scalar_float(INTERP, SELF, (FLOATVAL)v);
}
void i_add_float(FLOATVAL v) {
add_scalar_float(INTERP, SELF, v);
}
/*
=item* subtract(NumMatrix)
Add two matrices together, element-by-element.
=item* subtract(DEFAULT)
Take the number representation of the argument PMC and add it to every element
in the matrix.
=item* subtract_float
Add the float value to every element in the matrix.
=item* subtract_int
Add the integer value to every element in the matrix.
=cut
*/
MULTI PMC *subtract(NumMatrix2D *value, PMC *dest) {
dest = VTABLE_clone(INTERP, SELF);
add_matrices(INTERP, dest, value, 1);
return dest;
}
MULTI PMC *subtract(DEFAULT *value, PMC *dest) {
const FLOATVAL v = VTABLE_get_number(INTERP, value);
dest = VTABLE_clone(INTERP, SELF);
add_scalar_float(INTERP, dest, -v);
return dest;
}
VTABLE PMC *subtract_float(FLOATVAL v, PMC *dest) {
dest = VTABLE_clone(INTERP, SELF);
add_scalar_float(INTERP, dest, -v);
return dest;
}
VTABLE PMC *subtract_int(INTVAL v, PMC *dest) {
dest = VTABLE_clone(INTERP, SELF);
add_scalar_float(INTERP, dest, -(FLOATVAL)v);
return dest;
}
/*
=item* i_subtract(NumMatrix2D)
=item* i_subtract(DEFAULT)
=item* i_subtract_int
=item* i_subtract_float
=cut
*/
MULTI void i_subtract(NumMatrix2D* value) {
add_matrices(INTERP, SELF, value, 1);
}
MULTI void i_subtract(DEFAULT* value) {
const FLOATVAL v = VTABLE_get_number(INTERP, value);
add_scalar_float(INTERP, SELF, -v);
}
void i_subtract_int(INTVAL v) {
add_scalar_float(INTERP, SELF, -(FLOATVAL)v);
}
void i_subtract_float(FLOATVAL v) {
add_scalar_float(INTERP, SELF, -v);
}
/*
=item* multiply(NumMatrix2D)
=item* multiply(DEFAULT)
=cut
*/
MULTI PMC *multiply(NumMatrix2D *value, PMC *dest) {
DECLATTRS(SELF, selfattr);
DECLATTRS(value, valattr);
const INTVAL new_rows = selfattr->rows;
const INTVAL new_cols = valattr->cols;
if (selfattr->cols != valattr->rows) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": Dimensions do not match in matrix multiply");
}
dest = Parrot_pmc_new(INTERP, VTABLE_type(INTERP, SELF));
resize_matrix(INTERP, dest, new_rows - 1, new_cols - 1);
call_gemm(INTERP, 1.0, SELF, value, 0.0, dest);
return dest;
}
MULTI PMC *multiply(DEFAULT *value, PMC *dest) {
const FLOATVAL v = VTABLE_get_number(INTERP, value);
dest = VTABLE_clone(INTERP, SELF);
multiply_scalar_float(INTERP, dest, v);
return dest;
}
/*
=item* multiply_int
=item* multiply_float
=cut
*/
VTABLE PMC *multiply_int(INTVAL v, PMC * dest) {
dest = VTABLE_clone(INTERP, SELF);
multiply_scalar_float(INTERP, dest, (FLOATVAL)v);
return dest;
}
VTABLE PMC *multiply_float(FLOATVAL v, PMC * dest) {
dest = VTABLE_clone(INTERP, SELF);
multiply_scalar_float(INTERP, dest, v);
return dest;
}
/*
=item* i_multiply(NumMatrix2D)
=item* i_multiply(DEFAULT)
=cut
*/
MULTI void i_multiply(NumMatrix2D* value) {
PMC * const temp = VTABLE_clone(INTERP, SELF);
call_gemm(INTERP, 1.0, temp, value, 0.0, SELF);
}
MULTI void i_multiply(DEFAULT* value) {
const FLOATVAL v = VTABLE_get_number(INTERP, value);
multiply_scalar_float(INTERP, SELF, v);
}
/*
=item i_multiply_int
=item i_multiply_float
=cut
*/
VTABLE void i_multiply_int(INTVAL v) {
multiply_scalar_float(INTERP, SELF, (FLOATVAL)v);
}
VTABLE void i_multiply_float(FLOATVAL v) {
multiply_scalar_float(INTERP, SELF, v);
}
/*
=item* get_attr_str
=cut
*/
VTABLE PMC * get_attr_str(STRING * idx) {
DECLATTRS(SELF, attrs);
if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "rows"))) {
PMC * const rows = Parrot_pmc_new(INTERP, enum_class_Integer);
VTABLE_set_integer_native(INTERP, rows, attrs->rows);
return rows;
}
else if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "cols"))) {
PMC * const cols = Parrot_pmc_new(INTERP, enum_class_Integer);
VTABLE_set_integer_native(INTERP, cols, attrs->cols);
return cols;
}
else if (Parrot_str_equal(INTERP, idx, CONST_STRING(INTERP, "size"))) {
PMC * const size = Parrot_pmc_new(INTERP, enum_class_Integer);
VTABLE_set_integer_native(INTERP, size, attrs->cols * attrs->rows);
return size;
}
return PMCNULL;
}
/*
=item* clone
=cut
*/
VTABLE PMC * clone() {
PMC * const c = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
DECLATTRS(SELF, old_atts);
DECLATTRS(c, new_atts);
INTVAL const newsize = old_atts->rows * old_atts->cols;
FLOATVAL * const old_s = old_atts->storage;
FLOATVAL * const new_s = ALLOCATE_STORAGE(newsize);
memcpy(new_s, old_s, newsize * sizeof(FLOATVAL));
memcpy(new_atts, old_atts, sizeof(Parrot_NumMatrix2D_attributes));
new_atts->storage = new_s;
return c;
}
/*
=item* is_equal
=cut
*/
VTABLE INTVAL is_equal(PMC * other) {
if (other->vtable->base_type == SELF->vtable->base_type) {
DECLATTRS(SELF, self_attrs);
DECLATTRS(other, other_attrs);
const INTVAL self_rows = self_attrs->rows;
const INTVAL self_cols = self_attrs->cols;
const INTVAL self_flags = self_attrs->flags;
const INTVAL other_rows = other_attrs->rows;
const INTVAL other_cols = other_attrs->cols;
const INTVAL other_flags = other_attrs->flags;
FLOATVAL * const self_s = self_attrs->storage;
FLOATVAL * const other_s = other_attrs->storage;
INTVAL i, j;
if (self_rows != other_rows || self_cols != other_cols)
return 0;
for (i = 0; i < self_rows; i++) {
for (j = 0; j < self_cols; j++) {
const FLOATVAL self_value =
ITEM_XY(self_s, self_flags, self_rows, self_cols, i, j);
const FLOATVAL other_value =
ITEM_XY(other_s, other_flags, other_rows, other_cols, i, j);
if (!floats_are_equal(self_value, other_value))
return 0;
}
}
return 1;
}
return 0;
}
/*
=item* freeze
=item* thaw
=cut
*/
VTABLE void freeze(PMC *info) {
DECLATTRS(SELF, attrs);
INTVAL const rows = attrs->rows;
INTVAL const cols = attrs->cols;
INTVAL const flags = attrs->flags;
INTVAL i, j;
FLOATVAL * const s = attrs->storage;
VTABLE_push_integer(INTERP, info, rows);
VTABLE_push_integer(INTERP, info, cols);
VTABLE_push_integer(INTERP, info, flags);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
const FLOATVAL f = ITEM_XY(s, flags, rows, cols, i, j);
VTABLE_push_float(INTERP, info, f);
}
}
}
VTABLE void thaw(PMC *info) {
DECLATTRS(SELF, attrs);
INTVAL const rows = VTABLE_shift_integer(INTERP, info);
INTVAL const cols = VTABLE_shift_integer(INTERP, info);
INTVAL const flags = VTABLE_shift_integer(INTERP, info);
INTVAL i, j;
FLOATVAL * s;
attrs->rows = 0;
attrs->cols = 0;
attrs->storage = NULL;
attrs->flags = 0;
resize_matrix(INTERP, SELF, rows - 1, cols - 1);
s = attrs->storage;
attrs->flags = flags;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
const FLOATVAL f = VTABLE_shift_float(INTERP, info);
ITEM_XY(s, flags, rows, cols, i, j) = f;
}
}
}
/*
=back
=head1 METHODS
=over 4
=item resize()
=cut
*/
METHOD resize(INTVAL new_rows, INTVAL new_cols) {
resize_matrix(INTERP, SELF, new_rows - 1, new_cols - 1);
}
/*
=item get_proxy()
=cut
*/
METHOD get_proxy(PMC * key) {
PMC * const proxy = Parrot_pmc_new_init(INTERP, __PLA_MatrixProxy_typeid, SELF);
VTABLE_assign_pmc(INTERP, proxy, key);
RETURN(PMC * proxy);
}
/*
=item fill()
Fill the matrix with a single value. if sizes are provided, fill to those
sizes, growing the matrix if needed.
=cut
*/
METHOD fill(FLOATVAL value,
INTVAL rows_size :optional, INTVAL has_rows_size :opt_flag,
INTVAL cols_size :optional, INTVAL has_cols_size :opt_flag
) {
DECLATTRS(SELF, attrs);
FLOATVAL * s = attrs->storage;
INTVAL const curr_rows_size = attrs->rows;
INTVAL const curr_cols_size = attrs->cols;
INTVAL i, j;
if (!has_rows_size)
rows_size = curr_rows_size;
if (!has_cols_size)
cols_size = curr_cols_size;
if ((has_rows_size && rows_size > curr_rows_size) ||
(has_cols_size && cols_size > curr_cols_size)) {
resize_matrix(INTERP, SELF, rows_size - 1, cols_size - 1);
s = attrs->storage;
}
for (i = 0; i < rows_size; ++i) {
for (j = 0; j < cols_size; ++j) {
ITEM_XY_ROWMAJOR(s, rows_size, cols_size, i, j) = value;
}
}
}
/*
=item item_at()
=cut
*/
METHOD item_at(INTVAL row, INTVAL col,
FLOATVAL value :optional,
INTVAL has_value :opt_flag) {
DECLATTRS(SELF, attrs);
const INTVAL rows = attrs->rows;
const INTVAL cols = attrs->cols;
if (row >= rows || col >= cols || row < 0 || col < 0) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": indices out of bounds in item_at");
}
if (has_value)
ITEM_XY(attrs->storage, attrs->flags, rows, cols, row, col) = value;
else
value = ITEM_XY(attrs->storage, attrs->flags, rows, cols, row, col);
RETURN(FLOATVAL value);
}
/*
=item transpose()
Transposes the matrix lazily.
=cut
*/
METHOD transpose() {
DECLATTRS(SELF, attrs);
INTVAL transposed = IS_TRANSPOSED(attrs->flags);
SWAP_XY(attrs);
if (transposed)
FLAG_CLEAR(attrs->flags, FLAG_TRANSPOSED);
else
FLAG_SET(attrs->flags, FLAG_TRANSPOSED);
transposed = !transposed;
RETURN(INTVAL transposed);
}
/*
=item mem_transpose()
Transposes the actual data storage of the matrix. More expensive up-front
than the transpose() method.
=cut
*/
METHOD mem_transpose() {
DECLATTRS(SELF, attrs);
const INTVAL rows = attrs->rows;
const INTVAL cols = attrs->cols;
const INTVAL newsize = rows * cols;
FLOATVAL * const new_s = ALLOCATE_STORAGE(newsize);
FLOATVAL * const old_s = attrs->storage;
INTVAL i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
ITEM_XY_ROWMAJOR(new_s, cols, rows, j, i) =
ITEM_XY_ROWMAJOR(old_s, rows, cols, i, j);
}
}