File: usercomplex.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (1401 lines) | stat: -rw-r--r-- 59,763 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
//------------------------------------------------------------------------------
// GraphBLAS/Demo/Include/usercomplex.c:  complex numbers as a user-defined type
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// All functions must work if any input/outputs x, y, and/or z are aliased.
// Some methods (times, div, and rdiv) require temporary variables zre and zim
// as a result.  All other functions are OK as-is.

#ifdef MATLAB_MEX_FILE

    #include "GB_mex.h"
    #undef OK
    #define OK(method)                                                      \
    {                                                                       \
        info = method ;                                                     \
        if (! (info == GrB_SUCCESS || info == GrB_NO_VALUE))                \
        {                                                                   \
            printf ("%s, line %d, info %d\n", __FILE__, __LINE__, info) ;   \
            mexErrMsgTxt ("GraphBLAS error!") ;                             \
        }                                                                   \
    }

#else

    #include "graphblas_demos.h"
    #undef  FREE_ALL
    #define FREE_ALL Complex_finalize ( ) ;

    #if defined __INTEL_COMPILER
    #pragma warning (disable: 58 167 144 161 177 181 186 188 589 593 869 981 \
        1418 1419 1572 1599 2259 2282 2557 2547 3280 )
    #elif defined __GNUC__
    #pragma GCC diagnostic ignored "-Wunused-parameter"
    #if !defined ( __cplusplus )
    #pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
    #endif
    #endif

#endif

GrB_BinaryOp Complex_first = NULL, Complex_second = NULL, Complex_min = NULL,
             Complex_max   = NULL, Complex_plus   = NULL, Complex_minus = NULL,
             Complex_times = NULL, Complex_div    = NULL, Complex_rminus = NULL,
             Complex_rdiv  = NULL, Complex_pair   = NULL ;

GrB_BinaryOp Complex_iseq = NULL, Complex_isne = NULL,
             Complex_isgt = NULL, Complex_islt = NULL,
             Complex_isge = NULL, Complex_isle = NULL ;

GrB_BinaryOp Complex_or = NULL, Complex_and = NULL, Complex_xor = NULL ;

GrB_BinaryOp Complex_eq = NULL, Complex_ne = NULL,
             Complex_gt = NULL, Complex_lt = NULL,
             Complex_ge = NULL, Complex_le = NULL ;

GrB_BinaryOp Complex_complex = NULL ;

GrB_UnaryOp  Complex_identity = NULL, Complex_ainv = NULL, Complex_minv = NULL,
             Complex_not = NULL,      Complex_conj = NULL,
             Complex_one = NULL,      Complex_abs  = NULL ;

GrB_UnaryOp Complex_real = NULL, Complex_imag = NULL,
            Complex_cabs = NULL, Complex_angle = NULL ;

GrB_UnaryOp Complex_complex_real = NULL, Complex_complex_imag = NULL ;

GrB_Type Complex = NULL ;
GrB_Monoid   Complex_plus_monoid = NULL, Complex_times_monoid = NULL ;
GrB_Semiring Complex_plus_times = NULL ;

//------------------------------------------------------------------------------
// binary functions, z=f(x,y), where CxC -> Complex
//------------------------------------------------------------------------------

void mycx_first (mycx *z, const mycx *x, const mycx *y)
{
    z->re = x->re ;
    z->im = x->im ;
}

#define MYCX_FIRST                                                          \
"void mycx_first (mycx *z, const mycx *x, const mycx *y)                \n" \
"{                                                                      \n" \
"    z->re = x->re ;                                                    \n" \
"    z->im = x->im ;                                                    \n" \
"}"

void mycx_second (mycx *z, const mycx *x, const mycx *y)
{
    z->re = y->re ;
    z->im = y->im ;
}

#define MYCX_SECOND                                                         \
"void mycx_second (mycx *z, const mycx *x, const mycx *y)               \n" \
"{                                                                      \n" \
"    z->re = y->re ;                                                    \n" \
"    z->im = y->im ;                                                    \n" \
"}"

void mycx_pair (mycx *z, const mycx *x, const mycx *y)
{
    z->re = 1 ;
    z->im = 0 ;
}

#define MYCX_PAIR                                                           \
"void mycx_pair (mycx *z, const mycx *x, const mycx *y)                 \n" \
"{                                                                      \n" \
"    z->re = 1 ;                                                        \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void mycx_plus (mycx *z, const mycx *x, const mycx *y)
{
    z->re = x->re + y->re ;
    z->im = x->im + y->im ;
}

#define MYCX_PLUS                                                           \
"void mycx_plus (mycx *z, const mycx *x, const mycx *y)                 \n" \
"{                                                                      \n" \
"    z->re = x->re + y->re ;                                            \n" \
"    z->im = x->im + y->im ;                                            \n" \
"}"

void mycx_minus (mycx *z, const mycx *x, const mycx *y)
{
    z->re = x->re - y->re ;
    z->im = x->im - y->im ;
}

#define MYCX_MINUS                                                          \
"void mycx_minus (mycx *z, const mycx *x, const mycx *y)                \n" \
"{                                                                      \n" \
"    z->re = x->re - y->re ;                                            \n" \
"    z->im = x->im - y->im ;                                            \n" \
"}"

void mycx_rminus (mycx *z, const mycx *x, const mycx *y)
{
    z->re = y->re - x->re ;
    z->im = y->im - x->im ;
}

#define MYCX_RMINUS                                                         \
"void mycx_rminus (mycx *z, const mycx *x, const mycx *y)               \n" \
"{                                                                      \n" \
"    z->re = y->re - x->re ;                                            \n" \
"    z->im = y->im - x->im ;                                            \n" \
"}"

// temporary variables zre and zim are required for times, div, and rdiv:
void mycx_times (mycx *z, const mycx *x, const mycx *y)
{
    double zre = (x->re * y->re) - (x->im * y->im) ;
    double zim = (x->re * y->im) + (x->im * y->re) ;
    z->re = zre ;
    z->im = zim ;
}

#define MYCX_TIMES                                                          \
"void mycx_times (mycx *z, const mycx *x, const mycx *y)                \n" \
"{                                                                      \n" \
"    double zre = (x->re * y->re) - (x->im * y->im) ;                   \n" \
"    double zim = (x->re * y->im) + (x->im * y->re) ;                   \n" \
"    z->re = zre ;                                                      \n" \
"    z->im = zim ;                                                      \n" \
"}"

void mycx_div (mycx *z, const mycx *x, const mycx *y)
{
    double den = (y->re * y->re) + (y->im * y->im) ;
    double zre = ((x->re * y->re) + (x->im * y->im)) / den ;
    double zim = ((x->im * y->re) - (x->re * y->im)) / den ;
    z->re = zre ;
    z->im = zim ;
}

