From 2a42f814e9cd60ee22f2b193d1f1697ffcf64a36 Mon Sep 17 00:00:00 2001 From: sagar7162 Date: Sat, 7 Feb 2026 17:06:38 +0530 Subject: [PATCH 1/3] feat: add `optimize/base/brentq` --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/optimize/base/brentq/README.md | 101 ++++++++ .../base/brentq/benchmark/benchmark.js | 52 ++++ .../optimize/base/brentq/docs/repl.txt | 35 +++ .../base/brentq/docs/types/index.d.ts | 67 +++++ .../optimize/base/brentq/docs/types/test.ts | 74 ++++++ .../optimize/base/brentq/examples/index.js | 44 ++++ .../@stdlib/optimize/base/brentq/lib/index.js | 44 ++++ .../@stdlib/optimize/base/brentq/lib/main.js | 205 +++++++++++++++ .../@stdlib/optimize/base/brentq/package.json | 62 +++++ .../@stdlib/optimize/base/brentq/test/test.js | 241 ++++++++++++++++++ 10 files changed, 925 insertions(+) create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/README.md create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/examples/index.js create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/lib/index.js create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/lib/main.js create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/package.json create mode 100644 lib/node_modules/@stdlib/optimize/base/brentq/test/test.js 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..d70107cde604 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/README.md @@ -0,0 +1,101 @@ + + +# 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[, options] ) + +Finds a zero of a continuous function `f` on the interval `[a, b]`. + +```javascript +function f( x ) { + return ( x * x ) - 1; +} + +var root = brentq( f, 0, 2 ); +// returns 1.0 +``` + +The function accepts the following options: + +- **maxIter**: maximum number of iterations. Default: `100`. +- **xtol**: absolute tolerance. Default: `2e-12`. +- **rtol**: relative tolerance. Default: `8.88e-16`. + +
+ + + +
+ +## Examples + + + +```javascript +var sin = require( '@stdlib/math/base/special/sin' ); +var brentq = require( '@stdlib/optimize/base/brentq' ); + +function f( x ) { + return sin( x ); +} + +var root = brentq( f, 3.0, 3.2 ); +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..1c76dca9e4a9 --- /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 ); + 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..f1a4a36d6edb --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( f, a, b[, options] ) + 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. + options: Object (optional) + Function options. + options.maxIter: number (optional) + Maximum number of iterations. Default: 100. + options.xtol: number (optional) + Absolute tolerance. Default: 2e-12. + options.rtol: number (optional) + Relative tolerance. Default: 8.88e-16. + + Returns + ------- + x: number + Zero. + + Examples + -------- + > function f( x ) { return x*x - 1.0; }; + > {{alias}}( f, 0.0, 2.0 ) + 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..327ae88b73c6 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/index.d.ts @@ -0,0 +1,67 @@ +/* +* @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 + +/** +* Interface defining options. +*/ +interface Options { + /** + * Maximum number of iterations. + */ + maxIter?: number; + + /** + * Absolute tolerance. + */ + xtol?: number; + + /** + * Relative tolerance. + */ + rtol?: number; +} + +/** +* Callback function. +* +* @param x - input value +* @returns function value +*/ +type Unary = ( x: number ) => number; + +/** +* 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 options - function options +* @returns zero +* +* @example +* function f( x ) { +* return x * x - 1.0; +* } +* var v = brentq( f, 0.0, 2.0 ); +* // returns 1.0 +*/ +declare function brentq( f: Unary, a: number, b: number, options?: Options ): number; + +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..8a14ba5ed7d9 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts @@ -0,0 +1,74 @@ +/* +* @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 number... +{ + brentq( f, 0.0, 2.0 ); // $ExpectType number + brentq( f, 0.0, 2.0, { 'maxIter': 20 } ); // $ExpectType number +} + +// The compiler throws an error if the function is provided an invalid first argument... +{ + brentq( true, 0.0, 2.0 ); // $ExpectError + brentq( false, 0.0, 2.0 ); // $ExpectError + brentq( 5, 0.0, 2.0 ); // $ExpectError + brentq( [], 0.0, 2.0 ); // $ExpectError + brentq( {}, 0.0, 2.0 ); // $ExpectError + brentq( 'abc', 0.0, 2.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid second argument... +{ + brentq( f, true, 2.0 ); // $ExpectError + brentq( f, false, 2.0 ); // $ExpectError + brentq( f, [], 2.0 ); // $ExpectError + brentq( f, {}, 2.0 ); // $ExpectError + brentq( f, 'abc', 2.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid third argument... +{ + brentq( f, 0.0, true ); // $ExpectError + brentq( f, 0.0, false ); // $ExpectError + brentq( f, 0.0, [] ); // $ExpectError + brentq( f, 0.0, {} ); // $ExpectError + brentq( f, 0.0, 'abc' ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid options argument... +{ + brentq( f, 0.0, 2.0, true ); // $ExpectError + brentq( f, 0.0, 2.0, false ); // $ExpectError + brentq( f, 0.0, 2.0, 5 ); // $ExpectError + brentq( f, 0.0, 2.0, [] ); // $ExpectError + brentq( f, 0.0, 2.0, 'abc' ); // $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..f5b8c5152ea5 --- /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 root = brentq( f, 2.0, 3.0 ); +console.log( 'f(x) = x^3 - 2x - 5' ); +console.log( 'Root: %d', root ); +console.log( 'f(root): %d', f( root ) ); + +// Example with options +function g( x ) { + return sin( x ); +} + +root = brentq( g, 3.0, 4.0, { + 'xtol': 1e-4 +}); +console.log( '\nf(x) = sin(x)' ); +console.log( 'Root matching 3.14 on [3, 4] with xtol 1e-4: %d', root ); +console.log( 'f(root): %d', g( root ) ); 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..84c99c30edb7 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/lib/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'; + +/** +* Find a zero of a continuous function using Brent's method. +* +* @module @stdlib/optimize/base/brentq +* +* @example +* var brentq = require( '@stdlib/optimize/base/brentq' ); +* +* function f( x ) { +* return Math.sin( x ); +* } +* +* var root = brentq( f, 3.0, 3.2 ); +* // returns ~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..5f106bd47fb5 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/lib/main.js @@ -0,0 +1,205 @@ +/** +* @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 format = require( '@stdlib/string/format' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); + + +// 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 {Options} [options] - function options +* @param {number} [options.maxIter=100] - maximum number of iterations +* @param {number} [options.xtol=2e-12] - absolute tolerance +* @param {number} [options.rtol=8.88e-16] - relative tolerance +* @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} options argument must be an object +* @throws {Error} function values at the interval endpoints must have opposite signs +* @returns {number} zero +* +* @example +* function f( x ) { +* return x * x - 1.0; +* } +* var v = brentq( f, 0.0, 2.0 ); +* // returns 1.0 +*/ +function brentq( f, a, b, options ) { + var maxIter; + var delta; // tolerance + 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 xtol; + var rtol; + 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 ) ); + } + maxIter = 100; + xtol = 2.0e-12; + rtol = 4.0 * EPS; + + if ( arguments.length > 3 ) { + if ( typeof options !== 'object' || options === null || Array.isArray( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( options.maxIter !== void 0 ) { + maxIter = options.maxIter; + } + if ( options.xtol !== void 0 ) { + xtol = options.xtol; + } + if ( options.rtol !== void 0 ) { + rtol = options.rtol; + } + } + + xpre = a; + xcur = b; + xblk = 0.0; + fblk = 0.0; + spre = 0.0; + scur = 0.0; + + fpre = f( xpre ); + fcur = f( xcur ); + + if ( fpre === 0.0 ) { + return xpre; + } + if ( fcur === 0.0 ) { + return xcur; + } + 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*fabs(xcur))/2; + delta = ( xtol + ( rtol * abs( xcur ) ) ) / 2.0; + + sbis = ( xblk - xcur ) / 2.0; + + if ( fcur === 0.0 || abs( sbis ) < delta ) { + return xcur; + } + + 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; + } + + fcur = f( xcur ); + } + return xcur; +} + + +// 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..9e0e0798e611 --- /dev/null +++ b/lib/node_modules/@stdlib/optimize/base/brentq/test/test.js @@ -0,0 +1,241 @@ +/** +* @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 sin = require( '@stdlib/math/base/special/sin' ); +var abs = require( '@stdlib/math/base/special/abs' ); +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, + void 0, + 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 ); + }; + } +}); + +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 ); + }; + } +}); + +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 ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid options argument', function test( t ) { + var values; + var i; + + function f( x ) { + return x; + } + + values = [ + '5', + 5, + true, + [], + NaN + ]; + + 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 ); + }; + } +}); + +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 ); + }, Error, 'throws error' ); + t.end(); +}); + +tape( 'the function finds the root of a polynomial', function test( t ) { + var root; + var tol; + + function f( x ) { + return ( x * x ) - 1.0; + } + + tol = 1.0e-12; + root = brentq( f, 0.0, 2.0, { + 'xtol': tol + }); + t.ok( abs( root - 1.0 ) <= tol, 'root is within tolerance' ); + + root = brentq( f, -2.0, 0.0, { + 'xtol': tol + }); + t.ok( abs( root + 1.0 ) <= tol, 'root is within tolerance' ); + + t.end(); +}); + +tape( 'the function finds the root of sin(x)', function test( t ) { + var root; + var tol; + + function f( x ) { + return sin( x ); + } + + tol = 1.0e-12; + + // Root at PI (~3.14159) + root = brentq( f, 3.0, 4.0, { + 'xtol': tol + }); + t.ok( abs( root - PI ) <= tol, 'root is within tolerance' ); + t.end(); +}); + +tape( 'the function returns the exact root if found immediately (endpoints)', function test( t ) { + var root; + + function f( x ) { + return ( x * x ) - 1.0; + } + root = brentq( f, 1.0, 2.0 ); + t.equal( root, 1.0, 'returns bracket endpoint' ); + + root = brentq( f, 0.0, 1.0 ); + t.equal( root, 1.0, 'returns bracket endpoint' ); + t.end(); +}); + +tape( 'the function respects relative tolerance', function test( t ) { + var expected; + var root; + var rtol; + + function f( x ) { + // Root at x = 1000 + return ( x * x ) - 1000000.0; + } + + // 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; + + root = brentq( f, 900.0, 1100.0, { + 'rtol': rtol, + 'xtol': 0.0 // Disable xtol to test rtol + }); + + // Note: the convergence check uses `delta` which is half the tolerance interval. + + // The condition in the code is `|x - x_exact| <= delta`. + + // Delta = (xtol + rtol*|xcur|)/2 + + // So we expect error <= (rtol * |root|)/2 approximately. + t.ok( abs( root - expected ) <= ( rtol * abs( expected ) ), 'root satisfies relative tolerance' ); + t.end(); +}); From 273bc894128e90e90d3d06090baa33c489a7717e Mon Sep 17 00:00:00 2001 From: sagar7162 Date: Thu, 12 Feb 2026 16:02:00 +0530 Subject: [PATCH 2/3] fix: match return type and function parameters with scipy equivalent --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/optimize/base/brentq/README.md | 32 +++- .../base/brentq/benchmark/benchmark.js | 2 +- .../optimize/base/brentq/docs/repl.txt | 32 ++-- .../base/brentq/docs/types/index.d.ts | 60 ++++-- .../optimize/base/brentq/docs/types/test.ts | 85 ++++++--- .../optimize/base/brentq/examples/index.js | 16 +- .../@stdlib/optimize/base/brentq/lib/main.js | 115 ++++++++---- .../@stdlib/optimize/base/brentq/test/test.js | 177 +++++++++++++----- 8 files changed, 361 insertions(+), 158 deletions(-) diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/README.md b/lib/node_modules/@stdlib/optimize/base/brentq/README.md index d70107cde604..827fda41b67b 100644 --- a/lib/node_modules/@stdlib/optimize/base/brentq/README.md +++ b/lib/node_modules/@stdlib/optimize/base/brentq/README.md @@ -38,24 +38,38 @@ Finds a zero of a continuous function $f$ on the interval $\lbrack a, b \rbrack$ var brentq = require( '@stdlib/optimize/base/brentq' ); ``` -#### brentq( f, a, b[, options] ) +#### 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; + return ( x * x ) - 1.0; } -var root = brentq( f, 0, 2 ); +var out = brentq( f, 0.0, 2.0, 100, 2.0e-12, 8.88e-16, [] ); +var root = out.root; // returns 1.0 ``` -The function accepts the following options: +The function returns a solution object with the following properties: -- **maxIter**: maximum number of iterations. Default: `100`. -- **xtol**: absolute tolerance. Default: `2e-12`. -- **rtol**: relative tolerance. Default: `8.88e-16`. +- **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`. @@ -75,7 +89,9 @@ function f( x ) { return sin( x ); } -var root = brentq( f, 3.0, 3.2 ); +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 index 1c76dca9e4a9..152f4e757d6a 100644 --- a/lib/node_modules/@stdlib/optimize/base/brentq/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/optimize/base/brentq/benchmark/benchmark.js @@ -38,7 +38,7 @@ bench( pkg, function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - root = brentq( f, 0.0, 2.0 ); + root = brentq( f, 0.0, 2.0, 100, 2e-12, 8.88e-16, [] ); if ( isnan( root ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt b/lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt index f1a4a36d6edb..8ce306bfdf27 100644 --- a/lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt +++ b/lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt @@ -1,35 +1,43 @@ -{{alias}}( f, a, b[, options] ) +{{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. - options: Object (optional) - Function options. - options.maxIter: number (optional) - Maximum number of iterations. Default: 100. - options.xtol: number (optional) - Absolute tolerance. Default: 2e-12. - options.rtol: number (optional) - Relative tolerance. Default: 8.88e-16. + + maxIter: integer + Maximum number of iterations. + + xtol: number + Absolute tolerance. + + rtol: number + Relative tolerance. + + args: Array + Callback function arguments. Returns ------- - x: number - Zero. + results: Object + Solution object. Examples -------- > function f( x ) { return x*x - 1.0; }; - > {{alias}}( f, 0.0, 2.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 index 327ae88b73c6..51ff0086ee7f 100644 --- 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 @@ -19,32 +19,48 @@ // TypeScript Version: 4.1 /** -* Interface defining options. +* Callback function. +* +* @param x - input value +* @param args - callback function arguments +* @returns function value +*/ +type Callback = ( x: number, ...args: Array ) => number; + +/** +* Solution object. */ -interface Options { +interface Results { /** - * Maximum number of iterations. + * The root. */ - maxIter?: number; + root: number; /** - * Absolute tolerance. + * Number of iterations. */ - xtol?: number; + iterations: number; /** - * Relative tolerance. + * Number of function calls. */ - rtol?: number; -} + function_calls: number; -/** -* Callback function. -* -* @param x - input value -* @returns function value -*/ -type Unary = ( x: number ) => 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]`. @@ -52,16 +68,20 @@ type Unary = ( x: number ) => number; * @param f - objective function * @param a - lower bound * @param b - upper bound -* @param options - function options -* @returns zero +* @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 v = brentq( f, 0.0, 2.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: Unary, a: number, b: number, options?: Options ): number; +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 index 8a14ba5ed7d9..74c09fe16b3e 100644 --- a/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts +++ b/lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts @@ -30,45 +30,80 @@ function f( x: number ): number { // TESTS // -// The function returns a number... +// The function returns a solution object... { - brentq( f, 0.0, 2.0 ); // $ExpectType number - brentq( f, 0.0, 2.0, { 'maxIter': 20 } ); // $ExpectType number + 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 ); // $ExpectError - brentq( false, 0.0, 2.0 ); // $ExpectError - brentq( 5, 0.0, 2.0 ); // $ExpectError - brentq( [], 0.0, 2.0 ); // $ExpectError - brentq( {}, 0.0, 2.0 ); // $ExpectError - brentq( 'abc', 0.0, 2.0 ); // $ExpectError + 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 ); // $ExpectError - brentq( f, false, 2.0 ); // $ExpectError - brentq( f, [], 2.0 ); // $ExpectError - brentq( f, {}, 2.0 ); // $ExpectError - brentq( f, 'abc', 2.0 ); // $ExpectError + 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 ); // $ExpectError - brentq( f, 0.0, false ); // $ExpectError - brentq( f, 0.0, [] ); // $ExpectError - brentq( f, 0.0, {} ); // $ExpectError - brentq( f, 0.0, 'abc' ); // $ExpectError + 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 options argument... +// The compiler throws an error if the function is provided an invalid fourth argument... { - brentq( f, 0.0, 2.0, true ); // $ExpectError - brentq( f, 0.0, 2.0, false ); // $ExpectError - brentq( f, 0.0, 2.0, 5 ); // $ExpectError - brentq( f, 0.0, 2.0, [] ); // $ExpectError - brentq( f, 0.0, 2.0, 'abc' ); // $ExpectError + 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 index f5b8c5152ea5..fc56d962e42b 100644 --- a/lib/node_modules/@stdlib/optimize/base/brentq/examples/index.js +++ b/lib/node_modules/@stdlib/optimize/base/brentq/examples/index.js @@ -26,19 +26,19 @@ function f( x ) { return pow( x, 3 ) - ( 2.0 * x ) - 5.0; } -var root = brentq( f, 2.0, 3.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', root ); -console.log( 'f(root): %d', f( root ) ); +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 ); } -root = brentq( g, 3.0, 4.0, { - 'xtol': 1e-4 -}); +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', root ); -console.log( 'f(root): %d', g( root ) ); +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/main.js b/lib/node_modules/@stdlib/optimize/base/brentq/lib/main.js index 5f106bd47fb5..7ff3c96a4621 100644 --- a/lib/node_modules/@stdlib/optimize/base/brentq/lib/main.js +++ b/lib/node_modules/@stdlib/optimize/base/brentq/lib/main.js @@ -22,9 +22,9 @@ 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' ); -var EPS = require( '@stdlib/constants/float64/eps' ); // MAIN // @@ -35,27 +35,32 @@ var EPS = require( '@stdlib/constants/float64/eps' ); * @param {Function} f - objective function * @param {number} a - lower bound * @param {number} b - upper bound -* @param {Options} [options] - function options -* @param {number} [options.maxIter=100] - maximum number of iterations -* @param {number} [options.xtol=2e-12] - absolute tolerance -* @param {number} [options.rtol=8.88e-16] - relative tolerance +* @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} options argument must be an object +* @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 {number} zero +* @returns {Object} solution object * * @example * function f( x ) { * return x * x - 1.0; * } -* var v = brentq( f, 0.0, 2.0 ); -* // returns 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, options ) { - var maxIter; - var delta; // tolerance +function brentq( f, a, b, maxIter, xtol, rtol, args ) { + var funcalls; + var delta; + var nargs; + var vargs; var xpre; var xcur; var xblk; @@ -68,8 +73,6 @@ function brentq( f, a, b, options ) { var stry; var dpre; var dblk; - var xtol; - var rtol; var i; if ( !isFunction( f ) ) { @@ -81,23 +84,17 @@ function brentq( f, a, b, options ) { if ( !isNumber( b ) ) { throw new TypeError( format( 'invalid argument. Third argument must be a number. Value: `%s`.', b ) ); } - maxIter = 100; - xtol = 2.0e-12; - rtol = 4.0 * EPS; - - if ( arguments.length > 3 ) { - if ( typeof options !== 'object' || options === null || Array.isArray( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); - } - if ( options.maxIter !== void 0 ) { - maxIter = options.maxIter; - } - if ( options.xtol !== void 0 ) { - xtol = options.xtol; - } - if ( options.rtol !== void 0 ) { - rtol = options.rtol; - } + 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; @@ -107,14 +104,38 @@ function brentq( f, a, b, options ) { spre = 0.0; scur = 0.0; - fpre = f( xpre ); - fcur = f( xcur ); + 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 xpre; + return { + 'root': xpre, + 'iterations': 0, + 'function_calls': funcalls, + 'converged': true, + 'flag': 'converged', + 'method': 'brentq' + }; } if ( fcur === 0.0 ) { - return xcur; + 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.' ); @@ -142,13 +163,20 @@ function brentq( f, a, b, options ) { fblk = fpre; } - // delta = (xtol + rtol*fabs(xcur))/2; + // 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 xcur; + return { + 'root': xcur, + 'iterations': i + 1, + 'function_calls': funcalls, + 'converged': true, + 'flag': 'converged', + 'method': 'brentq' + }; } if ( abs( spre ) > delta && abs( fcur ) < abs( fpre ) ) { @@ -194,9 +222,18 @@ function brentq( f, a, b, options ) { xcur += ( sbis > 0.0 ) ? delta : -delta; } - fcur = f( xcur ); + vargs[ 0 ] = xcur; + fcur = f.apply( null, vargs ); + funcalls += 1; } - return xcur; + return { + 'root': xcur, + 'iterations': i, + 'function_calls': funcalls, + 'converged': false, + 'flag': 'suboptimal convergence', + 'method': 'brentq' + }; } diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/test/test.js b/lib/node_modules/@stdlib/optimize/base/brentq/test/test.js index 9e0e0798e611..c5a687f75225 100644 --- a/lib/node_modules/@stdlib/optimize/base/brentq/test/test.js +++ b/lib/node_modules/@stdlib/optimize/base/brentq/test/test.js @@ -19,8 +19,8 @@ 'use strict'; var tape = require( 'tape' ); -var sin = require( '@stdlib/math/base/special/sin' ); 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' ); @@ -40,7 +40,6 @@ tape( 'the function throws an error if provided an invalid first argument', func NaN, true, null, - void 0, undefined, [], {} @@ -53,7 +52,7 @@ tape( 'the function throws an error if provided an invalid first argument', func function badValue( value ) { return function badValue() { - brentq( value, 0.0, 1.0 ); + brentq( value, 0.0, 1.0, 100, 2e-12, 8.88e-16, [] ); }; } }); @@ -82,7 +81,7 @@ tape( 'the function throws an error if provided an invalid second argument', fun function badValue( value ) { return function badValue() { - brentq( f, value, 1.0 ); + brentq( f, value, 1.0, 100, 2e-12, 8.88e-16, [] ); }; } }); @@ -111,12 +110,41 @@ tape( 'the function throws an error if provided an invalid third argument', func function badValue( value ) { return function badValue() { - brentq( f, 0.0, value ); + brentq( f, 0.0, value, 100, 2e-12, 8.88e-16, [] ); }; } }); -tape( 'the function throws an error if provided an invalid options argument', function test( t ) { +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; @@ -126,10 +154,11 @@ tape( 'the function throws an error if provided an invalid options argument', fu values = [ '5', - 5, true, + null, + undefined, [], - NaN + {} ]; for ( i = 0; i < values.length; i++ ) { @@ -139,7 +168,65 @@ tape( 'the function throws an error if provided an invalid options argument', fu function badValue( value ) { return function badValue() { - brentq( f, 0.0, 1.0, value ); + 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 ); }; } }); @@ -149,13 +236,13 @@ tape( 'the function throws an error if function values at endpoints do not have return x * x; } t.throws( function bad() { - brentq( f, 1.0, 2.0 ); + 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 root; + var out; var tol; function f( x ) { @@ -163,79 +250,79 @@ tape( 'the function finds the root of a polynomial', function test( t ) { } tol = 1.0e-12; - root = brentq( f, 0.0, 2.0, { - 'xtol': tol - }); - t.ok( abs( root - 1.0 ) <= tol, 'root is within tolerance' ); - - root = brentq( f, -2.0, 0.0, { - 'xtol': tol - }); - t.ok( abs( root + 1.0 ) <= tol, 'root is within tolerance' ); + 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 root; + var out; var tol; function f( x ) { return sin( x ); } - tol = 1.0e-12; - // Root at PI (~3.14159) - root = brentq( f, 3.0, 4.0, { - 'xtol': tol - }); - t.ok( abs( root - PI ) <= tol, 'root is within tolerance' ); + 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 root; + var out; function f( x ) { return ( x * x ) - 1.0; } - root = brentq( f, 1.0, 2.0 ); - t.equal( root, 1.0, 'returns bracket endpoint' ); + out = brentq( f, 1.0, 2.0, 100, 2e-12, 8.88e-16, [] ); + t.equal( out.root, 1.0, 'returns bracket endpoint' ); - root = brentq( f, 0.0, 1.0 ); - t.equal( 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 root; var rtol; + var out; function f( x ) { - // Root at x = 1000 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 + // 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; - root = brentq( f, 900.0, 1100.0, { - 'rtol': rtol, - 'xtol': 0.0 // Disable xtol to test rtol - }); - - // Note: the convergence check uses `delta` which is half the tolerance interval. + 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(); +}); - // The condition in the code is `|x - x_exact| <= delta`. +tape( 'the function supports extra arguments', function test( t ) { + var out; - // Delta = (xtol + rtol*|xcur|)/2 + function f( x, y ) { + return ( x * x ) - y; + } - // So we expect error <= (rtol * |root|)/2 approximately. - t.ok( abs( root - expected ) <= ( rtol * abs( expected ) ), 'root satisfies relative tolerance' ); + 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(); }); From cf309367067bf560d2317013e8c9b6774f6309e2 Mon Sep 17 00:00:00 2001 From: sagar7162 Date: Thu, 12 Feb 2026 16:14:12 +0530 Subject: [PATCH 3/3] fix: fixed lib/index.js example --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/optimize/base/brentq/lib/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/optimize/base/brentq/lib/index.js b/lib/node_modules/@stdlib/optimize/base/brentq/lib/index.js index 84c99c30edb7..893efb1b41cc 100644 --- a/lib/node_modules/@stdlib/optimize/base/brentq/lib/index.js +++ b/lib/node_modules/@stdlib/optimize/base/brentq/lib/index.js @@ -24,14 +24,17 @@ * @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 Math.sin( x ); +* return sin( x ); * } * -* var root = brentq( f, 3.0, 3.2 ); -* // returns ~3.14 +* 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 //