⍝ Balanced Ternary Arithmetic:

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ monadic

    cmps←{                              ⍝ monadic comparisons.
        cmp←{                           ⍝ operand function comparison.
            rarg←⊤bt ⍵                  ⍝ BT argument.
            rslt←⍵⍵ bt rarg             ⍝ BT result.
            (3⊥rslt)≡⍺⍺ ⍵               ⍝ comparison with integer result.
        }
        ∧/⍺⍺ cmp ⍵⍵¨⍵                   ⍝ outer-product comparison.
    }

    +cmps+ ¯9 to 9                      ⍝ identity
1
    -cmps- ¯9 to 9                      ⍝ negative
1
    ×cmps× ¯9 to 9                      ⍝ signum
1
    |cmps| ¯9 to 9                      ⍝ absolute value
1
    ⊤bt +/3* 0 to 9                     ⍝ int to BT encode
1 1 1 1 1 1 1 1 1 1

    ⊤bt 1++/3* 0 to 9                   ⍝ int to BT encode
1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1

    ⊥bt¨,\10↑1                          ⍝ BT to int decodes
1 3 9 27 81 243 729 2187 6561 19683

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ dyadic

    cmps←{                              ⍝ dyadic comparisons.
        cmp←{                           ⍝ operand function comparison.
            larg rarg←⊤bt¨⍺ ⍵           ⍝ BT arguments.
            rslt←larg ⍵⍵ bt rarg        ⍝ BT result.
            (3⊥rslt)≡⍺ ⍺⍺ ⍵             ⍝ comparison with integer result.
        }
        ∧/,⍺∘.(⍺⍺ cmp ⍵⍵)⍵              ⍝ outer-product comparison.
    }

    (¯9 to 9) +cmps+ ¯9 to 9            ⍝ + sum
1
    (¯9 to 9) -cmps- ¯9 to 9            ⍝ - difference
1
    (¯9 to 9) ×cmps× ¯9 to 9            ⍝ × product
1
    (¯9 to 9) (⌊÷)cmps÷(¯9 to 9)~0      ⍝ ÷ integer-quotient
1
    (¯9 to 9) |cmps| ¯9 to 9            ⍝ | residue
1
    (¯9 to 9) *cmps* 0 to 9             ⍝ * power
1
    (¯9 to 9) ∨cmps∨ ¯9 to 9            ⍝ ∨ gcd
1
    (¯9 to 9) ∧cmps∧ ¯9 to 9            ⍝ ∧ lcm
1
    (¯9 to 9) ⌊cmps⌊ ¯9 to 9            ⍝ ⌊ min
1
    (¯9 to 9) ⌈cmps⌈ ¯9 to 9            ⍝ ⌈ max
1
    rel←{⍺ (⍎⍵)cmps(⍎⍵) ⍺}              ⍝ relational function ⍵

    ∧/(¯9 to 9)∘rel¨'<≤=≥>≠'            ⍝ test relational functions.
1

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ errors

    1÷bt 0                              ⍝ div-by-zero.
11::Divide by zero

    1*bt ¯1                             ⍝ negative power.
11::Negative power

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ LeRoy Eide's method.

    fast_div←{                      ⍝ Fast division by ⍵⍵ - LeRoy Eide.

        scan←{(enc-⍺-+/2↑⍵),1↓⍵}    ⍝ pair-wise deduction of digits.
        nlz←{(-1⌈+/∨\0≠⍵)↑⍵}        ⍝ without superfluous leading zeros.
        enc←⍺⍺                      ⍝ 2-digit encode.

        rslt←1↓↑scan/0,⍵,0          ⍝ remainder, quotient.
        rem←⍵⍵|⍵⍵-1↑rslt            ⍝ remainder.
        rem=0:(nlz rslt)rem         ⍝ exact divide: quotient and 0-remainder.

        norm←{(⍺ 0+enc ⍬⍴⍵),1↓⍵}    ⍝ pair-wise overflow resolution.
        rslt←1↓↑norm/0,rem+rslt     ⍝ vector-sum of rem with each digit.
        (nlz rslt)rem               ⍝ integer quotient and remainder.
    }

    enc_dec ← 0 10∘⊤                ⍝ 2-digit decimal encode.
    dec_div_9 ← enc_dec fast_div 9  ⍝ decimal divide-by-9.
                                    ⍝
    dec_div_9 1 2 3 4 5             ⍝ decimal 12345÷9 → 1371r6
┌───────┬─┐
│1 3 7 1│6│
└───────┴─┘

    enc_bt ← ¯1+ 0 3⊤ 3⊥ 1+ 0 3⊤ ⊢  ⍝ 2-digit balanced ternary encode.
    bt_div_2  ← enc_bt fast_div 2   ⍝ bt divide-by-2.
                                    ⍝                    __       _
    bt_div_2  1 0 ¯1 ¯1             ⍝ balanced ternary 1011÷2 → 111r1
┌──────┬─┐
│1 1 ¯1│1│
└──────┴─┘

    ∧/{(⌊⍵÷9)(9|⍵)≡10⊥¨dec_div_9 ⍎¨⍕⍵}¨0 to 100         ⍝ dec_div_by_9s.
1
    ∧/{(⌊⍵÷2)(2|⍵)≡⊥bt¨bt_div_2 ⊤bt ⍵}¨¯100 to 100      ⍝ bt_div_by_2.
1
    ternac ← 1 ¯1∘(∘.=)             ⍝ BT to binary

    ternac 1 0 ¯1 1 ¯1
1 0 0 1 0
0 0 1 0 1
    canret ← 1 ¯1∘(+.×)             ⍝ binary to BT

    canret ↑(1 0 0 1 0)(0 0 1 0 1)
1 0 ¯1 1 ¯1

⍝∇ bt to

Back to: code

Back to: Workspaces