All files / kernel-betaincinv/lib newton_raphson.js

35.26% Statements 61/173
100% Branches 1/1
0% Functions 0/1
35.26% Lines 61/173

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 169 170 171 172 173 1741x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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.
*
*
* ## Notice
*
* The original C++ code and copyright notice are from the [Boost library]{@link http://www.boost.org/doc/libs/1_92_0/boost/math/tools/roots.hpp}. The implementation has been modified for JavaScript.
*
* ```text
* Copyright John Maddock 2006.
* Copyright Matt Borland 2024.
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
* ```
*/
 
'use strict';
 
// MODULES //
 
var sign = require( '@stdlib/math/base/special/signum' );
var abs = require( '@stdlib/math/base/special/abs' );
var ldexp = require( '@stdlib/math/base/special/ldexp' );
var MAX_VALUE = require( '@stdlib/constants/float64/max' );
 
 
// MAIN //
 
/**
* Performs root finding via second order Newton-Raphson iteration.
*
* @private
* @param {Function} fun - function which returns the function value and its first derivative
* @param {number} guess - initial starting value
* @param {number} min - minimum possible value for the result, used as initial lower bracket
* @param {number} max - maximum possible value for the result, used as initial upper bracket
* @param {PositiveInteger} digits - desired number of binary digits
* @param {PositiveInteger} maxIter - maximum number of iterations
* @returns {number} function value
*/
function newtonRaphsonIterate( fun, guess, min, max, digits, maxIter ) {
	var maxRangeF;
	var minRangeF;
	var f0last;
	var delta1;
	var delta2;
	var factor;
	var result;
	var count;
	var delta;
	var shift;
	var res;
	var f0;
	var f1;

	f0 = 0.0;
	f0last = 0.0;
	result = guess;

	factor = ldexp( 1.0, 1.0 - digits );
	delta = MAX_VALUE;
	delta1 = MAX_VALUE;
	delta2 = MAX_VALUE;

	// We use these to sanity check that we do actually bracket a root; if we update both ends of the brackets and the function value has the same sign at both ends, then there is no root to be found:
	maxRangeF = 0.0;
	minRangeF = 0.0;

	count = maxIter;
	do {
		f0last = f0;
		delta2 = delta1;
		delta1 = delta;
		res = fun(result);
		f0 = res[ 0 ];
		f1 = res[ 1 ];
		count -= 1;
		if ( f0 === 0.0 ) {
			break;
		}
		if ( f1 === 0.0 ) {
			// Oops zero derivative!!!
			if ( f0last === 0.0 ) {
				// Must be the first iteration, pretend that we had a previous one at either min or max:
				if ( result === min ) {
					guess = max;
				} else {
					guess = min;
				}
				f0last = fun( guess )[ 0 ];
				delta = guess - result;
			}
			if ( sign( f0last ) * sign( f0 ) < 0 ) {
				// We've crossed over so move in opposite direction to last step:
				if ( delta < 0 ) {
					delta = (result - min) / 2.0;
				} else {
					delta = (result - max) / 2.0;
				}
			} else if ( delta < 0 ) {
				delta = (result - max) / 2.0;
			} else {
				delta = (result - min) / 2.0;
			}
		} else {
			delta = f0 / f1;
		}
		if ( abs( delta * 2.0 ) > abs( delta2 ) ) {
			// Last two steps haven't converged:
			shift = ( delta > 0.0 ) ? (result-min) / 2.0 : (result-max) / 2.0;
			if ( result !== 0.0 && abs( shift ) > abs( result ) ) {
				delta = sign( delta ) * abs( result ); // Protect against huge jumps!
			} else {
				delta = shift;
			}
			// Reset delta1/delta2 so that this branch will *not* be taken on the next iteration:
			delta1 = 3.0 * delta;
			delta2 = 3.0 * delta;
		}
		guess = result;
		result -= delta;
		if ( result <= min ) {
			delta = 0.5 * (guess - min);
			result = guess - delta;
			if ( result === min || result === max ) {
				break;
			}
		} else if ( result >= max ) {
			delta = 0.5 * (guess - max);
			result = guess - delta;
			if ( result === min || result === max ) {
				break;
			}
		}
		// Update brackets:
		if ( delta > 0.0 ) {
			max = guess;
			maxRangeF = f0;
		} else {
			min = guess;
			minRangeF = f0;
		}
		// Sanity check that we bracket the root:
		if ( maxRangeF * minRangeF > 0.0 ) {
			// There appears to be no root to be found (the original C++ implementation raises an evaluation error; we instead return the current best guess):
			return guess;
		}
	}
	while ( count && ( abs( result * factor ) < abs( delta ) ) );

	return result;
}
 
 
// EXPORTS //
 
module.exports = newtonRaphsonIterate;