[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Aug 30 18:30:14 1994 
Subject:Re: Arrays with null bounds (again) 
From:Steve Knight 
Volume-ID:940831.02 

Hi James,

> Some years ago there was a discussion about the behaviour
> of pop arrays that have the empty list as bounds. Such arrays
> contain an element.

True, they do.  All 0D arrays contain exactly one element.
This is a consequence of the relationship between shape and
size.
    size( A ) = product( shape( A ) )
or equivalently, but more elegantly,
    size = shape <> product

Aside:
    ;;; Pop11 doesn't define arrays in terms of shape
    ;;; but through the less natural mechanism of a
    ;;; boundslist.  For clarity, here's the definition
    ;;; of shape.
    ;;;
    define shape( A ); lvars A;
        lvars bounds = boundslist( A );
        [%
            until null( bounds ) do
                lvars a, b;
                bounds.dest.dest -> ( a, b, bounds );
                b - a + 1
            enduntil
        %]
    enddefine;

    ;;; And here's product.  More on this later.
    define product( L ); lvars L;
        applist( 1, L, nonop * )
    enddefine;


> As I recall, much of the discussion was about:
> 
>     a) Arrays by analogy with matrices.
>     b) Arrays by analogy with coordinate vectors.

Well, sort of.  But not by me.  The point about 0D arrays
is that they are the natural basis for recursion.  Consider,
for example, the array summation operator (which I am going to
define).  The summation operator takes an array A of dimension
N and reduces it to an array B of dimension N-1.  The 
two arrays are related by

    FORALL j, k, ...
    SIGMA i: A[ i, j, k, ...] = B[ j, k, ... ]

If we apply this operator to a 3D array we get a 2D array whose
elements are the sum of "columns" in the original.  If we apply
it to a matrix 2D array (matrix) we get a 1D array (vector) 
whose elements are the sum of columns of the original.  And
if we apply it to a 1D array (vector) we get a 0D array which
"contains" the sum of the elements of the array.

Indeed, to add up all the elements of an N dimensional array
we write a recursive procedure.

    define sum( A );
        until A.boundslist = [] do
            A.summation_operator -> A
        enduntil;
        A()
    enddefine;


> As I recall, I was persuaded that the behaviour of pop
> arrays is sensible because it supports some very general
> array re-shaping procedures. But I would like to understand
> this argument by analogy (a) with matrices.

Hmm.  But there is no basis for this analogy because matrices
are 2D arrays.  


> Suppose that a pop array with no bounds is analogous to a
> 0 x 0 matrix (whatever that might be). Then a 0 x 0 matrix
> contains an element.

This is wrong.  The number of elements in an array is the
product of the dimensions.  The product of the list [0 0] is
0.  Therefore a 0x0 matrix has no elements.  However the 
product of [] is naturally defined as 1.  Therefore every
0D array has one and only one element.  [By product I simply
mean multiplying all the elements of a list together.]

As an aside, one may attempt to challenge the proposition
that
    product( [] ) = 1
After all, you might argue, this is a pure definition.
However, there are many properties of product that are easily
defined if we take this to be true.  Without this, we end up
having to write lots of "except for []" side-conditions.  For
example, 
    A <> B = L 
implies
    product( A ) * product( B ) = product( L )
Unless we allow product( [] ) to be 1, we must make this 
theorem more complicated & write
    A <> B = L and A /= [] and B /= []
implies
    product( A ) * product( B ) = product( L )
Furthermore, there are no problems associated with defining
product this way.  Indeed, there are many problems with 
defining it any other way.  In short, it is natural and
"obvious".


> Now my question. Persuing this analogy, what does a 
> 0 x N matrix contain?

The product of [ 0 N ] is 0.  There are no elements in
such a matrix.  It is an empty array (well, an infinite 
family of empty arrays.)


Indeed, 0D arrays are the only dimension which has no
empty arrays.  There is only one empty 1D array.  In all
higher dimensions there are an infinite number of distinct
empty arrays -- all of which can be distinguished by 
their shape.

Steve