Encode: Difference between revisions
Jump to navigation
Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>") |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
Line 1: | Line 1: | ||
{{Built-in|Encode|⊤}}, also called '''Represent''' or '''Antibase''', is a [[dyadic]] [[primitive function]] which computes the representation of the right argument in the radix system defined by the left argument. Some implementations add [[monadic]] usage to this function, which computes the binary representation using as many bits as needed. Encode is the [[inverse]] of [[Decode]] < | {{Built-in|Encode|⊤}}, also called '''Represent''' or '''Antibase''', is a [[dyadic]] [[primitive function]] which computes the representation of the right argument in the radix system defined by the left argument. Some implementations add [[monadic]] usage to this function, which computes the binary representation using as many bits as needed. Encode is the [[inverse]] of [[Decode]] <syntaxhighlight lang=apl inline>⊥</syntaxhighlight> with the same left argument X, when X is a [[vector]]. | ||
== Concept == | == Concept == | ||
Encode works by finding the "digits" one by one from the rightmost digit. Let's consider < | Encode works by finding the "digits" one by one from the rightmost digit. Let's consider <syntaxhighlight lang=apl inline>0 7 24 60⊤12345</syntaxhighlight> (convert 12345 minutes to weeks, days, hours, and minutes) as an example. | ||
< | <syntaxhighlight lang=apl> | ||
60|12345 ⍝ Minute's digit; 45 minutes | 60|12345 ⍝ Minute's digit; 45 minutes | ||
45 | 45 | ||
Line 22: | Line 22: | ||
Collecting all the digits gives the desired result. | Collecting all the digits gives the desired result. | ||
< | <syntaxhighlight lang=apl> | ||
0 7 24 60⊤12345 | 0 7 24 60⊤12345 | ||
1 1 13 45 | 1 1 13 45 | ||
Line 29: | Line 29: | ||
If the left argument has high [[rank]], the vectors over the first [[axis]] act as independent radix systems. | If the left argument has high [[rank]], the vectors over the first [[axis]] act as independent radix systems. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←mat←8 3⍴2 10 16 ⍝ Base 2, 10, and 16, being able to represent at most 8 digits | ⎕←mat←8 3⍴2 10 16 ⍝ Base 2, 10, and 16, being able to represent at most 8 digits | ||
2 10 16 | 2 10 16 | ||
Line 54: | Line 54: | ||
A common use case is to convert an integer to base N, usually base 2 or 10. However, Encode does not know how many digits to produce, so it needs to be supplied as the [[shape]] of the left argument. | A common use case is to convert an integer to base N, usually base 2 or 10. However, Encode does not know how many digits to produce, so it needs to be supplied as the [[shape]] of the left argument. | ||
< | <syntaxhighlight lang=apl> | ||
10⊤12345 ⍝ This does not work | 10⊤12345 ⍝ This does not work | ||
5 | 5 | ||
Line 65: | Line 65: | ||
Encode can be also used to convert a measure given in the smallest unit to a hierarchy of units. For example, given that 1 day = 24 hours, 1 hour = 60 minutes, and 1 minute = 60 seconds, how many days/hours/minutes/seconds is 210859 seconds? | Encode can be also used to convert a measure given in the smallest unit to a hierarchy of units. For example, given that 1 day = 24 hours, 1 hour = 60 minutes, and 1 minute = 60 seconds, how many days/hours/minutes/seconds is 210859 seconds? | ||
< | <syntaxhighlight lang=apl> | ||
0 24 60 60⊤210859 | 0 24 60 60⊤210859 | ||
2 10 34 19 | 2 10 34 19 | ||
Line 72: | Line 72: | ||
Another common usage of Encode is to simulate "divmod": a function which, given two integers, computes the quotient and remainder at once. The construct can also be used to extract the integer part and fractional part of a real number. | Another common usage of Encode is to simulate "divmod": a function which, given two integers, computes the quotient and remainder at once. The construct can also be used to extract the integer part and fractional part of a real number. | ||
< | <syntaxhighlight lang=apl> | ||
⌊7÷3 ⍝ Quotient | ⌊7÷3 ⍝ Quotient | ||
2 | 2 | ||
Line 83: | Line 83: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Encode has an important property with array [[index]]: Given an arbitrary array A with [[shape]] S, encoding a [[ravel|raveled]] index by S gives the original index in A. This can be used to generate all indices of a given array or array shape. Note that [[index origin]] 0 (< | Encode has an important property with array [[index]]: Given an arbitrary array A with [[shape]] S, encoding a [[ravel|raveled]] index by S gives the original index in A. This can be used to generate all indices of a given array or array shape. Note that [[index origin]] 0 (<syntaxhighlight lang=apl inline>⎕IO←0</syntaxhighlight>) is required for this to hold. | ||
< | <syntaxhighlight lang=apl> | ||
⎕IO←0 | ⎕IO←0 | ||
⎕A[17]=(2 3 4⊤17)⌷2 3 4⍴⎕A | ⎕A[17]=(2 3 4⊤17)⌷2 3 4⍴⎕A | ||
Line 105: | Line 105: | ||
* [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Encode.htm Dyalog] | * [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Encode.htm Dyalog] | ||
* [http://microapl.com/apl_help/ch_020_020_650.htm APLX] | * [http://microapl.com/apl_help/ch_020_020_650.htm APLX] | ||
* J [https://www.jsoftware.com/help/dictionary/d402.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/numberco#dyadic NuVoc] (as < | * J [https://www.jsoftware.com/help/dictionary/d402.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/numberco#dyadic NuVoc] (as <syntaxhighlight lang=j inline>#:</syntaxhighlight>) | ||
{{APL built-ins}}[[Category:Primitive functions]] | {{APL built-ins}}[[Category:Primitive functions]] |