Skip to content

Commit 868e779

Browse files
MeKaustubh07kgryte
andauthored
feat: add blas/base/ndarray/gscal
PR-URL: #11946 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent a65e1fd commit 868e779

10 files changed

Lines changed: 823 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
# gscal
22+
23+
> Multiply a one-dimensional ndarray by a scalar constant.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gscal = require( '@stdlib/blas/base/ndarray/gscal' );
37+
```
38+
39+
#### gscal( arrays )
40+
41+
Multiplies a one-dimensional ndarray by a scalar constant.
42+
43+
```javascript
44+
var vector = require( '@stdlib/ndarray/vector/ctor' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
47+
var x = vector( [ 1.0, 2.0, 3.0, 4.0, 5.0 ], 'generic' );
48+
49+
var alpha = scalar2ndarray( 5.0, {
50+
'dtype': 'generic'
51+
});
52+
53+
var y = gscal( [ x, alpha ] );
54+
// returns <ndarray>[ 5.0, 10.0, 15.0, 20.0, 25.0 ]
55+
56+
var bool = ( y === x );
57+
// returns true
58+
```
59+
60+
The function has the following parameters:
61+
62+
- **arrays**: array-like object containing the following ndarrays:
63+
64+
- a one-dimensional input ndarray.
65+
- a zero-dimensional ndarray containing a scalar constant.
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="notes">
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
85+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
86+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
87+
var gscal = require( '@stdlib/blas/base/ndarray/gscal' );
88+
89+
var opts = {
90+
'dtype': 'generic'
91+
};
92+
93+
var x = discreteUniform( [ 10 ], 0, 100, opts );
94+
console.log( ndarray2array( x ) );
95+
96+
var alpha = scalar2ndarray( 5.0, opts );
97+
98+
var out = gscal( [ x, alpha ] );
99+
console.log( ndarray2array( out ) );
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
107+
108+
<section class="related">
109+
110+
</section>
111+
112+
<!-- /.related -->
113+
114+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
115+
116+
<section class="links">
117+
118+
</section>
119+
120+
<!-- /.links -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 uniform = require( '@stdlib/random/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gscal = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var alpha;
51+
var x;
52+
53+
x = uniform( [ len ], -100.0, 100.0, options );
54+
alpha = scalar2ndarray( 5.0, options );
55+
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var z;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
z = gscal( [ x, alpha ] );
71+
if ( typeof z !== 'object' ) {
72+
b.fail( 'should return an ndarray' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnan( z.get( i%len ) ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( format( '%s:len=%d', pkg, len ), f );
106+
}
107+
}
108+
109+
main();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( arrays )
3+
Multiplies a one-dimensional ndarray by a scalar constant.
4+
5+
If provided an empty input ndarray, the function returns the input ndarray
6+
unchanged.
7+
8+
Parameters
9+
----------
10+
arrays: ArrayLikeObject<ndarray>
11+
Array-like object containing the following ndarrays:
12+
13+
- a one-dimensional input/output ndarray.
14+
- a zero-dimensional ndarray containing a scalar constant.
15+
16+
Returns
17+
-------
18+
out: ndarray
19+
Input ndarray.
20+
21+
Examples
22+
--------
23+
> var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 4.0, 2.0, -3.0, 5.0, -1.0 ], 'generic' );
24+
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, { 'dtype': 'generic' } );
25+
26+
> {{alias}}( [ x, alpha ] );
27+
> x
28+
<ndarray>[ 20.0, 10.0, -15.0, 25.0, -5.0 ]
29+
30+
See Also
31+
--------
32+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Multiplies a one-dimensional ndarray by a scalar constant.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a zero-dimensional ndarray containing a scalar constant.
34+
*
35+
* @param arrays - array-like object containing ndarrays
36+
* @returns input ndarray
37+
*
38+
* @example
39+
* var vector = require( '@stdlib/ndarray/vector/ctor' );
40+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
41+
*
42+
* var x = vector( [ 1.0, 2.0, 3.0, 4.0, 5.0 ], 'generic' );
43+
*
44+
* var alpha = scalar2ndarray( 5.0, {
45+
* 'dtype': 'generic'
46+
* });
47+
*
48+
* var y = gscal( [ x, alpha ] );
49+
* // returns <ndarray>[ 5.0, 10.0, 15.0, 20.0, 25.0 ]
50+
*
51+
* var bool = ( y === x );
52+
* // returns true
53+
*/
54+
declare function gscal<T extends typedndarray<number> = typedndarray<number>>( arrays: [ T, typedndarray<number> ] ): T;
55+
56+
57+
// EXPORTS //
58+
59+
export = gscal;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import gscal = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'generic'
31+
});
32+
const alpha = zeros( [], {
33+
'dtype': 'generic'
34+
});
35+
36+
gscal( [ x, alpha ] ); // $ExpectType genericndarray<number>
37+
}
38+
39+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
40+
{
41+
gscal( '10' ); // $ExpectError
42+
gscal( 10 ); // $ExpectError
43+
gscal( true ); // $ExpectError
44+
gscal( false ); // $ExpectError
45+
gscal( null ); // $ExpectError
46+
gscal( undefined ); // $ExpectError
47+
gscal( [] ); // $ExpectError
48+
gscal( {} ); // $ExpectError
49+
gscal( ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
const x = zeros( [ 10 ], {
55+
'dtype': 'generic'
56+
});
57+
const alpha = zeros( [], {
58+
'dtype': 'generic'
59+
});
60+
61+
gscal(); // $ExpectError
62+
gscal( [ x, alpha ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)