-
Notifications
You must be signed in to change notification settings - Fork 0
/
pf_qdot.c
852 lines (676 loc) · 25.9 KB
/
pf_qdot.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
//***************************************************************************
// NARS2000 -- Primitive Function -- QuoteDot
//***************************************************************************
/***************************************************************************
NARS2000 -- An Experimental APL Interpreter
Copyright (C) 2006-2016 Sudley Place Software
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
#define STRICT
#include <windows.h>
#include <math.h>
#define __GSL_MATRIX_COMPLEX_LONG_DOUBLE_H__
#include "headers.h"
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_sf_result.h>
#include <gsl/gsl_errno.h>
#ifndef PROTO
PRIMSPEC PrimSpecQuoteDot =
{
// Monadic functions
&PrimFnMon_EM_YY,
&PrimSpecQuoteDotStorageTypeMon,
NULL, // &PrimFnMonQuoteDotAPA_EM, -- Can't happen w/QuoteDot
&PrimFnMonQuoteDotBisB,
NULL, // &PrimFnMonQuoteDotBisI, -- Can't happen w/QuoteDot
NULL, // &PrimFnMonQuoteDotBisF, -- Can't happen w/QuoteDot
//// IisB, // Handled via type promotion (to IisI)
&PrimFnMonQuoteDotIisI,
NULL, // &PrimFnMonQuoteDotIisF, -- Can't happen w/QuoteDot
//// FisB, // Handled via type promotion (to FisI)
&PrimFnMonQuoteDotFisI,
&PrimFnMonQuoteDotFisF,
&PrimFnMonQuoteDotRisR,
//// VisR, // Handled via type promotion (to VisV)
&PrimFnMonQuoteDotVisV,
// Dyadic functions
&PrimFnDyd_EM_YY,
&PrimSpecQuoteDotStorageTypeDyd,
NULL, // &PrimFnDydQuoteDotAPA_EM, -- Can't happen w/QuoteDot
&PrimFnDydQuoteDotBisBvB,
NULL, // &PrimFnDydQuoteDotBisIvI, -- Can't happen w/QuoteDot
NULL, // &PrimFnDydQuoteDotBisFvF, -- Can't happen w/QuoteDot
NULL, // &PrimFnDydQuoteDotBisCvC, -- Can't happen w/QuoteDot
//// IisBvB, // Handled via type promotion (to IisIvI)
&PrimFnDydQuoteDotIisIvI,
NULL, // &PrimFnDydQuoteDotIisFvF, -- Can't happen w/QuoteDot
//// FisBvB, // Handled via type promotion (to FisIvI)
&PrimFnDydQuoteDotFisIvI,
&PrimFnDydQuoteDotFisFvF,
NULL, // &PrimFnDydQuoteDotBisRvR, -- Can't happen w/QuoteDot
&PrimFnDydQuoteDotRisRvR,
NULL, // &PrimFnDydQuoteDotBisVvV, -- Can't happen w/QuoteDot
//// VisRvR // Handled via type promotion (to VisVvV)
&PrimFnDydQuoteDotVisVvV,
NULL, // &PrimFnMonQuoteDotB64isB64, -- Can't happen w/QuoteDot
NULL, // &PrimFnMonQuoteDotB32isB32, -- Can't happen w/QuoteDot
NULL, // &PrimFnMonQuoteDotB16isB16, -- Can't happen w/QuoteDot
NULL, // &PrimFnMonQuoteDotB08isB08, -- Can't happen w/QuoteDot
&PrimFnDydLeftCaretUnderbarB64isB64vB64,
&PrimFnDydLeftCaretUnderbarB32isB32vB32,
&PrimFnDydLeftCaretUnderbarB16isB16vB16,
&PrimFnDydLeftCaretUnderbarB08isB08vB08,
};
static LPPRIMSPEC lpPrimSpec = {&PrimSpecQuoteDot};
#endif
//***************************************************************************
// $PrimFnQuoteDot_EM_YY
//
// Primitive function for monadic and dyadic QuoteDot ("factorial" and "binomial")
//***************************************************************************
#ifdef DEBUG
#define APPEND_NAME L" -- PrimFnQuoteDot_EM_YY"
#else
#define APPEND_NAME
#endif
LPPL_YYSTYPE PrimFnQuoteDot_EM_YY
(LPTOKEN lptkLftArg, // Ptr to left arg token (may be NULL if monadic)
LPTOKEN lptkFunc, // Ptr to function token
LPTOKEN lptkRhtArg, // Ptr to right arg token
LPTOKEN lptkAxis) // Ptr to axis token (may be NULL)
{
// Ensure not an overflow function
Assert (lptkFunc->tkData.tkChar EQ UTF16_QUOTEDOT);
// Split cases based upon monadic or dyadic
if (lptkLftArg EQ NULL)
return (*lpPrimSpec->PrimFnMon_EM_YY) ( lptkFunc, lptkRhtArg, lptkAxis, lpPrimSpec);
else
return (*lpPrimSpec->PrimFnDyd_EM_YY) (lptkLftArg, lptkFunc, lptkRhtArg, lptkAxis, lpPrimSpec);
} // End PrimFnQuoteDot_EM_YY
#undef APPEND_NAME
//***************************************************************************
// $PrimSpecQuoteDotStorageTypeMon
//
// Primitive monadic scalar function special handling: Storage type
//***************************************************************************
APLSTYPE PrimSpecQuoteDotStorageTypeMon
(APLNELM aplNELMRht,
LPAPLSTYPE lpaplTypeRht,
LPTOKEN lptkFunc)
{
APLSTYPE aplTypeRes;
// In case the right arg is an empty char,
// change its type to BOOL
if (IsCharEmpty (*lpaplTypeRht, aplNELMRht))
*lpaplTypeRht = ARRAY_BOOL;
// The storage type of the result is
// the same as that of the right arg
aplTypeRes = *lpaplTypeRht;
// Split cases based upon the storage type
switch (aplTypeRes)
{
case ARRAY_BOOL:
case ARRAY_INT:
case ARRAY_FLOAT:
case ARRAY_RAT:
case ARRAY_VFP:
case ARRAY_NESTED:
break;
// Except that APAs are converted to INTs
case ARRAY_APA:
aplTypeRes = ARRAY_INT;
break;
case ARRAY_CHAR:
case ARRAY_HETERO:
aplTypeRes = ARRAY_ERROR;
break;
defstop
break;
} // End SWITCH
return aplTypeRes;
} // End PrimSpecQuoteDotStorageTypeMon
//***************************************************************************
// $PrimFnMonQuoteDotBisB
//
// Primitive scalar function monadic QuoteDot: B {is} fn B
//***************************************************************************
APLBOOL PrimFnMonQuoteDotBisB
(APLBOOL aplBooleanRht,
LPPRIMSPEC lpPrimSpec)
{
return (APLBOOL) 1;
} // End PrimFnMonQuoteDotBisB
//***************************************************************************
// $PrimFnMonQuoteDotIisI
//
// Primitive scalar function monadic QuoteDot: I {is} fn I
//***************************************************************************
APLINT PrimFnMonQuoteDotIisI
(APLINT aplIntegerRht,
LPPRIMSPEC lpPrimSpec)
{
APLFLOAT aplFloatRes;
static APLINT factorial[] =
{
1, // 0
1, // 1
2, // 2
6, // 3
24, // 4
120, // 5
720, // 6
5040, // 7
40320, // 8
362880, // 9
3628800, // 10
39916800, // 11
479001600, // 12
6227020800, // 13
87178291200, // 14
1307674368000, // 15
20922789888000, // 16
355687428096000, // 17
6402373705728000, // 18
121645100408832000, // 19
2432902008176640000, // 20
};
// Check for indeterminates: !N for integer N < 0
if (aplIntegerRht < 0)
{
// Get the result as float
aplFloatRes =
TranslateQuadICIndex (0,
ICNDX_QDOTn,
(APLFLOAT) aplIntegerRht,
FALSE);
// If it's infinite, ...
if (IsFltInfinity (aplFloatRes))
RaiseException (EXCEPTION_RESULT_FLOAT, 0, 0, NULL);
else
return (APLINT) aplFloatRes;
} // End IF
// Check for results too large to express as integers
if (aplIntegerRht >= countof (factorial))
RaiseException (EXCEPTION_RESULT_FLOAT, 0, 0, NULL);
// Lookup the result
return factorial[aplIntegerRht];
} // End PrimFnMonQuoteDotIisI
//***************************************************************************
// $PrimFnMonQuoteDotFisI
//
// Primitive scalar function monadic QuoteDot: F {is} fn I
//***************************************************************************
APLFLOAT PrimFnMonQuoteDotFisI
(APLINT aplIntegerRht,
LPPRIMSPEC lpPrimSpec)
{
return PrimFnMonQuoteDotFisF ((APLFLOAT) aplIntegerRht, lpPrimSpec);
} // End PrimFnMonQuoteDotFisI
//***************************************************************************
// $PrimFnMonQuoteDotFisF
//
// Primitive scalar function monadic QuoteDot: F {is} fn F
//***************************************************************************
APLFLOAT PrimFnMonQuoteDotFisF
(APLFLOAT aplFloatRht,
LPPRIMSPEC lpPrimSpec)
{
int iRet;
gsl_sf_result gsr = {0};
// Check for indeterminates: !N for integer N < 0
if (aplFloatRht < 0)
{
if (aplFloatRht EQ floor (aplFloatRht)
|| aplFloatRht EQ ceil (aplFloatRht))
return TranslateQuadICIndex (0,
ICNDX_QDOTn,
aplFloatRht,
FALSE);
} // End IF
// Check for PosInfinity
if (IsFltPosInfinity (aplFloatRht))
return fltPosInfinity;
// Check for too large for GSL
if ((aplFloatRht + 1) > GSL_SF_GAMMA_XMAX)
RaiseException (EXCEPTION_DOMAIN_ERROR, 0, 0, NULL);
// Use the GNU Scientific Library Gamma function
iRet = gsl_sf_gamma_e (aplFloatRht + 1, &gsr);
// Check the return code
switch (iRet)
{
case GSL_SUCCESS:
break;
case GSL_FAILURE:
case GSL_EDOM:
case GSL_ERANGE:
RaiseException (EXCEPTION_DOMAIN_ERROR, 0, 0, NULL);
defstop
break;
} // End SWITCH
return gsr.val;
} // End PrimFnMonQuoteDotFisF
//***************************************************************************
// $PrimFnMonQuoteDotRisR
//
// Primitive scalar function monadic QuoteDot: R {is} fn R
//***************************************************************************
APLRAT PrimFnMonQuoteDotRisR
(APLRAT aplRatRht,
LPPRIMSPEC lpPrimSpec)
{
APLRAT mpqRes = {0};
UINT uRht;
// Check for indeterminates: !N for integer N < 0
if (mpq_integer_p (&aplRatRht)
&& mpq_cmp_ui (&aplRatRht, 0, 1) < 0)
return *mpq_QuadICValue (&aplRatRht, // No left arg
ICNDX_QDOTn,
&aplRatRht,
&mpqRes,
FALSE);
// Check for PosInfinity
if (IsMpqPosInfinity (&aplRatRht))
return mpqPosInfinity;
// If the denominator is 1,
// and the numerator fts in a UINT, ...
if (mpq_integer_p (&aplRatRht)
&& mpz_fits_slong_p (mpq_numref (&aplRatRht)) NE 0)
{
// Initialize the result to 0/1
mpq_init (&mpqRes);
// Extract the numerator
uRht = (UINT) mpz_get_si (mpq_numref (&aplRatRht));
// Compute the factorial
mpz_fac_ui (mpq_numref (&mpqRes), uRht);
} else
RaiseException (EXCEPTION_RESULT_VFP, 0, 0, NULL);
return mpqRes;
} // End PrimFnMonQuoteDotRisR
//***************************************************************************
// $PrimFnMonQuoteDotVisV
//
// Primitive scalar function monadic QuoteDot: V {is} fn V
//***************************************************************************
APLVFP PrimFnMonQuoteDotVisV
(APLVFP aplVfpRht,
LPPRIMSPEC lpPrimSpec)
{
APLMPI mpzRes = {0};
APLVFP mpfRes = {0};
// Check for indeterminates: !N for integer N < 0
if (mpfr_integer_p (&aplVfpRht)
&& mpfr_cmp_ui (&aplVfpRht, 0) < 0)
return *mpfr_QuadICValue (&aplVfpRht, // No left arg
ICNDX_QDOTn,
&aplVfpRht,
&mpfRes,
FALSE);
// Check for PosInfinity
if (IsMpfPosInfinity (&aplVfpRht))
return mpfPosInfinity;
// If the arg is an integer,
// and it fits in a ULONG, ...
if (mpfr_integer_p (&aplVfpRht)
&& mpfr_fits_uint_p (&aplVfpRht, MPFR_RNDN))
{
mpz_init (&mpzRes);
mpfr_init0 (&mpfRes);
mpz_fac_ui (&mpzRes, mpfr_get_ui (&aplVfpRht, MPFR_RNDN));
mpfr_set_z (&mpfRes, &mpzRes, MPFR_RNDN);
Myz_clear (&mpzRes);
} else
{
// Initialize the result
mpfr_init_set (&mpfRes, &aplVfpRht, MPFR_RNDN);
mpfr_add_ui (&mpfRes, &mpfRes, 1, MPFR_RNDN);
// Let MPFR handle it
mpfr_gamma (&mpfRes, &mpfRes, MPFR_RNDN);
#ifdef DEBUG
mpfr_free_cache ();
#endif
} // End IF/ELSE
return mpfRes;
} // End PrimFnMonQuoteDotVisV
//***************************************************************************
// $PrimSpecQuoteDotStorageTypeDyd
//
// Primitive dyadic scalar function special handling: Storage type
//***************************************************************************
APLSTYPE PrimSpecQuoteDotStorageTypeDyd
(APLNELM aplNELMLft,
LPAPLSTYPE lpaplTypeLft,
LPTOKEN lptkFunc,
APLNELM aplNELMRht,
LPAPLSTYPE lpaplTypeRht)
{
APLSTYPE aplTypeRes;
// In case the left arg is an empty char,
// change its type to BOOL
if (IsCharEmpty (*lpaplTypeLft, aplNELMLft))
*lpaplTypeLft = ARRAY_BOOL;
// In case the right arg is an empty char,
// change its type to BOOL
if (IsCharEmpty (*lpaplTypeRht, aplNELMRht))
*lpaplTypeRht = ARRAY_BOOL;
// Calculate the storage type of the result
aplTypeRes = StorageType (*lpaplTypeLft, lptkFunc, *lpaplTypeRht);
return aplTypeRes;
} // End PrimSpecQuoteDotStorageTypeDyd
//***************************************************************************
// $PrimFnDydQuoteDotBisBvB
//
// Primitive scalar function dyadic QuoteDot: B {is} B fn B
//***************************************************************************
APLBOOL PrimFnDydQuoteDotBisBvB
(APLBOOL aplBooleanLft,
APLBOOL aplBooleanRht,
LPPRIMSPEC lpPrimSpec)
{
return (aplBooleanLft <= aplBooleanRht);
} // End PrimFnDydQuoteDotBisBvB
//***************************************************************************
// $PrimFnDydQuoteDotIisIvI
//
// Primitive scalar function dyadic QuoteDot: I {is} I fn I
//***************************************************************************
APLINT PrimFnDydQuoteDotIisIvI
(APLINT aplIntegerLft,
APLINT aplIntegerRht,
LPPRIMSPEC lpPrimSpec)
{
APLINT aplIntegerTmp,
aplIntegerRes,
aplIntegerInd;
#define L aplIntegerLft
#define R aplIntegerRht
#define T aplIntegerTmp
#define Z aplIntegerRes
#define I aplIntegerInd
// Determine whether or not L, R, or L - R is a negative integer
// From ISO-IEC 13751 Extended APL Standard, p. 90
switch (4*(L < 0) + 2*(R < 0) + 1*(R < L))
{
// L R R-L
case 4*0 + 2*0 + 0: // (!R)/((!L)*!R-L)
// WLOG, change L so that L <= (R/2)
// using the reflection rule for
// binomial coefficients.
if (L > (R / 2))
L = R - L;
// Compute the result as:
// T = R-L;
// Z = Prod (I, 1, L, (I+T) / I)
for (T = R - L, Z = 1, I = 1; I <= L; I++)
Z = (imul64_RE (Z, I + T)) / I;
return Z;
case 4*0 + 2*0 + 1*1: // 0
return 0;
case 4*0 + 2*1 + 1*0: // DOMAIN ERROR
RaiseException (EXCEPTION_DOMAIN_ERROR, 0, 0, NULL);
case 4*0 + 2*1 + 1*1: // ((-1)*L)*L!(L-(R+1))
return ((L%2) ? -1: 1) * PrimFnDydQuoteDotIisIvI (L, L-(R+1), NULL);
case 4*1 + 2*0 + 1*0: // 0
return 0;
case 4*1 + 2*0 + 1*1: // (Can't happen)
return -1;
case 4*1 + 2*1 + 1*0: // ((-1)*R-L)*(-R+1)!|L+1
return (((R-L)%2) ? -1: 1) * PrimFnDydQuoteDotIisIvI (-(R+1), -(L+1), NULL);
case 4*1 + 2*1 + 1*1: // 0
return 0;
defstop
return -1;
} // End SWITCH
#undef I
#undef Z
#undef T
#undef R
#undef L
} // End PrimFnDydQuoteDotIisIvI
//***************************************************************************
// $PrimFnDydQuoteDotFisIvI
//
// Primitive scalar function dyadic QuoteDot: F {is} I fn I
//***************************************************************************
APLFLOAT PrimFnDydQuoteDotFisIvI
(APLINT aplIntegerLft,
APLINT aplIntegerRht,
LPPRIMSPEC lpPrimSpec)
{
APLFLOAT aplFloatTmp,
aplFloatRes,
aplFloatInd;
APLINT aplIntegerTmp,
aplIntegerRes,
aplIntegerInd,
aplIntegerTmp2;
UBOOL bRet = TRUE; // Assume the result is valid
// FYI: The only way this function can be called is
// from an exception raised from IisIvI above.
#define LI aplIntegerLft
#define RI aplIntegerRht
#define TI aplIntegerTmp
#define TI2 aplIntegerTmp2
#define ZI aplIntegerRes
#define II aplIntegerInd
#define LF aplFloatLft
#define RF aplFloatRht
#define TF aplFloatTmp
#define ZF aplFloatRes
#define IF aplFloatInd
// Determine whether or not LI, RI, or LI - RI is a negative integer
// From ISO-IEC 13751 Extended APL Standard, p. 90
switch (4*(LI < 0) + 2*(RI < 0) + 1*(RI < LI))
{
// LI RI RI-LI
case 4*0 + 2*0 + 0: // (!RI)/((!LI)*!RI-LI)
// WLOG, change LI so that LI <= (RI/2)
// using the reflection rule for
// binomial coefficients.
if (LI > (RI / 2))
LI = RI - LI;
// Compute the result as:
// TI = RI-LI;
// ZI = Prod (II, 1, LI, (II+TI) / II)
for (TI = RI - LI, ZI = 1, II = 1;
II <= LI;
II++)
{
TI2 = imul64 (ZI, II + TI, &bRet);
if (bRet)
ZI = TI2 / II;
else
{
for (ZF = (APLFLOAT) ZI, TF = (APLFLOAT) TI, IF = (APLFLOAT) II;
II <= LI;
II++, IF++)
{
ZF = (ZF * (IF + TF)) / IF;
if (IsFltInfinity (ZF))
RaiseException (EXCEPTION_DOMAIN_ERROR, 0, 0, NULL);
} // End FOR
return ZF;
} // End IF/ELSE
} // End FOR
return (APLFLOAT) ZI;
case 4*0 + 2*0 + 1*1: // 0
return 0;
case 4*0 + 2*1 + 1*0: // DOMAIN ERROR
RaiseException (EXCEPTION_DOMAIN_ERROR, 0, 0, NULL);
case 4*0 + 2*1 + 1*1: // ((-1)*LI)*LI!(LI-(RI+1))
return ((LI%2) ? -1: 1) * PrimFnDydQuoteDotFisIvI (LI, LI-(RI+1), NULL);
case 4*1 + 2*0 + 1*0: // 0
return 0;
case 4*1 + 2*0 + 1*1: // (Can't happen)
return -1;
case 4*1 + 2*1 + 1*0: // ((-1)*RI-LI)*(-RI+1)!|LI+1
return (((RI-LI)%2) ? -1: 1) * PrimFnDydQuoteDotFisIvI (-(RI+1), -(LI+1), NULL);
case 4*1 + 2*1 + 1*1: // 0
return 0;
defstop
return -1;
} // End SWITCH
#undef IF
#undef ZF
#undef TF
#undef RF
#undef LF
#undef II
#undef ZI
#undef TI2
#undef TI
#undef RI
#undef LI
} // End PrimFnDydQuoteDotFisIvI
//***************************************************************************
// $PrimFnDydQuoteDotFisFvF
//
// Primitive scalar function dyadic QuoteDot: F {is} F fn F
//***************************************************************************
APLFLOAT PrimFnDydQuoteDotFisFvF
(APLFLOAT aplFloatLft,
APLFLOAT aplFloatRht,
LPPRIMSPEC lpPrimSpec)
{
APLFLOAT aplFloatTmp,
aplFloatRes,
aplFloatInd;
#define LF aplFloatLft
#define RF aplFloatRht
#define TF aplFloatTmp
#define ZF aplFloatRes
#define IF aplFloatInd
// If LF and RF are exact integers
if ((LF EQ floor (LF)
|| LF EQ ceil (LF))
&& (RF EQ floor (RF)
|| RF EQ ceil (RF)))
{
// Determine whether or not LF, RF, or LF - RF is a negative integer
// From ISO-IEC 13751 Extended APL Standard, p. 90
switch (4*(LF < 0) + 2*(RF < 0) + 1*(RF < LF))
{
// LF RF RF-LF
case 4*0 + 2*0 + 0: // (!RF)/((!LF)*!RF-LF)
// WLOG, change LF so that LF <= (RF/2)
// using the reflection rule for
// binomial coefficients.
if (LF > (RF / 2))
LF = RF - LF;
// Compute the result as:
// TF = RF-LF;
// ZF = Prod (IF, 1, LF, (IF+TF) / IF)
for (TF = RF - LF, ZF = 1, IF = 1;
IF <= LF;
IF++)
ZF = (ZF * (IF + TF)) / IF;
return ZF;
case 4*0 + 2*0 + 1*1: // 0
return 0;
case 4*0 + 2*1 + 1*0: // DOMAIN ERROR
RaiseException (EXCEPTION_DOMAIN_ERROR, 0, 0, NULL);
case 4*0 + 2*1 + 1*1: // ((-1)*LF)*LF!(LF-(RF+1))
return (fmod (LF, 2) ? -1: 1) * PrimFnDydQuoteDotFisFvF (LF, LF-(RF+1), NULL);
case 4*1 + 2*0 + 1*0: // 0
return 0;
case 4*1 + 2*0 + 1*1: // (Can't happen)
return -1;
case 4*1 + 2*1 + 1*0: // ((-1)*RI-LI)*(-RI+1)!|LI+1
return (fmod (RF-LF, 2) ? -1: 1) * PrimFnDydQuoteDotFisFvF (-(RF+1), -(LF+1), NULL);
case 4*1 + 2*1 + 1*1: // 0
return 0;
defstop
return -1;
} // End SWITCH
} else
{
// Z = (!R) / (!L) * !R-L
return
PrimFnMonQuoteDotFisF (RF, NULL)
/ (PrimFnMonQuoteDotFisF (LF, NULL)
* PrimFnMonQuoteDotFisF (RF - LF, NULL));
} // End IF/ELSE
#undef IF
#undef ZF
#undef TF
#undef RF
#undef LF
} // End PrimFnDydQuoteDotFisFvF
//***************************************************************************
// $PrimFnDydQuoteDotRisRvR
//
// Primitive scalar function dyadic QuoteDot: R {is} R fn R
//***************************************************************************
APLRAT PrimFnDydQuoteDotRisRvR
(APLRAT aplRatLft,
APLRAT aplRatRht,
LPPRIMSPEC lpPrimSpec)
{
APLRAT mpqRes = {0};
UINT uLft;
// Initialize the result to 0/1
mpq_init (&mpqRes);
// Using the identity A!B <=> (B-A)!B and because the function
// mpz_bin_ui requires that the left argument must fit in a
// UINT, we calculate the alternate left argument in case it
// fits in a UINT.
mpq_sub (&mpqRes, &aplRatRht, &aplRatLft);
// Use the smaller of the two for the left arg
if (mpq_cmp (&mpqRes, &aplRatLft) > 0)
mpq_set (&mpqRes, &aplRatLft);
// If both denominators are 1,
// and the left numerator fits in a UINT, ...
if (mpq_integer_p (&aplRatLft)
&& mpq_integer_p (&aplRatRht)
&& mpz_fits_slong_p (mpq_numref (&mpqRes)) NE 0)
{
// Extract the numerator of the left argument
uLft = (UINT) mpz_get_si (mpq_numref (&mpqRes));
// Compute the binomial coefficient
mpz_bin_ui (mpq_numref (&mpqRes), mpq_numref (&aplRatRht), uLft);
} else
{
Myq_clear (&mpqRes);
RaiseException (EXCEPTION_RESULT_VFP, 0, 0, NULL);
} // End IF/ELSE
return mpqRes;
} // End PrimFnDydQuoteDotRisRvR
//***************************************************************************
// $PrimFnDydQuoteDotVisVvV
//
// Primitive scalar function dyadic QuoteDot: V {is} V fn V
//***************************************************************************
APLVFP PrimFnDydQuoteDotVisVvV
(APLVFP aplVfpLft,
APLVFP aplVfpRht,
LPPRIMSPEC lpPrimSpec)
{
APLVFP mpfRes,
aplLft,
aplRht,
aplTmp = {0};
// Z = (!R) / (!L) * !R-L
aplLft = PrimFnMonQuoteDotVisV (aplVfpLft, NULL); // !L
aplRht = PrimFnMonQuoteDotVisV (aplVfpRht, NULL); // !R
mpfr_init_copy (&aplTmp, &aplVfpRht); // R
mpfr_sub (&aplTmp, &aplTmp, &aplVfpLft, MPFR_RNDN); // R-L
mpfRes = PrimFnMonQuoteDotVisV (aplTmp, NULL); // !R-L
mpfr_mul (&mpfRes, &aplLft, &mpfRes , MPFR_RNDN); // (!L) * !R-L
mpfr_div (&mpfRes, &aplRht, &mpfRes , MPFR_RNDN); // (!R) / ((!L) * !R-L)
// We no longer need this storage
Myf_clear (&aplRht);
Myf_clear (&aplLft);
Myf_clear (&aplTmp);
return mpfRes;
} // End PrimFnDydQuoteDotVisVvV
//***************************************************************************
// End of File: pf_qdot.c
//***************************************************************************