Enclose

From APL Wiki
(Redirected from Box function)
Jump to navigation Jump to search
<

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 76 7 87 8 9
└─────┴─────┴─────┘
      1 2 3,¨4 5 6  ⍝ Computes (1,4 5 6)(2,4 5 6)(3,4 5 6)
┌───────┬───────┬───────┐
1 4 5 62 4 5 63 4 5 6
└───────┴───────┴───────┘
      (2 21 2 2 1)¨(1 2)(3 4)(5 6)  ⍝ "chipmunk" idiom
┌───┬───┐
1 23 4
├───┼───┤
3 41 2
└───┴───┘

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

      M2 3 4⎕A
ABCD
EFGH
IJKL
    
MNOP
QRST
UVWX
      [3]M  ⍝ Enclose last axis; same as ↓M
┌────┬────┬────┐
ABCDEFGHIJKL
├────┼────┼────┤
MNOPQRSTUVWX
└────┴────┴────┘
      [2 3]M  ⍝ Enclose two axes at once
┌────┬────┐
ABCDMNOP
EFGHQRST
IJKLUVWX
└────┴────┘

      N2 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

Some implementations allow a simple scalar to be boxed, while the others do not.

Enclose (without axis) is the inverse of Mix and First in the sense that the latter two undo the additional nesting introduced by Enclose.

External links

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 DivideFormatExecuteMaterialise
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-beamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencil
Quad names
Arrays Index originMigration level
Functions Case convert
Operators SearchReplace