1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2007 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 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 REGISTER_FUNCTION(exp, eval_func(exp_eval).
103 evalf_func(exp_evalf).
104 derivative_func(exp_deriv).
105 real_part_func(exp_real_part).
106 imag_part_func(exp_imag_part).
107 latex_name("\\exp"));
113 static ex log_evalf(const ex & x)
115 if (is_exactly_a<numeric>(x))
116 return log(ex_to<numeric>(x));
118 return log(x).hold();
121 static ex log_eval(const ex & x)
123 if (x.info(info_flags::numeric)) {
124 if (x.is_zero()) // log(0) -> infinity
125 throw(pole_error("log_eval(): log(0)",0));
126 if (x.info(info_flags::rational) && x.info(info_flags::negative))
127 return (log(-x)+I*Pi);
128 if (x.is_equal(_ex1)) // log(1) -> 0
130 if (x.is_equal(I)) // log(I) -> Pi*I/2
131 return (Pi*I*_ex1_2);
132 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
133 return (Pi*I*_ex_1_2);
135 // log(float) -> float
136 if (!x.info(info_flags::crational))
137 return log(ex_to<numeric>(x));
140 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
141 if (is_ex_the_function(x, exp)) {
142 const ex &t = x.op(0);
143 if (t.info(info_flags::real))
147 return log(x).hold();
150 static ex log_deriv(const ex & x, unsigned deriv_param)
152 GINAC_ASSERT(deriv_param==0);
154 // d/dx log(x) -> 1/x
155 return power(x, _ex_1);
158 static ex log_series(const ex &arg,
159 const relational &rel,
163 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
165 bool must_expand_arg = false;
166 // maybe substitution of rel into arg fails because of a pole
168 arg_pt = arg.subs(rel, subs_options::no_pattern);
169 } catch (pole_error) {
170 must_expand_arg = true;
172 // or we are at the branch point anyways
173 if (arg_pt.is_zero())
174 must_expand_arg = true;
176 if (must_expand_arg) {
178 // This is the branch point: Series expand the argument first, then
179 // trivially factorize it to isolate that part which has constant
180 // leading coefficient in this fashion:
181 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
182 // Return a plain n*log(x) for the x^n part and series expand the
183 // other part. Add them together and reexpand again in order to have
184 // one unnested pseries object. All this also works for negative n.
185 pseries argser; // series expansion of log's argument
186 unsigned extra_ord = 0; // extra expansion order
188 // oops, the argument expanded to a pure Order(x^something)...
189 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
191 } while (!argser.is_terminating() && argser.nops()==1);
193 const symbol &s = ex_to<symbol>(rel.lhs());
194 const ex &point = rel.rhs();
195 const int n = argser.ldegree(s);
197 // construct what we carelessly called the n*log(x) term above
198 const ex coeff = argser.coeff(s, n);
199 // expand the log, but only if coeff is real and > 0, since otherwise
200 // it would make the branch cut run into the wrong direction
201 if (coeff.info(info_flags::positive))
202 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
204 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
206 if (!argser.is_terminating() || argser.nops()!=1) {
207 // in this case n more (or less) terms are needed
208 // (sadly, to generate them, we have to start from the beginning)
209 if (n == 0 && coeff == 1) {
211 ex acc = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
213 epv.push_back(expair(-1, _ex0));
214 epv.push_back(expair(Order(_ex1), order));
215 ex rest = pseries(rel, epv).add_series(argser);
216 for (int i = order-1; i>0; --i) {
219 cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
220 acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
221 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
225 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
226 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
227 } else // it was a monomial
228 return pseries(rel, seq);
230 if (!(options & series_options::suppress_branchcut) &&
231 arg_pt.info(info_flags::negative)) {
233 // This is the branch cut: assemble the primitive series manually and
234 // then add the corresponding complex step function.
235 const symbol &s = ex_to<symbol>(rel.lhs());
236 const ex &point = rel.rhs();
238 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
240 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
241 seq.push_back(expair(Order(_ex1), order));
242 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
244 throw do_taylor(); // caught by function::series()
247 static ex log_real_part(const ex & x)
249 if (x.info(info_flags::nonnegative))
250 return log(x).hold();
254 static ex log_imag_part(const ex & x)
256 if (x.info(info_flags::nonnegative))
258 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
261 REGISTER_FUNCTION(log, eval_func(log_eval).
262 evalf_func(log_evalf).
263 derivative_func(log_deriv).
264 series_func(log_series).
265 real_part_func(log_real_part).
266 imag_part_func(log_imag_part).
270 // sine (trigonometric function)
273 static ex sin_evalf(const ex & x)
275 if (is_exactly_a<numeric>(x))
276 return sin(ex_to<numeric>(x));
278 return sin(x).hold();
281 static ex sin_eval(const ex & x)
283 // sin(n/d*Pi) -> { all known non-nested radicals }
284 const ex SixtyExOverPi = _ex60*x/Pi;
286 if (SixtyExOverPi.info(info_flags::integer)) {
287 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
289 // wrap to interval [0, Pi)
294 // wrap to interval [0, Pi/2)
297 if (z.is_equal(*_num0_p)) // sin(0) -> 0
299 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
300 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
301 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
302 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
303 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
305 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
306 return sign*_ex1_2*sqrt(_ex2);
307 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
308 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
309 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
310 return sign*_ex1_2*sqrt(_ex3);
311 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
312 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
313 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
317 if (is_exactly_a<function>(x)) {
318 const ex &t = x.op(0);
321 if (is_ex_the_function(x, asin))
324 // sin(acos(x)) -> sqrt(1-x^2)
325 if (is_ex_the_function(x, acos))
326 return sqrt(_ex1-power(t,_ex2));
328 // sin(atan(x)) -> x/sqrt(1+x^2)
329 if (is_ex_the_function(x, atan))
330 return t*power(_ex1+power(t,_ex2),_ex_1_2);
333 // sin(float) -> float
334 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
335 return sin(ex_to<numeric>(x));
338 if (x.info(info_flags::negative))
341 return sin(x).hold();
344 static ex sin_deriv(const ex & x, unsigned deriv_param)
346 GINAC_ASSERT(deriv_param==0);
348 // d/dx sin(x) -> cos(x)
352 static ex sin_real_part(const ex & x)
354 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
357 static ex sin_imag_part(const ex & x)
359 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
362 REGISTER_FUNCTION(sin, eval_func(sin_eval).
363 evalf_func(sin_evalf).
364 derivative_func(sin_deriv).
365 real_part_func(sin_real_part).
366 imag_part_func(sin_imag_part).
367 latex_name("\\sin"));
370 // cosine (trigonometric function)
373 static ex cos_evalf(const ex & x)
375 if (is_exactly_a<numeric>(x))
376 return cos(ex_to<numeric>(x));
378 return cos(x).hold();
381 static ex cos_eval(const ex & x)
383 // cos(n/d*Pi) -> { all known non-nested radicals }
384 const ex SixtyExOverPi = _ex60*x/Pi;
386 if (SixtyExOverPi.info(info_flags::integer)) {
387 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
389 // wrap to interval [0, Pi)
393 // wrap to interval [0, Pi/2)
397 if (z.is_equal(*_num0_p)) // cos(0) -> 1
399 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
400 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
401 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
402 return sign*_ex1_2*sqrt(_ex3);
403 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
404 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
405 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
406 return sign*_ex1_2*sqrt(_ex2);
407 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
409 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
410 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
411 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
412 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
413 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
417 if (is_exactly_a<function>(x)) {
418 const ex &t = x.op(0);
421 if (is_ex_the_function(x, acos))
424 // cos(asin(x)) -> sqrt(1-x^2)
425 if (is_ex_the_function(x, asin))
426 return sqrt(_ex1-power(t,_ex2));
428 // cos(atan(x)) -> 1/sqrt(1+x^2)
429 if (is_ex_the_function(x, atan))
430 return power(_ex1+power(t,_ex2),_ex_1_2);
433 // cos(float) -> float
434 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
435 return cos(ex_to<numeric>(x));
438 if (x.info(info_flags::negative))
441 return cos(x).hold();
444 static ex cos_deriv(const ex & x, unsigned deriv_param)
446 GINAC_ASSERT(deriv_param==0);
448 // d/dx cos(x) -> -sin(x)
452 static ex cos_real_part(const ex & x)
454 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
457 static ex cos_imag_part(const ex & x)
459 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
462 REGISTER_FUNCTION(cos, eval_func(cos_eval).
463 evalf_func(cos_evalf).
464 derivative_func(cos_deriv).
465 real_part_func(cos_real_part).
466 imag_part_func(cos_imag_part).
467 latex_name("\\cos"));
470 // tangent (trigonometric function)
473 static ex tan_evalf(const ex & x)
475 if (is_exactly_a<numeric>(x))
476 return tan(ex_to<numeric>(x));
478 return tan(x).hold();
481 static ex tan_eval(const ex & x)
483 // tan(n/d*Pi) -> { all known non-nested radicals }
484 const ex SixtyExOverPi = _ex60*x/Pi;
486 if (SixtyExOverPi.info(info_flags::integer)) {
487 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
489 // wrap to interval [0, Pi)
493 // wrap to interval [0, Pi/2)
497 if (z.is_equal(*_num0_p)) // tan(0) -> 0
499 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
500 return sign*(_ex2-sqrt(_ex3));
501 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
502 return sign*_ex1_3*sqrt(_ex3);
503 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
505 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
506 return sign*sqrt(_ex3);
507 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
508 return sign*(sqrt(_ex3)+_ex2);
509 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
510 throw (pole_error("tan_eval(): simple pole",1));
513 if (is_exactly_a<function>(x)) {
514 const ex &t = x.op(0);
517 if (is_ex_the_function(x, atan))
520 // tan(asin(x)) -> x/sqrt(1+x^2)
521 if (is_ex_the_function(x, asin))
522 return t*power(_ex1-power(t,_ex2),_ex_1_2);
524 // tan(acos(x)) -> sqrt(1-x^2)/x
525 if (is_ex_the_function(x, acos))
526 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
529 // tan(float) -> float
530 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
531 return tan(ex_to<numeric>(x));
535 if (x.info(info_flags::negative))
538 return tan(x).hold();
541 static ex tan_deriv(const ex & x, unsigned deriv_param)
543 GINAC_ASSERT(deriv_param==0);
545 // d/dx tan(x) -> 1+tan(x)^2;
546 return (_ex1+power(tan(x),_ex2));
549 static ex tan_real_part(const ex & x)
551 ex a = GiNaC::real_part(x);
552 ex b = GiNaC::imag_part(x);
553 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
556 static ex tan_imag_part(const ex & x)
558 ex a = GiNaC::real_part(x);
559 ex b = GiNaC::imag_part(x);
560 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
563 static ex tan_series(const ex &x,
564 const relational &rel,
568 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
570 // Taylor series where there is no pole falls back to tan_deriv.
571 // On a pole simply expand sin(x)/cos(x).
572 const ex x_pt = x.subs(rel, subs_options::no_pattern);
573 if (!(2*x_pt/Pi).info(info_flags::odd))
574 throw do_taylor(); // caught by function::series()
575 // if we got here we have to care for a simple pole
576 return (sin(x)/cos(x)).series(rel, order, options);
579 REGISTER_FUNCTION(tan, eval_func(tan_eval).
580 evalf_func(tan_evalf).
581 derivative_func(tan_deriv).
582 series_func(tan_series).
583 real_part_func(tan_real_part).
584 imag_part_func(tan_imag_part).
585 latex_name("\\tan"));
588 // inverse sine (arc sine)
591 static ex asin_evalf(const ex & x)
593 if (is_exactly_a<numeric>(x))
594 return asin(ex_to<numeric>(x));
596 return asin(x).hold();
599 static ex asin_eval(const ex & x)
601 if (x.info(info_flags::numeric)) {
608 if (x.is_equal(_ex1_2))
609 return numeric(1,6)*Pi;
612 if (x.is_equal(_ex1))
615 // asin(-1/2) -> -Pi/6
616 if (x.is_equal(_ex_1_2))
617 return numeric(-1,6)*Pi;
620 if (x.is_equal(_ex_1))
623 // asin(float) -> float
624 if (!x.info(info_flags::crational))
625 return asin(ex_to<numeric>(x));
628 if (x.info(info_flags::negative))
632 return asin(x).hold();
635 static ex asin_deriv(const ex & x, unsigned deriv_param)
637 GINAC_ASSERT(deriv_param==0);
639 // d/dx asin(x) -> 1/sqrt(1-x^2)
640 return power(1-power(x,_ex2),_ex_1_2);
643 REGISTER_FUNCTION(asin, eval_func(asin_eval).
644 evalf_func(asin_evalf).
645 derivative_func(asin_deriv).
646 latex_name("\\arcsin"));
649 // inverse cosine (arc cosine)
652 static ex acos_evalf(const ex & x)
654 if (is_exactly_a<numeric>(x))
655 return acos(ex_to<numeric>(x));
657 return acos(x).hold();
660 static ex acos_eval(const ex & x)
662 if (x.info(info_flags::numeric)) {
665 if (x.is_equal(_ex1))
669 if (x.is_equal(_ex1_2))
676 // acos(-1/2) -> 2/3*Pi
677 if (x.is_equal(_ex_1_2))
678 return numeric(2,3)*Pi;
681 if (x.is_equal(_ex_1))
684 // acos(float) -> float
685 if (!x.info(info_flags::crational))
686 return acos(ex_to<numeric>(x));
688 // acos(-x) -> Pi-acos(x)
689 if (x.info(info_flags::negative))
693 return acos(x).hold();
696 static ex acos_deriv(const ex & x, unsigned deriv_param)
698 GINAC_ASSERT(deriv_param==0);
700 // d/dx acos(x) -> -1/sqrt(1-x^2)
701 return -power(1-power(x,_ex2),_ex_1_2);
704 REGISTER_FUNCTION(acos, eval_func(acos_eval).
705 evalf_func(acos_evalf).
706 derivative_func(acos_deriv).
707 latex_name("\\arccos"));
710 // inverse tangent (arc tangent)
713 static ex atan_evalf(const ex & x)
715 if (is_exactly_a<numeric>(x))
716 return atan(ex_to<numeric>(x));
718 return atan(x).hold();
721 static ex atan_eval(const ex & x)
723 if (x.info(info_flags::numeric)) {
730 if (x.is_equal(_ex1))
734 if (x.is_equal(_ex_1))
737 if (x.is_equal(I) || x.is_equal(-I))
738 throw (pole_error("atan_eval(): logarithmic pole",0));
740 // atan(float) -> float
741 if (!x.info(info_flags::crational))
742 return atan(ex_to<numeric>(x));
745 if (x.info(info_flags::negative))
749 return atan(x).hold();
752 static ex atan_deriv(const ex & x, unsigned deriv_param)
754 GINAC_ASSERT(deriv_param==0);
756 // d/dx atan(x) -> 1/(1+x^2)
757 return power(_ex1+power(x,_ex2), _ex_1);
760 static ex atan_series(const ex &arg,
761 const relational &rel,
765 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
767 // Taylor series where there is no pole or cut falls back to atan_deriv.
768 // There are two branch cuts, one runnig from I up the imaginary axis and
769 // one running from -I down the imaginary axis. The points I and -I are
771 // On the branch cuts and the poles series expand
772 // (log(1+I*x)-log(1-I*x))/(2*I)
774 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
775 if (!(I*arg_pt).info(info_flags::real))
776 throw do_taylor(); // Re(x) != 0
777 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
778 throw do_taylor(); // Re(x) == 0, but abs(x)<1
779 // care for the poles, using the defining formula for atan()...
780 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
781 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
782 if (!(options & series_options::suppress_branchcut)) {
784 // This is the branch cut: assemble the primitive series manually and
785 // then add the corresponding complex step function.
786 const symbol &s = ex_to<symbol>(rel.lhs());
787 const ex &point = rel.rhs();
789 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
790 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
792 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
794 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
796 seq.push_back(expair(Order0correction, _ex0));
797 seq.push_back(expair(Order(_ex1), order));
798 return series(replarg - pseries(rel, seq), rel, order);
803 REGISTER_FUNCTION(atan, eval_func(atan_eval).
804 evalf_func(atan_evalf).
805 derivative_func(atan_deriv).
806 series_func(atan_series).
807 latex_name("\\arctan"));
810 // inverse tangent (atan2(y,x))
813 static ex atan2_evalf(const ex &y, const ex &x)
815 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
816 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
818 return atan2(y, x).hold();
821 static ex atan2_eval(const ex & y, const ex & x)
829 // atan(0, x), x real and positive -> 0
830 if (x.info(info_flags::positive))
833 // atan(0, x), x real and negative -> -Pi
834 if (x.info(info_flags::negative))
840 // atan(y, 0), y real and positive -> Pi/2
841 if (y.info(info_flags::positive))
844 // atan(y, 0), y real and negative -> -Pi/2
845 if (y.info(info_flags::negative))
851 // atan(y, y), y real and positive -> Pi/4
852 if (y.info(info_flags::positive))
855 // atan(y, y), y real and negative -> -3/4*Pi
856 if (y.info(info_flags::negative))
857 return numeric(-3, 4)*Pi;
860 if (y.is_equal(-x)) {
862 // atan(y, -y), y real and positive -> 3*Pi/4
863 if (y.info(info_flags::positive))
864 return numeric(3, 4)*Pi;
866 // atan(y, -y), y real and negative -> -Pi/4
867 if (y.info(info_flags::negative))
871 // atan(float, float) -> float
872 if (is_a<numeric>(y) && is_a<numeric>(x) && !y.info(info_flags::crational)
873 && !x.info(info_flags::crational))
874 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
876 // atan(real, real) -> atan(y/x) +/- Pi
877 if (y.info(info_flags::real) && x.info(info_flags::real)) {
878 if (x.info(info_flags::positive))
880 else if(y.info(info_flags::positive))
886 return atan2(y, x).hold();
889 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
891 GINAC_ASSERT(deriv_param<2);
893 if (deriv_param==0) {
895 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
898 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
901 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
902 evalf_func(atan2_evalf).
903 derivative_func(atan2_deriv));
906 // hyperbolic sine (trigonometric function)
909 static ex sinh_evalf(const ex & x)
911 if (is_exactly_a<numeric>(x))
912 return sinh(ex_to<numeric>(x));
914 return sinh(x).hold();
917 static ex sinh_eval(const ex & x)
919 if (x.info(info_flags::numeric)) {
925 // sinh(float) -> float
926 if (!x.info(info_flags::crational))
927 return sinh(ex_to<numeric>(x));
930 if (x.info(info_flags::negative))
934 if ((x/Pi).info(info_flags::numeric) &&
935 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
938 if (is_exactly_a<function>(x)) {
939 const ex &t = x.op(0);
941 // sinh(asinh(x)) -> x
942 if (is_ex_the_function(x, asinh))
945 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
946 if (is_ex_the_function(x, acosh))
947 return sqrt(t-_ex1)*sqrt(t+_ex1);
949 // sinh(atanh(x)) -> x/sqrt(1-x^2)
950 if (is_ex_the_function(x, atanh))
951 return t*power(_ex1-power(t,_ex2),_ex_1_2);
954 return sinh(x).hold();
957 static ex sinh_deriv(const ex & x, unsigned deriv_param)
959 GINAC_ASSERT(deriv_param==0);
961 // d/dx sinh(x) -> cosh(x)
965 static ex sinh_real_part(const ex & x)
967 return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
970 static ex sinh_imag_part(const ex & x)
972 return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
975 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
976 evalf_func(sinh_evalf).
977 derivative_func(sinh_deriv).
978 real_part_func(sinh_real_part).
979 imag_part_func(sinh_imag_part).
980 latex_name("\\sinh"));
983 // hyperbolic cosine (trigonometric function)
986 static ex cosh_evalf(const ex & x)
988 if (is_exactly_a<numeric>(x))
989 return cosh(ex_to<numeric>(x));
991 return cosh(x).hold();
994 static ex cosh_eval(const ex & x)
996 if (x.info(info_flags::numeric)) {
1002 // cosh(float) -> float
1003 if (!x.info(info_flags::crational))
1004 return cosh(ex_to<numeric>(x));
1007 if (x.info(info_flags::negative))
1011 if ((x/Pi).info(info_flags::numeric) &&
1012 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
1015 if (is_exactly_a<function>(x)) {
1016 const ex &t = x.op(0);
1018 // cosh(acosh(x)) -> x
1019 if (is_ex_the_function(x, acosh))
1022 // cosh(asinh(x)) -> sqrt(1+x^2)
1023 if (is_ex_the_function(x, asinh))
1024 return sqrt(_ex1+power(t,_ex2));
1026 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1027 if (is_ex_the_function(x, atanh))
1028 return power(_ex1-power(t,_ex2),_ex_1_2);
1031 return cosh(x).hold();
1034 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1036 GINAC_ASSERT(deriv_param==0);
1038 // d/dx cosh(x) -> sinh(x)
1042 static ex cosh_real_part(const ex & x)
1044 return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1047 static ex cosh_imag_part(const ex & x)
1049 return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1052 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1053 evalf_func(cosh_evalf).
1054 derivative_func(cosh_deriv).
1055 real_part_func(cosh_real_part).
1056 imag_part_func(cosh_imag_part).
1057 latex_name("\\cosh"));
1060 // hyperbolic tangent (trigonometric function)
1063 static ex tanh_evalf(const ex & x)
1065 if (is_exactly_a<numeric>(x))
1066 return tanh(ex_to<numeric>(x));
1068 return tanh(x).hold();
1071 static ex tanh_eval(const ex & x)
1073 if (x.info(info_flags::numeric)) {
1079 // tanh(float) -> float
1080 if (!x.info(info_flags::crational))
1081 return tanh(ex_to<numeric>(x));
1084 if (x.info(info_flags::negative))
1088 if ((x/Pi).info(info_flags::numeric) &&
1089 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1092 if (is_exactly_a<function>(x)) {
1093 const ex &t = x.op(0);
1095 // tanh(atanh(x)) -> x
1096 if (is_ex_the_function(x, atanh))
1099 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1100 if (is_ex_the_function(x, asinh))
1101 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1103 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1104 if (is_ex_the_function(x, acosh))
1105 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1108 return tanh(x).hold();
1111 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1113 GINAC_ASSERT(deriv_param==0);
1115 // d/dx tanh(x) -> 1-tanh(x)^2
1116 return _ex1-power(tanh(x),_ex2);
1119 static ex tanh_series(const ex &x,
1120 const relational &rel,
1124 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1126 // Taylor series where there is no pole falls back to tanh_deriv.
1127 // On a pole simply expand sinh(x)/cosh(x).
1128 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1129 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1130 throw do_taylor(); // caught by function::series()
1131 // if we got here we have to care for a simple pole
1132 return (sinh(x)/cosh(x)).series(rel, order, options);
1135 static ex tanh_real_part(const ex & x)
1137 ex a = GiNaC::real_part(x);
1138 ex b = GiNaC::imag_part(x);
1139 return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1142 static ex tanh_imag_part(const ex & x)
1144 ex a = GiNaC::real_part(x);
1145 ex b = GiNaC::imag_part(x);
1146 return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1149 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1150 evalf_func(tanh_evalf).
1151 derivative_func(tanh_deriv).
1152 series_func(tanh_series).
1153 real_part_func(tanh_real_part).
1154 imag_part_func(tanh_imag_part).
1155 latex_name("\\tanh"));
1158 // inverse hyperbolic sine (trigonometric function)
1161 static ex asinh_evalf(const ex & x)
1163 if (is_exactly_a<numeric>(x))
1164 return asinh(ex_to<numeric>(x));
1166 return asinh(x).hold();
1169 static ex asinh_eval(const ex & x)
1171 if (x.info(info_flags::numeric)) {
1177 // asinh(float) -> float
1178 if (!x.info(info_flags::crational))
1179 return asinh(ex_to<numeric>(x));
1182 if (x.info(info_flags::negative))
1186 return asinh(x).hold();
1189 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1191 GINAC_ASSERT(deriv_param==0);
1193 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1194 return power(_ex1+power(x,_ex2),_ex_1_2);
1197 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1198 evalf_func(asinh_evalf).
1199 derivative_func(asinh_deriv));
1202 // inverse hyperbolic cosine (trigonometric function)
1205 static ex acosh_evalf(const ex & x)
1207 if (is_exactly_a<numeric>(x))
1208 return acosh(ex_to<numeric>(x));
1210 return acosh(x).hold();
1213 static ex acosh_eval(const ex & x)
1215 if (x.info(info_flags::numeric)) {
1217 // acosh(0) -> Pi*I/2
1219 return Pi*I*numeric(1,2);
1222 if (x.is_equal(_ex1))
1225 // acosh(-1) -> Pi*I
1226 if (x.is_equal(_ex_1))
1229 // acosh(float) -> float
1230 if (!x.info(info_flags::crational))
1231 return acosh(ex_to<numeric>(x));
1233 // acosh(-x) -> Pi*I-acosh(x)
1234 if (x.info(info_flags::negative))
1235 return Pi*I-acosh(-x);
1238 return acosh(x).hold();
1241 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1243 GINAC_ASSERT(deriv_param==0);
1245 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1246 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1249 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1250 evalf_func(acosh_evalf).
1251 derivative_func(acosh_deriv));
1254 // inverse hyperbolic tangent (trigonometric function)
1257 static ex atanh_evalf(const ex & x)
1259 if (is_exactly_a<numeric>(x))
1260 return atanh(ex_to<numeric>(x));
1262 return atanh(x).hold();
1265 static ex atanh_eval(const ex & x)
1267 if (x.info(info_flags::numeric)) {
1273 // atanh({+|-}1) -> throw
1274 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1275 throw (pole_error("atanh_eval(): logarithmic pole",0));
1277 // atanh(float) -> float
1278 if (!x.info(info_flags::crational))
1279 return atanh(ex_to<numeric>(x));
1282 if (x.info(info_flags::negative))
1286 return atanh(x).hold();
1289 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1291 GINAC_ASSERT(deriv_param==0);
1293 // d/dx atanh(x) -> 1/(1-x^2)
1294 return power(_ex1-power(x,_ex2),_ex_1);
1297 static ex atanh_series(const ex &arg,
1298 const relational &rel,
1302 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1304 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1305 // There are two branch cuts, one runnig from 1 up the real axis and one
1306 // one running from -1 down the real axis. The points 1 and -1 are poles
1307 // On the branch cuts and the poles series expand
1308 // (log(1+x)-log(1-x))/2
1310 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1311 if (!(arg_pt).info(info_flags::real))
1312 throw do_taylor(); // Im(x) != 0
1313 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1314 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1315 // care for the poles, using the defining formula for atanh()...
1316 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1317 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1318 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1319 if (!(options & series_options::suppress_branchcut)) {
1321 // This is the branch cut: assemble the primitive series manually and
1322 // then add the corresponding complex step function.
1323 const symbol &s = ex_to<symbol>(rel.lhs());
1324 const ex &point = rel.rhs();
1326 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1327 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1329 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1331 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1333 seq.push_back(expair(Order0correction, _ex0));
1334 seq.push_back(expair(Order(_ex1), order));
1335 return series(replarg - pseries(rel, seq), rel, order);
1340 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1341 evalf_func(atanh_evalf).
1342 derivative_func(atanh_deriv).
1343 series_func(atanh_series));
1346 } // namespace GiNaC