#define MYCX_DIV                                                            \
"void mycx_div (mycx *z, const mycx *x, const mycx *y)                  \n" \
"{                                                                      \n" \
"    double den = (y->re * y->re) + (y->im * y->im) ;                   \n" \
"    double zre = ((x->re * y->re) + (x->im * y->im)) / den ;           \n" \
"    double zim = ((x->im * y->re) - (x->re * y->im)) / den ;           \n" \
"    z->re = zre ;                                                      \n" \
"    z->im = zim ;                                                      \n" \
"}"

void mycx_rdiv (mycx *z, const mycx *x, const mycx *y)
{
    double den = (x->re * x->re) + (x->im * x->im) ;
    double zre = ((y->re * x->re) + (y->im * x->im)) / den ;
    double zim = ((y->im * x->re) - (y->re * x->im)) / den ;
    z->re = zre ;
    z->im = zim ;
}

#define MYCX_RDIV                                                           \
"void mycx_div (mycx *z, const mycx *x, const mycx *y)                  \n" \
"{                                                                      \n" \
"    double den = (x->re * x->re) + (x->im * x->im) ;                   \n" \
"    double zre = ((y->re * x->re) + (y->im * x->im)) / den ;           \n" \
"    double zim = ((y->im * x->re) - (y->re * x->im)) / den ;           \n" \
"    z->re = zre ;                                                      \n" \
"    z->im = zim ;                                                      \n" \
"}"

// min (x,y): complex number with smallest magnitude.  If tied, select the
// one with the smallest phase angle.  No special cases for NaNs.

void fx64_min (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    double absx = cabs (*x) ;
    double absy = cabs (*y) ;
    if (absx < absy)
    {
        (*z) = (*x) ;
    }
    else if (absx > absy)
    {
        (*z) = (*y) ;
    }
    else
    {
        (*z) = (carg (*x) < carg (*y)) ? (*x) : (*y) ;
    }
}

#define FX64_MIN                                                            \
"void fx64_min (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)\n" \
"{                                                                      \n" \
"    double absx = cabs (*x) ;                                          \n" \
"    double absy = cabs (*y) ;                                          \n" \
"    if (absx < absy)                                                   \n" \
"    {                                                                  \n" \
"        (*z) = (*x) ;                                                  \n" \
"    }                                                                  \n" \
"    else if (absx > absy)                                              \n" \
"    {                                                                  \n" \
"        (*z) = (*y) ;                                                  \n" \
"    }                                                                  \n" \
"    else                                                               \n" \
"    {                                                                  \n" \
"        (*z) = (carg (*x) < carg (*y)) ? (*x) : (*y) ;                 \n" \
"    }                                                                  \n" \
"}"

void mycx_min (mycx *z, const mycx *x, const mycx *y)
{
    GxB_FC64_t X = GxB_CMPLX (x->re, x->im) ;
    GxB_FC64_t Y = GxB_CMPLX (y->re, y->im) ;
    double absx = cabs (X) ;
    double absy = cabs (Y) ;
    if (absx < absy)
    {
        z->re = x->re ;
        z->im = x->im ;
    }
    else if (absx > absy)
    {
        z->re = y->re ;
        z->im = y->im ;
    }
    else
    {
        if (carg (X) < carg (Y))
        {
            z->re = x->re ;
            z->im = x->im ;
        }
        else
        {
            z->re = y->re ;
            z->im = y->im ;
        }
    }
}

#define MYCX_MIN                                                            \
"void mycx_min (mycx *z, const mycx *x, const mycx *y)                  \n" \
"{                                                                      \n" \
"    GxB_FC64_t X = GxB_CMPLX (x->re, x->im) ;                          \n" \
"    GxB_FC64_t Y = GxB_CMPLX (y->re, y->im) ;                          \n" \
"    double absx = cabs (X) ;                                           \n" \
"    double absy = cabs (Y) ;                                           \n" \
"    if (absx < absy)                                                   \n" \
"    {                                                                  \n" \
"        z->re = x->re ;                                                \n" \
"        z->im = x->im ;                                                \n" \
"    }                                                                  \n" \
"    else if (absx > absy)                                              \n" \
"    {                                                                  \n" \
"        z->re = y->re ;                                                \n" \
"        z->im = y->im ;                                                \n" \
"    }                                                                  \n" \
"    else                                                               \n" \
"    {                                                                  \n" \
"        if (carg (X) < carg (Y))                                       \n" \
"        {                                                              \n" \
"            z->re = x->re ;                                            \n" \
"            z->im = x->im ;                                            \n" \
"        }                                                              \n" \
"        else                                                           \n" \
"        {                                                              \n" \
"            z->re = y->re ;                                            \n" \
"            z->im = y->im ;                                            \n" \
"        }                                                              \n" \
"    }                                                                  \n" \
"}"

// max (x,y): complex number with largest magnitude.  If tied, select the one
// with the largest phase angle.  No special cases for NaNs.

void fx64_max (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    double absx = cabs (*x) ;
    double absy = cabs (*y) ;
    if (absx > absy)
    {
        (*z) = (*x) ;
    }
    else if (absx < absy)
    {
        (*z) = (*y) ;
    }
    else
    {
        (*z) = (carg (*x) > carg (*y)) ? (*x) : (*y) ;
    }
}

#define FX64_MAX                                                            \
"void fx64_max (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)\n" \
"{                                                                      \n" \
"    double absx = cabs (*x) ;                                          \n" \
"    double absy = cabs (*y) ;                                          \n" \
"    if (absx > absy)                                                   \n" \
"    {                                                                  \n" \
"        (*z) = (*x) ;                                                  \n" \
"    }                                                                  \n" \
"    else if (absx < absy)                                              \n" \
"    {                                                                  \n" \
"        (*z) = (*y) ;                                                  \n" \
"    }                                                                  \n" \
"    else                                                               \n" \
"    {                                                                  \n" \
"        (*z) = (carg (*x) > carg (*y)) ? (*x) : (*y) ;                 \n" \
"    }                                                                  \n" \
"}"

void mycx_max (mycx *z, const mycx *x, const mycx *y)
{
    GxB_FC64_t X = GxB_CMPLX (x->re, x->im) ;
    GxB_FC64_t Y = GxB_CMPLX (y->re, y->im) ;
    double absx = cabs (X) ;
    double absy = cabs (Y) ;
    if (absx > absy)
    {
        z->re = x->re ;
        z->im = x->im ;
    }
    else if (absx < absy)
    {
        z->re = y->re ;
        z->im = y->im ;
    }
    else
    {
        if (carg (X) > carg (Y))
        {
            z->re = x->re ;
            z->im = x->im ;
        }
        else
        {
            z->re = y->re ;
            z->im = y->im ;
        }
    }
}

