digs ← alph ##.adic numb                    ⍝ Bijective base-⍺ numeration.
numb ← alph ##.adic digs                    ⍝ and its inverse.

Bijective  base-⍺  numeration is a 1-1 mapping between strings in alphabet ⍺ and
the natural numbers 0 1 2 ...

The strings are mapped in "shortlex" order so that shorter strings map to small-
er numbers and same-length strings map in lexicographical order.  In particular,
natural number 0 maps to the null string.

Here are the mappings for the numbers 0..16 using alphabet 'AB':

        'AB'∘adic¨ 0 to 16
    ┌┬─┬─┬──┬──┬──┬──┬───┬───┬───┬───┬───┬───┬───┬───┬────┬────┐
    ││A│B│AA│AB│BA│BB│AAA│AAB│ABA│ABB│BAA│BAB│BBA│BBB│AAAA│AAAB│
    └┴─┴─┴──┴──┴──┴──┴───┴───┴───┴───┴───┴───┴───┴───┴────┴────┘

Notice the difference between 2-adic enumeration (with alphabet 0 1):

        0 1∘adic¨ 1 to 10               ⍝ 2-adic  1..10
    ┌─┬─┬───┬───┬───┬───┬─────┬─────┬─────┬─────┐
    │0│1│0 0│0 1│1 0│1 1│0 0 0│0 0 1│0 1 0│0 1 1│
    └─┴─┴───┴───┴───┴───┴─────┴─────┴─────┴─────┘

and regular binary:

        2 ⊥⍣¯1¨ 1 to 10                 ⍝ binary 1..10
    ┌─┬───┬───┬─────┬─────┬─────┬─────┬───────┬───────┬───────┐
    │1│1 0│1 1│1 0 0│1 0 1│1 1 0│1 1 1│1 0 0 0│1 0 0 1│1 0 1 0│
    └─┴───┴───┴─────┴─────┴─────┴─────┴───────┴───────┴───────┘

Ref: http://en.wikipedia.org/wiki/Bijective_numeration

With coding from Jay Foad,  function [adic] converts between natural numbers and
⍺-adic digit strings.  The function is a self-inverse:  given a  _scalar_  right
argument, it returns a vector of ⍺-digits;  and given a _vector_ right argument,
it returns the corresponding natural number:

        ⎕A adic 'DYALOG'                ⍝ 'A..Z'-adic → natural number.
    58975989

        phrase ← ,¨ 'I' 'AM' 'OK' '' 'HOW' 'R' 'U'

        phrase                          ⍝ vector of word vectors.
    ┌─┬──┬──┬┬───┬─┬─┐
    │I│AM│OK││HOW│R│U│
    └─┴──┴──┴┴───┴─┴─┘

        ⎕A∘adic¨ phrase                 ⍝ ⎕A-adic number of each word.
    9 39 401 0 5821 18 21

        'abc' adic 99                   ⍝ 'abc'-adic representation of number,
    cabc

        'abc' adic 'abc' adic 99        ⍝ and its inverse.
    99

        ⎕A (adic⍣2) 1234                ⍝ adic⍣2 is identity.
    1234

        ⎕A (adic⍣2) 'DYALOG'            ⍝   ..      ..
    DYALOG

Applications
------------
The function could be used  for a systematic exploration of words that are writ-
able in a given alphabet.  For example, given some error-trapping,  it could be
used to test arbitrary expressions:

    try←{                       ⍝ execute of ⍵ expressions using tokens from ⍺.
        join←{↑⍺{⍺,⍺⍺,⍵}/⍵}     ⍝ ⍺-join of vector ⍵.
        ' 'join''join ⍺∘adic{   ⍝ ⍺-adic translation from natural number.
            0::''               ⍝ error: null.
            expr←⍺⍺ ⍵           ⍝ expression to try.
            rslt←⍎expr          ⍝ attempted execution.
            ⊂expr               ⍝ success: good expression.
        }¨⍳⍵                    ⍝ ⍵ is number of expressions to try.
    }

    '+/'try 500
+ / +/ // +// /// +/// //// +//// ///// +///// ////// +////// /////// +///////

    '+/⍬'try 50
+ / ⍬ +/ +⍬ // ⍬⍬ ++⍬ +// +/⍬ +⍬⍬ /// //⍬ ⍬+⍬ ⍬/⍬ ⍬⍬⍬ +++⍬ ++/⍬ ++⍬⍬

    try'#.[⍬]'try 1e3
