Start with a guess, then repeatedly update the guess until we're close enough to the desired solution.

use Haldayne\Fox\Improve;
$calculator = new Improve(
    function ($guess) { return abs(($guess * $guess) - ($guess + 1)) < .00001; },
    function ($guess) { return 1/$guess + 1; }
);
$phi = $calculator(1);
$calculator->getGuessHistory();
package Haldayne

 Methods

Create a new iterative improvement algorithm.

__construct(callable $decide, callable $update) 

Parameters

$decide

callable

Decides if the guess is good enough. Receives one argument, the current guess.

$update

callable

Updates the guess to a new guess. Receives two arguments: the current guess and the current loop count, in that order.

Given an initial guess, decide if the guess is close enough. If not, update the guess to a new value and repeat up to the maximum number of times.

__invoke(mixed $guess) : mixed

Parameters

$guess

mixed

The initial guess

Returns

mixedThe calculated result

Get the history of guesses, from oldest to youngest.

getGuessHistory() : \Haldayne\Boost\Map

Returns

\Haldayne\Boost\Map

Set the maximum number of times the iteration will run. Useful when your improvement algorithm is subject to failure cases (like the Newton-Raphson Method).

setMaximumIterations(integer|null $maxIterations) : void

By default, there is no limit.

Parameters

$maxIterations

integernull

 Properties

 

$decide

$decide 

Default

null
 

$guesses

$guesses 

Default

null
 

$maxIterations

$maxIterations 

Default

null
 

$update

$update 

Default

null