From d988a620fd67581242f364b3d38165ed505244af Mon Sep 17 00:00:00 2001 From: dzaima Date: Tue, 26 Nov 2024 18:08:15 +0200 Subject: [PATCH] =?UTF-8?q?native=20F=E2=8C=BE(a=E2=8A=B8=E2=A5=8A)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sfns.c | 31 +++++++++++++++++++++++++++++++ test/cases/under.bqn | 16 ++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/builtins/sfns.c b/src/builtins/sfns.c index 1c8609e8..3c3ca3b2 100644 --- a/src/builtins/sfns.c +++ b/src/builtins/sfns.c @@ -3,6 +3,7 @@ #include "../utils/talloc.h" #include "../builtins.h" +// TODO clear sortedness flags on customizeShape & cpyWithShape Arr* customizeShape(B x) { if (reusable(x) && RNK(x)<=1) return a(x); return TI(x,slice)(x,0,IA(x)); @@ -1473,6 +1474,35 @@ B shape_uc1(B t, B o, B x) { return truncReshape(shape_uc1_t(c1(o, shape_c1(t, x)), xia), xia, xia, xr, sh); } +B shape_ucw(B t, B o, B w, B x) { + if (!isArr(x)) return def_fn_ucw(t, o, w, x); + B arg = shape_c2(t, inc(w), incG(x)); + usz xia = IA(x); + usz aia = IA(arg); + if (aia > xia) { + decG(arg); + return def_fn_ucw(t, o, w, x); + } + dec(w); + B rep = c1(o, incG(arg)); + if (!isArr(rep) || !eqShape(arg, rep)) thrF("𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (%H ≡ ≢a⥊𝕩, %H ≡ shape of result of 𝔽)", arg, rep); + + B r; + if (xia == aia) { + r = taga(arr_shCopy(customizeShape(rep), x)); + decG(x); + } else { + MAKE_MUT_INIT(rm, xia, el_or(TI(x,elType), TI(rep,elType))); MUTG_INIT(rm); + mut_copyG(rm, 0, rep, 0, aia); + mut_copyG(rm, aia, x, aia, xia-aia); + decG(rep); + r = mut_fcd(rm, x); + } + + decG(arg); + return r; +} + B reverse_ix(B t, B w, B x) { if (isAtm(x) || RNK(x)==0) thrM("⌽⁼: 𝕩 must have rank at least 1"); @@ -1511,6 +1541,7 @@ void sfns_init(void) { c(BFn,bi_reverse)->ucw = reverse_ucw; c(BFn,bi_pick)->ucw = pick_ucw; c(BFn,bi_select)->ucw = select_ucw; // TODO move to new init fn + c(BFn,bi_shape)->ucw = shape_ucw; c(BFn,bi_shape)->uc1 = shape_uc1; c(BFn,bi_take)->ucw = take_ucw; c(BFn,bi_drop)->ucw = drop_ucw; diff --git a/test/cases/under.bqn b/test/cases/under.bqn index 56154651..b926a858 100644 --- a/test/cases/under.bqn +++ b/test/cases/under.bqn @@ -87,6 +87,22 @@ n←500 ⋄ a←↕n ⋄ i←(-n)+↕2×n ⋄ r←⌽(2×n)⥊a ⋄ ! (⌽a) ≡ # !% ⊢⌾⥊ 4 # TODO enable # !% ⊢⌾(3⥊⊢) 4 # TODO enable +⍉⌾(2‿2⊸⥊) ↕10 %% 0‿2‿1‿3‿4‿5‿6‿7‿8‿9 +1⊸+⌾(10‿2⊸⥊) ↕7 %% 1‿2‿3‿4‿5‿6‿7 +{!𝕩≡<0 ⋄ <9}⌾(⟨⟩⊸⥊) ↕7 %% 9‿1‿2‿3‿4‿5‿6 +⌽⌾(∘‿2⊸⥊) ↕10 %% 8‿9‿6‿7‿4‿5‿2‿3‿0‿1 +!"𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (⟨⟩ ≡ ≢a⥊𝕩, ⟨⟩ ≡ shape of result of 𝔽)" % ⊑⌾(⟨⟩⊸⥊) ↕7 +!"𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (⟨2⟩ ≡ ≢a⥊𝕩, ⟨⟩ ≡ shape of result of 𝔽)" % 0⌾(2⊸⥊) ↕10 +!"𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (⟨2⟩ ≡ ≢a⥊𝕩, ⟨1⟩ ≡ shape of result of 𝔽)" % 1⊸↓⌾(2⊸⥊) ↕10 +!"𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (2‿2 ≡ ≢a⥊𝕩, 1‿2 ≡ shape of result of 𝔽)" % 1⊸↓⌾(2‿2⊸⥊) ↕10 +!"𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (2‿2 ≡ ≢a⥊𝕩, ⟨⟩ ≡ shape of result of 𝔽)" % ⊑⌾(2‿2⊸⥊) ↕10 +!"𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (3‿2 ≡ ≢a⥊𝕩, 2‿3 ≡ shape of result of 𝔽)" % ⍉⌾(3‿2⊸⥊) ↕10 +!"⁼: Inverse not found" % ⊑⌾(2‿20⊸⥊) ↕10 +!"⁼: Inverse not found" % ⍉⌾(3‿20⊸⥊) ↕10 +1⊸+⌾(2‿2⊸⥊) 5 %% <6 +!"⌾: Incompatible result elements in structural Under" % 1‿2⊸+⌾(2‿2⊸⥊) 5 +10‿20‿30⊸+˘⌾(4‿3⊸⥊) 1‿2‿3 %% 11‿22‿33 + # / !"/: Lengths of components of 𝕨 must match 𝕩 (3 ≠ 10)" % 1⊸∾⌾(1‿0‿1⊸/) ↕10