Skip to content

Commit 848f226

Browse files
committed
feat: add factory method
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 70d6643 commit 848f226

File tree

12 files changed

+1834
-491
lines changed

12 files changed

+1834
-491
lines changed

lib/node_modules/@stdlib/ndarray/vector/ctor/README.md

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,41 @@ var dt6 = getDType( arr6 );
186186
// returns 'int16'
187187
```
188188

189+
#### vector.factory( dtype\[, options] )
190+
191+
Returns a function for creating a one-dimensional [ndarray][@stdlib/ndarray/ctor].
192+
193+
```javascript
194+
var getDType = require( '@stdlib/ndarray/dtype' );
195+
var numel = require( '@stdlib/ndarray/numel' );
196+
197+
var Float32Vector = vector.factory( 'float32' );
198+
199+
var arr = new Float32Vector( [ 1, 2, 3 ] );
200+
// returns <ndarray>
201+
202+
var dt = getDType( arr );
203+
// returns 'float32'
204+
205+
var len = numel( arr );
206+
// returns 3
207+
```
208+
209+
The function supports the following parameters:
210+
211+
- **dtype**: [data type][@stdlib/ndarray/dtypes].
212+
- **options**: function options (_optional_).
213+
214+
The function accepts the following options:
215+
216+
- **order**: specifies whether the default memory layout for a returned [ndarray][@stdlib/ndarray/ctor] should be `'row-major'` (C-style) or `'column-major'` (Fortran-style). Default: `'row-major'`.
217+
- **mode**: specifies the default behavior when handling indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`.
218+
- **readonly**: boolean indicating whether to return a **read-only** [ndarray][@stdlib/ndarray/ctor] by default. Default: `false`.
219+
220+
The function returned by the `factory` method supports the same arguments and options as `vector` above, except for the `dtype` argument, as the returned function always returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes].
221+
222+
When providing options to the returned function, the provided option values override the defaults established during function creation.
223+
189224
</section>
190225

191226
<!-- /.usage -->
@@ -211,7 +246,7 @@ var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
211246
var cartesianProduct = require( '@stdlib/array/cartesian-product' );
212247
var unzip = require( '@stdlib/utils/unzip' );
213248
var dtypes = require( '@stdlib/ndarray/dtypes' );
214-
var getShape = require( '@stdlib/ndarray/shape' );
249+
var sum = require( '@stdlib/blas/ext/sum' );
215250
var logEachMap = require( '@stdlib/console/log-each-map' );
216251
var vector = require( '@stdlib/ndarray/vector/ctor' );
217252

@@ -220,23 +255,23 @@ var lens = discreteUniform( 10, 5, 15, {
220255
'dtype': 'int32'
221256
});
222257

223-
// Resolve a list of supported ndarray date types:
224-
var dts = dtypes();
258+
// Resolve a list of supported ndarray real-valued data types:
259+
var dts = dtypes( 'real_and_generic' );
225260

226261
// Create length-dtype pairs:
227262
var pairs = cartesianProduct( lens, dts );
228263

229264
// Split the pairs into individual arguments:
230265
var args = unzip( pairs );
231266

232-
// Define a callback to create a vector and return the vector shape:
267+
// Define a callback to create a random vector and return the sum of all vector elements:
233268
function clbk( len, dtype ) {
234-
var x = vector( len, dtype );
235-
return getShape( x );
269+
var x = vector( discreteUniform( len, 0, 100 ), dtype );
270+
return sum( x ).get();
236271
}
237272