#define MYCX_MAX                                                            \
"void mycx_max (mycx *z, const mycx *x, const mycx *y)                  \n" \
"{                                                                      \n" \
"    GxB_FC64_t X = GxB_CMPLX (x->re, x->im) ;                          \n" \
"    GxB_FC64_t Y = GxB_CMPLX (y->re, y->im) ;                          \n" \
"    double absx = cabs (X) ;                                           \n" \
"    double absy = cabs (Y) ;                                           \n" \
"    if (absx > absy)                                                   \n" \
"    {                                                                  \n" \
"        z->re = x->re ;                                                \n" \
"        z->im = x->im ;                                                \n" \
"    }                                                                  \n" \
"    else if (absx < absy)                                              \n" \
"    {                                                                  \n" \
"        z->re = y->re ;                                                \n" \
"        z->im = y->im ;                                                \n" \
"    }                                                                  \n" \
"    else                                                               \n" \
"    {                                                                  \n" \
"        if (carg (X) > carg (Y))                                       \n" \
"        {                                                              \n" \
"            z->re = x->re ;                                            \n" \
"            z->im = x->im ;                                            \n" \
"        }                                                              \n" \
"        else                                                           \n" \
"        {                                                              \n" \
"            z->re = y->re ;                                            \n" \
"            z->im = y->im ;                                            \n" \
"        }                                                              \n" \
"    }                                                                  \n" \
"}"

//------------------------------------------------------------------------------
// 6 binary functions, z=f(x,y); CxC -> Complex ; (1,0) = true, (0,0) = false
//------------------------------------------------------------------------------

