Equal to: Difference between revisions
Miraheze>Marshall |
m (→References) |
||
(13 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{Built-in|Equal to|<nowiki>=</nowiki>}} is a [[comparison function]] which tests whether argument elements are [[ | {{Built-in|Equal to|<nowiki>=</nowiki>}} is a [[comparison function]] which tests whether argument elements are tolerantly<ref>[[Robert Bernecky|Bernecky, Robert]]. [https://www.jsoftware.com/papers/satn23.htm "Comparison Tolerance"]. Sharp APL Technical Notes. 1977-06-10;.</ref> equal to each other: it returns 1 if the [[element]]s being compared [[match]] and 0 if they do not. It is the [[Not|negation]] of [[Not Equal to]] (<syntaxhighlight lang=apl inline>≠</syntaxhighlight>). | ||
APL's < | APL's <syntaxhighlight lang=apl inline>=</syntaxhighlight> differs from the usage of <math>=</math> in [[Comparison with traditional mathematics|traditional mathematical notation]] by having an implied [[Ken_Iverson#Iverson_bracket|Iverson bracket]]. Thus, <syntaxhighlight lang=apl inline>i=j</syntaxhighlight> is APL is equivalent to <math>[i=j]</math> or <math>\delta_{ij}</math> ([[wikipedia:Kronecker delta|Kronecker delta]]) in mathematics. | ||
== Examples == | == Examples == | ||
Equal to compares arrays one [[element]] at a time, returning 1 when elements match and 0 when they do not. | Equal to compares arrays one [[element]] at a time, returning 1 when elements match and 0 when they do not. | ||
< | <syntaxhighlight lang=apl> | ||
3 2 1 = 1 2 3 | 3 2 1 = 1 2 3 | ||
0 1 0 | 0 1 0 | ||
Line 13: | Line 13: | ||
3j2 2j2 1j2 = 2j2 | 3j2 2j2 1j2 = 2j2 | ||
0 1 0 | 0 1 0 | ||
</ | </syntaxhighlight> | ||
It is subject to [[comparison tolerance]]: floating-point numbers which are very close together are considered equal, even though they are slightly different: | It is subject to [[comparison tolerance]]: floating-point numbers which are very close together are considered equal, even though they are slightly different: | ||
< | <syntaxhighlight lang=apl> | ||
⎕CT | ⎕CT | ||
1E¯14 | 1E¯14 | ||
Line 25: | Line 25: | ||
1 {⎕CT←0⋄⍺=⍵} x ⍝ Overriding ⎕CT within a dfn | 1 {⎕CT←0⋄⍺=⍵} x ⍝ Overriding ⎕CT within a dfn | ||
0 0 1 0 0 | 0 0 1 0 0 | ||
</ | </syntaxhighlight> | ||
In a [[Nested array model|nested]] APL, Equal to is pervasive, and compares all [[simple scalar]]s found in a [[nested array]]. | In a [[Nested array model|nested]] APL, Equal to is pervasive, and compares all [[simple scalar]]s found in a [[nested array]]. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←n ← ((2 1)0)(1 0 3)(2 2⍴4 0) | |||
┌───────┬─────┬───┐ | ┌───────┬─────┬───┐ | ||
│┌───┬─┐│1 0 3│4 0│ | │┌───┬─┐│1 0 3│4 0│ | ||
Line 40: | Line 40: | ||
│└───┴─┘│ │ │ | │└───┴─┘│ │ │ | ||
└───────┴─────┴───┘ | └───────┴─────┴───┘ | ||
</ | </syntaxhighlight> | ||
In a [[Flat array model|flat]] array language such as [[J]], it instead compares [[Box]]es directly, since they are the [[element]]s of a boxed array. | In a [[Flat array model|flat]] array language such as [[J]], it instead compares [[Box]]es directly, since they are the [[element]]s of a boxed array. | ||
< | <syntaxhighlight lang=j> | ||
(<'string') = 'which';'is';'string';'?' | (<'string') = 'which';'is';'string';'?' | ||
0 0 1 0 | 0 0 1 0 | ||
</ | </syntaxhighlight> | ||
== Boolean function == | == Boolean function == | ||
{{Main|Boolean function}} | |||
When the arguments to Equal to are [[Boolean]], it is the [[wikipedia:Logical biconditional|Logical biconditional]] function, or the [[Not|negation]] of the [[xor]] function (which is [[Not Equal to]] restricted to Boolean arguments): | |||
{|class=wikitable | |||
!<syntaxhighlight lang=apl inline>=</syntaxhighlight>!!<syntaxhighlight lang=apl inline>0</syntaxhighlight>!!<syntaxhighlight lang=apl inline>1</syntaxhighlight> | |||
|- | |||
!<syntaxhighlight lang=apl inline>0</syntaxhighlight> | |||
|<syntaxhighlight lang=apl inline>1</syntaxhighlight>||<syntaxhighlight lang=apl inline>0</syntaxhighlight> | |||
|- | |||
!<syntaxhighlight lang=apl inline>1</syntaxhighlight> | |||
|<syntaxhighlight lang=apl inline>0</syntaxhighlight>||<syntaxhighlight lang=apl inline>1</syntaxhighlight> | |||
|} | |||
In the context of logic, it can be read as ''if and only if''. This function is also known as xnor or the Kronecker delta. | |||
[[Reduction]] with <syntaxhighlight lang=apl inline>=</syntaxhighlight> is the same as reduction with xor (<syntaxhighlight lang=apl inline>≠</syntaxhighlight>), but [[Not|inverted]] if the number of reduced elements is even. The left and right [[identity element]] for Boolean <syntaxhighlight lang=apl inline>=</syntaxhighlight> is 1. | |||
== External links == | |||
=== Documentation === | === Documentation === | ||
* [ | * [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Equal.htm Dyalog] | ||
* [http://microapl.com/apl_help/ch_020_020_310.htm APLX] | * [http://microapl.com/apl_help/ch_020_020_310.htm APLX] | ||
* J [https://www.jsoftware.com/help/dictionary/d000.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/eq#dyadic NuVoc] | * J [https://www.jsoftware.com/help/dictionary/d000.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/eq#dyadic NuVoc] | ||
{{APL built-ins}} | * [https://mlochbaum.github.io/BQN/doc/arithmetic.html#comparisons BQN] | ||
== References == | |||
<references /> | |||
{{APL built-ins}}[[Category:Primitive functions]][[Category:Comparison functions]] |
Latest revision as of 21:57, 28 November 2022
=
|
Equal to (=
) is a comparison function which tests whether argument elements are tolerantly[1] equal to each other: it returns 1 if the elements being compared match and 0 if they do not. It is the negation of Not Equal to (≠
).
APL's =
differs from the usage of in traditional mathematical notation by having an implied Iverson bracket. Thus, i=j
is APL is equivalent to or (Kronecker delta) in mathematics.
Examples
Equal to compares arrays one element at a time, returning 1 when elements match and 0 when they do not.
3 2 1 = 1 2 3 0 1 0 '321' = '123' 0 1 0 3j2 2j2 1j2 = 2j2 0 1 0
It is subject to comparison tolerance: floating-point numbers which are very close together are considered equal, even though they are slightly different:
⎕CT 1E¯14 x ← 1 + (⎕CT×0.6)ׯ2 ¯1 0 1 2 ⍝ Numbers close to 1 1 - x ⍝ Nonzero difference 1.199040867E¯14 5.995204333E¯15 0 ¯5.995204333E¯15 ¯1.199040867E¯14 1 = x ⍝ Close but different values are equal 0 1 1 1 0 1 {⎕CT←0⋄⍺=⍵} x ⍝ Overriding ⎕CT within a dfn 0 0 1 0 0
In a nested APL, Equal to is pervasive, and compares all simple scalars found in a nested array.
⎕←n ← ((2 1)0)(1 0 3)(2 2⍴4 0) ┌───────┬─────┬───┐ │┌───┬─┐│1 0 3│4 0│ ││2 1│0││ │4 0│ │└───┴─┘│ │ │ └───────┴─────┴───┘ 0 = n ┌───────┬─────┬───┐ │┌───┬─┐│0 1 0│0 1│ ││0 0│1││ │0 1│ │└───┴─┘│ │ │ └───────┴─────┴───┘
In a flat array language such as J, it instead compares Boxes directly, since they are the elements of a boxed array.
(<'string') = 'which';'is';'string';'?' 0 0 1 0
Boolean function
- Main article: Boolean function
When the arguments to Equal to are Boolean, it is the Logical biconditional function, or the negation of the xor function (which is Not Equal to restricted to Boolean arguments):
= |
0 |
1
|
---|---|---|
0
|
1 |
0
|
1
|
0 |
1
|
In the context of logic, it can be read as if and only if. This function is also known as xnor or the Kronecker delta.
Reduction with =
is the same as reduction with xor (≠
), but inverted if the number of reduced elements is even. The left and right identity element for Boolean =
is 1.
External links
Documentation
- Dyalog
- APLX
- J Dictionary, NuVoc
- BQN
References
- ↑ Bernecky, Robert. "Comparison Tolerance". Sharp APL Technical Notes. 1977-06-10;.