Skip to content

Commit 99a5f38

Browse files
committed
feat: add support for stack for sasum
1 parent 7fd112c commit 99a5f38

File tree

12 files changed

+1314
-0
lines changed

12 files changed

+1314
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# sasum
22+
23+
> Compute the sum of [absolute values][@stdlib/math/base/special/abs] ([_L1_ norm][l1norm]).
24+
25+
<section class="intro">
26+
27+
The [_L1_ norm][l1norm] is defined as
28+
29+
<!-- <equation class="equation" label="eq:l1norm" align="center" raw="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \vert x_i \vert" alt="L1 norm definition."> -->
30+
31+
```math
32+
\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \vert x_i \vert
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \vert x_i \vert" data-equation="eq:l1norm">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@c403cb0cbb15d9b7b453e3cea34ca2379500ddd4/lib/node_modules/@stdlib/blas/base/sasum/docs/img/equation_l1norm.svg" alt="L1 norm definition.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var sasum = require( '@stdlib/blas/sasum' );
52+
```
53+
54+
#### sasum( x\[, dim] )
55+
56+
Computes the sum of [absolute values][@stdlib/math/base/special/abs].
57+
58+
```javascript
59+
var Float32Array = require( '@stdlib/array/float32' );
60+
var array = require( '@stdlib/ndarray/array' );
61+
62+
var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) );
63+
64+
var z = sasum( x );
65+
// returns <ndarray>
66+
67+
var v = z.get();
68+
// returns 15.0
69+
```
70+
71+
The function has the following parameters:
72+
73+
- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] whose underlying data type is `float32`.
74+
- **dim**: dimension for which to compute the sum. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`.
75+
76+
For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function computes the sum for each pair of vectors in `x` according to the specified dimension index.
77+
78+
```javascript
79+
var Float32Array = require( '@stdlib/array/float32' );
80+
var array = require( '@stdlib/ndarray/array' );
81+
82+
var opts = {
83+
'shape': [ 2, 3 ]
84+
};
85+
var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 3.0 ] ), opts );
86+
87+
var z = sasum( x );
88+
// returns <ndarray>
89+
90+
var v1 = z.get( 0 );
91+
// returns 9.0
92+
93+
var v2 = z.get( 1 );
94+
// returns 9.0
95+
```
96+
97+
</section>
98+
99+
<!-- /.usage -->
100+
101+
<section class="notes">
102+
103+
## Notes
104+
105+
- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`.
106+
- The output [`ndarray`][@stdlib/ndarray/ctor] has the same data type as the input [`ndarray`][@stdlib/ndarray/ctor] and has a shape which is determined by broadcasting and excludes the contracted dimension.
107+
- If provided an empty vector, the sum is `0`.
108+
- `sasum()` provides a higher-level interface to the [BLAS][blas] level 1 function [`sasum`][@stdlib/blas/base/sasum].
109+
110+
</section>
111+
112+
<!-- /.notes -->
113+
114+
<section class="examples">
115+
116+
## Examples
117+
118+
<!-- eslint no-undef: "error" -->
119+
120+
```javascript
121+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
122+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
123+
var array = require( '@stdlib/ndarray/array' );
124+
var sasum = require( '@stdlib/blas/sasum' );
125+
126+
var opts = {
127+
'dtype': 'float32'
128+
};
129+
130+
var x = array( discreteUniform( 10, 0, 100, opts ), {
131+
'shape': [ 5, 2 ]
132+
});
133+
console.log( ndarray2array( x ) );
134+
135+
var z = sasum( x, -1 );
136+
console.log( ndarray2array( z ) );
137+
```
138+
139+
</section>
140+
141+
<!-- /.examples -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[l1norm]: https://en.wikipedia.org/wiki/Norm_%28mathematics%29
156+
157+
[@stdlib/math/base/special/abs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/abs
158+
159+
[blas]: http://www.netlib.org/blas
160+
161+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
162+
163+
[@stdlib/blas/base/sasum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sasum
164+
165+
</section>
166+
167+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 uniform = require( '@stdlib/random/array/uniform' );
27+
var array = require( '@stdlib/ndarray/array' );
28+
var pkg = require( './../package.json' ).name;
29+
var sasum = require( './../lib/main.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var opts = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = array( uniform( len, -100.0, 100.0, opts ) );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var d;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
d = sasum( x );
65+
if ( isnan( d.get() ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( d.get() ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( pkg+'::vectors:len='+len, f );
100+
}
101+
}
102+
103+
main();
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 uniform = require( '@stdlib/random/array/uniform' );
27+
var numel = require( '@stdlib/ndarray/base/numel' );
28+
var array = require( '@stdlib/ndarray/array' );
29+
var pkg = require( './../package.json' ).name;
30+
var sasum = require( './../lib/main.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var OPTS = {
36+
'dtype': 'float32'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveIntegerArray} shape - array shape
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( shape ) {
50+
var x;
51+
var N;
52+
var o;
53+
54+
N = numel( shape );
55+
o = {
56+
'shape': shape
57+
};
58+
x = array( uniform( N, -100.0, 100.0, OPTS ), o );
59+
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var d;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
d = sasum( x );
75+
if ( isnan( d.iget( 0 ) ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
}
79+
b.toc();
80+
if ( isnan( d.iget( 0 ) ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
/**
92+
* Main execution sequence.
93+
*
94+
* @private
95+
*/
96+
function main() {
97+
var shape;
98+
var min;
99+
var max;
100+
var N;
101+
var f;
102+
var i;
103+
104+
min = 1; // 10^min
105+
max = 6; // 10^max
106+
107+
for ( i = min; i <= max; i++ ) {
108+
N = pow( 10, i );
109+
110+
shape = [ 2, N/2 ];
111+
f = createBenchmark( shape );
112+
bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f );
113+
114+
shape = [ N/2, 2 ];
115+
f = createBenchmark( shape );
116+
bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f );
117+
}
118+
}
119+
120+
main();

0 commit comments

Comments
 (0)