README.md (5982B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2020 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # dsnannsumors 22 23 > Calculate the sum of single-precision floating-point strided array elements, ignoring `NaN` values, using ordinary recursive summation with extended accumulation, and returning an extended precision result. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var dsnannsumors = require( '@stdlib/blas/ext/base/dsnannsumors' ); 37 ``` 38 39 #### dsnannsumors( N, x, strideX, out, strideOut ) 40 41 Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values, using ordinary recursive summation with extended accumulation, and returning an extended precision result. 42 43 ```javascript 44 var Float32Array = require( '@stdlib/array/float32' ); 45 var Float64Array = require( '@stdlib/array/float64' ); 46 47 var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] ); 48 var out = new Float64Array( 2 ); 49 50 var v = dsnannsumors( x.length, x, 1, out, 1 ); 51 // returns <Float64Array>[ 1.0, 3 ] 52 ``` 53 54 The function has the following parameters: 55 56 - **N**: number of indexed elements. 57 - **x**: input [`Float32Array`][@stdlib/array/float32]. 58 - **strideX**: index increment for `x`. 59 - **out**: output [`Float64Array`][@stdlib/array/float64] whose first element is the sum and whose second element is the number of non-NaN elements. 60 - **strideOut**: index increment for `out`. 61 62 The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the sum of every other element in `x`, 63 64 ```javascript 65 var Float32Array = require( '@stdlib/array/float32' ); 66 var Float64Array = require( '@stdlib/array/float64' ); 67 var floor = require( '@stdlib/math/base/special/floor' ); 68 69 var x = new Float32Array( [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ] ); 70 var out = new Float64Array( 2 ); 71 var N = floor( x.length / 2 ); 72 73 var v = dsnannsumors( N, x, 2, out, 1 ); 74 // returns <Float64Array>[ 5.0, 2 ] 75 ``` 76 77 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 78 79 <!-- eslint-disable stdlib/capitalized-comments --> 80 81 ```javascript 82 var Float32Array = require( '@stdlib/array/float32' ); 83 var Float64Array = require( '@stdlib/array/float64' ); 84 var floor = require( '@stdlib/math/base/special/floor' ); 85 86 var x0 = new Float32Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 87 var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 88 89 var out0 = new Float64Array( 4 ); 90 var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element 91 92 var N = floor( x0.length / 2 ); 93 94 var v = dsnannsumors( N, x1, 2, out1, 1 ); 95 // returns <Float64Array>[ 5.0, 4 ] 96 ``` 97 98 #### dsnannsumors.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut ) 99 100 Computes the sum of single-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation with extended accumulation and alternative indexing semantics. 101 102 ```javascript 103 var Float32Array = require( '@stdlib/array/float32' ); 104 var Float64Array = require( '@stdlib/array/float64' ); 105 106 var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] ); 107 var out = new Float64Array( 2 ); 108 109 var v = dsnannsumors.ndarray( x.length, x, 1, 0, out, 1, 0 ); 110 // returns <Float64Array>[ 1.0, 3 ] 111 ``` 112 113 The function has the following additional parameters: 114 115 - **offsetX**: starting index for `x`. 116 - **offsetOut**: starting index for `out`. 117 118 While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the sum of every other value in `x` starting from the second value 119 120 ```javascript 121 var Float32Array = require( '@stdlib/array/float32' ); 122 var Float64Array = require( '@stdlib/array/float64' ); 123 var floor = require( '@stdlib/math/base/special/floor' ); 124 125 var x = new Float32Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 126 var out = new Float64Array( 4 ); 127 var N = floor( x.length / 2 ); 128 129 var v = dsnannsumors.ndarray( N, x, 2, 1, out, 2, 1 ); 130 // returns <Float64Array>[ 0.0, 5.0, 0.0, 4 ] 131 ``` 132 133 </section> 134 135 <!-- /.usage --> 136 137 <section class="notes"> 138 139 ## Notes 140 141 - If `N <= 0`, both functions return a sum equal to `0.0`. 142 - Accumulated intermediate values are stored as double-precision floating-point numbers. 143 144 </section> 145 146 <!-- /.notes --> 147 148 <section class="examples"> 149 150 ## Examples 151 152 <!-- eslint no-undef: "error" --> 153 154 ```javascript 155 var randu = require( '@stdlib/random/base/randu' ); 156 var round = require( '@stdlib/math/base/special/round' ); 157 var Float32Array = require( '@stdlib/array/float32' ); 158 var Float64Array = require( '@stdlib/array/float64' ); 159 var dsnannsumors = require( '@stdlib/blas/ext/base/dsnannsumors' ); 160 161 var x; 162 var i; 163 164 x = new Float32Array( 10 ); 165 for ( i = 0; i < x.length; i++ ) { 166 if ( randu() < 0.2 ) { 167 x[ i ] = NaN; 168 } else { 169 x[ i ] = round( randu()*100.0 ); 170 } 171 } 172 console.log( x ); 173 174 var out = new Float64Array( 2 ); 175 dsnannsumors( x.length, x, 1, out, 1 ); 176 console.log( out ); 177 ``` 178 179 </section> 180 181 <!-- /.examples --> 182 183 <section class="links"> 184 185 [@stdlib/array/float32]: https://www.npmjs.com/package/@stdlib/array-float32 186 187 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64 188 189 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 190 191 </section> 192 193 <!-- /.links -->