/****************************************************************************
 * 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 {
      (-0.12968750000000001110,
       -0.51000000000000000888,
       1.21195000000000008278,
       0.48500000000000004219) => (1280,947)
   }



   formula {
     
      z    = current;
      zold = [0, 0];

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

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

      $xtot = 0;    
      $ytot = 0;    
      $mtot = 0;    
     
      while($count < 20 && ssq(z - zold) > 1e-6)
      {
         arg = z ^ 2 + sin(z);

         f_z  = log(arg);
         fp_z = 1 / (arg) * (2 * z + cos(z));

         zold = z;
         z    = z - fp_z / f_z;

         $x = real(sec(z));
         $y = imag(csc(z));
         $m = mag(cot(z));

         $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 = ($mtot / $count) / $mmax;
         $b = ($ytot / $count) / $ymax;

         $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);
   }
}








