-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add optimize/base/brentq
#10145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sagar7162
wants to merge
3
commits into
stdlib-js:develop
Choose a base branch
from
sagar7162:opti_brentq
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
lib/node_modules/@stdlib/optimize/base/brentq/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # brentq | ||
|
|
||
| > Find a zero of a continuous function using Brent's method. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| 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. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## 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`. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```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 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
52 changes: 52 additions & 0 deletions
52
lib/node_modules/@stdlib/optimize/base/brentq/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| }); |
35 changes: 35 additions & 0 deletions
35
lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| -------- | ||
|
|
||
67 changes: 67 additions & 0 deletions
67
lib/node_modules/@stdlib/optimize/base/brentq/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; |
74 changes: 74 additions & 0 deletions
74
lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
44 changes: 44 additions & 0 deletions
44
lib/node_modules/@stdlib/optimize/base/brentq/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ) ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.