Array Basics

Expermient with the expressions in the following block to gain an understanding of the functions used. Then, write a brief definition in English for each expression.

For example: {2×⍳⍵} ⍝ Even integers from 2 to 2×⍵ inclusive.

The first two have been given for you.

      rain←?7 5 12⍴250                   ⍝ rain is the monthly rainfall in millimeters over 7 years for 5 countries
      3 4⍴⍳12                            ⍝ A 3 row, 4 column matrix of the integers from 1 to 12 inclusive      
      ⍴cards←'A123456789TJQK'∘.,'♠♥♦♣'
      ⍉cards
      (,cards)[?52]
      alph←2 3 4⍴⎕A
      ,alph      
    1. Use the match function to determine which expressions below produce the empty character vector '' and the empty numeric vector .
             0⍴0
             0↑⎕A 
             ⍳0
             0⍴''
             0⍴'def'
             0 0⍴'abc'
             2 0 3⍴⍳6
             rain[⍸rain>250]
             alph[⍸alph='Z']
             ⌈⌿⍬⍬
             ⎕JSON'{}'
             ⎕JSON'[]'
             ⎕JSON'""'
      
    2. 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
      
  1. What is the shape of a scalar?

    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?
    
  2. 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?

  3. The rank of an array is the number of axes or dimensions an array has. If the shape of an array X is given by the vector result of the shape function ⍴X, give an expression for the rank of an array.

Some Points in Space

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

  1. 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
    
  2. Write a function NormRows to normalise each vector in pos so that the sum of squares of each vector is 1.

           +/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    
    
The replicate / and reshape functions might be useful.

Summary Statistics

Below each expression below, write a brief statement of what it does. If necessary, consult the hint which follows the group of expressions.

      (+⌿⍤1)rain   ⍝ Total rainfall for each of 7 years in 5 countries
      +⌿rain          
      (+⌿⍤2)rain
      (+⌿⍤3)rain
      ⌈⌿rain
      (⌈⌿⍤2)rain
      rain[⍸rain>250]
Look at the shapes of the arguments and the results, ⍴rain and ⍴+⌿rain etc.
  1. Write an expression to find the average monthly rainfall for each of the 7 years in each of the 5 countries.

  2. Write an expression to find the average annual rainfall over the 7 years for each of the 5 countries.

  3. Assign scalar numeric values (single numbers) to the variables years countries months such that the rain data can be summarised as follows.

           ⍴(+⌿⍤years)rain       ⍝ Sum over years
     5 12
           ⍴(+⌿⍤countries)rain   ⍝ Sum over countries
     7 12
           ⍴(+⌿⍤months)rain      ⍝ Sum over months
     7 5
    

The (jot-diaresis) symbol in this context is called rank. We will learn why in the next section.

Bonus

For your interest, here are some reductions of note. Try to ask yourself why they give the results they do. Could they have been given different definitions?

      +/⍬
      ×/⍬
      ⌊/⍬
      ,/'APPLE' 'DOG' 'BISCUIT'