public function testCanEvaluateHyperbolicFunctions() { $x = $this->variables['x']; $this->assertResult('sinh(0)', 0); $this->assertResult('sinh(x)', Complex::sinh($x)); $this->assertResult('cosh(0)', 1); $this->assertResult('cosh(x)', Complex::cosh($x)); $this->assertResult('tanh(0)', 0); $this->assertResult('tanh(x)', Complex::tanh($x)); $this->assertResult('coth(x)', Complex::div(1, Complex::tanh($x))); $this->assertResult('arsinh(0)', 0); $this->assertResult('arsinh(x)', Complex::arsinh($x)); $this->assertResult('arcosh(1)', 0); $this->assertResult('arcosh(3)', Complex::arcosh(3)); $this->assertResult('arcosh(x)', Complex::arcosh($x)); $this->assertResult('artanh(0)', 0); $this->assertResult('artanh(x)', Complex::artanh($x)); }
/** * Evaluate a FunctionNode * * Computes the value of a FunctionNode `f(x)`, where f is * an elementary function recognized by StdMathLexer and StdMathParser. * * @see \MathParser\Lexer\StdMathLexer StdMathLexer * @see \MathParser\StdMathParser StdMathParser * @throws UnknownFunctionException if the function respresented by the * FunctionNode is *not* recognized. * * @param FunctionNode $node AST to be evaluated * @retval float */ public function visitFunctionNode(FunctionNode $node) { $z = $node->getOperand()->accept($this); $a = $z->r(); $b = $z->i(); switch ($node->getName()) { // Trigonometric functions case 'sin': return Complex::sin($z); case 'cos': return Complex::cos($z); case 'tan': return Complex::tan($z); case 'cot': return Complex::cot($z); // Inverse trigonometric functions // Inverse trigonometric functions case 'arcsin': return Complex::arcsin($z); case 'arccos': return Complex::arccos($z); case 'arctan': return Complex::arctan($z); case 'arccot': return Complex::arccot($z); case 'sinh': return Complex::sinh($z); case 'cosh': return Complex::cosh($z); case 'tanh': return Complex::tanh($z); case 'coth': return Complex::div(1, Complex::tanh($z)); case 'arsinh': return Complex::arsinh($z); case 'arcosh': return Complex::arcosh($z); case 'artanh': return Complex::artanh($z); case 'arcoth': return Complex::div(1, Complex::artanh($z)); case 'exp': return Complex::exp($z); case 'log': return Complex::log($z); case 'lg': return Complex::div(Complex::log($z), M_LN10); case 'sqrt': return Complex::sqrt($z); case 'abs': return new Complex($z->abs(), 0); case 'arg': return new Complex($z->arg(), 0); case 're': return new Complex($z->r(), 0); case 'im': return new Complex($z->i(), 0); case 'conj': return new Complex($z->r(), -$z->i()); default: throw new UnknownFunctionException($node->getName()); } return new FunctionNode($node->getName(), $inner); }
public function testCanDoAritmethic() { $z = new Complex(1, 2); $w = new Complex(2, -1); $this->assertEquals(new Complex(3, 1), Complex::add($z, $w)); $this->assertEquals(new Complex(-1, 3), Complex::sub($z, $w)); $this->assertEquals(new Complex(4, 3), Complex::mul($z, $w)); $this->assertEquals(new Complex(0, 1), Complex::div($z, $w)); }