From 1bf60c0cfc8e26d4f9e92e044d37169f55ac170b Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Tue, 25 Jan 2022 17:05:41 +0000 Subject: Get ready for inverse of functions --- apl9.h | 3 +++ inverse.c | 13 +++++++++++++ mkfile | 1 + operators.c | 8 +++++++- 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 inverse.c diff --git a/apl9.h b/apl9.h index c275d73..7c03918 100644 --- a/apl9.h +++ b/apl9.h @@ -264,6 +264,9 @@ void throwerror(Rune *, int); memcpy(globalerror.jmp, old, sizeof(jmp_buf));\ } +/* inverse.c */ +Function inverse(Function); + /* Monadic functions from function.c */ Array *fnNegate(Array *); Array *fnSign(Array *); diff --git a/inverse.c b/inverse.c new file mode 100644 index 0000000..b37664b --- /dev/null +++ b/inverse.c @@ -0,0 +1,13 @@ +#include +#include +#include + +#include "apl9.h" + +Function +inverse(Function f) +{ + Rune *msg = runesmprint("No inverse defined for %S", ppfunction(f)); + throwerror(msg, EDomain); + return f; +} \ No newline at end of file diff --git a/mkfile b/mkfile index ab00568..017cc1a 100644 --- a/mkfile +++ b/mkfile @@ -14,6 +14,7 @@ OFILES=\ quadnames.$O\ error.$O\ hybrids.$O\ + inverse.$O\ HFILES=\ apl9.h\ diff --git a/operators.c b/operators.c index 17ba76e..1ecb8aa 100644 --- a/operators.c +++ b/operators.c @@ -101,8 +101,14 @@ opPower(Datum *lefto, Datum *righto, Array *left, Array *right) else code = L"next←⍶⍵ ⋄ next⍹⍵:⍵ ⋄ ∇next"; }else if(righto->tag == ArrayTag){ - if(righto->array->type != AtypeInt || righto->array->rank != 0 || righto->array->size != 1 || righto->array->intdata[0] < 0) + if(righto->array->type != AtypeInt || righto->array->rank != 0 || righto->array->size != 1) throwerror(L"right operand to ⍣", EDomain); + vlong times = righto->array->intdata[0]; + if(times < 0){ + lefto->func = inverse(lefto->func); + times = -times; + } + righto->array->intdata[0] = times; if(left) code = L"⍹=0:⍵ ⋄ ⍺ ⍶⍙(⍹-1)⊢⍺⍶⍵"; else -- cgit v1.2.3