Enclose: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
== Examples ==
== Examples ==


An enclosed array is a [[scalar]], which is subject to [[scalar extension]]. This can be used to simulate [[Outer Product|outer product]] by a [[scalar function]] or one-sided [[Each]] (pair the entire right argument with each element of the left argument, or vice versa). A notable application of this behavior is the "chipmunk idiom" <source lang=apl inline>X⊃¨⊂Y</source>, which simulates <source lang=apl inline>Y[X]</source> for (possibly nested) [[vector]] Y and [[simple]] X.
An enclosed array is a [[scalar]], which is subject to [[scalar extension]]. This can be used to simulate [[Outer Product|outer product]] by a [[scalar function]] or one-sided [[Each]] (pair the entire right argument with each element of the left argument, or vice versa). A notable application of this behavior is the "chipmunk idiom" <syntaxhighlight lang=apl inline>X⊃¨⊂Y</syntaxhighlight>, which simulates <syntaxhighlight lang=apl inline>Y[X]</syntaxhighlight> for (possibly nested) [[vector]] Y and [[simple]] X.


<source lang=apl>
<syntaxhighlight lang=apl>
       1 2 3+⊂4 5 6  ⍝ Computes (1+4 5 6)(2+4 5 6)(3+4 5 6)
       1 2 3+⊂4 5 6  ⍝ Computes (1+4 5 6)(2+4 5 6)(3+4 5 6)
┌─────┬─────┬─────┐
┌─────┬─────┬─────┐
Line 20: Line 20:
│3 4│1 2│
│3 4│1 2│
└───┴───┘
└───┴───┘
</source>
</syntaxhighlight>


Enclose with function axis can be used to move one or more axes to an extra level of nesting.
Enclose with function axis can be used to move one or more axes to an extra level of nesting.


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←M←2 3 4⍴⎕A
       ⎕←M←2 3 4⍴⎕A
ABCD
ABCD
Line 51: Line 51:
       ⍴⊃⊂[1 3]N  ⍝ Shape of each element is the enclosed axes
       ⍴⊃⊂[1 3]N  ⍝ Shape of each element is the enclosed axes
2 4
2 4
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}


== Description ==
== Description ==
Line 67: Line 67:
* [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Enclose.htm Dyalog], [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Enclose%20with%20Axes.htm with axes]
* [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Enclose.htm Dyalog], [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Enclose%20with%20Axes.htm with axes]
* [http://microapl.com/apl_help/ch_020_020_580.htm APLX]
* [http://microapl.com/apl_help/ch_020_020_580.htm APLX]
* J [https://www.jsoftware.com/help/dictionary/d010.htm Vocabulary], [https://code.jsoftware.com/wiki/Vocabulary/lt NuVoc] (as <source lang=j inline><</source>)
* J [https://www.jsoftware.com/help/dictionary/d010.htm Vocabulary], [https://code.jsoftware.com/wiki/Vocabulary/lt NuVoc] (as <syntaxhighlight lang=j inline><</syntaxhighlight>)
* [https://mlochbaum.github.io/BQN/doc/enclose.html BQN] (as <code><</code>)
* [https://mlochbaum.github.io/BQN/doc/enclose.html BQN] (as <code><</code>)


{{APL built-ins}}[[Category:Primitive functions]]
{{APL built-ins}}[[Category:Primitive functions]]

Latest revision as of 22:17, 10 September 2022

<

Enclose (, <), or Box, is a monadic primitive function which creates a nested scalar by wrapping its argument under one level of nesting. When used with function axis, only the selected axes of the given array are enclosed. This can be seen as a more general form of Split, which can only enclose (or split) one selected axis.

Examples

An enclosed array is a scalar, which is subject to scalar extension. This can be used to simulate outer product by a scalar function or one-sided Each (pair the entire right argument with each element of the left argument, or vice versa). A notable application of this behavior is the "chipmunk idiom" X⊃¨⊂Y, which simulates Y[X] for (possibly nested) vector Y and simple X.

      1 2 3+⊂4 5 6  ⍝ Computes (1+4 5 6)(2+4 5 6)(3+4 5 6)
┌─────┬─────┬─────┐
│5 6 7│6 7 8│7 8 9│
└─────┴─────┴─────┘
      1 2 3,¨⊂4 5 6  ⍝ Computes (1,4 5 6)(2,4 5 6)(3,4 5 6)
┌───────┬───────┬───────┐
│1 4 5 6│2 4 5 6│3 4 5 6│
└───────┴───────┴───────┘
      (2 2⍴1 2 2 1)⊃¨⊂(1 2)(3 4)(5 6)  ⍝ "chipmunk" idiom
┌───┬───┐
│1 2│3 4│
├───┼───┤
│3 4│1 2│
└───┴───┘

Enclose with function axis can be used to move one or more axes to an extra level of nesting.

      ⎕←M←2 3 4⍴⎕A
ABCD
EFGH
IJKL
    
MNOP
QRST
UVWX
      ⊂[3]M  ⍝ Enclose last axis; same as ↓M
┌────┬────┬────┐
│ABCD│EFGH│IJKL│
├────┼────┼────┤
│MNOP│QRST│UVWX│
└────┴────┴────┘
      ⊂[2 3]M  ⍝ Enclose two axes at once
┌────┬────┐
│ABCD│MNOP│
│EFGH│QRST│
│IJKL│UVWX│
└────┴────┘

      N←2 3 4 5⍴⎕A
      ⍴⊂[1 3]N  ⍝ Shape of the array is enclosed axes removed
3 5
      ⍴⊃⊂[1 3]N  ⍝ Shape of each element is the enclosed axes
2 4
Works in: Dyalog APL

Description

With no function axis, Enclose returns a scalar array whose only element is the argument. In the flat array model this means that the argument is placed in a box, and the result is this box as a scalar. In the nested array model it can simply be viewed as creating a new array. However, if the argument is a simple scalar, then the result will match the argument because nested array theory dicatates that simple scalars float.

Properties

Enclose (without axis) is the inverse of both Mix and First in the sense that either of these undoes the additional nesting introduced by Enclose.

External links

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