From 684442d308ec7789fe38697e810b4c4fe9a6c0e0 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Tue, 1 Feb 2022 23:13:40 +0000 Subject: =?UTF-8?q?Add=20encode=20and=20decode=20=E2=8A=A5=20=E2=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apl9.h | 2 ++ functions.c | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/apl9.h b/apl9.h index e33df18..947d58c 100644 --- a/apl9.h +++ b/apl9.h @@ -333,6 +333,8 @@ Array *fnDeal(Array *, Array *); Array *fnResidue(Array *, Array *); Array *fnMaximum(Array *, Array *); Array *fnMinimum(Array *, Array *); +Array *fnDecode(Array *, Array *); +Array *fnEncode(Array *, Array *); Array *fnLeft(Array *, Array *); Array *fnRight(Array *, Array *); Array *fnEqual(Array *, Array *); diff --git a/functions.c b/functions.c index 24fefa9..68dcbe7 100644 --- a/functions.c +++ b/functions.c @@ -76,8 +76,8 @@ fndyad dyadfunctiondefs[] = { fnResidue, /* | */ fnMaximum, /* ⌈ */ fnMinimum, /* ⌊ */ - 0, /* ⊥ */ - 0, /* ⊤ */ + fnDecode, /* ⊥ */ + fnEncode, /* ⊤ */ fnLeft, /* ⊣ */ fnRight, /* ⊢ */ fnEqual, /* = */ @@ -1051,6 +1051,30 @@ SCALAR_FUNCTION_2(fnMinimum, 0, left->type, res->intdata[i] = right->intdata[i]; ) +Array * +fnDecode(Array *left, Array *right) +{ + if(left->type != AtypeInt && left->type != AtypeFloat) + throwerror(nil, EType); + if(right->type != AtypeInt && right->type != AtypeFloat) + throwerror(nil, EType); + return rundfn(L"(⌽×\\1,⌽⍵∘{1↓(≢⍺)⍴⍵}⍤1⊢⍺)+.×⍵", nil, nil, left, right); +} + +Array * +fnEncode(Array *left, Array *right) +{ + if(left->type != AtypeInt && left->type != AtypeFloat) + throwerror(nil, EType); + if(right->type != AtypeInt && right->type != AtypeFloat) + throwerror(nil, EType); + + if(left->rank > 1) + return rundfn(L"⍉(⍉⍺) (⊤⍤1) ⍵", nil, nil, left, right); + else + return rundfn(L"⎕div←1 ⋄ ((⍴⍺),⍴⍵)⍴(⌽⍺){0=≢⍺:⍬ ⋄ n←⊃⍺ ⋄ x←n|⍵ ⋄ ((1↓⍺)∇n÷⍨⍵-x)⍪x}⍵", nil, nil, left, right); +} + Array * fnLeft(Array *left, Array *right) { -- cgit v1.2.3