1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "operators.h"
33 #include "relational.h"
41 // exponential function
44 static ex exp_evalf(const ex & x)
46 if (is_exactly_a<numeric>(x))
47 return exp(ex_to<numeric>(x));
52 static ex exp_eval(const ex & x)
59 // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
60 const ex TwoExOverPiI=(_ex2*x)/(Pi*I);
61 if (TwoExOverPiI.info(info_flags::integer)) {
62 const numeric z = mod(ex_to<numeric>(TwoExOverPiI),*_num4_p);
63 if (z.is_equal(*_num0_p))
65 if (z.is_equal(*_num1_p))
67 if (z.is_equal(*_num2_p))
69 if (z.is_equal(*_num3_p))
74 if (is_ex_the_function(x, log))
77 // exp(float) -> float
78 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
79 return exp(ex_to<numeric>(x));
84 static ex exp_deriv(const ex & x, unsigned deriv_param)
86 GINAC_ASSERT(deriv_param==0);
88 // d/dx exp(x) -> exp(x)
92 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)
252 static ex log_imag_part(const ex & x)
254 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
257 REGISTER_FUNCTION(log, eval_func(log_eval).
258 evalf_func(log_evalf).
259 derivative_func(log_deriv).
260 series_func(log_series).
261 real_part_func(log_real_part).
262 imag_part_func(log_imag_part).
266 // sine (trigonometric function)
269 static ex sin_evalf(const ex & x)
271 if (is_exactly_a<numeric>(x))
272 return sin(ex_to<numeric>(x));
274 return sin(x).hold();
277 static ex sin_eval(const ex & x)
279 // sin(n/d*Pi) -> { all known non-nested radicals }
280 const ex SixtyExOverPi = _ex60*x/Pi;
282 if (SixtyExOverPi.info(info_flags::integer)) {
283 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
285 // wrap to interval [0, Pi)
290 // wrap to interval [0, Pi/2)
293 if (z.is_equal(*_num0_p)) // sin(0) -> 0
295 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
296 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
297 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
298 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
299 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
301 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
302 return sign*_ex1_2*sqrt(_ex2);
303 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
304 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
305 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
306 return sign*_ex1_2*sqrt(_ex3);
307 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
308 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
309 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
313 if (is_exactly_a<function>(x)) {
314 const ex &t = x.op(0);
317 if (is_ex_the_function(x, asin))
320 // sin(acos(x)) -> sqrt(1-x^2)
321 if (is_ex_the_function(x, acos))
322 return sqrt(_ex1-power(t,_ex2));
324 // sin(atan(x)) -> x/sqrt(1+x^2)
325 if (is_ex_the_function(x, atan))
326 return t*power(_ex1+power(t,_ex2),_ex_1_2);
329 // sin(float) -> float
330 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
331 return sin(ex_to<numeric>(x));
334 if (x.info(info_flags::negative))
337 return sin(x).hold();
340 static ex sin_deriv(const ex & x, unsigned deriv_param)
342 GINAC_ASSERT(deriv_param==0);
344 // d/dx sin(x) -> cos(x)
348 static ex sin_real_part(const ex & x)
350 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
353 static ex sin_imag_part(const ex & x)
355 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
358 REGISTER_FUNCTION(sin, eval_func(sin_eval).
359 evalf_func(sin_evalf).
360 derivative_func(sin_deriv).
361 real_part_func(sin_real_part).
362 imag_part_func(sin_imag_part).
363 latex_name("\\sin"));
366 // cosine (trigonometric function)
369 static ex cos_evalf(const ex & x)
371 if (is_exactly_a<numeric>(x))
372 return cos(ex_to<numeric>(x));
374 return cos(x).hold();
377 static ex cos_eval(const ex & x)
379 // cos(n/d*Pi) -> { all known non-nested radicals }
380 const ex SixtyExOverPi = _ex60*x/Pi;
382 if (SixtyExOverPi.info(info_flags::integer)) {
383 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
385 // wrap to interval [0, Pi)
389 // wrap to interval [0, Pi/2)
393 if (z.is_equal(*_num0_p)) // cos(0) -> 1
395 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
396 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
397 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
398 return sign*_ex1_2*sqrt(_ex3);
399 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
400 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
401 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
402 return sign*_ex1_2*sqrt(_ex2);
403 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
405 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
406 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
407 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
408 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
409 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
413 if (is_exactly_a<function>(x)) {
414 const ex &t = x.op(0);
417 if (is_ex_the_function(x, acos))
420 // cos(asin(x)) -> sqrt(1-x^2)
421 if (is_ex_the_function(x, asin))
422 return sqrt(_ex1-power(t,_ex2));
424 // cos(atan(x)) -> 1/sqrt(1+x^2)
425 if (is_ex_the_function(x, atan))
426 return power(_ex1+power(t,_ex2),_ex_1_2);
429 // cos(float) -> float
430 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
431 return cos(ex_to<numeric>(x));
434 if (x.info(info_flags::negative))
437 return cos(x).hold();
440 static ex cos_deriv(const ex & x, unsigned deriv_param)
442 GINAC_ASSERT(deriv_param==0);
444 // d/dx cos(x) -> -sin(x)
448 static ex cos_real_part(const ex & x)
450 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
453 static ex cos_imag_part(const ex & x)
455 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
458 REGISTER_FUNCTION(cos, eval_func(cos_eval).
459 evalf_func(cos_evalf).
460 derivative_func(cos_deriv).
461 real_part_func(cos_real_part).
462 imag_part_func(cos_imag_part).
463 latex_name("\\cos"));
466 // tangent (trigonometric function)
469 static ex tan_evalf(const ex & x)
471 if (is_exactly_a<numeric>(x))
472 return tan(ex_to<numeric>(x));
474 return tan(x).hold();
477 static ex tan_eval(const ex & x)
479 // tan(n/d*Pi) -> { all known non-nested radicals }
480 const ex SixtyExOverPi = _ex60*x/Pi;
482 if (SixtyExOverPi.info(info_flags::integer)) {
483 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
485 // wrap to interval [0, Pi)
489 // wrap to interval [0, Pi/2)
493 if (z.is_equal(*_num0_p)) // tan(0) -> 0
495 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
496 return sign*(_ex2-sqrt(_ex3));
497 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
498 return sign*_ex1_3*sqrt(_ex3);
499 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
501 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
502 return sign*sqrt(_ex3);
503 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
504 return sign*(sqrt(_ex3)+_ex2);
505 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
506 throw (pole_error("tan_eval(): simple pole",1));
509 if (is_exactly_a<function>(x)) {
510 const ex &t = x.op(0);
513 if (is_ex_the_function(x, atan))
516 // tan(asin(x)) -> x/sqrt(1+x^2)
517 if (is_ex_the_function(x, asin))
518 return t*power(_ex1-power(t,_ex2),_ex_1_2);
520 // tan(acos(x)) -> sqrt(1-x^2)/x
521 if (is_ex_the_function(x, acos))
522 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
525 // tan(float) -> float
526 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
527 return tan(ex_to<numeric>(x));
531 if (x.info(info_flags::negative))
534 return tan(x).hold();
537 static ex tan_deriv(const ex & x, unsigned deriv_param)
539 GINAC_ASSERT(deriv_param==0);
541 // d/dx tan(x) -> 1+tan(x)^2;
542 return (_ex1+power(tan(x),_ex2));
545 static ex tan_real_part(const ex & x)
547 ex a = GiNaC::real_part(x);
548 ex b = GiNaC::imag_part(x);
549 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
552 static ex tan_imag_part(const ex & x)
554 ex a = GiNaC::real_part(x);
555 ex b = GiNaC::imag_part(x);
556 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
559 static ex tan_series(const ex &x,
560 const relational &rel,
564 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
566 // Taylor series where there is no pole falls back to tan_deriv.
567 // On a pole simply expand sin(x)/cos(x).
568 const ex x_pt = x.subs(rel, subs_options::no_pattern);
569 if (!(2*x_pt/Pi).info(info_flags::odd))
570 throw do_taylor(); // caught by function::series()
571 // if we got here we have to care for a simple pole
572 return (sin(x)/cos(x)).series(rel, order, options);
575 REGISTER_FUNCTION(tan, eval_func(tan_eval).
576 evalf_func(tan_evalf).
577 derivative_func(tan_deriv).
578 series_func(tan_series).
579 real_part_func(tan_real_part).
580 imag_part_func(tan_imag_part).
581 latex_name("\\tan"));
584 // inverse sine (arc sine)
587 static ex asin_evalf(const ex & x)
589 if (is_exactly_a<numeric>(x))
590 return asin(ex_to<numeric>(x));
592 return asin(x).hold();
595 static ex asin_eval(const ex & x)
597 if (x.info(info_flags::numeric)) {
604 if (x.is_equal(_ex1_2))
605 return numeric(1,6)*Pi;
608 if (x.is_equal(_ex1))
611 // asin(-1/2) -> -Pi/6
612 if (x.is_equal(_ex_1_2))
613 return numeric(-1,6)*Pi;
616 if (x.is_equal(_ex_1))
619 // asin(float) -> float
620 if (!x.info(info_flags::crational))
621 return asin(ex_to<numeric>(x));
624 if (x.info(info_flags::negative))
628 return asin(x).hold();
631 static ex asin_deriv(const ex & x, unsigned deriv_param)
633 GINAC_ASSERT(deriv_param==0);
635 // d/dx asin(x) -> 1/sqrt(1-x^2)
636 return power(1-power(x,_ex2),_ex_1_2);
639 REGISTER_FUNCTION(asin, eval_func(asin_eval).
640 evalf_func(asin_evalf).
641 derivative_func(asin_deriv).
642 latex_name("\\arcsin"));
645 // inverse cosine (arc cosine)
648 static ex acos_evalf(const ex & x)
650 if (is_exactly_a<numeric>(x))
651 return acos(ex_to<numeric>(x));
653 return acos(x).hold();
656 static ex acos_eval(const ex & x)
658 if (x.info(info_flags::numeric)) {
661 if (x.is_equal(_ex1))
665 if (x.is_equal(_ex1_2))
672 // acos(-1/2) -> 2/3*Pi
673 if (x.is_equal(_ex_1_2))
674 return numeric(2,3)*Pi;
677 if (x.is_equal(_ex_1))
680 // acos(float) -> float
681 if (!x.info(info_flags::crational))
682 return acos(ex_to<numeric>(x));
684 // acos(-x) -> Pi-acos(x)
685 if (x.info(info_flags::negative))
689 return acos(x).hold();
692 static ex acos_deriv(const ex & x, unsigned deriv_param)
694 GINAC_ASSERT(deriv_param==0);
696 // d/dx acos(x) -> -1/sqrt(1-x^2)
697 return -power(1-power(x,_ex2),_ex_1_2);
700 REGISTER_FUNCTION(acos, eval_func(acos_eval).
701 evalf_func(acos_evalf).
702 derivative_func(acos_deriv).
703 latex_name("\\arccos"));
706 // inverse tangent (arc tangent)
709 static ex atan_evalf(const ex & x)
711 if (is_exactly_a<numeric>(x))
712 return atan(ex_to<numeric>(x));
714 return atan(x).hold();
717 static ex atan_eval(const ex & x)
719 if (x.info(info_flags::numeric)) {
726 if (x.is_equal(_ex1))
730 if (x.is_equal(_ex_1))
733 if (x.is_equal(I) || x.is_equal(-I))
734 throw (pole_error("atan_eval(): logarithmic pole",0));
736 // atan(float) -> float
737 if (!x.info(info_flags::crational))
738 return atan(ex_to<numeric>(x));
741 if (x.info(info_flags::negative))
745 return atan(x).hold();
748 static ex atan_deriv(const ex & x, unsigned deriv_param)
750 GINAC_ASSERT(deriv_param==0);
752 // d/dx atan(x) -> 1/(1+x^2)
753 return power(_ex1+power(x,_ex2), _ex_1);
756 static ex atan_series(const ex &arg,
757 const relational &rel,
761 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
763 // Taylor series where there is no pole or cut falls back to atan_deriv.
764 // There are two branch cuts, one runnig from I up the imaginary axis and
765 // one running from -I down the imaginary axis. The points I and -I are
767 // On the branch cuts and the poles series expand
768 // (log(1+I*x)-log(1-I*x))/(2*I)
770 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
771 if (!(I*arg_pt).info(info_flags::real))
772 throw do_taylor(); // Re(x) != 0
773 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
774 throw do_taylor(); // Re(x) == 0, but abs(x)<1
775 // care for the poles, using the defining formula for atan()...
776 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
777 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
778 if (!(options & series_options::suppress_branchcut)) {
780 // This is the branch cut: assemble the primitive series manually and
781 // then add the corresponding complex step function.
782 const symbol &s = ex_to<symbol>(rel.lhs());
783 const ex &point = rel.rhs();
785 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
786 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
788 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
790 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
792 seq.push_back(expair(Order0correction, _ex0));
793 seq.push_back(expair(Order(_ex1), order));
794 return series(replarg - pseries(rel, seq), rel, order);
799 REGISTER_FUNCTION(atan, eval_func(atan_eval).
800 evalf_func(atan_evalf).
801 derivative_func(atan_deriv).
802 series_func(atan_series).
803 latex_name("\\arctan"));
806 // inverse tangent (atan2(y,x))
809 static ex atan2_evalf(const ex &y, const ex &x)
811 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
812 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
814 return atan2(y, x).hold();
817 static ex atan2_eval(const ex & y, const ex & x)
819 if (y.info(info_flags::numeric) && x.info(info_flags::numeric)) {
827 // atan(0, x), x real and positive -> 0
828 if (x.info(info_flags::positive))
831 // atan(0, x), x real and negative -> -Pi
832 if (x.info(info_flags::negative))
838 // atan(y, 0), y real and positive -> Pi/2
839 if (y.info(info_flags::positive))
842 // atan(y, 0), y real and negative -> -Pi/2
843 if (y.info(info_flags::negative))
849 // atan(y, y), y real and positive -> Pi/4
850 if (y.info(info_flags::positive))
853 // atan(y, y), y real and negative -> -3/4*Pi
854 if (y.info(info_flags::negative))
855 return numeric(-3, 4)*Pi;
858 if (y.is_equal(-x)) {
860 // atan(y, -y), y real and positive -> 3*Pi/4
861 if (y.info(info_flags::positive))
862 return numeric(3, 4)*Pi;
864 // atan(y, -y), y real and negative -> -Pi/4
865 if (y.info(info_flags::negative))
869 // atan(float, float) -> float
870 if (!y.info(info_flags::crational) && !x.info(info_flags::crational))
871 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
873 // atan(real, real) -> atan(y/x) +/- Pi
874 if (y.info(info_flags::real) && x.info(info_flags::real)) {
875 if (x.info(info_flags::positive))
877 else if(y.info(info_flags::positive))
884 return atan2(y, x).hold();
887 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
889 GINAC_ASSERT(deriv_param<2);
891 if (deriv_param==0) {
893 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
896 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
899 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
900 evalf_func(atan2_evalf).
901 derivative_func(atan2_deriv));
904 // hyperbolic sine (trigonometric function)
907 static ex sinh_evalf(const ex & x)
909 if (is_exactly_a<numeric>(x))
910 return sinh(ex_to<numeric>(x));
912 return sinh(x).hold();
915 static ex sinh_eval(const ex & x)
917 if (x.info(info_flags::numeric)) {
923 // sinh(float) -> float
924 if (!x.info(info_flags::crational))
925 return sinh(ex_to<numeric>(x));
928 if (x.info(info_flags::negative))
932 if ((x/Pi).info(info_flags::numeric) &&
933 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
936 if (is_exactly_a<function>(x)) {
937 const ex &t = x.op(0);
939 // sinh(asinh(x)) -> x
940 if (is_ex_the_function(x, asinh))
943 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
944 if (is_ex_the_function(x, acosh))
945 return sqrt(t-_ex1)*sqrt(t+_ex1);
947 // sinh(atanh(x)) -> x/sqrt(1-x^2)
948 if (is_ex_the_function(x, atanh))
949 return t*power(_ex1-power(t,_ex2),_ex_1_2);
952 return sinh(x).hold();
955 static ex sinh_deriv(const ex & x, unsigned deriv_param)
957 GINAC_ASSERT(deriv_param==0);
959 // d/dx sinh(x) -> cosh(x)
963 static ex sinh_real_part(const ex & x)
965 return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
968 static ex sinh_imag_part(const ex & x)
970 return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
973 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
974 evalf_func(sinh_evalf).
975 derivative_func(sinh_deriv).
976 real_part_func(sinh_real_part).
977 imag_part_func(sinh_imag_part).
978 latex_name("\\sinh"));
981 // hyperbolic cosine (trigonometric function)
984 static ex cosh_evalf(const ex & x)
986 if (is_exactly_a<numeric>(x))
987 return cosh(ex_to<numeric>(x));
989 return cosh(x).hold();
992 static ex cosh_eval(const ex & x)
994 if (x.info(info_flags::numeric)) {
1000 // cosh(float) -> float
1001 if (!x.info(info_flags::crational))
1002 return cosh(ex_to<numeric>(x));
1005 if (x.info(info_flags::negative))
1009 if ((x/Pi).info(info_flags::numeric) &&
1010 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
1013 if (is_exactly_a<function>(x)) {
1014 const ex &t = x.op(0);
1016 // cosh(acosh(x)) -> x
1017 if (is_ex_the_function(x, acosh))
1020 // cosh(asinh(x)) -> sqrt(1+x^2)
1021 if (is_ex_the_function(x, asinh))
1022 return sqrt(_ex1+power(t,_ex2));
1024 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1025 if (is_ex_the_function(x, atanh))
1026 return power(_ex1-power(t,_ex2),_ex_1_2);
1029 return cosh(x).hold();
1032 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1034 GINAC_ASSERT(deriv_param==0);
1036 // d/dx cosh(x) -> sinh(x)
1040 static ex cosh_real_part(const ex & x)
1042 return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1045 static ex cosh_imag_part(const ex & x)
1047 return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1050 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1051 evalf_func(cosh_evalf).
1052 derivative_func(cosh_deriv).
1053 real_part_func(cosh_real_part).
1054 imag_part_func(cosh_imag_part).
1055 latex_name("\\cosh"));
1058 // hyperbolic tangent (trigonometric function)
1061 static ex tanh_evalf(const ex & x)
1063 if (is_exactly_a<numeric>(x))
1064 return tanh(ex_to<numeric>(x));
1066 return tanh(x).hold();
1069 static ex tanh_eval(const ex & x)
1071 if (x.info(info_flags::numeric)) {
1077 // tanh(float) -> float
1078 if (!x.info(info_flags::crational))
1079 return tanh(ex_to<numeric>(x));
1082 if (x.info(info_flags::negative))
1086 if ((x/Pi).info(info_flags::numeric) &&
1087 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1090 if (is_exactly_a<function>(x)) {
1091 const ex &t = x.op(0);
1093 // tanh(atanh(x)) -> x
1094 if (is_ex_the_function(x, atanh))
1097 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1098 if (is_ex_the_function(x, asinh))
1099 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1101 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1102 if (is_ex_the_function(x, acosh))
1103 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1106 return tanh(x).hold();
1109 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1111 GINAC_ASSERT(deriv_param==0);
1113 // d/dx tanh(x) -> 1-tanh(x)^2
1114 return _ex1-power(tanh(x),_ex2);
1117 static ex tanh_series(const ex &x,
1118 const relational &rel,
1122 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1124 // Taylor series where there is no pole falls back to tanh_deriv.
1125 // On a pole simply expand sinh(x)/cosh(x).
1126 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1127 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1128 throw do_taylor(); // caught by function::series()
1129 // if we got here we have to care for a simple pole
1130 return (sinh(x)/cosh(x)).series(rel, order, options);
1133 static ex tanh_real_part(const ex & x)
1135 ex a = GiNaC::real_part(x);
1136 ex b = GiNaC::imag_part(x);
1137 return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1140 static ex tanh_imag_part(const ex & x)
1142 ex a = GiNaC::real_part(x);
1143 ex b = GiNaC::imag_part(x);
1144 return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1147 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1148 evalf_func(tanh_evalf).
1149 derivative_func(tanh_deriv).
1150 series_func(tanh_series).
1151 real_part_func(tanh_real_part).
1152 imag_part_func(tanh_imag_part).
1153 latex_name("\\tanh"));
1156 // inverse hyperbolic sine (trigonometric function)
1159 static ex asinh_evalf(const ex & x)
1161 if (is_exactly_a<numeric>(x))
1162 return asinh(ex_to<numeric>(x));
1164 return asinh(x).hold();
1167 static ex asinh_eval(const ex & x)
1169 if (x.info(info_flags::numeric)) {
1175 // asinh(float) -> float
1176 if (!x.info(info_flags::crational))
1177 return asinh(ex_to<numeric>(x));
1180 if (x.info(info_flags::negative))
1184 return asinh(x).hold();
1187 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1189 GINAC_ASSERT(deriv_param==0);
1191 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1192 return power(_ex1+power(x,_ex2),_ex_1_2);
1195 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1196 evalf_func(asinh_evalf).
1197 derivative_func(asinh_deriv));
1200 // inverse hyperbolic cosine (trigonometric function)
1203 static ex acosh_evalf(const ex & x)
1205 if (is_exactly_a<numeric>(x))
1206 return acosh(ex_to<numeric>(x));
1208 return acosh(x).hold();
1211 static ex acosh_eval(const ex & x)
1213 if (x.info(info_flags::numeric)) {
1215 // acosh(0) -> Pi*I/2
1217 return Pi*I*numeric(1,2);
1220 if (x.is_equal(_ex1))
1223 // acosh(-1) -> Pi*I
1224 if (x.is_equal(_ex_1))
1227 // acosh(float) -> float
1228 if (!x.info(info_flags::crational))
1229 return acosh(ex_to<numeric>(x));
1231 // acosh(-x) -> Pi*I-acosh(x)
1232 if (x.info(info_flags::negative))
1233 return Pi*I-acosh(-x);
1236 return acosh(x).hold();
1239 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1241 GINAC_ASSERT(deriv_param==0);
1243 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1244 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1247 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1248 evalf_func(acosh_evalf).
1249 derivative_func(acosh_deriv));
1252 // inverse hyperbolic tangent (trigonometric function)
1255 static ex atanh_evalf(const ex & x)
1257 if (is_exactly_a<numeric>(x))
1258 return atanh(ex_to<numeric>(x));
1260 return atanh(x).hold();
1263 static ex atanh_eval(const ex & x)
1265 if (x.info(info_flags::numeric)) {
1271 // atanh({+|-}1) -> throw
1272 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1273 throw (pole_error("atanh_eval(): logarithmic pole",0));
1275 // atanh(float) -> float
1276 if (!x.info(info_flags::crational))
1277 return atanh(ex_to<numeric>(x));
1280 if (x.info(info_flags::negative))
1284 return atanh(x).hold();
1287 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1289 GINAC_ASSERT(deriv_param==0);
1291 // d/dx atanh(x) -> 1/(1-x^2)
1292 return power(_ex1-power(x,_ex2),_ex_1);
1295 static ex atanh_series(const ex &arg,
1296 const relational &rel,
1300 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1302 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1303 // There are two branch cuts, one runnig from 1 up the real axis and one
1304 // one running from -1 down the real axis. The points 1 and -1 are poles
1305 // On the branch cuts and the poles series expand
1306 // (log(1+x)-log(1-x))/2
1308 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1309 if (!(arg_pt).info(info_flags::real))
1310 throw do_taylor(); // Im(x) != 0
1311 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1312 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1313 // care for the poles, using the defining formula for atanh()...
1314 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1315 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1316 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1317 if (!(options & series_options::suppress_branchcut)) {
1319 // This is the branch cut: assemble the primitive series manually and
1320 // then add the corresponding complex step function.
1321 const symbol &s = ex_to<symbol>(rel.lhs());
1322 const ex &point = rel.rhs();
1324 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1325 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1327 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1329 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1331 seq.push_back(expair(Order0correction, _ex0));
1332 seq.push_back(expair(Order(_ex1), order));
1333 return series(replarg - pseries(rel, seq), rel, order);
1338 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1339 evalf_func(atanh_evalf).
1340 derivative_func(atanh_deriv).
1341 series_func(atanh_series));
1344 } // namespace GiNaC