Skip to content

Commit 253e7a1

Browse files
committed
Add support for a casting mode option
1 parent 7c5de5c commit 253e7a1

4 files changed

Lines changed: 100 additions & 34 deletions

File tree

lib/node_modules/@stdlib/ndarray/array/README.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,47 @@ arr = array( array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ) );
8080
The function accepts the following `options`:
8181

8282
- `buffer`: data source. If provided along with a `buffer` argument, the argument takes precedence.
83+
8384
- `dtype`: underlying storage [data type][@stdlib/ndarray/dtypes]. If not specified and a data source is provided, the data type is inferred from the provided data source. If an input data source is not of the same type, this option specifies the data type to which to cast the input data. For non-[`ndarray`][@stdlib/ndarray/ctor] generic array data sources, the function casts generic array data elements to the default data type. In order to prevent this cast, the `dtype` option **must** be explicitly set to `'generic'`. Any time a cast is required, the `copy` option is set to `true`, as memory must be copied from the data source to an output data buffer. Default: `'float64'`.
84-
- `order`: specifies the memory layout of the data source as either row-major (C-style) or column-major (Fortran-style). If set to `'any'`, if a data source is column-major and not row-major, the order of the returned array is column-major; otherwise, the order of the returned array is always row-major. If set to `'same'`, the order of the returned array matches the order of an input data source. If set to either `'row-major'` or `'column-major'`, the order of the returned array is set to the option value. Note that specifying an order which differs from the order of a provided data source does **not** entail a conversion from one memory layout to another. In short, this option is descriptive, not prescriptive. Default: `'row-major'`.
85-
- `shape`: array shape (dimensions). If a shape is not specified, the function attempts to infer a shape based on a provided data source. For example, if provided a nested array, the function resolves nested array dimensions. If provided a multidimensional array data source, the function uses the array's associated shape. For most use cases, such inference suffices. In the remaining use cases, specifying a shape is necessary. For example, provide a shape to create a multidimensional array view over a linear data buffer, ignoring any existing shape meta data associated with a provided data source.
85+
86+
- `order`: specifies the memory layout of the data source as either row-major (C-style) or column-major (Fortran-style). The option may be one of the following values:
87+
88+
- `row-major`: the order of the returned array is row-major.
89+
- `column-major`: the order of the returned array is column-major.
90+
- `any`: if a data source is column-major and not row-major, the order of the returned array is column-major; otherwise, the order of the returned array is row-major.
91+
- `same`: the order of the returned array matches the order of an input data source.
92+
93+
Note that specifying an order which differs from the order of a provided data source does **not** entail a conversion from one memory layout to another. In short, this option is descriptive, not prescriptive. Default: `'row-major'`.
94+
95+
- `shape`: array shape (dimensions). If a shape is not specified, the function attempts to infer a shape based on a provided data source. For example, if provided a nested array, the function resolves nested array dimensions. If provided a multidimensional array data source, the function uses the array's associated shape. For most use cases, such inference suffices. For the remaining use cases, specifying a shape is necessary. For example, provide a shape to create a multidimensional array view over a linear data buffer, ignoring any existing shape meta data associated with a provided data source.
96+
8697
- `flatten`: `boolean` indicating whether to automatically flatten generic array data sources. If an array shape is not specified, the shape is inferred from the dimensions of nested arrays prior to flattening. If a use case requires partial flattening, partially flatten **prior** to invoking this function and set the option value to `false` to prevent further flattening during invocation. Default: `true`.
98+
8799
- `copy`: `boolean` indicating whether to (shallow) copy source data to a new data buffer. The function does **not** perform a deep copy. To prevent undesired shared changes in state for generic arrays containing objects, perform a deep copy **prior** to invoking this function. Default: `false`.
100+
88101
- `ndmin`: specifies the minimum number of dimensions. If an array shape has fewer dimensions than required by `ndmin`, the function **prepends** singleton dimensions to the array shape in order to satisfy the dimensions requirement. Default: `0`.
102+
103+
- `casting`: specifies the casting rule used to determine acceptable casts. The option may be one of the following values:
104+
105+
- `none`: only allow casting between identical types.
106+
- `equiv`: allow casting between identical and byte swapped types.
107+
- `safe`: only allow "safe" casts.
108+
- `same-kind`: allow "safe" casts and casts within the same kind (e.g., between signed integers or between floats).
109+
- `unsafe`: allow casting between all types (including between integers and floats).
110+
111+
Default: `'safe'`.
112+
89113
- `codegen`: `boolean` indicating whether to use code generation. Default: `true`.
90-
- `mode`: specifies how to handle indices which exceed array dimensions. Default: `'throw'`.
91-
- `submode`: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
92114

