Skip to content
Merged
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
15 changes: 9 additions & 6 deletions lib/node_modules/@stdlib/blas/base/gnrm2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var gnrm2 = require( '@stdlib/blas/base/gnrm2' );

#### gnrm2( N, x, stride )

Computes the [L2-norm][l2-norm] of a vector `x`.
Computes the [L2-norm][l2-norm] of a vector.

```javascript
var x = [ 1.0, -2.0, 2.0 ];
Expand All @@ -62,9 +62,9 @@ The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
- **stride**: index increment for `x`.
- **stride**: stride length.

The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [L2-norm][l2-norm] of every other element in `x`,
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [L2-norm][l2-norm] of every other element:

```javascript
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
Expand All @@ -87,7 +87,7 @@ var z = gnrm2( 4, x1, 2 );
// returns 5.0
```

If either `N` or `stride` is less than or equal to `0`, the function returns `0`.
If `N` is less than or equal to `0`, the function returns `0`.

#### gnrm2.ndarray( N, x, stride, offset )

Expand All @@ -102,9 +102,9 @@ var z = gnrm2.ndarray( x.length, x, 1, 0 );

The function has the following additional parameters:

- **offset**: starting index for `x`.
- **offset**: starting index.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [L2-norm][l2-norm] for every other value in `x` starting from the second value
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [L2-norm][l2-norm] for every other value in the strided array starting from the second value:

```javascript
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
Expand All @@ -123,6 +123,7 @@ var z = gnrm2.ndarray( 4, x, 2, 1 );

