Power (operator): Difference between revisions

Jump to navigation Jump to search
757 bytes added ,  22:30, 10 September 2022
m
Text replacement - "</source>" to "</syntaxhighlight>"
(Add Power page)
 
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Built-in|Power|⍣}} is a primitive [[dyadic operator]] that performs bounded looping, unbounded looping, and function inverses in [[Dyalog APL]].
{{Built-in|Power|⍣}} is a primitive [[dyadic operator]] that performs bounded looping, unbounded looping, and function [[inverse]]s in [[NARS2000]], [[Dyalog APL]], and related implementations like [[ngn/apl]], [[dzaima/APL]], and [[Extended Dyalog APL]].


== Description ==
== Description ==
A call to Power is of the form <source lang=apl inline>X(f⍣g)Y</source>, where  
A call to Power is of the form <syntaxhighlight lang=apl inline>X(f⍣g)Y</syntaxhighlight>, where  
* <source lang=apl inline>X</source> is an optional argument.  
* <syntaxhighlight lang=apl inline>X</syntaxhighlight> is an optional argument.  
* <source lang=apl inline>f</source> is a function. If <source lang=apl inline>X</source> is given, it is bound to <source lang=apl inline>f</source> (<source lang=apl inline>X∘f</source>).
* <syntaxhighlight lang=apl inline>f</syntaxhighlight> is a function. If <syntaxhighlight lang=apl inline>X</syntaxhighlight> is given, then it is bound to <syntaxhighlight lang=apl inline>f</syntaxhighlight> so <syntaxhighlight lang=apl inline>X f⍣g Y</syntaxhighlight> is equivalent to <syntaxhighlight lang=apl inline>X∘f⍣g Y</syntaxhighlight>.
* <source lang=apl inline>g</source> can be an integer or a function.
* <syntaxhighlight lang=apl inline>g</syntaxhighlight> can be an [[array]] or a function.


Power repeatedly applies <source lang=apl inline>f</source> to <source lang=apl inline>Y</source> based on the type of operand <source lang=apl inline>g</source>:
Power repeatedly applies <syntaxhighlight lang=apl inline>f</syntaxhighlight> to <syntaxhighlight lang=apl inline>Y</syntaxhighlight> based on the type of operand <syntaxhighlight lang=apl inline>g</syntaxhighlight>:
* '''Function''': Must be dyadic and must return a boolean value. The previous iteration value is provided in <source lang=apl inline></source>, and the current iteration value is given in <source lang=apl inline>⍵</source>. <source lang=apl inline>f</source> is repeatedly applied until this function returns 1.
* '''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</syntaxhighlight>, and the current iteration value is given as the left argument. <syntaxhighlight lang=apl inline>f</syntaxhighlight> is repeatedly applied until this function returns 1.
* '''Integer''': Apply <source lang=apl inline>f</source> <source lang=apl inline>g</source> times to <source lang=apl inline>Y</source>. If <source lang=apl inline>g</source> is negative, then <source lang=apl inline>f</source> is inverted before it is applied.
* '''Integer''': Applies <syntaxhighlight lang=apl inline>f</syntaxhighlight> <syntaxhighlight lang=apl inline>g</syntaxhighlight> times to <syntaxhighlight lang=apl inline>Y</syntaxhighlight>. If <syntaxhighlight lang=apl inline>g</syntaxhighlight> is negative, then the inverse of <syntaxhighlight lang=apl inline>f</syntaxhighlight> (if available) is applied.
* '''Integer Array''': In [[Extended Dyalog APL]], <source lang=apl inline>g</source> can be an integer array. Each integer <source lang=apl inline></source> in <source lang=apl inline>g</source> will be replaced by <source lang=apl inline>X(f⍣⍵)Y</source>.
* '''Integer Array''': In [[Extended Dyalog APL]], <syntaxhighlight lang=apl inline>g</syntaxhighlight> can be an integer array. Each integer <syntaxhighlight lang=apl inline>i</syntaxhighlight> in <syntaxhighlight lang=apl inline>g</syntaxhighlight> will be replaced by <syntaxhighlight lang=apl inline>(f⍣i)Y</syntaxhighlight>.


== Examples ==
== Examples ==
Some basic examples:
Some basic examples:
<source lang=apl>      1 (+⍣3) 5 ⍝ Fixed number of iterations
<syntaxhighlight lang=apl>      1 (+⍣3) 5 ⍝ Fixed number of iterations
8
8
       (2∘×⍣3) 5 ⍝ No X given
       (2∘×⍣3) 5 ⍝ No X given
40
40
       1 +∘÷⍣= 1 ⍝ iterate till fixed point
       1 +∘÷⍣= 1 ⍝ iterate till fixed point
1.618033989</source>
1.618033989</syntaxhighlight>
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.  
<source lang=apl>
<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
1 0 1
1 0 1
1 0 1</source>
1 0 1</syntaxhighlight>
Power is also used to access function inverses.
Power is also used to access function inverses.
<source lang=apl>      2(⊥⍣¯1)5
<syntaxhighlight lang=apl>      2(⊥⍣¯1)5
1 0 1</source>
1 0 1</syntaxhighlight>
== External Links ==
== External Links ==
=== Lessons ===
=== Lessons ===
Line 35: Line 35:
* [https://help.dyalog.com/latest/#Language/Primitive%20Operators/Power%20Operator.htm Dyalog]
* [https://help.dyalog.com/latest/#Language/Primitive%20Operators/Power%20Operator.htm Dyalog]
* J [https://www.jsoftware.com/help/dictionary/d202n.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/hatco NuVoc]
* J [https://www.jsoftware.com/help/dictionary/d202n.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/hatco NuVoc]
* [https://mlochbaum.github.io/BQN/doc/repeat.html BQN]
{{APL built-ins}}[[Category:Primitive operators]]

Navigation menu