Encode: Difference between revisions

Jump to navigation Jump to search
99 bytes added ,  21:44, 10 September 2022
m
Text replacement - "</source>" to "</syntaxhighlight>"
(Encode should link to dyadic version of #: in J)
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]] <source lang=apl inline>⊥</source> with the same left argument X, when X is a [[vector]].
{{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]] <source 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 <source lang=apl inline>0 7 24 60⊤12345</source> (convert 12345 minutes to weeks, days, hours, and minutes) as an example.
Encode works by finding the "digits" one by one from the rightmost digit. Let's consider <source lang=apl inline>0 7 24 60⊤12345</syntaxhighlight> (convert 12345 minutes to weeks, days, hours, and minutes) as an example.


<source lang=apl>
<source lang=apl>
Line 18: Line 18:
       7÷⍨8-1        ⍝ 1 week remaining to convert
       7÷⍨8-1        ⍝ 1 week remaining to convert
1                  ⍝ No more conversion needed, since there is no limit for the highest digit
1                  ⍝ No more conversion needed, since there is no limit for the highest digit
</source>
</syntaxhighlight>


Collecting all the digits gives the desired result.
Collecting all the digits gives the desired result.
Line 25: Line 25:
       0 7 24 60⊤12345
       0 7 24 60⊤12345
1 1 13 45
1 1 13 45
</source>
</syntaxhighlight>


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.
Line 48: Line 48:
0 2 7
0 2 7
1 1 9
1 1 9
</source>
</syntaxhighlight>


== Examples ==
== Examples ==
Line 61: Line 61:
       ((1+⌊10⍟12345)⍴10)⊤12345  ⍝ Convert to decimal digits using just as many digits as needed
       ((1+⌊10⍟12345)⍴10)⊤12345  ⍝ Convert to decimal digits using just as many digits as needed
1 2 3 4 5
1 2 3 4 5
</source>
</syntaxhighlight>


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?
Line 68: Line 68:
       0 24 60 60⊤210859
       0 24 60 60⊤210859
2 10 34 19
2 10 34 19
</source>
</syntaxhighlight>


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.
Line 81: Line 81:
       0 1⊤○1  ⍝ Integer and fractional parts of pi
       0 1⊤○1  ⍝ Integer and fractional parts of pi
3 0.1415926536
3 0.1415926536
</source>
</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 (<source lang=apl inline>⎕IO←0</source>) is required for this to hold.
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 (<source lang=apl inline>⎕IO←0</syntaxhighlight>) is required for this to hold.


<source lang=apl>
<source lang=apl>
Line 93: Line 93:
0 0 0 0 1 1 1 1 2 2 2 2 0 0 0 0 1 1 1 1 2 2 2 2
0 0 0 0 1 1 1 1 2 2 2 2 0 0 0 0 1 1 1 1 2 2 2 2
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
</source>
</syntaxhighlight>


== External links ==
== External links ==
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 <source lang=j inline>#:</source>)
* J [https://www.jsoftware.com/help/dictionary/d402.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/numberco#dyadic NuVoc] (as <source lang=j inline>#:</syntaxhighlight>)


{{APL built-ins}}[[Category:Primitive functions]]
{{APL built-ins}}[[Category:Primitive functions]]

Navigation menu