aboutsummaryrefslogtreecommitdiff
path: root/src/hyper/pages/ARRAY2.ht
blob: 8e53f226198130af18f063114cfc43de6eafc112 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\TwoDimensionalArrayXmpTitle}{TwoDimensionalArray}
\newcommand{\TwoDimensionalArrayXmpNumber}{9.82}
%
% =====================================================================
\begin{page}{TwoDimensionalArrayXmpPage}{9.82 TwoDimensionalArray}
% =====================================================================
\beginscroll

The \spadtype{TwoDimensionalArray} domain is used for storing data in a
%-% \HDindex{array!two-dimensional}{TwoDimensionalArrayXmpPage}{9.82}{TwoDimensionalArray}
\twodim{} data structure indexed by row and by column.
Such an array is a homogeneous data structure in that all the entries of
the array must belong to the same \Language{} domain (although see
\downlink{``\ugTypesAnyNoneTitle''}{ugTypesAnyNonePage} in Section \ugTypesAnyNoneNumber\ignore{ugTypesAnyNone}).
Each array has a fixed number of rows and columns specified by the user
and arrays are not extensible.
In \Language{}, the indexing of two-dimensional arrays is one-based.
This means that both the ``first'' row of an array and the ``first''
column of an array are given the index \spad{1}.
Thus, the entry in the upper left corner of an array is in position
\spad{(1,1)}.


The operation \spadfunFrom{new}{TwoDimensionalArray} creates
an array with a specified number of rows and columns and fills the components
of that array with a specified entry.
The arguments of this operation specify the number of rows, the number
of columns, and the entry.
\xtc{
This creates a five-by-four array of integers, all of whose entries are
zero.
}{
\spadpaste{arr : ARRAY2 INT := new(5,4,0) \bound{arr}}
}
The entries of this array can be set to other integers using
the operation \spadfunFrom{setelt}{TwoDimensionalArray}.

\xtc{
Issue this to set the element in the upper left corner of this array to
\spad{17}.
}{
\spadpaste{setelt(arr,1,1,17) \free{arr}\bound{arr1}}
}
\xtc{
Now the first element of the array is \spad{17.}
}{
\spadpaste{arr \free{arr1}}
}
\xtc{
Likewise, elements of an array are extracted using the operation
\spadfunFrom{elt}{TwoDimensionalArray}.
}{
\spadpaste{elt(arr,1,1) \free{arr1}}
}
\xtc{
Another way to use these two operations is as follows.
This sets the element in position \spad{(3,2)} of the array to \spad{15}.
}{
\spadpaste{arr(3,2) := 15 \free{arr1}\bound{arr2}}
}
\xtc{
This extracts the element in position \spad{(3,2)} of the array.
}{
\spadpaste{arr(3,2) \free{arr2}}
}
The operations \spadfunFrom{elt}{TwoDimensionalArray} and
\spadfunFrom{setelt}{TwoDimensionalArray} come equipped with an error
check which verifies that the indices are in the proper ranges.
For example, the above array has five rows and four columns, so if you ask
for the entry in position \spad{(6,2)} with \spad{arr(6,2)} \Language{}
displays an error message.
If there is no need for an error check, you can call the operations
\spadfunFrom{qelt}{TwoDimensionalArray} and
\spadfunFromX{qsetelt}{TwoDimensionalArray} which provide the same
functionality but without the error check.
Typically, these operations are called in well-tested programs.

\xtc{
The operations \spadfunFrom{row}{TwoDimensionalArray} and
\spadfunFrom{column}{TwoDimensionalArray} extract rows and columns,
respectively, and return objects of \spadtype{OneDimensionalArray} with
the same underlying element type.
}{
\spadpaste{row(arr,1) \free{arr2}}
}
\xtc{
}{
\spadpaste{column(arr,1) \free{arr2}}
}

\xtc{
You can determine the dimensions of an array by calling the
operations \spadfunFrom{nrows}{TwoDimensionalArray} and
\spadfunFrom{ncols}{TwoDimensionalArray},
which return the number of rows and columns, respectively.
}{
\spadpaste{nrows(arr) \free{arr2}}
}
\xtc{
}{
\spadpaste{ncols(arr) \free{arr2}}
}
\xtc{
To apply an operation to every element of an array, use
\spadfunFrom{map}{TwoDimensionalArray}.
This creates a new array.
This expression negates every element.
}{
\spadpaste{map(-,arr) \free{arr2}}
}
\xtc{
This creates an array where all the elements are doubled.
}{
\spadpaste{map((x +-> x + x),arr) \free{arr2}}
}
\xtc{
To change the array destructively, use
\spadfunFromX{map}{TwoDimensionalArray} instead of
\spadfunFrom{map}{TwoDimensionalArray}.
If you need to make a copy of any array, use
\spadfunFrom{copy}{TwoDimensionalArray}.
}{
\spadpaste{arrc := copy(arr) \bound{arrc}\free{arr2}}
}
\xtc{
}{
\spadpaste{map!(-,arrc) \free{arrc}}
}
\xtc{
}{
\spadpaste{arrc \free{arrc}}
}
\xtc{
}{
\spadpaste{arr  \free{arr2}}
}

\xtc{
Use \spadfunFrom{member?}{TwoDimensionalArray} to see if a given element
is in an array.
}{
\spadpaste{member?(17,arr) \free{arr2}}
}
\xtc{
}{
\spadpaste{member?(10317,arr) \free{arr2}}
}
\xtc{
To see how many times an element appears in an array, use
\spadfunFrom{count}{TwoDimensionalArray}.
}{
\spadpaste{count(17,arr) \free{arr2}}
}
\xtc{
}{
\spadpaste{count(0,arr) \free{arr2}}
}

For more information about the operations available for
\spadtype{TwoDimensionalArray}, issue \spadcmd{)show
TwoDimensionalArray}.
For information on related topics, see \downlink{`Matrix'}{MatrixXmpPage}\ignore{Matrix} and
\downlink{`OneDimensionalArray'}{OneDimensionalArrayXmpPage}\ignore{OneDimensionalArray}.
\endscroll
\autobuttons
\end{page}
%