Indices: Difference between revisions

Jump to navigation Jump to search
2,494 bytes added ,  20:05, 4 August 2021
mNo edit summary
(17 intermediate revisions by 3 users not shown)
Line 1: Line 1:
:''This page describes a primitive whose result is a list of indices. See [[Index]] for the page on indices themselves. See [[Index of]] for the index generator.''
:''This page about finding indices of non-zero values. See [[Index]] for the page on indices themselves. See [[Indexing]], [[Index Generator]], [[Index of]], and [[Interval Index]] for other operations named after indices.''


{{Built-in|Indices|⍸}}, or '''Where''', is a [[monadic]] [[primitive function]] which returns the [[Index|indices]] of all ones in a [[Boolean]] array. More generally, Indices accepts an array of non-negative integers and copies each index the corresponding number of times.
{{Built-in|Indices|⍸}}, or '''Where''', is a [[monadic]] [[primitive function]] which returns the [[Index|indices]] of all ones in a [[Boolean]] array. More generally, Indices accepts an array of non-negative integers and copies each index the corresponding number of times. It is closely related to [[Replicate]], and may be seen as an index-based equivalent to Replicate in the same way that [[Grade]] is an index-based equivalent of sorting.


Where on a Boolean argument was introduced in [[K]] as <source lang=k inline>&</source>, and it was extended to non-negative integers in [[J]], with the name Indices (<source lang=j inline>I.</source>).
In [[K]], the first language to include the primitive, it is called Where (<code>&</code>). In [[J]], it is called Indices (<source lang=j inline>I.</source>).


== Examples ==
== Examples ==
Line 51: Line 51:


Indices may be viewed as a way to convert between two ways of writing [[wikipedia:multiset|multiset]]s of array [[Index|indices]]. The argument uses a dense representation indicating for each index the number of times it appears, and the result uses a sparse representation which lists all the indices contained in the set.
Indices may be viewed as a way to convert between two ways of writing [[wikipedia:multiset|multiset]]s of array [[Index|indices]]. The argument uses a dense representation indicating for each index the number of times it appears, and the result uses a sparse representation which lists all the indices contained in the set.
== Relation with Replicate ==
Indices on a [[vector]] is closely related to [[Replicate]]: for vectors <source lang=apl inline>V</source> and <source lang=apl inline>W</source>, we have <source lang=apl inline>⍸V</source> {{←→}} <source lang=apl inline>V/⍳≢V</source> and <source lang=apl inline>V⌿X</source> {{←→}} <source lang=apl inline>X[⍸V]</source>. While Replicate performs a transformation on another array, Indices gives a representation of that transformation using [[Index|indices]]. The relationship between Indices and Replicate parallels that between [[Grade]] and [[Sort By]].
[[K]] takes advantage of this relationship by removing the primitive Replicate entirely: the glyph <code>&</code> is paired with [[Minimum]] instead. In K, Replicate is performed by using Where and then [[Bracket indexing|indexing]].
== Inverse ==
The [[inverse]] of Indices, <source lang=apl inline>⍸⍣¯1</source>, is the mapping from an ordered (multi-)set of indices to an array where each element is the count for its position. For a simple non-empty vector <source lang=apl inline>Y</source> without duplicates, the expression <source lang=apl inline>R←(⍸⍣¯1)Y</source> gives a Boolean vector <source lang=apl inline>R</source> with ones at the indices in <source lang=apl inline>Y</source>. This is equivalent to <source lang=apl inline>R←(1@Y)0⍴⍨⌈/Y</source> which is useful in conversion between [[partition representations]].
It should be noted that the inverse is not unique because <source lang=apl inline>(⍸Y) ≡ (⍸Z)</source> if <source lang=apl inline>Y</source> and <source lang=apl inline>Z</source> differ only by the number of trailing zeros. <source lang=apl inline>⍸⍣¯1</source> does not add any trailing zeros, and it may be necessary to add those separately, for example using [[overtake]].


== History ==
== History ==
Line 56: Line 68:
[[Idiom]]s with similar behavior to Indices were widely used in APL long before it was made into a primitive. For example, the [[FinnAPL idiom library]], first presented in 1984, lists <source lang=apl inline>X/⍳⍴X</source> as "594. Indices of ones in logical vector X".
[[Idiom]]s with similar behavior to Indices were widely used in APL long before it was made into a primitive. For example, the [[FinnAPL idiom library]], first presented in 1984, lists <source lang=apl inline>X/⍳⍴X</source> as "594. Indices of ones in logical vector X".


