Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<!--

@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.

-->

# resultsFactory

> Create a new constructor for creating a NaN-aware summation results object.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var resultsFactory = require( '@stdlib/blas/ext/base/nannsum/results/factory' );
```

#### resultsFactory( dtype )

Returns a new constructor for creating a NaN-aware summation results object.

```javascript
var Results = resultsFactory( 'float64' );
// returns <Function>

var r = new Results();
// returns <Results>
```

The function supports the following parameters:

- **dtype**: floating-point data type for storing floating-point results. Must be either `'float64'` or `'float32'`.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- A results object is a [`struct`][@stdlib/dstructs/struct] providing a fixed-width composite data structure for storing NaN-aware summation results and providing an ABI-stable data layout for JavaScript-C interoperation.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var BigInt = require( '@stdlib/bigint/ctor' );
var resultsFactory = require( '@stdlib/blas/ext/base/nannsum/results/factory' );

var Results = resultsFactory( 'float64' );
var results = new Results({
'sum': 11.7586,
'count': BigInt( 5 )
});

var str = results.toString();
console.log( str );
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- 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/dstructs/struct]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/dstructs/struct

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @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 isFunction = require( '@stdlib/assert/is-function' );
var isObject = require( '@stdlib/assert/is-object' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var v;
var i;

values = [
'float64',
'float32'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = factory( values[ i%values.length ] );
if ( typeof v !== 'function' ) {
b.fail( 'should return a function' );
}
}
b.toc();
if ( !isFunction( v ) ) {
b.fail( 'should return a function' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::constructor,new', pkg ), function benchmark( b ) {
var values;
var v;
var i;

values = [
factory( 'float64' ),
factory( 'float32' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = new ( values[ i%values.length ] )();
if ( typeof v !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isObject( v ) ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::constructor,no_new', pkg ), function benchmark( b ) {
var values;
var v;
var i;

values = [
factory( 'float64' ),
factory( 'float32' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = values[ i%values.length ]();
if ( typeof v !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isObject( v ) ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

{{alias}}( dtype )
Returns a constructor for creating a NaN-aware summation results object.

Parameters
----------
dtype: string
Floating-point data type for storing floating-point results.

Returns
-------
fcn: Function
Results constructor.

Examples
--------
> var R = {{alias}}( 'float64' );
> var r = new R();
> r.toString()
<string>

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* @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

/**
* Interface describing NaN-aware summation results.
*/
interface Results {
/**
* Sum of non-NaN elements.
*/
sum?: number;

/**
* Number of non-NaN elements.
*/
count?: bigint;
}

/**
* Interface describing options when serializing a results object to a string.
*/
interface ToStringOptions {
/**
* Number of digits to display after decimal points. Default: `4`.
*/
digits?: number;
}

/**
* Interface describing a results data structure.
*/
declare class ResultsStruct {
/**
* Results constructor.
*
* @param arg - buffer or data object
* @param byteOffset - byte offset
* @param byteLength - maximum byte length
* @returns results
*/
constructor( arg?: ArrayBuffer | Results, byteOffset?: number, byteLength?: number );

/**
* Sum of non-NaN elements.
*/
sum: number;

/**
* Number of non-NaN elements.
*/
count: bigint;

/**
* Method name.
*/
method: string;

/**
* Serializes a results object as a formatted string.
*
* @param options - options object
* @returns serialized results
*/
toString( options?: ToStringOptions ): string;

/**
* Serializes a results object as a JSON object.
*
* @returns serialized object
*/
toJSON(): object;

/**
* Returns a DataView of a results object.
*
* @returns DataView
*/
toDataView(): DataView;
}

/**
* Interface defining a results constructor which is both "newable" and "callable".
*/
interface ResultsConstructor {
/**
* Results constructor.
*
* @param arg - buffer or data object
* @param byteOffset - byte offset
* @param byteLength - maximum byte length
* @returns results object
*/
new( arg?: ArrayBuffer | Results, byteOffset?: number, byteLength?: number ): ResultsStruct;

/**
* Results constructor.
*
* @param arg - buffer or data object
* @param byteOffset - byte offset
* @param byteLength - maximum byte length
* @returns results object
*/
( arg?: ArrayBuffer | Results, byteOffset?: number, byteLength?: number ): ResultsStruct;
}

/**
* Returns a new results constructor for creating a NaN-aware summation results object.
*
* @param dtype - floating-point data type for storing floating-point results
* @returns results constructor
*
* @example
* var Results = resultsFactory( 'float64' );
* // returns <Function>
*
* var r = new Results();
* // returns <Results>
*/
declare function resultsFactory( dtype: 'float64' | 'float32' ): ResultsConstructor;


// EXPORTS //

export = resultsFactory;
Loading
Loading