All files main.js

100% Statements 168/168
100% Branches 13/13
100% Functions 3/3
100% Lines 168/168

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 1693x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 56x 56x 56x 56x 56x 56x 56x 56x 2x 56x 34x 34x 36x 56x 108x 108x 36x 56x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 34x 34x 34x 12x 12x 12x 34x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 22x 22x 22x 11x 11x 1x 22x 3x 3x 3x 3x 3x 3x 3x 3x  
/**
* @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 dtypes = require( '@stdlib/ndarray/dtypes' );
var gcovarmtk = require( '@stdlib/stats/base/ndarray/covarmtk' );
var dcovarmtk = require( '@stdlib/stats/base/ndarray/dcovarmtk' );
var scovarmtk = require( '@stdlib/stats/base/ndarray/scovarmtk' );
var factory = require( '@stdlib/ndarray/base/binary-reduce-strided1d-dispatch-factory' );
var isObject = require( '@stdlib/assert/is-object' );
var getShape = require( '@stdlib/ndarray/shape' );
var indicesComplement = require( '@stdlib/array/base/indices-complement' );
var takeIndexed = require( '@stdlib/array/base/take-indexed' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var broadcast = require( '@stdlib/ndarray/base/broadcast-array' );
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
 
 
// VARIABLES //
 
var idtypes = dtypes( 'real_and_generic' );
var odtypes = dtypes( 'real_floating_point_and_generic' );
var policies = {
	'output': 'real_floating_point_and_generic',
	'casting': 'none'
};
var table = {
	'types': [
		'float64',
		'float64',
		'float32',
		'float32'
	],
	'fcns': [
		dcovarmtk,
		scovarmtk
	],
	'default': gcovarmtk
};
var itypes = [ idtypes, idtypes, idtypes, idtypes, idtypes ];
var dispatcher = factory( table, itypes, odtypes, policies );
 
 
// FUNCTIONS //
 
/**
* Broadcasts arguments to a loop shape.
*
* @private
* @param {Array} args - arguments to broadcast
* @param {ndarray} x - reference ndarray for shape
* @param {Object} [options] - function options
* @returns {Array} broadcasted arguments
*/
function broadcastArgs( args, x, options ) {
	var loopShape;
	var sh;
	var d;
	var i;
 
	sh = getShape( x );
	if ( isObject( options ) && options.dims ) {
		d = options.dims;
	} else {
		d = zeroTo( sh.length );
	}
	loopShape = takeIndexed( sh, indicesComplement( sh.length, d ) );
	for ( i = 0; i < args.length; i++ ) {
		args[ i ] = broadcast( args[ i ], loopShape );
	}
	return args;
}
 
 
// MAIN //
 
/**
* Computes the covariance of two ndarrays provided known means and using a one-pass textbook algorithm.
*
* @name covarmtk
* @type {Function}
* @param {ndarray} x - first input ndarray
* @param {ndarray} y - second input ndarray
* @param {ndarray} correction - zero-dimensional ndarray specifying the degrees of freedom adjustment
* @param {ndarray} meanx - zero-dimensional ndarray specifying the mean of the first input ndarray
* @param {ndarray} meany - zero-dimensional ndarray specifying the mean of the second input ndarray
* @param {Options} [options] - function options
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction
* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions
* @param {*} [options.dtype] - output ndarray data type
* @throws {TypeError} first argument must be an ndarray-like object
* @throws {TypeError} second argument must be an ndarray-like object
* @throws {TypeError} third argument must be an ndarray-like object
* @throws {TypeError} fourth argument must be an ndarray-like object
* @throws {TypeError} fifth argument must be an ndarray-like object
* @throws {TypeError} options argument must be an object
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
* @throws {Error} must provide valid options
* @returns {ndarray} output ndarray
*/
function covarmtk( x, y, correction, meanx, meany, options ) {
	var args = broadcastArgs( [ correction, meanx, meany ], x, options );
	if ( arguments.length < 6 ) {
		return dispatcher( x, y, args[ 0 ], args[ 1 ], args[ 2 ] );
	}
	return dispatcher( x, y, args[ 0 ], args[ 1 ], args[ 2 ], options );
}
 
/**
* Computes the covariance of two ndarrays provided known means and using a one-pass textbook algorithm and assigns results to a provided output ndarray.
*
* @private
* @name assign
* @memberof covarmtk
* @type {Function}
* @param {ndarray} x - first input ndarray
* @param {ndarray} y - second input ndarray
* @param {ndarray} correction - zero-dimensional ndarray specifying the degrees of freedom adjustment
* @param {ndarray} meanx - zero-dimensional ndarray specifying the mean of the first input ndarray
* @param {ndarray} meany - zero-dimensional ndarray specifying the mean of the second input ndarray
* @param {ndarray} out - output ndarray
* @param {Options} [options] - function options
* @throws {TypeError} first argument must be an ndarray-like object
* @throws {TypeError} second argument must be an ndarray-like object
* @throws {TypeError} third argument must be an ndarray-like object
* @throws {TypeError} fourth argument must be an ndarray-like object
* @throws {TypeError} fifth argument must be an ndarray-like object
* @throws {TypeError} sixth argument must be an ndarray-like object
* @throws {TypeError} options argument must be an object
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
* @throws {Error} must provide valid options
* @returns {ndarray} output ndarray
*/
function assign( x, y, correction, meanx, meany, out, options ) {
	var args = broadcastArgs( [ correction, meanx, meany ], x, options );
	if ( arguments.length < 7 ) {
		return dispatcher.assign( x, y, args[ 0 ], args[ 1 ], args[ 2 ], out );
	}
	return dispatcher.assign( x, y, args[ 0 ], args[ 1 ], args[ 2 ], out, options ); // eslint-disable-line max-len
}
 
// Set the `assign` method:
setReadOnly( covarmtk, 'assign', assign );
 
 
// EXPORTS //
 
module.exports = covarmtk;