Depth: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
Miraheze>Adám Brudzewsky
m (Text replacement - "{{APL programming language}}" to "{{APL features}}")
 
m (Text replacement - "<source" to "<syntaxhighlight")
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
:''This page is about array depth, a number associated with every array. For the operator that applies a function at a given depth, see [[Depth (operator)]].''
{{Built-in|Depth|≡}} is a [[monadic]] [[primitive function]] that returns an array's depth. In the APL [[array model]], the '''depth''' of an array is the number of levels of [[Nested array model|nesting]] or [[box]]ing it exhibits. In some languages, Depth returns a negative result to indicate that not all paths through the array have the same depth.  
{{Built-in|Depth|≡}} is a [[monadic]] [[primitive function]] that returns an array's depth. In the APL [[array model]], the '''depth''' of an array is the number of levels of [[Nested array model|nesting]] or [[box]]ing it exhibits. In some languages, Depth returns a negative result to indicate that not all paths through the array have the same depth.  


== Nested array depth ==
== Nested array depth ==


Nested APLs vary in their definition of depth. They may take into account the array's [[prototype]], or not, and may use the positive depth, signed depth, or minimum depth as defined below (the choice may also depend on [[migration level]]). The APL Wiki generally uses "depth" to mean the positive depth.
Nested APLs vary in their definition of depth. They may take into account the array's [[prototype]], or not, and may use the positive or signed depth as defined below (the choice may also depend on [[migration level]]). The APL Wiki generally uses "depth" to mean the positive depth.


In the [[nested array model]], the depth is defined using the base case of a [[simple scalar]], which by definition has depth 0.
In the [[nested array model]], the depth is defined using the base case of a [[simple scalar]], which by definition has depth 0.
Line 9: Line 10:
The positive depth of a non-[[empty]] array other than a simple scalar is defined to be the largest depth among its [[element]]s, plus one. Thus a [[simple]] but non-[[scalar]] array has depth 1.
The positive depth of a non-[[empty]] array other than a simple scalar is defined to be the largest depth among its [[element]]s, plus one. Thus a [[simple]] but non-[[scalar]] array has depth 1.


The positive depth of an [[empty]] array is usually defined (for example, in [[Dyalog APL]]) to be the depth of its [[prototype]] plus one. It can also be set to 1, since it contains no elements but is not a simple scalar. This is the case in [[ngn/apl]].
The positive depth of an [[empty]] array is usually defined (for example, in [[Dyalog APL]]) to be the depth of its [[prototype]] plus one. It can also be set to 1, since it contains no elements but is not a simple scalar. This is the case in [[ngn/apl]] and [[dzaima/APL]].


An array has a ''consistent depth'' if it is a simple scalar, or if all of its elements (including the prototype, if prototype is used to determine depth) have a consistent depth and are equal in depth. The signed depth of an array is an integer with [[absolute value]] equal to its positive depth. It is negative if and only if it does not have a consistent depth.
An array has a ''consistent depth'' if it is a simple scalar, or if all of its elements (including the prototype, if prototypes are used to determine depth) have a consistent depth and are equal in depth. For example, the following works in all nested APLs with [[stranding]]:
 
<syntaxhighlight lang=apl>
[[dzaima/APL]] uses the minimum depth, which is 0 for a simple scalar and otherwise is one plus the ''minimum'' (rather than maximum) of the elements of a non-empty array. It defines the depth of an empty array to be one plus the depth of its prototype.
 
For arrays with a consistent depth the positive, signed, and minimum depth coincide. Thus the following example works in all nested APLs with [[stranding]].
<source lang=apl>
       ≡('ab' 'cde')('fg' 'hi')
       ≡('ab' 'cde')('fg' 'hi')
3
3
</source>
</syntaxhighlight>
 
The signed depth of an array is an integer with [[Magnitude|absolute value]] equal to its positive depth. It is negative if and only if the array does not have a consistent depth.


A [[simple]] array must have a consistent depth, because it is either a [[simple scalar]] or contains only simple scalars. In the latter case each element necessarily has depth 0 and a consistent depth. Because of this it is not possible to have an array with a signed depth of ¯1: any array with a positive depth of 1 must be simple, and hence have consistent depth.
A [[simple]] array must have a consistent depth, because it is either a [[simple scalar]] or contains only simple scalars. In the latter case each element necessarily has depth 0 and a consistent depth. Because of this it is not possible to have an array with a signed depth of ¯1: any array with a positive depth of 1 must be simple, and hence have consistent depth.
Line 27: Line 26:
In the [[flat array model]], the depth is the number of levels of [[box]]ing in an array. More precisely, the depth of a non-boxed or [[empty]] array is 0, and a non-empty boxed array has depth equal to one plus the maximum of the depths of the arrays it contains.
In the [[flat array model]], the depth is the number of levels of [[box]]ing in an array. More precisely, the depth of a non-boxed or [[empty]] array is 0, and a non-empty boxed array has depth equal to one plus the maximum of the depths of the arrays it contains.


The [[J]] language uses the token <source lang=apl inline>L.</source> and name "Level Of" for depth.
The [[J]] language uses the token <syntaxhighlight lang=j inline>L.</syntaxhighlight> and name "Level Of" for depth.


== External links ==
== External links ==
Line 39: Line 38:
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Depth.htm Dyalog]
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Depth.htm Dyalog]
* [http://microapl.com/apl_help/ch_020_020_350.htm APLX]
* [http://microapl.com/apl_help/ch_020_020_350.htm APLX]
* [https://code.jsoftware.com/wiki/Vocabulary/dollar#dyadic J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/lcapdot J NuVoc] (as <source lang=apl inline>L.</source> "Level of")  
* [https://www.jsoftware.com/help/dictionary/dlcapdot.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/lcapdot J NuVoc] (as <syntaxhighlight lang=apl inline>L.</syntaxhighlight> "Level of")  
* [https://mlochbaum.github.io/BQN/doc/depth.html#the-depth-function BQN]
{{APL features}}
{{APL features}}
{{APL built-ins}}
{{APL built-ins}}[[Category:Primitive functions]]

Latest revision as of 21:14, 10 September 2022

This page is about array depth, a number associated with every array. For the operator that applies a function at a given depth, see Depth (operator).

Depth () is a monadic primitive function that returns an array's depth. In the APL array model, the depth of an array is the number of levels of nesting or boxing it exhibits. In some languages, Depth returns a negative result to indicate that not all paths through the array have the same depth.

Nested array depth

Nested APLs vary in their definition of depth. They may take into account the array's prototype, or not, and may use the positive or signed depth as defined below (the choice may also depend on migration level). The APL Wiki generally uses "depth" to mean the positive depth.

In the nested array model, the depth is defined using the base case of a simple scalar, which by definition has depth 0.

The positive depth of a non-empty array other than a simple scalar is defined to be the largest depth among its elements, plus one. Thus a simple but non-scalar array has depth 1.

The positive depth of an empty array is usually defined (for example, in Dyalog APL) to be the depth of its prototype plus one. It can also be set to 1, since it contains no elements but is not a simple scalar. This is the case in ngn/apl and dzaima/APL.

An array has a consistent depth if it is a simple scalar, or if all of its elements (including the prototype, if prototypes are used to determine depth) have a consistent depth and are equal in depth. For example, the following works in all nested APLs with stranding:

      ≡('ab' 'cde')('fg' 'hi')
3

The signed depth of an array is an integer with absolute value equal to its positive depth. It is negative if and only if the array does not have a consistent depth.

A simple array must have a consistent depth, because it is either a simple scalar or contains only simple scalars. In the latter case each element necessarily has depth 0 and a consistent depth. Because of this it is not possible to have an array with a signed depth of ¯1: any array with a positive depth of 1 must be simple, and hence have consistent depth.

Flat array depth

In the flat array model, the depth is the number of levels of boxing in an array. More precisely, the depth of a non-boxed or empty array is 0, and a non-empty boxed array has depth equal to one plus the maximum of the depths of the arrays it contains.

The J language uses the token L. and name "Level Of" for depth.

External links

Lessons

Documentation

APL features [edit]
Built-ins Primitives (functions, operators) ∙ Quad name
Array model ShapeRankDepthBoundIndex (Indexing) ∙ AxisRavelRavel orderElementScalarVectorMatrixSimple scalarSimple arrayNested arrayCellMajor cellSubarrayEmpty arrayPrototype
Data types Number (Boolean, Complex number) ∙ Character (String) ∙ BoxNamespaceFunction array
Concepts and paradigms Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity elementComplex floorArray ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ GlyphLeading axis theoryMajor cell searchFirst-class function
Errors LIMIT ERRORRANK ERRORSYNTAX ERRORDOMAIN ERRORLENGTH ERRORINDEX ERRORVALUE ERROREVOLUTION ERROR
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