1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2016 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
31 #include "operators.h"
32 #include "relational.h"
43 // exponential function
46 static ex exp_evalf(const ex & x)
48 if (is_exactly_a<numeric>(x))
49 return exp(ex_to<numeric>(x));
54 static ex exp_eval(const ex & x)
61 // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
62 const ex TwoExOverPiI=(_ex2*x)/(Pi*I);
63 if (TwoExOverPiI.info(info_flags::integer)) {
64 const numeric z = mod(ex_to<numeric>(TwoExOverPiI),*_num4_p);
65 if (z.is_equal(*_num0_p))
67 if (z.is_equal(*_num1_p))
69 if (z.is_equal(*_num2_p))
71 if (z.is_equal(*_num3_p))
76 if (is_ex_the_function(x, log))
79 // exp(float) -> float
80 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
81 return exp(ex_to<numeric>(x));
86 static ex exp_expand(const ex & arg, unsigned options)
89 if (options & expand_options::expand_function_args)
90 exp_arg = arg.expand(options);
94 if ((options & expand_options::expand_transcendental)
95 && is_exactly_a<add>(exp_arg)) {
97 prodseq.reserve(exp_arg.nops());
98 for (const_iterator i = exp_arg.begin(); i != exp_arg.end(); ++i)
99 prodseq.push_back(exp(*i));
101 return dynallocate<mul>(prodseq).setflag(status_flags::expanded);
104 return exp(exp_arg).hold();
107 static ex exp_deriv(const ex & x, unsigned deriv_param)
109 GINAC_ASSERT(deriv_param==0);
111 // d/dx exp(x) -> exp(x)
115 static ex exp_real_part(const ex & x)
117 return exp(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
120 static ex exp_imag_part(const ex & x)
122 return exp(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
125 static ex exp_conjugate(const ex & x)
127 // conjugate(exp(x))==exp(conjugate(x))
128 return exp(x.conjugate());
131 REGISTER_FUNCTION(exp, eval_func(exp_eval).
132 evalf_func(exp_evalf).
133 expand_func(exp_expand).
134 derivative_func(exp_deriv).
135 real_part_func(exp_real_part).
136 imag_part_func(exp_imag_part).
137 conjugate_func(exp_conjugate).
138 latex_name("\\exp"));
144 static ex log_evalf(const ex & x)
146 if (is_exactly_a<numeric>(x))
147 return log(ex_to<numeric>(x));
149 return log(x).hold();
152 static ex log_eval(const ex & x)
154 if (x.info(info_flags::numeric)) {
155 if (x.is_zero()) // log(0) -> infinity
156 throw(pole_error("log_eval(): log(0)",0));
157 if (x.info(info_flags::rational) && x.info(info_flags::negative))
158 return (log(-x)+I*Pi);
159 if (x.is_equal(_ex1)) // log(1) -> 0
161 if (x.is_equal(I)) // log(I) -> Pi*I/2
162 return (Pi*I*_ex1_2);
163 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
164 return (Pi*I*_ex_1_2);
166 // log(float) -> float
167 if (!x.info(info_flags::crational))
168 return log(ex_to<numeric>(x));
171 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
172 if (is_ex_the_function(x, exp)) {
173 const ex &t = x.op(0);
174 if (t.info(info_flags::real))
178 return log(x).hold();
181 static ex log_deriv(const ex & x, unsigned deriv_param)
183 GINAC_ASSERT(deriv_param==0);
185 // d/dx log(x) -> 1/x
186 return power(x, _ex_1);
189 static ex log_series(const ex &arg,
190 const relational &rel,
194 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
196 bool must_expand_arg = false;
197 // maybe substitution of rel into arg fails because of a pole
199 arg_pt = arg.subs(rel, subs_options::no_pattern);
200 } catch (pole_error) {
201 must_expand_arg = true;
203 // or we are at the branch point anyways
204 if (arg_pt.is_zero())
205 must_expand_arg = true;
207 if (arg.diff(ex_to<symbol>(rel.lhs())).is_zero()) {
211 if (must_expand_arg) {
213 // This is the branch point: Series expand the argument first, then
214 // trivially factorize it to isolate that part which has constant
215 // leading coefficient in this fashion:
216 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
217 // Return a plain n*log(x) for the x^n part and series expand the
218 // other part. Add them together and reexpand again in order to have
219 // one unnested pseries object. All this also works for negative n.
220 pseries argser; // series expansion of log's argument
221 unsigned extra_ord = 0; // extra expansion order
223 // oops, the argument expanded to a pure Order(x^something)...
224 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
226 } while (!argser.is_terminating() && argser.nops()==1);
228 const symbol &s = ex_to<symbol>(rel.lhs());
229 const ex &point = rel.rhs();
230 const int n = argser.ldegree(s);
232 // construct what we carelessly called the n*log(x) term above
233 const ex coeff = argser.coeff(s, n);
234 // expand the log, but only if coeff is real and > 0, since otherwise
235 // it would make the branch cut run into the wrong direction
236 if (coeff.info(info_flags::positive))
237 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
239 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
241 if (!argser.is_terminating() || argser.nops()!=1) {
242 // in this case n more (or less) terms are needed
243 // (sadly, to generate them, we have to start from the beginning)
244 if (n == 0 && coeff == 1) {
245 ex rest = pseries(rel, epvector{expair(-1, _ex0), expair(Order(_ex1), order)}).add_series(argser);
246 ex acc = dynallocate<pseries>(rel, epvector());
247 for (int i = order-1; i>0; --i) {
248 epvector cterm { expair(i%2 ? _ex1/i : _ex_1/i, _ex0) };
249 acc = pseries(rel, std::move(cterm)).add_series(ex_to<pseries>(acc));
250 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
254 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
255 return pseries(rel, std::move(seq)).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
256 } else // it was a monomial
257 return pseries(rel, std::move(seq));
259 if (!(options & series_options::suppress_branchcut) &&
260 arg_pt.info(info_flags::negative)) {
262 // This is the branch cut: assemble the primitive series manually and
263 // then add the corresponding complex step function.
264 const symbol &s = ex_to<symbol>(rel.lhs());
265 const ex &point = rel.rhs();
267 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
268 epvector seq { expair(-I*csgn(arg*I)*Pi, _ex0),
269 expair(Order(_ex1), order) };
270 return series(replarg - I*Pi + pseries(rel, std::move(seq)), rel, order);
272 throw do_taylor(); // caught by function::series()
275 static ex log_real_part(const ex & x)
277 if (x.info(info_flags::nonnegative))
278 return log(x).hold();
282 static ex log_imag_part(const ex & x)
284 if (x.info(info_flags::nonnegative))
286 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
289 static ex log_expand(const ex & arg, unsigned options)
291 if ((options & expand_options::expand_transcendental)
292 && is_exactly_a<mul>(arg) && !arg.info(info_flags::indefinite)) {
295 sumseq.reserve(arg.nops());
296 prodseq.reserve(arg.nops());
299 // searching for positive/negative factors
300 for (const_iterator i = arg.begin(); i != arg.end(); ++i) {
302 if (options & expand_options::expand_function_args)
303 e=i->expand(options);
306 if (e.info(info_flags::positive))
307 sumseq.push_back(log(e));
308 else if (e.info(info_flags::negative)) {
309 sumseq.push_back(log(-e));
312 prodseq.push_back(e);
315 if (sumseq.size() > 0) {
317 if (options & expand_options::expand_function_args)
318 newarg=((possign?_ex1:_ex_1)*mul(prodseq)).expand(options);
320 newarg=(possign?_ex1:_ex_1)*mul(prodseq);
321 ex_to<basic>(newarg).setflag(status_flags::purely_indefinite);
323 return add(sumseq)+log(newarg);
325 if (!(options & expand_options::expand_function_args))
326 ex_to<basic>(arg).setflag(status_flags::purely_indefinite);
330 if (options & expand_options::expand_function_args)
331 return log(arg.expand(options)).hold();
333 return log(arg).hold();
336 static ex log_conjugate(const ex & x)
338 // conjugate(log(x))==log(conjugate(x)) unless on the branch cut which
339 // runs along the negative real axis.
340 if (x.info(info_flags::positive)) {
343 if (is_exactly_a<numeric>(x) &&
344 !x.imag_part().is_zero()) {
345 return log(x.conjugate());
347 return conjugate_function(log(x)).hold();
350 REGISTER_FUNCTION(log, eval_func(log_eval).
351 evalf_func(log_evalf).
352 expand_func(log_expand).
353 derivative_func(log_deriv).
354 series_func(log_series).
355 real_part_func(log_real_part).
356 imag_part_func(log_imag_part).
357 conjugate_func(log_conjugate).
361 // sine (trigonometric function)
364 static ex sin_evalf(const ex & x)
366 if (is_exactly_a<numeric>(x))
367 return sin(ex_to<numeric>(x));
369 return sin(x).hold();
372 static ex sin_eval(const ex & x)
374 // sin(n/d*Pi) -> { all known non-nested radicals }
375 const ex SixtyExOverPi = _ex60*x/Pi;
377 if (SixtyExOverPi.info(info_flags::integer)) {
378 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
380 // wrap to interval [0, Pi)
385 // wrap to interval [0, Pi/2)
388 if (z.is_equal(*_num0_p)) // sin(0) -> 0
390 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
391 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
392 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
393 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
394 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
396 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
397 return sign*_ex1_2*sqrt(_ex2);
398 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
399 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
400 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
401 return sign*_ex1_2*sqrt(_ex3);
402 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
403 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
404 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
408 if (is_exactly_a<function>(x)) {
409 const ex &t = x.op(0);
412 if (is_ex_the_function(x, asin))
415 // sin(acos(x)) -> sqrt(1-x^2)
416 if (is_ex_the_function(x, acos))
417 return sqrt(_ex1-power(t,_ex2));
419 // sin(atan(x)) -> x/sqrt(1+x^2)
420 if (is_ex_the_function(x, atan))
421 return t*power(_ex1+power(t,_ex2),_ex_1_2);
424 // sin(float) -> float
425 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
426 return sin(ex_to<numeric>(x));
429 if (x.info(info_flags::negative))
432 return sin(x).hold();
435 static ex sin_deriv(const ex & x, unsigned deriv_param)
437 GINAC_ASSERT(deriv_param==0);
439 // d/dx sin(x) -> cos(x)
443 static ex sin_real_part(const ex & x)
445 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
448 static ex sin_imag_part(const ex & x)
450 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
453 static ex sin_conjugate(const ex & x)
455 // conjugate(sin(x))==sin(conjugate(x))
456 return sin(x.conjugate());
459 REGISTER_FUNCTION(sin, eval_func(sin_eval).
460 evalf_func(sin_evalf).
461 derivative_func(sin_deriv).
462 real_part_func(sin_real_part).
463 imag_part_func(sin_imag_part).
464 conjugate_func(sin_conjugate).
465 latex_name("\\sin"));
468 // cosine (trigonometric function)
471 static ex cos_evalf(const ex & x)
473 if (is_exactly_a<numeric>(x))
474 return cos(ex_to<numeric>(x));
476 return cos(x).hold();
479 static ex cos_eval(const ex & x)
481 // cos(n/d*Pi) -> { all known non-nested radicals }
482 const ex SixtyExOverPi = _ex60*x/Pi;
484 if (SixtyExOverPi.info(info_flags::integer)) {
485 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
487 // wrap to interval [0, Pi)
491 // wrap to interval [0, Pi/2)
495 if (z.is_equal(*_num0_p)) // cos(0) -> 1
497 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
498 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
499 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
500 return sign*_ex1_2*sqrt(_ex3);
501 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
502 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
503 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
504 return sign*_ex1_2*sqrt(_ex2);
505 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
507 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
508 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
509 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
510 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
511 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
515 if (is_exactly_a<function>(x)) {
516 const ex &t = x.op(0);
519 if (is_ex_the_function(x, acos))
522 // cos(asin(x)) -> sqrt(1-x^2)
523 if (is_ex_the_function(x, asin))
524 return sqrt(_ex1-power(t,_ex2));
526 // cos(atan(x)) -> 1/sqrt(1+x^2)
527 if (is_ex_the_function(x, atan))
528 return power(_ex1+power(t,_ex2),_ex_1_2);
531 // cos(float) -> float
532 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
533 return cos(ex_to<numeric>(x));
536 if (x.info(info_flags::negative))
539 return cos(x).hold();
542 static ex cos_deriv(const ex & x, unsigned deriv_param)
544 GINAC_ASSERT(deriv_param==0);
546 // d/dx cos(x) -> -sin(x)
550 static ex cos_real_part(const ex & x)
552 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
555 static ex cos_imag_part(const ex & x)
557 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
560 static ex cos_conjugate(const ex & x)
562 // conjugate(cos(x))==cos(conjugate(x))
563 return cos(x.conjugate());
566 REGISTER_FUNCTION(cos, eval_func(cos_eval).
567 evalf_func(cos_evalf).
568 derivative_func(cos_deriv).
569 real_part_func(cos_real_part).
570 imag_part_func(cos_imag_part).
571 conjugate_func(cos_conjugate).
572 latex_name("\\cos"));
575 // tangent (trigonometric function)
578 static ex tan_evalf(const ex & x)
580 if (is_exactly_a<numeric>(x))
581 return tan(ex_to<numeric>(x));
583 return tan(x).hold();
586 static ex tan_eval(const ex & x)
588 // tan(n/d*Pi) -> { all known non-nested radicals }
589 const ex SixtyExOverPi = _ex60*x/Pi;
591 if (SixtyExOverPi.info(info_flags::integer)) {
592 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
594 // wrap to interval [0, Pi)
598 // wrap to interval [0, Pi/2)
602 if (z.is_equal(*_num0_p)) // tan(0) -> 0
604 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
605 return sign*(_ex2-sqrt(_ex3));
606 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
607 return sign*_ex1_3*sqrt(_ex3);
608 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
610 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
611 return sign*sqrt(_ex3);
612 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
613 return sign*(sqrt(_ex3)+_ex2);
614 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
615 throw (pole_error("tan_eval(): simple pole",1));
618 if (is_exactly_a<function>(x)) {
619 const ex &t = x.op(0);
622 if (is_ex_the_function(x, atan))
625 // tan(asin(x)) -> x/sqrt(1+x^2)
626 if (is_ex_the_function(x, asin))
627 return t*power(_ex1-power(t,_ex2),_ex_1_2);
629 // tan(acos(x)) -> sqrt(1-x^2)/x
630 if (is_ex_the_function(x, acos))
631 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
634 // tan(float) -> float
635 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
636 return tan(ex_to<numeric>(x));
640 if (x.info(info_flags::negative))
643 return tan(x).hold();
646 static ex tan_deriv(const ex & x, unsigned deriv_param)
648 GINAC_ASSERT(deriv_param==0);
650 // d/dx tan(x) -> 1+tan(x)^2;
651 return (_ex1+power(tan(x),_ex2));
654 static ex tan_real_part(const ex & x)
656 ex a = GiNaC::real_part(x);
657 ex b = GiNaC::imag_part(x);
658 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
661 static ex tan_imag_part(const ex & x)
663 ex a = GiNaC::real_part(x);
664 ex b = GiNaC::imag_part(x);
665 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
668 static ex tan_series(const ex &x,
669 const relational &rel,
673 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
675 // Taylor series where there is no pole falls back to tan_deriv.
676 // On a pole simply expand sin(x)/cos(x).
677 const ex x_pt = x.subs(rel, subs_options::no_pattern);
678 if (!(2*x_pt/Pi).info(info_flags::odd))
679 throw do_taylor(); // caught by function::series()
680 // if we got here we have to care for a simple pole
681 return (sin(x)/cos(x)).series(rel, order, options);
684 static ex tan_conjugate(const ex & x)
686 // conjugate(tan(x))==tan(conjugate(x))
687 return tan(x.conjugate());
690 REGISTER_FUNCTION(tan, eval_func(tan_eval).
691 evalf_func(tan_evalf).
692 derivative_func(tan_deriv).
693 series_func(tan_series).
694 real_part_func(tan_real_part).
695 imag_part_func(tan_imag_part).
696 conjugate_func(tan_conjugate).
697 latex_name("\\tan"));
700 // inverse sine (arc sine)
703 static ex asin_evalf(const ex & x)
705 if (is_exactly_a<numeric>(x))
706 return asin(ex_to<numeric>(x));
708 return asin(x).hold();
711 static ex asin_eval(const ex & x)
713 if (x.info(info_flags::numeric)) {
720 if (x.is_equal(_ex1_2))
721 return numeric(1,6)*Pi;
724 if (x.is_equal(_ex1))
727 // asin(-1/2) -> -Pi/6
728 if (x.is_equal(_ex_1_2))
729 return numeric(-1,6)*Pi;
732 if (x.is_equal(_ex_1))
735 // asin(float) -> float
736 if (!x.info(info_flags::crational))
737 return asin(ex_to<numeric>(x));
740 if (x.info(info_flags::negative))
744 return asin(x).hold();
747 static ex asin_deriv(const ex & x, unsigned deriv_param)
749 GINAC_ASSERT(deriv_param==0);
751 // d/dx asin(x) -> 1/sqrt(1-x^2)
752 return power(1-power(x,_ex2),_ex_1_2);
755 static ex asin_conjugate(const ex & x)
757 // conjugate(asin(x))==asin(conjugate(x)) unless on the branch cuts which
758 // run along the real axis outside the interval [-1, +1].
759 if (is_exactly_a<numeric>(x) &&
760 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
761 return asin(x.conjugate());
763 return conjugate_function(asin(x)).hold();
766 REGISTER_FUNCTION(asin, eval_func(asin_eval).
767 evalf_func(asin_evalf).
768 derivative_func(asin_deriv).
769 conjugate_func(asin_conjugate).
770 latex_name("\\arcsin"));
773 // inverse cosine (arc cosine)
776 static ex acos_evalf(const ex & x)
778 if (is_exactly_a<numeric>(x))
779 return acos(ex_to<numeric>(x));
781 return acos(x).hold();
784 static ex acos_eval(const ex & x)
786 if (x.info(info_flags::numeric)) {
789 if (x.is_equal(_ex1))
793 if (x.is_equal(_ex1_2))
800 // acos(-1/2) -> 2/3*Pi
801 if (x.is_equal(_ex_1_2))
802 return numeric(2,3)*Pi;
805 if (x.is_equal(_ex_1))
808 // acos(float) -> float
809 if (!x.info(info_flags::crational))
810 return acos(ex_to<numeric>(x));
812 // acos(-x) -> Pi-acos(x)
813 if (x.info(info_flags::negative))
817 return acos(x).hold();
820 static ex acos_deriv(const ex & x, unsigned deriv_param)
822 GINAC_ASSERT(deriv_param==0);
824 // d/dx acos(x) -> -1/sqrt(1-x^2)
825 return -power(1-power(x,_ex2),_ex_1_2);
828 static ex acos_conjugate(const ex & x)
830 // conjugate(acos(x))==acos(conjugate(x)) unless on the branch cuts which
831 // run along the real axis outside the interval [-1, +1].
832 if (is_exactly_a<numeric>(x) &&
833 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
834 return acos(x.conjugate());
836 return conjugate_function(acos(x)).hold();
839 REGISTER_FUNCTION(acos, eval_func(acos_eval).
840 evalf_func(acos_evalf).
841 derivative_func(acos_deriv).
842 conjugate_func(acos_conjugate).
843 latex_name("\\arccos"));
846 // inverse tangent (arc tangent)
849 static ex atan_evalf(const ex & x)
851 if (is_exactly_a<numeric>(x))
852 return atan(ex_to<numeric>(x));
854 return atan(x).hold();
857 static ex atan_eval(const ex & x)
859 if (x.info(info_flags::numeric)) {
866 if (x.is_equal(_ex1))
870 if (x.is_equal(_ex_1))
873 if (x.is_equal(I) || x.is_equal(-I))
874 throw (pole_error("atan_eval(): logarithmic pole",0));
876 // atan(float) -> float
877 if (!x.info(info_flags::crational))
878 return atan(ex_to<numeric>(x));
881 if (x.info(info_flags::negative))
885 return atan(x).hold();
888 static ex atan_deriv(const ex & x, unsigned deriv_param)
890 GINAC_ASSERT(deriv_param==0);
892 // d/dx atan(x) -> 1/(1+x^2)
893 return power(_ex1+power(x,_ex2), _ex_1);
896 static ex atan_series(const ex &arg,
897 const relational &rel,
901 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
903 // Taylor series where there is no pole or cut falls back to atan_deriv.
904 // There are two branch cuts, one runnig from I up the imaginary axis and
905 // one running from -I down the imaginary axis. The points I and -I are
907 // On the branch cuts and the poles series expand
908 // (log(1+I*x)-log(1-I*x))/(2*I)
910 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
911 if (!(I*arg_pt).info(info_flags::real))
912 throw do_taylor(); // Re(x) != 0
913 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
914 throw do_taylor(); // Re(x) == 0, but abs(x)<1
915 // care for the poles, using the defining formula for atan()...
916 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
917 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
918 if (!(options & series_options::suppress_branchcut)) {
920 // This is the branch cut: assemble the primitive series manually and
921 // then add the corresponding complex step function.
922 const symbol &s = ex_to<symbol>(rel.lhs());
923 const ex &point = rel.rhs();
925 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
926 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
928 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
930 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
931 epvector seq { expair(Order0correction, _ex0),
932 expair(Order(_ex1), order) };
933 return series(replarg - pseries(rel, std::move(seq)), rel, order);
938 static ex atan_conjugate(const ex & x)
940 // conjugate(atan(x))==atan(conjugate(x)) unless on the branch cuts which
941 // run along the imaginary axis outside the interval [-I, +I].
942 if (x.info(info_flags::real))
944 if (is_exactly_a<numeric>(x)) {
945 const numeric x_re = ex_to<numeric>(x.real_part());
946 const numeric x_im = ex_to<numeric>(x.imag_part());
947 if (!x_re.is_zero() ||
948 (x_im > *_num_1_p && x_im < *_num1_p))
949 return atan(x.conjugate());
951 return conjugate_function(atan(x)).hold();
954 REGISTER_FUNCTION(atan, eval_func(atan_eval).
955 evalf_func(atan_evalf).
956 derivative_func(atan_deriv).
957 series_func(atan_series).
958 conjugate_func(atan_conjugate).
959 latex_name("\\arctan"));
962 // inverse tangent (atan2(y,x))
965 static ex atan2_evalf(const ex &y, const ex &x)
967 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
968 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
970 return atan2(y, x).hold();
973 static ex atan2_eval(const ex & y, const ex & x)
981 // atan2(0, x), x real and positive -> 0
982 if (x.info(info_flags::positive))
985 // atan2(0, x), x real and negative -> Pi
986 if (x.info(info_flags::negative))
992 // atan2(y, 0), y real and positive -> Pi/2
993 if (y.info(info_flags::positive))
996 // atan2(y, 0), y real and negative -> -Pi/2
997 if (y.info(info_flags::negative))
1001 if (y.is_equal(x)) {
1003 // atan2(y, y), y real and positive -> Pi/4
1004 if (y.info(info_flags::positive))
1007 // atan2(y, y), y real and negative -> -3/4*Pi
1008 if (y.info(info_flags::negative))
1009 return numeric(-3, 4)*Pi;
1012 if (y.is_equal(-x)) {
1014 // atan2(y, -y), y real and positive -> 3*Pi/4
1015 if (y.info(info_flags::positive))
1016 return numeric(3, 4)*Pi;
1018 // atan2(y, -y), y real and negative -> -Pi/4
1019 if (y.info(info_flags::negative))
1023 // atan2(float, float) -> float
1024 if (is_a<numeric>(y) && !y.info(info_flags::crational) &&
1025 is_a<numeric>(x) && !x.info(info_flags::crational))
1026 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
1028 // atan2(real, real) -> atan(y/x) +/- Pi
1029 if (y.info(info_flags::real) && x.info(info_flags::real)) {
1030 if (x.info(info_flags::positive))
1033 if (x.info(info_flags::negative)) {
1034 if (y.info(info_flags::positive))
1035 return atan(y/x)+Pi;
1036 if (y.info(info_flags::negative))
1037 return atan(y/x)-Pi;
1041 return atan2(y, x).hold();
1044 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
1046 GINAC_ASSERT(deriv_param<2);
1048 if (deriv_param==0) {
1050 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1053 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1056 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
1057 evalf_func(atan2_evalf).
1058 derivative_func(atan2_deriv));
1061 // hyperbolic sine (trigonometric function)
1064 static ex sinh_evalf(const ex & x)
1066 if (is_exactly_a<numeric>(x))
1067 return sinh(ex_to<numeric>(x));
1069 return sinh(x).hold();
1072 static ex sinh_eval(const ex & x)
1074 if (x.info(info_flags::numeric)) {
1080 // sinh(float) -> float
1081 if (!x.info(info_flags::crational))
1082 return sinh(ex_to<numeric>(x));
1085 if (x.info(info_flags::negative))
1089 if ((x/Pi).info(info_flags::numeric) &&
1090 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
1093 if (is_exactly_a<function>(x)) {
1094 const ex &t = x.op(0);
1096 // sinh(asinh(x)) -> x
1097 if (is_ex_the_function(x, asinh))
1100 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
1101 if (is_ex_the_function(x, acosh))
1102 return sqrt(t-_ex1)*sqrt(t+_ex1);
1104 // sinh(atanh(x)) -> x/sqrt(1-x^2)
1105 if (is_ex_the_function(x, atanh))
1106 return t*power(_ex1-power(t,_ex2),_ex_1_2);
1109 return sinh(x).hold();
1112 static ex sinh_deriv(const ex & x, unsigned deriv_param)
1114 GINAC_ASSERT(deriv_param==0);
1116 // d/dx sinh(x) -> cosh(x)
1120 static ex sinh_real_part(const ex & x)
1122 return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1125 static ex sinh_imag_part(const ex & x)
1127 return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1130 static ex sinh_conjugate(const ex & x)
1132 // conjugate(sinh(x))==sinh(conjugate(x))
1133 return sinh(x.conjugate());
1136 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
1137 evalf_func(sinh_evalf).
1138 derivative_func(sinh_deriv).
1139 real_part_func(sinh_real_part).
1140 imag_part_func(sinh_imag_part).
1141 conjugate_func(sinh_conjugate).
1142 latex_name("\\sinh"));
1145 // hyperbolic cosine (trigonometric function)
1148 static ex cosh_evalf(const ex & x)
1150 if (is_exactly_a<numeric>(x))
1151 return cosh(ex_to<numeric>(x));
1153 return cosh(x).hold();
1156 static ex cosh_eval(const ex & x)
1158 if (x.info(info_flags::numeric)) {
1164 // cosh(float) -> float
1165 if (!x.info(info_flags::crational))
1166 return cosh(ex_to<numeric>(x));
1169 if (x.info(info_flags::negative))
1173 if ((x/Pi).info(info_flags::numeric) &&
1174 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
1177 if (is_exactly_a<function>(x)) {
1178 const ex &t = x.op(0);
1180 // cosh(acosh(x)) -> x
1181 if (is_ex_the_function(x, acosh))
1184 // cosh(asinh(x)) -> sqrt(1+x^2)
1185 if (is_ex_the_function(x, asinh))
1186 return sqrt(_ex1+power(t,_ex2));
1188 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1189 if (is_ex_the_function(x, atanh))
1190 return power(_ex1-power(t,_ex2),_ex_1_2);
1193 return cosh(x).hold();
1196 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1198 GINAC_ASSERT(deriv_param==0);
1200 // d/dx cosh(x) -> sinh(x)
1204 static ex cosh_real_part(const ex & x)
1206 return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1209 static ex cosh_imag_part(const ex & x)
1211 return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1214 static ex cosh_conjugate(const ex & x)
1216 // conjugate(cosh(x))==cosh(conjugate(x))
1217 return cosh(x.conjugate());
1220 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1221 evalf_func(cosh_evalf).
1222 derivative_func(cosh_deriv).
1223 real_part_func(cosh_real_part).
1224 imag_part_func(cosh_imag_part).
1225 conjugate_func(cosh_conjugate).
1226 latex_name("\\cosh"));
1229 // hyperbolic tangent (trigonometric function)
1232 static ex tanh_evalf(const ex & x)
1234 if (is_exactly_a<numeric>(x))
1235 return tanh(ex_to<numeric>(x));
1237 return tanh(x).hold();
1240 static ex tanh_eval(const ex & x)
1242 if (x.info(info_flags::numeric)) {
1248 // tanh(float) -> float
1249 if (!x.info(info_flags::crational))
1250 return tanh(ex_to<numeric>(x));
1253 if (x.info(info_flags::negative))
1257 if ((x/Pi).info(info_flags::numeric) &&
1258 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1261 if (is_exactly_a<function>(x)) {
1262 const ex &t = x.op(0);
1264 // tanh(atanh(x)) -> x
1265 if (is_ex_the_function(x, atanh))
1268 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1269 if (is_ex_the_function(x, asinh))
1270 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1272 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1273 if (is_ex_the_function(x, acosh))
1274 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1277 return tanh(x).hold();
1280 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1282 GINAC_ASSERT(deriv_param==0);
1284 // d/dx tanh(x) -> 1-tanh(x)^2
1285 return _ex1-power(tanh(x),_ex2);
1288 static ex tanh_series(const ex &x,
1289 const relational &rel,
1293 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1295 // Taylor series where there is no pole falls back to tanh_deriv.
1296 // On a pole simply expand sinh(x)/cosh(x).
1297 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1298 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1299 throw do_taylor(); // caught by function::series()
1300 // if we got here we have to care for a simple pole
1301 return (sinh(x)/cosh(x)).series(rel, order, options);
1304 static ex tanh_real_part(const ex & x)
1306 ex a = GiNaC::real_part(x);
1307 ex b = GiNaC::imag_part(x);
1308 return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1311 static ex tanh_imag_part(const ex & x)
1313 ex a = GiNaC::real_part(x);
1314 ex b = GiNaC::imag_part(x);
1315 return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1318 static ex tanh_conjugate(const ex & x)
1320 // conjugate(tanh(x))==tanh(conjugate(x))
1321 return tanh(x.conjugate());
1324 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1325 evalf_func(tanh_evalf).
1326 derivative_func(tanh_deriv).
1327 series_func(tanh_series).
1328 real_part_func(tanh_real_part).
1329 imag_part_func(tanh_imag_part).
1330 conjugate_func(tanh_conjugate).
1331 latex_name("\\tanh"));
1334 // inverse hyperbolic sine (trigonometric function)
1337 static ex asinh_evalf(const ex & x)
1339 if (is_exactly_a<numeric>(x))
1340 return asinh(ex_to<numeric>(x));
1342 return asinh(x).hold();
1345 static ex asinh_eval(const ex & x)
1347 if (x.info(info_flags::numeric)) {
1353 // asinh(float) -> float
1354 if (!x.info(info_flags::crational))
1355 return asinh(ex_to<numeric>(x));
1358 if (x.info(info_flags::negative))
1362 return asinh(x).hold();
1365 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1367 GINAC_ASSERT(deriv_param==0);
1369 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1370 return power(_ex1+power(x,_ex2),_ex_1_2);
1373 static ex asinh_conjugate(const ex & x)
1375 // conjugate(asinh(x))==asinh(conjugate(x)) unless on the branch cuts which
1376 // run along the imaginary axis outside the interval [-I, +I].
1377 if (x.info(info_flags::real))
1379 if (is_exactly_a<numeric>(x)) {
1380 const numeric x_re = ex_to<numeric>(x.real_part());
1381 const numeric x_im = ex_to<numeric>(x.imag_part());
1382 if (!x_re.is_zero() ||
1383 (x_im > *_num_1_p && x_im < *_num1_p))
1384 return asinh(x.conjugate());
1386 return conjugate_function(asinh(x)).hold();
1389 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1390 evalf_func(asinh_evalf).
1391 derivative_func(asinh_deriv).
1392 conjugate_func(asinh_conjugate));
1395 // inverse hyperbolic cosine (trigonometric function)
1398 static ex acosh_evalf(const ex & x)
1400 if (is_exactly_a<numeric>(x))
1401 return acosh(ex_to<numeric>(x));
1403 return acosh(x).hold();
1406 static ex acosh_eval(const ex & x)
1408 if (x.info(info_flags::numeric)) {
1410 // acosh(0) -> Pi*I/2
1412 return Pi*I*numeric(1,2);
1415 if (x.is_equal(_ex1))
1418 // acosh(-1) -> Pi*I
1419 if (x.is_equal(_ex_1))
1422 // acosh(float) -> float
1423 if (!x.info(info_flags::crational))
1424 return acosh(ex_to<numeric>(x));
1426 // acosh(-x) -> Pi*I-acosh(x)
1427 if (x.info(info_flags::negative))
1428 return Pi*I-acosh(-x);
1431 return acosh(x).hold();
1434 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1436 GINAC_ASSERT(deriv_param==0);
1438 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1439 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1442 static ex acosh_conjugate(const ex & x)
1444 // conjugate(acosh(x))==acosh(conjugate(x)) unless on the branch cut
1445 // which runs along the real axis from +1 to -inf.
1446 if (is_exactly_a<numeric>(x) &&
1447 (!x.imag_part().is_zero() || x > *_num1_p)) {
1448 return acosh(x.conjugate());
1450 return conjugate_function(acosh(x)).hold();
1453 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1454 evalf_func(acosh_evalf).
1455 derivative_func(acosh_deriv).
1456 conjugate_func(acosh_conjugate));
1459 // inverse hyperbolic tangent (trigonometric function)
1462 static ex atanh_evalf(const ex & x)
1464 if (is_exactly_a<numeric>(x))
1465 return atanh(ex_to<numeric>(x));
1467 return atanh(x).hold();
1470 static ex atanh_eval(const ex & x)
1472 if (x.info(info_flags::numeric)) {
1478 // atanh({+|-}1) -> throw
1479 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1480 throw (pole_error("atanh_eval(): logarithmic pole",0));
1482 // atanh(float) -> float
1483 if (!x.info(info_flags::crational))
1484 return atanh(ex_to<numeric>(x));
1487 if (x.info(info_flags::negative))
1491 return atanh(x).hold();
1494 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1496 GINAC_ASSERT(deriv_param==0);
1498 // d/dx atanh(x) -> 1/(1-x^2)
1499 return power(_ex1-power(x,_ex2),_ex_1);
1502 static ex atanh_series(const ex &arg,
1503 const relational &rel,
1507 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1509 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1510 // There are two branch cuts, one runnig from 1 up the real axis and one
1511 // one running from -1 down the real axis. The points 1 and -1 are poles
1512 // On the branch cuts and the poles series expand
1513 // (log(1+x)-log(1-x))/2
1515 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1516 if (!(arg_pt).info(info_flags::real))
1517 throw do_taylor(); // Im(x) != 0
1518 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1519 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1520 // care for the poles, using the defining formula for atanh()...
1521 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1522 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1523 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1524 if (!(options & series_options::suppress_branchcut)) {
1526 // This is the branch cut: assemble the primitive series manually and
1527 // then add the corresponding complex step function.
1528 const symbol &s = ex_to<symbol>(rel.lhs());
1529 const ex &point = rel.rhs();
1531 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1532 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1534 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1536 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1537 epvector seq { expair(Order0correction, _ex0),
1538 expair(Order(_ex1), order) };
1539 return series(replarg - pseries(rel, std::move(seq)), rel, order);
1544 static ex atanh_conjugate(const ex & x)
1546 // conjugate(atanh(x))==atanh(conjugate(x)) unless on the branch cuts which
1547 // run along the real axis outside the interval [-1, +1].
1548 if (is_exactly_a<numeric>(x) &&
1549 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
1550 return atanh(x.conjugate());
1552 return conjugate_function(atanh(x)).hold();
1555 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1556 evalf_func(atanh_evalf).
1557 derivative_func(atanh_deriv).
1558 series_func(atanh_series).
1559 conjugate_func(atanh_conjugate));
1562 } // namespace GiNaC