Unique: Difference between revisions

Jump to navigation Jump to search
2,612 bytes added ,  05:52, 9 June 2020
m
Text replacement - " ⊢( *[^∘])" to " ⎕←$1"
Miraheze>Adám Brudzewsky
m (Text replacement - " ⊢( *[^∘])" to " ⎕←$1")
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Primitive|∪|Unique}}, or '''Nub''', is a [[monadic]] [[set function]] which removes duplicate [[major cells]] from an array. It returns only the distinct cells (those which do not [[match]] an earlier distinct cell) in the order they appeared in the array.
{{Built-ins|Unique|∪|}}, or '''Nub''', is a [[monadic]] [[set function]] which removes duplicate [[major cells]] from an array. It returns only the distinct cells (those which do not [[match]] an earlier distinct cell) in the order they appeared in the array.


== Examples ==
== Examples ==
Line 15: Line 15:
└───┴───┴────┘
└───┴───┴────┘
</source>
</source>
In some languages, such as [[Dyalog APL]] and [[J]], Unique applies to the [[major cells]] of an array, including those with rank greater than 1:
In some languages, such as [[J]] and [[Dyalog APL 17.0]] and later, Unique applies to the [[major cells]] of an array, including those with rank greater than 1:
<source lang=apl>
<source lang=apl>
       ⊢x ← (⍳10)∘.∨2 3 6
       ⎕←x ← (⍳10)∘.∨2 3 6
1 1 1
1 1 1
2 1 2
2 1 2
Line 41: Line 41:
To construct the Unique of an array, the major cells are considered in order of increasing [[index]]. Each cell is included if it does not [[match]] any cell which was already included.
To construct the Unique of an array, the major cells are considered in order of increasing [[index]]. Each cell is included if it does not [[match]] any cell which was already included.


