Etienne Kneuss

home » repository » php » Math_Expression » Structures » Math_Expression_Structure_Vector.php
Tree:
school
mp3
php
sample
publications
Math_Derivative
Math_Expression
Operators
Exceptions
Structures
Math_Expression_Structure_Parenthesis.php
Math_Expression_Structure_Real.php
Math_Expression_Structure.php
Math_Expression_Structure_Vector.php
example.php
Expression.php
Math_Expression-0.3.tar.gz
Math_Expression-0.2.tar.gz
ircbot
php.net
images
dump

Source code raw phps

    <?php require_once 'Math_Expression_Structure.php'; class Math_Expression_Structure_Vector extends Math_Expression_Structure {     public $regex          = '/^(\{((?:[^{}]+|(?1))+)\})/';     public $match          = array();     protected $_expression = null;     public $pieces = array();     protected $_separator = ',';          protected $_evaluated = false;          public function evaluate()      {         if ($this->_evaluated) {             return $this;         } else {             $this->_evaluated = true;         }         if (empty($this->match)) {             preg_match($this->regex, $this->_expression, $this->match);         }                  // have to parse that expression         $deepness = 0;         $key      = 0;         $parts = array('');         for($i = 0; $i < strlen($this->match[2]); $i++) {                          $char = $this->match[2][$i];                          if ($char == '{') {                 $deepness++;             } elseif ($char == '}') {                 $deepness--;             } elseif ($char == $this->_separator && !$deepness) {                 $key++;                 array_push($parts, '');                 continue;             }             $parts[$key] .= $char;                                   }                  $this->pieces = array();         foreach ($parts as $key => $piece) {             $expr = new Math_Expression($piece);                          $this->pieces[] = $expr->evaluate();         }                  return $this;     }     public function __construct($pieces = '')      {         if (is_string($pieces)) {             $this->_expression = $pieces;         } else if(is_array($pieces)) {             $this->pieces = $pieces;             $this->_evaluated = true;         }     }          public function handleOperation($op, $struct2, $reverse = false)      {         $obj2 = $struct2->evaluate();                  $this->evaluate();         $new_pieces = array();         if ($obj2 instanceof Math_Expression_Structure_Real) {                                       switch(get_class($op)) {                              case 'Math_Expression_Operator_Mult':                 case 'Math_Expression_Operator_Div':                                                              foreach($this->pieces as $key => $piece) {                         $new_pieces[$key] = Math_Expression::handleOperation($piece, $op, $obj2);                     }                     $new_vector = new self($new_pieces);                     return $new_vector->evaluate();             }         }         return false;                  }     public function __toString()     {         $first  = true;         $return = '';         foreach ($this->pieces as $piece) {             if ($first) {                 $first = false;                 $return .= $piece->__toString();             } else {                 $return .= $this->_separator.' '.$piece->evaluate()->__toString();                 }         }         return '{'.$return.'}';     } } ?>

Comments

There is currently no comment here.

Add a comment

Username:

Spam Challenge: 15+20=?

Comment: