Power (operator): Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "<source" to "<syntaxhighlight")
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
Line 2: Line 2:


== Description ==
== Description ==
A call to Power is of the form <syntaxhighlight 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  
* <syntaxhighlight lang=apl inline>X</source> is an optional argument.  
* <syntaxhighlight lang=apl inline>X</syntaxhighlight> 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>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>.
* <syntaxhighlight lang=apl inline>g</source> can be an [[array]] or a function.
* <syntaxhighlight lang=apl inline>g</syntaxhighlight> 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>:
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 [[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.
* '''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''': 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''': 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]], <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>.
* '''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 ==
Line 19: Line 19:
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.  
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
Line 25: Line 25:
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.
<syntaxhighlight 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 ===

Latest revision as of 22:30, 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 X(fg)Y, where

  • X is an optional argument.
  • f is a function. If X is given, then it is bound to f so X fg Y is equivalent to Xfg Y.
  • g can be an array or a function.

Power repeatedly applies f to Y based on the type of operand g:

  • Function: Must be dyadic and must return a boolean singleton. The previous iteration value is provided as the right argument to f, and the current iteration value is given as the left argument. f is repeatedly applied until this function returns 1.
  • Integer: Applies f g times to Y. If g is negative, then the inverse of f (if available) is applied.
  • Integer Array: In Extended Dyalog APL, g can be an integer array. Each integer i in g will be replaced by (fi)Y.

Examples

Some basic examples:

      1 (+3) 5 ⍝ Fixed number of iterations
8
      (2×3) 5 ⍝ No X given
40
      1 +÷= 1 ⍝ iterate till fixed point
1.618033989

A well-known use for Power is iterating until a fixed point is reached.

      (.∨⊢)3 30 0 1 1 0 1 1 0 1 ⍝ Transitive closure of an adjacency matrix
1 0 1
1 0 1
1 0 1

Power is also used to access function inverses.

      2(¯1)5
1 0 1

External Links

Lessons

Documentation

APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace