Location: PHPKode > scripts > PHPolynomials > phpolynomials/polynomials.php
<?
/*
+-------------------------------------------------+
+                                                 +
+      Polynomials.php by Boštjan Zajec           +
+               hide@address.com                   +
+                                                 +
+    This class can be used and distributed       +
+    free of charge, but cannot be modified       +
+        without permission of author.            +
+                                                 +
+-------------------------------------------------+
*/
class polynomials
 {
 var $output;
 //Extract numbers form polynom
 function polynom2num($input)
  {
  $input=str_replace(",",".",strtolower($input));
  $ok=explode(",","1,2,3,4,5,6,7,8,9,0,+,-,.");
  
  $o=0;
  
  for($i=0; $i<strlen($input); $i++)
   {
   $char=substr($input,$i,1);
   if($char=="(") $o=1;
   
   if(in_array($char,$ok) && $o==0) { $output.=$char; }
   
   if($char==")") $o=0;
  }
  if($output=="+" || $output=="") $output=1;
  if($output=="-") $output=-1;
  return $output;
 }
 function charat($string,$i)
  {
  return substr($string,$i,1);
 }
 
 //Get unknowns (spremenljivke in slovenian)
 function getsprs($clen)
  {
  $all=explode(",","a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,w,z,x,y");
  $spr=array();
  
  for($i=0; $i<strlen($clen); $i++)
   {
   $char=$this->charat($clen,$i);
   $x=0;
   $p=1;
   
   if($char=="(")
    {
    $x=$i+1;
    while($this->charat($clen,$x)!==")" && $x<strlen($clen)) $x++;
    $char=substr($clen,$i+1,$x-1-$i);
    $p=substr($char,strpos($char,'^')+1,strlen($char));
    $char=substr($char,0,strpos($char,'^'));
    $i=$x;
   }
   if((in_array($char,$all) || $x>0)&& !in_array($char,$spr)) $spr[$char]=$p;
  }
  return $spr;
 }
 
 //Multiply polynoms
 function multiply_polynomials($a,$b)
  {
  $num1=$this->polynom2num($a);
  $num2=$this->polynom2num($b);
  
  $k=$num1*$num2;
  
  $sprs1=$this->getsprs($a);
  $sprs2=$this->getsprs($b);
  
  $s="";
  foreach($sprs1 as $base=>$exp)
   {
   if(!empty($sprs2[$base]))
    {
    $p=$exp+$sprs2[$base];
    if($p!==0)
     {
     if($p==1) $s.=$base;
     else $s.="($base^$p)";
    }
    unset($sprs2[$base]);
   }
   else $s.=$base;
  }
  
  foreach($sprs2 as $base=>$exp)
   {
   if($exp>1) $s.="($base^$exp)";
   else $s.=$base;
  }
  if($k>0) $k="+".$k;
  return $k.$s;
 }
 
 //Divide polynoms
 function divide_polynomials($a,$b)
  {
  $num1=$this->polynom2num($a);
  $num2=$this->polynom2num($b);
  $k=$num1/$num2;
  
  $sprs1=$this->getsprs($a);
  $sprs2=$this->getsprs($b);
  
  foreach($sprs1 as $base=>$exp)
   {
   if(!empty($sprs2[$base]))
    {
    $p=$exp-$sprs2[$base];
    if($p!==0)
     {
     if($p==1) $s.=$base;
     else $s.="($base^$p)";
    }
    unset($sprs2[$base]);
   }
   else $s.=$base;
  }
  foreach($sprs2 as $base=>$exp)
   {
   $s.="($base^".($exp*-1).")";
  }
  
  if($k>0) $k="+".$k;
  return $k.$s;
 }
 
 //Show the html code of polynom (x^2) -> x<sup>2</sup>
 function polynom2html($spr)
  {
  $output="";
  for($i=0; $i<strlen($spr); $i++)
   {
   $char=$this->charat($spr,$i);
   if($char=="^") $output.="<sup>";
   elseif($char==")") $output.="</sup>";
   elseif($char=="(") $output.="";
   else $output.=$char;
  }
  return $output;
 }
 
 //Get monomials
 function monomials($racun)
  {
  $cleni=array();
  $sign='+';
  $bracket=0;
  for($i=0; $i<strlen($racun); $i++)
   {
   $char=substr($racun,$i,1);
   if(($char=="-" || $char=="+") && $bracket==0)
    {
    if($tmp!=="")
     {
     $cleni[]=$sign.$tmp;
     $tmp="";
    }
    $sign=$char;
   }
   else
    {
    if($char=="(") $bracket++;
    if($char==")") $bracket--;
    $tmp.=$char;
   }
  }
  $cleni[]=$sign.$tmp;
  return $cleni;
 }
 
 //Remove brackets
 function remove_brackets($input,$p=1)
  {
  $pos=strpos($input,'(')+1;
  $sign=$this->multiply_polynomials(str_replace("*","",substr($input,0,$pos-1)),$p);
  
  $input=substr($input,$pos,strlen($input)-$pos-1);
  
  $cleni=$this->monomials($input);
  $izhod=array();
   
  foreach($cleni as $clen)
   {
   if(!strpos($clen,'('))
    {
    $this->output.=$this->multiply_polynomials($clen,$sign);
   }
   else
    {
    $this->remove_brackets($clen,$sign);
   }
  }
 }
}
?>
Return current item: PHPolynomials