Tally: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(References section)
m (Text replacement - "<source" to "<syntaxhighlight")
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Built-in|Tally|≢}} is a [[primitive function|primitive]] [[monadic function]] which returns the number of [[major cell]]s in its [[argument]]. The Tally of an array is also the first [[element]] of its [[shape]], or 1 if it is a [[scalar]] (since a scalar is its own major cell by convention). Tally counts the first [[axis]] rather than the last because the number of major cells is more useful in [[leading axis theory]].
{{Built-in|Tally|≢}} or '''Count''' is a [[primitive function|primitive]] [[monadic function]] which returns the number of [[major cell]]s in its [[argument]]. The Tally of an array is also the first [[element]] of its [[shape]], or 1 if it is a [[scalar]] (since a scalar is its own major cell by convention). Tally counts the first [[axis]] rather than the last because the number of major cells is more useful in [[leading axis theory]].


== Examples ==
== Examples ==


Tally can compute the length of a [[numeric]] [[vector]] or [[string]].
Tally can compute the length of a [[numeric]] [[vector]] or [[string]].
<source lang=apl>
<syntaxhighlight lang=apl>
       ≢⍳12
       ≢⍳12
12
12
       ≢'string'
       ≢'string'
6
6
</source>
</syntaxhighlight>
It gives the length of the first [[axis]] in a higher-rank array. Tally applied to an array's [[shape]] gives its [[rank]].
It gives the length of the first [[axis]] in a higher-rank array. Tally applied to an array's [[shape]] gives its [[rank]].
<source lang=apl>
<syntaxhighlight lang=apl>
       ≢5 4 3 2⍴1 'b' 3 'd'   
       ≢5 4 3 2⍴1 'b' 3 'd'   
5
5
       ≢⍴5 4 3 2⍴1 'b' 3 'd'
       ≢⍴5 4 3 2⍴1 'b' 3 'd'
4
4
</source>
</syntaxhighlight>
The Tally of a [[scalar]] is always 1.
The Tally of a [[scalar]] is always 1.
<source lang=apl>
<syntaxhighlight lang=apl>
       ≢3.14
       ≢3.14
1
1
</source>
</syntaxhighlight>


== Description ==
== Description ==


Tally returns the length of the first [[axis]] of its argument if it has any axes (that is, if it is not a scalar), and 1 otherwise. This can be modelled easily with [[Shape]] and [[First]]:
Tally returns the length of the first [[axis]] of its argument if it has any axes (that is, if it is not a scalar), and 1 otherwise. This can be modelled easily with [[Shape]] and [[First]]:
<source lang=apl>
<syntaxhighlight lang=apl>
Tally ← {⊃(⍴⍵),1}
Tally ← {⊃(⍴⍵),1}
</source>
</syntaxhighlight>
An alternative implementation is to count the major cells by turning each into a scalar 1 with the [[Rank operator]], then adding them up:
An alternative implementation is to count the major cells by turning each into a scalar 1 with the [[Rank operator]], then adding them up:
<source lang=apl>
<syntaxhighlight lang=apl>
Tally ← +⌿ {1}⍤¯1
Tally ← +⌿ {1}⍤¯1
</source>
</syntaxhighlight>


== History ==
== History ==


Tally was introduced in [[A]] with the name "count" and symbol <source lang=j inline>#</source>. The same notation was carried forward to [[A+]], as well as [[J]] following [[Arthur Whitney]]'s suggestion. The primitive was present in [[NARS2000]] by 2010, with the name "Tally" and symbol <source lang=apl inline>></source><ref>NARS2000 Wiki. [http://wiki.nars2000.org/index.php?title=Tally&oldid=573 Tally]. Old revision: 2010-08-29.</ref>. The symbol <source lang=apl inline>≢</source> for Tally was introduced in [[Dyalog APL 14.0]], and quickly adopted by NARS2000. It was later added to [[GNU APL]] and has been included in many recent APLs based on Dyalog, such as [[ngn/apl]], [[dzaima/APL]], and [[APL\iv]].
Tally was introduced in [[A]] with the name "count" and symbol <syntaxhighlight lang=j inline>#</syntaxhighlight>. The same notation was carried forward to [[A+]], as well as [[J]] following [[Arthur Whitney]]'s suggestion. The primitive was present in [[NARS2000]] by 2010, with the name "Tally" and symbol <syntaxhighlight lang=apl inline>></syntaxhighlight><ref>NARS2000 Wiki. [http://wiki.nars2000.org/index.php?title=Tally&oldid=573 Tally]. Old revision: 2010-08-29.</ref>. The symbol <syntaxhighlight lang=apl inline>≢</syntaxhighlight> for Tally was introduced in [[Dyalog APL 14.0]], and quickly adopted by NARS2000. It was later added to [[GNU APL]] and has been included in many recent APLs based on Dyalog, such as [[ngn/apl]], [[dzaima/APL]], and [[APL\iv]].
 
