Shape Reshape
Array basics
 Expermient with the expressions in the following block to gain an understanding of the functions used.
 Write a brief definition in English for each expression.
For example:{2×⍳⍵} ⍝ Even integers from 2 to 2×⍵ inclusive
.  Use the language bar to discover the names of unfamiliar functions.
3 4⍴⍳12 ⍝ A 3 row, 4 column matrix of the integers from 1 to 12 inclusive
⍴cards←'A23456789TJQK'∘.,'SCHD'
⍉cards
(,cards)[?×/⍴cards]
alph←2 3 4⍴⎕A
,alph
The shape of a scalar
Use the match function ⍺≡⍵
to determine which expressions below produce the empty character vector ''
("quotequote") and which produce the empty numeric vector ⍬
("zilde").
0⍴0
0↑⎕A
⍳0
0⍴''
0⍴'def'
0 0⍴'abc'
2 0 3⍴⍳6
alph[⍸alph='Z']
⌈⌿⍬⍬
⎕JSON'{}'
⎕JSON'[]'
⎕JSON'""'
A simple array contains only single numbers and/or characters as elements. Single numbers or characters are called scalars. Arrays generally can contain any other array. These are called nested arrays. For example cards
is a nested matrix of character vectors.
⍴¨1 2 3 4 ⍝ What is the shape of each number in ⍳4?
⍴¨'ABCDE' ⍝ What is the shape of each letter in 5↑⎕A?
⍴¨cards ⍝ What is the shape of each array in cards?
Problem set 5


Write a function
IsEmpty
to determine if an array has an empty axis.IsEmpty 2 0 3⍴⍳4
1
IsEmpty 2 2 3⍴⍳4
0
IsEmpty 0
0
IsEmpty 0⍴0
1

Any simple array can be described using two vectors. Which two primitive functions (that is, functions represented by symbols) return these vectors when given a simple array argument?

The rank of an array is the number of axes or dimensions an array has. If the shape of an array
⍵
is given by the vector result of the shape function⍴⍵
, give an expression for the rank of an array.
Answers
IsEmpty←{0∊⍴⍵}
 The functions shape
⍴⍵
and ravel,⍵
return these two vectors, which are called the shape and the ravel of elements.  Traditionally, the rank is defined as the shape of the shape
⍴⍴⍵
. However, in modern APL it is preferable to use the tally of the shape≢⍴⍵
.


Some Points in Space
The positions of 5 points in 3D space are given by the matrix
pos
:pos←5 3⍴0 1 3 4 2

Write a function
AddRows
to add a vector to a matrix.1 ¯3 4 AddRows pos
1 ¯2 7 5 ¯1 4 2 0 8 3 ¯3 5 4 1 6

Write a function
NormRows
to normalise each vector inpos
so that the sum of squares of each vector is1
.+/pos*2
10 20 26 5 29
+/(NormRows pos)*2
1 1 1 1 1
÷/2/pos
0.5 1 2 ¯2 ¯0.5
÷/2/NormRows pos ⍝ Relative proportions stay the same
0.5 1 2 ¯2 ¯0.5
Hint
The replicate
/
and reshape⍴
functions might be useful.
Bonus
Write commutative versions of
AddRows
which will still work when the vector and matrix arguments are swapped.Answers
 This solutions assumes a vector left argument:
AddRows ← {⍵+(⍴⍵)⍴⍺}
 Using only things which have been introduced so far:
NormRows ← {⍵÷(⍴⍵)⍴(⍴⍵)[2]/0.5*⍨+/⍵*2}
Bonus Answers
A commutative version of
AddRows
can be made by sorting using the ranks, and is expressed most clearly as two expressions separated by the statement separator⋄
It can be coded in a single expression as a reduction, but then the result must be disclosed using pickAddRows ← {(v m)←⍺ ⍵[(≢⍴)¨⍺ ⍵] ⋄ m+(⍴m)⍴v} ⍝ v is the vector, m is the matrix
⊃
, which we will cover later in the course. If you want to know why, look at the shape of the result with and without the pick⊃
In the next section, we will see a better way to write functions that work on arrays of differing rank.AddRows ← {⊃{⍵+(⍴⍵)⍴⍺}/⍺ ⍵[(≢⍴)¨⍺ ⍵]}


Without executing them, determine the shape of the results of the following expressions:
1 3 5∘.!2 4 6 8 1 2 3 + 4 5 6 {(+⌿⍵)÷≢⍵}3 1 4 1 5 +⌿2 3⍴⍳6 ?⌿2 3⍴3/4 52 ⌈/(2 3⍴⍳6)∘.×¯1+?5 4⍴0
Hint
Of course, you can always check your answers using the shape function
⍴
Answers
3 4 ≡ ⍴1 3 5∘.!2 4 6 8 3 = ⍴1 2 3 + 4 5 6 ⍬ ≡ ⍴{(+⌿⍵)÷≢⍵}3 1 4 1 5 ⍝ The empty numeric vector 3 = ⍴+⌿2 3⍴⍳6 3 = ⍴?⌿2 3⍴3/4 52 2 3 5 ≡ ⍴⌈/(2 3⍴⍳6)∘.×¯1+?5 4⍴0