Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Feb 25, 2025
1 parent b4a9e77 commit 8cbea4f
Show file tree
Hide file tree
Showing 9 changed files with 438 additions and 78 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,28 @@

<!-- /.package -->

<section class="package" id="blas-base-gasum-unreleased">

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

<details>

<section class="features">

##### Features

- [`57184e0`](https://github.com/stdlib-js/stdlib/commit/57184e03168cf78b9daeee492995a7ea2670d463) - add accessor array support to `blas/base/gasum` [(#5439)](https://github.com/stdlib-js/stdlib/pull/5439)

</section>

<!-- /.features -->

</details>

</section>

<!-- /.package -->

<section class="package" id="blas-base-gscal-unreleased">

#### [@stdlib/blas/base/gscal](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/gscal)
Expand Down Expand Up @@ -6121,6 +6143,7 @@ A total of 13 people contributed to this release. Thank you to the following con

<details>

- [`57184e0`](https://github.com/stdlib-js/stdlib/commit/57184e03168cf78b9daeee492995a7ea2670d463) - **feat:** add accessor array support to `blas/base/gasum` [(#5439)](https://github.com/stdlib-js/stdlib/pull/5439) _(by Muhammad Haris)_
- [`2d26990`](https://github.com/stdlib-js/stdlib/commit/2d26990255c7d3ef2396f2d8c7ebf545e7ba41ee) - **docs:** update examples for `blas/base/wasm/zcopy` [(#5442)](https://github.com/stdlib-js/stdlib/pull/5442) _(by Gururaj Gurram)_
- [`3b48bb5`](https://github.com/stdlib-js/stdlib/commit/3b48bb5eb47d5a8559d8d7e82f7639ee59d90d21) - **feat:** add accessor array support to `blas/base/gscal` [(#5418)](https://github.com/stdlib-js/stdlib/pull/5418) _(by Muhammad Haris, Athan Reines)_
- [`f027bb0`](https://github.com/stdlib-js/stdlib/commit/f027bb01176eb0409c263cf3a9c4529bafccc5a9) - **docs:** update examples for `blas/base/wasm/ccopy` [(#5410)](https://github.com/stdlib-js/stdlib/pull/5410) _(by Gururaj Gurram)_
Expand Down
3 changes: 3 additions & 0 deletions base/gasum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ sum = gasum.ndarray( 3, x, -1, x.length-1 );

- If `N <= 0`, both functions return `0`.
- `gasum()` corresponds to the [BLAS][blas] level 1 function [`dasum`][dasum] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dasum`][@stdlib/blas/base/dasum], [`sasum`][@stdlib/blas/base/sasum], 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 @@ -199,6 +200,8 @@ console.log( y );

[@stdlib/blas/base/sasum]: https://github.com/stdlib-js/blas/tree/main/base/sasum

[@stdlib/array/base/accessor]: https://github.com/stdlib-js/array-base-accessor

<!-- <related-links> -->

<!-- </related-links> -->
Expand Down
11 changes: 8 additions & 3 deletions base/gasum/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 `gasum`.
Expand All @@ -40,7 +45,7 @@ interface Routine {
* var z = gasum( x.length, x, 1 );
* // returns 15.0
*/
( N: number, x: NumericArray, stride: number ): number;
( N: number, x: InputArray, stride: number ): number;

/**
* Computes the sum of the absolute values using alternative indexing semantics.
Expand All @@ -57,7 +62,7 @@ interface Routine {
* var z = gasum.ndarray( x.length, x, 1, 0 );
* // returns 21.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 base/gasum/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 gasum = require( './index' );


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

gasum( x.length, x, 1 ); // $ExpectType number
gasum( 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 @@ -86,6 +88,7 @@ import gasum = require( './index' );
const x = new Float64Array( 10 );

gasum.ndarray( x.length, x, 1, 0 ); // $ExpectType number
gasum.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
101 changes: 101 additions & 0 deletions base/gasum/lib/accessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @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 abs = require( '@stdlib/math/base/special/abs' );


// VARIABLES //

var M = 6;


// MAIN //

/**
* Computes the sum of absolute values.
*
* @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 - index increment
* @param {NonNegativeInteger} offset - starting index
* @returns {number} sum
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
*
* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ];
*
* var s = gasum( x.length, arraylike2object( toAccessorArray( x ) ), 1, 0 );
* // returns 15.0
*/
function gasum( N, x, stride, offset ) {
var buf;
var get;
var sum;
var ix;
var m;
var i;

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

sum = 0.0;
ix = offset;
if ( stride === 0 ) {
sum = abs( get( buf, ix ) * N );
return sum;
}

// Use unrolled loops if the stride is equal to `1`...
if ( stride === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
sum += abs( get( buf, ix ) );
ix += stride;
}
}
if ( N < M ) {
return sum;
}
for ( i = m; i < N; i += M ) {
sum += abs( get( buf, ix ) ) + abs( get( buf, ix+1 ) ) + abs( get( buf, ix+2 ) ) + abs( get( buf, ix+3 ) ) + abs( get( buf, ix+4 ) ) + abs( get( buf, ix+5 ) ); // eslint-disable-line max-len
ix += M;
}
return sum;
}
for ( i = 0; i < N; i++ ) {
sum += abs( get( buf, ix ) );
ix += stride;
}
return sum;
}


// EXPORTS //

module.exports = gasum;
42 changes: 4 additions & 38 deletions base/gasum/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@

// MODULES //

var abs = require( '@stdlib/math/base/special/abs' );


// VARIABLES //

var M = 6;
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -42,40 +38,10 @@ var M = 6;
* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0 ] );
*
* var s = gasum( x.length, x, 1 );
* // 15.0
* // returns 15.0
*/
function gasum( N, x, stride ) {
var sum;
var m;
var i;

sum = 0.0;
if ( N <= 0 || stride <= 0 ) {
return sum;
}
// Use unrolled loops if the stride is equal to `1`...
if ( stride === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
sum += abs( x[i] );
}
}
if ( N < M ) {
return sum;
}
for ( i = m; i < N; i += M ) {
sum += abs(x[i]) + abs(x[i+1]) + abs(x[i+2]) + abs(x[i+3]) + abs(x[i+4]) + abs(x[i+5]); // eslint-disable-line max-len
}
return sum;
}
N *= stride;
for ( i = 0; i < N; i += stride ) {
sum += abs( x[i] );
}
return sum;
return ndarray( N, x, stride, stride2offset( N, stride ) );
}


Expand Down
14 changes: 13 additions & 1 deletion base/gasum/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// MODULES //

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


// VARIABLES //
Expand All @@ -43,19 +45,29 @@ var M = 6;
* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0 ] );
*
* var s = gasum( x.length, x, 1, 0 );
* // 15.0
* // returns 15.0
*/
function gasum( N, x, stride, offset ) {
var sum;
var ix;
var m;
var i;
var o;

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

ix = offset;
if ( stride === 0 ) {
sum = abs( x[ix] * N );
return sum;
}

// Use unrolled loops if the stride is equal to `1`...
if ( stride === 1 ) {
Expand Down
Loading

0 comments on commit 8cbea4f

Please sign in to comment.