1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2011 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
29 #include "operators.h"
30 #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 static ex exp_real_part(const ex & x)
94 return exp(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
97 static ex exp_imag_part(const ex & x)
99 return exp(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
102 static ex exp_conjugate(const ex & x)
104 // conjugate(exp(x))==exp(conjugate(x))
105 return exp(x.conjugate());
108 REGISTER_FUNCTION(exp, eval_func(exp_eval).
109 evalf_func(exp_evalf).
110 derivative_func(exp_deriv).
111 real_part_func(exp_real_part).
112 imag_part_func(exp_imag_part).
113 conjugate_func(exp_conjugate).
114 latex_name("\\exp"));
120 static ex log_evalf(const ex & x)
122 if (is_exactly_a<numeric>(x))
123 return log(ex_to<numeric>(x));
125 return log(x).hold();
128 static ex log_eval(const ex & x)
130 if (x.info(info_flags::numeric)) {
131 if (x.is_zero()) // log(0) -> infinity
132 throw(pole_error("log_eval(): log(0)",0));
133 if (x.info(info_flags::rational) && x.info(info_flags::negative))
134 return (log(-x)+I*Pi);
135 if (x.is_equal(_ex1)) // log(1) -> 0
137 if (x.is_equal(I)) // log(I) -> Pi*I/2
138 return (Pi*I*_ex1_2);
139 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
140 return (Pi*I*_ex_1_2);
142 // log(float) -> float
143 if (!x.info(info_flags::crational))
144 return log(ex_to<numeric>(x));
147 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
148 if (is_ex_the_function(x, exp)) {
149 const ex &t = x.op(0);
150 if (t.info(info_flags::real))
154 return log(x).hold();
157 static ex log_deriv(const ex & x, unsigned deriv_param)
159 GINAC_ASSERT(deriv_param==0);
161 // d/dx log(x) -> 1/x
162 return power(x, _ex_1);
165 static ex log_series(const ex &arg,
166 const relational &rel,
170 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
172 bool must_expand_arg = false;
173 // maybe substitution of rel into arg fails because of a pole
175 arg_pt = arg.subs(rel, subs_options::no_pattern);
176 } catch (pole_error) {
177 must_expand_arg = true;
179 // or we are at the branch point anyways
180 if (arg_pt.is_zero())
181 must_expand_arg = true;
183 if (must_expand_arg) {
185 // This is the branch point: Series expand the argument first, then
186 // trivially factorize it to isolate that part which has constant
187 // leading coefficient in this fashion:
188 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
189 // Return a plain n*log(x) for the x^n part and series expand the
190 // other part. Add them together and reexpand again in order to have
191 // one unnested pseries object. All this also works for negative n.
192 pseries argser; // series expansion of log's argument
193 unsigned extra_ord = 0; // extra expansion order
195 // oops, the argument expanded to a pure Order(x^something)...
196 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
198 } while (!argser.is_terminating() && argser.nops()==1);
200 const symbol &s = ex_to<symbol>(rel.lhs());
201 const ex &point = rel.rhs();
202 const int n = argser.ldegree(s);
204 // construct what we carelessly called the n*log(x) term above
205 const ex coeff = argser.coeff(s, n);
206 // expand the log, but only if coeff is real and > 0, since otherwise
207 // it would make the branch cut run into the wrong direction
208 if (coeff.info(info_flags::positive))
209 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
211 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
213 if (!argser.is_terminating() || argser.nops()!=1) {
214 // in this case n more (or less) terms are needed
215 // (sadly, to generate them, we have to start from the beginning)
216 if (n == 0 && coeff == 1) {
218 ex acc = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
220 epv.push_back(expair(-1, _ex0));
221 epv.push_back(expair(Order(_ex1), order));
222 ex rest = pseries(rel, epv).add_series(argser);
223 for (int i = order-1; i>0; --i) {
226 cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
227 acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
228 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
232 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
233 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
234 } else // it was a monomial
235 return pseries(rel, seq);
237 if (!(options & series_options::suppress_branchcut) &&
238 arg_pt.info(info_flags::negative)) {
240 // This is the branch cut: assemble the primitive series manually and
241 // then add the corresponding complex step function.
242 const symbol &s = ex_to<symbol>(rel.lhs());
243 const ex &point = rel.rhs();
245 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
247 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
248 seq.push_back(expair(Order(_ex1), order));
249 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
251 throw do_taylor(); // caught by function::series()
254 static ex log_real_part(const ex & x)
256 if (x.info(info_flags::nonnegative))
257 return log(x).hold();
261 static ex log_imag_part(const ex & x)
263 if (x.info(info_flags::nonnegative))
265 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
268 static ex log_conjugate(const ex & x)
270 // conjugate(log(x))==log(conjugate(x)) unless on the branch cut which
271 // runs along the negative real axis.
272 if (x.info(info_flags::positive)) {
275 if (is_exactly_a<numeric>(x) &&
276 !x.imag_part().is_zero()) {
277 return log(x.conjugate());
279 return conjugate_function(log(x)).hold();
282 REGISTER_FUNCTION(log, eval_func(log_eval).
283 evalf_func(log_evalf).
284 derivative_func(log_deriv).
285 series_func(log_series).
286 real_part_func(log_real_part).
287 imag_part_func(log_imag_part).
288 conjugate_func(log_conjugate).
292 // sine (trigonometric function)
295 static ex sin_evalf(const ex & x)
297 if (is_exactly_a<numeric>(x))
298 return sin(ex_to<numeric>(x));
300 return sin(x).hold();
303 static ex sin_eval(const ex & x)
305 // sin(n/d*Pi) -> { all known non-nested radicals }
306 const ex SixtyExOverPi = _ex60*x/Pi;
308 if (SixtyExOverPi.info(info_flags::integer)) {
309 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
311 // wrap to interval [0, Pi)
316 // wrap to interval [0, Pi/2)
319 if (z.is_equal(*_num0_p)) // sin(0) -> 0
321 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
322 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
323 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
324 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
325 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
327 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
328 return sign*_ex1_2*sqrt(_ex2);
329 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
330 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
331 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
332 return sign*_ex1_2*sqrt(_ex3);
333 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
334 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
335 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
339 if (is_exactly_a<function>(x)) {
340 const ex &t = x.op(0);
343 if (is_ex_the_function(x, asin))
346 // sin(acos(x)) -> sqrt(1-x^2)
347 if (is_ex_the_function(x, acos))
348 return sqrt(_ex1-power(t,_ex2));
350 // sin(atan(x)) -> x/sqrt(1+x^2)
351 if (is_ex_the_function(x, atan))
352 return t*power(_ex1+power(t,_ex2),_ex_1_2);
355 // sin(float) -> float
356 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
357 return sin(ex_to<numeric>(x));
360 if (x.info(info_flags::negative))
363 return sin(x).hold();
366 static ex sin_deriv(const ex & x, unsigned deriv_param)
368 GINAC_ASSERT(deriv_param==0);
370 // d/dx sin(x) -> cos(x)
374 static ex sin_real_part(const ex & x)
376 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
379 static ex sin_imag_part(const ex & x)
381 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
384 static ex sin_conjugate(const ex & x)
386 // conjugate(sin(x))==sin(conjugate(x))
387 return sin(x.conjugate());
390 REGISTER_FUNCTION(sin, eval_func(sin_eval).
391 evalf_func(sin_evalf).
392 derivative_func(sin_deriv).
393 real_part_func(sin_real_part).
394 imag_part_func(sin_imag_part).
395 conjugate_func(sin_conjugate).
396 latex_name("\\sin"));
399 // cosine (trigonometric function)
402 static ex cos_evalf(const ex & x)
404 if (is_exactly_a<numeric>(x))
405 return cos(ex_to<numeric>(x));
407 return cos(x).hold();
410 static ex cos_eval(const ex & x)
412 // cos(n/d*Pi) -> { all known non-nested radicals }
413 const ex SixtyExOverPi = _ex60*x/Pi;
415 if (SixtyExOverPi.info(info_flags::integer)) {
416 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
418 // wrap to interval [0, Pi)
422 // wrap to interval [0, Pi/2)
426 if (z.is_equal(*_num0_p)) // cos(0) -> 1
428 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
429 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
430 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
431 return sign*_ex1_2*sqrt(_ex3);
432 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
433 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
434 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
435 return sign*_ex1_2*sqrt(_ex2);
436 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
438 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
439 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
440 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
441 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
442 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
446 if (is_exactly_a<function>(x)) {
447 const ex &t = x.op(0);
450 if (is_ex_the_function(x, acos))
453 // cos(asin(x)) -> sqrt(1-x^2)
454 if (is_ex_the_function(x, asin))
455 return sqrt(_ex1-power(t,_ex2));
457 // cos(atan(x)) -> 1/sqrt(1+x^2)
458 if (is_ex_the_function(x, atan))
459 return power(_ex1+power(t,_ex2),_ex_1_2);
462 // cos(float) -> float
463 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
464 return cos(ex_to<numeric>(x));
467 if (x.info(info_flags::negative))
470 return cos(x).hold();
473 static ex cos_deriv(const ex & x, unsigned deriv_param)
475 GINAC_ASSERT(deriv_param==0);
477 // d/dx cos(x) -> -sin(x)
481 static ex cos_real_part(const ex & x)
483 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
486 static ex cos_imag_part(const ex & x)
488 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
491 static ex cos_conjugate(const ex & x)
493 // conjugate(cos(x))==cos(conjugate(x))
494 return cos(x.conjugate());
497 REGISTER_FUNCTION(cos, eval_func(cos_eval).
498 evalf_func(cos_evalf).
499 derivative_func(cos_deriv).
500 real_part_func(cos_real_part).
501 imag_part_func(cos_imag_part).
502 conjugate_func(cos_conjugate).
503 latex_name("\\cos"));
506 // tangent (trigonometric function)
509 static ex tan_evalf(const ex & x)
511 if (is_exactly_a<numeric>(x))
512 return tan(ex_to<numeric>(x));
514 return tan(x).hold();
517 static ex tan_eval(const ex & x)
519 // tan(n/d*Pi) -> { all known non-nested radicals }
520 const ex SixtyExOverPi = _ex60*x/Pi;
522 if (SixtyExOverPi.info(info_flags::integer)) {
523 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
525 // wrap to interval [0, Pi)
529 // wrap to interval [0, Pi/2)
533 if (z.is_equal(*_num0_p)) // tan(0) -> 0
535 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
536 return sign*(_ex2-sqrt(_ex3));
537 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
538 return sign*_ex1_3*sqrt(_ex3);
539 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
541 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
542 return sign*sqrt(_ex3);
543 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
544 return sign*(sqrt(_ex3)+_ex2);
545 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
546 throw (pole_error("tan_eval(): simple pole",1));
549 if (is_exactly_a<function>(x)) {
550 const ex &t = x.op(0);
553 if (is_ex_the_function(x, atan))
556 // tan(asin(x)) -> x/sqrt(1+x^2)
557 if (is_ex_the_function(x, asin))
558 return t*power(_ex1-power(t,_ex2),_ex_1_2);
560 // tan(acos(x)) -> sqrt(1-x^2)/x
561 if (is_ex_the_function(x, acos))
562 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
565 // tan(float) -> float
566 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
567 return tan(ex_to<numeric>(x));
571 if (x.info(info_flags::negative))
574 return tan(x).hold();
577 static ex tan_deriv(const ex & x, unsigned deriv_param)
579 GINAC_ASSERT(deriv_param==0);
581 // d/dx tan(x) -> 1+tan(x)^2;
582 return (_ex1+power(tan(x),_ex2));
585 static ex tan_real_part(const ex & x)
587 ex a = GiNaC::real_part(x);
588 ex b = GiNaC::imag_part(x);
589 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
592 static ex tan_imag_part(const ex & x)
594 ex a = GiNaC::real_part(x);
595 ex b = GiNaC::imag_part(x);
596 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
599 static ex tan_series(const ex &x,
600 const relational &rel,
604 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
606 // Taylor series where there is no pole falls back to tan_deriv.
607 // On a pole simply expand sin(x)/cos(x).
608 const ex x_pt = x.subs(rel, subs_options::no_pattern);
609 if (!(2*x_pt/Pi).info(info_flags::odd))
610 throw do_taylor(); // caught by function::series()
611 // if we got here we have to care for a simple pole
612 return (sin(x)/cos(x)).series(rel, order, options);
615 static ex tan_conjugate(const ex & x)
617 // conjugate(tan(x))==tan(conjugate(x))
618 return tan(x.conjugate());
621 REGISTER_FUNCTION(tan, eval_func(tan_eval).
622 evalf_func(tan_evalf).
623 derivative_func(tan_deriv).
624 series_func(tan_series).
625 real_part_func(tan_real_part).
626 imag_part_func(tan_imag_part).
627 conjugate_func(tan_conjugate).
628 latex_name("\\tan"));
631 // inverse sine (arc sine)
634 static ex asin_evalf(const ex & x)
636 if (is_exactly_a<numeric>(x))
637 return asin(ex_to<numeric>(x));
639 return asin(x).hold();
642 static ex asin_eval(const ex & x)
644 if (x.info(info_flags::numeric)) {
651 if (x.is_equal(_ex1_2))
652 return numeric(1,6)*Pi;
655 if (x.is_equal(_ex1))
658 // asin(-1/2) -> -Pi/6
659 if (x.is_equal(_ex_1_2))
660 return numeric(-1,6)*Pi;
663 if (x.is_equal(_ex_1))
666 // asin(float) -> float
667 if (!x.info(info_flags::crational))
668 return asin(ex_to<numeric>(x));
671 if (x.info(info_flags::negative))
675 return asin(x).hold();
678 static ex asin_deriv(const ex & x, unsigned deriv_param)
680 GINAC_ASSERT(deriv_param==0);
682 // d/dx asin(x) -> 1/sqrt(1-x^2)
683 return power(1-power(x,_ex2),_ex_1_2);
686 static ex asin_conjugate(const ex & x)
688 // conjugate(asin(x))==asin(conjugate(x)) unless on the branch cuts which
689 // run along the real axis outside the interval [-1, +1].
690 if (is_exactly_a<numeric>(x) &&
691 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
692 return asin(x.conjugate());
694 return conjugate_function(asin(x)).hold();
697 REGISTER_FUNCTION(asin, eval_func(asin_eval).
698 evalf_func(asin_evalf).
699 derivative_func(asin_deriv).
700 conjugate_func(asin_conjugate).
701 latex_name("\\arcsin"));
704 // inverse cosine (arc cosine)
707 static ex acos_evalf(const ex & x)
709 if (is_exactly_a<numeric>(x))
710 return acos(ex_to<numeric>(x));
712 return acos(x).hold();
715 static ex acos_eval(const ex & x)
717 if (x.info(info_flags::numeric)) {
720 if (x.is_equal(_ex1))
724 if (x.is_equal(_ex1_2))
731 // acos(-1/2) -> 2/3*Pi
732 if (x.is_equal(_ex_1_2))
733 return numeric(2,3)*Pi;
736 if (x.is_equal(_ex_1))
739 // acos(float) -> float
740 if (!x.info(info_flags::crational))
741 return acos(ex_to<numeric>(x));
743 // acos(-x) -> Pi-acos(x)
744 if (x.info(info_flags::negative))
748 return acos(x).hold();
751 static ex acos_deriv(const ex & x, unsigned deriv_param)
753 GINAC_ASSERT(deriv_param==0);
755 // d/dx acos(x) -> -1/sqrt(1-x^2)
756 return -power(1-power(x,_ex2),_ex_1_2);
759 static ex acos_conjugate(const ex & x)
761 // conjugate(acos(x))==acos(conjugate(x)) unless on the branch cuts which
762 // run along the real axis outside the interval [-1, +1].
763 if (is_exactly_a<numeric>(x) &&
764 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
765 return acos(x.conjugate());
767 return conjugate_function(acos(x)).hold();
770 REGISTER_FUNCTION(acos, eval_func(acos_eval).
771 evalf_func(acos_evalf).
772 derivative_func(acos_deriv).
773 conjugate_func(acos_conjugate).
774 latex_name("\\arccos"));
777 // inverse tangent (arc tangent)
780 static ex atan_evalf(const ex & x)
782 if (is_exactly_a<numeric>(x))
783 return atan(ex_to<numeric>(x));
785 return atan(x).hold();
788 static ex atan_eval(const ex & x)
790 if (x.info(info_flags::numeric)) {
797 if (x.is_equal(_ex1))
801 if (x.is_equal(_ex_1))
804 if (x.is_equal(I) || x.is_equal(-I))
805 throw (pole_error("atan_eval(): logarithmic pole",0));
807 // atan(float) -> float
808 if (!x.info(info_flags::crational))
809 return atan(ex_to<numeric>(x));
812 if (x.info(info_flags::negative))
816 return atan(x).hold();
819 static ex atan_deriv(const ex & x, unsigned deriv_param)
821 GINAC_ASSERT(deriv_param==0);
823 // d/dx atan(x) -> 1/(1+x^2)
824 return power(_ex1+power(x,_ex2), _ex_1);
827 static ex atan_series(const ex &arg,
828 const relational &rel,
832 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
834 // Taylor series where there is no pole or cut falls back to atan_deriv.
835 // There are two branch cuts, one runnig from I up the imaginary axis and
836 // one running from -I down the imaginary axis. The points I and -I are
838 // On the branch cuts and the poles series expand
839 // (log(1+I*x)-log(1-I*x))/(2*I)
841 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
842 if (!(I*arg_pt).info(info_flags::real))
843 throw do_taylor(); // Re(x) != 0
844 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
845 throw do_taylor(); // Re(x) == 0, but abs(x)<1
846 // care for the poles, using the defining formula for atan()...
847 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
848 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
849 if (!(options & series_options::suppress_branchcut)) {
851 // This is the branch cut: assemble the primitive series manually and
852 // then add the corresponding complex step function.
853 const symbol &s = ex_to<symbol>(rel.lhs());
854 const ex &point = rel.rhs();
856 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
857 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
859 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
861 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
863 seq.push_back(expair(Order0correction, _ex0));
864 seq.push_back(expair(Order(_ex1), order));
865 return series(replarg - pseries(rel, seq), rel, order);
870 static ex atan_conjugate(const ex & x)
872 // conjugate(atan(x))==atan(conjugate(x)) unless on the branch cuts which
873 // run along the imaginary axis outside the interval [-I, +I].
874 if (x.info(info_flags::real))
876 if (is_exactly_a<numeric>(x)) {
877 const numeric x_re = ex_to<numeric>(x.real_part());
878 const numeric x_im = ex_to<numeric>(x.imag_part());
879 if (!x_re.is_zero() ||
880 (x_im > *_num_1_p && x_im < *_num1_p))
881 return atan(x.conjugate());
883 return conjugate_function(atan(x)).hold();
886 REGISTER_FUNCTION(atan, eval_func(atan_eval).
887 evalf_func(atan_evalf).
888 derivative_func(atan_deriv).
889 series_func(atan_series).
890 conjugate_func(atan_conjugate).
891 latex_name("\\arctan"));
894 // inverse tangent (atan2(y,x))
897 static ex atan2_evalf(const ex &y, const ex &x)
899 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
900 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
902 return atan2(y, x).hold();
905 static ex atan2_eval(const ex & y, const ex & x)
913 // atan2(0, x), x real and positive -> 0
914 if (x.info(info_flags::positive))
917 // atan2(0, x), x real and negative -> Pi
918 if (x.info(info_flags::negative))
924 // atan2(y, 0), y real and positive -> Pi/2
925 if (y.info(info_flags::positive))
928 // atan2(y, 0), y real and negative -> -Pi/2
929 if (y.info(info_flags::negative))
935 // atan2(y, y), y real and positive -> Pi/4
936 if (y.info(info_flags::positive))
939 // atan2(y, y), y real and negative -> -3/4*Pi
940 if (y.info(info_flags::negative))
941 return numeric(-3, 4)*Pi;
944 if (y.is_equal(-x)) {
946 // atan2(y, -y), y real and positive -> 3*Pi/4
947 if (y.info(info_flags::positive))
948 return numeric(3, 4)*Pi;
950 // atan2(y, -y), y real and negative -> -Pi/4
951 if (y.info(info_flags::negative))
955 // atan2(float, float) -> float
956 if (is_a<numeric>(y) && !y.info(info_flags::crational) &&
957 is_a<numeric>(x) && !x.info(info_flags::crational))
958 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
960 // atan2(real, real) -> atan(y/x) +/- Pi
961 if (y.info(info_flags::real) && x.info(info_flags::real)) {
962 if (x.info(info_flags::positive))
965 if (x.info(info_flags::negative)) {
966 if (y.info(info_flags::positive))
968 if (y.info(info_flags::negative))
973 return atan2(y, x).hold();
976 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
978 GINAC_ASSERT(deriv_param<2);
980 if (deriv_param==0) {
982 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
985 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
988 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
989 evalf_func(atan2_evalf).
990 derivative_func(atan2_deriv));
993 // hyperbolic sine (trigonometric function)
996 static ex sinh_evalf(const ex & x)
998 if (is_exactly_a<numeric>(x))
999 return sinh(ex_to<numeric>(x));
1001 return sinh(x).hold();
1004 static ex sinh_eval(const ex & x)
1006 if (x.info(info_flags::numeric)) {
1012 // sinh(float) -> float
1013 if (!x.info(info_flags::crational))
1014 return sinh(ex_to<numeric>(x));
1017 if (x.info(info_flags::negative))
1021 if ((x/Pi).info(info_flags::numeric) &&
1022 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
1025 if (is_exactly_a<function>(x)) {
1026 const ex &t = x.op(0);
1028 // sinh(asinh(x)) -> x
1029 if (is_ex_the_function(x, asinh))
1032 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
1033 if (is_ex_the_function(x, acosh))
1034 return sqrt(t-_ex1)*sqrt(t+_ex1);
1036 // sinh(atanh(x)) -> x/sqrt(1-x^2)
1037 if (is_ex_the_function(x, atanh))
1038 return t*power(_ex1-power(t,_ex2),_ex_1_2);
1041 return sinh(x).hold();
1044 static ex sinh_deriv(const ex & x, unsigned deriv_param)
1046 GINAC_ASSERT(deriv_param==0);
1048 // d/dx sinh(x) -> cosh(x)
1052 static ex sinh_real_part(const ex & x)
1054 return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1057 static ex sinh_imag_part(const ex & x)
1059 return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1062 static ex sinh_conjugate(const ex & x)
1064 // conjugate(sinh(x))==sinh(conjugate(x))
1065 return sinh(x.conjugate());
1068 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
1069 evalf_func(sinh_evalf).
1070 derivative_func(sinh_deriv).
1071 real_part_func(sinh_real_part).
1072 imag_part_func(sinh_imag_part).
1073 conjugate_func(sinh_conjugate).
1074 latex_name("\\sinh"));
1077 // hyperbolic cosine (trigonometric function)
1080 static ex cosh_evalf(const ex & x)
1082 if (is_exactly_a<numeric>(x))
1083 return cosh(ex_to<numeric>(x));
1085 return cosh(x).hold();
1088 static ex cosh_eval(const ex & x)
1090 if (x.info(info_flags::numeric)) {
1096 // cosh(float) -> float
1097 if (!x.info(info_flags::crational))
1098 return cosh(ex_to<numeric>(x));
1101 if (x.info(info_flags::negative))
1105 if ((x/Pi).info(info_flags::numeric) &&
1106 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
1109 if (is_exactly_a<function>(x)) {
1110 const ex &t = x.op(0);
1112 // cosh(acosh(x)) -> x
1113 if (is_ex_the_function(x, acosh))
1116 // cosh(asinh(x)) -> sqrt(1+x^2)
1117 if (is_ex_the_function(x, asinh))
1118 return sqrt(_ex1+power(t,_ex2));
1120 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1121 if (is_ex_the_function(x, atanh))
1122 return power(_ex1-power(t,_ex2),_ex_1_2);
1125 return cosh(x).hold();
1128 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1130 GINAC_ASSERT(deriv_param==0);
1132 // d/dx cosh(x) -> sinh(x)
1136 static ex cosh_real_part(const ex & x)
1138 return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1141 static ex cosh_imag_part(const ex & x)
1143 return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1146 static ex cosh_conjugate(const ex & x)
1148 // conjugate(cosh(x))==cosh(conjugate(x))
1149 return cosh(x.conjugate());
1152 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1153 evalf_func(cosh_evalf).
1154 derivative_func(cosh_deriv).
1155 real_part_func(cosh_real_part).
1156 imag_part_func(cosh_imag_part).
1157 conjugate_func(cosh_conjugate).
1158 latex_name("\\cosh"));
1161 // hyperbolic tangent (trigonometric function)
1164 static ex tanh_evalf(const ex & x)
1166 if (is_exactly_a<numeric>(x))
1167 return tanh(ex_to<numeric>(x));
1169 return tanh(x).hold();
1172 static ex tanh_eval(const ex & x)
1174 if (x.info(info_flags::numeric)) {
1180 // tanh(float) -> float
1181 if (!x.info(info_flags::crational))
1182 return tanh(ex_to<numeric>(x));
1185 if (x.info(info_flags::negative))
1189 if ((x/Pi).info(info_flags::numeric) &&
1190 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1193 if (is_exactly_a<function>(x)) {
1194 const ex &t = x.op(0);
1196 // tanh(atanh(x)) -> x
1197 if (is_ex_the_function(x, atanh))
1200 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1201 if (is_ex_the_function(x, asinh))
1202 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1204 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1205 if (is_ex_the_function(x, acosh))
1206 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1209 return tanh(x).hold();
1212 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1214 GINAC_ASSERT(deriv_param==0);
1216 // d/dx tanh(x) -> 1-tanh(x)^2
1217 return _ex1-power(tanh(x),_ex2);
1220 static ex tanh_series(const ex &x,
1221 const relational &rel,
1225 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1227 // Taylor series where there is no pole falls back to tanh_deriv.
1228 // On a pole simply expand sinh(x)/cosh(x).
1229 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1230 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1231 throw do_taylor(); // caught by function::series()
1232 // if we got here we have to care for a simple pole
1233 return (sinh(x)/cosh(x)).series(rel, order, options);
1236 static ex tanh_real_part(const ex & x)
1238 ex a = GiNaC::real_part(x);
1239 ex b = GiNaC::imag_part(x);
1240 return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1243 static ex tanh_imag_part(const ex & x)
1245 ex a = GiNaC::real_part(x);
1246 ex b = GiNaC::imag_part(x);
1247 return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1250 static ex tanh_conjugate(const ex & x)
1252 // conjugate(tanh(x))==tanh(conjugate(x))
1253 return tanh(x.conjugate());
1256 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1257 evalf_func(tanh_evalf).
1258 derivative_func(tanh_deriv).
1259 series_func(tanh_series).
1260 real_part_func(tanh_real_part).
1261 imag_part_func(tanh_imag_part).
1262 conjugate_func(tanh_conjugate).
1263 latex_name("\\tanh"));
1266 // inverse hyperbolic sine (trigonometric function)
1269 static ex asinh_evalf(const ex & x)
1271 if (is_exactly_a<numeric>(x))
1272 return asinh(ex_to<numeric>(x));
1274 return asinh(x).hold();
1277 static ex asinh_eval(const ex & x)
1279 if (x.info(info_flags::numeric)) {
1285 // asinh(float) -> float
1286 if (!x.info(info_flags::crational))
1287 return asinh(ex_to<numeric>(x));
1290 if (x.info(info_flags::negative))
1294 return asinh(x).hold();
1297 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1299 GINAC_ASSERT(deriv_param==0);
1301 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1302 return power(_ex1+power(x,_ex2),_ex_1_2);
1305 static ex asinh_conjugate(const ex & x)
1307 // conjugate(asinh(x))==asinh(conjugate(x)) unless on the branch cuts which
1308 // run along the imaginary axis outside the interval [-I, +I].
1309 if (x.info(info_flags::real))
1311 if (is_exactly_a<numeric>(x)) {
1312 const numeric x_re = ex_to<numeric>(x.real_part());
1313 const numeric x_im = ex_to<numeric>(x.imag_part());
1314 if (!x_re.is_zero() ||
1315 (x_im > *_num_1_p && x_im < *_num1_p))
1316 return asinh(x.conjugate());
1318 return conjugate_function(asinh(x)).hold();
1321 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1322 evalf_func(asinh_evalf).
1323 derivative_func(asinh_deriv).
1324 conjugate_func(asinh_conjugate));
1327 // inverse hyperbolic cosine (trigonometric function)
1330 static ex acosh_evalf(const ex & x)
1332 if (is_exactly_a<numeric>(x))
1333 return acosh(ex_to<numeric>(x));
1335 return acosh(x).hold();
1338 static ex acosh_eval(const ex & x)
1340 if (x.info(info_flags::numeric)) {
1342 // acosh(0) -> Pi*I/2
1344 return Pi*I*numeric(1,2);
1347 if (x.is_equal(_ex1))
1350 // acosh(-1) -> Pi*I
1351 if (x.is_equal(_ex_1))
1354 // acosh(float) -> float
1355 if (!x.info(info_flags::crational))
1356 return acosh(ex_to<numeric>(x));
1358 // acosh(-x) -> Pi*I-acosh(x)
1359 if (x.info(info_flags::negative))
1360 return Pi*I-acosh(-x);
1363 return acosh(x).hold();
1366 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1368 GINAC_ASSERT(deriv_param==0);
1370 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1371 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1374 static ex acosh_conjugate(const ex & x)
1376 // conjugate(acosh(x))==acosh(conjugate(x)) unless on the branch cut
1377 // which runs along the real axis from +1 to -inf.
1378 if (is_exactly_a<numeric>(x) &&
1379 (!x.imag_part().is_zero() || x > *_num1_p)) {
1380 return acosh(x.conjugate());
1382 return conjugate_function(acosh(x)).hold();
1385 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1386 evalf_func(acosh_evalf).
1387 derivative_func(acosh_deriv).
1388 conjugate_func(acosh_conjugate));
1391 // inverse hyperbolic tangent (trigonometric function)
1394 static ex atanh_evalf(const ex & x)
1396 if (is_exactly_a<numeric>(x))
1397 return atanh(ex_to<numeric>(x));
1399 return atanh(x).hold();
1402 static ex atanh_eval(const ex & x)
1404 if (x.info(info_flags::numeric)) {
1410 // atanh({+|-}1) -> throw
1411 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1412 throw (pole_error("atanh_eval(): logarithmic pole",0));
1414 // atanh(float) -> float
1415 if (!x.info(info_flags::crational))
1416 return atanh(ex_to<numeric>(x));
1419 if (x.info(info_flags::negative))
1423 return atanh(x).hold();
1426 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1428 GINAC_ASSERT(deriv_param==0);
1430 // d/dx atanh(x) -> 1/(1-x^2)
1431 return power(_ex1-power(x,_ex2),_ex_1);
1434 static ex atanh_series(const ex &arg,
1435 const relational &rel,
1439 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1441 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1442 // There are two branch cuts, one runnig from 1 up the real axis and one
1443 // one running from -1 down the real axis. The points 1 and -1 are poles
1444 // On the branch cuts and the poles series expand
1445 // (log(1+x)-log(1-x))/2
1447 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1448 if (!(arg_pt).info(info_flags::real))
1449 throw do_taylor(); // Im(x) != 0
1450 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1451 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1452 // care for the poles, using the defining formula for atanh()...
1453 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1454 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1455 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1456 if (!(options & series_options::suppress_branchcut)) {
1458 // This is the branch cut: assemble the primitive series manually and
1459 // then add the corresponding complex step function.
1460 const symbol &s = ex_to<symbol>(rel.lhs());
1461 const ex &point = rel.rhs();
1463 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1464 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1466 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1468 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1470 seq.push_back(expair(Order0correction, _ex0));
1471 seq.push_back(expair(Order(_ex1), order));
1472 return series(replarg - pseries(rel, seq), rel, order);
1477 static ex atanh_conjugate(const ex & x)
1479 // conjugate(atanh(x))==atanh(conjugate(x)) unless on the branch cuts which
1480 // run along the real axis outside the interval [-1, +1].
1481 if (is_exactly_a<numeric>(x) &&
1482 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
1483 return atanh(x.conjugate());
1485 return conjugate_function(atanh(x)).hold();
1488 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1489 evalf_func(atanh_evalf).
1490 derivative_func(atanh_deriv).
1491 series_func(atanh_series).
1492 conjugate_func(atanh_conjugate));
1495 } // namespace GiNaC