Module pl.array2d
Operations on two-dimensional arrays.
Functions
column (a, key) | extract a column from the 2D array. |
map (f, a, arg) | map a function over a 2D array |
reduce_rows (f, a) | reduce the rows using a function. |
reduce_cols (f, a) | reduce the columns using a function. |
reduce2 (opc, opr, a) | reduce a 2D array into a scalar, using two operations. |
map2 (f, ad, bd, a, b, arg) | map a function over two arrays. |
product (f, t1, t2) | cartesian product of two 1d arrays. |
swap_rows (t, i1, i2) | swap two rows of an array. |
swap_cols (t, i1, i2, j1, j2) | swap two columns of an array. |
extract_rows (a, ridx, t) | extract the specified rows. |
extract_cols (a, cidx, t) | extract the specified columns. |
remove_row (t, i) | remove a row from an array. |
remove_col (t, j) | remove a column from an array. |
parse_range (s) | parse a spreadsheet range. |
range (t, rstr) | get a slice of a 2D array using spreadsheet range notation. |
slice (t, i1, j1, i2, j1, j2) | get a slice of a 2D array. |
set (t, value, i1, j1, i2, j1, j2) | set a specified range of an array to a value. |
write (t, f, fmt, i1, j1, i2, j1, j2) | write a 2D array to a file. |
forall (a, row_op, end_row_op, i1, j1, i2, j1, t, j2) | perform an operation for all values in a 2D array. |
iter (a, indices, i1, j1, i2, j1, j2) | iterate over all elements in a 2D array, with optional indices. |
Functions
- column (a, key)
-
extract a column from the 2D array.
Parameters:
a
: 2d arraykey
: an index or key
Returns:
-
1d array
- map (f, a, arg)
-
map a function over a 2D array
Parameters:
f
: a function of at least one argumenta
: 2d arrayarg
: an optional extra argument to be passed to the function.
Returns:
-
2d array
- reduce_rows (f, a)
-
reduce the rows using a function.
Parameters:
f
: a binary functiona
: 2d array
Returns:
-
1d array
see also:
- reduce_cols (f, a)
-
reduce the columns using a function.
Parameters:
f
: a binary functiona
: 2d array
Returns:
-
1d array
see also:
- reduce2 (opc, opr, a)
-
reduce a 2D array into a scalar, using two operations.
Parameters:
opc
: operation to reduce the final resultopr
: operation to reduce the rowsa
: 2D array
- map2 (f, ad, bd, a, b, arg)
-
map a function over two arrays.
They can be both or either 2D arrays
Parameters:
f
: function of at least two argumentsad
: order of first arraybd
: order of second arraya
: 1d or 2d arrayb
: 1d or 2d arrayarg
: optional extra argument to pass to function
Returns:
-
2D array, unless both arrays are 1D
- product (f, t1, t2)
-
cartesian product of two 1d arrays.
Parameters:
f
: a function of 2 argumentst1
: a 1d tablet2
: a 1d table
Returns:
-
2d table
- swap_rows (t, i1, i2)
-
swap two rows of an array.
Parameters:
t
: a 2d arrayi1
: a row indexi2
: a row index
- swap_cols (t, i1, i2, j1, j2)
-
swap two columns of an array.
Parameters:
t
: a 2d arrayi1
: a column indexi2
: a column indexj1
:j2
:
- extract_rows (a, ridx, t)
-
extract the specified rows.
Parameters:
a
: 2d arrayridx
: a table of row indicest
:
- extract_cols (a, cidx, t)
-
extract the specified columns.
Parameters:
a
: 2d arraycidx
: a table of column indicest
:
- remove_row (t, i)
-
remove a row from an array.
Parameters:
t
: a 2d arrayi
: a row index
- remove_col (t, j)
-
remove a column from an array.
Parameters:
t
: a 2d arrayj
: a column index
- parse_range (s)
-
parse a spreadsheet range.
The range can be specified either as 'A1:B2' or 'R1C1:R2C2';
a special case is a single element (e.g 'A1' or 'R1C1')
Parameters:
s
: a range.
Returns:
- start col
- start row
- end col
- end row
- range (t, rstr)
-
get a slice of a 2D array using spreadsheet range notation.
@see parse_range
Parameters:
t
: a 2D arrayrstr
: range expression
Returns:
-
a slice
see also:
- slice (t, i1, j1, i2, j1, j2)
-
get a slice of a 2D array.
Note that if the specified range has
a 1D result, the rank of the result will be 1.
Parameters:
t
: a 2D arrayi1
: start row (default 1)j1
: end col (default M)i2
: end row (default N)j1
: end col (default M)j2
:
Returns:
-
an array, 2D in general but 1D in special cases.
- set (t, value, i1, j1, i2, j1, j2)
-
set a specified range of an array to a value.
Parameters:
t
: a 2D arrayvalue
: the valuei1
: start row (default 1)j1
: end col (default M)i2
: end row (default N)j1
: end col (default M)j2
:
- write (t, f, fmt, i1, j1, i2, j1, j2)
-
write a 2D array to a file.
Parameters:
t
: a 2D arrayf
: a file object (default stdout)fmt
: a format string (default is just to use tostring)i1
: start row (default 1)j1
: end col (default M)i2
: end row (default N)j1
: end col (default M)j2
:
- forall (a, row_op, end_row_op, i1, j1, i2, j1, t, j2)
-
perform an operation for all values in a 2D array.
Parameters:
a
: 2D arrayrow_op
: function to call on each valueend_row_op
: function to call at end of each rowi1
: start row (default 1)j1
: end col (default M)i2
: end row (default N)j1
: end col (default M)t
:j2
:
- iter (a, indices, i1, j1, i2, j1, j2)
-
iterate over all elements in a 2D array, with optional indices.
Parameters:
a
: 2D arrayindices
: with indices (default false)i1
: start row (default 1)j1
: end col (default M)i2
: end row (default N)j1
: end col (default M)j2
:
Returns:
-
either value or i,j,value depending on indices