apl: power f⍣n + fixed-point f⍣≡ (+9 tests, 191/191)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 56s

This commit is contained in:
2026-05-06 23:32:26 +00:00
parent 3d2bdc52b5
commit 4dbd3a0b34
3 changed files with 81 additions and 2 deletions

View File

@@ -1066,3 +1066,20 @@
(define apl-compose (fn (f g x) (f (g x))))
(define apl-compose-dyadic (fn (f g x y) (f x (g y))))
(define
apl-power
(fn (f n x) (reduce (fn (acc i) (f acc)) x (range 0 n))))
(define
apl-power-fixed
(fn
(f x)
(let
((next (f x)))
(if
(and
(equal? (get next :shape) (get x :shape))
(equal? (get next :ravel) (get x :ravel)))
x
(apl-power-fixed f next)))))

View File

@@ -579,4 +579,65 @@
apl-neg-m
(make-array (list 2 3) (list 1 2 3 4 5 6))
(make-array (list 2 3) (list 1 1 1 1 1 1))))
(list 2 3))
(list 2 3))
(apl-test
"power n=0 identity"
(rv (apl-power (fn (a) (apl-add a (apl-scalar 1))) 0 (apl-scalar 5)))
(list 5))
(apl-test
"power increment by 3"
(rv (apl-power (fn (a) (apl-add a (apl-scalar 1))) 3 (apl-scalar 0)))
(list 3))
(apl-test
"power double 4 times = 16"
(rv (apl-power (fn (a) (apl-mul a (apl-scalar 2))) 4 (apl-scalar 1)))
(list 16))
(apl-test
"power on vector +5"
(rv
(apl-power
(fn (a) (apl-add a (apl-scalar 1)))
5
(make-array (list 3) (list 1 2 3))))
(list 6 7 8))
(apl-test
"power on vector preserves shape"
(sh
(apl-power
(fn (a) (apl-add a (apl-scalar 1)))
5
(make-array (list 3) (list 1 2 3))))
(list 3))
(apl-test
"power on matrix"
(rv
(apl-power
(fn (a) (apl-mul a (apl-scalar 3)))
2
(make-array (list 2 2) (list 1 2 3 4))))
(list 9 18 27 36))
(apl-test
"power-fixed identity stops immediately"
(rv (apl-power-fixed (fn (a) a) (make-array (list 3) (list 1 2 3))))
(list 1 2 3))
(apl-test
"power-fixed floor half scalar to 0"
(rv
(apl-power-fixed
(fn (a) (apl-floor (apl-div a (apl-scalar 2))))
(apl-scalar 100)))
(list 0))
(apl-test
"power-fixed shape preserved"
(sh
(apl-power-fixed (fn (a) a) (make-array (list 2 2) (list 1 2 3 4))))
(list 2 2))