# ⍬ ## #⍬ ⍬# ⍬⍬ ### ##⍬ #.# #⍬# #⍬⍬ ⍬## ⍬#⍬ ⍬[] ⍬⍬# ⍬⍬⍬ #### ###⍬ ##.# ##⍬# ##⍬⍬
       #.## #.#⍬ #⍬## #⍬#⍬ #⍬[] #⍬⍬# #⍬⍬⍬ ⍬### ⍬##⍬ ⍬#.# ⍬#[] ⍬#⍬# ⍬#⍬⍬ ⍬[⍬] ⍬[]
      # ⍬[]⍬ ⍬⍬## ⍬⍬#⍬ ⍬⍬[] ⍬⍬⍬# ⍬⍬⍬⍬ ##### ####⍬ ###.# ###[] ###⍬# ###⍬⍬ ##.##
      ##.#⍬ ##⍬## ##⍬#⍬ ##⍬[] ##⍬⍬# ##⍬⍬⍬ #.### #.##⍬ #.#.# #.#⍬# #.#⍬⍬

    try'+/¨∘⍬'try 1e3
+ / ⍬ +/ +¨ +⍬ // /¨ ∘¨ ⍬¨ ⍬⍬ ++⍬ +// +/¨ +/⍬ +¨/ +¨¨ +¨⍬ +∘+ +∘/ +∘⍬ +⍬⍬ /// //
      ¨ //⍬ /¨/ /¨¨ /∘+ /∘/ /∘⍬ ∘¨/ ∘¨¨ ∘∘+ ∘∘/ ∘∘⍬ ⍬+⍬ ⍬/⍬ ⍬¨/ ⍬¨¨ ⍬∘+ ⍬∘/ ⍬∘⍬
      ⍬⍬¨ ⍬⍬⍬ +++⍬ ++/⍬ ++¨⍬ ++⍬⍬ +/+⍬ +/// +//¨ +/¨/ +/¨¨ +/¨⍬ +/∘+ +/∘/ +/∘⍬ +
      /⍬⍬ +¨+⍬ +¨// +¨/¨ +¨¨/ +¨¨¨ +¨¨⍬ +¨∘+ +¨∘/ +¨∘⍬ +¨⍬⍬ +∘+/ +∘+¨ +∘+⍬ +∘//
      +∘/¨ +∘∘¨ +∘⍬/ +∘⍬¨ +∘⍬⍬ +⍬+⍬ +⍬/⍬ +⍬⍬⍬ //+⍬ //// ///¨ //¨/ //¨¨ //¨⍬ //∘+
       //∘/ //∘⍬ //⍬⍬ /¨// /¨/¨ /¨¨/ /¨¨¨ /¨∘+ /¨∘/ /¨∘⍬ /∘+/ /∘+¨ /∘// /∘/¨ /∘∘
      ¨ /∘⍬/ /∘⍬¨ /∘⍬⍬ ∘¨// ∘¨/¨ ∘¨¨/ ∘¨¨¨ ∘¨∘+ ∘¨∘/ ∘¨∘⍬ ∘∘+/ ∘∘+¨ ∘∘// ∘∘/¨ ∘∘
      ∘¨ ∘∘⍬/ ∘∘⍬¨ ∘∘⍬⍬ ⍬++⍬ ⍬+¨⍬ ⍬/+⍬ ⍬/¨⍬ ⍬¨// ⍬¨/¨ ⍬¨¨/ ⍬¨¨¨ ⍬¨∘+ ⍬¨∘/ ⍬¨∘⍬ ⍬
      ∘+/ ⍬∘+¨ ⍬∘+⍬ ⍬∘// ⍬∘/¨ ⍬∘/⍬ ⍬∘∘¨ ⍬∘⍬/ ⍬∘⍬¨ ⍬∘⍬⍬ ⍬⍬¨/ ⍬⍬¨¨ ⍬⍬∘+ ⍬⍬∘/ ⍬⍬∘⍬
      ⍬⍬⍬¨ ⍬⍬⍬⍬ ++++⍬ +++/⍬ +++¨⍬ +++⍬⍬ ++/+⍬ ++/¨⍬ ++/⍬⍬ ++¨+⍬ ++¨¨⍬ ++¨⍬⍬ ++∘+
      ⍬ ++∘/⍬ ++⍬+⍬ ++⍬/⍬ ++⍬⍬⍬ +/++⍬ +/+/⍬ +/+¨⍬ +/+⍬⍬ +//// +///¨ +//¨/ +//¨¨
      +//¨⍬ +//∘+ +//∘/ +//∘⍬ +/¨+⍬ +/¨// +/¨/¨ +/¨¨/ +/¨¨¨ +/¨¨⍬ +/¨∘+ +/¨∘/ +/
      ¨∘⍬ +/¨⍬⍬ +/∘+/ +/∘+¨ +/∘+⍬ +/∘// +/∘/¨ +/∘∘¨

