addon.cpp (4440B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include "stdlib/stats/base/dvarmtk.h" 20 #include <node_api.h> 21 #include <stdint.h> 22 #include <stdlib.h> 23 #include <stdbool.h> 24 #include <assert.h> 25 26 /** 27 * Add-on namespace. 28 */ 29 namespace stdlib_stats_base_dvarmtk { 30 31 /** 32 * Computes the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm. 33 * 34 * ## Notes 35 * 36 * - When called from JavaScript, the function expects five arguments: 37 * 38 * - `N`: number of indexed elements 39 * - `mean`: mean 40 * - `correction`: degrees of freedom adjustment 41 * - `X`: input array 42 * - `stride`: stride length 43 */ 44 napi_value node_dvarmtk( napi_env env, napi_callback_info info ) { 45 napi_status status; 46 47 size_t argc = 5; 48 napi_value argv[ 5 ]; 49 status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); 50 assert( status == napi_ok ); 51 52 if ( argc < 5 ) { 53 napi_throw_error( env, nullptr, "invalid invocation. Must provide 5 arguments." ); 54 return nullptr; 55 } 56 57 napi_valuetype vtype0; 58 status = napi_typeof( env, argv[ 0 ], &vtype0 ); 59 assert( status == napi_ok ); 60 if ( vtype0 != napi_number ) { 61 napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); 62 return nullptr; 63 } 64 65 napi_valuetype vtype1; 66 status = napi_typeof( env, argv[ 1 ], &vtype1 ); 67 assert( status == napi_ok ); 68 if ( vtype1 != napi_number ) { 69 napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a number." ); 70 return nullptr; 71 } 72 73 napi_valuetype vtype2; 74 status = napi_typeof( env, argv[ 2 ], &vtype2 ); 75 assert( status == napi_ok ); 76 if ( vtype2 != napi_number ) { 77 napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a number." ); 78 return nullptr; 79 } 80 81 bool res; 82 status = napi_is_typedarray( env, argv[ 3 ], &res ); 83 assert( status == napi_ok ); 84 if ( res == false ) { 85 napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a Float64Array." ); 86 return nullptr; 87 } 88 89 napi_valuetype vtype4; 90 status = napi_typeof( env, argv[ 4 ], &vtype4 ); 91 assert( status == napi_ok ); 92 if ( vtype4 != napi_number ) { 93 napi_throw_type_error( env, nullptr, "invalid argument. Fifth argument must be a number." ); 94 return nullptr; 95 } 96 97 int64_t N; 98 status = napi_get_value_int64( env, argv[ 0 ], &N ); 99 assert( status == napi_ok ); 100 101 double mean; 102 status = napi_get_value_double( env, argv[ 1 ], &mean ); 103 assert( status == napi_ok ); 104 105 double correction; 106 status = napi_get_value_double( env, argv[ 2 ], &correction ); 107 assert( status == napi_ok ); 108 109 int64_t stride; 110 status = napi_get_value_int64( env, argv[ 4 ], &stride ); 111 assert( status == napi_ok ); 112 113 napi_typedarray_type vtype3; 114 size_t xlen; 115 void *X; 116 status = napi_get_typedarray_info( env, argv[ 3 ], &vtype3, &xlen, &X, nullptr, nullptr ); 117 assert( status == napi_ok ); 118 if ( vtype3 != napi_float64_array ) { 119 napi_throw_type_error( env, nullptr, "invalid argument. Fourth argument must be a Float64Array." ); 120 return nullptr; 121 } 122 if ( (N-1)*llabs(stride) >= (int64_t)xlen ) { 123 napi_throw_range_error( env, nullptr, "invalid argument. Fourth argument has insufficient elements based on the associated stride and the number of indexed elements." ); 124 return nullptr; 125 } 126 127 napi_value v; 128 status = napi_create_double( env, stdlib_strided_dvarmtk( N, mean, correction, (double *)X, stride ), &v ); 129 assert( status == napi_ok ); 130 131 return v; 132 } 133 134 napi_value Init( napi_env env, napi_value exports ) { 135 napi_status status; 136 napi_value fcn; 137 status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_dvarmtk, NULL, &fcn ); 138 assert( status == napi_ok ); 139 return fcn; 140 } 141 142 NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) 143 } // end namespace stdlib_stats_base_dvarmtk