pnum ← {fmt←1} (##.phinary) nums        ⍝ Phinary representation of numbers ⍵.

        phinary 4       ⍝ A "phinary" number.
    101.01

Phi "The Golden Mean" has many interesting properties. A rectangle with sides of
ratio Phi:1 is considered to have a  particularly  pleasing shape and appears in
many  classical  works  of art.  Appending a square to this rectangle produces a
larger rectangle, whose sides are also in the ratio Phi:1.

            ┌──────Ø──────┐
          ┌─┌─────────────┐─┐
          │ │             │ │
          │ │             │ 1
          Ø │             │ │
          + ├─────────────┤─┤
          1 │             │ │       Golden Rectangles
          │ │             │ │
          │ │             │ Ø           Ø+1    Ø
          │ │             │ │           --- = ---
          │ │             │ │            Ø     1
          │ │             │ │
          └─└─────────────┘─┘
            └──────Ø──────┘

Therefore:

         (Ø*2) = Ø+1                ⍝ from above.               [1]

    →    0 = (Ø*2) + (-Ø) + ¯1      ⍝ by rearranging terms.

    →    0 = Ø⊥1 ¯1 ¯1              ⍝ from definition of ⊥.

Solving this quadratic equation in Ø, with coefficients 1 ¯1 ¯1, yields a posit-
ive root (see →roots←) of (1+(1-¯4)*0.5)÷2 or (0.5×1+5*0.5),  giving Phi a value
of:
    1.61803398874989484820458683436563811772030917980576286213544862270526046...

See: http://en.wikipedia.org/wiki/Golden_ratio

Using Phi as a number base
--------------------------
We are accustomed  to representing rational numbers using various integral bases
such as binary, octal, hexadecimal and, of course, regular decimal. See →ary←.

        (5/2) ⊤ 19                  ⍝ base-2 encode (binary).
    1 0 0 1 1

        (5/3) ⊤ 19                  ⍝ base-3 encode (ternary).
    0 0 2 0 1

Bases could also be non-integral rational numbers:

        (5/7÷3) ⊤ 19                ⍝ base-7÷3 encode.
    0 1 0.6666666667 1 0.3333333333

or even irrational numbers:

        (5/○1) ⊤ 19                 ⍝ base-Pi encode.
    0 0 1 2.858407346 0.1504440785

In particular Phi may be used as a number base:

        Ø ← 0.5×1+5*0.5             ⍝ Phi is "root five plus one over two".

        (5/Ø) ⊤ 19                  ⍝ base-Ø encode.
    1 1.381966011 1.145898034 1.291796068 1.201626124

Positional number systems tend to have a  "canonical" or "normal" form.  For ex-
ample, when using decimal notation,  we prefer  "forty-two" to  "thirty-twelve",
and 42.0 to 41.99...

A normal form for phinary numbers suggests that:

    [a] Only digits 1 and 0 be used.

    [b] There be no adjacent 1 digits.

Notice that, in phinary, 100 = 011:                             [2]

        100                     ⍝ [base Ø]
    →        Ø ⊥ 1 0 0          ⍝ defn of base
    →           (Ø*2) + 0 + 0   ⍝ defn of ⊤
    →              0  + Ø + 1   ⍝ from [1] above.
    →        Ø ⊥ 0 1 1          ⍝ defn of ⊤
    →   011                     ⍝ [base Ø]

Of course,  we can multiply both sides of an equation by any constant, including
a power of Phi, so:

    100=011 => 1000=0110 => 1.00=0.11 => 0.1=0.011 => ...

This gives us a rule for addition of phinary numbers in normal form:

        1 + 1
    →   1 + 1.00                ⍝ ⍵ = ⍵.00..
    →   1 + 0.11                ⍝ from [2] above
    →       1.11                ⍝ ..1.. + ..0.. → ..1..
    →       1.10 + 0.01         ⍝ ..1.. + ..0.. → ..1..
    →      10.00 + 0.01         ⍝ from [2]
    →      10.01                ⍝ ..1.. + ..0.. → ..1..

Arranged in a traditional tableau:

        1.00
        1.00 +
       -----
       10.01
       -----

Notice how addition overflow causes a "carry" to propogate both one place to the
left AND two places to the right!

From this, it is clear that successive natural numbers, generated by adding 1 to
the previous number, each has a finite number of phinary digits. In other words,
natural phinary numbers all have a finite representation, despite being the sums
of powers of an irrational base.

    0     0
    1     1
    2    10.01
    3   100.01
    4   101.01
    5  1000.1001
    6  1010.0001

See http://en.wikipedia.org/wiki/Phinary

Function [phinary] takes a numeric right argument and returns its "phinary" rep-
resentation, in normal form, as character vectors of '0' and '1' digits, togeth-
er with a phinary point where necessary. For example:

        phinary 42                      ⍝ phinary from decimal.
    10100010.00100001

If optional left argument [fmt] is passed as 0, the formatting is suppressed and
a vector of powers of Phi is returned:

        0 phinary 42                    ⍝ raw powers of phi.
    7 5 1 ¯3 ¯8

        Ø +.* 7 5 1 ¯3 ¯8               ⍝ reconstituted decimal number.
    42

[phinary] is a self-inverse (or "involution") in that:

        ⍵ ≡ (phinary⍣2) ⍵               ⍝ self-inverse: phinary⍣2 ←→ ⊢

        phinary'10100010.00100001'      ⍝ decimal from phinary.
    42

The round-trip, applied to non-normal phinary numbers, returns the normal form:

        phinary⍣2 ,'111.111'  '222'     ⍝ normal form of phinary numbers.
     1010.1  10101

The phinary representation of numbers with components  that are exclusively non-
negative powers of Phi, have no '1's to the right of the phinary point:

        phinary Ø +.*¨ (2 4)(3 5)       ⍝ powers of Phi => clean phinary.
     10100  101000

whereas, apart from 0 and 1, regular counting numbers, expressed in phinary, in-
clude components that are negative powers of Phi.  This  means that such numbers
always have '1's to the right of the phinary point:

        phinary 24 35                   ⍝ counting numbers => scruffy phinary.
     1001010.000101  10001010.00001001

(muse:

    This raises the question:  what constitutes a "whole"  or "natural"  phinary
    number? Is it:

        [a] those numbers with only zeros to the right of the point, or is it

        [b] those that are conversions of decimal whole numbers into phinary?

    The answer is that the concept of "natural number" is deeper than  its  rep-
    resentation in any particular base.  Such numbers  have  the  property  that
    they are closed under addition: adding any two produces a third  as  result.
    We can test which of the above sets of numbers is "natural",  geometrically,
    using  a  rule and compass (if we accept compass steps as a model for addit-
    ion).

    0         1    10       100       101  1000      1001  1010     10000
    │         │     │         │         │     │         │     │         │
    ├─────────┼─────┴───┬─────┴───┬─────┴───┬─┴───────┬─┴─────┴─┬───────┴─...
    │         │         │         │         │         │         │
    0         1        10.01    100.01    101.01   1000.1001 1010.0001

    ... so the evenly spaced members of the second set (0 1 10.01 100.01 ...)
    are the natural numbers, despite their untidy appearance.

    Perhaps, in this context, there is a distinction between whole- and natural-
    numbers.
)

Ref:
    http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/phigits.html
    http://www.goldennumber.net

Examples:

    phinary 1 2 3 4                 ⍝ some small phinary numbers.
 1  10.01  100.01  101.01

    align←{(-∘(⌈/)⍨⍵⍳¨'.')⌽↑⍵}      ⍝ align '.'s in char matrix.

    ,∘align∘phinary⍨ ¯4 to 10       ⍝ integers with their phinary equivalents.
¯4  ¯101.01
¯3  ¯100.01
¯2   ¯10.01
¯1    ¯1
 0     0
 1     1
 2    10.01
 3   100.01
 4   101.01
 5  1000.1001
 6  1010.0001
 7 10000.0001
 8 10001.0001
 9 10010.0101
10 10100.0101

    phinary 3÷17                    ⍝ rational number (non-terminating).
0.000100010100001010100101000001001000000100010100001010100101000001001

    phinary 2*÷2                    ⍝ irrational number (non-terminating).
1.010000010100101001000000010100000000010101010101010010000000101

    Ø ← +∘÷/40/1                    ⍝ Phi from continued fraction →cfract←.

    phinary Ø*¯3 to 3               ⍝ powers of Phi have simple phinary reps.
 0.001  0.01  0.1  1  10  100  1000

    Ø ≡ +∘(*∘0.5)/0,40/1            ⍝ Phi ≡ sqrt 1 + sqrt 1 + ... sqrt 1
1
    pcan ← phinary⍣2                ⍝ round-tripping produces canonical form.

    pcan '111.111'                  ⍝ canonical form.
1010.1

    {↑⍵(pcan ⍵)},\9/'1'             ⍝ non- and canonical forms.
 1  11   111   1111   11111   111111   1111111   11111111   111111111
 1  100  1001  10100  101001  1010100  10101001  101010100  1010101001

See also: ary ratrep ratsum bt mayan hex
See also: eval.dws/notes.bta eval.dws/notes.sba
See also: nats rats fibonacci
See also: numbers

Back to: contents

Back to: Workspaces