93-
The function supports the following `modes`:
115+
- `mode`: specifies how to handle indices which exceed array dimensions.
116+
117+
- `throw`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should throw an error when an index exceeds array dimensions.
118+
- `wrap`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should wrap around an index exceeding array dimensions using modulo arithmetic.
119+
- `clamp`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should set an index exceeding array dimensions to either `0` (minimum index) or the maximum index.
120+
121+
Default: `'throw'`.
94122

95-
- `throw`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should throw an error when an index exceeds array dimensions.
96-
- `wrap`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should wrap around an index exceeding array dimensions using modulo arithmetic.
97-
- `clamp`: specifies that an [`ndarray`][@stdlib/ndarray/ctor] instance should set an index exceeding array dimensions to either `0` (minimum index) or the maximum index.
123+
- `submode`: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
98124

99125
By default, the function returns [`ndarray`][@stdlib/ndarray/ctor] instances which use code generation for fast element lookup. To disable code generation, set the `codegen` option to `false`.
100126

lib/node_modules/@stdlib/ndarray/array/docs/repl.txt

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@
2727

2828
options.order: string (optional)
2929
Specifies the memory layout of the data source as either row-major (C-
30-
style) or column-major (Fortran-style). If set to 'any', if a data
31-
source is column-major and not row-major, the order of the returned
32-
array is column-major; otherwise, the order of the returned array is
33-
always row-major. If set to 'same', the order of the returned array
34-
matches the order of an input data source. If set to either 'row-major'
35-
or 'column-major', the order of the returned array is set to the option
36-
value. Note that specifying an order which differs from the order of a
30+
style) or column-major (Fortran-style). The option may be one of the
31+
following values:
32+
33+
- 'row-major': the order of the returned array is row-major.
34+
- 'column-major': the order of the returned array is column-major.
35+
- 'any': if a data source is column-major and not row-major, the order
36+
of the returned array is column-major; otherwise, the order of the
37+
returned array is row-major.
38+
- 'same': the order of the returned array matches the order of an input
39+
data source.
40+
41+
Note that specifying an order which differs from the order of a
3742
provided data source does *not* entail a conversion from one memory
3843
layout to another. In short, this option is descriptive, not
3944
prescriptive. Default: 'row-major'.
@@ -44,7 +49,7 @@
4449
if provided a nested array, the function resolves nested array
4550
dimensions. If provided a multidimensional array data source, the
4651
function uses the array's associated shape. For most use cases, such
47-
inference suffices. In the remaining use cases, specifying a shape is
52+
inference suffices. For the remaining use cases, specifying a shape is
4853
necessary. For example, provide a shape to create a multidimensional
4954
array view over a linear data buffer, ignoring any existing shape meta
5055
data associated with a provided data source.
@@ -69,29 +74,53 @@
6974
dimensions to the array shape in order to satisfy the dimensions
7075
requirement. Default: 0.
7176

77+
options.casting: string ( optional)
78+
Specifies the casting rule used to determine acceptable casts. The
79+
option may be one of the following values:
80+
81+
- 'none': only allow casting between identical types.
82+
- 'equiv': allow casting between identical and byte swapped types.
83+
- 'safe': only allow "safe" casts.
84+
- 'same-kind': allow "safe" casts and casts within the same kind (e.g.,
85+
between signed integers or between floats).
86+
- 'unsafe': allow casting between all types (including between integers
87+
and floats).
88+
89+
Default: 'safe'.
90+
7291
options.codegen: boolean (optional)
7392
Boolean indicating whether to use code generation. Code generation can
7493
boost performance, but may be problematic in browser contexts enforcing
7594
a strict content security policy (CSP). Default: true.
7695

