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 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*_num1_2);
120 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
121 return (Pi*I*_num_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 (is_a<symbol>(t) && t.info(info_flags::real)) {
134 if (t.info(info_flags::numeric)) {
135 const numeric &nt = ex_to<numeric>(t);
141 return log(x).hold();
144 static ex log_deriv(const ex & x, unsigned deriv_param)
146 GINAC_ASSERT(deriv_param==0);
148 // d/dx log(x) -> 1/x
149 return power(x, _ex_1);
152 static ex log_series(const ex &arg,
153 const relational &rel,
157 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
159 bool must_expand_arg = false;
160 // maybe substitution of rel into arg fails because of a pole
162 arg_pt = arg.subs(rel, subs_options::no_pattern);
163 } catch (pole_error) {
164 must_expand_arg = true;
166 // or we are at the branch point anyways
167 if (arg_pt.is_zero())
168 must_expand_arg = true;
170 if (must_expand_arg) {
172 // This is the branch point: Series expand the argument first, then
173 // trivially factorize it to isolate that part which has constant
174 // leading coefficient in this fashion:
175 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
176 // Return a plain n*log(x) for the x^n part and series expand the
177 // other part. Add them together and reexpand again in order to have
178 // one unnested pseries object. All this also works for negative n.
179 pseries argser; // series expansion of log's argument
180 unsigned extra_ord = 0; // extra expansion order
182 // oops, the argument expanded to a pure Order(x^something)...
183 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
185 } while (!argser.is_terminating() && argser.nops()==1);
187 const symbol &s = ex_to<symbol>(rel.lhs());
188 const ex &point = rel.rhs();
189 const int n = argser.ldegree(s);
191 // construct what we carelessly called the n*log(x) term above
192 const ex coeff = argser.coeff(s, n);
193 // expand the log, but only if coeff is real and > 0, since otherwise
194 // it would make the branch cut run into the wrong direction
195 if (coeff.info(info_flags::positive))
196 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
198 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
200 if (!argser.is_terminating() || argser.nops()!=1) {
201 // in this case n more (or less) terms are needed
202 // (sadly, to generate them, we have to start from the beginning)
203 if (n == 0 && coeff == 1) {
205 ex acc = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
207 epv.push_back(expair(-1, _ex0));
208 epv.push_back(expair(Order(_ex1), order));
209 ex rest = pseries(rel, epv).add_series(argser);
210 for (int i = order-1; i>0; --i) {
213 cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
214 acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
215 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
219 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
220 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
221 } else // it was a monomial
222 return pseries(rel, seq);
224 if (!(options & series_options::suppress_branchcut) &&
225 arg_pt.info(info_flags::negative)) {
227 // This is the branch cut: assemble the primitive series manually and
228 // then add the corresponding complex step function.
229 const symbol &s = ex_to<symbol>(rel.lhs());
230 const ex &point = rel.rhs();
232 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
234 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
235 seq.push_back(expair(Order(_ex1), order));
236 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
238 throw do_taylor(); // caught by function::series()
241 REGISTER_FUNCTION(log, eval_func(log_eval).
242 evalf_func(log_evalf).
243 derivative_func(log_deriv).
244 series_func(log_series).
248 // sine (trigonometric function)
251 static ex sin_evalf(const ex & x)
253 if (is_exactly_a<numeric>(x))
254 return sin(ex_to<numeric>(x));
256 return sin(x).hold();
259 static ex sin_eval(const ex & x)
261 // sin(n/d*Pi) -> { all known non-nested radicals }
262 const ex SixtyExOverPi = _ex60*x/Pi;
264 if (SixtyExOverPi.info(info_flags::integer)) {
265 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
267 // wrap to interval [0, Pi)
272 // wrap to interval [0, Pi/2)
275 if (z.is_equal(_num0)) // sin(0) -> 0
277 if (z.is_equal(_num5)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
278 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
279 if (z.is_equal(_num6)) // sin(Pi/10) -> sqrt(5)/4-1/4
280 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
281 if (z.is_equal(_num10)) // sin(Pi/6) -> 1/2
283 if (z.is_equal(_num15)) // sin(Pi/4) -> sqrt(2)/2
284 return sign*_ex1_2*sqrt(_ex2);
285 if (z.is_equal(_num18)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
286 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
287 if (z.is_equal(_num20)) // sin(Pi/3) -> sqrt(3)/2
288 return sign*_ex1_2*sqrt(_ex3);
289 if (z.is_equal(_num25)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
290 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
291 if (z.is_equal(_num30)) // sin(Pi/2) -> 1
295 if (is_exactly_a<function>(x)) {
296 const ex &t = x.op(0);
299 if (is_ex_the_function(x, asin))
302 // sin(acos(x)) -> sqrt(1-x^2)
303 if (is_ex_the_function(x, acos))
304 return sqrt(_ex1-power(t,_ex2));
306 // sin(atan(x)) -> x/sqrt(1+x^2)
307 if (is_ex_the_function(x, atan))
308 return t*power(_ex1+power(t,_ex2),_ex_1_2);
311 // sin(float) -> float
312 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
313 return sin(ex_to<numeric>(x));
316 if (x.info(info_flags::negative))
319 return sin(x).hold();
322 static ex sin_deriv(const ex & x, unsigned deriv_param)
324 GINAC_ASSERT(deriv_param==0);
326 // d/dx sin(x) -> cos(x)
330 REGISTER_FUNCTION(sin, eval_func(sin_eval).
331 evalf_func(sin_evalf).
332 derivative_func(sin_deriv).
333 latex_name("\\sin"));
336 // cosine (trigonometric function)
339 static ex cos_evalf(const ex & x)
341 if (is_exactly_a<numeric>(x))
342 return cos(ex_to<numeric>(x));
344 return cos(x).hold();
347 static ex cos_eval(const ex & x)
349 // cos(n/d*Pi) -> { all known non-nested radicals }
350 const ex SixtyExOverPi = _ex60*x/Pi;
352 if (SixtyExOverPi.info(info_flags::integer)) {
353 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num120);
355 // wrap to interval [0, Pi)
359 // wrap to interval [0, Pi/2)
363 if (z.is_equal(_num0)) // cos(0) -> 1
365 if (z.is_equal(_num5)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
366 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
367 if (z.is_equal(_num10)) // cos(Pi/6) -> sqrt(3)/2
368 return sign*_ex1_2*sqrt(_ex3);
369 if (z.is_equal(_num12)) // cos(Pi/5) -> sqrt(5)/4+1/4
370 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
371 if (z.is_equal(_num15)) // cos(Pi/4) -> sqrt(2)/2
372 return sign*_ex1_2*sqrt(_ex2);
373 if (z.is_equal(_num20)) // cos(Pi/3) -> 1/2
375 if (z.is_equal(_num24)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
376 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
377 if (z.is_equal(_num25)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
378 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
379 if (z.is_equal(_num30)) // cos(Pi/2) -> 0
383 if (is_exactly_a<function>(x)) {
384 const ex &t = x.op(0);
387 if (is_ex_the_function(x, acos))
390 // cos(asin(x)) -> sqrt(1-x^2)
391 if (is_ex_the_function(x, asin))
392 return sqrt(_ex1-power(t,_ex2));
394 // cos(atan(x)) -> 1/sqrt(1+x^2)
395 if (is_ex_the_function(x, atan))
396 return power(_ex1+power(t,_ex2),_ex_1_2);
399 // cos(float) -> float
400 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
401 return cos(ex_to<numeric>(x));
404 if (x.info(info_flags::negative))
407 return cos(x).hold();
410 static ex cos_deriv(const ex & x, unsigned deriv_param)
412 GINAC_ASSERT(deriv_param==0);
414 // d/dx cos(x) -> -sin(x)
418 REGISTER_FUNCTION(cos, eval_func(cos_eval).
419 evalf_func(cos_evalf).
420 derivative_func(cos_deriv).
421 latex_name("\\cos"));
424 // tangent (trigonometric function)
427 static ex tan_evalf(const ex & x)
429 if (is_exactly_a<numeric>(x))
430 return tan(ex_to<numeric>(x));
432 return tan(x).hold();
435 static ex tan_eval(const ex & x)
437 // tan(n/d*Pi) -> { all known non-nested radicals }
438 const ex SixtyExOverPi = _ex60*x/Pi;
440 if (SixtyExOverPi.info(info_flags::integer)) {
441 numeric z = mod(ex_to<numeric>(SixtyExOverPi),_num60);
443 // wrap to interval [0, Pi)
447 // wrap to interval [0, Pi/2)
451 if (z.is_equal(_num0)) // tan(0) -> 0
453 if (z.is_equal(_num5)) // tan(Pi/12) -> 2-sqrt(3)
454 return sign*(_ex2-sqrt(_ex3));
455 if (z.is_equal(_num10)) // tan(Pi/6) -> sqrt(3)/3
456 return sign*_ex1_3*sqrt(_ex3);
457 if (z.is_equal(_num15)) // tan(Pi/4) -> 1
459 if (z.is_equal(_num20)) // tan(Pi/3) -> sqrt(3)
460 return sign*sqrt(_ex3);
461 if (z.is_equal(_num25)) // tan(5/12*Pi) -> 2+sqrt(3)
462 return sign*(sqrt(_ex3)+_ex2);
463 if (z.is_equal(_num30)) // tan(Pi/2) -> infinity
464 throw (pole_error("tan_eval(): simple pole",1));
467 if (is_exactly_a<function>(x)) {
468 const ex &t = x.op(0);
471 if (is_ex_the_function(x, atan))
474 // tan(asin(x)) -> x/sqrt(1+x^2)
475 if (is_ex_the_function(x, asin))
476 return t*power(_ex1-power(t,_ex2),_ex_1_2);
478 // tan(acos(x)) -> sqrt(1-x^2)/x
479 if (is_ex_the_function(x, acos))
480 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
483 // tan(float) -> float
484 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
485 return tan(ex_to<numeric>(x));
489 if (x.info(info_flags::negative))
492 return tan(x).hold();
495 static ex tan_deriv(const ex & x, unsigned deriv_param)
497 GINAC_ASSERT(deriv_param==0);
499 // d/dx tan(x) -> 1+tan(x)^2;
500 return (_ex1+power(tan(x),_ex2));
503 static ex tan_series(const ex &x,
504 const relational &rel,
508 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
510 // Taylor series where there is no pole falls back to tan_deriv.
511 // On a pole simply expand sin(x)/cos(x).
512 const ex x_pt = x.subs(rel, subs_options::no_pattern);
513 if (!(2*x_pt/Pi).info(info_flags::odd))
514 throw do_taylor(); // caught by function::series()
515 // if we got here we have to care for a simple pole
516 return (sin(x)/cos(x)).series(rel, order, options);
519 REGISTER_FUNCTION(tan, eval_func(tan_eval).
520 evalf_func(tan_evalf).
521 derivative_func(tan_deriv).
522 series_func(tan_series).
523 latex_name("\\tan"));
526 // inverse sine (arc sine)
529 static ex asin_evalf(const ex & x)
531 if (is_exactly_a<numeric>(x))
532 return asin(ex_to<numeric>(x));
534 return asin(x).hold();
537 static ex asin_eval(const ex & x)
539 if (x.info(info_flags::numeric)) {
546 if (x.is_equal(_ex1_2))
547 return numeric(1,6)*Pi;
550 if (x.is_equal(_ex1))
553 // asin(-1/2) -> -Pi/6
554 if (x.is_equal(_ex_1_2))
555 return numeric(-1,6)*Pi;
558 if (x.is_equal(_ex_1))
561 // asin(float) -> float
562 if (!x.info(info_flags::crational))
563 return asin(ex_to<numeric>(x));
566 if (x.info(info_flags::negative))
570 return asin(x).hold();
573 static ex asin_deriv(const ex & x, unsigned deriv_param)
575 GINAC_ASSERT(deriv_param==0);
577 // d/dx asin(x) -> 1/sqrt(1-x^2)
578 return power(1-power(x,_ex2),_ex_1_2);
581 REGISTER_FUNCTION(asin, eval_func(asin_eval).
582 evalf_func(asin_evalf).
583 derivative_func(asin_deriv).
584 latex_name("\\arcsin"));
587 // inverse cosine (arc cosine)
590 static ex acos_evalf(const ex & x)
592 if (is_exactly_a<numeric>(x))
593 return acos(ex_to<numeric>(x));
595 return acos(x).hold();
598 static ex acos_eval(const ex & x)
600 if (x.info(info_flags::numeric)) {
603 if (x.is_equal(_ex1))
607 if (x.is_equal(_ex1_2))
614 // acos(-1/2) -> 2/3*Pi
615 if (x.is_equal(_ex_1_2))
616 return numeric(2,3)*Pi;
619 if (x.is_equal(_ex_1))
622 // acos(float) -> float
623 if (!x.info(info_flags::crational))
624 return acos(ex_to<numeric>(x));
626 // acos(-x) -> Pi-acos(x)
627 if (x.info(info_flags::negative))
631 return acos(x).hold();
634 static ex acos_deriv(const ex & x, unsigned deriv_param)
636 GINAC_ASSERT(deriv_param==0);
638 // d/dx acos(x) -> -1/sqrt(1-x^2)
639 return -power(1-power(x,_ex2),_ex_1_2);
642 REGISTER_FUNCTION(acos, eval_func(acos_eval).
643 evalf_func(acos_evalf).
644 derivative_func(acos_deriv).
645 latex_name("\\arccos"));
648 // inverse tangent (arc tangent)
651 static ex atan_evalf(const ex & x)
653 if (is_exactly_a<numeric>(x))
654 return atan(ex_to<numeric>(x));
656 return atan(x).hold();
659 static ex atan_eval(const ex & x)
661 if (x.info(info_flags::numeric)) {
668 if (x.is_equal(_ex1))
672 if (x.is_equal(_ex_1))
675 if (x.is_equal(I) || x.is_equal(-I))
676 throw (pole_error("atan_eval(): logarithmic pole",0));
678 // atan(float) -> float
679 if (!x.info(info_flags::crational))
680 return atan(ex_to<numeric>(x));
683 if (x.info(info_flags::negative))
687 return atan(x).hold();
690 static ex atan_deriv(const ex & x, unsigned deriv_param)
692 GINAC_ASSERT(deriv_param==0);
694 // d/dx atan(x) -> 1/(1+x^2)
695 return power(_ex1+power(x,_ex2), _ex_1);
698 static ex atan_series(const ex &arg,
699 const relational &rel,
703 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
705 // Taylor series where there is no pole or cut falls back to atan_deriv.
706 // There are two branch cuts, one runnig from I up the imaginary axis and
707 // one running from -I down the imaginary axis. The points I and -I are
709 // On the branch cuts and the poles series expand
710 // (log(1+I*x)-log(1-I*x))/(2*I)
712 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
713 if (!(I*arg_pt).info(info_flags::real))
714 throw do_taylor(); // Re(x) != 0
715 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
716 throw do_taylor(); // Re(x) == 0, but abs(x)<1
717 // care for the poles, using the defining formula for atan()...
718 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
719 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
720 if (!(options & series_options::suppress_branchcut)) {
722 // This is the branch cut: assemble the primitive series manually and
723 // then add the corresponding complex step function.
724 const symbol &s = ex_to<symbol>(rel.lhs());
725 const ex &point = rel.rhs();
727 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
728 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
730 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
732 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
734 seq.push_back(expair(Order0correction, _ex0));
735 seq.push_back(expair(Order(_ex1), order));
736 return series(replarg - pseries(rel, seq), rel, order);
741 REGISTER_FUNCTION(atan, eval_func(atan_eval).
742 evalf_func(atan_evalf).
743 derivative_func(atan_deriv).
744 series_func(atan_series).
745 latex_name("\\arctan"));
748 // inverse tangent (atan2(y,x))
751 static ex atan2_evalf(const ex &y, const ex &x)
753 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
754 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
756 return atan2(y, x).hold();
759 static ex atan2_eval(const ex & y, const ex & x)
761 if (y.info(info_flags::numeric) && x.info(info_flags::numeric)) {
769 // atan(0, x), x real and positive -> 0
770 if (x.info(info_flags::positive))
773 // atan(0, x), x real and negative -> -Pi
774 if (x.info(info_flags::negative))
780 // atan(y, 0), y real and positive -> Pi/2
781 if (y.info(info_flags::positive))
784 // atan(y, 0), y real and negative -> -Pi/2
785 if (y.info(info_flags::negative))
791 // atan(y, y), y real and positive -> Pi/4
792 if (y.info(info_flags::positive))
795 // atan(y, y), y real and negative -> -3/4*Pi
796 if (y.info(info_flags::negative))
797 return numeric(-3, 4)*Pi;
800 if (y.is_equal(-x)) {
802 // atan(y, -y), y real and positive -> 3*Pi/4
803 if (y.info(info_flags::positive))
804 return numeric(3, 4)*Pi;
806 // atan(y, -y), y real and negative -> -Pi/4
807 if (y.info(info_flags::negative))
811 // atan(float, float) -> float
812 if (!y.info(info_flags::crational) && !x.info(info_flags::crational))
813 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
815 // atan(real, real) -> atan(y/x) +/- Pi
816 if (y.info(info_flags::real) && x.info(info_flags::real)) {
817 if (x.info(info_flags::positive))
819 else if(y.info(info_flags::positive))
826 return atan2(y, x).hold();
829 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
831 GINAC_ASSERT(deriv_param<2);
833 if (deriv_param==0) {
835 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
838 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
841 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
842 evalf_func(atan2_evalf).
843 derivative_func(atan2_deriv));
846 // hyperbolic sine (trigonometric function)
849 static ex sinh_evalf(const ex & x)
851 if (is_exactly_a<numeric>(x))
852 return sinh(ex_to<numeric>(x));
854 return sinh(x).hold();
857 static ex sinh_eval(const ex & x)
859 if (x.info(info_flags::numeric)) {
865 // sinh(float) -> float
866 if (!x.info(info_flags::crational))
867 return sinh(ex_to<numeric>(x));
870 if (x.info(info_flags::negative))
874 if ((x/Pi).info(info_flags::numeric) &&
875 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
878 if (is_exactly_a<function>(x)) {
879 const ex &t = x.op(0);
881 // sinh(asinh(x)) -> x
882 if (is_ex_the_function(x, asinh))
885 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
886 if (is_ex_the_function(x, acosh))
887 return sqrt(t-_ex1)*sqrt(t+_ex1);
889 // sinh(atanh(x)) -> x/sqrt(1-x^2)
890 if (is_ex_the_function(x, atanh))
891 return t*power(_ex1-power(t,_ex2),_ex_1_2);
894 return sinh(x).hold();
897 static ex sinh_deriv(const ex & x, unsigned deriv_param)
899 GINAC_ASSERT(deriv_param==0);
901 // d/dx sinh(x) -> cosh(x)
905 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
906 evalf_func(sinh_evalf).
907 derivative_func(sinh_deriv).
908 latex_name("\\sinh"));
911 // hyperbolic cosine (trigonometric function)
914 static ex cosh_evalf(const ex & x)
916 if (is_exactly_a<numeric>(x))
917 return cosh(ex_to<numeric>(x));
919 return cosh(x).hold();
922 static ex cosh_eval(const ex & x)
924 if (x.info(info_flags::numeric)) {
930 // cosh(float) -> float
931 if (!x.info(info_flags::crational))
932 return cosh(ex_to<numeric>(x));
935 if (x.info(info_flags::negative))
939 if ((x/Pi).info(info_flags::numeric) &&
940 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
943 if (is_exactly_a<function>(x)) {
944 const ex &t = x.op(0);
946 // cosh(acosh(x)) -> x
947 if (is_ex_the_function(x, acosh))
950 // cosh(asinh(x)) -> sqrt(1+x^2)
951 if (is_ex_the_function(x, asinh))
952 return sqrt(_ex1+power(t,_ex2));
954 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
955 if (is_ex_the_function(x, atanh))
956 return power(_ex1-power(t,_ex2),_ex_1_2);
959 return cosh(x).hold();
962 static ex cosh_deriv(const ex & x, unsigned deriv_param)
964 GINAC_ASSERT(deriv_param==0);
966 // d/dx cosh(x) -> sinh(x)
970 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
971 evalf_func(cosh_evalf).
972 derivative_func(cosh_deriv).
973 latex_name("\\cosh"));
976 // hyperbolic tangent (trigonometric function)
979 static ex tanh_evalf(const ex & x)
981 if (is_exactly_a<numeric>(x))
982 return tanh(ex_to<numeric>(x));
984 return tanh(x).hold();
987 static ex tanh_eval(const ex & x)
989 if (x.info(info_flags::numeric)) {
995 // tanh(float) -> float
996 if (!x.info(info_flags::crational))
997 return tanh(ex_to<numeric>(x));
1000 if (x.info(info_flags::negative))
1004 if ((x/Pi).info(info_flags::numeric) &&
1005 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1008 if (is_exactly_a<function>(x)) {
1009 const ex &t = x.op(0);
1011 // tanh(atanh(x)) -> x
1012 if (is_ex_the_function(x, atanh))
1015 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1016 if (is_ex_the_function(x, asinh))
1017 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1019 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1020 if (is_ex_the_function(x, acosh))
1021 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1024 return tanh(x).hold();
1027 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1029 GINAC_ASSERT(deriv_param==0);
1031 // d/dx tanh(x) -> 1-tanh(x)^2
1032 return _ex1-power(tanh(x),_ex2);
1035 static ex tanh_series(const ex &x,
1036 const relational &rel,
1040 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1042 // Taylor series where there is no pole falls back to tanh_deriv.
1043 // On a pole simply expand sinh(x)/cosh(x).
1044 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1045 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1046 throw do_taylor(); // caught by function::series()
1047 // if we got here we have to care for a simple pole
1048 return (sinh(x)/cosh(x)).series(rel, order, options);
1051 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1052 evalf_func(tanh_evalf).
1053 derivative_func(tanh_deriv).
1054 series_func(tanh_series).
1055 latex_name("\\tanh"));
1058 // inverse hyperbolic sine (trigonometric function)
1061 static ex asinh_evalf(const ex & x)
1063 if (is_exactly_a<numeric>(x))
1064 return asinh(ex_to<numeric>(x));
1066 return asinh(x).hold();
1069 static ex asinh_eval(const ex & x)
1071 if (x.info(info_flags::numeric)) {
1077 // asinh(float) -> float
1078 if (!x.info(info_flags::crational))
1079 return asinh(ex_to<numeric>(x));
1082 if (x.info(info_flags::negative))
1086 return asinh(x).hold();
1089 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1091 GINAC_ASSERT(deriv_param==0);
1093 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1094 return power(_ex1+power(x,_ex2),_ex_1_2);
1097 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1098 evalf_func(asinh_evalf).
1099 derivative_func(asinh_deriv));
1102 // inverse hyperbolic cosine (trigonometric function)
1105 static ex acosh_evalf(const ex & x)
1107 if (is_exactly_a<numeric>(x))
1108 return acosh(ex_to<numeric>(x));
1110 return acosh(x).hold();
1113 static ex acosh_eval(const ex & x)
1115 if (x.info(info_flags::numeric)) {
1117 // acosh(0) -> Pi*I/2
1119 return Pi*I*numeric(1,2);
1122 if (x.is_equal(_ex1))
1125 // acosh(-1) -> Pi*I
1126 if (x.is_equal(_ex_1))
1129 // acosh(float) -> float
1130 if (!x.info(info_flags::crational))
1131 return acosh(ex_to<numeric>(x));
1133 // acosh(-x) -> Pi*I-acosh(x)
1134 if (x.info(info_flags::negative))
1135 return Pi*I-acosh(-x);
1138 return acosh(x).hold();
1141 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1143 GINAC_ASSERT(deriv_param==0);
1145 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1146 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1149 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1150 evalf_func(acosh_evalf).
1151 derivative_func(acosh_deriv));
1154 // inverse hyperbolic tangent (trigonometric function)
1157 static ex atanh_evalf(const ex & x)
1159 if (is_exactly_a<numeric>(x))
1160 return atanh(ex_to<numeric>(x));
1162 return atanh(x).hold();
1165 static ex atanh_eval(const ex & x)
1167 if (x.info(info_flags::numeric)) {
1173 // atanh({+|-}1) -> throw
1174 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1175 throw (pole_error("atanh_eval(): logarithmic pole",0));
1177 // atanh(float) -> float
1178 if (!x.info(info_flags::crational))
1179 return atanh(ex_to<numeric>(x));
1182 if (x.info(info_flags::negative))
1186 return atanh(x).hold();
1189 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1191 GINAC_ASSERT(deriv_param==0);
1193 // d/dx atanh(x) -> 1/(1-x^2)
1194 return power(_ex1-power(x,_ex2),_ex_1);
1197 static ex atanh_series(const ex &arg,
1198 const relational &rel,
1202 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1204 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1205 // There are two branch cuts, one runnig from 1 up the real axis and one
1206 // one running from -1 down the real axis. The points 1 and -1 are poles
1207 // On the branch cuts and the poles series expand
1208 // (log(1+x)-log(1-x))/2
1210 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1211 if (!(arg_pt).info(info_flags::real))
1212 throw do_taylor(); // Im(x) != 0
1213 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1214 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1215 // care for the poles, using the defining formula for atanh()...
1216 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1217 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1218 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1219 if (!(options & series_options::suppress_branchcut)) {
1221 // This is the branch cut: assemble the primitive series manually and
1222 // then add the corresponding complex step function.
1223 const symbol &s = ex_to<symbol>(rel.lhs());
1224 const ex &point = rel.rhs();
1226 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1227 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1229 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1231 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1233 seq.push_back(expair(Order0correction, _ex0));
1234 seq.push_back(expair(Order(_ex1), order));
1235 return series(replarg - pseries(rel, seq), rel, order);
1240 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1241 evalf_func(atanh_evalf).
1242 derivative_func(atanh_deriv).
1243 series_func(atanh_series));
1246 } // namespace GiNaC