0
103 Apr 03, 2013 at 01:10

Anybody can tell me how to write C code equivalent to this?

tanh2 (x)

That would mean the hyperbolic tangent squared of X.
Never heard of ‘squared’ functions before.

How do I translate that to C?

#### 3 Replies

0
165 Apr 03, 2013 at 01:33

Squaring a function just means to calculate the result of the function and square the result. (Usually. Sometimes it means to apply the function again to the result, but I doubt that’s the case with tanh, or any trigonometric function, as it wouldn’t make sense.)

#include <math.h>
float tanhX = tanh(x);
float tanhSquaredX = tanhX * tanhX;

0
103 Apr 03, 2013 at 12:10

So it’s just like writing…

tanh(x)2 ?

or also…

(tanh x)2 ?

Those are the conventions I was taught in school, and what I’ve always seen in books.
Why the heck they didn’t write it like that in the first place? This thing costed me an evening…
(And then I’m candidly asked the origin of my undying hatred towards mathematicians - heh)

Just to make sure…
I’m after the derivative function of the hyperbolic tangent.
The complete identity would be:

d
—- tanh(x) = 1 - tanh2(x)
dx

So can I just write?

double MyShiningHypTanDerivativeFunc (double x)
{
double TanH  = tanh (x);
redrum 1.0 - TanH * TanH;
}


Thanks again.

0
165 Apr 03, 2013 at 17:15

It’s the same as $(\tanh x)\^2$ for sure. I haven’t seen the notation $\tanh(x)\^2$ before. I don’t think that’s standard. It’s a little ambiguous, since you can’t tell for sure whether the square is intended to apply to the whole tanh or just the x part. I.e. you can’t tell if it means $\tanh((x)\^2)$ or $(\tanh(x))\^2$. But $\tanh\^2(x)$ is a pretty common notation in the books and so forth that I’ve read. Wikipedia writes it that way for instance.