Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions lib/node_modules/@stdlib/blas/scal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# scal

> Multiply an input [ndarray][@stdlib/ndarray/ctor] by a scalar constant along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.

<section class="usage">

## Usage

```javascript
var scal = require( '@stdlib/blas/scal' );
```

#### scal( x, alpha\[, options] )

Multiplies an input [ndarray][@stdlib/ndarray/ctor] by a scalar constant along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.

```javascript
var array = require( '@stdlib/ndarray/array' );

var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]

var y = scal( x, 5.0 );
// returns <ndarray>[ 5.0, 10.0, 15.0, 20.0 ]

var bool = ( x === y );
// returns true
```

The function has the following parameters:

- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].
- **alpha**: scalar constant. May be either a numeric value or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided a numeric value, the value is cast to the [data type][@stdlib/ndarray/dtypes] of the input [ndarray][@stdlib/ndarray/ctor]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] for `alpha` must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] for `alpha` must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
- **options**: function options (_optional_).

The function accepts the following options:

- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].

By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option.

```javascript
var array = require( '@stdlib/ndarray/array' );

var x = array( [ 1.0, 2.0, 3.0, 4.0 ], {
'shape': [ 2, 2 ],
'order': 'row-major'
});
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]

var y = scal( x, 5.0, {
'dims': [ 1 ]
});
// returns <ndarray>[ [ 5.0, 10.0 ], [ 15.0, 20.0 ] ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The input [ndarray][@stdlib/ndarray/ctor] is multiplied **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**).
- A provided scalar constant is cast to the [data type][@stdlib/ndarray/dtypes] of the input [ndarray][@stdlib/ndarray/ctor].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var scal = require( '@stdlib/blas/scal' );

// Generate an ndarray of random numbers:
var x = discreteUniform( [ 5, 5 ], -20, 20, {
'dtype': 'generic'
});
console.log( ndarray2array( x ) );

// Perform operation:
scal( x, 5.0, {
'dims': [ 0 ]
});

// Print the results:
console.log( ndarray2array( x ) );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes

[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes

</section>

<!-- /.links -->
103 changes: 103 additions & 0 deletions lib/node_modules/@stdlib/blas/scal/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var uniform = require( '@stdlib/random/uniform' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var scal = require( './../lib' );


// VARIABLES //

var options = {
'dtype': 'float64'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = uniform( [ len ], -50.0, 50.0, options );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var o;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
o = scal( x, 1.0 );
if ( typeof o !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( isnan( o.get( i%len ) ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f );
}
}

main();
48 changes: 48 additions & 0 deletions lib/node_modules/@stdlib/blas/scal/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

{{alias}}( x, alpha[, options] )
Multiplies an input ndarray by a scalar constant along one or more ndarray
dimensions.

The function multiplies an input ndarray in-place and thus mutates an
input ndarray.

Parameters
----------
x: ndarray
Input array. Must have a numeric or "generic" data type.

alpha: ndarray|number|Complex
Scalar constant. May be either a scalar value or an ndarray having a
numeric or "generic" data type. If provided a scalar value, the value
is cast to the data type of the input ndarray. If provided an ndarray,
the value must have a shape which is broadcast compatible with the
complement of the shape defined by `options.dims`. For example, given
the input shape `[2, 3, 4]` and `options.dims=[0]`, an ndarray for
`alpha` must have a shape which is broadcast compatible with the shape
`[3, 4]`. Similarly, when performing the operation over all elements in
a provided input ndarray, an ndarray for `alpha` must be a
zero-dimensional ndarray.

options: Object (optional)
Function options.

options.dims: Array<integer> (optional)
List of dimensions over which to perform operation. If not provided, the
function performs the operation over all elements in a provided input
ndarray.

Returns
-------
out: ndarray
Input array.

Examples
--------
> var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] );
> var y = {{alias}}( x, 5.0 );
> {{alias:@stdlib/ndarray/to-array}}( y )
[ 5.0, 10.0, 15.0, 20.0 ]

See Also
--------

69 changes: 69 additions & 0 deletions lib/node_modules/@stdlib/blas/scal/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { ArrayLike } from '@stdlib/types/array';
import { typedndarray } from '@stdlib/types/ndarray';
import { ComplexLike } from '@stdlib/types/complex';

/**
* Interface defining options.
*/
interface Options {
/**
* List of dimensions over which to perform operation.
*/
dims?: ArrayLike<number>;
}

/**
* Multiplies an input ndarray by a scalar constant along one or more ndarray dimensions.
*
* ## Notes
*
* - The input ndarray is multiplied **in-place** (i.e., the input ndarray is **mutated**).
* - A provided scalar constant is cast to the data type of the input ndarray.
*
* @param x - input ndarray
* @param alpha - scalar constant
* @param options - function options
* @returns input ndarray
*
* @example
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ 1.0, 2.0, 3.0, 4.0 ], {
* 'shape': [ 2, 2 ],
* 'order': 'row-major'
* });
* // returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
*
* var y = scal( x, 5.0, {
* 'dims': [ 1 ]
* });
* // returns <ndarray>[ [ 5.0, 10.0 ], [ 15.0, 20.0 ] ]
*/
declare function scal<T extends typedndarray<unknown>>( x: T, alpha: typedndarray<unknown> | number | ComplexLike, options?: Options ): T;


// EXPORTS //

export = scal;
Loading