7796
options.mode: string (optional)
78-
Specifies how to handle indices which exceed array dimensions. If equal
79-
to 'throw', an ndarray instance throws an error when an index exceeds
80-
array dimensions. If equal to 'wrap', an ndarray instance wraps around
81-
indices exceeding array dimensions using modulo arithmetic. If equal to
82-
'clamp', an ndarray instance sets an index exceeding array dimensions to
83-
either `0` (minimum index) or the maximum index. Default: 'throw'.
97+
Specifies how to handle indices which exceed array dimensions. The
98+
option may be one of the following values:
99+
100+
- 'throw': an ndarray instance throws an error when an index exceeds
101+
array dimensions.
102+
- 'wrap': an ndarray instance wraps around indices exceeding array
103+
dimensions using modulo arithmetic.
104+
- 'clamp', an ndarray instance sets an index exceeding array dimensions
105+
to either `0` (minimum index) or the maximum index.
106+
107+
Default: 'throw'.
84108

85109
options.submode: Array<string> (optional)
86110
Specifies how to handle subscripts which exceed array dimensions. If a
87-
mode for a corresponding dimension is equal to 'throw', an ndarray
88-
instance throws an error when a subscript exceeds array dimensions. If
89-
equal to 'wrap', an ndarray instance wraps around subscripts exceeding
90-
array dimensions using modulo arithmetic. If equal to 'clamp', an
91-
ndarray instance sets a subscript exceeding array dimensions to either
92-
`0` (minimum index) or the maximum index. If the number of modes is
93-
fewer than the number of dimensions, the function recycles modes using
94-
modulo arithmetic. Default: [ options.mode ].
111+
mode for a corresponding dimension is equal to
112+
113+
- 'throw': an ndarray instance throws an error when a subscript exceeds
114+
array dimensions.
115+
- 'wrap': an ndarray instance wraps around subscripts exceeding array
116+
dimensions using modulo arithmetic.
117+
- 'clamp': an ndarray instance sets a subscript exceeding array
118+
dimensions to either `0` (minimum index) or the maximum index.
119+
120+
If the number of modes is fewer than the number of dimensions, the
121+
function recycles modes using modulo arithmetic.
122+
123+
Default: [ options.mode ].
95124

96125
Returns
97126
-------
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
2-
"dtype": "float64",
3-
"order": "row-major",
2+
"casting": "safe",
43
"codegen": true,
5-
"mode": "throw",
64
"copy": false,
5+
"dtype": "float64",
76
"flatten": true,
8-
"ndmin": 0
7+
"mode": "throw",
8+
"ndmin": 0,
9+
"order": "row-major"
910
}

lib/node_modules/@stdlib/ndarray/array/lib/main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var ctor = require( '@stdlib/ndarray/ctor' );
3434
var mctor = require( '@stdlib/ndarray/memoized-ctor' );
3535
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
3636
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
37+
var isCastingMode = require( '@stdlib/ndarray/base/assert/is-casting-mode' );
3738
var createBuffer = require( '@stdlib/ndarray/base/buffer' );
3839
var getType = require( '@stdlib/ndarray/base/buffer-dtype' );
3940
var arrayShape = require( '@stdlib/array/shape' );
@@ -63,6 +64,7 @@ var expandStrides = require( './expand_strides.js' );
6364
* @param {boolean} [options.copy=false] - boolean indicating whether to copy source data to a new data buffer
6465
* @param {boolean} [options.flatten=true] - boolean indicating whether to automatically flatten generic array data sources
6566
* @param {NonNegativeInteger} [options.ndmin=0] - minimum number of dimensions
67+
* @param {string} [options.casting="safe"] - casting rule used to determine what constitutes an acceptable cast
6668
* @throws {TypeError} options argument must be an object
6769
* @throws {TypeError} must provide valid options
6870
* @throws {Error} must provide either an array shape, data source, or both
@@ -243,6 +245,14 @@ function array() {
243245
} else {
244246
opts.copy = defaults.copy;
245247
}
248+
if ( hasOwnProp( options, 'casting' ) ) {
249+
opts.casting = options.casting;
250+
if ( !isCastingMode( opts.casting ) ) {
251+
throw new TypeError( 'invalid option. `casting` option must be a recognized casting mode. Option: `' + opts.casting + '`.' );
252+
}
253+
} else {
254+
opts.casting = defaults.casting;
255+
}
246256
// If not provided a shape, infer from a provided data source...
247257
if ( hasOwnProp( options, 'shape' ) ) {
248258
shape = options.shape;

0 commit comments

Comments
 (0)