void mycx_iseq (mycx *z, const mycx *x, const mycx *y)
{
    z->re = (x->re == y->re && x->im == y->im) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_ISEQ                                                           \
"void mycx_iseq (mycx *z, const mycx *x, const mycx *y)                 \n" \
"{                                                                      \n" \
"    z->re = (x->re == y->re && x->im == y->im) ? 1 : 0 ;               \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void mycx_isne (mycx *z, const mycx *x, const mycx *y)
{
    z->re = (x->re != y->re || x->im != y->im) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_ISNE                                                           \
"void mycx_isne (mycx *z, const mycx *x, const mycx *y)                 \n" \
"{                                                                      \n" \
"    z->re = (x->re != y->re || x->im != y->im) ? 1 : 0 ;               \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void fx64_isgt (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    bool gt = (creal (*x) > creal (*y)) ;
    (*z) = gt ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;
}

#define FX64_ISGT                                                           \
"void fx64_isgt (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)\n"\
"{                                                                      \n" \
"    bool gt = (creal (*x) > creal (*y)) ;                              \n" \
"    (*z) = gt ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;                    \n" \
"}"

void mycx_isgt (mycx *z, const mycx *x, const mycx *y)
{
    z->re = (x->re > y->re) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_ISGT                                                           \
"void mycx_isgt (mycx *z, const mycx *x, const mycx *y)                 \n" \
"{                                                                      \n" \
"    z->re = (x->re > y->re) ? 1 : 0 ;                                  \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void fx64_islt (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    bool lt = (creal (*x) < creal (*y)) ;
    (*z) = lt ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;
}

#define FX64_ISLT                                                           \
"void fx64_islt (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)\n"\
"{                                                                      \n" \
"    bool lt = (creal (*x) < creal (*y)) ;                              \n" \
"    (*z) = lt ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;                    \n" \
"}"

void mycx_islt (mycx *z, const mycx *x, const mycx *y)
{
    z->re = (x->re < y->re) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_ISLT                                                           \
"void mycx_islt (mycx *z, const mycx *x, const mycx *y)                 \n" \
"{                                                                      \n" \
"    z->re = (x->re < y->re) ? 1 : 0 ;                                  \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void fx64_isge (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    bool ge = (creal (*x) >= creal (*y)) ;
    (*z) = ge ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;
}

#define FX64_ISGE                                                           \
"void fx64_isge (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)\n"\
"{                                                                      \n" \
"    bool ge = (creal (*x) >= creal (*y)) ;                             \n" \
"    (*z) = ge ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;                    \n" \
"}"

void mycx_isge (mycx *z, const mycx *x, const mycx *y)
{
    z->re = (x->re >= y->re) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_ISGE                                                           \
"void mycx_isge (mycx *z, const mycx *x, const mycx *y)                 \n" \
"{                                                                      \n" \
"    z->re = (x->re >= y->re) ? 1 : 0 ;                                 \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void fx64_isle (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    bool le = (creal (*x) <= creal (*y)) ;
    (*z) = le ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;
}

#define FX64_ISLE                                                           \
"void fx64_isle (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)\n"\
"{                                                                      \n" \
"    bool le = (creal (*x) <= creal (*y)) ;                             \n" \
"    (*z) = le ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;                    \n" \
"}"

void mycx_isle (mycx *z, const mycx *x, const mycx *y)
{
    z->re = (x->re <= y->re) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_ISLE                                                           \
"void mycx_isle (mycx *z, const mycx *x, const mycx *y)                 \n" \
"{                                                                      \n" \
"    z->re = (x->re <= y->re) ? 1 : 0 ;                                 \n" \
"    z->im = 0 ;                                                        \n" \
"}"

//------------------------------------------------------------------------------
// binary boolean functions, z=f(x,y), where CxC -> Complex
//------------------------------------------------------------------------------

void fx64_or (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    bool xbool = (creal (*x) != 0 || cimag (*x) != 0) ;
    bool ybool = (creal (*y) != 0 || cimag (*y) != 0) ;
    (*z) = (xbool || ybool) ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;
}

#define FX64_OR                                                             \
"void fx64_or (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y) \n" \
"{                                                                      \n" \
"    bool xbool = (creal (*x) != 0 || cimag (*x) != 0) ;                \n" \
"    bool ybool = (creal (*y) != 0 || cimag (*y) != 0) ;                \n" \
"    (*z) = (xbool || ybool) ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;      \n" \
"}"

void mycx_or (mycx *z, const mycx *x, const mycx *y)
{
    bool xbool = (x->re != 0 || x->im != 0) ;
    bool ybool = (y->re != 0 || y->im != 0) ;
    z->re = (xbool || ybool) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_OR                                                             \
"void mycx_or (mycx *z, const mycx *x, const mycx *y)                   \n" \
"{                                                                      \n" \
"    bool xbool = (x->re != 0 || x->im != 0) ;                          \n" \
"    bool ybool = (y->re != 0 || y->im != 0) ;                          \n" \
"    z->re = (xbool || ybool) ? 1 : 0 ;                                 \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void fx64_and (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    bool xbool = (creal (*x) != 0 || cimag (*x) != 0) ;
    bool ybool = (creal (*y) != 0 || cimag (*y) != 0) ;
    (*z) = (xbool && ybool) ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;
}

#define FX64_AND                                                            \
"void fx64_and (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)\n" \
"{                                                                      \n" \
"    bool xbool = (creal (*x) != 0 || cimag (*x) != 0) ;                \n" \
"    bool ybool = (creal (*y) != 0 || cimag (*y) != 0) ;                \n" \
"    (*z) = (xbool && ybool) ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;      \n" \
"}"

void mycx_and (mycx *z, const mycx *x, const mycx *y)
{
    bool xbool = (x->re != 0 || x->im != 0) ;
    bool ybool = (y->re != 0 || y->im != 0) ;
    z->re = (xbool && ybool) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_AND                                                            \
"void mycx_and (mycx *z, const mycx *x, const mycx *y)                  \n" \
"{                                                                      \n" \
"    bool xbool = (x->re != 0 || x->im != 0) ;                          \n" \
"    bool ybool = (y->re != 0 || y->im != 0) ;                          \n" \
"    z->re = (xbool && ybool) ? 1 : 0 ;                                 \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void fx64_xor (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    bool xbool = (creal (*x) != 0 || cimag (*x) != 0) ;
    bool ybool = (creal (*y) != 0 || cimag (*y) != 0) ;
    (*z) = (xbool != ybool) ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;
}

#define FX64_XOR                                                            \
"void fx64_xor (GxB_FC64_t *z, const GxB_FC64_t *x, const GxB_FC64_t *y)\n" \
"{                                                                      \n" \
"    bool xbool = (creal (*x) != 0 || cimag (*x) != 0) ;                \n" \
"    bool ybool = (creal (*y) != 0 || cimag (*y) != 0) ;                \n" \
"    (*z) = (xbool != ybool) ? GxB_CMPLX (1,0) : GxB_CMPLX (0,0) ;      \n" \
"}"

void mycx_xor (mycx *z, const mycx *x, const mycx *y)
{
    bool xbool = (x->re != 0 || x->im != 0) ;
    bool ybool = (y->re != 0 || y->im != 0) ;
    z->re = (xbool && ybool) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_XOR                                                            \
"void mycx_xor (mycx *z, const mycx *x, const mycx *y)                  \n" \
"{                                                                      \n" \
"    bool xbool = (x->re != 0 || x->im != 0) ;                          \n" \
"    bool ybool = (y->re != 0 || y->im != 0) ;                          \n" \
"    z->re = (xbool != ybool) ? 1 : 0 ;                                 \n" \
"    z->im = 0 ;                                                        \n" \
"}"

//------------------------------------------------------------------------------
// 6 binary functions, z=f(x,y), where CxC -> bool
//------------------------------------------------------------------------------

void mycx_eq (bool *z, const mycx *x, const mycx *y)
{
    (*z) = (x->re == y->re && x->im == y->im) ;
}

#define MYCX_EQ                                                             \
"void mycx_eq (bool *z, const mycx *x, const mycx *y)                   \n" \
"{                                                                      \n" \
"    (*z) = (x->re == y->re && x->im == y->im) ;                        \n" \
"}"

void mycx_ne (bool *z, const mycx *x, const mycx *y)
{
    (*z) = (x->re == y->re && x->im == y->im) ;
}

#define MYCX_NE                                                             \
"void mycx_ne (bool *z, const mycx *x, const mycx *y)                   \n" \
"{                                                                      \n" \
"    (*z) = (x->re == y->re && x->im == y->im) ;                        \n" \
"}"

void fx64_gt (bool *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    (*z) = (creal (*x) > creal (*y)) ;
}

#define FX64_GT                                                             \
"void fx64_gt (bool *z, const GxB_FC64_t *x, const GxB_FC64_t *y)       \n" \
"{                                                                      \n" \
"    (*z) = (creal (*x) > creal (*y)) ;                                 \n" \
"}"

void mycx_gt (bool *z, const mycx *x, const mycx *y)
{
    (*z) = (x->re > y->re) ;
}

#define MYCX_GT                                                             \
"void mycx_gt (bool *z, const mycx *x, const mycx *y)                   \n" \
"{                                                                      \n" \
"    (*z) = (x->re > y->re) ;                                           \n" \
"}"

void fx64_lt (bool *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    (*z) = (creal (*x) > creal (*y)) ;
}

#define FX64_LT                                                             \
"void fx64_lt (bool *z, const GxB_FC64_t *x, const GxB_FC64_t *y)       \n" \
"{                                                                      \n" \
"    (*z) = (creal (*x) < creal (*y)) ;                                 \n" \
"}"

void mycx_lt (bool *z, const mycx *x, const mycx *y)
{
    (*z) = (x->re < y->re) ;
}

#define MYCX_LT                                                             \
"void mycx_lt (bool *z, const mycx *x, const mycx *y)                   \n" \
"{                                                                      \n" \
"    (*z) = (x->re < y->re) ;                                           \n" \
"}"

void fx64_ge (bool *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    (*z) = (creal (*x) >= creal (*y)) ;
}

#define FX64_GE                                                             \
"void fx64_ge (bool *z, const GxB_FC64_t *x, const GxB_FC64_t *y)       \n" \
"{                                                                      \n" \
"    (*z) = (creal (*x) >= creal (*y)) ;                                \n" \
"}"

void mycx_ge (bool *z, const mycx *x, const mycx *y)
{
    (*z) = (x->re >= y->re) ;
}

#define MYCX_GE                                                             \
"void mycx_ge (bool *z, const mycx *x, const mycx *y)                   \n" \
"{                                                                      \n" \
"    (*z) = (x->re >= y->re) ;                                          \n" \
"}"

void fx64_le (bool *z, const GxB_FC64_t *x, const GxB_FC64_t *y)
{
    (*z) = (creal (*x) >= creal (*y)) ;
}

#define FX64_LE                                                             \
"void fx64_lt (bool *z, const GxB_FC64_t *x, const GxB_FC64_t *y)       \n" \
"{                                                                      \n" \
"    (*z) = (creal (*x) <= creal (*y)) ;                                \n" \
"}"

void mycx_le (bool *z, const mycx *x, const mycx *y)
{
    (*z) = (x->re <= y->re) ;
}

#define MYCX_LE                                                             \
"void mycx_le (bool *z, const mycx *x, const mycx *y)                   \n" \
"{                                                                      \n" \
"    (*z) = (x->re <= y->re) ;                                          \n" \
"}"


//------------------------------------------------------------------------------
// binary functions, z=f(x,y), where double x double -> complex
//------------------------------------------------------------------------------

void mycx_cmplx (mycx *z, const double *x, const double *y)
{
    z->re = (*x) ;
    z->im = (*y) ;
}

#define MYCX_CMPLX                                                          \
"void mycx_cmplx (mycx *z, const double *x, const double *y)            \n" \
"{                                                                      \n" \
"    z->re = (*x) ;                                                     \n" \
"    z->im = (*y) ;                                                     \n" \
"}"

//------------------------------------------------------------------------------
// unary functions, z=f(x) where Complex -> Complex
//------------------------------------------------------------------------------

void mycx_one (mycx *z, const mycx *x)
{
    z->re = 1 ;
    z->im = 0 ;
}

#define MYCX_ONE                                                            \
"void mycx_one (mycx *z, const mycx *x)                                 \n" \
"{                                                                      \n" \
"    z->re = 1 ;                                                        \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void mycx_identity (mycx *z, const mycx *x)
{
    z->re = x->re ;
    z->im = x->im ;
}

#define MYCX_IDENTITY                                                       \
"void mycx_identity (mycx *z, const mycx *x)                            \n" \
"{                                                                      \n" \
"    z->re = x->re ;                                                    \n" \
"    z->im = x->im ;                                                    \n" \
"}"

void mycx_ainv (mycx *z, const mycx *x)
{
    z->re = -(x->re) ;
    z->im = -(x->im) ;
}

#define MYCX_AINV                                                           \
"void mycx_ainv (mycx *z, const mycx *x)                                \n" \
"{                                                                      \n" \
"    z->re = -(x->re) ;                                                 \n" \
"    z->im = -(x->im) ;                                                 \n" \
"}"

void mycx_minv (mycx *z, const mycx *x)
{
    double den = (x->re * x->re) + (x->im * x->im) ;
    z->re =  (x->re) / den ;
    z->im = -(x->im) / den ;
}

#define MYCX_MINV                                                           \
"void mycx_minv (mycx *z, const mycx *x)                                \n" \
"{                                                                      \n" \
"    double den = (x->re * x->re) + (x->im * x->im) ;                   \n" \
"    z->re =  (x->re) / den ;                                           \n" \
"    z->im = -(x->im) / den ;                                           \n" \
"}"

void mycx_conj (mycx *z, const mycx *x)
{
    z->re =  (x->re) ;
    z->im = -(x->im) ;
}

#define MYCX_CONJ                                                           \
"void mycx_conj (mycx *z, const mycx *x)                                \n" \
"{                                                                      \n" \
"    z->re =  (x->re) ;                                                 \n" \
"    z->im = -(x->im) ;                                                 \n" \
"}"

void fx64_abs (GxB_FC64_t *z, const GxB_FC64_t *x)
{
    (*z) = GxB_CMPLX (cabs (*x), 0) ;
}

#define FX64_ABS                                                            \
"void fx64_abs (GxB_FC64_t *z, const GxB_FC64_t *x)                     \n" \
"{                                                                      \n" \
"    (*z) = GxB_CMPLX (cabs (*x), 0) ;                                  \n" \
"}"

void mycx_abs (mycx *z, const mycx *x)
{
    z->re = sqrt ((x->re * x->re) + (x->im * x->im)) ;
    z->im = 0 ;
}

#define MYCX_ABS                                                            \
"void mycx_abs (mycx *z, const mycx *x)                                 \n" \
"{                                                                      \n" \
"    z->re = sqrt ((x->re * x->re) + (x->im * x->im)) ;                 \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void fx64_not (GxB_FC64_t *z, const GxB_FC64_t *x)
{
    bool xbool = (creal (*x) != 0 || cimag (*x) != 0) ;
    (*z) = xbool ? GxB_CMPLX (0,0) : GxB_CMPLX (1,0) ;
}

#define FX64_NOT                                                            \
"void fx64_not (GxB_FC64_t *z, const GxB_FC64_t *x)                     \n" \
"{                                                                      \n" \
"   bool xbool = (creal (*x) != 0 || cimag (*x) != 0) ;                 \n" \
"   (*z) = xbool ? GxB_CMPLX (0,0) : GxB_CMPLX (1,0) ;                  \n" \
"}"

void mycx_not (mycx *z, const mycx *x)
{
    z->re = (x->re != 0 || x->im != 0) ? 1 : 0 ;
    z->im = 0 ;
}

#define MYCX_NOT                                                            \
"void mycx_not (mycx *z, const mycx *x)                                 \n" \
"{                                                                      \n" \
"    z->re = (x->re != 0 || x->im != 0) ? 1 : 0 ;                       \n" \
"    z->im = 0 ;                                                        \n" \
"}"

//------------------------------------------------------------------------------
// unary functions, z=f(x) where Complex -> double
//------------------------------------------------------------------------------

void mycx_real (double *z, const mycx *x)
{
    (*z) = x->re ;
}

#define MYCX_REAL                                                           \
"void mycx_real (double *z, const mycx *x)                              \n" \
"{                                                                      \n" \
"    (*z) = x->re ;                                                     \n" \
"}"

void mycx_imag (double *z, const mycx *x)
{
    (*z) = x->im ;
}

#define MYCX_IMAG                                                           \
"void mycx_imag (double *z, const mycx *x)                              \n" \
"{                                                                      \n" \
"    (*z) = x->im ;                                                     \n" \
"}"

void mycx_cabs (double *z, const mycx *x)
{
    (*z) = sqrt ((x->re * x->re) + (x->im * x->im)) ;
}

#define MYCX_CABS                                                           \
"void mycx_cabs (double *z, const mycx *x)                              \n" \
"{                                                                      \n" \
"    (*z) = sqrt ((x->re * x->re) + (x->im * x->im)) ;                  \n" \
"}"

void mycx_angle (double *z, const mycx *x)
{
    GxB_FC64_t X = GxB_CMPLX (x->re, x->im) ;
    (*z) = carg (X) ;
}

#define MYCX_ANGLE                                                          \
"void mycx_angle (double *z, const mycx *x)                             \n" \
"{                                                                      \n" \
"    GxB_FC64_t X = GxB_CMPLX (x->re, x->im) ;                          \n" \
"    (*z) = carg (X) ;                                                  \n" \
"}"

//------------------------------------------------------------------------------
// unary functions, z=f(x) where double -> Complex
//------------------------------------------------------------------------------

void fx64_cmplx_real (GxB_FC64_t *z, const double *x)
{
    (*z) = GxB_CMPLX ((*x), 0) ;
}

#define FX64_CMPLX_REAL                                                     \
"void fx64_cmplx_real (GxB_FC64_t *z, const double *x)                  \n" \
"{                                                                      \n" \
"    (*z) = GxB_CMPLX ((*x), 0) ;                                       \n" \
"}"

void mycx_cmplx_real (mycx *z, const double *x)
{
    z->re = (*x) ;
    z->im = 0 ;
}

#define MYCX_CMPLX_REAL                                                     \
"void mycx_cmplx_real (mycx *z, const double *x)                        \n" \
"{                                                                      \n" \
"    z->re = (*x) ;                                                     \n" \
"    z->im = 0 ;                                                        \n" \
"}"

void fx64_cmplx_imag (GxB_FC64_t *z, const double *x)
{
    (*z) = GxB_CMPLX (0, (*x)) ;
}

#define FX64_CMPLX_IMAG                                                     \
"void fx64_cmplx_imag (GxB_FC64_t *z, const double *x)                  \n" \
"{                                                                      \n" \
"    (*z) = GxB_CMPLX (0, (*x)) ;                                       \n" \
"}"

void mycx_cmplx_imag (mycx *z, const double *x)
{
    z->re = 0 ;
    z->im = (*x) ;
}

#define MYCX_CMPLX_IMAG                                                     \
"void mycx_cmplx_imag (mycx *z, const double *x)                        \n" \
"{                                                                      \n" \
"    z->re = 0 ;                                                        \n" \
"    z->im = (*x) ;                                                     \n" \
"}"

//------------------------------------------------------------------------------
// Complex_init: create the complex type, operators, monoids, and semiring
//------------------------------------------------------------------------------

#define U (GxB_unary_function)
#define B (GxB_binary_function)

GrB_Info Complex_init (bool builtin_complex)
{

    GrB_Info info ;

    //--------------------------------------------------------------------------
    // create the Complex type, or set to GxB_FC64
    //--------------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in type
        Complex = GxB_FC64 ;
    }
    else
    {
        // create the user-defined type
        // Normally, the typename should be "GxB_FC64_t",
        // but the C type GxB_FC64_t is already defined.
        OK (GxB_Type_new (&Complex, sizeof (GxB_FC64_t), "mycx", MYCX_DEFN)) ;
    }

    //--------------------------------------------------------------------------
    // create the Complex binary operators, CxC->Complex
    //--------------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in versions
        Complex_first  = GxB_FIRST_FC64 ;
        Complex_second = GxB_SECOND_FC64 ;
        Complex_pair   = GxB_PAIR_FC64 ;
        Complex_plus   = GxB_PLUS_FC64 ;
        Complex_minus  = GxB_MINUS_FC64 ;
        Complex_rminus = GxB_RMINUS_FC64 ;
        Complex_times  = GxB_TIMES_FC64 ;
        Complex_div    = GxB_DIV_FC64 ;
        Complex_rdiv   = GxB_RDIV_FC64 ;
    }
    else
    {
        // create user-defined versions
        OK (GxB_BinaryOp_new (&Complex_first  , B mycx_first  , Complex, Complex, Complex, "mycx_first" , MYCX_FIRST)) ;
        OK (GxB_BinaryOp_new (&Complex_second , B mycx_second , Complex, Complex, Complex, "mycx_second", MYCX_SECOND)) ;
        OK (GxB_BinaryOp_new (&Complex_pair   , B mycx_pair   , Complex, Complex, Complex, "mycx_pair"  , MYCX_PAIR)) ;
        OK (GxB_BinaryOp_new (&Complex_plus   , B mycx_plus   , Complex, Complex, Complex, "mycx_plus"  , MYCX_PLUS)) ;
        OK (GxB_BinaryOp_new (&Complex_minus  , B mycx_minus  , Complex, Complex, Complex, "mycx_minus" , MYCX_MINUS)) ;
        OK (GxB_BinaryOp_new (&Complex_rminus , B mycx_rminus , Complex, Complex, Complex, "mycx_rminus", MYCX_RMINUS)) ;
        OK (GxB_BinaryOp_new (&Complex_times  , B mycx_times  , Complex, Complex, Complex, "mycx_times" , MYCX_TIMES)) ;
        OK (GxB_BinaryOp_new (&Complex_div    , B mycx_div    , Complex, Complex, Complex, "mycx_div"   , MYCX_DIV)) ;
        OK (GxB_BinaryOp_new (&Complex_rdiv   , B mycx_rdiv   , Complex, Complex, Complex, "mycx_rdiv"  , MYCX_RDIV)) ;
    }

    // these are not built-in
    if (builtin_complex)
    {
        OK (GxB_BinaryOp_new (&Complex_min    , B fx64_min    , Complex, Complex, Complex, "fx64_min"   , FX64_MIN)) ;
        OK (GxB_BinaryOp_new (&Complex_max    , B fx64_max    , Complex, Complex, Complex, "fx64_max"   , FX64_MAX)) ;
    }
    else
    {
        OK (GxB_BinaryOp_new (&Complex_min    , B mycx_min    , Complex, Complex, Complex, "mycx_min"   , MYCX_MIN)) ;
        OK (GxB_BinaryOp_new (&Complex_max    , B mycx_max    , Complex, Complex, Complex, "mycx_max"   , MYCX_MAX)) ;
    }

    //--------------------------------------------------------------------------
    // create the Complex binary comparators, CxC -> Complex
    //--------------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in versions
        Complex_iseq = GxB_ISEQ_FC64 ;
        Complex_isne = GxB_ISNE_FC64 ;
    }
    else
    {
        // create user-defined versions
        OK (GxB_BinaryOp_new (&Complex_iseq   , B mycx_iseq   , Complex, Complex, Complex, "mycx_iseq"  , MYCX_ISEQ)) ;
        OK (GxB_BinaryOp_new (&Complex_isne   , B mycx_isne   , Complex, Complex, Complex, "mycx_isne"  , MYCX_ISNE)) ;
    }

    // these are not built-in
    if (builtin_complex)
    {
        OK (GxB_BinaryOp_new (&Complex_isgt   , B fx64_isgt   , Complex, Complex, Complex, "fx64_isgt"  , FX64_ISGT)) ;
        OK (GxB_BinaryOp_new (&Complex_islt   , B fx64_islt   , Complex, Complex, Complex, "fx64_islt"  , FX64_ISLT)) ;
        OK (GxB_BinaryOp_new (&Complex_isge   , B fx64_isge   , Complex, Complex, Complex, "fx64_isge"  , FX64_ISGE)) ;
        OK (GxB_BinaryOp_new (&Complex_isle   , B fx64_isle   , Complex, Complex, Complex, "fx64_isle"  , FX64_ISLE)) ;
    }
    else
    {
        OK (GxB_BinaryOp_new (&Complex_isgt   , B mycx_isgt   , Complex, Complex, Complex, "mycx_isgt"  , MYCX_ISGT)) ;
        OK (GxB_BinaryOp_new (&Complex_islt   , B mycx_islt   , Complex, Complex, Complex, "mycx_islt"  , MYCX_ISLT)) ;
        OK (GxB_BinaryOp_new (&Complex_isge   , B mycx_isge   , Complex, Complex, Complex, "mycx_isge"  , MYCX_ISGE)) ;
        OK (GxB_BinaryOp_new (&Complex_isle   , B mycx_isle   , Complex, Complex, Complex, "mycx_isle"  , MYCX_ISLE)) ;
    }

    //--------------------------------------------------------------------------
    // create the Complex boolean operators, CxC -> Complex
    //--------------------------------------------------------------------------

    // these are not built-in
    if (builtin_complex)
    {
        OK (GxB_BinaryOp_new (&Complex_or     , B fx64_or     , Complex, Complex, Complex, "fx64_or"    , FX64_OR)) ;
        OK (GxB_BinaryOp_new (&Complex_and    , B fx64_and    , Complex, Complex, Complex, "fx64_and"   , FX64_AND)) ;
        OK (GxB_BinaryOp_new (&Complex_xor    , B fx64_xor    , Complex, Complex, Complex, "fx64_xor"   , FX64_XOR)) ;
    }
    else
    {
        OK (GxB_BinaryOp_new (&Complex_or     , B mycx_or     , Complex, Complex, Complex, "mycx_or"    , MYCX_OR)) ;
        OK (GxB_BinaryOp_new (&Complex_and    , B mycx_and    , Complex, Complex, Complex, "mycx_and"   , MYCX_AND)) ;
        OK (GxB_BinaryOp_new (&Complex_xor    , B mycx_xor    , Complex, Complex, Complex, "mycx_xor"   , MYCX_XOR)) ;
    }

    //--------------------------------------------------------------------------
    // create the Complex binary operators, CxC -> bool
    //--------------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in versions
        Complex_eq = GxB_EQ_FC64 ;
        Complex_ne = GxB_NE_FC64 ;
    }
    else
    {
        // create user-defined versions
        OK (GxB_BinaryOp_new (&Complex_eq     , B mycx_eq     ,GrB_BOOL, Complex, Complex, "mycx_eq"    , MYCX_EQ)) ;
        OK (GxB_BinaryOp_new (&Complex_ne     , B mycx_ne     ,GrB_BOOL, Complex, Complex, "mycx_ne"    , MYCX_NE)) ;
    }

    // these are not built-in
    if (builtin_complex)
    {
        OK (GxB_BinaryOp_new (&Complex_gt     , B fx64_gt     ,GrB_BOOL, Complex, Complex, "fx64_gt"    , FX64_GT)) ;
        OK (GxB_BinaryOp_new (&Complex_lt     , B fx64_lt     ,GrB_BOOL, Complex, Complex, "fx64_lt"    , FX64_LT)) ;
        OK (GxB_BinaryOp_new (&Complex_ge     , B fx64_ge     ,GrB_BOOL, Complex, Complex, "fx64_ge"    , FX64_GE)) ;
        OK (GxB_BinaryOp_new (&Complex_le     , B fx64_le     ,GrB_BOOL, Complex, Complex, "fx64_le"    , FX64_LE)) ;
    }
    else
    {
        OK (GxB_BinaryOp_new (&Complex_gt     , B mycx_gt     ,GrB_BOOL, Complex, Complex, "mycx_gt"    , MYCX_GT)) ;
        OK (GxB_BinaryOp_new (&Complex_lt     , B mycx_lt     ,GrB_BOOL, Complex, Complex, "mycx_lt"    , MYCX_LT)) ;
        OK (GxB_BinaryOp_new (&Complex_ge     , B mycx_ge     ,GrB_BOOL, Complex, Complex, "mycx_ge"    , MYCX_GE)) ;
        OK (GxB_BinaryOp_new (&Complex_le     , B mycx_le     ,GrB_BOOL, Complex, Complex, "mycx_le"    , MYCX_LE)) ;
    }

    //--------------------------------------------------------------------------
    // create the Complex binary operator, double x double -> Complex
    //--------------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in versions
        Complex_complex = GxB_CMPLX_FP64 ;
    }
    else
    {
        // create user-defined versions
        OK (GxB_BinaryOp_new (&Complex_complex, B mycx_cmplx  , Complex,GrB_FP64,GrB_FP64, "mycx_cmplx" ,MYCX_CMPLX)) ;
    }

    //--------------------------------------------------------------------------
    // create the Complex unary operators, Complex->Complex
    //--------------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in versions
        Complex_one      = GxB_ONE_FC64 ;
        Complex_identity = GxB_IDENTITY_FC64 ;
        Complex_ainv     = GxB_AINV_FC64 ;
        Complex_minv     = GxB_MINV_FC64 ;
        Complex_conj     = GxB_CONJ_FC64 ;
    }
    else
    {
        // create user-defined versions
        OK (GxB_UnaryOp_new (&Complex_one     , U mycx_one     , Complex, Complex, "mycx_one"     , MYCX_ONE)) ;
        OK (GxB_UnaryOp_new (&Complex_identity, U mycx_identity, Complex, Complex, "mycx_identity", MYCX_IDENTITY)) ;
        OK (GxB_UnaryOp_new (&Complex_ainv    , U mycx_ainv    , Complex, Complex, "mycx_ainv"    , MYCX_AINV)) ;
        OK (GxB_UnaryOp_new (&Complex_minv    , U mycx_minv    , Complex, Complex, "mycx_minv"    , MYCX_MINV)) ;
        OK (GxB_UnaryOp_new (&Complex_conj    , U mycx_conj    , Complex, Complex, "mycx_conj"    , MYCX_CONJ)) ;
    }

    // these are not built-in
    if (builtin_complex)
    {
        OK (GxB_UnaryOp_new (&Complex_abs     , U fx64_abs     , Complex, Complex, "fx64_abs"     , FX64_ABS)) ;
        OK (GxB_UnaryOp_new (&Complex_not     , U fx64_not     , Complex, Complex, "fx64_not"     , FX64_NOT)) ;
    }
    else
    {
        OK (GxB_UnaryOp_new (&Complex_abs     , U mycx_abs     , Complex, Complex, "mycx_abs"     , MYCX_ABS)) ;
        OK (GxB_UnaryOp_new (&Complex_not     , U mycx_not     , Complex, Complex, "mycx_not"     , MYCX_NOT)) ;
    }

    //--------------------------------------------------------------------------
    // create the unary functions, Complex -> double
    //--------------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in versions
        Complex_real  = GxB_CREAL_FC64 ;
        Complex_imag  = GxB_CIMAG_FC64 ;
        Complex_cabs  = GxB_ABS_FC64 ;
        Complex_angle = GxB_CARG_FC64 ;
    }
    else
    {
        // create user-defined versions
        OK (GxB_UnaryOp_new (&Complex_real    , U mycx_real    ,GrB_FP64, Complex, "mycx_real"    , MYCX_REAL)) ;
        OK (GxB_UnaryOp_new (&Complex_imag    , U mycx_imag    ,GrB_FP64, Complex, "mycx_imag"    , MYCX_IMAG)) ;
        OK (GxB_UnaryOp_new (&Complex_cabs    , U mycx_cabs    ,GrB_FP64, Complex, "mycx_cabs"    , MYCX_CABS)) ;
        OK (GxB_UnaryOp_new (&Complex_angle   , U mycx_angle   ,GrB_FP64, Complex, "mycx_angle"   , MYCX_ANGLE)) ;
    }

    //--------------------------------------------------------------------------
    // create the unary functions, double -> Complex
    //--------------------------------------------------------------------------

    // these are not built-in
    if (builtin_complex)
    {
        OK (GxB_UnaryOp_new (&Complex_complex_real, U fx64_cmplx_real, Complex, GrB_FP64, "fx64_cmplx_real", FX64_CMPLX_REAL)) ;
        OK (GxB_UnaryOp_new (&Complex_complex_imag, U fx64_cmplx_imag, Complex, GrB_FP64, "fx64_cmplx_imag", FX64_CMPLX_IMAG)) ;
    }
    else
    {
        OK (GxB_UnaryOp_new (&Complex_complex_real, U mycx_cmplx_real, Complex, GrB_FP64, "mycx_cmplx_real", MYCX_CMPLX_REAL)) ;
        OK (GxB_UnaryOp_new (&Complex_complex_imag, U mycx_cmplx_imag, Complex, GrB_FP64, "mycx_cmplx_imag", MYCX_CMPLX_IMAG)) ;
    }

    //--------------------------------------------------------------------------
    // create the Complex monoids
    //--------------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in versions
        Complex_plus_monoid  = GxB_PLUS_FC64_MONOID ;
        Complex_times_monoid = GxB_TIMES_FC64_MONOID ;
    }
    else
    {
        // create user-defined versions
        mycx one, zero ;
        one.re = 1 ;
        one.im = 0 ;
        zero.re = 0 ;
        zero.im = 0 ;
        OK (GrB_Monoid_new_UDT (&Complex_plus_monoid,  Complex_plus,  (void *) &zero)) ;
        OK (GrB_Monoid_new_UDT (&Complex_times_monoid, Complex_times, (void *) &one)) ;
    }

    //----------------------------------------------------------------------
    // create the Complex plus-times semiring
    //----------------------------------------------------------------------

    if (builtin_complex)
    {
        // use the built-in versions
        Complex_plus_times = GxB_PLUS_TIMES_FC64 ;
    }
    else
    {
        // more could be created, but this suffices for testing GraphBLAS
        OK (GrB_Semiring_new (&Complex_plus_times, Complex_plus_monoid, Complex_times)) ;
    }

    return (GrB_SUCCESS) ;
}