Where (<source lang=k inline>&</source>) with a Boolean argument was present in [[K]] by K2 in 1998.<ref>Kx Systems. [http://web.archive.org/web/20041022042401/http://www.kx.com/technical/documents/kusrlite.pdf "K User Manual"]. 1998.</ref> It was extended to vectors of non-negative integers when it was added to [[J]] as Indices (<source lang=j inline>I.</source>) in release 5.02 (2003).<ref>Jsoftware. [https://www.jsoftware.com/docs/archive/release/ifb.htm "I. Implements ''Indices'']. 2003.</ref>
Where (<code>&</code>) with a Boolean argument was present in [[K]] by K2 in 1996,<ref>Kx Systems. [http://web.archive.org/web/20041022042401/http://www.kx.com/technical/documents/kusrlite.pdf "K User Manual"]. 1998.</ref> and extended to non-negative integers by K4 in 2000. It was added to [[J]] for the domain of non-negative integer vectors as Indices (<source lang=j inline>I.</source>) in release 5.02 (2003), introducing the pairing of Indices and [[Interval Index]] now used in APL.<ref>Jsoftware. [https://www.jsoftware.com/docs/archive/release/ifb.htm "I. Implements ''Indices'']. 2003.</ref>
 
Indices (<source lang=apl inline>⍸</source>) was first introduced to APL, and the [[nested array model]], by [[NARS2000]]. Originally defined only for vectors, the generalised definition <source lang=apl inline>(,R)/,⍳⍴1/R</source> was introduced in about 2013 after some experimentation with alternatives.<ref>NARS2000 Wiki. [http://wiki.nars2000.org/index.php?title=Indices&direction=next&oldid=863 Indices]. Old revision: 2013-05-26.</ref> Where (<source lang=apl inline>⍸</source>) was added to [[Dyalog APL 16.0]] (June 2017), with the nearly-identical definition <source lang=apl inline>{(,⍵)⌿,⍳⍴⍵}</source>, but also with the restriction that the argument be Boolean. This restriction that was lifted to allow non-negative integers in [[Dyalog APL 18.0|18.0]] (2020). For a [[scalar]] <source lang=apl inline>I</source>, Dyalog's definition gives <source lang=apl inline>I⍴⊂⍬</source> for <source lang=apl inline>⍸I</source>, while NARS2000 returned <source lang=apl inline>I⍴1</source>. By January 2018, NARS2000 switched to Dyalog's definition, removing the discrepancy for scalar arguments.


Indices (<source lang=apl inline>⍸</source>) was first introduced to APL, and the [[nested array model]], by [[NARS2000]]. Originally defined only for vectors, the generalised definition <source lang=apl inline>(,R)/,⍳⍴1/R</source> was introduced in about 2013 after some experimentation with alternatives.<ref>NARS2000 Wiki. [http://wiki.nars2000.org/index.php?title=Indices&direction=next&oldid=863 Indices]. Old revision: 2013-05-26.</ref> Where (<source lang=apl inline>⍸</source>), with almost the same definition, was added to [[Dyalog APL 16.0]] (June 2017), but with the restriction that the argument be Boolean, a restriction that was lifted to allow non-negative integers in [[Dyalog APL 18.0|18.0]] (2020). Dyalog's definition also returned <source lang=apl inline>I⍴⊂⍬</source> for <source lang=apl inline>⍸I</source> on a scalar <source lang=apl inline>I</source>, as opposed to NARS2000 which returned <source lang=apl inline>I⍴1</source>. In January 2018, NARS2000 switched to Dyalog's definition for scalar arguments; <source lang=apl inline>{(,⍵)⌿,⍳⍴⍵}</source>.
The [[#inverse|inverse]] of Indices became supported with [[Dyalog APL 18.0]].


== External links ==
== External links ==
Line 64: Line 78:
=== Lessons ===
=== Lessons ===


[https://chat.stackexchange.com/transcript/52405?m=41724812#41724812 APL Cultivation]
* [https://chat.stackexchange.com/transcript/52405?m=41724812#41724812 APL Cultivation]


=== Documentation ===
=== Documentation ===


[http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Where.htm Dyalog]
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Where.htm Dyalog]
 
* [http://wiki.nars2000.org/index.php/Indices NARS2000]
[http://wiki.nars2000.org/index.php/Indices NARS2000]
* J [https://www.jsoftware.com/help/dictionary/dicapdot.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/icapdot NuVoc] (as <source lang=j inline>I.</source>)
 
* [https://mlochbaum.github.io/BQN/doc/replicate.html#indices BQN] (as <code>/</code>)
J [https://www.jsoftware.com/help/dictionary/dicapdot.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/icapdot NuVoc]
* [https://github.com/kevinlawler/kona/wiki/Where Kona (K3)] (as <code>&</code>)


== References ==
== References ==


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

Navigation menu