The array matching is subject to [[tolerant comparison]]. In the intolerant case, that is, when equality is [https://en.wikipedia.org/wiki/Transitive_relation transitive], a cell is included if and only if it does not match any other cell which appears earlier in the array. That's because all the discarded duplicate cells must have matched an earlier included cell; if equality is transitive then a cell which matches the duplicate would also match the earlier cell.
The array matching is subject to [[tolerant comparison]]. In the intolerant case, that is, when equality is [[wikipedia:Transitive_relation|transitive]], a cell is included if and only if it does not match any other cell which appears earlier in the array. That's because all the discarded duplicate cells must have matched an earlier included cell; if equality is transitive then a cell which matches the duplicate would also match the earlier cell.


=== Tolerant comparison ===
=== Tolerant comparison ===
Line 49: Line 49:
       ⎕CT←1e¯14
       ⎕CT←1e¯14
       ⎕PP←18 ⍝ Print all digits
       ⎕PP←18 ⍝ Print all digits
       ⊢x←1+⎕CT×0 0.6 1.2
       ⎕←x←1+⎕CT×0 0.6 1.2
1 1.000000000000006 1.000000000000012
1 1.000000000000006 1.000000000000012
       ⍳⍨x    ⍝ Each element equal to the previous
       ⍳⍨x    ⍝ Each element equal to the previous
Line 61: Line 61:
</source>
</source>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
== History ==
The function Nub was proposed in 1978 by [[Ken Iverson|Iverson]] as one of a collection of set functions (including [[Union]], [[Intersection]], and [[Difference]]) with the symbol <source lang=apl inline>∪</source> and definition <source lang=apl inline>∪w</source> {{←→}} <source lang=apl inline>((⍳⍴w)=w⍳w)/w←,w</source>.<ref>[[Ken Iverson|Iverson, Ken]]. [https://www.jsoftware.com/papers/opfns.htm ''Operators and Functions'']. IBM Research Report #RC7091. 1978-04-26.</ref> It was implemented as part of [[STSC]]'s [[NARS]] in 1981 with the name Unique and the same definition, except that the argument was restricted to be a vector or scalar.<ref>Cheney, Carl. ''APL*PLUS Nested Arrays Reference Manual. [[STSC]] Inc. 1981.</ref> Later APLs such as [[Dyalog APL]] generally adopted this version of the primitive, and it is featured in the [[ISO/IEC 13751:2001]] standard. Dyalog extended Unique to higher-rank arrays in [[Dyalog APL 17.0]], following the much earlier extension made by [[J]].
Iverson continued to refine his definition of Nub. He included it in his [[Rationalized APL]] specification with no changes in 1983, but modified it to work on [[major cell]]s (thus aligning it with [[leading axis theory]]) in his 1987 [[A Dictionary of APL]]. Iverson's dictionary also changed the symbol for Nub to <source lang=apl inline>↑</source>, added the functions [[Nub Sieve]] (<source lang=apl inline>≠</source>) and [[Nub in]] (<source lang=apl inline>=</source>), and defined Nub in terms of Nub Sieve. These definitions were adopted by [[IPSA]] in the [[SHARP APL]] successor [[SAX]]. [[J]] uses similar definitions: its Nub-related functions are Nub (<source lang=j inline>~.</source>), [[Nub Sieve]] (<source lang=j inline>~:</source>), and [[Self-classify]] (<source lang=j inline>=</source>). However, Self-classify is indicated as deprecated in J's NuVoc because its result can be much larger than its argument.
[[A+]] does not include Unique or any related functions, but [[K]]'s function range (<code>?</code>) gives the unique elements of a list argument. In later versions it is called "distinct" or "unique". While APLs allow a [[scalar]] argument ([[scalar rank extension]]), K gives a [[RANK ERROR]] if the argument is not a list.


== APL model ==
== APL model ==


For [[Vector|vectors]] the following implementation based on [[Union]] can be used. It repeatedly adds cells of the argument to an accumulated unique vector <code>u</code>, using Union so that duplicate cells are never added.
For [[Vector|vectors]] the following implementation based on [[Union]] can be used. It repeatedly adds cells of the argument to an accumulated unique vector <source lang=apl inline>u</source>, using Union so that duplicate cells are never added.
<source lang=apl>
<source lang=apl>
VecUnique ← {
VecUnique ← {
Line 83: Line 91:
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}


While the model <code>{((⍳≢⍵)=(⍵⍳⍵))⌿⍵}</code>, based on [[Nub Sieve]], is often described as an implementation of Unique, it does not correctly handle [[tolerant comparison]]. A correct implementation of Nub Sieve could be used to implement Unique, but writing such an implementation is no easier than implementing Unique directly.
While the model <source lang=apl inline>{((⍳≢⍵)=(⍵⍳⍵))⌿⍵}</source>, based on [[Nub Sieve]], is often described as an implementation of Unique, it does not correctly handle [[tolerant comparison]].<ref>Hui, Roger. [https://dyalog.tv/Dyalog17/?v=fPWky9IOG40 Tolerant Unique]. [[Dyalog '17]].</ref> A correct implementation of Nub Sieve could be used to implement Unique, but writing such an implementation is no easier than implementing Unique directly.


== Properties ==
== Properties ==
Line 95: Line 103:
=== Documentation ===
=== Documentation ===


* [http://help.dyalog.com/latest/Content/Language/Primitive%20Functions/Unique.htm Dyalog]
* [http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Unique.htm Dyalog]
 
* [http://microapl.com/apl_help/ch_020_020_392.htm APLX]
 
* [https://www.jsoftware.com/help/dictionary/d221.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/tildedot J NuVoc] (as <source lang=apl inline>~.</source>)


* [https://www.jsoftware.com/help/dictionary/d221.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/tildedot J NuVoc] (as <code>~.</code>)
== References ==
{{APL built-ins}}
<references />
{{APL built-ins}}[[Category:Primitive functions]][[Category:Set functions]]

Navigation menu