/****************************************************************************
 * This formula file or any formula file derived from it cannot be used for
 * commercial purposes or to create hard copy output without the explicit
 * consent of the original author.
 ****************************************************************************/

fractal {

   // Establish the mapping between complex coordinates
   // and display coordinates

   mapping {
      (-5.79999999999999982236,
       -8.00000000000000000000,
       5.36000000000000031974,
       7.83999999999999985789) => (676,960)
   }


   formula
   {
      zvalue = exp(current);

      $xavg = 0;    
      $yavg = 0;    
      $mavg = 0;    

      $xmax = 0;    
      $ymax = 0;    
      $mmax = 0;    

      $xtot = 0;    
      $ytot = 0;    
      $mtot = 0;    
 
      // Changing these values can produce interesting variations

      $maxIterations = 15;
      $tolerance     = 1e-6;

      zSum           = 0;
      $denominator   = 1;

      point = cot(current);

      // Loop until we've been through the loop '$maxIterations' times
      // or we deem that zSum is close enough to zvalue

      while($count < $maxIterations && ssq(zvalue - zSum) > $tolerance)
      {
         zSum += exp(point) * ((current - point) ^ $count) / $denominator;

         if($count > 0) { $denominator *= ($count + 1); }

         if($count % 3 == 0)     { value = sech(zSum); }
         elseif($count % 3 == 1) { value = csch(zSum); }
         else                    { value = coth(zSum); }


         $x = mag(value ^ 2);
         $y = mag(value ^ 4);
         $m = mag(value ^ 6);

         $xavg = ($x + $xavg * $count) / ($count + 1);
         $yavg = ($y + $yavg * $count) / ($count + 1);
         $mavg = ($m + $mavg * $count) / ($count + 1);

         $xdev = sqrt(($x - $xavg) * ($x - $xavg));
         $ydev = sqrt(($y - $yavg) * ($y - $yavg));
         $mdev = sqrt(($m - $mavg) * ($m - $mavg));

         $xmax = max($xmax, $xdev);
         $ymax = max($ymax, $ydev);
         $mmax = max($mmax, $mdev);

         $xtot += $xdev;
         $ytot += $ydev;
         $mtot += $mdev;
      }


      $r = 0;
      $g = 0;
      $b = 0;

      if($count > 0)
      {
         $r = ($xtot/$count) / $xmax;
         $g = ($ytot/$count) / $ymax;
         $b = ($mtot/$count) / $mmax;

         $r = get_sin_color($r, 255, 1);
         $g = get_sin_color($g, 255, 1);
         $b = get_sin_color($b, 255, 1);
      }

      set_color($r, $g, $b);
   }
}













