1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2001 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 "relational.h"
40 // exponential function
43 static ex exp_evalf(const ex & x)
45 if (is_exactly_a<numeric>(x))
46 return exp(ex_to<numeric>(x));
51 static ex exp_eval(const ex & x)
57 // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
58 const ex TwoExOverPiI=(_ex2()*x)/(Pi*I);
59 if (TwoExOverPiI.info(info_flags::integer)) {
60 numeric z=mod(ex_to<numeric>(TwoExOverPiI),_num4());
61 if (z.is_equal(_num0()))
63 if (z.is_equal(_num1()))
65 if (z.is_equal(_num2()))
67 if (z.is_equal(_num3()))
71 if (is_ex_the_function(x, log))
75 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
76 return exp(ex_to<numeric>(x));
81 static ex exp_deriv(const ex & x, unsigned deriv_param)
83 GINAC_ASSERT(deriv_param==0);
85 // d/dx exp(x) -> exp(x)
89 REGISTER_FUNCTION(exp, eval_func(exp_eval).
90 evalf_func(exp_evalf).
91 derivative_func(exp_deriv).
98 static ex log_evalf(const ex & x)
100 if (is_exactly_a<numeric>(x))
101 return log(ex_to<numeric>(x));
103 return log(x).hold();
106 static ex log_eval(const ex & x)
108 if (x.info(info_flags::numeric)) {
109 if (x.is_zero()) // log(0) -> infinity
110 throw(pole_error("log_eval(): log(0)",0));
111 if (x.info(info_flags::real) && x.info(info_flags::negative))
112 return (log(-x)+I*Pi);
113 if (x.is_equal(_ex1())) // log(1) -> 0
115 if (x.is_equal(I)) // log(I) -> Pi*I/2
116 return (Pi*I*_num1_2());
117 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
118 return (Pi*I*_num_1_2());
120 if (!x.info(info_flags::crational))
121 return log(ex_to<numeric>(x));
123 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
124 if (is_ex_the_function(x, exp)) {
126 if (t.info(info_flags::numeric)) {
127 numeric nt = ex_to<numeric>(t);
133 return log(x).hold();
136 static ex log_deriv(const ex & x, unsigned deriv_param)
138 GINAC_ASSERT(deriv_param==0);
140 // d/dx log(x) -> 1/x
141 return power(x, _ex_1());
144 static ex log_series(const ex &arg,
145 const relational &rel,
149 GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
151 bool must_expand_arg = false;
152 // maybe substitution of rel into arg fails because of a pole
154 arg_pt = arg.subs(rel);
155 } catch (pole_error) {
156 must_expand_arg = true;
158 // or we are at the branch point anyways
159 if (arg_pt.is_zero())
160 must_expand_arg = true;
162 if (must_expand_arg) {
164 // This is the branch point: Series expand the argument first, then
165 // trivially factorize it to isolate that part which has constant
166 // leading coefficient in this fashion:
167 // x^n + Order(x^(n+m)) -> x^n * (1 + Order(x^m)).
168 // Return a plain n*log(x) for the x^n part and series expand the
169 // other part. Add them together and reexpand again in order to have
170 // one unnested pseries object. All this also works for negative n.
171 const pseries argser = ex_to<pseries>(arg.series(rel, order, options));
172 const symbol *s = static_cast<symbol *>(rel.lhs().bp);
173 const ex point = rel.rhs();
174 const int n = argser.ldegree(*s);
176 // construct what we carelessly called the n*log(x) term above
177 ex coeff = argser.coeff(*s, n);
178 // expand the log, but only if coeff is real and > 0, since otherwise
179 // it would make the branch cut run into the wrong direction
180 if (coeff.info(info_flags::positive))
181 seq.push_back(expair(n*log(*s-point)+log(coeff), _ex0()));
183 seq.push_back(expair(log(coeff*pow(*s-point, n)), _ex0()));
184 if (!argser.is_terminating() || argser.nops()!=1) {
185 // in this case n more terms are needed
186 // (sadly, to generate them, we have to start from the beginning)
187 ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
188 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
189 } else // it was a monomial
190 return pseries(rel, seq);
192 if (!(options & series_options::suppress_branchcut) &&
193 arg_pt.info(info_flags::negative)) {
195 // This is the branch cut: assemble the primitive series manually and
196 // then add the corresponding complex step function.
197 const symbol *s = static_cast<symbol *>(rel.lhs().bp);
198 const ex point = rel.rhs();
200 const ex replarg = series(log(arg), *s==foo, order).subs(foo==point);
202 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0()));
203 seq.push_back(expair(Order(_ex1()), order));
204 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
206 throw do_taylor(); // caught by function::series()
209 REGISTER_FUNCTION(log, eval_func(log_eval).
210 evalf_func(log_evalf).
211 derivative_func(log_deriv).
212 series_func(log_series).
216 // sine (trigonometric function)
219 static ex sin_evalf(const ex & x)
221 if (is_exactly_a<numeric>(x))
222 return sin(ex_to<numeric>(x));
224 return sin(x).hold();
227 static ex sin_eval(const ex & x)
229 // sin(n/d*Pi) -> { all known non-nested radicals }
230 const ex SixtyExOverPi = _ex60()*x/Pi;
232 if (SixtyExOverPi.info(info_flags::integer)) {
233 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120());
235 // wrap to interval [0, Pi)
240 // wrap to interval [0, Pi/2)
243 if (z.is_equal(_num0())) // sin(0) -> 0
245 if (z.is_equal(_num5())) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
246 return sign*_ex1_4()*power(_ex6(),_ex1_2())*(_ex1()+_ex_1_3()*power(_ex3(),_ex1_2()));
247 if (z.is_equal(_num6())) // sin(Pi/10) -> sqrt(5)/4-1/4
248 return sign*(_ex1_4()*power(_ex5(),_ex1_2())+_ex_1_4());
249 if (z.is_equal(_num10())) // sin(Pi/6) -> 1/2
250 return sign*_ex1_2();
251 if (z.is_equal(_num15())) // sin(Pi/4) -> sqrt(2)/2
252 return sign*_ex1_2()*power(_ex2(),_ex1_2());
253 if (z.is_equal(_num18())) // sin(3/10*Pi) -> sqrt(5)/4+1/4
254 return sign*(_ex1_4()*power(_ex5(),_ex1_2())+_ex1_4());
255 if (z.is_equal(_num20())) // sin(Pi/3) -> sqrt(3)/2
256 return sign*_ex1_2()*power(_ex3(),_ex1_2());
257 if (z.is_equal(_num25())) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
258 return sign*_ex1_4()*power(_ex6(),_ex1_2())*(_ex1()+_ex1_3()*power(_ex3(),_ex1_2()));
259 if (z.is_equal(_num30())) // sin(Pi/2) -> 1
263 if (is_exactly_a<function>(x)) {
266 if (is_ex_the_function(x, asin))
268 // sin(acos(x)) -> sqrt(1-x^2)
269 if (is_ex_the_function(x, acos))
270 return power(_ex1()-power(t,_ex2()),_ex1_2());
271 // sin(atan(x)) -> x*(1+x^2)^(-1/2)
272 if (is_ex_the_function(x, atan))
273 return t*power(_ex1()+power(t,_ex2()),_ex_1_2());
276 // sin(float) -> float
277 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
278 return sin(ex_to<numeric>(x));
280 return sin(x).hold();
283 static ex sin_deriv(const ex & x, unsigned deriv_param)
285 GINAC_ASSERT(deriv_param==0);
287 // d/dx sin(x) -> cos(x)
291 REGISTER_FUNCTION(sin, eval_func(sin_eval).
292 evalf_func(sin_evalf).
293 derivative_func(sin_deriv).
294 latex_name("\\sin"));
297 // cosine (trigonometric function)
300 static ex cos_evalf(const ex & x)
302 if (is_exactly_a<numeric>(x))
303 return cos(ex_to<numeric>(x));
305 return cos(x).hold();
308 static ex cos_eval(const ex & x)
310 // cos(n/d*Pi) -> { all known non-nested radicals }
311 const ex SixtyExOverPi = _ex60()*x/Pi;
313 if (SixtyExOverPi.info(info_flags::integer)) {
314 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120());
316 // wrap to interval [0, Pi)
320 // wrap to interval [0, Pi/2)
324 if (z.is_equal(_num0())) // cos(0) -> 1
326 if (z.is_equal(_num5())) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
327 return sign*_ex1_4()*power(_ex6(),_ex1_2())*(_ex1()+_ex1_3()*power(_ex3(),_ex1_2()));
328 if (z.is_equal(_num10())) // cos(Pi/6) -> sqrt(3)/2
329 return sign*_ex1_2()*power(_ex3(),_ex1_2());
330 if (z.is_equal(_num12())) // cos(Pi/5) -> sqrt(5)/4+1/4
331 return sign*(_ex1_4()*power(_ex5(),_ex1_2())+_ex1_4());
332 if (z.is_equal(_num15())) // cos(Pi/4) -> sqrt(2)/2
333 return sign*_ex1_2()*power(_ex2(),_ex1_2());
334 if (z.is_equal(_num20())) // cos(Pi/3) -> 1/2
335 return sign*_ex1_2();
336 if (z.is_equal(_num24())) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
337 return sign*(_ex1_4()*power(_ex5(),_ex1_2())+_ex_1_4());
338 if (z.is_equal(_num25())) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
339 return sign*_ex1_4()*power(_ex6(),_ex1_2())*(_ex1()+_ex_1_3()*power(_ex3(),_ex1_2()));
340 if (z.is_equal(_num30())) // cos(Pi/2) -> 0
344 if (is_exactly_a<function>(x)) {
347 if (is_ex_the_function(x, acos))
349 // cos(asin(x)) -> (1-x^2)^(1/2)
350 if (is_ex_the_function(x, asin))
351 return power(_ex1()-power(t,_ex2()),_ex1_2());
352 // cos(atan(x)) -> (1+x^2)^(-1/2)
353 if (is_ex_the_function(x, atan))
354 return power(_ex1()+power(t,_ex2()),_ex_1_2());
357 // cos(float) -> float
358 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
359 return cos(ex_to<numeric>(x));
361 return cos(x).hold();
364 static ex cos_deriv(const ex & x, unsigned deriv_param)
366 GINAC_ASSERT(deriv_param==0);
368 // d/dx cos(x) -> -sin(x)
369 return _ex_1()*sin(x);
372 REGISTER_FUNCTION(cos, eval_func(cos_eval).
373 evalf_func(cos_evalf).
374 derivative_func(cos_deriv).
375 latex_name("\\cos"));
378 // tangent (trigonometric function)
381 static ex tan_evalf(const ex & x)
383 if (is_exactly_a<numeric>(x))
384 return tan(ex_to<numeric>(x));
386 return tan(x).hold();
389 static ex tan_eval(const ex & x)
391 // tan(n/d*Pi) -> { all known non-nested radicals }
392 const ex SixtyExOverPi = _ex60()*x/Pi;
394 if (SixtyExOverPi.info(info_flags::integer)) {
395 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num60());
397 // wrap to interval [0, Pi)
401 // wrap to interval [0, Pi/2)
405 if (z.is_equal(_num0())) // tan(0) -> 0
407 if (z.is_equal(_num5())) // tan(Pi/12) -> 2-sqrt(3)
408 return sign*(_ex2()-power(_ex3(),_ex1_2()));
409 if (z.is_equal(_num10())) // tan(Pi/6) -> sqrt(3)/3
410 return sign*_ex1_3()*power(_ex3(),_ex1_2());
411 if (z.is_equal(_num15())) // tan(Pi/4) -> 1
413 if (z.is_equal(_num20())) // tan(Pi/3) -> sqrt(3)
414 return sign*power(_ex3(),_ex1_2());
415 if (z.is_equal(_num25())) // tan(5/12*Pi) -> 2+sqrt(3)
416 return sign*(power(_ex3(),_ex1_2())+_ex2());
417 if (z.is_equal(_num30())) // tan(Pi/2) -> infinity
418 throw (pole_error("tan_eval(): simple pole",1));
421 if (is_exactly_a<function>(x)) {
424 if (is_ex_the_function(x, atan))
426 // tan(asin(x)) -> x*(1+x^2)^(-1/2)
427 if (is_ex_the_function(x, asin))
428 return t*power(_ex1()-power(t,_ex2()),_ex_1_2());
429 // tan(acos(x)) -> (1-x^2)^(1/2)/x
430 if (is_ex_the_function(x, acos))
431 return power(t,_ex_1())*power(_ex1()-power(t,_ex2()),_ex1_2());
434 // tan(float) -> float
435 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
436 return tan(ex_to<numeric>(x));
439 return tan(x).hold();
442 static ex tan_deriv(const ex & x, unsigned deriv_param)
444 GINAC_ASSERT(deriv_param==0);
446 // d/dx tan(x) -> 1+tan(x)^2;
447 return (_ex1()+power(tan(x),_ex2()));
450 static ex tan_series(const ex &x,
451 const relational &rel,
455 GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
457 // Taylor series where there is no pole falls back to tan_deriv.
458 // On a pole simply expand sin(x)/cos(x).
459 const ex x_pt = x.subs(rel);
460 if (!(2*x_pt/Pi).info(info_flags::odd))
461 throw do_taylor(); // caught by function::series()
462 // if we got here we have to care for a simple pole
463 return (sin(x)/cos(x)).series(rel, order+2, options);
466 REGISTER_FUNCTION(tan, eval_func(tan_eval).
467 evalf_func(tan_evalf).
468 derivative_func(tan_deriv).
469 series_func(tan_series).
470 latex_name("\\tan"));
473 // inverse sine (arc sine)
476 static ex asin_evalf(const ex & x)
478 if (is_exactly_a<numeric>(x))
479 return asin(ex_to<numeric>(x));
481 return asin(x).hold();
484 static ex asin_eval(const ex & x)
486 if (x.info(info_flags::numeric)) {
491 if (x.is_equal(_ex1_2()))
492 return numeric(1,6)*Pi;
494 if (x.is_equal(_ex1()))
496 // asin(-1/2) -> -Pi/6
497 if (x.is_equal(_ex_1_2()))
498 return numeric(-1,6)*Pi;
500 if (x.is_equal(_ex_1()))
501 return _num_1_2()*Pi;
502 // asin(float) -> float
503 if (!x.info(info_flags::crational))
504 return asin(ex_to<numeric>(x));
507 return asin(x).hold();
510 static ex asin_deriv(const ex & x, unsigned deriv_param)
512 GINAC_ASSERT(deriv_param==0);
514 // d/dx asin(x) -> 1/sqrt(1-x^2)
515 return power(1-power(x,_ex2()),_ex_1_2());
518 REGISTER_FUNCTION(asin, eval_func(asin_eval).
519 evalf_func(asin_evalf).
520 derivative_func(asin_deriv).
521 latex_name("\\arcsin"));
524 // inverse cosine (arc cosine)
527 static ex acos_evalf(const ex & x)
529 if (is_exactly_a<numeric>(x))
530 return acos(ex_to<numeric>(x));
532 return acos(x).hold();
535 static ex acos_eval(const ex & x)
537 if (x.info(info_flags::numeric)) {
539 if (x.is_equal(_ex1()))
542 if (x.is_equal(_ex1_2()))
547 // acos(-1/2) -> 2/3*Pi
548 if (x.is_equal(_ex_1_2()))
549 return numeric(2,3)*Pi;
551 if (x.is_equal(_ex_1()))
553 // acos(float) -> float
554 if (!x.info(info_flags::crational))
555 return acos(ex_to<numeric>(x));
558 return acos(x).hold();
561 static ex acos_deriv(const ex & x, unsigned deriv_param)
563 GINAC_ASSERT(deriv_param==0);
565 // d/dx acos(x) -> -1/sqrt(1-x^2)
566 return _ex_1()*power(1-power(x,_ex2()),_ex_1_2());
569 REGISTER_FUNCTION(acos, eval_func(acos_eval).
570 evalf_func(acos_evalf).
571 derivative_func(acos_deriv).
572 latex_name("\\arccos"));
575 // inverse tangent (arc tangent)
578 static ex atan_evalf(const ex & x)
580 if (is_exactly_a<numeric>(x))
581 return atan(ex_to<numeric>(x));
583 return atan(x).hold();
586 static ex atan_eval(const ex & x)
588 if (x.info(info_flags::numeric)) {
593 if (x.is_equal(_ex1()))
596 if (x.is_equal(_ex_1()))
598 if (x.is_equal(I) || x.is_equal(-I))
599 throw (pole_error("atan_eval(): logarithmic pole",0));
600 // atan(float) -> float
601 if (!x.info(info_flags::crational))
602 return atan(ex_to<numeric>(x));
605 return atan(x).hold();
608 static ex atan_deriv(const ex & x, unsigned deriv_param)
610 GINAC_ASSERT(deriv_param==0);
612 // d/dx atan(x) -> 1/(1+x^2)
613 return power(_ex1()+power(x,_ex2()), _ex_1());
616 static ex atan_series(const ex &arg,
617 const relational &rel,
621 GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
623 // Taylor series where there is no pole or cut falls back to atan_deriv.
624 // There are two branch cuts, one runnig from I up the imaginary axis and
625 // one running from -I down the imaginary axis. The points I and -I are
627 // On the branch cuts and the poles series expand
628 // (log(1+I*x)-log(1-I*x))/(2*I)
630 const ex arg_pt = arg.subs(rel);
631 if (!(I*arg_pt).info(info_flags::real))
632 throw do_taylor(); // Re(x) != 0
633 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1())
634 throw do_taylor(); // Re(x) == 0, but abs(x)<1
635 // care for the poles, using the defining formula for atan()...
636 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
637 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
638 if (!(options & series_options::suppress_branchcut)) {
640 // This is the branch cut: assemble the primitive series manually and
641 // then add the corresponding complex step function.
642 const symbol *s = static_cast<symbol *>(rel.lhs().bp);
643 const ex point = rel.rhs();
645 const ex replarg = series(atan(arg), *s==foo, order).subs(foo==point);
646 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2();
647 if ((I*arg_pt)<_ex0())
648 Order0correction += log((I*arg_pt+_ex_1())/(I*arg_pt+_ex1()))*I*_ex_1_2();
650 Order0correction += log((I*arg_pt+_ex1())/(I*arg_pt+_ex_1()))*I*_ex1_2();
652 seq.push_back(expair(Order0correction, _ex0()));
653 seq.push_back(expair(Order(_ex1()), order));
654 return series(replarg - pseries(rel, seq), rel, order);
659 REGISTER_FUNCTION(atan, eval_func(atan_eval).
660 evalf_func(atan_evalf).
661 derivative_func(atan_deriv).
662 series_func(atan_series).
663 latex_name("\\arctan"));
666 // inverse tangent (atan2(y,x))
669 static ex atan2_evalf(const ex &y, const ex &x)
671 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
672 return atan2(ex_to<numeric>(y), ex_to<numeric>(x));
674 return atan2(y, x).hold();
677 static ex atan2_eval(const ex & y, const ex & x)
679 if (y.info(info_flags::numeric) && !y.info(info_flags::crational) &&
680 x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
681 return atan2_evalf(y,x);
684 return atan2(y,x).hold();
687 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
689 GINAC_ASSERT(deriv_param<2);
691 if (deriv_param==0) {
693 return x*power(power(x,_ex2())+power(y,_ex2()),_ex_1());
696 return -y*power(power(x,_ex2())+power(y,_ex2()),_ex_1());
699 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
700 evalf_func(atan2_evalf).
701 derivative_func(atan2_deriv));
704 // hyperbolic sine (trigonometric function)
707 static ex sinh_evalf(const ex & x)
709 if (is_exactly_a<numeric>(x))
710 return sinh(ex_to<numeric>(x));
712 return sinh(x).hold();
715 static ex sinh_eval(const ex & x)
717 if (x.info(info_flags::numeric)) {
718 if (x.is_zero()) // sinh(0) -> 0
720 if (!x.info(info_flags::crational)) // sinh(float) -> float
721 return sinh(ex_to<numeric>(x));
724 if ((x/Pi).info(info_flags::numeric) &&
725 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
728 if (is_exactly_a<function>(x)) {
730 // sinh(asinh(x)) -> x
731 if (is_ex_the_function(x, asinh))
733 // sinh(acosh(x)) -> (x-1)^(1/2) * (x+1)^(1/2)
734 if (is_ex_the_function(x, acosh))
735 return power(t-_ex1(),_ex1_2())*power(t+_ex1(),_ex1_2());
736 // sinh(atanh(x)) -> x*(1-x^2)^(-1/2)
737 if (is_ex_the_function(x, atanh))
738 return t*power(_ex1()-power(t,_ex2()),_ex_1_2());
741 return sinh(x).hold();
744 static ex sinh_deriv(const ex & x, unsigned deriv_param)
746 GINAC_ASSERT(deriv_param==0);
748 // d/dx sinh(x) -> cosh(x)
752 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
753 evalf_func(sinh_evalf).
754 derivative_func(sinh_deriv).
755 latex_name("\\sinh"));
758 // hyperbolic cosine (trigonometric function)
761 static ex cosh_evalf(const ex & x)
763 if (is_exactly_a<numeric>(x))
764 return cosh(ex_to<numeric>(x));
766 return cosh(x).hold();
769 static ex cosh_eval(const ex & x)
771 if (x.info(info_flags::numeric)) {
772 if (x.is_zero()) // cosh(0) -> 1
774 if (!x.info(info_flags::crational)) // cosh(float) -> float
775 return cosh(ex_to<numeric>(x));
778 if ((x/Pi).info(info_flags::numeric) &&
779 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
782 if (is_exactly_a<function>(x)) {
784 // cosh(acosh(x)) -> x
785 if (is_ex_the_function(x, acosh))
787 // cosh(asinh(x)) -> (1+x^2)^(1/2)
788 if (is_ex_the_function(x, asinh))
789 return power(_ex1()+power(t,_ex2()),_ex1_2());
790 // cosh(atanh(x)) -> (1-x^2)^(-1/2)
791 if (is_ex_the_function(x, atanh))
792 return power(_ex1()-power(t,_ex2()),_ex_1_2());
795 return cosh(x).hold();
798 static ex cosh_deriv(const ex & x, unsigned deriv_param)
800 GINAC_ASSERT(deriv_param==0);
802 // d/dx cosh(x) -> sinh(x)
806 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
807 evalf_func(cosh_evalf).
808 derivative_func(cosh_deriv).
809 latex_name("\\cosh"));
812 // hyperbolic tangent (trigonometric function)
815 static ex tanh_evalf(const ex & x)
817 if (is_exactly_a<numeric>(x))
818 return tanh(ex_to<numeric>(x));
820 return tanh(x).hold();
823 static ex tanh_eval(const ex & x)
825 if (x.info(info_flags::numeric)) {
826 if (x.is_zero()) // tanh(0) -> 0
828 if (!x.info(info_flags::crational)) // tanh(float) -> float
829 return tanh(ex_to<numeric>(x));
832 if ((x/Pi).info(info_flags::numeric) &&
833 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
836 if (is_exactly_a<function>(x)) {
838 // tanh(atanh(x)) -> x
839 if (is_ex_the_function(x, atanh))
841 // tanh(asinh(x)) -> x*(1+x^2)^(-1/2)
842 if (is_ex_the_function(x, asinh))
843 return t*power(_ex1()+power(t,_ex2()),_ex_1_2());
844 // tanh(acosh(x)) -> (x-1)^(1/2)*(x+1)^(1/2)/x
845 if (is_ex_the_function(x, acosh))
846 return power(t-_ex1(),_ex1_2())*power(t+_ex1(),_ex1_2())*power(t,_ex_1());
849 return tanh(x).hold();
852 static ex tanh_deriv(const ex & x, unsigned deriv_param)
854 GINAC_ASSERT(deriv_param==0);
856 // d/dx tanh(x) -> 1-tanh(x)^2
857 return _ex1()-power(tanh(x),_ex2());
860 static ex tanh_series(const ex &x,
861 const relational &rel,
865 GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
867 // Taylor series where there is no pole falls back to tanh_deriv.
868 // On a pole simply expand sinh(x)/cosh(x).
869 const ex x_pt = x.subs(rel);
870 if (!(2*I*x_pt/Pi).info(info_flags::odd))
871 throw do_taylor(); // caught by function::series()
872 // if we got here we have to care for a simple pole
873 return (sinh(x)/cosh(x)).series(rel, order+2, options);
876 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
877 evalf_func(tanh_evalf).
878 derivative_func(tanh_deriv).
879 series_func(tanh_series).
880 latex_name("\\tanh"));
883 // inverse hyperbolic sine (trigonometric function)
886 static ex asinh_evalf(const ex & x)
888 if (is_exactly_a<numeric>(x))
889 return asinh(ex_to<numeric>(x));
891 return asinh(x).hold();
894 static ex asinh_eval(const ex & x)
896 if (x.info(info_flags::numeric)) {
900 // asinh(float) -> float
901 if (!x.info(info_flags::crational))
902 return asinh(ex_to<numeric>(x));
905 return asinh(x).hold();
908 static ex asinh_deriv(const ex & x, unsigned deriv_param)
910 GINAC_ASSERT(deriv_param==0);
912 // d/dx asinh(x) -> 1/sqrt(1+x^2)
913 return power(_ex1()+power(x,_ex2()),_ex_1_2());
916 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
917 evalf_func(asinh_evalf).
918 derivative_func(asinh_deriv));
921 // inverse hyperbolic cosine (trigonometric function)
924 static ex acosh_evalf(const ex & x)
926 if (is_exactly_a<numeric>(x))
927 return acosh(ex_to<numeric>(x));
929 return acosh(x).hold();
932 static ex acosh_eval(const ex & x)
934 if (x.info(info_flags::numeric)) {
935 // acosh(0) -> Pi*I/2
937 return Pi*I*numeric(1,2);
939 if (x.is_equal(_ex1()))
942 if (x.is_equal(_ex_1()))
944 // acosh(float) -> float
945 if (!x.info(info_flags::crational))
946 return acosh(ex_to<numeric>(x));
949 return acosh(x).hold();
952 static ex acosh_deriv(const ex & x, unsigned deriv_param)
954 GINAC_ASSERT(deriv_param==0);
956 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
957 return power(x+_ex_1(),_ex_1_2())*power(x+_ex1(),_ex_1_2());
960 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
961 evalf_func(acosh_evalf).
962 derivative_func(acosh_deriv));
965 // inverse hyperbolic tangent (trigonometric function)
968 static ex atanh_evalf(const ex & x)
970 if (is_exactly_a<numeric>(x))
971 return atanh(ex_to<numeric>(x));
973 return atanh(x).hold();
976 static ex atanh_eval(const ex & x)
978 if (x.info(info_flags::numeric)) {
982 // atanh({+|-}1) -> throw
983 if (x.is_equal(_ex1()) || x.is_equal(_ex_1()))
984 throw (pole_error("atanh_eval(): logarithmic pole",0));
985 // atanh(float) -> float
986 if (!x.info(info_flags::crational))
987 return atanh(ex_to<numeric>(x));
990 return atanh(x).hold();
993 static ex atanh_deriv(const ex & x, unsigned deriv_param)
995 GINAC_ASSERT(deriv_param==0);
997 // d/dx atanh(x) -> 1/(1-x^2)
998 return power(_ex1()-power(x,_ex2()),_ex_1());
1001 static ex atanh_series(const ex &arg,
1002 const relational &rel,
1006 GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
1008 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1009 // There are two branch cuts, one runnig from 1 up the real axis and one
1010 // one running from -1 down the real axis. The points 1 and -1 are poles
1011 // On the branch cuts and the poles series expand
1012 // (log(1+x)-log(1-x))/2
1014 const ex arg_pt = arg.subs(rel);
1015 if (!(arg_pt).info(info_flags::real))
1016 throw do_taylor(); // Im(x) != 0
1017 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1())
1018 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1019 // care for the poles, using the defining formula for atanh()...
1020 if (arg_pt.is_equal(_ex1()) || arg_pt.is_equal(_ex_1()))
1021 return ((log(_ex1()+arg)-log(_ex1()-arg))*_ex1_2()).series(rel, order, options);
1022 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1023 if (!(options & series_options::suppress_branchcut)) {
1025 // This is the branch cut: assemble the primitive series manually and
1026 // then add the corresponding complex step function.
1027 const symbol *s = static_cast<symbol *>(rel.lhs().bp);
1028 const ex point = rel.rhs();
1030 const ex replarg = series(atanh(arg), *s==foo, order).subs(foo==point);
1031 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2();
1033 Order0correction += log((arg_pt+_ex_1())/(arg_pt+_ex1()))*_ex1_2();
1035 Order0correction += log((arg_pt+_ex1())/(arg_pt+_ex_1()))*_ex_1_2();
1037 seq.push_back(expair(Order0correction, _ex0()));
1038 seq.push_back(expair(Order(_ex1()), order));
1039 return series(replarg - pseries(rel, seq), rel, order);
1044 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1045 evalf_func(atanh_evalf).
1046 derivative_func(atanh_deriv).
1047 series_func(atanh_series));
1050 } // namespace GiNaC