Expand: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "<source" to "<syntaxhighlight")
Line 1: Line 1:
{{Built-ins|Expand|<nowiki>\</nowiki>|⍀}} is a [[dyadic function]] or [[monadic operator]] that copies each [[element]] of the right [[argument]] a given number of times and inserts [[prototype]] elements, ordering the copies along a specified [[axis]]. Typically <source lang=apl inline>\</source> is called Expand while <source lang=apl inline>⍀</source> is called "Expand First" or an equivalent.
{{Built-ins|Expand|<nowiki>\</nowiki>|⍀}} is a [[dyadic function]] or [[monadic operator]] that copies each [[element]] of the right [[argument]] a given number of times and inserts [[prototype]] elements, ordering the copies along a specified [[axis]]. Typically <syntaxhighlight lang=apl inline>\</source> is called Expand while <syntaxhighlight lang=apl inline>⍀</source> is called "Expand First" or an equivalent.


Expand is usually associated with [[Replicate]] (<source lang=apl inline>/</source>), and the two functions are related to [[Mesh]] and [[Mask]]. It shares a [[glyph]] with [[Scan]] even though Expand is naturally a [[function]] and Scan must be an [[operator]]. This incongruity is sometimes resolved by making Expand an operator itself, and sometimes by [[function-operator overloading]] allowing both syntactic elements to coexist.
Expand is usually associated with [[Replicate]] (<syntaxhighlight lang=apl inline>/</source>), and the two functions are related to [[Mesh]] and [[Mask]]. It shares a [[glyph]] with [[Scan]] even though Expand is naturally a [[function]] and Scan must be an [[operator]]. This incongruity is sometimes resolved by making Expand an operator itself, and sometimes by [[function-operator overloading]] allowing both syntactic elements to coexist.


== Examples ==
== Examples ==
Line 7: Line 7:
If the right argument is a [[vector]], the number of positive values in the left argument must match the length of the right argument. Each positive value produces copies of the corresponding item on the right argument; a zero or negative value produces one or more prototype elements.
If the right argument is a [[vector]], the number of positive values in the left argument must match the length of the right argument. Each positive value produces copies of the corresponding item on the right argument; a zero or negative value produces one or more prototype elements.


<source lang=apl>
<syntaxhighlight lang=apl>
       1 0 3 ¯2 2\'abc'
       1 0 3 ¯2 2\'abc'
a bbb  cc
a bbb  cc
Line 14: Line 14:
For [[Boolean]] left argument, Expand is the right inverse of [[Replicate]], as Expand inserts prototype elements at the exact places which Replicate will remove:
For [[Boolean]] left argument, Expand is the right inverse of [[Replicate]], as Expand inserts prototype elements at the exact places which Replicate will remove:


<source lang=apl>
<syntaxhighlight lang=apl>
       1 0 0 1 0 1\'abc'
       1 0 0 1 0 1\'abc'
a  b c
a  b c
Line 23: Line 23:
=== High-rank arrays ===
=== High-rank arrays ===


Expand works along a particular [[axis]], which can be specified in languages with [[function axis]] and otherwise is the first axis for <source lang=apl inline>⍀</source>, and the last axis for <source lang=apl inline>\</source>.
Expand works along a particular [[axis]], which can be specified in languages with [[function axis]] and otherwise is the first axis for <syntaxhighlight lang=apl inline>⍀</source>, and the last axis for <syntaxhighlight lang=apl inline>\</source>.


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←A←3 4⍴⎕A
       ⎕←A←3 4⍴⎕A
ABCD
ABCD
Line 45: Line 45:


Some implementations allow the right argument to have length 1 along the expansion axis even if other axes have lengths not equal to 1.
Some implementations allow the right argument to have length 1 along the expansion axis even if other axes have lengths not equal to 1.
<source lang=apl>
<syntaxhighlight lang=apl>
       1 ¯2 3 \ ⍪'abc'
       1 ¯2 3 \ ⍪'abc'
a  aaa
a  aaa

Revision as of 22:04, 10 September 2022

\

Expand (\, ) is a dyadic function or monadic operator that copies each element of the right argument a given number of times and inserts prototype elements, ordering the copies along a specified axis. Typically <syntaxhighlight lang=apl inline>\</source> is called Expand while <syntaxhighlight lang=apl inline>⍀</source> is called "Expand First" or an equivalent.

Expand is usually associated with Replicate (<syntaxhighlight lang=apl inline>/</source>), and the two functions are related to Mesh and Mask. It shares a glyph with Scan even though Expand is naturally a function and Scan must be an operator. This incongruity is sometimes resolved by making Expand an operator itself, and sometimes by function-operator overloading allowing both syntactic elements to coexist.

Examples

If the right argument is a vector, the number of positive values in the left argument must match the length of the right argument. Each positive value produces copies of the corresponding item on the right argument; a zero or negative value produces one or more prototype elements.

<syntaxhighlight lang=apl>

     1 0 3 ¯2 2\'abc'

a bbb cc </source>

For Boolean left argument, Expand is the right inverse of Replicate, as Expand inserts prototype elements at the exact places which Replicate will remove:

<syntaxhighlight lang=apl>

     1 0 0 1 0 1\'abc'

a b c

     1 0 0 1 0 1/'a  b c'

abc </source>

High-rank arrays

Expand works along a particular axis, which can be specified in languages with function axis and otherwise is the first axis for <syntaxhighlight lang=apl inline>⍀</source>, and the last axis for <syntaxhighlight lang=apl inline>\</source>.

<syntaxhighlight lang=apl>

     ⎕←A←3 4⍴⎕A

ABCD EFGH IJKL

     1 0 3 2 ¯2 1\A

A BBBCC D E FFFGG H I JJJKK L

     3 1 ¯1 2⍀A

ABCD ABCD ABCD EFGH

IJKL IJKL </source>

Some implementations allow the right argument to have length 1 along the expansion axis even if other axes have lengths not equal to 1. <syntaxhighlight lang=apl>

     1 ¯2 3 \ ⍪'abc'

a aaa b bbb c ccc </source>

External Links

Lessons

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector