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
145 changes: 145 additions & 0 deletions lib/node_modules/@stdlib/ml/base/kmeans/stats/struct-factory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<!--

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

-->

# structFactory

> Create a new [`struct`][@stdlib/dstructs/struct] constructor tailored to a specified floating-point data type.

<!-- 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 structFactory = require( '@stdlib/ml/base/kmeans/stats/struct-factory' );
```

#### structFactory( dtype, k )

Returns a new [`struct`][@stdlib/dstructs/struct] constructor tailored to a specified floating-point data type.

```javascript
var Struct = structFactory( 'float64', 5 );
// returns <Function>

var s = new Struct();
// returns <Struct>
```

The function supports the following parameters:

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

</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 [`struct`][@stdlib/dstructs/struct] provides a fixed-width composite data structure for storing kmeans clustering statistics and provides an ABI-stable data layout for JavaScript-C interoperation.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Float32Array = require( '@stdlib/array/float32' );
var Int32Array = require( '@stdlib/array/int32' );
var structFactory = require( '@stdlib/ml/base/kmeans/stats/struct-factory' );

var Struct = structFactory( 'float64', 2 );
var results = new Struct({
'nObs': new Int32Array( [ 3, 2 ] ),
'sumSquaredDist': new Float64Array( [ 9.998, 11.412 ] ),
'meanSquaredDist': new Float64Array( [ 4.353, 2.897 ] ),
'stdevSquaredDist': new Float64Array( [ 1.232, 8.913 ] )
});

var str = results.toString({
'format': 'linear'
});
console.log( str );

Struct = structFactory( 'float32', 2 );
results = new Struct({
'nObs': new Int32Array( [ 3, 2 ] ),
'sumSquaredDist': new Float32Array( [ 9.998, 11.412 ] ),
'meanSquaredDist': new Float32Array( [ 4.353, 2.897 ] ),
'stdevSquaredDist': new Float32Array( [ 1.232, 8.913 ] )
});

str = results.toString({
'format': 'linear'
});
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,56 @@
/**
* @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 pkg = require( './../package.json' ).name;
var factory = require( './../lib' );


// MAIN //

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

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = factory( values[ i%values.length ], k );
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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

{{alias}}( dtype, k )
Returns a new struct constructor tailored to a specified floating-point data
type.

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

k: integer
Number of centroids.

Returns
-------
fcn: Function
Struct constructor.

Examples
--------
> var S = {{alias}}( 'float64', 3 );
> var r = new S();
> 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 cluster statistics.
*/
interface Statistics<T> {
/**
* Number of observations in each cluster.
*/
nObs?: Int32Array;

/**
* Sum of squared distance from cluster centroids.
*/
sumSquaredDist?: T;

/**
* Mean squared distance from cluster centroids.
*/
meanSquaredDist?: T;

/**
* Standard deviation of squared distance from cluster centroids.
*/
stdevSquaredDist?: T;
}

/**
* Interface describing a struct data structure.
*/
declare class Struct<T> {
/**
* Struct constructor.
*
* @param arg - buffer or data object
* @param byteOffset - byte offset
* @param byteLength - maximum byte length
* @returns struct
*/
constructor( arg?: ArrayBuffer | Statistics<T>, byteOffset?: number, byteLength?: number );

/**
* Number of observations in each cluster.
*/
nObs: Int32Array;

/**
* Sum of squared distance from cluster centroids.
*/
sumSquaredDist: T;

/**
* Mean squared distance from cluster centroids.
*/
meanSquaredDist: T;

/**
* Standard deviation of squared distance from cluster centroids.
*/
stdevSquaredDist: T;
}

/**
* Interface defining a struct constructor which is both "newable" and "callable".
*/
interface StructConstructor<T> {
/**
* Struct constructor.
*
* @param arg - buffer or data object
* @param byteOffset - byte offset
* @param byteLength - maximum byte length
* @returns struct
*/
new( arg?: ArrayBuffer | Statistics<T>, byteOffset?: number, byteLength?: number ): Struct<T>;

/**
* Struct constructor.
*
* @param arg - buffer or data object
* @param byteOffset - byte offset
* @param byteLength - maximum byte length
* @returns struct
*/
( arg?: ArrayBuffer | Statistics<T>, byteOffset?: number, byteLength?: number ): Struct<T>;
}

/**
* Returns a new struct constructor tailored to a specified floating-point data type.
*
* @param dtype - floating-point data type for storing floating-point statistics
* @param k - number of centroids
* @returns struct constructor
*
* @example
* var Struct = structFactory( 'float64', 5 );
* // returns <Function>
*
* var s = new Struct();
* // returns <Struct>
*/
declare function structFactory( dtype: 'float64', k: number ): StructConstructor<Float64Array>;

/**
* Returns a new struct constructor tailored to a specified floating-point data type.
*
* @param dtype - floating-point data type for storing floating-point statistics
* @param k - number of centroids
* @returns struct constructor
*
* @example
* var Struct = structFactory( 'float32', 5 );
* // returns <Function>
*
* var s = new Struct();
* // returns <Struct>
*/
declare function structFactory( dtype: 'float32', k: number ): StructConstructor<Float32Array>;


// EXPORTS //

export = structFactory;
Loading