native F⌾(a⊸⥊)

This commit is contained in:
dzaima 2024-11-26 18:08:15 +02:00
parent 44e8c1e7fa
commit d988a620fd
2 changed files with 47 additions and 0 deletions

View File

@ -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;

View File

@ -87,6 +87,22 @@ n←500 ⋄ a←↕n ⋄ i←(-n)+↕2×n ⋄ r←⌽(2×n)⥊a ⋄ ! (⌽a) ≡
# !% ⊢⌾⥊ 4 # TODO enable
# !% ⊢⌾(3⥊⊢) 4 # TODO enable
(22) 10 %% 0213456789
1+(102) 7 %% 1234567
{!𝕩<0 <9}() 7 %% 9123456
(2) 10 %% 8967452301
!"𝔽⌾(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(22) 10
!"𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (2‿2 ≡ ≢a⥊𝕩, ⟨⟩ ≡ shape of result of 𝔽)" % (22) 10
!"𝔽⌾(a⊸⥊): 𝔽 must return an array with the same shape as its input (3‿2 ≡ ≢a⥊𝕩, 2‿3 ≡ shape of result of 𝔽)" % (32) 10
!"⁼: Inverse not found" % (220) 10
!"⁼: Inverse not found" % (320) 10
1+(22) 5 %% <6
!"⌾: Incompatible result elements in structural Under" % 12+(22) 5
102030+˘(43) 123 %% 112233
# /
!"/: Lengths of components of 𝕨 must match 𝕩 (3 ≠ 10)" % 1(101/) 10