README.md (5562B)
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 # dnannsumors 22 23 > Calculate the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var dnannsumors = require( '@stdlib/blas/ext/base/dnannsumors' ); 37 ``` 38 39 #### dnannsumors( N, x, strideX, out, strideOut ) 40 41 Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation. 42 43 ```javascript 44 var Float64Array = require( '@stdlib/array/float64' ); 45 46 var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); 47 var out = new Float64Array( 2 ); 48 49 var v = dnannsumors( x.length, x, 1, out, 1 ); 50 // returns <Float64Array>[ 1.0, 3 ] 51 ``` 52 53 The function has the following parameters: 54 55 - **N**: number of indexed elements. 56 - **x**: input [`Float64Array`][@stdlib/array/float64]. 57 - **strideX**: index increment for `x`. 58 - **out**: output [`Float64Array`][@stdlib/array/float64] whose first element is the sum and whose second element is the number of non-NaN elements. 59 - **strideOut**: index increment for `out`. 60 61 The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the sum of every other element in `x`, 62 63 ```javascript 64 var Float64Array = require( '@stdlib/array/float64' ); 65 var floor = require( '@stdlib/math/base/special/floor' ); 66 67 var x = new Float64Array( [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ] ); 68 var out = new Float64Array( 2 ); 69 var N = floor( x.length / 2 ); 70 71 var v = dnannsumors( N, x, 2, out, 1 ); 72 // returns <Float64Array>[ 5.0, 2 ] 73 ``` 74 75 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 76 77 <!-- eslint-disable stdlib/capitalized-comments --> 78 79 ```javascript 80 var Float64Array = require( '@stdlib/array/float64' ); 81 var floor = require( '@stdlib/math/base/special/floor' ); 82 83 var x0 = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 84 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 85 86 var out0 = new Float64Array( 4 ); 87 var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element 88 89 var N = floor( x0.length / 2 ); 90 91 var v = dnannsumors( N, x1, 2, out1, 1 ); 92 // returns <Float64Array>[ 5.0, 4 ] 93 ``` 94 95 #### dnannsumors.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut ) 96 97 Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics. 98 99 ```javascript 100 var Float64Array = require( '@stdlib/array/float64' ); 101 102 var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); 103 var out = new Float64Array( 2 ); 104 105 var v = dnannsumors.ndarray( x.length, x, 1, 0, out, 1, 0 ); 106 // returns <Float64Array>[ 1.0, 3 ] 107 ``` 108 109 The function has the following additional parameters: 110 111 - **offsetX**: starting index for `x`. 112 - **offsetOut**: starting index for `out`. 113 114 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 115 116 ```javascript 117 var Float64Array = require( '@stdlib/array/float64' ); 118 var floor = require( '@stdlib/math/base/special/floor' ); 119 120 var x = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 121 var out = new Float64Array( 4 ); 122 var N = floor( x.length / 2 ); 123 124 var v = dnannsumors.ndarray( N, x, 2, 1, out, 2, 1 ); 125 // returns <Float64Array>[ 0.0, 5.0, 0.0, 4 ] 126 ``` 127 128 </section> 129 130 <!-- /.usage --> 131 132 <section class="notes"> 133 134 ## Notes 135 136 - If `N <= 0`, both functions return a sum equal to `0.0`. 137 - Ordinary recursive summation (i.e., a "simple" sum) is performant, but can incur significant numerical error. If performance is paramount and error tolerated, using ordinary recursive summation is acceptable; in all other cases, exercise due caution. 138 139 </section> 140 141 <!-- /.notes --> 142 143 <section class="examples"> 144 145 ## Examples 146 147 <!-- eslint no-undef: "error" --> 148 149 ```javascript 150 var randu = require( '@stdlib/random/base/randu' ); 151 var round = require( '@stdlib/math/base/special/round' ); 152 var Float64Array = require( '@stdlib/array/float64' ); 153 var dnannsumors = require( '@stdlib/blas/ext/base/dnannsumors' ); 154 155 var x; 156 var i; 157 158 x = new Float64Array( 10 ); 159 for ( i = 0; i < x.length; i++ ) { 160 if ( randu() < 0.2 ) { 161 x[ i ] = NaN; 162 } else { 163 x[ i ] = round( randu()*100.0 ); 164 } 165 } 166 console.log( x ); 167 168 var out = new Float64Array( 2 ); 169 dnannsumors( x.length, x, 1, out, 1 ); 170 console.log( out ); 171 ``` 172 173 </section> 174 175 <!-- /.examples --> 176 177 <section class="links"> 178 179 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64 180 181 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 182 183 </section> 184 185 <!-- /.links -->