4,494
edits
m (Text replacement - "<source" to "<syntaxhighlight") |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
Line 1: | Line 1: | ||
{{Built-in|Decode|⊥}}, also called '''Base''', is a [[dyadic]] [[primitive function]] which evaluates the right [[argument]] in terms of the radix system defined by the left argument. Some implementations add [[monadic]] usage to this function, where the left argument defaults to the [[scalar]] 2. Decode is the [[inverse]] of [[Encode]] <syntaxhighlight lang=apl inline>⊤</ | {{Built-in|Decode|⊥}}, also called '''Base''', is a [[dyadic]] [[primitive function]] which evaluates the right [[argument]] in terms of the radix system defined by the left argument. Some implementations add [[monadic]] usage to this function, where the left argument defaults to the [[scalar]] 2. Decode is the [[inverse]] of [[Encode]] <syntaxhighlight lang=apl inline>⊤</syntaxhighlight> with the same left argument X, when X is a [[vector]] and the right argument is a valid representation of a number in the radix system defined by X. | ||
== APL model == | == APL model == | ||
Line 10: | Line 10: | ||
⌽×\1,⌽1↓0 24 60 ⍝ The digit values of radix 0 24 60 | ⌽×\1,⌽1↓0 24 60 ⍝ The digit values of radix 0 24 60 | ||
1440 60 1 | 1440 60 1 | ||
</ | </syntaxhighlight> | ||
If the left argument is a [[scalar]] number, it is converted to a [[vector]] filled with that number. | If the left argument is a [[scalar]] number, it is converted to a [[vector]] filled with that number. | ||
Line 19: | Line 19: | ||
⌽×\1,⌽1↓3⍴10 | ⌽×\1,⌽1↓3⍴10 | ||
100 10 1 | 100 10 1 | ||
</ | </syntaxhighlight> | ||
The resulting model looks like this: | The resulting model looks like this: | ||
Line 27: | Line 27: | ||
(⌽×\1,⌽(1↓(≢⍵)⍴⊢)⍤1⊢⍺)+.×⍵ | (⌽×\1,⌽(1↓(≢⍵)⍴⊢)⍤1⊢⍺)+.×⍵ | ||
} | } | ||
</ | </syntaxhighlight> | ||
== Examples == | == Examples == | ||
Line 38: | Line 38: | ||
2⊥1 0 1 0 1 ⍝ Base 2 | 2⊥1 0 1 0 1 ⍝ Base 2 | ||
21 | 21 | ||
</ | </syntaxhighlight> | ||
But since the left and right argument's values are not restricted in any way, <syntaxhighlight lang=apl inline>X⊥Y</ | But since the left and right argument's values are not restricted in any way, <syntaxhighlight lang=apl inline>X⊥Y</syntaxhighlight> with scalar X can also be thought as evaluating the [[wikipedia:Polynomial|polynomial]] Y at given value X. For example, evaluating <math>y = 2x^4 + 3x + 1</math> at <math>x = -3</math> can be done as follows: | ||
<syntaxhighlight lang=apl> | <syntaxhighlight lang=apl> | ||
¯3⊥2 0 0 3 1 ⍝ Polynomial terms are written highest first, and absent terms are written as 0 | ¯3⊥2 0 0 3 1 ⍝ Polynomial terms are written highest first, and absent terms are written as 0 | ||
154 | 154 | ||
</ | </syntaxhighlight> | ||
Decode can be also used to convert from a measure given in a hierarchy of units to the smallest unit. For example, given that 1 day = 24 hours, 1 hour = 60 minutes, and 1 minute = 60 seconds, how many seconds is 2 days, 10 hours, 34 minutes, and 19 seconds? | Decode can be also used to convert from a measure given in a hierarchy of units to the smallest unit. For example, given that 1 day = 24 hours, 1 hour = 60 minutes, and 1 minute = 60 seconds, how many seconds is 2 days, 10 hours, 34 minutes, and 19 seconds? | ||
Line 52: | Line 52: | ||
0 24 60 60⊥2 10 34 19 | 0 24 60 60⊥2 10 34 19 | ||
210859 | 210859 | ||
</ | </syntaxhighlight> | ||
Decode can be also found in many [[idiom|idioms]]. Some of the simple and useful ones are reproduced here: | Decode can be also found in many [[idiom|idioms]]. Some of the simple and useful ones are reproduced here: | ||
Line 77: | Line 77: | ||
⊥⍨1 1 0 1 1 1 | ⊥⍨1 1 0 1 1 1 | ||
3 | 3 | ||
</ | </syntaxhighlight> | ||
Decode has an important property with array [[index]]: Given an arbitrary array A with [[shape]] S, the index of an [[element]] in A can be decoded with S to obtain the corresponding index in the [[ravel]] of A. Decoding all indices given by [[Index Generator]] shows the ravel order of the elements of an array with the given shape. Note that [[index origin]] 0 (<syntaxhighlight lang=apl inline>⎕IO←0</ | Decode has an important property with array [[index]]: Given an arbitrary array A with [[shape]] S, the index of an [[element]] in A can be decoded with S to obtain the corresponding index in the [[ravel]] of A. Decoding all indices given by [[Index Generator]] shows the ravel order of the elements of an array with the given shape. Note that [[index origin]] 0 (<syntaxhighlight lang=apl inline>⎕IO←0</syntaxhighlight>) is required for this to hold. | ||
<syntaxhighlight lang=apl> | <syntaxhighlight lang=apl> | ||
Line 94: | Line 94: | ||
16 17 18 19 | 16 17 18 19 | ||
20 21 22 23 | 20 21 22 23 | ||
</ | </syntaxhighlight> | ||
== External links == | == External links == | ||
Line 106: | Line 106: | ||
* [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Decode.htm Dyalog] | * [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Decode.htm Dyalog] | ||
* [http://microapl.com/apl_help/ch_020_020_660.htm APLX] | * [http://microapl.com/apl_help/ch_020_020_660.htm APLX] | ||
* J [https://www.jsoftware.com/help/dictionary/d401.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/numberdot#dyadic NuVoc] (as <syntaxhighlight lang=j inline>#.</ | * J [https://www.jsoftware.com/help/dictionary/d401.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/numberdot#dyadic NuVoc] (as <syntaxhighlight lang=j inline>#.</syntaxhighlight>) | ||
{{APL built-ins}}[[Category:Primitive functions]] | {{APL built-ins}}[[Category:Primitive functions]] |