[LON-CAPA-users] Hyperbolic Trig Function Problem
Raeburn, Stuart
raeburn at msu.edu
Wed Jan 29 20:50:56 EST 2020
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-bounces at mail.lon-capa.org> on behalf of Harding, Gene L <glhardin at 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
More information about the LON-CAPA-users
mailing list