Power (operator): Difference between revisions
(fixed arguments of function Y argument) |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
Line 2: | Line 2: | ||
== Description == | == Description == | ||
A call to Power is of the form < | A call to Power is of the form <syntaxhighlight lang=apl inline>X(f⍣g)Y</source>, where | ||
* < | * <syntaxhighlight lang=apl inline>X</source> is an optional argument. | ||
* < | * <syntaxhighlight lang=apl inline>f</source> is a function. If <syntaxhighlight lang=apl inline>X</source> is given, then it is bound to <syntaxhighlight lang=apl inline>f</source> so <syntaxhighlight lang=apl inline>X f⍣g Y</source> is equivalent to <syntaxhighlight lang=apl inline>X∘f⍣g Y</source>. | ||
* < | * <syntaxhighlight lang=apl inline>g</source> can be an [[array]] or a function. | ||
Power repeatedly applies < | Power repeatedly applies <syntaxhighlight lang=apl inline>f</source> to <syntaxhighlight lang=apl inline>Y</source> based on the type of operand <syntaxhighlight lang=apl inline>g</source>: | ||
* '''Function''': Must be dyadic and must return a boolean [[singleton]]. The previous iteration value is provided as the right argument to < | * '''Function''': Must be dyadic and must return a boolean [[singleton]]. The previous iteration value is provided as the right argument to <syntaxhighlight lang=apl inline>f</source>, and the current iteration value is given as the left argument. <syntaxhighlight lang=apl inline>f</source> is repeatedly applied until this function returns 1. | ||
* '''Integer''': Applies < | * '''Integer''': Applies <syntaxhighlight lang=apl inline>f</source> <syntaxhighlight lang=apl inline>g</source> times to <syntaxhighlight lang=apl inline>Y</source>. If <syntaxhighlight lang=apl inline>g</source> is negative, then the inverse of <syntaxhighlight lang=apl inline>f</source> (if available) is applied. | ||
* '''Integer Array''': In [[Extended Dyalog APL]], < | * '''Integer Array''': In [[Extended Dyalog APL]], <syntaxhighlight lang=apl inline>g</source> can be an integer array. Each integer <syntaxhighlight lang=apl inline>i</source> in <syntaxhighlight lang=apl inline>g</source> will be replaced by <syntaxhighlight lang=apl inline>⊂(f⍣i)Y</source>. | ||
== Examples == | == Examples == | ||
Some basic examples: | Some basic examples: | ||
< | <syntaxhighlight lang=apl> 1 (+⍣3) 5 ⍝ Fixed number of iterations | ||
8 | 8 | ||
(2∘×⍣3) 5 ⍝ No X given | (2∘×⍣3) 5 ⍝ No X given | ||
Line 21: | Line 21: | ||
1.618033989</source> | 1.618033989</source> | ||
A well-known use for Power is iterating until a fixed point is reached. | A well-known use for Power is iterating until a fixed point is reached. | ||
< | <syntaxhighlight lang=apl> | ||
(∨.∧⍨∨⊢)⍣≡3 3⍴0 0 1 1 0 1 1 0 1 ⍝ Transitive closure of an adjacency matrix | (∨.∧⍨∨⊢)⍣≡3 3⍴0 0 1 1 0 1 1 0 1 ⍝ Transitive closure of an adjacency matrix | ||
1 0 1 | 1 0 1 | ||
Line 27: | Line 27: | ||
1 0 1</source> | 1 0 1</source> | ||
Power is also used to access function inverses. | Power is also used to access function inverses. | ||
< | <syntaxhighlight lang=apl> 2(⊥⍣¯1)5 | ||
1 0 1</source> | 1 0 1</source> | ||
== External Links == | == External Links == |
Revision as of 21:14, 10 September 2022
⍣
|
Power (⍣
) is a primitive dyadic operator that performs bounded looping, unbounded looping, and function inverses in NARS2000, Dyalog APL, and related implementations like ngn/apl, dzaima/APL, and Extended Dyalog APL.
Description
A call to Power is of the form <syntaxhighlight lang=apl inline>X(f⍣g)Y</source>, where
- <syntaxhighlight lang=apl inline>X</source> is an optional argument.
- <syntaxhighlight lang=apl inline>f</source> is a function. If <syntaxhighlight lang=apl inline>X</source> is given, then it is bound to <syntaxhighlight lang=apl inline>f</source> so <syntaxhighlight lang=apl inline>X f⍣g Y</source> is equivalent to <syntaxhighlight lang=apl inline>X∘f⍣g Y</source>.
- <syntaxhighlight lang=apl inline>g</source> can be an array or a function.
Power repeatedly applies <syntaxhighlight lang=apl inline>f</source> to <syntaxhighlight lang=apl inline>Y</source> based on the type of operand <syntaxhighlight lang=apl inline>g</source>:
- Function: Must be dyadic and must return a boolean singleton. The previous iteration value is provided as the right argument to <syntaxhighlight lang=apl inline>f</source>, and the current iteration value is given as the left argument. <syntaxhighlight lang=apl inline>f</source> is repeatedly applied until this function returns 1.
- Integer: Applies <syntaxhighlight lang=apl inline>f</source> <syntaxhighlight lang=apl inline>g</source> times to <syntaxhighlight lang=apl inline>Y</source>. If <syntaxhighlight lang=apl inline>g</source> is negative, then the inverse of <syntaxhighlight lang=apl inline>f</source> (if available) is applied.
- Integer Array: In Extended Dyalog APL, <syntaxhighlight lang=apl inline>g</source> can be an integer array. Each integer <syntaxhighlight lang=apl inline>i</source> in <syntaxhighlight lang=apl inline>g</source> will be replaced by <syntaxhighlight lang=apl inline>⊂(f⍣i)Y</source>.
Examples
Some basic examples: <syntaxhighlight lang=apl> 1 (+⍣3) 5 ⍝ Fixed number of iterations 8
(2∘×⍣3) 5 ⍝ No X given
40
1 +∘÷⍣= 1 ⍝ iterate till fixed point
1.618033989</source> A well-known use for Power is iterating until a fixed point is reached. <syntaxhighlight lang=apl>
(∨.∧⍨∨⊢)⍣≡3 3⍴0 0 1 1 0 1 1 0 1 ⍝ Transitive closure of an adjacency matrix
1 0 1 1 0 1 1 0 1</source> Power is also used to access function inverses. <syntaxhighlight lang=apl> 2(⊥⍣¯1)5 1 0 1</source>
External Links
Lessons
Documentation
- Dyalog
- J Dictionary, NuVoc
- BQN