Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions lib/node_modules/@stdlib/optimize/base/brentq/README.md
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&#91;, options&#93; )

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 -->
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 lib/node_modules/@stdlib/optimize/base/brentq/docs/repl.txt
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
--------

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 lib/node_modules/@stdlib/optimize/base/brentq/docs/types/test.ts
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 lib/node_modules/@stdlib/optimize/base/brentq/examples/index.js
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 ) );
Loading