Skip to content

Commit 9cdcdaa

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/assign-scalar
PR-URL: #11029 Closes: stdlib-js/metr-issue-tracker#202 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 14c965b commit 9cdcdaa

File tree

118 files changed

+24171
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+24171
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# assignScalar
22+
23+
> Assign a scalar value to every element of an output [ndarray][@stdlib/ndarray/base/ctor].
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var assignScalar = require( '@stdlib/ndarray/base/assign-scalar' );
37+
```
38+
39+
#### assignScalar( arrays )
40+
41+
Assigns a scalar value to every element of an output [ndarray][@stdlib/ndarray/base/ctor].
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
47+
var Float64Array = require( '@stdlib/array/float64' );
48+
49+
// Create a zero-dimensional ndarray containing the scalar value:
50+
var x = scalar2ndarray( 5.0, {
51+
'dtype': 'float64'
52+
});
53+
54+
// Create a data buffer:
55+
var ybuf = new Float64Array( 6 );
56+
57+
// Define the shape of the output array:
58+
var shape = [ 3, 1, 2 ];
59+
60+
// Define the array strides:
61+
var sy = [ 2, 2, 1 ];
62+
63+
// Define the index offset:
64+
var oy = 0;
65+
66+
// Create the output ndarray-like object:
67+
var y = {
68+
'dtype': 'float64',
69+
'data': ybuf,
70+
'shape': shape,
71+
'strides': sy,
72+
'offset': oy,
73+
'order': 'row-major'
74+
};
75+
76+
// Assign the scalar value:
77+
assignScalar( [ x, y ] );
78+
79+
console.log( y.data );
80+
// => <Float64Array>[ 5.0, 5.0, 5.0, 5.0, 5.0, 5.0 ]
81+
```
82+
83+
The function accepts the following arguments:
84+
85+
- **arrays**: array-like object containing a zero-dimensional input [ndarray][@stdlib/ndarray/base/ctor] and an output [ndarray][@stdlib/ndarray/base/ctor].
86+
87+
Each provided [ndarray][@stdlib/ndarray/base/ctor] should be an object with the following properties:
88+
89+
- **dtype**: data type.
90+
- **data**: data buffer.
91+
- **shape**: dimensions.
92+
- **strides**: stride lengths.
93+
- **offset**: index offset.
94+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
95+
96+
</section>
97+
98+
<!-- /.usage -->
99+
100+
<section class="notes">
101+
102+
</section>
103+
104+
<!-- /.notes -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
114+
var filledarray = require( '@stdlib/array/filled' );
115+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
116+
var assignScalar = require( '@stdlib/ndarray/base/assign-scalar' );
117+
118+
var x = scalar2ndarray( 10.0, {
119+
'dtype': 'generic'
120+
});
121+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
122+
123+
var y = {
124+
'dtype': 'generic',
125+
'data': filledarray( 0, 10, 'generic' ),
126+
'shape': [ 5, 2 ],
127+
'strides': [ 2, 1 ],
128+
'offset': 0,
129+
'order': 'row-major'
130+
};
131+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
132+
133+
assignScalar( [ x, y ] );
134+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="references">
144+
145+
</section>
146+
147+
<!-- /.references -->
148+
149+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
150+
151+
<section class="related">
152+
153+
</section>
154+
155+
<!-- /.related -->
156+
157+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
158+
159+
<section class="links">
160+
161+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor
162+
163+
<!-- <related-links> -->
164+
165+
<!-- </related-links> -->
166+
167+
</section>
168+
169+
<!-- /.links -->
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var floor = require( '@stdlib/math/base/special/floor' );
27+
var filledarray = require( '@stdlib/array/filled' );
28+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var assignScalar = require( './../lib/10d_blocked.js' );
32+
33+
34+
// VARIABLES //
35+
36+
var types = [ 'float64' ];
37+
var order = 'column-major';
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - ndarray length
47+
* @param {NonNegativeIntegerArray} shape - ndarray shape
48+
* @param {string} xtype - input ndarray data type
49+
* @param {string} ytype - output ndarray data type
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len, shape, xtype, ytype ) {
53+
var x;
54+
var y;
55+
56+
x = {
57+
'dtype': xtype,
58+
'data': filledarray( 10.0, 1, xtype ),
59+
'shape': [],
60+
'strides': [ 0 ],
61+
'offset': 0,
62+
'order': order
63+
};
64+
y = {
65+
'dtype': ytype,
66+
'data': filledarray( 0.0, len, ytype ),
67+
'shape': shape,
68+
'strides': shape2strides( shape, order ),
69+
'offset': 0,
70+
'order': order
71+
};
72+
return benchmark;
73+
74+
/**
75+
* Benchmark function.
76+
*
77+
* @private
78+
* @param {Benchmark} b - benchmark instance
79+
*/
80+
function benchmark( b ) {
81+
var i;
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
assignScalar( x, y );
86+
if ( isnan( y.data[ i%len ] ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
}
90+
b.toc();
91+
if ( isnan( y.data[ i%len ] ) ) {
92+
b.fail( 'should not return NaN' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
}
97+
}
98+
99+
100+
// MAIN //
101+
102+
/**
103+
* Main execution sequence.
104+
*
105+
* @private
106+
*/
107+
function main() {
108+
var len;
109+
var min;
110+
var max;
111+
var sh;
112+
var t1;
113+
var t2;
114+
var f;
115+
var i;
116+
var j;
117+
118+
min = 1; // 10^min
119+
max = 6; // 10^max
120+
121+
for ( j = 0; j < types.length; j++ ) {
122+
t1 = types[ j ];
123+
t2 = types[ j ];
124+
for ( i = min; i <= max; i++ ) {
125+
len = pow( 10, i );
126+
127+
sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
128+
f = createBenchmark( len, sh, t1, t2 );
129+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,yorder=%s,xtype=%s,ytype=%s', pkg, sh.length, len, sh.join( ',' ), order, order, t1, t2 ), f );
130+
131+
sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
132+
f = createBenchmark( len, sh, t1, t2 );
133+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,yorder=%s,xtype=%s,ytype=%s', pkg, sh.length, len, sh.join( ',' ), order, order, t1, t2 ), f );
134+
135+
len = floor( pow( len, 1.0/10.0 ) );
136+
sh = [ len, len, len, len, len, len, len, len, len, len ];
137+
len *= pow( len, 9 );
138+
f = createBenchmark( len, sh, t1, t2 );
139+
bench( format( '%s::blocked:ndims=%d,len=%d,shape=[%s],xorder=%s,yorder=%s,xtype=%s,ytype=%s', pkg, sh.length, len, sh.join( ',' ), order, order, t1, t2 ), f );
140+
}
141+
}
142+
}
143+
144+
main();

0 commit comments

Comments
 (0)