From 89a55a14f4acf10eebde931db934f4ac25b4dd95 Mon Sep 17 00:00:00 2001 From: Rutam Kathale Date: Fri, 8 Mar 2024 01:57:29 +0530 Subject: [PATCH] docs: improve README examples of ndarray/base/assert namespace Updated examples for ndarray/base/assert namespace Fixes: #1586 --- .../@stdlib/ndarray/base/assert/README.md | 77 +++++++++++++++++-- .../ndarray/base/assert/examples/index.js | 75 +++++++++++++++++- 2 files changed, 143 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/assert/README.md b/lib/node_modules/@stdlib/ndarray/base/assert/README.md index e9ed14ae15d6..2e53f2ba7edc 100644 --- a/lib/node_modules/@stdlib/ndarray/base/assert/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/assert/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2018 The Stdlib Authors. +Copyright (c) 2024 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. @@ -86,10 +86,77 @@ var o = ns; ```javascript -var objectKeys = require( '@stdlib/utils/keys' ); -var ns = require( '@stdlib/ndarray/base/assert' ); - -console.log( objectKeys( ns ) ); +var Float64Array = require('@stdlib/array/float64'); +var ndarray = require('@stdlib/ndarray').ndarray; +var randn = require('@stdlib/random/base/randn'); +var sqrt = require('@stdlib/math/base/special/sqrt'); +var pow = require('@stdlib/math/base/special/pow'); +var erf = require('@stdlib/math/base/special/erf'); +var cdf = require('@stdlib/stats/base/dists/normal/cdf'); +var isContiguous = require('@stdlib/ndarray/base/assert/is-contiguous'); + +/* +* Let's take an example of generating a 5x5 multidimensional array filled with random numbers from a normal distribution. +* We want to calculate some statistical properties of this data, such as the mean and variance, and use some special mathematical functions to analyze the data further. +*/ + +var varianceValue; +var meanValue; +var sdValue; +var erfValue; +var cdfValue; +var strides; +var arrLen; +var shape; +var data; +var array; +var i; + +function calculateVariance(acc, val) { + return acc + pow(val - meanValue, 2); +} + +function calculateMean(acc, val) { + return acc + val; +} + +shape = [5, 5]; +data = new Float64Array(shape[0] * shape[1]); +for (i = 0; i < data.length; i++) { + // Fill the array with random numbers from a normal distribution. + data[i] = randn(); +} + +strides = [shape[1], 1]; +array = ndarray('float64', data, shape, strides, 0, 'row-major'); + +// Check if the array is contiguous and of the correct data type +if (isContiguous(array.shape, array.strides, array.offset)) { + // Calculate the mean of the array data which gives us a measure of the central tendency of the data. + arrLen = array.data.length; + meanValue = array.data.reduce(calculateMean, 0) / arrLen; + + // Calculate the variance of the array data which gives us a measure of how much the data varies from the mean. + varianceValue = array.data.reduce(calculateVariance, 0) / arrLen; + + // Calculate the standard deviation of the array data which, being the square root of the variance, gives us a measure of the spread of the data around the mean. + sdValue = sqrt(varianceValue); + + // Calculate the error function at the mean. The error function is used in statistics to find probabilities in normal distributions. + erfValue = erf(meanValue); + + // Calculate the cumulative distribution function at the mean which gives us the probability that a random variable is less than or equal to a certain value. + cdfValue = cdf(meanValue, 0, sdValue); + + console.log(true); + console.log(meanValue); + console.log(varianceValue); + console.log(sdValue); + console.log(erfValue); + console.log(cdfValue); +} else { + console.log(false); +} ``` diff --git a/lib/node_modules/@stdlib/ndarray/base/assert/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/assert/examples/index.js index c7e940d87f24..8fc5e21a96ac 100644 --- a/lib/node_modules/@stdlib/ndarray/base/assert/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/assert/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* Copyright (c) 2024 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. @@ -18,7 +18,74 @@ 'use strict'; -var objectKeys = require( '@stdlib/utils/keys' ); -var ns = require( './../lib' ); +var Float64Array = require('@stdlib/array/float64'); +var ndarray = require('@stdlib/ndarray').ndarray; +var randn = require('@stdlib/random/base/randn'); +var sqrt = require('@stdlib/math/base/special/sqrt'); +var pow = require('@stdlib/math/base/special/pow'); +var erf = require('@stdlib/math/base/special/erf'); +var cdf = require('@stdlib/stats/base/dists/normal/cdf'); +var isContiguous = require( './../is-contiguous/lib' ); -console.log( objectKeys( ns ) ); +/* +* Let's take an example of generating a 5x5 multidimensional array filled with random numbers from a normal distribution. +* We want to calculate some statistical properties of this data, such as the mean and variance, and use some special mathematical functions to analyze the data further. +*/ + +var varianceValue; +var meanValue; +var sdValue; +var erfValue; +var cdfValue; +var strides; +var arrLen; +var shape; +var data; +var array; +var i; + +function calculateVariance(acc, val) { + return acc + pow(val - meanValue, 2); +} + +function calculateMean(acc, val) { + return acc + val; +} + +shape = [5, 5]; +data = new Float64Array(shape[0] * shape[1]); +for (i = 0; i < data.length; i++) { + // Fill the array with random numbers from a normal distribution. + data[i] = randn(); +} + +strides = [shape[1], 1]; +array = ndarray('float64', data, shape, strides, 0, 'row-major'); + +// Check if the array is contiguous and of the correct data type +if (isContiguous(array.shape, array.strides, array.offset)) { + // Calculate the mean of the array data which gives us a measure of the central tendency of the data. + arrLen = array.data.length; + meanValue = array.data.reduce(calculateMean, 0) / arrLen; + + // Calculate the variance of the array data which gives us a measure of how much the data varies from the mean. + varianceValue = array.data.reduce(calculateVariance, 0) / arrLen; + + // Calculate the standard deviation of the array data which, being the square root of the variance, gives us a measure of the spread of the data around the mean. + sdValue = sqrt(varianceValue); + + // Calculate the error function at the mean. The error function is used in statistics to find probabilities in normal distributions. + erfValue = erf(meanValue); + + // Calculate the cumulative distribution function at the mean which gives us the probability that a random variable is less than or equal to a certain value. + cdfValue = cdf(meanValue, 0, sdValue); + + console.log(true); + console.log(meanValue); + console.log(varianceValue); + console.log(sdValue); + console.log(erfValue); + console.log(cdfValue); +} else { + console.log(false); +}