// Rascal, the Advanced Scientific CALculator
// Copyright (C) 2001, Sebastian Ritterbusch (Rascal@Ritterbusch.de)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more detauls.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#ifndef STANDALONE_VALUE
#ifndef PARSERvariable
#include "../y.tab.h"
#endif
#include "../symtab.h"
#endif

string output(const valuecomplex & a)
{  
   value re(output(a.real())),im(output(a.imag()));
   string sre,sim;
   if(re.isSTRING())
      sre=re.asSTRING();
   else
      sre="Error";
   if(im.isSTRING())
      sim=im.asSTRING();
   else
      sim="Error";

#ifndef STANDALONE_VALUE
   symrec *ptr;
   ptr=getsym("i",PARSERvariable);
   if(ptr && ptr->var.isCOMPLEX() && ptr->var.asCOMPLEX()==valuecomplex(0,1))
   {
      if(a.real()==value(0))
      {
         if(a.imag()==value(1))
            return "i";
         else if(a.imag()==value(-1))
            return "-i";
         else if(a.imag()!=value(0))
            return "("+sim+"*i)";
         else
            return sre;
      }
      if(a.imag()==value(0))
         return sre;
      if(a.imag()<value(0))
      {
         if(a.imag()==value(-1))
            return "("+sre+"-i)";
         return "("+sre+sim+"*i)";
      }
      if(a.imag()==value(1))
         return "("+sre+"+i)";
      return "("+sre+"+"+sim+"*i)";
   }   
   ptr=getsym("j",PARSERvariable);
   if(ptr && ptr->var.isCOMPLEX() && ptr->var.asCOMPLEX()==valuecomplex(0,1))
   {
      if(a.real()==value(0))
      {
         if(a.imag()==value(1))
            return "j";
         else if(a.imag()==value(-1))
            return "-j";
         else if(a.imag()!=value(0))        
            return "("+sim+"*j)";
         else
            return sre;
      }
      if(a.imag()==value(0))
         return sre;
      if(a.imag()<value(0))
      {
         if(a.imag()==value(-1))
            return "("+sre+"-j)";
         return "("+sre+sim+"*j)";         
      }
      if(a.imag()==value(1))
         return "("+sre+"+i)";
      return "("+sre+"+"+sim+"*j)";
   } 
#endif  
   
   return "complex(" + sre + "," + sim + ")";
}

valuematrix conjugate_matrix(const valuematrix & a)
{
   valuematrix out(a);
   int i,j;
   for(i=0;i<out.N;i++)
      for(j=0;j<out.M;j++)
      {
         if(out(i,j).isCOMPLEX())
         {
            out(i,j)=valuecomplex(out(i,j).asCOMPLEX().real(),-out(i,j).asCOMPLEX().imag());
         }
      }
   return out;
}


syntax highlighted by Code2HTML, v. 0.9.1