diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/README.md b/lib/node_modules/@stdlib/optimize/base/brentq/README.md new file mode 100644 index 000000000000..827fda41b67b --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/README.md @@ -0,0 +1,117 @@ + + +# brentq + +> Find a zero of a continuous function using Brent's method. + +
+ +Finds a zero of a continuous function $f$ on the interval $\lbrack a, b \rbrack$ where the sign of $f(a)$ and $f(b)$ must be opposite. + +
+ + + +
+ +## Usage + +```javascript +var brentq = require( '@stdlib/optimize/base/brentq' ); +``` + +#### brentq( f, a, b, maxIter, xtol, rtol, args ) + +Finds a zero of a continuous function `f` on the interval `[a, b]`. + +```javascript +function f( x ) { + return ( x * x ) - 1.0; +} + +var out = brentq( f, 0.0, 2.0, 100, 2.0e-12, 8.88e-16, [] ); +var root = out.root; +// returns 1.0 +``` + +The function returns a solution object with the following properties: + +- **root**: the root. +- **iterations**: number of iterations. +- **function_calls**: number of function calls. +- **converged**: boolean indicating if convergence was reached. +- **flag**: convergence condition message. +- **method**: method name. + +The function has the following parameters: + +- **f**: objective function `f(x, ...args)`. +- **a**: lower bound. +- **b**: upper bound. +- **maxIter**: maximum number of iterations. +- **xtol**: absolute tolerance. +- **rtol**: relative tolerance. +- **args**: array of arguments to be passed to `f`. + +
+ + + +
+ +## Examples + + + +```javascript +var sin = require( '@stdlib/math/base/special/sin' ); +var brentq = require( '@stdlib/optimize/base/brentq' ); + +function f( x ) { + return sin( x ); +} + +var out = brentq( f, 3.0, 3.2, 100, 2e-12, 8.88e-16, [] ); +var root = out.root; + +console.log( root ); +// => 3.141592653589793 +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/benchmark/benchmark.js b/lib/node_modules/@stdlib/optimize/base/brentq/benchmark/benchmark.js new file mode 100644 index 000000000000..152f4e757d6a --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/benchmark/benchmark.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var brentq = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var root; + var i; + + function f( x ) { + return ( x * x ) - 1.0; + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + root = brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); + if ( isnan( root ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( root ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt b/lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt new file mode 100644 index 000000000000..8ce306bfdf27 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt @@ -0,0 +1,43 @@ + +{{alias}}( f, a, b, maxIter, xtol, rtol, args ) + Finds a zero of a continuous function `f` on the interval `[a, b]`. + + Parameters + ---------- + f: Function + Objective function. + + a: number + Lower bound. + + b: number + Upper bound. + + maxIter: integer + Maximum number of iterations. + + xtol: number + Absolute tolerance. + + rtol: number + Relative tolerance. + + args: Array + Callback function arguments. + + Returns + ------- + results: Object + Solution object. + + Examples + -------- + > function f( x ) { return x*x - 1.0; }; + > var out = {{alias}}( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); + > out.root + 1.0 + + See Also + -------- + + diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/index.d.ts b/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/index.d.ts new file mode 100644 index 000000000000..51ff0086ee7f --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/index.d.ts @@ -0,0 +1,87 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +// TypeScript Version: 4.1 + +/** +* Callback function. +* +* @param x - input value +* @param args - callback function arguments +* @returns function value +*/ +type Callback = ( x: number, ...args: Array ) => number; + +/** +* Solution object. +*/ +interface Results { + /** + * The root. + */ + root: number; + + /** + * Number of iterations. + */ + iterations: number; + + /** + * Number of function calls. + */ + function_calls: number; + + /** + * Boolean indicating if convergence was reached. + */ + converged: boolean; + + /** + * Convergence condition message. + */ + flag: string; + + /** + * Method name. + */ + method: string; +} + +/** +* Finds a zero of a continuous function `f` on the interval `[a, b]`. +* +* @param f - objective function +* @param a - lower bound +* @param b - upper bound +* @param maxIter - maximum number of iterations +* @param xtol - absolute tolerance +* @param rtol - relative tolerance +* @param args - callback function arguments +* @returns solution object +* +* @example +* function f( x ) { +* return x * x - 1.0; +* } +* var out = brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); +* var root = out.root; +* // returns 1.0 +*/ +declare function brentq( f: Callback, a: number, b: number, maxIter: number, xtol: number, rtol: number, args: Array ): Results; + +export = brentq; diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts b/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts new file mode 100644 index 000000000000..74c09fe16b3e --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts @@ -0,0 +1,109 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +import brentq = require( './index' ); + +/** +* Callback function. +* +* @param x - input value +* @returns result +*/ +function f( x: number ): number { + return x * x - 1.0; +} + +// TESTS // + +// The function returns a solution object... +{ + brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectType Results + brentq( ( x: number, y: number ) => x * x - y, 0.0, 2.0, 100, 2e-12, 8.88e-16, [ 1.0 ] ); // $ExpectType Results +} + +// The compiler throws an error if the function is provided an invalid first argument... +{ + brentq( true, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( false, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( 5, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( [], 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( {}, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( 'abc', 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid second argument... +{ + brentq( f, true, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, false, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, [], 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, {}, 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 'abc', 2.0, 100, 2e-12, 8.88e-16, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid third argument... +{ + brentq( f, 0.0, true, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, false, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, [], 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, {}, 100, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 'abc', 100, 2e-12, 8.88e-16, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid fourth argument... +{ + brentq( f, 0.0, 2.0, true, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 2.0, false, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 2.0, [], 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 2.0, {}, 2e-12, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 'abc', 2e-12, 8.88e-16, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid fifth argument... +{ + brentq( f, 0.0, 2.0, 100, true, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 100, false, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 100, [], 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 100, {}, 8.88e-16, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 'abc', 8.88e-16, [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid sixth argument... +{ + brentq( f, 0.0, 2.0, 100, 2e-12, true, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, false, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, [], [] ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, {}, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, 'abc', [] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid seventh argument... +{ + brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, true ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, false ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, {} ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, 'abc' ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, 123 ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + brentq( f, 0.0, 2.0 ); // $ExpectError + brentq( f, 0.0, 2.0, 100 ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12 ); // $ExpectError + brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/examples/index.js b/lib/node_modules/@stdlib/optimize/base/brentq/examples/index.js new file mode 100644 index 000000000000..fc56d962e42b --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +var pow = require( '@stdlib/math/base/special/pow' ); +var sin = require( '@stdlib/math/base/special/sin' ); +var brentq = require( './../lib' ); + +function f( x ) { + return pow( x, 3 ) - ( 2.0 * x ) - 5.0; +} + +var out = brentq( f, 2.0, 3.0, 100, 2e-12, 8.88e-16, [] ); +console.log( 'f(x) = x^3 - 2x - 5' ); +console.log( 'Root: %d', out.root ); +console.log( 'f(root): %d', f( out.root ) ); +console.log( 'Iterations: %d', out.iterations ); + +// Example with options +function g( x ) { + return sin( x ); +} + +out = brentq( g, 3.0, 4.0, 100, 1e-4, 8.88e-16, [] ); +console.log( '\nf(x) = sin(x)' ); +console.log( 'Root matching 3.14 on [3, 4] with xtol 1e-4: %d', out.root ); +console.log( 'f(root): %d', g( out.root ) ); +console.log( 'Iterations: %d', out.iterations ); diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/lib/index.js b/lib/node_modules/@stdlib/optimize/base/brentq/lib/index.js new file mode 100644 index 000000000000..893efb1b41cc --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +/** +* Find a zero of a continuous function using Brent's method. +* +* @module @stdlib/optimize/base/brentq +* +* @example +* var sin = require( '@stdlib/math/base/special/sin' ); +* var brentq = require( '@stdlib/optimize/base/brentq' ); +* +* function f( x ) { +* return sin( x ); +* } +* +* var out = brentq( f, 3.0, 4.0, 100, 1.0e-12, 8.88e-16, [] ); +* var root = out.root; +* console.log( root ); +* // => ~3.14 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/lib/main.js b/lib/node_modules/@stdlib/optimize/base/brentq/lib/main.js new file mode 100644 index 000000000000..7ff3c96a4621 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/lib/main.js @@ -0,0 +1,242 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var isNumber = require( '@stdlib/assert/is-number' ); +var isArray = require( '@stdlib/assert/is-array' ); +var format = require( '@stdlib/string/format' ); +var abs = require( '@stdlib/math/base/special/abs' ); + + +// MAIN // + +/** +* Finds a zero of a continuous function `f` on the interval `[a, b]`. +* +* @param {Function} f - objective function +* @param {number} a - lower bound +* @param {number} b - upper bound +* @param {number} maxIter - maximum number of iterations +* @param {number} xtol - absolute tolerance +* @param {number} rtol - relative tolerance +* @param {Array} args - array of arguments to be passed to f +* @throws {TypeError} first argument must be a function +* @throws {TypeError} second argument must be a number +* @throws {TypeError} third argument must be a number +* @throws {TypeError} fourth argument must be a number +* @throws {TypeError} fifth argument must be a number +* @throws {TypeError} sixth argument must be a number +* @throws {TypeError} seventh argument must be an array +* @throws {Error} function values at the interval endpoints must have opposite signs +* @returns {Object} solution object +* +* @example +* function f( x ) { +* return x * x - 1.0; +* } +* var out = brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); +* // returns { 'root': 1.0, ... } +*/ +function brentq( f, a, b, maxIter, xtol, rtol, args ) { + var funcalls; + var delta; + var nargs; + var vargs; + var xpre; + var xcur; + var xblk; + var fpre; + var fcur; + var fblk; + var spre; + var scur; + var sbis; + var stry; + var dpre; + var dblk; + var i; + + if ( !isFunction( f ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', f ) ); + } + if ( !isNumber( a ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a number. Value: `%s`.', a ) ); + } + if ( !isNumber( b ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a number. Value: `%s`.', b ) ); + } + if ( !isNumber( maxIter ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be a number. Value: `%s`.', maxIter ) ); + } + if ( !isNumber( xtol ) ) { + throw new TypeError( format( 'invalid argument. Fifth argument must be a number. Value: `%s`.', xtol ) ); + } + if ( !isNumber( rtol ) ) { + throw new TypeError( format( 'invalid argument. Sixth argument must be a number. Value: `%s`.', rtol ) ); + } + if ( !isArray( args ) ) { + throw new TypeError( format( 'invalid argument. Seventh argument must be an array. Value: `%s`.', args ) ); + } + + xpre = a; + xcur = b; + xblk = 0.0; + fblk = 0.0; + spre = 0.0; + scur = 0.0; + + nargs = args.length; + vargs = [ 0.0 ]; + for ( i = 0; i < nargs; i++ ) { + vargs.push( args[ i ] ); + } + + vargs[ 0 ] = xpre; + fpre = f.apply( null, vargs ); + + vargs[ 0 ] = xcur; + fcur = f.apply( null, vargs ); + funcalls = 2; + + if ( fpre === 0.0 ) { + return { + 'root': xpre, + 'iterations': 0, + 'function_calls': funcalls, + 'converged': true, + 'flag': 'converged', + 'method': 'brentq' + }; + } + if ( fcur === 0.0 ) { + return { + 'root': xcur, + 'iterations': 0, + 'function_calls': funcalls, + 'converged': true, + 'flag': 'converged', + 'method': 'brentq' + }; + } + if ( ( fpre > 0.0 && fcur > 0.0 ) || ( fpre < 0.0 && fcur < 0.0 ) ) { + throw new Error( 'invalid arguments. Function values at the interval endpoints must have opposite signs.' ); + } + + for ( i = 0; i < maxIter; i++ ) { + if ( + fpre !== 0.0 && + fcur !== 0.0 && + ( ( ( fpre > 0.0 ) && ( fcur < 0.0 ) ) || ( ( fpre < 0.0 ) && ( fcur > 0.0 ) ) ) + ) { + // Signs are different + xblk = xpre; + fblk = fpre; + spre = xcur - xpre; + scur = spre; + } + if ( abs( fblk ) < abs( fcur ) ) { + xpre = xcur; + xcur = xblk; + xblk = xpre; + + fpre = fcur; + fcur = fblk; + fblk = fpre; + } + + // delta = ( xtol + rtol * abs(xcur) ) / 2; + delta = ( xtol + ( rtol * abs( xcur ) ) ) / 2.0; + + sbis = ( xblk - xcur ) / 2.0; + + if ( fcur === 0.0 || abs( sbis ) < delta ) { + return { + 'root': xcur, + 'iterations': i + 1, + 'function_calls': funcalls, + 'converged': true, + 'flag': 'converged', + 'method': 'brentq' + }; + } + + if ( abs( spre ) > delta && abs( fcur ) < abs( fpre ) ) { + if ( xpre === xblk ) { + // Interpolate + stry = ( -fcur * ( xcur - xpre ) ) / ( fcur - fpre ); + } else { + // Extrapolate + dpre = ( fpre - fcur ) / ( xpre - xcur ); + dblk = ( fblk - fcur ) / ( xblk - xcur ); + stry = ( -fcur * ( ( fblk * dblk ) - ( fpre * dpre ) ) ) / + ( dblk * dpre * ( fblk - fpre ) ); + } + + // Check if short step is acceptable + // 2*fabs(stry) < MIN(fabs(spre), 3*fabs(sbis) - delta) + if ( + 2.0 * abs( stry ) < + ( ( abs( spre ) < ( ( 3.0 * abs( sbis ) ) - delta ) ) ? + abs( spre ) : + ( ( 3.0 * abs( sbis ) ) - delta ) ) + ) { + // Good short step + spre = scur; + scur = stry; + } else { + // Bisect + spre = sbis; + scur = sbis; + } + } else { + // Bisect + spre = sbis; + scur = sbis; + } + + xpre = xcur; + fpre = fcur; + + if ( abs( scur ) > delta ) { + xcur += scur; + } else { + xcur += ( sbis > 0.0 ) ? delta : -delta; + } + + vargs[ 0 ] = xcur; + fcur = f.apply( null, vargs ); + funcalls += 1; + } + return { + 'root': xcur, + 'iterations': i, + 'function_calls': funcalls, + 'converged': false, + 'flag': 'suboptimal convergence', + 'method': 'brentq' + }; +} + + +// EXPORTS // + +module.exports = brentq; diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/package.json b/lib/node_modules/@stdlib/optimize/base/brentq/package.json new file mode 100644 index 000000000000..7f8af3e04306 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/optimize/base/brentq", + "version": "0.0.0", + "description": "Find a root of a continuous function using Brent's method.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "optimize", + "brent", + "brentq", + "root", + "find", + "zero", + "math", + "mathematics" + ] +} diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/test/test.js b/lib/node_modules/@stdlib/optimize/base/brentq/test/test.js new file mode 100644 index 000000000000..c5a687f75225 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/test/test.js @@ -0,0 +1,328 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +'use strict'; + +var tape = require( 'tape' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var sin = require( '@stdlib/math/base/special/sin' ); +var PI = require( '@stdlib/constants/float64/pi' ); +var brentq = require( './../lib' ); + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.equal( typeof brentq, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an invalid first argument', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + null, + undefined, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + brentq( value, 0.0, 1.0, 100, 2e-12, 8.88e-16, [] ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid second argument', function test( t ) { + var values; + var i; + + function f( x ) { + return x; + } + + values = [ + '5', + true, + null, + undefined, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + brentq( f, value, 1.0, 100, 2e-12, 8.88e-16, [] ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid third argument', function test( t ) { + var values; + var i; + + function f( x ) { + return x; + } + + values = [ + '5', + true, + null, + undefined, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + brentq( f, 0.0, value, 100, 2e-12, 8.88e-16, [] ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid fourth argument', function test( t ) { + var values; + var i; + + function f( x ) { + return x; + } + + values = [ + '5', + true, + null, + undefined, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + brentq( f, 0.0, 1.0, value, 2e-12, 8.88e-16, [] ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid fifth argument', function test( t ) { + var values; + var i; + + function f( x ) { + return x; + } + + values = [ + '5', + true, + null, + undefined, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + brentq( f, 0.0, 1.0, 100, value, 8.88e-16, [] ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid sixth argument', function test( t ) { + var values; + var i; + + function f( x ) { + return x; + } + + values = [ + '5', + true, + null, + undefined, + [], + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + brentq( f, 0.0, 1.0, 100, 2e-12, value, [] ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid seventh argument', function test( t ) { + var values; + var i; + + function f( x ) { + return x; + } + + values = [ + '5', + true, + null, + undefined, + 5, + {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + brentq( f, 0.0, 1.0, 100, 2e-12, 8.88e-16, value ); + }; + } +}); + +tape( 'the function throws an error if function values at endpoints do not have opposite signs', function test( t ) { + function f( x ) { + return x * x; + } + t.throws( function bad() { + brentq( f, 1.0, 2.0, 100, 2e-12, 8.88e-16, [] ); + }, Error, 'throws error' ); + t.end(); +}); + +tape( 'the function finds the root of a polynomial', function test( t ) { + var out; + var tol; + + function f( x ) { + return ( x * x ) - 1.0; + } + + tol = 1.0e-12; + out = brentq( f, 0.0, 2.0, 100, tol, 8.88e-16, [] ); + t.ok( abs( out.root - 1.0 ) <= tol, 'root is within tolerance' ); + t.equal( typeof out.iterations, 'number', 'iterations is a number' ); + t.equal( typeof out.function_calls, 'number', 'function_calls is a number' ); + t.equal( typeof out.converged, 'boolean', 'converged is a boolean' ); + t.equal( typeof out.flag, 'string', 'flag is a string' ); + t.equal( out.method, 'brentq', 'method is brentq' ); + + // Negative range + out = brentq( f, -2.0, 0.0, 100, tol, 8.88e-16, [] ); + t.ok( abs( out.root + 1.0 ) <= tol, 'root is within tolerance' ); + + t.end(); +}); + +tape( 'the function finds the root of sin(x)', function test( t ) { + var out; + var tol; + + function f( x ) { + return sin( x ); + } + + // Root at PI (~3.14159) + tol = 1.0e-12; + out = brentq( f, 3.0, 4.0, 100, tol, 8.88e-16, [] ); + t.ok( abs( out.root - PI ) <= tol, 'root is within tolerance' ); + t.end(); +}); + +tape( 'the function returns the exact root if found immediately (endpoints)', function test( t ) { + var out; + + function f( x ) { + return ( x * x ) - 1.0; + } + out = brentq( f, 1.0, 2.0, 100, 2e-12, 8.88e-16, [] ); + t.equal( out.root, 1.0, 'returns bracket endpoint' ); + + out = brentq( f, 0.0, 1.0, 100, 2e-12, 8.88e-16, [] ); + t.equal( out.root, 1.0, 'returns bracket endpoint' ); + t.end(); +}); + +tape( 'the function respects relative tolerance', function test( t ) { + var expected; + var rtol; + var out; + + function f( x ) { + return ( x * x ) - 1000000.0; + } + + // Root at x = 1000 + // For large roots, rtol dominates xtol (default 2e-12) + // |x - root| <= xtol + rtol*|root| + // if we set rtol to 1e-4, then error can be up to 1e-4 * 1000 = 0.1 + rtol = 1.0e-4; + expected = 1000.0; + + out = brentq( f, 900.0, 1100.0, 100, 0.0, rtol, [] ); + t.ok( abs( out.root - expected ) <= ( rtol * abs( expected ) ), 'root satisfies relative tolerance' ); + t.end(); +}); + +tape( 'the function supports extra arguments', function test( t ) { + var out; + + function f( x, y ) { + return ( x * x ) - y; + } + + out = brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, [ 4.0 ] ); + t.ok( abs( out.root - 2.0 ) <= 2e-12, 'root is 2.0 for x^2 - 4' ); + t.end(); +});