Code rất dài dòng và xúc tích.
Ngỉ 30/5 - 1/4 chán qué :<
PHP:
<?php
/**
* @package: Math Solver Class
* @author: Do Minh Hieu
* @email: [email protected]
* @copyright: Copyright (c) 2021
* @Version: 1.0
*/
class Math_Solver {
/**
* @Return: array[
# num: number of root
# root: all of root
]
# num = 0 & root = null
=> impossible equation
# num = true & root = true
=> infinity root
**/
/** SOLVE THE EQUATION **/
/** Linear equation
* @Wiki: https://en.wikipedia.org/wiki/Linear_equation
* @Formule ax + b = 0
* @Use Linear(a, b)
**/
public function Linear($a = '', $b = '') {
if ($a == 0) {
// a = 0 => impossible equation
return array (
'num' => 0,
'root' => null
);
} else if ($b == 0) {
// b = 0 => a = 0
return array (
'num' => 1,
'root' => array('0')
);
} else {
return array (
'num' => 1,
'root' => (- $b) / $a
);
}
}
/** Quadratic equation
* Wiki: https://en.wikipedia.org/wiki/Quadratic_equation
* @Formula ax² + bx + c = 0 (a, b, c is parameters)
* @Use Quadratic(a, b, c)
**/
public function Quadratic($a = '', $b = '', $c = '') {
/* Application of Viète's theorem
# a + b + c = 0
=> x1 = 1, x2 = c/a
*/
if ($a + $b + $c == 0) {
return array(
'num' => 2,
'root' => array(
1,
$c/$a
)
);
}
/**
# a - b + c = 0
=> x1 = -1, x2 = -c/a
*/
if ($a + $b + $c == 0) {
return array(
'num' => 2,
'root' => array(
-1,
- ($c) / $a
)
)
}
# Calculation delta (∆)
# ∆ = b² - 4ac
$delta = ($b*$b) - 4*$a*c
if ($delta < 0) {
// delta < 0 => impossible equation
return array (
'num' => 0,
'root' => null
);
} else if ($delta == 0) {
// delta = 0 => 1 root
return array (
'num' => 1,
'root' => array( - $b / (2*$a))
);
} else {
// delta > 0 => 2 root
$sqrt = sqrt($delta);
return array(
'num' => 2,
'root' => array(
( - $b + $sqrt) / (2 * $a),
( - $b - $sqrt) / (2 * $a)
)
);
}
}
/** Cubic equation
* Wiki: https://en.wikipedia.org/wiki/Cubic_equation
* @Formula ax³ + bx² + cx + d = 0 (a, b, c, d is parameters)
* @Use Cubic(a, b, c, d)
**/
public function Cublic($a='', $b='', $c='', $d='') {
//∆ = b² - 3ac
}
}
Sửa lần cuối: