1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "operators.h"
33 #include "relational.h"
41 // exponential function
44 static ex exp_evalf(const ex & x)
46 if (is_exactly_a<numeric>(x))
47 return exp(ex_to<numeric>(x));
52 static ex exp_eval(const ex & x)
59 // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
60 const ex TwoExOverPiI=(_ex2*x)/(Pi*I);
61 if (TwoExOverPiI.info(info_flags::integer)) {
62 const numeric z = mod(ex_to<numeric>(TwoExOverPiI),_num4);
63 if (z.is_equal(_num0))
65 if (z.is_equal(_num1))
67 if (z.is_equal(_num2))
69 if (z.is_equal(_num3))
74 if (is_ex_the_function(x, log))
77 // exp(float) -> float
78 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
79 return exp(ex_to<numeric>(x));
84 static ex exp_deriv(const ex & x, unsigned deriv_param)
86 GINAC_ASSERT(deriv_param==0);
88 // d/dx exp(x) -> exp(x)
92 REGISTER_FUNCTION(exp, eval_func(exp_eval).
93 evalf_func(exp_evalf).
94 derivative_func(exp_deriv).
101 static ex log_evalf(const ex & x)
103 if (is_exactly_a<numeric>(x))
104 return log(ex_to<numeric>(x));
106 return log(x).hold();
109 static ex log_eval(const ex & x)
111 if (x.info(info_flags::numeric)) {
112 if (x.is_zero()) // log(0) -> infinity
113 throw(pole_error("log_eval(): log(0)",0));
114 if (x.info(info_flags::real) && x.info(info_flags::negative))
115 //if (x.info(info_flags::rational) && x.info(info_flags::negative))
116 return (log(-x)+I*Pi);
117 if (x.is_equal(_ex1)) // log(1) -> 0
119 if (x.is_equal(I)) // log(I) -> Pi*I/2
120 return (Pi*I*_num1_2);
121 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
122 return (Pi*I*_num_1_2);
124 // log(float) -> float
125 if (!x.info(info_flags::crational))
126 return log(ex_to<numeric>(x));
129 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
130 if (is_ex_the_function(x, exp)) {
131 const ex &t = x.op(0);
132 if (is_a<symbol>(t) && t.info(info_flags::real)) {
135 if (t.info(info_flags::numeric)) {
136 const numeric &nt = ex_to<numeric>(t);
142 return log(x).hold();
145 static ex log_deriv(const ex & x, unsigned deriv_param)
147 GINAC_ASSERT(deriv_param==0);
149 // d/dx log(x) -> 1/x
150 return power(x, _ex_1);
153 static ex log_series(const ex &arg,
154 const relational &rel,
158 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
160 bool must_expand_arg = false;
161 // maybe substitution of rel into arg fails because of a pole
163 arg_pt = arg.subs(rel, subs_options::no_pattern);
164 } catch (pole_error) {
165 must_expand_arg = true;
167 // or we are at the branch point anyways
168 if (arg_pt.is_zero())
169 must_expand_arg = true;
171 if (must_expand_arg) {
173 // This is the branch point: Series expand the argument first, then
174 // trivially factorize it to isolate that part which has constant
175 // leading coefficient in this fashion:
176 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
177 // Return a plain n*log(x) for the x^n part and series expand the
178 // other part. Add them together and reexpand again in order to have
179 // one unnested pseries object. All this also works for negative n.
180 pseries argser; // series expansion of log's argument
181 unsigned extra_ord = 0; // extra expansion order
183 // oops, the argument expanded to a pure Order(x^something)...
184 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
186 } while (!argser.is_terminating() && argser.nops()==1);
188 const symbol &s = ex_to<symbol>(rel.lhs());
189 const ex &point = rel.rhs();
190 const int n = argser.ldegree(s);
192 // construct what we carelessly called the n*log(x) term above
193 const ex coeff = argser.coeff(s, n);
194 // expand the log, but only if coeff is real and > 0, since otherwise
195 // it would make the branch cut run into the wrong direction
196 if (coeff.info(info_flags::positive))
197 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
199 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
201 if (!argser.is_terminating() || argser.nops()!=1) {
202 // in this case n more (or less) terms are needed
203 // (sadly, to generate them, we have to start from the beginning)
204 if (n == 0 && coeff == 1) {
206 ex acc = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
208 epv.push_back(expair(-1, _ex0));
209 epv.push_back(expair(Order(_ex1), order));
210 ex rest = pseries(rel, epv).add_series(argser);
211 for (int i = order-1; i>0; --i) {
214 cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
215 acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
216 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
220 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
221 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
222 } else // it was a monomial
223 return pseries(rel, seq);
225 if (!(options & series_options::suppress_branchcut) &&
226 arg_pt.info(info_flags::negative)) {
228 // This is the branch cut: assemble the primitive series manually and
229 // then add the corresponding complex step function.
230 const symbol &s = ex_to<symbol>(rel.lhs());
231 const ex &point = rel.rhs();
233 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
235 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
236 seq.push_back(expair(Order(_ex1), order));
237 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
239 throw do_taylor(); // caught by function::series()
242 REGISTER_FUNCTION(log, eval_func(log_eval).
243 evalf_func(log_evalf).
244 derivative_func(log_deriv).
245 series_func(log_series).
249 // sine (trigonometric function)
252 static ex sin_evalf(const ex & x)
254 if (is_exactly_a<numeric>(x))
255 return sin(ex_to<numeric>(x));
257 return sin(x).hold();
260 static ex sin_eval(const ex & x)
262 // sin(n/d*Pi) -> { all known non-nested radicals }
263 const ex SixtyExOverPi = _ex60*x/Pi;
265 if (SixtyExOverPi.info(info_flags::integer)) {
266 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
268 // wrap to interval [0, Pi)
273 // wrap to interval [0, Pi/2)
276 if (z.is_equal(_num0)) // sin(0) -> 0
278 if (z.is_equal(_num5)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
279 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
280 if (z.is_equal(_num6)) // sin(Pi/10) -> sqrt(5)/4-1/4
281 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
282 if (z.is_equal(_num10)) // sin(Pi/6) -> 1/2
284 if (z.is_equal(_num15)) // sin(Pi/4) -> sqrt(2)/2
285 return sign*_ex1_2*sqrt(_ex2);
286 if (z.is_equal(_num18)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
287 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
288 if (z.is_equal(_num20)) // sin(Pi/3) -> sqrt(3)/2
289 return sign*_ex1_2*sqrt(_ex3);
290 if (z.is_equal(_num25)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
291 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
292 if (z.is_equal(_num30)) // sin(Pi/2) -> 1
296 if (is_exactly_a<function>(x)) {
297 const ex &t = x.op(0);
300 if (is_ex_the_function(x, asin))
303 // sin(acos(x)) -> sqrt(1-x^2)
304 if (is_ex_the_function(x, acos))
305 return sqrt(_ex1-power(t,_ex2));
307 // sin(atan(x)) -> x/sqrt(1+x^2)
308 if (is_ex_the_function(x, atan))
309 return t*power(_ex1+power(t,_ex2),_ex_1_2);
312 // sin(float) -> float
313 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
314 return sin(ex_to<numeric>(x));
317 if (x.info(info_flags::negative))
320 return sin(x).hold();
323 static ex sin_deriv(const ex & x, unsigned deriv_param)
325 GINAC_ASSERT(deriv_param==0);
327 // d/dx sin(x) -> cos(x)
331 REGISTER_FUNCTION(sin, eval_func(sin_eval).
332 evalf_func(sin_evalf).
333 derivative_func(sin_deriv).
334 latex_name("\\sin"));
337 // cosine (trigonometric function)
340 static ex cos_evalf(const ex & x)
342 if (is_exactly_a<numeric>(x))
343 return cos(ex_to<numeric>(x));
345 return cos(x).hold();
348 static ex cos_eval(const ex & x)
350 // cos(n/d*Pi) -> { all known non-nested radicals }
351 const ex SixtyExOverPi = _ex60*x/Pi;
353 if (SixtyExOverPi.info(info_flags::integer)) {
354 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
356 // wrap to interval [0, Pi)
360 // wrap to interval [0, Pi/2)
364 if (z.is_equal(_num0)) // cos(0) -> 1
366 if (z.is_equal(_num5)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
367 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
368 if (z.is_equal(_num10)) // cos(Pi/6) -> sqrt(3)/2
369 return sign*_ex1_2*sqrt(_ex3);
370 if (z.is_equal(_num12)) // cos(Pi/5) -> sqrt(5)/4+1/4
371 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
372 if (z.is_equal(_num15)) // cos(Pi/4) -> sqrt(2)/2
373 return sign*_ex1_2*sqrt(_ex2);
374 if (z.is_equal(_num20)) // cos(Pi/3) -> 1/2
376 if (z.is_equal(_num24)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
377 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
378 if (z.is_equal(_num25)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
379 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
380 if (z.is_equal(_num30)) // cos(Pi/2) -> 0
384 if (is_exactly_a<function>(x)) {
385 const ex &t = x.op(0);
388 if (is_ex_the_function(x, acos))
391 // cos(asin(x)) -> sqrt(1-x^2)
392 if (is_ex_the_function(x, asin))
393 return sqrt(_ex1-power(t,_ex2));
395 // cos(atan(x)) -> 1/sqrt(1+x^2)
396 if (is_ex_the_function(x, atan))
397 return power(_ex1+power(t,_ex2),_ex_1_2);
400 // cos(float) -> float
401 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
402 return cos(ex_to<numeric>(x));
405 if (x.info(info_flags::negative))
408 return cos(x).hold();
411 static ex cos_deriv(const ex & x, unsigned deriv_param)
413 GINAC_ASSERT(deriv_param==0);
415 // d/dx cos(x) -> -sin(x)
419 REGISTER_FUNCTION(cos, eval_func(cos_eval).
420 evalf_func(cos_evalf).
421 derivative_func(cos_deriv).
422 latex_name("\\cos"));
425 // tangent (trigonometric function)
428 static ex tan_evalf(const ex & x)
430 if (is_exactly_a<numeric>(x))
431 return tan(ex_to<numeric>(x));
433 return tan(x).hold();
436 static ex tan_eval(const ex & x)
438 // tan(n/d*Pi) -> { all known non-nested radicals }
439 const ex SixtyExOverPi = _ex60*x/Pi;
441 if (SixtyExOverPi.info(info_flags::integer)) {
442 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num60);
444 // wrap to interval [0, Pi)
448 // wrap to interval [0, Pi/2)
452 if (z.is_equal(_num0)) // tan(0) -> 0
454 if (z.is_equal(_num5)) // tan(Pi/12) -> 2-sqrt(3)
455 return sign*(_ex2-sqrt(_ex3));
456 if (z.is_equal(_num10)) // tan(Pi/6) -> sqrt(3)/3
457 return sign*_ex1_3*sqrt(_ex3);
458 if (z.is_equal(_num15)) // tan(Pi/4) -> 1
460 if (z.is_equal(_num20)) // tan(Pi/3) -> sqrt(3)
461 return sign*sqrt(_ex3);
462 if (z.is_equal(_num25)) // tan(5/12*Pi) -> 2+sqrt(3)
463 return sign*(sqrt(_ex3)+_ex2);
464 if (z.is_equal(_num30)) // tan(Pi/2) -> infinity
465 throw (pole_error("tan_eval(): simple pole",1));
468 if (is_exactly_a<function>(x)) {
469 const ex &t = x.op(0);
472 if (is_ex_the_function(x, atan))
475 // tan(asin(x)) -> x/sqrt(1+x^2)
476 if (is_ex_the_function(x, asin))
477 return t*power(_ex1-power(t,_ex2),_ex_1_2);
479 // tan(acos(x)) -> sqrt(1-x^2)/x
480 if (is_ex_the_function(x, acos))
481 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
484 // tan(float) -> float
485 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
486 return tan(ex_to<numeric>(x));
490 if (x.info(info_flags::negative))
493 return tan(x).hold();
496 static ex tan_deriv(const ex & x, unsigned deriv_param)
498 GINAC_ASSERT(deriv_param==0);
500 // d/dx tan(x) -> 1+tan(x)^2;
501 return (_ex1+power(tan(x),_ex2));
504 static ex tan_series(const ex &x,
505 const relational &rel,
509 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
511 // Taylor series where there is no pole falls back to tan_deriv.
512 // On a pole simply expand sin(x)/cos(x).
513 const ex x_pt = x.subs(rel, subs_options::no_pattern);
514 if (!(2*x_pt/Pi).info(info_flags::odd))
515 throw do_taylor(); // caught by function::series()
516 // if we got here we have to care for a simple pole
517 return (sin(x)/cos(x)).series(rel, order, options);
520 REGISTER_FUNCTION(tan, eval_func(tan_eval).
521 evalf_func(tan_evalf).
522 derivative_func(tan_deriv).
523 series_func(tan_series).
524 latex_name("\\tan"));
527 // inverse sine (arc sine)
530 static ex asin_evalf(const ex & x)
532 if (is_exactly_a<numeric>(x))
533 return asin(ex_to<numeric>(x));
535 return asin(x).hold();
538 static ex asin_eval(const ex & x)
540 if (x.info(info_flags::numeric)) {
547 if (x.is_equal(_ex1_2))
548 return numeric(1,6)*Pi;
551 if (x.is_equal(_ex1))
554 // asin(-1/2) -> -Pi/6
555 if (x.is_equal(_ex_1_2))
556 return numeric(-1,6)*Pi;
559 if (x.is_equal(_ex_1))
562 // asin(float) -> float
563 if (!x.info(info_flags::crational))
564 return asin(ex_to<numeric>(x));
567 if (x.info(info_flags::negative))
571 return asin(x).hold();
574 static ex asin_deriv(const ex & x, unsigned deriv_param)
576 GINAC_ASSERT(deriv_param==0);
578 // d/dx asin(x) -> 1/sqrt(1-x^2)
579 return power(1-power(x,_ex2),_ex_1_2);
582 REGISTER_FUNCTION(asin, eval_func(asin_eval).
583 evalf_func(asin_evalf).
584 derivative_func(asin_deriv).
585 latex_name("\\arcsin"));
588 // inverse cosine (arc cosine)
591 static ex acos_evalf(const ex & x)
593 if (is_exactly_a<numeric>(x))
594 return acos(ex_to<numeric>(x));
596 return acos(x).hold();
599 static ex acos_eval(const ex & x)
601 if (x.info(info_flags::numeric)) {
604 if (x.is_equal(_ex1))
608 if (x.is_equal(_ex1_2))
615 // acos(-1/2) -> 2/3*Pi
616 if (x.is_equal(_ex_1_2))
617 return numeric(2,3)*Pi;
620 if (x.is_equal(_ex_1))
623 // acos(float) -> float
624 if (!x.info(info_flags::crational))
625 return acos(ex_to<numeric>(x));
627 // acos(-x) -> Pi-acos(x)
628 if (x.info(info_flags::negative))
632 return acos(x).hold();
635 static ex acos_deriv(const ex & x, unsigned deriv_param)
637 GINAC_ASSERT(deriv_param==0);
639 // d/dx acos(x) -> -1/sqrt(1-x^2)
640 return -power(1-power(x,_ex2),_ex_1_2);
643 REGISTER_FUNCTION(acos, eval_func(acos_eval).
644 evalf_func(acos_evalf).
645 derivative_func(acos_deriv).
646 latex_name("\\arccos"));
649 // inverse tangent (arc tangent)
652 static ex atan_evalf(const ex & x)
654 if (is_exactly_a<numeric>(x))
655 return atan(ex_to<numeric>(x));
657 return atan(x).hold();
660 static ex atan_eval(const ex & x)
662 if (x.info(info_flags::numeric)) {
669 if (x.is_equal(_ex1))
673 if (x.is_equal(_ex_1))
676 if (x.is_equal(I) || x.is_equal(-I))
677 throw (pole_error("atan_eval(): logarithmic pole",0));
679 // atan(float) -> float
680 if (!x.info(info_flags::crational))
681 return atan(ex_to<numeric>(x));
684 if (x.info(info_flags::negative))
688 return atan(x).hold();
691 static ex atan_deriv(const ex & x, unsigned deriv_param)
693 GINAC_ASSERT(deriv_param==0);
695 // d/dx atan(x) -> 1/(1+x^2)
696 return power(_ex1+power(x,_ex2), _ex_1);
699 static ex atan_series(const ex &arg,
700 const relational &rel,
704 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
706 // Taylor series where there is no pole or cut falls back to atan_deriv.
707 // There are two branch cuts, one runnig from I up the imaginary axis and
708 // one running from -I down the imaginary axis. The points I and -I are
710 // On the branch cuts and the poles series expand
711 // (log(1+I*x)-log(1-I*x))/(2*I)
713 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
714 if (!(I*arg_pt).info(info_flags::real))
715 throw do_taylor(); // Re(x) != 0
716 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
717 throw do_taylor(); // Re(x) == 0, but abs(x)<1
718 // care for the poles, using the defining formula for atan()...
719 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
720 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
721 if (!(options & series_options::suppress_branchcut)) {
723 // This is the branch cut: assemble the primitive series manually and
724 // then add the corresponding complex step function.
725 const symbol &s = ex_to<symbol>(rel.lhs());
726 const ex &point = rel.rhs();
728 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
729 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
731 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
733 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
735 seq.push_back(expair(Order0correction, _ex0));
736 seq.push_back(expair(Order(_ex1), order));
737 return series(replarg - pseries(rel, seq), rel, order);
742 REGISTER_FUNCTION(atan, eval_func(atan_eval).
743 evalf_func(atan_evalf).
744 derivative_func(atan_deriv).
745 series_func(atan_series).
746 latex_name("\\arctan"));
749 // inverse tangent (atan2(y,x))
752 static ex atan2_evalf(const ex &y, const ex &x)
754 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
755 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
757 return atan2(y, x).hold();
760 static ex atan2_eval(const ex & y, const ex & x)
762 if (y.info(info_flags::numeric) && x.info(info_flags::numeric)) {
770 // atan(0, x), x real and positive -> 0
771 if (x.info(info_flags::positive))
774 // atan(0, x), x real and negative -> -Pi
775 if (x.info(info_flags::negative))
781 // atan(y, 0), y real and positive -> Pi/2
782 if (y.info(info_flags::positive))
785 // atan(y, 0), y real and negative -> -Pi/2
786 if (y.info(info_flags::negative))
792 // atan(y, y), y real and positive -> Pi/4
793 if (y.info(info_flags::positive))
796 // atan(y, y), y real and negative -> -3/4*Pi
797 if (y.info(info_flags::negative))
798 return numeric(-3, 4)*Pi;
801 if (y.is_equal(-x)) {
803 // atan(y, -y), y real and positive -> 3*Pi/4
804 if (y.info(info_flags::positive))
805 return numeric(3, 4)*Pi;
807 // atan(y, -y), y real and negative -> -Pi/4
808 if (y.info(info_flags::negative))
812 // atan(float, float) -> float
813 if (!y.info(info_flags::crational) && !x.info(info_flags::crational))
814 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
816 // atan(real, real) -> atan(y/x) +/- Pi
817 if (y.info(info_flags::real) && x.info(info_flags::real)) {
818 if (x.info(info_flags::positive))
820 else if(y.info(info_flags::positive))
827 return atan2(y, x).hold();
830 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
832 GINAC_ASSERT(deriv_param<2);
834 if (deriv_param==0) {
836 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
839 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
842 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
843 evalf_func(atan2_evalf).
844 derivative_func(atan2_deriv));
847 // hyperbolic sine (trigonometric function)
850 static ex sinh_evalf(const ex & x)
852 if (is_exactly_a<numeric>(x))
853 return sinh(ex_to<numeric>(x));
855 return sinh(x).hold();
858 static ex sinh_eval(const ex & x)
860 if (x.info(info_flags::numeric)) {
866 // sinh(float) -> float
867 if (!x.info(info_flags::crational))
868 return sinh(ex_to<numeric>(x));
871 if (x.info(info_flags::negative))
875 if ((x/Pi).info(info_flags::numeric) &&
876 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
879 if (is_exactly_a<function>(x)) {
880 const ex &t = x.op(0);
882 // sinh(asinh(x)) -> x
883 if (is_ex_the_function(x, asinh))
886 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
887 if (is_ex_the_function(x, acosh))
888 return sqrt(t-_ex1)*sqrt(t+_ex1);
890 // sinh(atanh(x)) -> x/sqrt(1-x^2)
891 if (is_ex_the_function(x, atanh))
892 return t*power(_ex1-power(t,_ex2),_ex_1_2);
895 return sinh(x).hold();
898 static ex sinh_deriv(const ex & x, unsigned deriv_param)
900 GINAC_ASSERT(deriv_param==0);
902 // d/dx sinh(x) -> cosh(x)
906 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
907 evalf_func(sinh_evalf).
908 derivative_func(sinh_deriv).
909 latex_name("\\sinh"));
912 // hyperbolic cosine (trigonometric function)
915 static ex cosh_evalf(const ex & x)
917 if (is_exactly_a<numeric>(x))
918 return cosh(ex_to<numeric>(x));
920 return cosh(x).hold();
923 static ex cosh_eval(const ex & x)
925 if (x.info(info_flags::numeric)) {
931 // cosh(float) -> float
932 if (!x.info(info_flags::crational))
933 return cosh(ex_to<numeric>(x));
936 if (x.info(info_flags::negative))
940 if ((x/Pi).info(info_flags::numeric) &&
941 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
944 if (is_exactly_a<function>(x)) {
945 const ex &t = x.op(0);
947 // cosh(acosh(x)) -> x
948 if (is_ex_the_function(x, acosh))
951 // cosh(asinh(x)) -> sqrt(1+x^2)
952 if (is_ex_the_function(x, asinh))
953 return sqrt(_ex1+power(t,_ex2));
955 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
956 if (is_ex_the_function(x, atanh))
957 return power(_ex1-power(t,_ex2),_ex_1_2);
960 return cosh(x).hold();
963 static ex cosh_deriv(const ex & x, unsigned deriv_param)
965 GINAC_ASSERT(deriv_param==0);
967 // d/dx cosh(x) -> sinh(x)
971 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
972 evalf_func(cosh_evalf).
973 derivative_func(cosh_deriv).
974 latex_name("\\cosh"));
977 // hyperbolic tangent (trigonometric function)
980 static ex tanh_evalf(const ex & x)
982 if (is_exactly_a<numeric>(x))
983 return tanh(ex_to<numeric>(x));
985 return tanh(x).hold();
988 static ex tanh_eval(const ex & x)
990 if (x.info(info_flags::numeric)) {
996 // tanh(float) -> float
997 if (!x.info(info_flags::crational))
998 return tanh(ex_to<numeric>(x));
1001 if (x.info(info_flags::negative))
1005 if ((x/Pi).info(info_flags::numeric) &&
1006 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1009 if (is_exactly_a<function>(x)) {
1010 const ex &t = x.op(0);
1012 // tanh(atanh(x)) -> x
1013 if (is_ex_the_function(x, atanh))
1016 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1017 if (is_ex_the_function(x, asinh))
1018 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1020 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1021 if (is_ex_the_function(x, acosh))
1022 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1025 return tanh(x).hold();
1028 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1030 GINAC_ASSERT(deriv_param==0);
1032 // d/dx tanh(x) -> 1-tanh(x)^2
1033 return _ex1-power(tanh(x),_ex2);
1036 static ex tanh_series(const ex &x,
1037 const relational &rel,
1041 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1043 // Taylor series where there is no pole falls back to tanh_deriv.
1044 // On a pole simply expand sinh(x)/cosh(x).
1045 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1046 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1047 throw do_taylor(); // caught by function::series()
1048 // if we got here we have to care for a simple pole
1049 return (sinh(x)/cosh(x)).series(rel, order, options);
1052 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1053 evalf_func(tanh_evalf).
1054 derivative_func(tanh_deriv).
1055 series_func(tanh_series).
1056 latex_name("\\tanh"));
1059 // inverse hyperbolic sine (trigonometric function)
1062 static ex asinh_evalf(const ex & x)
1064 if (is_exactly_a<numeric>(x))
1065 return asinh(ex_to<numeric>(x));
1067 return asinh(x).hold();
1070 static ex asinh_eval(const ex & x)
1072 if (x.info(info_flags::numeric)) {
1078 // asinh(float) -> float
1079 if (!x.info(info_flags::crational))
1080 return asinh(ex_to<numeric>(x));
1083 if (x.info(info_flags::negative))
1087 return asinh(x).hold();
1090 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1092 GINAC_ASSERT(deriv_param==0);
1094 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1095 return power(_ex1+power(x,_ex2),_ex_1_2);
1098 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1099 evalf_func(asinh_evalf).
1100 derivative_func(asinh_deriv));
1103 // inverse hyperbolic cosine (trigonometric function)
1106 static ex acosh_evalf(const ex & x)
1108 if (is_exactly_a<numeric>(x))
1109 return acosh(ex_to<numeric>(x));
1111 return acosh(x).hold();
1114 static ex acosh_eval(const ex & x)
1116 if (x.info(info_flags::numeric)) {
1118 // acosh(0) -> Pi*I/2
1120 return Pi*I*numeric(1,2);
1123 if (x.is_equal(_ex1))
1126 // acosh(-1) -> Pi*I
1127 if (x.is_equal(_ex_1))
1130 // acosh(float) -> float
1131 if (!x.info(info_flags::crational))
1132 return acosh(ex_to<numeric>(x));
1134 // acosh(-x) -> Pi*I-acosh(x)
1135 if (x.info(info_flags::negative))
1136 return Pi*I-acosh(-x);
1139 return acosh(x).hold();
1142 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1144 GINAC_ASSERT(deriv_param==0);
1146 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1147 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1150 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1151 evalf_func(acosh_evalf).
1152 derivative_func(acosh_deriv));
1155 // inverse hyperbolic tangent (trigonometric function)
1158 static ex atanh_evalf(const ex & x)
1160 if (is_exactly_a<numeric>(x))
1161 return atanh(ex_to<numeric>(x));
1163 return atanh(x).hold();
1166 static ex atanh_eval(const ex & x)
1168 if (x.info(info_flags::numeric)) {
1174 // atanh({+|-}1) -> throw
1175 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1176 throw (pole_error("atanh_eval(): logarithmic pole",0));
1178 // atanh(float) -> float
1179 if (!x.info(info_flags::crational))
1180 return atanh(ex_to<numeric>(x));
1183 if (x.info(info_flags::negative))
1187 return atanh(x).hold();
1190 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1192 GINAC_ASSERT(deriv_param==0);
1194 // d/dx atanh(x) -> 1/(1-x^2)
1195 return power(_ex1-power(x,_ex2),_ex_1);
1198 static ex atanh_series(const ex &arg,
1199 const relational &rel,
1203 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1205 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1206 // There are two branch cuts, one runnig from 1 up the real axis and one
1207 // one running from -1 down the real axis. The points 1 and -1 are poles
1208 // On the branch cuts and the poles series expand
1209 // (log(1+x)-log(1-x))/2
1211 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1212 if (!(arg_pt).info(info_flags::real))
1213 throw do_taylor(); // Im(x) != 0
1214 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1215 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1216 // care for the poles, using the defining formula for atanh()...
1217 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1218 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1219 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1220 if (!(options & series_options::suppress_branchcut)) {
1222 // This is the branch cut: assemble the primitive series manually and
1223 // then add the corresponding complex step function.
1224 const symbol &s = ex_to<symbol>(rel.lhs());
1225 const ex &point = rel.rhs();
1227 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1228 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1230 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1232 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1234 seq.push_back(expair(Order0correction, _ex0));
1235 seq.push_back(expair(Order(_ex1), order));
1236 return series(replarg - pseries(rel, seq), rel, order);
1241 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1242 evalf_func(atanh_evalf).
1243 derivative_func(atanh_deriv).
1244 series_func(atanh_series));
1247 } // namespace GiNaC