Encode

From APL Wiki
Revision as of 22:21, 10 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "<source" to "<syntaxhighlight")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 with the same left argument X, when X is a vector.

Concept

Encode works by finding the "digits" one by one from the rightmost digit. Let's consider 0 7 24 60⊤12345 (convert 12345 minutes to weeks, days, hours, and minutes) as an example.

      60|12345      ⍝ Minute's digit; 45 minutes
45
      60÷⍨12345-45  ⍝ 205 hours remaining to convert
205
      24|205        ⍝ Hour's digit; 13 hours
13
      24÷⍨205-13    ⍝ 8 days remaining to convert
8
      7|8           ⍝ Day's digit; 1 day
1
      7÷⍨8-1        ⍝ 1 week remaining to convert
1                   ⍝ No more conversion needed, since there is no limit for the highest digit

Collecting all the digits gives the desired result.

      0 7 24 60⊤12345
1 1 13 45

If the left argument has high rank, the vectors over the first axis act as independent radix systems.

      ⎕←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
2 10 16
2 10 16
2 10 16
2 10 16
2 10 16
2 10 16
      mat⊤121  ⍝ 121 is 1111001 in binary, 121 in decimal, 79 in hexadecimal
0 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
0 2 7
1 1 9

Examples

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.

      10⊤12345  ⍝ This does not work
5
      (10⍴10)⊤12345  ⍝ Convert to ten decimal digits, giving leading zeros if too small
0 0 0 0 0 1 2 3 4 5
      ((1+⌊10⍟12345)⍴10)⊤12345  ⍝ Convert to decimal digits using just as many digits as needed
1 2 3 4 5

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?

      0 24 60 60⊤210859
2 10 34 19

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.

      ⌊7÷3  ⍝ Quotient
2
      3|7  ⍝ Remainder
1
      0 3⊤7  ⍝ Quotient and remainder at once
2 1
      0 1⊤○1  ⍝ Integer and fractional parts of pi
3 0.1415926536

Encode has an important property with array index: Given an arbitrary array A with shape S, encoding a 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 (⎕IO←0) is required for this to hold.

      ⎕IO←0
      ⎕A[17]=(2 3 4⊤17)⌷2 3 4⍴⎕A
1
      2 3 4⊤⍳×/2 3 4  ⍝ Generating all indices of a 2×3×4 array as columns
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
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

External links

Lesson

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector