help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Same .m file: different results with different versions of Octave


From: Judd Storrs
Subject: Re: Same .m file: different results with different versions of Octave
Date: Sun, 18 Apr 2010 22:23:31 -0400

I was able to get your code to work by porting GSL's complex tanh
algorithm (I called it _tanh.m):

function z = _tanh(a)
  # Port of GSL's tanh algorithm for complex tanh.

  R = real(a) ;
  I = imag(a) ;
  D = cos (I).^2 + sinh (R).^2 ;
  F = 1 + (cos(I)./sinh(R)).^2 ;

  z = a ;
  idx = abs(R) < 1.0 ;
  if any(idx)
    z(idx) = complex(sinh(R(idx)).*cosh(R(idx))./D(idx),
0.5.*sin(2*I(idx))./D(idx));
  endif

  idx = ~idx ; # abs(R) >= 1.0
  if any(idx)
    z(idx) = complex ( 1.0./(tanh(R(idx)).*F(idx)), 0.5*sin(2*I(idx))./D(idx));
  endif
endfunction

It can probably be optimized some more and there's probably a better
idiom for the if/else. When I replace the call to tanh with _tanh in
your sample code, I get a plot of the green circles and blue dots that
look like a pretty good fit.


--judd

For reference this is GSL's gsl_complex_tanh:

gsl_complex
gsl_complex_tanh (gsl_complex a)
{                               /* z = tanh(a) */
  double R = GSL_REAL (a), I = GSL_IMAG (a);

  gsl_complex z;

  if (fabs(R) < 1.0)
    {
      double D = pow (cos (I), 2.0) + pow (sinh (R), 2.0);

      GSL_SET_COMPLEX (&z, sinh (R) * cosh (R) / D, 0.5 * sin (2 * I) / D);
    }
  else
    {
      double D = pow (cos (I), 2.0) + pow (sinh (R), 2.0);
      double F = 1 + pow (cos (I) / sinh (R), 2.0);

      GSL_SET_COMPLEX (&z, 1.0 / (tanh (R) * F), 0.5 * sin (2 * I) / D);
    }

  return z;
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]