Exemplo n.º 1
0
 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));
 }
Exemplo n.º 2
0
 /**
  * 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);
 }
Exemplo n.º 3
0
 public function testCanComputeNonAnalytic()
 {
     $z = new Complex(1, 2);
     $accuracy = 1.0E-9;
     $this->assertEquals(sqrt(5), $z->abs(), 'abs', $accuracy);
     $this->assertEquals(1, $z->r(), 'r', $accuracy);
     $this->assertEquals(2, $z->i(), 'i', $accuracy);
     $this->assertEquals(1.107148718, $z->arg(), 'arg', $accuracy);
 }