Before the addition of Tally (and [[Zilde]]), there were numerous ways to get the length of a vector as a scalar:<ref>[[Roger Hui]] and [[Morten Kromberg]]. [https://dl.acm.org/doi/abs/10.1145/3386319 ''APL since 1978'']. §2.1 Tally. ACM [[HOPL]] IV. 2020-06.</ref>
<syntaxhighlight lang=apl>
''⍴⍴v
(⍴0)⍴⍴v
(⍳0)⍴⍴v
(⍴v)[0]
×/⍴v    ⍝ shortest and obscure
0⊥⍴v    ⍝ shortest and obscurest
</syntaxhighlight>


== External links ==
== External links ==
Line 46: Line 56:
=== Documentation ===
=== Documentation ===


* [http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Tally.htm Dyalog]
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Tally.htm Dyalog]
* [http://wiki.nars2000.org/index.php/Tally NARS2000]
* [http://wiki.nars2000.org/index.php/Tally NARS2000]
* J [http://www.jsoftware.com/help/dictionary/d400.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/number NuVoc]
* J [http://www.jsoftware.com/help/dictionary/d400.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/number NuVoc]
* [https://mlochbaum.github.io/BQN/doc/shape.html BQN] (as Length)


== References ==
== References ==


<references />
<references />
{{APL built-ins}}
{{APL built-ins}}[[Category:Primitive functions]]

Latest revision as of 10:59, 11 September 2022

Tally () or Count is a primitive monadic function which returns the number of major cells in its argument. The Tally of an array is also the first element of its shape, or 1 if it is a scalar (since a scalar is its own major cell by convention). Tally counts the first axis rather than the last because the number of major cells is more useful in leading axis theory.

Examples

Tally can compute the length of a numeric vector or string.

      ≢⍳12
12
      ≢'string'
6

It gives the length of the first axis in a higher-rank array. Tally applied to an array's shape gives its rank.

      ≢5 4 3 2⍴1 'b' 3 'd'  
5
      ≢⍴5 4 3 2⍴1 'b' 3 'd'
4

The Tally of a scalar is always 1.

      ≢3.14
1

Description

Tally returns the length of the first axis of its argument if it has any axes (that is, if it is not a scalar), and 1 otherwise. This can be modelled easily with Shape and First:

Tally ← {⊃(⍴⍵),1}

An alternative implementation is to count the major cells by turning each into a scalar 1 with the Rank operator, then adding them up:

Tally ← +⌿ {1}⍤¯1

History

Tally was introduced in A with the name "count" and symbol #. The same notation was carried forward to A+, as well as J following Arthur Whitney's suggestion. The primitive was present in NARS2000 by 2010, with the name "Tally" and symbol >[1]. The symbol for Tally was introduced in Dyalog APL 14.0, and quickly adopted by NARS2000. It was later added to GNU APL and has been included in many recent APLs based on Dyalog, such as ngn/apl, dzaima/APL, and APL\iv.

Before the addition of Tally (and Zilde), there were numerous ways to get the length of a vector as a scalar:[2]

''⍴⍴v
(⍴0)⍴⍴v
(⍳0)⍴⍴v
(⍴v)[0]
×/⍴v    ⍝ shortest and obscure
0⊥⍴v    ⍝ shortest and obscurest

External links

Lessons

Documentation

References

  1. NARS2000 Wiki. Tally. Old revision: 2010-08-29.
  2. Roger Hui and Morten Kromberg. APL since 1978. §2.1 Tally. ACM HOPL IV. 2020-06.
APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
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 axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector