Skip to content

Commit

Permalink
Minor code improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
markrogoyski committed May 10, 2018
1 parent 9b3b00e commit af66909
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 103 deletions.
22 changes: 14 additions & 8 deletions src/Algebra.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,14 @@ public static function factors(int $x): array
* @param number $a x² coefficient
* @param number $b x coefficient
* @param number $c constant coefficient
* @param bool $return_complex Whether to return complex numbers or NANs if imaginary roots
* @param bool $return_complex Whether to return complex numbers or NANs if imaginary roots
*
* @return array [x₁, x₂] roots of the equation, or
* [NAN, NAN] if discriminant is negative, or
* [Complex, Complex] if discriminant is negative and complex option is on or
* [x] if a = 0 and formula isn't quadratics
*
* @throws Exception\IncorrectTypeException
*/
public static function quadratic($a, $b, $c, bool $return_complex = false): array
{
Expand Down Expand Up @@ -290,14 +292,16 @@ public static function discriminant($a, $b, $c)
* z₂ = Complex conjugate; therefore, NAN
* z₃ = Complex conjugate; therefore, NAN
*
* @param number $a₃ z³ coefficient
* @param number $a₂ z² coefficient
* @param number $a₁ z coefficient
* @param number $a₀ constant coefficient
* @param bool $return_complex whether to return complex numbers
* @param number $a₃ z³ coefficient
* @param number $a₂ z² coefficient
* @param number $a₁ z coefficient
* @param number $a₀ constant coefficient
* @param bool $return_complex whether to return complex numbers
*
* @return array of roots (three real roots, or one real root and two NANs because complex numbers not yet supported)
* (If $a₃ = 0, then only two roots of quadratic equation)
*
* @throws Exception\IncorrectTypeException
*/
public static function cubic($a₃, $a₂, $a₁, $a₀, bool $return_complex = false): array
{
Expand Down Expand Up @@ -366,10 +370,12 @@ public static function cubic($a₃, $a₂, $a₁, $a₀, bool $return_complex =
* @param number $a₃ z³ coefficient
* @param number $a₂ z² coefficient
* @param number $a₁ z coefficient
* @param number $a₀ constant coefficient
* @param bool $return_complex whether to return complex numbers
* @param number $a₀ constant coefficient
* @param bool $return_complex whether to return complex numbers
*
* @return array of roots
*
* @throws Exception\IncorrectTypeException
*/
public static function quartic($a₄, $a₃, $a₂, $a₁, $a₀, bool $return_complex = false): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Functions/Arithmetic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Arithmetic
*
* @return callable Sum of the input functions
*/
public static function add(callable ...$args)
public static function add(callable ...$args): callable
{
$sum = function ($x, ... $args) {
$function = 0;
Expand All @@ -37,7 +37,7 @@ public static function add(callable ...$args)
*
* @return callable Product of the input functions
*/
public static function multiply(callable ...$args)
public static function multiply(callable ...$args): callable
{
$product = function ($x, ... $args) {
$function = 1;
Expand Down
12 changes: 6 additions & 6 deletions src/Functions/Piecewise.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public function __construct(array $intervals, array $functions)
* the corresponding function for that point in the domain, and then return
* the function evaluated at that point. If no function is found, throw an Exception.
*
* @param number $x₀ The value at which we are evaluating our piecewise function
* @param float $x₀ The value at which we are evaluating our piecewise function
*
* @return number The specific function evaluated at $x₀
* @return float The specific function evaluated at $x₀
*
* @throws Exception\BadDataException if an interval cannot be found which contains our $x₀
*/
public function __invoke($x₀)
public function __invoke(float $x₀): float
{
$function = $this->getFunction($x₀);
return $function($x₀);
Expand All @@ -107,14 +107,14 @@ public function __invoke($x₀)
* the function that corresponds to that subinterval. If no subinterval is found
* such that our input is contained within it, a false is returned.
*
* @param number $x The value at which we are searching for a subinterval that
* @param float $x The value at which we are searching for a subinterval that
* contains it, and thus has a corresponding function.
*
* @return callable Returns the function that contains $x in its domain
*
* @throws Exception\BadDataException if an interval cannot be found which contains our $x
*/
private function getFunction($x): callable
private function getFunction(float $x): callable
{
foreach ($this->intervals as $i => $interval) {
$a = $interval[0];
Expand Down Expand Up @@ -224,7 +224,7 @@ private function constructorPreconditions(array $intervals, array $functions)
* @param number $b
* @param number $lastA
* @param number $lastB
* @param number $lastBOpen
* @param bool $lastBOpen
* @param bool $aOpen
* @param bool $bOpen
*
Expand Down
8 changes: 8 additions & 0 deletions src/Functions/Polynomial.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ public function integrate(): Polynomial
* @param mixed $polynomial The polynomial or scaler we are adding to our current polynomial
*
* @return Polynomial The sum of our polynomial objects, also a polynomial object
*
* @throws Exception\IncorrectTypeException
*/
public function add($polynomial): Polynomial
{
Expand Down Expand Up @@ -327,6 +329,8 @@ public function add($polynomial): Polynomial
* @param mixed $polynomial The polynomial or scaler we are subtracting from our current polynomial
*
* @return Polynomial The defference of our polynomial objects, also a polynomial object
*
* @throws Exception\IncorrectTypeException
*/
public function subtract($polynomial): Polynomial
{
Expand All @@ -346,6 +350,8 @@ public function subtract($polynomial): Polynomial
* @param mixed $polynomial The polynomial or scaler we are multiplying with our current polynomial
*
* @return Polynomial The product of our polynomial objects, also a polynomial object
*
* @throws Exception\IncorrectTypeException
*/
public function multiply($polynomial): Polynomial
{
Expand Down Expand Up @@ -395,6 +401,8 @@ public function negate(): Polynomial
* Closed form solutions only exist if the degree is less than 5
*
* @return array of roots
*
* @throws Exception\IncorrectTypeException
*/
public function roots(): array
{
Expand Down
Loading

0 comments on commit af66909

Please sign in to comment.