- If `N <= 0`, both functions return `0.0`.
- `gnrm2()` corresponds to the [BLAS][blas] level 1 function [`dnrm2`][dnrm2] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dnrm2`][@stdlib/blas/base/dnrm2], [`snrm2`][@stdlib/blas/base/snrm2], etc.) are likely to be significantly more performant.
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).

</section>

Expand Down Expand Up @@ -181,6 +182,8 @@ console.log( out );

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor

[@stdlib/blas/base/dnrm2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dnrm2

[@stdlib/blas/base/snrm2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/snrm2
Expand Down
14 changes: 7 additions & 7 deletions lib/node_modules/@stdlib/blas/base/gnrm2/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{{alias}}( N, x, stride )
Computes the L2-norm of a vector.

The `N` and `stride` parameters determine which elements in `x` are accessed
at runtime.
The `N` and stride parameters determine which elements in the strided array
are accessed at runtime.

Indexing is relative to the first index. To introduce an offset, use a typed
array view.

If `N <= 0` or `stride <= 0`, the function returns `0`.
If `N <= 0`, the function returns `0`.

Parameters
----------
Expand All @@ -19,7 +19,7 @@
Input array.

stride: integer
Index increment.
Stride length.

Returns
-------
Expand Down Expand Up @@ -49,8 +49,8 @@
Computes the L2-norm of a vector using alternative indexing semantics.

While typed array views mandate a view offset based on the underlying
buffer, the `offset` parameter supports indexing semantics based on a
starting index.
buffer, the offset parameter supports indexing semantics based on a starting
index.

Parameters
----------
Expand All @@ -61,7 +61,7 @@
Input array.

stride: integer
Index increment.
Stride length.

offset: integer
Starting index.
Expand Down
11 changes: 8 additions & 3 deletions lib/node_modules/@stdlib/blas/base/gnrm2/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

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

import { NumericArray } from '@stdlib/types/array';
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';

/**
* Input array.
*/
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;

/**
* Interface describing `gnrm2`.
Expand All @@ -40,7 +45,7 @@ interface Routine {
* var z = gnrm2( x.length, x, 1 );
* // returns 3.0
*/
( N: number, x: NumericArray, stride: number ): number;
( N: number, x: InputArray, stride: number ): number;

/**
* Computes the L2-norm of a vector using alternative indexing semantics.
Expand All @@ -57,7 +62,7 @@ interface Routine {
* var z = gnrm2.ndarray( x.length, x, 1, 0 );
* // returns 3.0
*/
ndarray( N: number, x: NumericArray, stride: number, offset: number ): number;
ndarray( N: number, x: InputArray, stride: number, offset: number ): number;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/node_modules/@stdlib/blas/base/gnrm2/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

import AccessorArray = require( '@stdlib/array/base/accessor' );
import gnrm2 = require( './index' );


Expand All @@ -26,6 +27,7 @@ import gnrm2 = require( './index' );
const x = new Float64Array( 10 );

gnrm2( x.length, x, 1 ); // $ExpectType number
gnrm2( x.length, new AccessorArray( x ), 1 ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not a number...
Expand Down Expand Up @@ -85,6 +87,7 @@ import gnrm2 = require( './index' );
const x = new Float64Array( 10 );

gnrm2.ndarray( x.length, x, 1, 0 ); // $ExpectType number
gnrm2.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number
}

// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Expand Down
90 changes: 90 additions & 0 deletions lib/node_modules/@stdlib/blas/base/gnrm2/lib/accessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 sqrt = require( '@stdlib/math/base/special/sqrt' );
var abs = require( '@stdlib/math/base/special/abs' );
var pow = require( '@stdlib/math/base/special/pow' );


// MAIN //

/**
* Computes the L2-norm of a vector.
*
* @private
* @param {PositiveInteger} N - number of indexed elements
* @param {Object} x - input array object
* @param {Collection} x.data - input array data
* @param {Array<Function>} x.accessors - array element accessors
* @param {integer} stride - stride length
* @param {NonNegativeInteger} offset - starting index
* @returns {number} L2-norm
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
*
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
*
* var z = gnrm2( 4, arraylike2object( toAccessorArray( x ) ), 2, 1 );
* // returns 5.0
*/
function gnrm2( N, x, stride, offset ) {
var scale;
var buf;
var get;
var ssq;
var ax;
var ix;
var i;

buf = x.data;
get = x.accessors[ 0 ];

ix = offset;
if ( N === 1 ) {
return abs( get( buf, ix ) );
}
if ( stride === 0 ) {
return sqrt( N ) * abs( get( buf, ix ) );
}
scale = 0.0;
ssq = 1.0;
for ( i = 0; i < N; i++ ) {
if ( get( buf, ix ) !== 0.0 ) {
ax = abs( get( buf, ix ) );
if ( scale < ax ) {
ssq = 1.0 + ( ssq * pow( scale/ax, 2 ) );
scale = ax;
} else {
ssq += pow( ax/scale, 2 );
}
}
ix += stride;
}
return scale * sqrt( ssq );
}


// EXPORTS //

module.exports = gnrm2;
4 changes: 1 addition & 3 deletions lib/node_modules/@stdlib/blas/base/gnrm2/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@
* // returns 3.0
*
* @example
* var floor = require( '@stdlib/math/base/special/floor' );
* var gnrm2 = require( '@stdlib/blas/base/gnrm2' );
*
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
* var N = floor( x.length / 2 );
*
* var z = gnrm2.ndarray( N, x, 2, 1 );
* var z = gnrm2.ndarray( 4, x, 2, 1 );
* // returns 5.0
*/

Expand Down
32 changes: 3 additions & 29 deletions lib/node_modules/@stdlib/blas/base/gnrm2/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

// MODULES //

var sqrt = require( '@stdlib/math/base/special/sqrt' );
var abs = require( '@stdlib/math/base/special/abs' );
var pow = require( '@stdlib/math/base/special/pow' );
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -42,32 +41,7 @@ var pow = require( '@stdlib/math/base/special/pow' );
* // returns 3.0
*/
function gnrm2( N, x, stride ) {
var scale;
var ssq;
var ax;
var i;

if ( N <= 0 || stride <= 0 ) {
return 0.0;
}
if ( N === 1 ) {
return abs( x[ 0 ] );
}
scale = 0.0;
ssq = 1.0;
N *= stride;
for ( i = 0; i < N; i += stride ) {
if ( x[ i ] !== 0.0 ) {
ax = abs( x[ i ] );
if ( scale < ax ) {
ssq = 1.0 + ( ssq * pow( scale/ax, 2 ) );
scale = ax;
} else {
ssq += pow( ax/scale, 2 );
}
}
}
return scale * sqrt( ssq );
return ndarray( N, x, stride, stride2offset( N, stride ) );
}


Expand Down
21 changes: 15 additions & 6 deletions lib/node_modules/@stdlib/blas/base/gnrm2/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

// MODULES //

var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var abs = require( '@stdlib/math/base/special/abs' );
var pow = require( '@stdlib/math/base/special/pow' );
var accessors = require( './accessors.js' );


// MAIN //
Expand All @@ -37,28 +39,35 @@ var pow = require( '@stdlib/math/base/special/pow' );
* @returns {number} L2-norm
*
* @example
* var floor = require( '@stdlib/math/base/special/floor' );
*
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
* var N = floor( x.length / 2 );
*
* var z = gnrm2( N, x, 2, 1 );
* var z = gnrm2( 4, x, 2, 1 );
* // returns 5.0
*/
function gnrm2( N, x, stride, offset ) {
var scale;
var ssq;
var ax;
var ix;
var o;
var i;

if ( N <= 0 ) {
return 0.0;
}
if ( N === 1 ) {
return abs( x[ offset ] );

o = arraylike2object( x );
if ( o.accessorProtocol ) {
return accessors( N, o, stride, offset );
}

ix = offset;
if ( N === 1 ) {
return abs( x[ ix ] );
}
if ( stride === 0 ) {
return sqrt( N ) * abs( x[ ix ] );
}
scale = 0.0;
ssq = 1.0;
for ( i = 0; i < N; i++ ) {
Expand Down
Loading