Gene,

In a LON-CAPA script block a call to &tanh() will use the tanh() function 
provided by Math::Cephes, which only supports real numbers.

The Math::Cephes::Complex package, which does support complex numbers, has a 
ctanh() function,
but Math::Cephes::Complex is not currently available within LON-CAPA's perl 
safe space (where code within a script block in a LON-CAPA problem is 
evaluated).

Support for complex numbers in LON-CAPA's perl safe space is currently made 
available via the LONCAPA::LCMathComplex package, which is the standard 
Math::Complex module available from CPAN, but modified to work in Safe Space.

Where there is no overlap with function names used for Math::Cephes functions, 
then you can just use the LCMathComplex functions directly, e.g.. &cplx(), 
&Re(), &Im(), but where there is overlap, as is the case for tanh(), to ensure 
the function from LCMathComplex is called, include the full namespace 
identifier, e.g., &LONCAPA::LCMathComplex::tanh().

For the future I can add a set of unique function names to LON-CAPA for those 
functions in  LCMathComplex which overlap with function names in Math::Cephes, 
and then you could just call those without needing a full namespace identifier.

Anyway, as things stand today, the code below will display both real and 
imaginary parts for the hyperbolic tan -- tanh -- for a complex number (in this 
case 3 + 4i )

<script type="loncapa/perl">
$z = &cplx(3, 4);
$x = &LONCAPA::LCMathComplex::tanh($z);
$tanh_real = &Re($x);
$tanh_imag = &Im($x);
</script>
<startouttext />
tanh($z) is $x <br />
The real part is: $tanh_real <br />
The imaginary part is: $tanh_imag <br />
<endouttext />

Stuart Raeburn
LON-CAPA Academic Consortium
________________________________________
From: LON-CAPA-users <lon-capa-users-boun...@mail.lon-capa.org> on behalf of 
Harding, Gene L <glhar...@purdue.edu>
Sent: Wednesday, January 29, 2020 6:42 PM
To: Discussion list for LON-CAPA users
Subject: [LON-CAPA-users] Hyperbolic Trig Function Problem

Hi,

Does anyone have experience using the hyperbolic trig functions in LON-CAPA? I 
have recently been coding some new problems that use the &tanh() function. I 
was comparing the results to those I got using MATLAB, and after some 
troubleshooting it appears that the difference is the tanh. Specifically, the 
&tanh($x) function in LON-CAPA always returns an imaginary value of zero.

Here is the code:
$tanh               = &tanh($gamma*$len);
$tanh_real      = Re($tanh);
$tanh_imag    = Im($tanh);
where $gamma is a complex number and $len is a scalar. MATLAB computes a 
complex number for tanh in the examples I have tried. Am I doing something 
wrong? Does LC not compute tanh correctly?

Best regards,

Gene L. Harding, PE
Associate Professor of ECET
Purdue University
574-520-4190

_______________________________________________
LON-CAPA-users mailing list
LON-CAPA-users@mail.lon-capa.org
http://mail.lon-capa.org/mailman/listinfo/lon-capa-users

Reply via email to