1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_p);
63 if (z.is_equal(*_num0_p))
65 if (z.is_equal(*_num1_p))
67 if (z.is_equal(*_num2_p))
69 if (z.is_equal(*_num3_p))
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::rational) && x.info(info_flags::negative))
115 return (log(-x)+I*Pi);
116 if (x.is_equal(_ex1)) // log(1) -> 0
118 if (x.is_equal(I)) // log(I) -> Pi*I/2
119 return (Pi*I*_ex1_2);
120 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
121 return (Pi*I*_ex_1_2);
123 // log(float) -> float
124 if (!x.info(info_flags::crational))
125 return log(ex_to<numeric>(x));
128 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
129 if (is_ex_the_function(x, exp)) {
130 const ex &t = x.op(0);
131 if (t.info(info_flags::real))
135 return log(x).hold();
138 static ex log_deriv(const ex & x, unsigned deriv_param)
140 GINAC_ASSERT(deriv_param==0);
142 // d/dx log(x) -> 1/x
143 return power(x, _ex_1);
146 static ex log_series(const ex &arg,
147 const relational &rel,
151 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
153 bool must_expand_arg = false;
154 // maybe substitution of rel into arg fails because of a pole
156 arg_pt = arg.subs(rel, subs_options::no_pattern);
157 } catch (pole_error) {
158 must_expand_arg = true;
160 // or we are at the branch point anyways
161 if (arg_pt.is_zero())
162 must_expand_arg = true;
164 if (must_expand_arg) {
166 // This is the branch point: Series expand the argument first, then
167 // trivially factorize it to isolate that part which has constant
168 // leading coefficient in this fashion:
169 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
170 // Return a plain n*log(x) for the x^n part and series expand the
171 // other part. Add them together and reexpand again in order to have
172 // one unnested pseries object. All this also works for negative n.
173 pseries argser; // series expansion of log's argument
174 unsigned extra_ord = 0; // extra expansion order
176 // oops, the argument expanded to a pure Order(x^something)...
177 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
179 } while (!argser.is_terminating() && argser.nops()==1);
181 const symbol &s = ex_to<symbol>(rel.lhs());
182 const ex &point = rel.rhs();
183 const int n = argser.ldegree(s);
185 // construct what we carelessly called the n*log(x) term above
186 const ex coeff = argser.coeff(s, n);
187 // expand the log, but only if coeff is real and > 0, since otherwise
188 // it would make the branch cut run into the wrong direction
189 if (coeff.info(info_flags::positive))
190 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
192 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
194 if (!argser.is_terminating() || argser.nops()!=1) {
195 // in this case n more (or less) terms are needed
196 // (sadly, to generate them, we have to start from the beginning)
197 if (n == 0 && coeff == 1) {
199 ex acc = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
201 epv.push_back(expair(-1, _ex0));
202 epv.push_back(expair(Order(_ex1), order));
203 ex rest = pseries(rel, epv).add_series(argser);
204 for (int i = order-1; i>0; --i) {
207 cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
208 acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
209 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
213 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
214 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
215 } else // it was a monomial
216 return pseries(rel, seq);
218 if (!(options & series_options::suppress_branchcut) &&
219 arg_pt.info(info_flags::negative)) {
221 // This is the branch cut: assemble the primitive series manually and
222 // then add the corresponding complex step function.
223 const symbol &s = ex_to<symbol>(rel.lhs());
224 const ex &point = rel.rhs();
226 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
228 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
229 seq.push_back(expair(Order(_ex1), order));
230 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
232 throw do_taylor(); // caught by function::series()
235 REGISTER_FUNCTION(log, eval_func(log_eval).
236 evalf_func(log_evalf).
237 derivative_func(log_deriv).
238 series_func(log_series).
242 // sine (trigonometric function)
245 static ex sin_evalf(const ex & x)
247 if (is_exactly_a<numeric>(x))
248 return sin(ex_to<numeric>(x));
250 return sin(x).hold();
253 static ex sin_eval(const ex & x)
255 // sin(n/d*Pi) -> { all known non-nested radicals }
256 const ex SixtyExOverPi = _ex60*x/Pi;
258 if (SixtyExOverPi.info(info_flags::integer)) {
259 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
261 // wrap to interval [0, Pi)
266 // wrap to interval [0, Pi/2)
269 if (z.is_equal(*_num0_p)) // sin(0) -> 0
271 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
272 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
273 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
274 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
275 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
277 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
278 return sign*_ex1_2*sqrt(_ex2);
279 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
280 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
281 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
282 return sign*_ex1_2*sqrt(_ex3);
283 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
284 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
285 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
289 if (is_exactly_a<function>(x)) {
290 const ex &t = x.op(0);
293 if (is_ex_the_function(x, asin))
296 // sin(acos(x)) -> sqrt(1-x^2)
297 if (is_ex_the_function(x, acos))
298 return sqrt(_ex1-power(t,_ex2));
300 // sin(atan(x)) -> x/sqrt(1+x^2)
301 if (is_ex_the_function(x, atan))
302 return t*power(_ex1+power(t,_ex2),_ex_1_2);
305 // sin(float) -> float
306 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
307 return sin(ex_to<numeric>(x));
310 if (x.info(info_flags::negative))
313 return sin(x).hold();
316 static ex sin_deriv(const ex & x, unsigned deriv_param)
318 GINAC_ASSERT(deriv_param==0);
320 // d/dx sin(x) -> cos(x)
324 REGISTER_FUNCTION(sin, eval_func(sin_eval).
325 evalf_func(sin_evalf).
326 derivative_func(sin_deriv).
327 latex_name("\\sin"));
330 // cosine (trigonometric function)
333 static ex cos_evalf(const ex & x)
335 if (is_exactly_a<numeric>(x))
336 return cos(ex_to<numeric>(x));
338 return cos(x).hold();
341 static ex cos_eval(const ex & x)
343 // cos(n/d*Pi) -> { all known non-nested radicals }
344 const ex SixtyExOverPi = _ex60*x/Pi;
346 if (SixtyExOverPi.info(info_flags::integer)) {
347 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
349 // wrap to interval [0, Pi)
353 // wrap to interval [0, Pi/2)
357 if (z.is_equal(*_num0_p)) // cos(0) -> 1
359 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
360 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
361 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
362 return sign*_ex1_2*sqrt(_ex3);
363 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
364 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
365 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
366 return sign*_ex1_2*sqrt(_ex2);
367 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
369 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
370 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
371 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
372 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
373 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
377 if (is_exactly_a<function>(x)) {
378 const ex &t = x.op(0);
381 if (is_ex_the_function(x, acos))
384 // cos(asin(x)) -> sqrt(1-x^2)
385 if (is_ex_the_function(x, asin))
386 return sqrt(_ex1-power(t,_ex2));
388 // cos(atan(x)) -> 1/sqrt(1+x^2)
389 if (is_ex_the_function(x, atan))
390 return power(_ex1+power(t,_ex2),_ex_1_2);
393 // cos(float) -> float
394 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
395 return cos(ex_to<numeric>(x));
398 if (x.info(info_flags::negative))
401 return cos(x).hold();
404 static ex cos_deriv(const ex & x, unsigned deriv_param)
406 GINAC_ASSERT(deriv_param==0);
408 // d/dx cos(x) -> -sin(x)
412 REGISTER_FUNCTION(cos, eval_func(cos_eval).
413 evalf_func(cos_evalf).
414 derivative_func(cos_deriv).
415 latex_name("\\cos"));
418 // tangent (trigonometric function)
421 static ex tan_evalf(const ex & x)
423 if (is_exactly_a<numeric>(x))
424 return tan(ex_to<numeric>(x));
426 return tan(x).hold();
429 static ex tan_eval(const ex & x)
431 // tan(n/d*Pi) -> { all known non-nested radicals }
432 const ex SixtyExOverPi = _ex60*x/Pi;
434 if (SixtyExOverPi.info(info_flags::integer)) {
435 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
437 // wrap to interval [0, Pi)
441 // wrap to interval [0, Pi/2)
445 if (z.is_equal(*_num0_p)) // tan(0) -> 0
447 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
448 return sign*(_ex2-sqrt(_ex3));
449 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
450 return sign*_ex1_3*sqrt(_ex3);
451 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
453 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
454 return sign*sqrt(_ex3);
455 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
456 return sign*(sqrt(_ex3)+_ex2);
457 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
458 throw (pole_error("tan_eval(): simple pole",1));
461 if (is_exactly_a<function>(x)) {
462 const ex &t = x.op(0);
465 if (is_ex_the_function(x, atan))
468 // tan(asin(x)) -> x/sqrt(1+x^2)
469 if (is_ex_the_function(x, asin))
470 return t*power(_ex1-power(t,_ex2),_ex_1_2);
472 // tan(acos(x)) -> sqrt(1-x^2)/x
473 if (is_ex_the_function(x, acos))
474 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
477 // tan(float) -> float
478 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
479 return tan(ex_to<numeric>(x));
483 if (x.info(info_flags::negative))
486 return tan(x).hold();
489 static ex tan_deriv(const ex & x, unsigned deriv_param)
491 GINAC_ASSERT(deriv_param==0);
493 // d/dx tan(x) -> 1+tan(x)^2;
494 return (_ex1+power(tan(x),_ex2));
497 static ex tan_series(const ex &x,
498 const relational &rel,
502 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
504 // Taylor series where there is no pole falls back to tan_deriv.
505 // On a pole simply expand sin(x)/cos(x).
506 const ex x_pt = x.subs(rel, subs_options::no_pattern);
507 if (!(2*x_pt/Pi).info(info_flags::odd))
508 throw do_taylor(); // caught by function::series()
509 // if we got here we have to care for a simple pole
510 return (sin(x)/cos(x)).series(rel, order, options);
513 REGISTER_FUNCTION(tan, eval_func(tan_eval).
514 evalf_func(tan_evalf).
515 derivative_func(tan_deriv).
516 series_func(tan_series).
517 latex_name("\\tan"));
520 // inverse sine (arc sine)
523 static ex asin_evalf(const ex & x)
525 if (is_exactly_a<numeric>(x))
526 return asin(ex_to<numeric>(x));
528 return asin(x).hold();
531 static ex asin_eval(const ex & x)
533 if (x.info(info_flags::numeric)) {
540 if (x.is_equal(_ex1_2))
541 return numeric(1,6)*Pi;
544 if (x.is_equal(_ex1))
547 // asin(-1/2) -> -Pi/6
548 if (x.is_equal(_ex_1_2))
549 return numeric(-1,6)*Pi;
552 if (x.is_equal(_ex_1))
555 // asin(float) -> float
556 if (!x.info(info_flags::crational))
557 return asin(ex_to<numeric>(x));
560 if (x.info(info_flags::negative))
564 return asin(x).hold();
567 static ex asin_deriv(const ex & x, unsigned deriv_param)
569 GINAC_ASSERT(deriv_param==0);
571 // d/dx asin(x) -> 1/sqrt(1-x^2)
572 return power(1-power(x,_ex2),_ex_1_2);
575 REGISTER_FUNCTION(asin, eval_func(asin_eval).
576 evalf_func(asin_evalf).
577 derivative_func(asin_deriv).
578 latex_name("\\arcsin"));
581 // inverse cosine (arc cosine)
584 static ex acos_evalf(const ex & x)
586 if (is_exactly_a<numeric>(x))
587 return acos(ex_to<numeric>(x));
589 return acos(x).hold();
592 static ex acos_eval(const ex & x)
594 if (x.info(info_flags::numeric)) {
597 if (x.is_equal(_ex1))
601 if (x.is_equal(_ex1_2))
608 // acos(-1/2) -> 2/3*Pi
609 if (x.is_equal(_ex_1_2))
610 return numeric(2,3)*Pi;
613 if (x.is_equal(_ex_1))
616 // acos(float) -> float
617 if (!x.info(info_flags::crational))
618 return acos(ex_to<numeric>(x));
620 // acos(-x) -> Pi-acos(x)
621 if (x.info(info_flags::negative))
625 return acos(x).hold();
628 static ex acos_deriv(const ex & x, unsigned deriv_param)
630 GINAC_ASSERT(deriv_param==0);
632 // d/dx acos(x) -> -1/sqrt(1-x^2)
633 return -power(1-power(x,_ex2),_ex_1_2);
636 REGISTER_FUNCTION(acos, eval_func(acos_eval).
637 evalf_func(acos_evalf).
638 derivative_func(acos_deriv).
639 latex_name("\\arccos"));
642 // inverse tangent (arc tangent)
645 static ex atan_evalf(const ex & x)
647 if (is_exactly_a<numeric>(x))
648 return atan(ex_to<numeric>(x));
650 return atan(x).hold();
653 static ex atan_eval(const ex & x)
655 if (x.info(info_flags::numeric)) {
662 if (x.is_equal(_ex1))
666 if (x.is_equal(_ex_1))
669 if (x.is_equal(I) || x.is_equal(-I))
670 throw (pole_error("atan_eval(): logarithmic pole",0));
672 // atan(float) -> float
673 if (!x.info(info_flags::crational))
674 return atan(ex_to<numeric>(x));
677 if (x.info(info_flags::negative))
681 return atan(x).hold();
684 static ex atan_deriv(const ex & x, unsigned deriv_param)
686 GINAC_ASSERT(deriv_param==0);
688 // d/dx atan(x) -> 1/(1+x^2)
689 return power(_ex1+power(x,_ex2), _ex_1);
692 static ex atan_series(const ex &arg,
693 const relational &rel,
697 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
699 // Taylor series where there is no pole or cut falls back to atan_deriv.
700 // There are two branch cuts, one runnig from I up the imaginary axis and
701 // one running from -I down the imaginary axis. The points I and -I are
703 // On the branch cuts and the poles series expand
704 // (log(1+I*x)-log(1-I*x))/(2*I)
706 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
707 if (!(I*arg_pt).info(info_flags::real))
708 throw do_taylor(); // Re(x) != 0
709 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
710 throw do_taylor(); // Re(x) == 0, but abs(x)<1
711 // care for the poles, using the defining formula for atan()...
712 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
713 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
714 if (!(options & series_options::suppress_branchcut)) {
716 // This is the branch cut: assemble the primitive series manually and
717 // then add the corresponding complex step function.
718 const symbol &s = ex_to<symbol>(rel.lhs());
719 const ex &point = rel.rhs();
721 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
722 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
724 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
726 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
728 seq.push_back(expair(Order0correction, _ex0));
729 seq.push_back(expair(Order(_ex1), order));
730 return series(replarg - pseries(rel, seq), rel, order);
735 REGISTER_FUNCTION(atan, eval_func(atan_eval).
736 evalf_func(atan_evalf).
737 derivative_func(atan_deriv).
738 series_func(atan_series).
739 latex_name("\\arctan"));
742 // inverse tangent (atan2(y,x))
745 static ex atan2_evalf(const ex &y, const ex &x)
747 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
748 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
750 return atan2(y, x).hold();
753 static ex atan2_eval(const ex & y, const ex & x)
755 if (y.info(info_flags::numeric) && x.info(info_flags::numeric)) {
763 // atan(0, x), x real and positive -> 0
764 if (x.info(info_flags::positive))
767 // atan(0, x), x real and negative -> -Pi
768 if (x.info(info_flags::negative))
774 // atan(y, 0), y real and positive -> Pi/2
775 if (y.info(info_flags::positive))
778 // atan(y, 0), y real and negative -> -Pi/2
779 if (y.info(info_flags::negative))
785 // atan(y, y), y real and positive -> Pi/4
786 if (y.info(info_flags::positive))
789 // atan(y, y), y real and negative -> -3/4*Pi
790 if (y.info(info_flags::negative))
791 return numeric(-3, 4)*Pi;
794 if (y.is_equal(-x)) {
796 // atan(y, -y), y real and positive -> 3*Pi/4
797 if (y.info(info_flags::positive))
798 return numeric(3, 4)*Pi;
800 // atan(y, -y), y real and negative -> -Pi/4
801 if (y.info(info_flags::negative))
805 // atan(float, float) -> float
806 if (!y.info(info_flags::crational) && !x.info(info_flags::crational))
807 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
809 // atan(real, real) -> atan(y/x) +/- Pi
810 if (y.info(info_flags::real) && x.info(info_flags::real)) {
811 if (x.info(info_flags::positive))
813 else if(y.info(info_flags::positive))
820 return atan2(y, x).hold();
823 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
825 GINAC_ASSERT(deriv_param<2);
827 if (deriv_param==0) {
829 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
832 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
835 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
836 evalf_func(atan2_evalf).
837 derivative_func(atan2_deriv));
840 // hyperbolic sine (trigonometric function)
843 static ex sinh_evalf(const ex & x)
845 if (is_exactly_a<numeric>(x))
846 return sinh(ex_to<numeric>(x));
848 return sinh(x).hold();
851 static ex sinh_eval(const ex & x)
853 if (x.info(info_flags::numeric)) {
859 // sinh(float) -> float
860 if (!x.info(info_flags::crational))
861 return sinh(ex_to<numeric>(x));
864 if (x.info(info_flags::negative))
868 if ((x/Pi).info(info_flags::numeric) &&
869 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
872 if (is_exactly_a<function>(x)) {
873 const ex &t = x.op(0);
875 // sinh(asinh(x)) -> x
876 if (is_ex_the_function(x, asinh))
879 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
880 if (is_ex_the_function(x, acosh))
881 return sqrt(t-_ex1)*sqrt(t+_ex1);
883 // sinh(atanh(x)) -> x/sqrt(1-x^2)
884 if (is_ex_the_function(x, atanh))
885 return t*power(_ex1-power(t,_ex2),_ex_1_2);
888 return sinh(x).hold();
891 static ex sinh_deriv(const ex & x, unsigned deriv_param)
893 GINAC_ASSERT(deriv_param==0);
895 // d/dx sinh(x) -> cosh(x)
899 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
900 evalf_func(sinh_evalf).
901 derivative_func(sinh_deriv).
902 latex_name("\\sinh"));
905 // hyperbolic cosine (trigonometric function)
908 static ex cosh_evalf(const ex & x)
910 if (is_exactly_a<numeric>(x))
911 return cosh(ex_to<numeric>(x));
913 return cosh(x).hold();
916 static ex cosh_eval(const ex & x)
918 if (x.info(info_flags::numeric)) {
924 // cosh(float) -> float
925 if (!x.info(info_flags::crational))
926 return cosh(ex_to<numeric>(x));
929 if (x.info(info_flags::negative))
933 if ((x/Pi).info(info_flags::numeric) &&
934 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
937 if (is_exactly_a<function>(x)) {
938 const ex &t = x.op(0);
940 // cosh(acosh(x)) -> x
941 if (is_ex_the_function(x, acosh))
944 // cosh(asinh(x)) -> sqrt(1+x^2)
945 if (is_ex_the_function(x, asinh))
946 return sqrt(_ex1+power(t,_ex2));
948 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
949 if (is_ex_the_function(x, atanh))
950 return power(_ex1-power(t,_ex2),_ex_1_2);
953 return cosh(x).hold();
956 static ex cosh_deriv(const ex & x, unsigned deriv_param)
958 GINAC_ASSERT(deriv_param==0);
960 // d/dx cosh(x) -> sinh(x)
964 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
965 evalf_func(cosh_evalf).
966 derivative_func(cosh_deriv).
967 latex_name("\\cosh"));
970 // hyperbolic tangent (trigonometric function)
973 static ex tanh_evalf(const ex & x)
975 if (is_exactly_a<numeric>(x))
976 return tanh(ex_to<numeric>(x));
978 return tanh(x).hold();
981 static ex tanh_eval(const ex & x)
983 if (x.info(info_flags::numeric)) {
989 // tanh(float) -> float
990 if (!x.info(info_flags::crational))
991 return tanh(ex_to<numeric>(x));
994 if (x.info(info_flags::negative))
998 if ((x/Pi).info(info_flags::numeric) &&
999 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1002 if (is_exactly_a<function>(x)) {
1003 const ex &t = x.op(0);
1005 // tanh(atanh(x)) -> x
1006 if (is_ex_the_function(x, atanh))
1009 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1010 if (is_ex_the_function(x, asinh))
1011 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1013 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1014 if (is_ex_the_function(x, acosh))
1015 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1018 return tanh(x).hold();
1021 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1023 GINAC_ASSERT(deriv_param==0);
1025 // d/dx tanh(x) -> 1-tanh(x)^2
1026 return _ex1-power(tanh(x),_ex2);
1029 static ex tanh_series(const ex &x,
1030 const relational &rel,
1034 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1036 // Taylor series where there is no pole falls back to tanh_deriv.
1037 // On a pole simply expand sinh(x)/cosh(x).
1038 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1039 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1040 throw do_taylor(); // caught by function::series()
1041 // if we got here we have to care for a simple pole
1042 return (sinh(x)/cosh(x)).series(rel, order, options);
1045 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1046 evalf_func(tanh_evalf).
1047 derivative_func(tanh_deriv).
1048 series_func(tanh_series).
1049 latex_name("\\tanh"));
1052 // inverse hyperbolic sine (trigonometric function)
1055 static ex asinh_evalf(const ex & x)
1057 if (is_exactly_a<numeric>(x))
1058 return asinh(ex_to<numeric>(x));
1060 return asinh(x).hold();
1063 static ex asinh_eval(const ex & x)
1065 if (x.info(info_flags::numeric)) {
1071 // asinh(float) -> float
1072 if (!x.info(info_flags::crational))
1073 return asinh(ex_to<numeric>(x));
1076 if (x.info(info_flags::negative))
1080 return asinh(x).hold();
1083 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1085 GINAC_ASSERT(deriv_param==0);
1087 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1088 return power(_ex1+power(x,_ex2),_ex_1_2);
1091 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1092 evalf_func(asinh_evalf).
1093 derivative_func(asinh_deriv));
1096 // inverse hyperbolic cosine (trigonometric function)
1099 static ex acosh_evalf(const ex & x)
1101 if (is_exactly_a<numeric>(x))
1102 return acosh(ex_to<numeric>(x));
1104 return acosh(x).hold();
1107 static ex acosh_eval(const ex & x)
1109 if (x.info(info_flags::numeric)) {
1111 // acosh(0) -> Pi*I/2
1113 return Pi*I*numeric(1,2);
1116 if (x.is_equal(_ex1))
1119 // acosh(-1) -> Pi*I
1120 if (x.is_equal(_ex_1))
1123 // acosh(float) -> float
1124 if (!x.info(info_flags::crational))
1125 return acosh(ex_to<numeric>(x));
1127 // acosh(-x) -> Pi*I-acosh(x)
1128 if (x.info(info_flags::negative))
1129 return Pi*I-acosh(-x);
1132 return acosh(x).hold();
1135 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1137 GINAC_ASSERT(deriv_param==0);
1139 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1140 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1143 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1144 evalf_func(acosh_evalf).
1145 derivative_func(acosh_deriv));
1148 // inverse hyperbolic tangent (trigonometric function)
1151 static ex atanh_evalf(const ex & x)
1153 if (is_exactly_a<numeric>(x))
1154 return atanh(ex_to<numeric>(x));
1156 return atanh(x).hold();
1159 static ex atanh_eval(const ex & x)
1161 if (x.info(info_flags::numeric)) {
1167 // atanh({+|-}1) -> throw
1168 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1169 throw (pole_error("atanh_eval(): logarithmic pole",0));
1171 // atanh(float) -> float
1172 if (!x.info(info_flags::crational))
1173 return atanh(ex_to<numeric>(x));
1176 if (x.info(info_flags::negative))
1180 return atanh(x).hold();
1183 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1185 GINAC_ASSERT(deriv_param==0);
1187 // d/dx atanh(x) -> 1/(1-x^2)
1188 return power(_ex1-power(x,_ex2),_ex_1);
1191 static ex atanh_series(const ex &arg,
1192 const relational &rel,
1196 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1198 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1199 // There are two branch cuts, one runnig from 1 up the real axis and one
1200 // one running from -1 down the real axis. The points 1 and -1 are poles
1201 // On the branch cuts and the poles series expand
1202 // (log(1+x)-log(1-x))/2
1204 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1205 if (!(arg_pt).info(info_flags::real))
1206 throw do_taylor(); // Im(x) != 0
1207 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1208 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1209 // care for the poles, using the defining formula for atanh()...
1210 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1211 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1212 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1213 if (!(options & series_options::suppress_branchcut)) {
1215 // This is the branch cut: assemble the primitive series manually and
1216 // then add the corresponding complex step function.
1217 const symbol &s = ex_to<symbol>(rel.lhs());
1218 const ex &point = rel.rhs();
1220 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1221 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1223 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1225 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1227 seq.push_back(expair(Order0correction, _ex0));
1228 seq.push_back(expair(Order(_ex1), order));
1229 return series(replarg - pseries(rel, seq), rel, order);
1234 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1235 evalf_func(atanh_evalf).
1236 derivative_func(atanh_deriv).
1237 series_func(atanh_series));
1240 } // namespace GiNaC