Skip to the content.

The Shape Function

Monadic Use On Scalars

When used monadically returns the shape of an object. If the object is a scalar the result is a blank line

⍴ 3

Monadic Use On Arrays

If the object has shape, if it is an array, r returns the shape:

⍴ 3 4 5
3

It works on all scalars

⍴ 1J2 3J4
2

Dyadic Use On Scalars

Dyadically is used to reshape an object. If necessary it will create an array of the appropriate shape and populate it with the scalar:

2 3 ⍴ 4
4 4 4
4 4 4

This extends to higher dimensions:

2 3 4 ⍴ 4
4 4 4 4
4 4 4 4
4 4 4 4

4 4 4 4
4 4 4 4
4 4 4 4

Dyadic Use On Arrays

Dyadically is used to reshape an object. It will stripe an existing vector over its space:

2 3 4 ⍴ 9 8 7
9 8 7 9
8 7 9 8
7 9 8 7

9 8 7 9
8 7 9 8
7 9 8 7

It also works on arrays of arrays:

A ← 2 ⍴ 9
B ← 2 ⍴ 99
2 3 ⍴ A B
┌─────┐ ┌─────┐ ┌─────┐
│  9 9│ │99 99│ │  9 9│
└─────┘ └─────┘ └─────┘
┌─────┐ ┌─────┐ ┌─────┐
│99 99│ │  9 9│ │99 99│
└─────┘ └─────┘ └─────┘

The left hand side must be an array of integers:

2.2 3 4 ⍴ 9 8 7
Error
2.2 3 4 ⍴ 9 8 7
^
DOMAIN ERROR [dyadic ⍴ only accepts integer arguments to the left and was called with: Left: [2.2,3,4] - Right: [9,8,7] ] on line 1 at character 1

The formatter likes to keep things neat, which is nice:

3 4⍴ 9999 8 7

with some cheeky alignments going on:

9999    8    7 9999
   8    7 9999    8
   7 9999    8    7

is a procrustean operator - if there aren’t enough elements on the right hand side it will generate them:

2 3 ⍴ 9 8 7 6

giving:

9 8 7
6 9 8

If there are too many it will chop some off:

1 2 ⍴ 4 4 4

giving:

4 4

Dyadic Use On Scalars

It can take a scalar as an argument on the left side:

1 ⍴ 2 3
2

It can take a scalar as an argument on the right side:

1 2 ⍴ 3
3 3

It can take a scalar as an argument on the both sides:

1 ⍴ 2
2

It always returns a vector.

If the left hand side is not an integer scalar an error will be thrown:

1.1 ⍴ 2 3
Error
1.1 ⍴ 2 3
^
DOMAIN ERROR [dyadic ⍴ only accepts integer arguments to the left and was called with: Left: [1.1] - Right: [2,3] ] on line 1 at character 1