forked from masa16/narray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
na_func.c
1624 lines (1306 loc) · 36 KB
/
na_func.c
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
/*
na_func.c
Numerical Array Extention for Ruby
(C) Copyright 1999-2008 by Masahiro TANAKA
This program is free software.
You can distribute/modify this program
under the same terms as Ruby itself.
NO WARRANTY.
*/
#include <ruby.h>
#include "narray.h"
#include "narray_local.h"
int
na_max3(int a, int b, int c)
{
int m;
if ((a) > (b))
m = a;
else
m = b;
if ((c) > m)
m = c;
return m;
}
void
na_shape_max3(int ndim, int *max_shp, int *shp1, int *shp2, int *shp3)
{
int i;
for (i=0; i<ndim; ++i) {
max_shp[i] = na_max3(shp1[i], shp2[i], shp3[i]);
}
}
/* initialize slice structure */
void na_init_slice( struct slice *s, int rank, int *shape, int elmsz )
{
int r, i, b;
na_index_t *idx;
/*
if (rank<1)
rb_raise(rb_eRuntimeError,"cannot execute for empty array");
*/
/* set strides and clear index */
s[0].stride = 1;
for (r=1; r<rank; ++r)
s[r].stride = s[r-1].stride * shape[r-1];
for (r=0; r<rank; ++r) {
if ( s[r].idx == NULL )
/* regular interval */
s[r].pstep = s[r].step * s[r].stride * elmsz;
else {
/* index */
s[r].pstep = b = s[r].stride * elmsz;
/* convert index to byte-unit */
for (i=0; i<16; ++i)
if ( (1<<i) == b ) { b=i; break; }
if (i==16)
for (idx=s[r].idx,i=s[r].n; i-->0; ) { *(idx++)*=b; }
else
for (idx=s[r].idx,i=s[r].n; i-->0; ) { *(idx++)<<=b; }
}
}
/* set termination mark */
s[rank].n = 0;
s[rank].idx = NULL;
for (r=rank-1;r>=0;--r) {
/* set beginning pointers */
if ( s[r].idx == NULL )
s[r].pbeg = s[r].stride * s[r].beg * elmsz;
else
s[r].pbeg = s[r].idx[0];
}
}
static void
na_do_loop_unary( int nd, char *p1, char *p2,
struct slice *s1, struct slice *s2, void (*func)() )
{
int *si;
int i;
int ps1 = s1[0].pstep;
int ps2 = s2[0].pstep;
i = nd;
si = ALLOCA_N(int,nd);
s1[i].p = p1;
s2[i].p = p2;
for(;;) {
/* set pointers */
while (i > 0) {
--i;
s2[i].p = s2[i].pbeg + s2[i+1].p;
s1[i].p = s1[i].pbeg + s1[i+1].p;
si[i] = s1[i].n;
}
(*func)(s2[0].n, s1[0].p, ps1, s2[0].p, ps2);
/* rank up */
do {
if ( ++i >= nd ) return;
} while ( --si[i] == 0 );
/* next point */
s1[i].p += s1[i].pstep;
s2[i].p += s2[i].pstep;
}
}
static void
na_do_loop_binary( int nd, char *p1, char *p2, char *p3,
struct slice *s1, struct slice *s2, struct slice *s3,
void (*func)() )
{
int i;
int ps1 = s1[0].pstep;
int ps2 = s2[0].pstep;
int ps3 = s3[0].pstep;
int *si;
si = ALLOCA_N(int,nd);
i = nd;
s1[i].p = p1;
s2[i].p = p2;
s3[i].p = p3;
for(;;) {
/* set pointers */
while (i > 0) {
--i;
s3[i].p = s3[i].pbeg + s3[i+1].p;
s2[i].p = s2[i].pbeg + s2[i+1].p;
s1[i].p = s1[i].pbeg + s1[i+1].p;
si[i] = s1[i].n;
}
/* rank 0 loop */
(*func)(s2[0].n, s1[0].p, ps1, s2[0].p, ps2, s3[0].p, ps3);
/* rank up */
do {
if ( ++i >= nd ) return;
} while ( --si[i] == 0 );
/* next point */
s1[i].p += s1[i].pstep;
s2[i].p += s2[i].pstep;
s3[i].p += s3[i].pstep;
}
}
void na_loop_index_ref( struct NARRAY *a1, struct NARRAY *a2,
struct slice *s1, struct slice *s2, void (*func)() )
{
char *p1, *p2;
int nr, i, ii;
int ps1 = s1[0].pstep;
int ps2 = s2[0].pstep;
int *si;
na_index_t *idx;
/*
int copy;
if (a1->type==a2->type && s1[0].step==1 && s2[0].step==1)
copy = s1[0].n * na_sizeof[a1->type];
else
copy = 0;
*/
/* Initialize */
nr = i = a1->rank;
si = ALLOCA_N(int,nr);
s1[i].p = a1->ptr;
s2[i].p = a2->ptr;
for(;;) {
/* set pointers */
while (i > 0) {
--i;
s2[i].p = s2[i].pbeg + s2[i+1].p;
s1[i].p = s1[i].pbeg + s1[i+1].p;
si[i] = 0;
}
/* rank 0 loop */
if ( s2[0].idx == NULL ) {
/* regular interval */
/*
if (copy) {
memcpy(s1[0].p, s2[0].p, copy);
} else
*/
(*func)(s2[0].n, s1[0].p, ps1, s2[0].p, ps2);
} else {
/* a2 has index */
p1 = s1[0].p;
p2 = s2[1].p;
for ( idx=s2[0].idx, ii=s2[0].n; ii-->0;) {
(*func)( 1, p1, 0, p2+*(idx++), 0 );
p1 += ps1;
}
}
/* rank up */
do {
if ( ++i >= nr ) return;
} while ( ++si[i] == s1[i].n );
/* next point */
s1[i].p += s1[i].pstep;
/* array2 may have index */
if ( s2[i].idx == NULL )
s2[i].p += s2[i].pstep;
else
s2[i].p = s2[i+1].p + s2[i].idx[si[i]]; /* * s2[i].pstep; */
}
}
void na_loop_general( struct NARRAY *a1, struct NARRAY *a2,
struct slice *s1, struct slice *s2, void (*func)() )
{
char *p1, *p2;
int nr, i, ii;
int ps1 = s1[0].pstep;
int ps2 = s2[0].pstep;
int *si;
na_index_t *idx1, *idx2;
/* Initialize */
nr = i = a1->rank;
si = ALLOCA_N(int,nr);
s1[i].p = a1->ptr;
s2[i].p = a2->ptr;
for(;;) {
/* set pointers */
while (i > 0) {
--i;
s2[i].p = s2[i].pbeg + s2[i+1].p;
s1[i].p = s1[i].pbeg + s1[i+1].p;
si[i] = 0;
}
/* rank 0 loop */
if ( s1[0].idx == NULL ) {
if ( s2[0].idx == NULL ) {
/* normal interval */
(*func)(s2[0].n, s1[0].p, ps1, s2[0].p, ps2);
} else {
/* s2 has index */
p1 = s1[0].p;
p2 = s2[1].p;
for ( idx2=s2[0].idx, ii=s2[0].n; ii-->0; ) {
(*func)( 1, p1, 0, p2+*(idx2++), 0 );
p1 += ps1;
}
}
} else {
if ( s2[0].idx == NULL ) {
/* s1 has index */
p1 = s1[1].p;
p2 = s2[0].p;
for ( idx1=s1[0].idx, ii=s2[0].n; ii-->0; ) {
(*func)( 1, p1+*(idx1++), 0, p2, 0 );
p2 += ps2;
}
} else {
/* s1 & s2 has index */
p1 = s1[1].p;
p2 = s2[1].p;
for ( idx1=s1[0].idx, idx2=s2[0].idx, ii=s2[0].n; ii-->0; ) {
(*func)( 1, p1+*(idx1++), 0, p2+*(idx2++), 0 );
}
}
}
/* rank up */
do {
if ( ++i >= nr ) return;
} while ( ++si[i] == s1[i].n );
/* next point for a1 */
if ( s1[i].idx == NULL )
s1[i].p += s1[i].pstep;
else
s1[i].p = s1[i+1].p + s1[i].idx[si[i]];
/* next point for a2 */
if ( s2[i].idx == NULL )
s2[i].p += s2[i].pstep;
else
s2[i].p = s2[i+1].p + s2[i].idx[si[i]];
}
}
void
na_shape_copy( int ndim, int *shape, struct NARRAY *a )
{
int i;
for (i=0; i<a->rank; ++i)
shape[i] = a->shape[i];
for ( ; i<ndim; ++i)
shape[i] = 1;
}
void
na_set_slice_1obj( int ndim, struct slice *slc, int *shape )
{
int i;
/* for normal access */
for (i=0; i<ndim; ++i) {
slc[i].n = shape[i];
slc[i].beg = 0;
slc[i].step = 1;
slc[i].idx = NULL;
}
}
static int
na_set_slice_2obj( int ndim, struct slice *s1, struct slice *s2,
int *shp1, int *shp2 )
{
int i, j;
for (i=j=0; i<ndim; ++i) {
if ( shp1[i]==1 && shp2[i]>1 ) {
s1[j].n =
s2[j].n = shp2[i];
s1[j].step = 0;
s2[j].step = 1;
} else
if ( shp2[i]==1 && shp1[i]>1 ) {
s1[j].n =
s2[j].n = shp1[i];
s1[j].step = 1;
s2[j].step = 0;
} else
if ( shp1[i] == shp2[i] ) {
s1[j].n =
s2[j].n = shp1[i];
s1[j].step = 1;
s2[j].step = 1;
} else
rb_raise(rb_eRuntimeError, "Array size mismatch: %i != %i in %i-th dim",
shp1[i], shp2[i], i);
if (j<i) {
shp1[j] = shp1[i];
shp2[j] = shp2[i];
}
if (j>0)
if ( s1[j].step == s1[j-1].step &&
s2[j].step == s2[j-1].step ) { /* contract dimension */
s1[j-1].n =
s2[j-1].n *= s2[j].n;
shp1[j-1] *= shp1[j];
shp2[j-1] *= shp2[j];
continue;
}
s1[j].beg =
s2[j].beg = 0;
s1[j].idx =
s2[j].idx = NULL;
++j;
}
return j;
}
static int
na_set_slice_check(int ary_sz, int itr_sz, int i)
{
if ( ary_sz == itr_sz )
return 1;
else if ( ary_sz == 1 )
return 0;
else
rb_raise(rb_eRuntimeError, "Array size mismatch: %i != %i at %i-th dim",
ary_sz, itr_sz, i);
}
int
na_set_slice_3obj( int ndim,
struct slice *s1, struct slice *s2, struct slice *s3,
int *shp1, int *shp2, int *shp3, int *shape )
{
int i, j;
/* for repetitous access */
for (i=j=0; i<ndim; ++i) {
s1[j].step = na_set_slice_check(shp1[i],shape[i],i);
s2[j].step = na_set_slice_check(shp2[i],shape[i],i);
s3[j].step = na_set_slice_check(shp3[i],shape[i],i);
if (j<i) {
shp1[j] = shp1[i];
shp2[j] = shp2[i];
shp3[j] = shp3[i];
}
if (j>0) {
if ( s1[j].step == s1[j-1].step &&
s2[j].step == s2[j-1].step &&
s3[j].step == s3[j-1].step ) /* contract dimension */
{
s1[j-1].n =
s2[j-1].n =
s3[j-1].n *= shape[i];
shp1[j-1] *= shp1[j];
shp2[j-1] *= shp2[j];
shp3[j-1] *= shp3[j];
continue;
}
}
s1[j].n =
s2[j].n =
s3[j].n = shape[i];
s1[j].beg =
s2[j].beg =
s3[j].beg = 0;
s1[j].idx =
s2[j].idx =
s3[j].idx = NULL;
++j;
}
return j;
}
static void
na_exec_unary(struct NARRAY *a1, struct NARRAY *a2, void (*func)())
{
int ndim;
int *shp1, *shp2;
struct slice *s1, *s2;
/* empty check */
if ( a1->total==0 || a2->total==0 )
/* rb_raise( rb_eTypeError, "cannot execute for empty array" ); */
return; /* do nothing */
ndim = NA_MAX(a1->rank,a2->rank);
NA_ALLOC_SLICE(s1,(ndim+1)*2,shp1,ndim*2);
shp2 = &shp1[ndim];
s2 = &s1[ndim+1];
na_shape_copy( ndim, shp1, a1 );
na_shape_copy( ndim, shp2, a2 );
ndim = na_set_slice_2obj( ndim, s1, s2, shp1, shp2 );
na_init_slice( s1, ndim, shp1, na_sizeof[a1->type] );
na_init_slice( s2, ndim, shp2, na_sizeof[a2->type] );
na_do_loop_unary( ndim, a1->ptr, a2->ptr, s1, s2, func );
xfree(s1);
}
/* a1 and/or a2 and/or a3 have extensible index */
static void
na_exec_binary( struct NARRAY *a1, struct NARRAY *a2,
struct NARRAY *a3, void (*func)() )
{
int ndim;
int *itr, *shp1, *shp2, *shp3;
struct slice *s1, *s2, *s3;
/* empty check */
if (a1->total==0) return; /* do nothing */
ndim = na_max3(a1->rank, a2->rank, a3->rank);
NA_ALLOC_SLICE(s1,(ndim+1)*3,shp1,ndim*4);
shp2 = &shp1[ndim];
shp3 = &shp2[ndim];
itr = &shp3[ndim];
s2 = &s1[ndim+1];
s3 = &s2[ndim+1];
na_shape_copy( ndim, shp1, a1 );
na_shape_copy( ndim, shp2, a2 );
na_shape_copy( ndim, shp3, a3 );
na_shape_max3( ndim, itr, shp1, shp2, shp3 );
ndim = na_set_slice_3obj( ndim, s1, s2, s3, shp1, shp2, shp3, itr );
na_init_slice(s1, ndim, shp1, na_sizeof[a1->type] );
na_init_slice(s2, ndim, shp2, na_sizeof[a2->type] );
na_init_slice(s3, ndim, shp3, na_sizeof[a3->type] );
na_do_loop_binary( ndim, a1->ptr, a2->ptr, a3->ptr, s1, s2, s3, func );
xfree(s1);
}
static void
na_shape_max_2obj(int ndim, int *shape, struct NARRAY *a1, struct NARRAY *a2)
{
struct NARRAY *tmp;
int i;
/* empty check */
if ( a1->total==0 || a2->total==0 )
rb_raise( rb_eTypeError, "cannot execute for empty array" );
if (a1->rank < a2->rank) {
NA_SWAP(a1,a2,tmp);
}
for (i=0; i<a2->rank; ++i) {
shape[i] = NA_MAX(a1->shape[i],a2->shape[i]);
}
for ( ; i<a1->rank; ++i) {
shape[i] = a1->shape[i];
}
for ( ; i<ndim; ++i) {
shape[i] = 1;
}
}
static VALUE
na_make_object_extend(struct NARRAY *a1, struct NARRAY *a2,
int type, VALUE klass)
{
int ndim;
int *shape;
/* empty check */
if ( a1->total==0 || a2->total==0 )
return na_make_empty(type, klass); /* return empty */
ndim = NA_MAX(a1->rank, a2->rank);
shape = ALLOCA_N(int, ndim);
na_shape_max_2obj( ndim, shape, a1, a2 );
return na_make_object( type, ndim, shape, klass );
}
static ID na_bifunc_to_id(na_bifunc_t funcs)
{
if (funcs==AddBFuncs) return na_id_add;
if (funcs==SbtBFuncs) return na_id_sbt;
if (funcs==MulBFuncs) return na_id_mul;
if (funcs==DivBFuncs) return na_id_div;
if (funcs==ModBFuncs) return na_id_mod;
return 0;
/* if (funcs==PowFuncs) return na_id_power;
rb_raise(rb_eRuntimeError, "coerce_rev: function not soppurted");
*/
}
static VALUE
na_bifunc_class(VALUE klass1, VALUE klass2)
{
if ( klass2==cNArray || klass2==cNArrayScalar ) {
if ( klass1==cNArrayScalar ) return cNArray;
else return klass1;
}
return Qnil;
}
static VALUE
na_bifunc(VALUE obj1, VALUE obj2, VALUE klass, na_bifunc_t funcs)
{
VALUE obj3;
ID id;
int type;
Check_Type(obj1, T_DATA);
obj2 = na_upcast_object(obj2,NA_STRUCT(obj1)->type);
obj1 = na_upcast_type(obj1,type=NA_STRUCT(obj2)->type);
if (klass==Qnil) {
klass = na_bifunc_class(CLASS_OF(obj1),CLASS_OF(obj2));
if (klass==Qnil) { /* coerce_rev */
if ((id=na_bifunc_to_id(funcs))!=0)
return rb_funcall( obj2, na_id_coerce_rev, 2, obj1, ID2SYM(id) );
else
klass = cNArray;
}
}
obj3 = na_make_object_extend(NA_STRUCT(obj1),NA_STRUCT(obj2),type,klass);
na_exec_binary( NA_STRUCT(obj3), NA_STRUCT(obj1), NA_STRUCT(obj2),
funcs[type] );
return obj3;
}
static VALUE
na_power(VALUE val1, VALUE val2)
{
volatile VALUE obj1, obj2, obj3;
struct NARRAY *a1, *a2;
obj1 = val1;
obj2 = val2;
GetNArray(obj1,a1);
obj2 = na_to_narray(obj2);
GetNArray(obj2,a2);
if (a1->type==NA_ROBJ && a2->type!=NA_ROBJ) {
obj2 = na_change_type(obj2,NA_ROBJ);
GetNArray(obj2,a2);
} else
if (a2->type==NA_ROBJ && a1->type!=NA_ROBJ) {
obj1 = na_change_type(obj1,NA_ROBJ);
GetNArray(obj1,a1);
} else
if (!NA_IsCOMPLEX(a1) && NA_IsCOMPLEX(a2)) {
obj1 = na_upcast_type(obj1,a2->type);
GetNArray(obj1,a1);
}
obj3 = na_make_object_extend( a1, a2, na_upcast[a1->type][a2->type],
CLASS_OF(obj1) );
na_exec_binary( NA_STRUCT(obj3), a1, a2,
PowFuncs[a1->type][a2->type] );
return obj3;
}
static VALUE
na_set_func(VALUE obj1, volatile VALUE obj2, na_ufunc_t funcs)
{
struct NARRAY *a1;
GetNArray(obj1,a1);
obj2 = na_cast_object(obj2,a1->type);
na_exec_unary( NA_STRUCT(obj1), NA_STRUCT(obj2), funcs[a1->type] );
return obj1;
}
static VALUE
na_imag_set(VALUE obj1, volatile VALUE obj2)
{
struct NARRAY *a1;
GetNArray(obj1,a1);
obj2 = na_cast_object(obj2, na_cast_real[a1->type]);
na_exec_unary( NA_STRUCT(obj1), NA_STRUCT(obj2), ImgSetFuncs[a1->type] );
return obj1;
}
static VALUE
na_unary_func(VALUE self, const int *cast, na_ufunc_t funcs)
{
VALUE ans;
struct NARRAY *a2;
GetNArray(self,a2);
ans = na_make_object(cast[a2->type], a2->rank, a2->shape, CLASS_OF(self));
na_exec_unary( NA_STRUCT(ans), a2, funcs[a2->type] );
return ans;
}
/* local function for comparison */
static VALUE
na_compare_func(VALUE self, VALUE other, na_bifunc_t funcs)
{
VALUE ans;
int type;
Check_Type(self, T_DATA);
/*if (NA_IsComplex(a1)) rb_raise();*/
other = na_upcast_object(other,NA_STRUCT(self)->type);
self = na_upcast_type(self,type=NA_STRUCT(other)->type);
ans = na_make_object_extend( NA_STRUCT(self), NA_STRUCT(other),
NA_BYTE, cNArray );
na_exec_binary( NA_STRUCT(ans), NA_STRUCT(self), NA_STRUCT(other),
funcs[type] );
return ans;
}
/* method: self + other */
static VALUE na_add(VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, Qnil, AddBFuncs ); }
/* method: self - other */
static VALUE na_sbt(VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, Qnil, SbtBFuncs ); }
/* method: self * other */
static VALUE na_mul(VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, Qnil, MulBFuncs ); }
/* method: self / other */
static VALUE na_div(VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, Qnil, DivBFuncs ); }
/* method: self / other */
static VALUE na_mod(VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, Qnil, ModBFuncs ); }
/* method: self & other */
static VALUE na_bit_and(VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, Qnil, BAnFuncs ); }
/* method: self | other */
static VALUE na_bit_or(VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, Qnil, BOrFuncs ); }
/* method: self ^ other */
static VALUE na_bit_xor(VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, Qnil, BXoFuncs ); }
/* method: atan2(y,x) */
static VALUE na_math_atan2(VALUE module, volatile VALUE y, volatile VALUE x)
{
VALUE ans;
struct NARRAY *ya, *xa, *aa;
if (TYPE(y) == T_ARRAY) {
y = na_ary_to_nary(y,cNArray);
} else
if (!IsNArray(y)) {
y = na_make_scalar(y,na_object_type(y));
}
if (TYPE(x) == T_ARRAY) {
x = na_ary_to_nary(x,cNArray);
} else
if (!IsNArray(x)) {
x = na_make_scalar(x,na_object_type(x));
}
GetNArray(y,ya);
GetNArray(x,xa);
if (NA_IsINTEGER(ya) && NA_IsINTEGER(xa)) {
y = na_upcast_type(y,NA_DFLOAT);
x = na_upcast_type(x,NA_DFLOAT);
}
ans = na_bifunc( y, x, Qnil, atan2Funcs );
GetNArray(ans,aa);
if (CLASS_OF(y) == cNArrayScalar && CLASS_OF(x) == cNArrayScalar)
SetFuncs[NA_ROBJ][aa->type](1,&ans,0,aa->ptr,0);
return ans;
}
/* singleton method: NArray.mul( obj1, obj2 ) */
static VALUE
na_s_mul(VALUE klass, VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, klass, MulBFuncs ); }
/* singleton method: NArray.div( obj1, obj2 ) */
static VALUE
na_s_div(VALUE klass, VALUE obj1, VALUE obj2)
{ return na_bifunc( obj1, obj2, klass, DivBFuncs ); }
/* method: self.add!(other) */
static VALUE na_add_bang(VALUE obj1, VALUE obj2)
{ return na_set_func( obj1, obj2, AddUFuncs ); }
/* method: self.sbt!(other) */
static VALUE na_sbt_bang(VALUE obj1, VALUE obj2)
{ return na_set_func( obj1, obj2, SbtUFuncs ); }
/* method: self.div!(other) */
static VALUE na_div_bang(VALUE obj1, VALUE obj2)
{ return na_set_func( obj1, obj2, DivUFuncs ); }
/* method: self.mul!(other) */
static VALUE na_mul_bang(VALUE obj1, VALUE obj2)
{ return na_set_func( obj1, obj2, MulUFuncs ); }
/* method: self.conj! */
static VALUE na_conj_bang(VALUE self)
{ return na_set_func( self, self, ConjFuncs ); }
/* method: self.swap_byte */
static VALUE na_swap_byte(VALUE self)
{ return na_unary_func( self, na_no_cast, SwpFuncs ); }
/* method: self.hton , self.ntoh */
static VALUE na_hton(VALUE self)
{ return na_unary_func( self, na_no_cast, H2NFuncs ); }
/* method: self.htov , self.vtoh */
static VALUE na_htov(VALUE self)
{ return na_unary_func( self, na_no_cast, H2VFuncs ); }
/* method: ~self */
static VALUE na_bit_rev(VALUE self)
{ return na_unary_func( self, na_no_cast, BRvFuncs ); }
/* method: -self */
static VALUE na_neg(VALUE self)
{ return na_unary_func( self, na_no_cast, NegFuncs ); }
/* method: self.recip */
static VALUE na_recip(VALUE self)
{ return na_unary_func( self, na_no_cast, RcpFuncs ); }
/* method: self.abs */
static VALUE na_abs(VALUE self)
{ return na_unary_func( self, na_cast_real, AbsFuncs ); }
/* method: self.real */
static VALUE na_real(VALUE self)
{ return na_unary_func( self, na_cast_real, RealFuncs ); }
/* method: self.imag */
static VALUE na_imag(VALUE self)
{ return na_unary_func( self, na_cast_real, ImagFuncs ); }
/* method: self.imag */
static VALUE na_angle(VALUE self)
{ return na_unary_func( self, na_cast_real, AnglFuncs ); }
/* method: self.im */
static VALUE na_imag_mul(VALUE self)
{ return na_unary_func( self, na_cast_comp, ImagMulFuncs ); }
/* method: self.conj */
static VALUE na_conj(VALUE self)
{ return na_unary_func( self, na_no_cast, ConjFuncs ); }
/* method: self.floor */
static VALUE na_floor(VALUE self)
{ return na_unary_func( self, na_cast_round, FloorFuncs ); }
/* method: self.ceil */
static VALUE na_ceil(VALUE self)
{ return na_unary_func( self, na_cast_round, CeilFuncs ); }
/* method: self.round */
static VALUE na_round(VALUE self)
{ return na_unary_func( self, na_cast_round, RoundFuncs ); }
/* method: self.not */
static VALUE na_not(VALUE self)
{ return na_unary_func( self, na_cast_byte, NotFuncs ); }
/* method: self.and other */
static VALUE
na_cond_and(VALUE obj1, VALUE obj2)
{ return na_compare_func( obj1, obj2, AndFuncs ); }
/* method: self.or other */
static VALUE
na_cond_or(VALUE obj1, VALUE obj2)
{ return na_compare_func( obj1, obj2, Or_Funcs ); }
/* method: self.xor other */
static VALUE
na_cond_xor(VALUE obj1, VALUE obj2)
{ return na_compare_func( obj1, obj2, XorFuncs ); }
/* method: self <=> other */
static VALUE
na_compare(VALUE obj1, VALUE obj2)
{ return na_compare_func( obj1, obj2, CmpFuncs ); }
/* method: self.eq(other) */
static VALUE
na_equal(VALUE obj1, VALUE obj2)
{
return na_compare_func( obj1, obj2, EqlFuncs );
}
/* method: self.ne(other) */
static VALUE
na_not_equal(VALUE obj1, VALUE obj2)
{
VALUE obj;
int i; char *p;
struct NARRAY *a;
obj = na_compare_func( obj1, obj2, EqlFuncs );
GetNArray(obj,a);
p = a->ptr;
for( i=a->total; i-->0; ) {
*p = *p==0 ? 1 : 0;
++p;
}
return obj;
}
/* method: self > other */
static VALUE
na_greater_than(VALUE self, VALUE obj2)
{
int i; char *p;
struct NARRAY *a;
self = na_compare_func( self, obj2, CmpFuncs );
GetNArray(self,a);
p = a->ptr;
for( i=a->total; i-->0; ) {
if (*p!=1) *p=0;
++p;
}
return self;
}
/* method: self >= other */
static VALUE
na_greater_equal(VALUE obj1, VALUE obj2)
{
VALUE obj;
int i; char *p;
struct NARRAY *a;
obj = na_compare_func( obj1, obj2, CmpFuncs );
GetNArray(obj,a);
p = a->ptr;
for( i=a->total; i-->0; ) {
if (*p==1 || *p==0) *p=1;
else *p=0;
++p;
}
return obj;
}
/* method: self < other */
static VALUE
na_less_than(VALUE obj1, VALUE obj2)
{
VALUE obj;
int i; char *p;
struct NARRAY *a;
obj = na_compare_func( obj1, obj2, CmpFuncs );
GetNArray(obj,a);
p = a->ptr;
for( i=a->total; i-->0; ) {
if (*p==2) *p=1;
else *p=0;
++p;
}
return obj;
}
/* method: self <= other */
static VALUE
na_less_equal(VALUE obj1, VALUE obj2)
{
VALUE obj;