Notice that the last test found some errors in Dyalog's parser.  For example, ∘¨
and ∘∘+ should  generate syntax errors but seem not to.  In contrast, ⍬¨ and ⍬∘⍬
are syntactically correct derived functions, though they have yet to be assigned
a meaning in Dyalog. See the "muse" in →ncpath← for a suggestion for ⍬¨.

See also →parse←.

[adic] could also be used to stress-test a symbol table:

    (⎕ns'') ⍎¨ time ,∘'←0'¨ ⎕a∘adic¨ ⍳1e5       ⍝ create 100K symbols.
36.58

(muse:

    Function ⎕A∘adic defines 1-1 mapping between the natural numbers and the set
    of single words that can be written using the upper case alphabet.  Here are
    the "words" that correspond to: 1 10 100 1000 ...

        ⎕a∘adic¨ 10* 0 to 10
     A  J  CV  ALL  NTP  EQXD  BDWGN  UVXWJ  HJUNYV  CFDGSXL  AFIPYQJP

    Given enough time, this expression:

          {⍞←' ',⎕a adic ⍵ ⋄ ∇ ⍵+1} 1       ⍝ display all words.            [⍵]

    will display all possible words that can be written using upper case letters
    A-Z.

    This includes all possible names and some of these names will refer to meta-
    physical deities.

    It is generally understood  that  uttering  all of the names of the Almighty
    brings  the  universe  to an abrupt end.  The fate of the cosmos, therefore,
    hinges on whether displaying a name in the session constitutes an utterance.

    Computer-assisted devotion  certainly has a precedent in the form of digital
    prayer-wheels: https://en.wikipedia.org/wiki/Prayer_wheel

    More worrying, from a mathematical point of view, an expression is deemed to
    be _equivalent_  to its evaluation.  This  means  that the mere existence of
    expression [⍵] above, should be enough to trigger the apocalypse. Though the
    fact that (you believe) you are reading this must raise some doubts.
)

Examples:

    1 2∘adic¨ 0 to 15           ⍝ 2-adic numbers
┌┬─┬─┬───┬───┬───┬───┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬───────┐
││1│2│1 1│1 2│2 1│2 2│1 1 1│1 1 2│1 2 1│1 2 2│2 1 1│2 1 2│2 2 1│2 2 2│1 1 1 1│
└┴─┴─┴───┴───┴───┴───┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴───────┘

    'abc'∘adic¨0 to 23          ⍝ 3-adic numbers
┌┬─┬─┬─┬──┬──┬──┬──┬──┬──┬──┬──┬──┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
││a│b│c│aa│ab│ac│ba│bb│bc│ca│cb│cc│aaa│aab│aac│aba│abb│abc│aca│acb│acc│baa│bab│
└┴─┴─┴─┴──┴──┴──┴──┴──┴──┴──┴──┴──┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

    ⎕A∘adic¨⍳35                 ⍝ bijective base-26 numbers.
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬──┬──┬──┬──┬──┬──┬──┬──┐
│A│B│C│D│E│F│G│H│I│J│K│L│M│N│O│P│Q│R│S│T│U│V│W│X│Y│Z│AA│AB│AC│AD│AE│AF│AG│AH│AI│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──┴──┴──┴──┴──┴──┴──┴──┴──┘

    1 adic¨ 0 to 8              ⍝ 1-adic: unary numbers
┌┬─┬───┬─────┬───────┬─────────┬───────────┬─────────────┬───────────────┐
││1│1 1│1 1 1│1 1 1 1│1 1 1 1 1│1 1 1 1 1 1│1 1 1 1 1 1 1│1 1 1 1 1 1 1 1│
└┴─┴───┴─────┴───────┴─────────┴───────────┴─────────────┴───────────────┘

See also:ary nats words

Back to: contents

Back to: Workspaces