Topic: `APLX Help` : `Help on APL language` : `APL Primitives` : `⊤ Encode`
[ Previous | Next | Contents | Index | APL Home ]

`⊤` Encode

Represents a value in a given number system, for example, represents inches as yards, feet and inches.

The left-hand argument gives the base, or bases of the number system you want to use, the right-hand argument is the value to be encoded. Both arguments must be simple numeric arrays.

To convert 75 inches to yards feet and inches:

```             1760 3 12 ⊤ 75          (There are 12 inches to a foot, 3 feet
2 0 3                          to the yard, 1760 yards to a mile)
```

Note: Since three numbers were required in the result (yards, feet and inches) three numbers were given in the left argument. If you don't put sufficient numbers in the left argument, you lose some of the result:

```             3 12 ⊤ 75
0 3
```

You can be sure of not losing any of the result by making the first element of the left argument a number greater than the number to be encoded.

```             100000 12⊤75
6 3
```

To express the base-10 number 100 in base-16 (hexadecimal)

```             16 16 16 16 ⊤100
0 0 6 4
```

In addition, the right argument does not have to be an integer, and indeed `⊤` can be a handy way to separate the fractional part of a number from the integer part.

```             1760 3 12⊤75.3
2 0 3.3
0 1⊤75.3                (The second element of the left argument
75 0.3                         being 1 ensures that all of the right
argument except the fractional part
appears in the first element of the result)
```

Although the encode function is defined for scalar right arguments, it is possible to use encode with any array as the right argument. In this case the encode operation is applied to each element of the right argument to produce a vector result for each element. Similarly, if the left argument of `⊤` is not a vector, but a higher dimensional array, then each base vector across the first axis of the left argument is applied to obtain the representation of each element of the right argument. The shape of the result is the same as the shape generated by an outer product operation, `(⍴LEFTARG),⍴RIGHTARG.`

To convert a series of values expressed as decimal numbers to their binary (base 2) equivalent.

```             2 2 2 2 2 ⊤ 1 2 3 4 5
0 0 0 0 0                     (The vector left argument is applied to
0 0 0 0 0                      each element of the right argument. The
0 0 0 1 1                      results are displayed along the first
0 1 1 0 0                      axis (rows) of the result)
1 0 1 0 1
```

Topic: `APLX Help` : `Help on APL language` : `APL Primitives` : `⊤ Encode`
[ Previous | Next | Contents | Index | APL Home ]