238273
// Apply the callback and print the results:
239-
logEachMap( 'len: %2d. dtype: %10s. shape: [%d].', args[ 0 ], args[ 1 ], clbk );
274+
logEachMap( 'len: %2d. dtype: %7s. sum: %d.', args[ 0 ], args[ 1 ], clbk );
240275
```
241276

242277
</section>
Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isFunction = require( '@stdlib/assert/is-function' );
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var dtypes = require( '@stdlib/ndarray/dtypes' );
27+
var pkg = require( './../package.json' ).name;
28+
var factory = require( './../lib' ).factory;
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+':factory', function benchmark( b ) {
34+
var values;
35+
var v;
36+
var i;
37+
38+
values = dtypes();
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
v = factory( values[ i%values.length ] );
43+
if ( typeof v !== 'function' ) {
44+
b.fail( 'should return a function' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isFunction( v ) ) {
49+
b.fail( 'should return a function' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+':factory:dtype=float64', function benchmark( b ) {
56+
var vector;
57+
var arr;
58+
var i;
59+
60+
vector = factory( 'float64' );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
arr = vector( 0 );
65+
if ( arr.length !== 0 ) {
66+
b.fail( 'should have length 0' );
67+
}
68+
}
69+
b.toc();
70+
if ( !isndarrayLike( arr ) ) {
71+
b.fail( 'should return an ndarray' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
76+
77+
bench( pkg+':factory:dtype=float32', function benchmark( b ) {
78+
var vector;
79+
var arr;
80+
var i;
81+
82+
vector = factory( 'float32' );
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
arr = vector( 0 );
87+
if ( arr.length !== 0 ) {
88+
b.fail( 'should have length 0' );
89+
}
90+
}
91+
b.toc();
92+
if ( !isndarrayLike( arr ) ) {
93+
b.fail( 'should return an ndarray' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
});
98+
99+
bench( pkg+':factory:dtype=complex128', function benchmark( b ) {
100+
var vector;
101+
var arr;
102+
var i;
103+
104+
vector = factory( 'complex128' );
105+
106+
b.tic();
107+
for ( i = 0; i < b.iterations; i++ ) {
108+
arr = vector( 0 );
109+
if ( arr.length !== 0 ) {
110+
b.fail( 'should have length 0' );
111+
}
112+
}
113+
b.toc();
114+
if ( !isndarrayLike( arr ) ) {
115+
b.fail( 'should return an ndarray' );
116+
}
117+
b.pass( 'benchmark finished' );
118+
b.end();
119+
});
120+
121+
bench( pkg+':factory:dtype=complex64', function benchmark( b ) {
122+
var vector;
123+
var arr;
124+
var i;
125+
126+
vector = factory( 'complex64' );
127+
128+
b.tic();
129+
for ( i = 0; i < b.iterations; i++ ) {
130+
arr = vector( 0 );
131+
if ( arr.length !== 0 ) {
132+
b.fail( 'should have length 0' );
133+
}
134+
}
135+
b.toc();
136+
if ( !isndarrayLike( arr ) ) {
137+
b.fail( 'should return an ndarray' );
138+
}
139+
b.pass( 'benchmark finished' );
140+
b.end();
141+
});
142+
143+
bench( pkg+':factory:dtype=int32', function benchmark( b ) {
144+
var vector;
145+
var arr;
146+
var i;
147+
148+
vector = factory( 'int32' );
149+
150+
b.tic();
151+
for ( i = 0; i < b.iterations; i++ ) {
152+
arr = vector( 0 );
153+
if ( arr.length !== 0 ) {
154+
b.fail( 'should have length 0' );
155+
}
156+
}
157+
b.toc();
158+
if ( !isndarrayLike( arr ) ) {
159+
b.fail( 'should return an ndarray' );
160+
}
161+
b.pass( 'benchmark finished' );
162+
b.end();
163+
});
164+
165+
bench( pkg+':factory:dtype=uint32', function benchmark( b ) {
166+
var vector;
167+
var arr;
168+
var i;
169+
170+
vector = factory( 'uint32' );
171+
172+
b.tic();
173+
for ( i = 0; i < b.iterations; i++ ) {
174+
arr = vector( 0 );
175+
if ( arr.length !== 0 ) {
176+
b.fail( 'should have length 0' );
177+
}
178+
}
179+
b.toc();
180+
if ( !isndarrayLike( arr ) ) {
181+
b.fail( 'should return an ndarray' );
182+
}
183+
b.pass( 'benchmark finished' );
184+
b.end();
185+
});
186+
187+
bench( pkg+':factory:dtype=int16', function benchmark( b ) {
188+
var vector;
189+
var arr;
190+
var i;
191+
192+
vector = factory( 'int16' );
193+
194+
b.tic();
195+
for ( i = 0; i < b.iterations; i++ ) {
196+
arr = vector( 0 );
197+
if ( arr.length !== 0 ) {
198+
b.fail( 'should have length 0' );
199+
}
200+
}
201+
b.toc();
202+
if ( !isndarrayLike( arr ) ) {
203+
b.fail( 'should return an ndarray' );
204+
}
205+
b.pass( 'benchmark finished' );
206+
b.end();
207+
});
208+
209+
bench( pkg+':factory:dtype=uint16', function benchmark( b ) {
210+
var vector;
211+
var arr;
212+
var i;
213+
214+
vector = factory( 'uint16' );
215+
216+
b.tic();
217+
for ( i = 0; i < b.iterations; i++ ) {
218+
arr = vector( 0 );
219+
if ( arr.length !== 0 ) {
220+
b.fail( 'should have length 0' );
221+
}
222+
}
223+
b.toc();
224+
if ( !isndarrayLike( arr ) ) {
225+
b.fail( 'should return an ndarray' );
226+
}
227+
b.pass( 'benchmark finished' );
228+
b.end();
229+
});
230+
231+
bench( pkg+':factory:dtype=int8', function benchmark( b ) {
232+
var vector;
233+
var arr;
234+
var i;
235+
236+
vector = factory( 'int8' );
237+
238+
b.tic();
239+
for ( i = 0; i < b.iterations; i++ ) {
240+
arr = vector( 0 );
241+
if ( arr.length !== 0 ) {
242+
b.fail( 'should have length 0' );
243+
}
244+
}
245+
b.toc();
246+
if ( !isndarrayLike( arr ) ) {
247+
b.fail( 'should return an ndarray' );
248+
}
249+
b.pass( 'benchmark finished' );
250+
b.end();
251+
});
252+
253+
bench( pkg+':factory:dtype=uint8', function benchmark( b ) {
254+
var vector;
255+
var arr;
256+
var i;
257+
258+
vector = factory( 'uint8' );
259+
260+
b.tic();
261+
for ( i = 0; i < b.iterations; i++ ) {
262+
arr = vector( 0 );
263+
if ( arr.length !== 0 ) {
264+
b.fail( 'should have length 0' );
265+
}
266+
}
267+
b.toc();
268+
if ( !isndarrayLike( arr ) ) {
269+
b.fail( 'should return an ndarray' );
270+
}
271+
b.pass( 'benchmark finished' );
272+
b.end();
273+
});
274+
275+
bench( pkg+':factory:dtype=uint8c', function benchmark( b ) {
276+
var vector;
277+
var arr;
278+
var i;
279+
280+
vector = factory( 'uint8c' );
281+
282+
b.tic();
283+
for ( i = 0; i < b.iterations; i++ ) {
284+
arr = vector( 0 );
285+
if ( arr.length !== 0 ) {
286+
b.fail( 'should have length 0' );
287+
}
288+
}
289+
b.toc();
290+
if ( !isndarrayLike( arr ) ) {
291+
b.fail( 'should return an ndarray' );
292+
}
293+
b.pass( 'benchmark finished' );
294+
b.end();
295+
});
296+
297+
bench( pkg+':factory:dtype=generic', function benchmark( b ) {
298+
var vector;
299+
var arr;
300+
var i;
301+
302+
vector = factory( 'generic' );
303+
304+
b.tic();
305+
for ( i = 0; i < b.iterations; i++ ) {
306+
arr = vector( 0 );
307+
if ( arr.length !== 0 ) {
308+
b.fail( 'should have length 0' );
309+
}
310+
}
311+
b.toc();
312+
if ( !isndarrayLike( arr ) ) {
313+
b.fail( 'should return an ndarray' );
314+
}
315+
b.pass( 'benchmark finished' );
316+
b.end();
317+
});

0 commit comments

Comments
 (0)