//------------------------------------------------------------------------------
// Complex_finalize: free all complex types, operators, monoids, and semiring
//------------------------------------------------------------------------------

// These may be built-in types and operators.  They are safe to free; the
// GrB_*_free functions silently do nothing if asked to free bulit-in objects.

GrB_Info Complex_finalize ( )
{

    //--------------------------------------------------------------------------
    // free the Complex plus-times semiring
    //--------------------------------------------------------------------------

    GrB_Semiring_free (&Complex_plus_times) ;

    //--------------------------------------------------------------------------
    // free the Complex monoids
    //--------------------------------------------------------------------------

    GrB_Monoid_free (&Complex_plus_monoid ) ;
    GrB_Monoid_free (&Complex_times_monoid) ;

    //--------------------------------------------------------------------------
    // free the Complex binary operators, CxC->complex
    //--------------------------------------------------------------------------

    GrB_BinaryOp_free (&Complex_first ) ;
    GrB_BinaryOp_free (&Complex_second) ;
    GrB_BinaryOp_free (&Complex_pair  ) ;
    GrB_BinaryOp_free (&Complex_min   ) ;
    GrB_BinaryOp_free (&Complex_max   ) ;
    GrB_BinaryOp_free (&Complex_plus  ) ;
    GrB_BinaryOp_free (&Complex_minus ) ;
    GrB_BinaryOp_free (&Complex_rminus) ;
    GrB_BinaryOp_free (&Complex_times ) ;
    GrB_BinaryOp_free (&Complex_div   ) ;
    GrB_BinaryOp_free (&Complex_rdiv  ) ;

    GrB_BinaryOp_free (&Complex_iseq) ;
    GrB_BinaryOp_free (&Complex_isne) ;
    GrB_BinaryOp_free (&Complex_isgt) ;
    GrB_BinaryOp_free (&Complex_islt) ;
    GrB_BinaryOp_free (&Complex_isge) ;
    GrB_BinaryOp_free (&Complex_isle) ;

    GrB_BinaryOp_free (&Complex_or) ;
    GrB_BinaryOp_free (&Complex_and) ;
    GrB_BinaryOp_free (&Complex_xor) ;

    //--------------------------------------------------------------------------
    // free the Complex binary operators, CxC -> bool
    //--------------------------------------------------------------------------

    GrB_BinaryOp_free (&Complex_eq) ;
    GrB_BinaryOp_free (&Complex_ne) ;
    GrB_BinaryOp_free (&Complex_gt) ;
    GrB_BinaryOp_free (&Complex_lt) ;
    GrB_BinaryOp_free (&Complex_ge) ;
    GrB_BinaryOp_free (&Complex_le) ;

    //--------------------------------------------------------------------------
    // free the Complex binary operator, double x double -> complex
    //--------------------------------------------------------------------------

    GrB_BinaryOp_free (&Complex_complex) ;

    //--------------------------------------------------------------------------
    // free the Complex unary operators, complex->complex
    //--------------------------------------------------------------------------

    GrB_UnaryOp_free (&Complex_one     ) ;
    GrB_UnaryOp_free (&Complex_identity) ;
    GrB_UnaryOp_free (&Complex_ainv    ) ;
    GrB_UnaryOp_free (&Complex_abs     ) ;
    GrB_UnaryOp_free (&Complex_minv    ) ;
    GrB_UnaryOp_free (&Complex_not     ) ;
    GrB_UnaryOp_free (&Complex_conj    ) ;

    //--------------------------------------------------------------------------
    // free the unary functions, complex -> double
    //--------------------------------------------------------------------------

    GrB_UnaryOp_free (&Complex_real ) ;
    GrB_UnaryOp_free (&Complex_imag ) ;
    GrB_UnaryOp_free (&Complex_cabs ) ;
    GrB_UnaryOp_free (&Complex_angle) ;

    //--------------------------------------------------------------------------
    // free the unary functions, double -> complex
    //--------------------------------------------------------------------------

    GrB_UnaryOp_free (&Complex_complex_real) ;
    GrB_UnaryOp_free (&Complex_complex_imag) ;

    //--------------------------------------------------------------------------
    // free the Complex type
    //--------------------------------------------------------------------------

    GrB_Type_free (&Complex) ;

    return (GrB_SUCCESS) ;
}

#undef U
#undef B
#undef FREE_ALL