1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2021 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 static ex exp_power(const ex & x, const ex & a)
134 * The power law (e^x)^a=e^(x*a) is used in two cases:
135 * a) a is an integer and x may be complex;
136 * b) both x and a are reals.
137 * Negative a is excluded to keep automatic simplifications like exp(x)/exp(x)=1.
139 if (a.info(info_flags::nonnegative)
140 && (a.info(info_flags::integer) || (x.info(info_flags::real) && a.info(info_flags::real))))
142 else if (a.info(info_flags::negative)
143 && (a.info(info_flags::integer) || (x.info(info_flags::real) && a.info(info_flags::real))))
144 return power(exp(-x*a), _ex_1).hold();
146 return power(exp(x), a).hold();
149 REGISTER_FUNCTION(exp, eval_func(exp_eval).
150 evalf_func(exp_evalf).
151 expand_func(exp_expand).
152 derivative_func(exp_deriv).
153 real_part_func(exp_real_part).
154 imag_part_func(exp_imag_part).
155 conjugate_func(exp_conjugate).
156 power_func(exp_power).
157 latex_name("\\exp"));
163 static ex log_evalf(const ex & x)
165 if (is_exactly_a<numeric>(x))
166 return log(ex_to<numeric>(x));
168 return log(x).hold();
171 static ex log_eval(const ex & x)
173 if (x.info(info_flags::numeric)) {
174 if (x.is_zero()) // log(0) -> infinity
175 throw(pole_error("log_eval(): log(0)",0));
176 if (x.info(info_flags::rational) && x.info(info_flags::negative))
177 return (log(-x)+I*Pi);
178 if (x.is_equal(_ex1)) // log(1) -> 0
180 if (x.is_equal(I)) // log(I) -> Pi*I/2
181 return (Pi*I*_ex1_2);
182 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
183 return (Pi*I*_ex_1_2);
185 // log(float) -> float
186 if (!x.info(info_flags::crational))
187 return log(ex_to<numeric>(x));
190 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
191 if (is_ex_the_function(x, exp)) {
192 const ex &t = x.op(0);
193 if (t.info(info_flags::real))
197 return log(x).hold();
200 static ex log_deriv(const ex & x, unsigned deriv_param)
202 GINAC_ASSERT(deriv_param==0);
204 // d/dx log(x) -> 1/x
205 return power(x, _ex_1);
208 static ex log_series(const ex &arg,
209 const relational &rel,
213 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
215 bool must_expand_arg = false;
216 // maybe substitution of rel into arg fails because of a pole
218 arg_pt = arg.subs(rel, subs_options::no_pattern);
219 } catch (pole_error &) {
220 must_expand_arg = true;
222 // or we are at the branch point anyways
223 if (arg_pt.is_zero())
224 must_expand_arg = true;
226 if (arg.diff(ex_to<symbol>(rel.lhs())).is_zero()) {
230 if (must_expand_arg) {
232 // This is the branch point: Series expand the argument first, then
233 // trivially factorize it to isolate that part which has constant
234 // leading coefficient in this fashion:
235 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
236 // Return a plain n*log(x) for the x^n part and series expand the
237 // other part. Add them together and reexpand again in order to have
238 // one unnested pseries object. All this also works for negative n.
239 pseries argser; // series expansion of log's argument
240 unsigned extra_ord = 0; // extra expansion order
242 // oops, the argument expanded to a pure Order(x^something)...
243 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
245 } while (!argser.is_terminating() && argser.nops()==1);
247 const symbol &s = ex_to<symbol>(rel.lhs());
248 const ex &point = rel.rhs();
249 const int n = argser.ldegree(s);
251 // construct what we carelessly called the n*log(x) term above
252 const ex coeff = argser.coeff(s, n);
253 // expand the log, but only if coeff is real and > 0, since otherwise
254 // it would make the branch cut run into the wrong direction
255 if (coeff.info(info_flags::positive))
256 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
258 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
260 if (!argser.is_terminating() || argser.nops()!=1) {
261 // in this case n more (or less) terms are needed
262 // (sadly, to generate them, we have to start from the beginning)
263 if (n == 0 && coeff == 1) {
264 ex rest = pseries(rel, epvector{expair(-1, _ex0), expair(Order(_ex1), order)}).add_series(argser);
265 ex acc = dynallocate<pseries>(rel, epvector());
266 for (int i = order-1; i>0; --i) {
267 epvector cterm { expair(i%2 ? _ex1/i : _ex_1/i, _ex0) };
268 acc = pseries(rel, std::move(cterm)).add_series(ex_to<pseries>(acc));
269 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
273 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
274 return pseries(rel, std::move(seq)).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
275 } else // it was a monomial
276 return pseries(rel, std::move(seq));
278 if (!(options & series_options::suppress_branchcut) &&
279 arg_pt.info(info_flags::negative)) {
281 // This is the branch cut: assemble the primitive series manually and
282 // then add the corresponding complex step function.
283 const symbol &s = ex_to<symbol>(rel.lhs());
284 const ex &point = rel.rhs();
286 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
290 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
292 seq.push_back(expair(Order(_ex1), order));
293 return series(replarg - I*Pi + pseries(rel, std::move(seq)), rel, order);
295 throw do_taylor(); // caught by function::series()
298 static ex log_real_part(const ex & x)
300 if (x.info(info_flags::nonnegative))
301 return log(x).hold();
305 static ex log_imag_part(const ex & x)
307 if (x.info(info_flags::nonnegative))
309 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
312 static ex log_expand(const ex & arg, unsigned options)
314 if ((options & expand_options::expand_transcendental)
315 && is_exactly_a<mul>(arg) && !arg.info(info_flags::indefinite)) {
318 sumseq.reserve(arg.nops());
319 prodseq.reserve(arg.nops());
322 // searching for positive/negative factors
323 for (const_iterator i = arg.begin(); i != arg.end(); ++i) {
325 if (options & expand_options::expand_function_args)
326 e=i->expand(options);
329 if (e.info(info_flags::positive))
330 sumseq.push_back(log(e));
331 else if (e.info(info_flags::negative)) {
332 sumseq.push_back(log(-e));
335 prodseq.push_back(e);
338 if (sumseq.size() > 0) {
340 if (options & expand_options::expand_function_args)
341 newarg=((possign?_ex1:_ex_1)*mul(prodseq)).expand(options);
343 newarg=(possign?_ex1:_ex_1)*mul(prodseq);
344 ex_to<basic>(newarg).setflag(status_flags::purely_indefinite);
346 return add(sumseq)+log(newarg);
348 if (!(options & expand_options::expand_function_args))
349 ex_to<basic>(arg).setflag(status_flags::purely_indefinite);
353 if (options & expand_options::expand_function_args)
354 return log(arg.expand(options)).hold();
356 return log(arg).hold();
359 static ex log_conjugate(const ex & x)
361 // conjugate(log(x))==log(conjugate(x)) unless on the branch cut which
362 // runs along the negative real axis.
363 if (x.info(info_flags::positive)) {
366 if (is_exactly_a<numeric>(x) &&
367 !x.imag_part().is_zero()) {
368 return log(x.conjugate());
370 return conjugate_function(log(x)).hold();
373 REGISTER_FUNCTION(log, eval_func(log_eval).
374 evalf_func(log_evalf).
375 expand_func(log_expand).
376 derivative_func(log_deriv).
377 series_func(log_series).
378 real_part_func(log_real_part).
379 imag_part_func(log_imag_part).
380 conjugate_func(log_conjugate).
384 // sine (trigonometric function)
387 static ex sin_evalf(const ex & x)
389 if (is_exactly_a<numeric>(x))
390 return sin(ex_to<numeric>(x));
392 return sin(x).hold();
395 static ex sin_eval(const ex & x)
397 // sin(n/d*Pi) -> { all known non-nested radicals }
398 const ex SixtyExOverPi = _ex60*x/Pi;
400 if (SixtyExOverPi.info(info_flags::integer)) {
401 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
403 // wrap to interval [0, Pi)
408 // wrap to interval [0, Pi/2)
411 if (z.is_equal(*_num0_p)) // sin(0) -> 0
413 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
414 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
415 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
416 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
417 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
419 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
420 return sign*_ex1_2*sqrt(_ex2);
421 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
422 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
423 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
424 return sign*_ex1_2*sqrt(_ex3);
425 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
426 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
427 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
431 if (is_exactly_a<function>(x)) {
432 const ex &t = x.op(0);
435 if (is_ex_the_function(x, asin))
438 // sin(acos(x)) -> sqrt(1-x^2)
439 if (is_ex_the_function(x, acos))
440 return sqrt(_ex1-power(t,_ex2));
442 // sin(atan(x)) -> x/sqrt(1+x^2)
443 if (is_ex_the_function(x, atan))
444 return t*power(_ex1+power(t,_ex2),_ex_1_2);
447 // sin(float) -> float
448 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
449 return sin(ex_to<numeric>(x));
452 if (x.info(info_flags::negative))
455 return sin(x).hold();
458 static ex sin_deriv(const ex & x, unsigned deriv_param)
460 GINAC_ASSERT(deriv_param==0);
462 // d/dx sin(x) -> cos(x)
466 static ex sin_real_part(const ex & x)
468 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
471 static ex sin_imag_part(const ex & x)
473 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
476 static ex sin_conjugate(const ex & x)
478 // conjugate(sin(x))==sin(conjugate(x))
479 return sin(x.conjugate());
482 REGISTER_FUNCTION(sin, eval_func(sin_eval).
483 evalf_func(sin_evalf).
484 derivative_func(sin_deriv).
485 real_part_func(sin_real_part).
486 imag_part_func(sin_imag_part).
487 conjugate_func(sin_conjugate).
488 latex_name("\\sin"));
491 // cosine (trigonometric function)
494 static ex cos_evalf(const ex & x)
496 if (is_exactly_a<numeric>(x))
497 return cos(ex_to<numeric>(x));
499 return cos(x).hold();
502 static ex cos_eval(const ex & x)
504 // cos(n/d*Pi) -> { all known non-nested radicals }
505 const ex SixtyExOverPi = _ex60*x/Pi;
507 if (SixtyExOverPi.info(info_flags::integer)) {
508 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
510 // wrap to interval [0, Pi)
514 // wrap to interval [0, Pi/2)
518 if (z.is_equal(*_num0_p)) // cos(0) -> 1
520 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
521 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
522 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
523 return sign*_ex1_2*sqrt(_ex3);
524 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
525 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
526 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
527 return sign*_ex1_2*sqrt(_ex2);
528 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
530 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
531 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
532 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
533 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
534 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
538 if (is_exactly_a<function>(x)) {
539 const ex &t = x.op(0);
542 if (is_ex_the_function(x, acos))
545 // cos(asin(x)) -> sqrt(1-x^2)
546 if (is_ex_the_function(x, asin))
547 return sqrt(_ex1-power(t,_ex2));
549 // cos(atan(x)) -> 1/sqrt(1+x^2)
550 if (is_ex_the_function(x, atan))
551 return power(_ex1+power(t,_ex2),_ex_1_2);
554 // cos(float) -> float
555 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
556 return cos(ex_to<numeric>(x));
559 if (x.info(info_flags::negative))
562 return cos(x).hold();
565 static ex cos_deriv(const ex & x, unsigned deriv_param)
567 GINAC_ASSERT(deriv_param==0);
569 // d/dx cos(x) -> -sin(x)
573 static ex cos_real_part(const ex & x)
575 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
578 static ex cos_imag_part(const ex & x)
580 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
583 static ex cos_conjugate(const ex & x)
585 // conjugate(cos(x))==cos(conjugate(x))
586 return cos(x.conjugate());
589 REGISTER_FUNCTION(cos, eval_func(cos_eval).
590 evalf_func(cos_evalf).
591 derivative_func(cos_deriv).
592 real_part_func(cos_real_part).
593 imag_part_func(cos_imag_part).
594 conjugate_func(cos_conjugate).
595 latex_name("\\cos"));
598 // tangent (trigonometric function)
601 static ex tan_evalf(const ex & x)
603 if (is_exactly_a<numeric>(x))
604 return tan(ex_to<numeric>(x));
606 return tan(x).hold();
609 static ex tan_eval(const ex & x)
611 // tan(n/d*Pi) -> { all known non-nested radicals }
612 const ex SixtyExOverPi = _ex60*x/Pi;
614 if (SixtyExOverPi.info(info_flags::integer)) {
615 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
617 // wrap to interval [0, Pi)
621 // wrap to interval [0, Pi/2)
625 if (z.is_equal(*_num0_p)) // tan(0) -> 0
627 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
628 return sign*(_ex2-sqrt(_ex3));
629 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
630 return sign*_ex1_3*sqrt(_ex3);
631 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
633 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
634 return sign*sqrt(_ex3);
635 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
636 return sign*(sqrt(_ex3)+_ex2);
637 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
638 throw (pole_error("tan_eval(): simple pole",1));
641 if (is_exactly_a<function>(x)) {
642 const ex &t = x.op(0);
645 if (is_ex_the_function(x, atan))
648 // tan(asin(x)) -> x/sqrt(1+x^2)
649 if (is_ex_the_function(x, asin))
650 return t*power(_ex1-power(t,_ex2),_ex_1_2);
652 // tan(acos(x)) -> sqrt(1-x^2)/x
653 if (is_ex_the_function(x, acos))
654 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
657 // tan(float) -> float
658 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
659 return tan(ex_to<numeric>(x));
663 if (x.info(info_flags::negative))
666 return tan(x).hold();
669 static ex tan_deriv(const ex & x, unsigned deriv_param)
671 GINAC_ASSERT(deriv_param==0);
673 // d/dx tan(x) -> 1+tan(x)^2;
674 return (_ex1+power(tan(x),_ex2));
677 static ex tan_real_part(const ex & x)
679 ex a = GiNaC::real_part(x);
680 ex b = GiNaC::imag_part(x);
681 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
684 static ex tan_imag_part(const ex & x)
686 ex a = GiNaC::real_part(x);
687 ex b = GiNaC::imag_part(x);
688 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
691 static ex tan_series(const ex &x,
692 const relational &rel,
696 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
698 // Taylor series where there is no pole falls back to tan_deriv.
699 // On a pole simply expand sin(x)/cos(x).
700 const ex x_pt = x.subs(rel, subs_options::no_pattern);
701 if (!(2*x_pt/Pi).info(info_flags::odd))
702 throw do_taylor(); // caught by function::series()
703 // if we got here we have to care for a simple pole
704 return (sin(x)/cos(x)).series(rel, order, options);
707 static ex tan_conjugate(const ex & x)
709 // conjugate(tan(x))==tan(conjugate(x))
710 return tan(x.conjugate());
713 REGISTER_FUNCTION(tan, eval_func(tan_eval).
714 evalf_func(tan_evalf).
715 derivative_func(tan_deriv).
716 series_func(tan_series).
717 real_part_func(tan_real_part).
718 imag_part_func(tan_imag_part).
719 conjugate_func(tan_conjugate).
720 latex_name("\\tan"));
723 // inverse sine (arc sine)
726 static ex asin_evalf(const ex & x)
728 if (is_exactly_a<numeric>(x))
729 return asin(ex_to<numeric>(x));
731 return asin(x).hold();
734 static ex asin_eval(const ex & x)
736 if (x.info(info_flags::numeric)) {
743 if (x.is_equal(_ex1_2))
744 return numeric(1,6)*Pi;
747 if (x.is_equal(_ex1))
750 // asin(-1/2) -> -Pi/6
751 if (x.is_equal(_ex_1_2))
752 return numeric(-1,6)*Pi;
755 if (x.is_equal(_ex_1))
758 // asin(float) -> float
759 if (!x.info(info_flags::crational))
760 return asin(ex_to<numeric>(x));
763 if (x.info(info_flags::negative))
767 return asin(x).hold();
770 static ex asin_deriv(const ex & x, unsigned deriv_param)
772 GINAC_ASSERT(deriv_param==0);
774 // d/dx asin(x) -> 1/sqrt(1-x^2)
775 return power(1-power(x,_ex2),_ex_1_2);
778 static ex asin_conjugate(const ex & x)
780 // conjugate(asin(x))==asin(conjugate(x)) unless on the branch cuts which
781 // run along the real axis outside the interval [-1, +1].
782 if (is_exactly_a<numeric>(x) &&
783 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
784 return asin(x.conjugate());
786 return conjugate_function(asin(x)).hold();
789 REGISTER_FUNCTION(asin, eval_func(asin_eval).
790 evalf_func(asin_evalf).
791 derivative_func(asin_deriv).
792 conjugate_func(asin_conjugate).
793 latex_name("\\arcsin"));
796 // inverse cosine (arc cosine)
799 static ex acos_evalf(const ex & x)
801 if (is_exactly_a<numeric>(x))
802 return acos(ex_to<numeric>(x));
804 return acos(x).hold();
807 static ex acos_eval(const ex & x)
809 if (x.info(info_flags::numeric)) {
812 if (x.is_equal(_ex1))
816 if (x.is_equal(_ex1_2))
823 // acos(-1/2) -> 2/3*Pi
824 if (x.is_equal(_ex_1_2))
825 return numeric(2,3)*Pi;
828 if (x.is_equal(_ex_1))
831 // acos(float) -> float
832 if (!x.info(info_flags::crational))
833 return acos(ex_to<numeric>(x));
835 // acos(-x) -> Pi-acos(x)
836 if (x.info(info_flags::negative))
840 return acos(x).hold();
843 static ex acos_deriv(const ex & x, unsigned deriv_param)
845 GINAC_ASSERT(deriv_param==0);
847 // d/dx acos(x) -> -1/sqrt(1-x^2)
848 return -power(1-power(x,_ex2),_ex_1_2);
851 static ex acos_conjugate(const ex & x)
853 // conjugate(acos(x))==acos(conjugate(x)) unless on the branch cuts which
854 // run along the real axis outside the interval [-1, +1].
855 if (is_exactly_a<numeric>(x) &&
856 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
857 return acos(x.conjugate());
859 return conjugate_function(acos(x)).hold();
862 REGISTER_FUNCTION(acos, eval_func(acos_eval).
863 evalf_func(acos_evalf).
864 derivative_func(acos_deriv).
865 conjugate_func(acos_conjugate).
866 latex_name("\\arccos"));
869 // inverse tangent (arc tangent)
872 static ex atan_evalf(const ex & x)
874 if (is_exactly_a<numeric>(x))
875 return atan(ex_to<numeric>(x));
877 return atan(x).hold();
880 static ex atan_eval(const ex & x)
882 if (x.info(info_flags::numeric)) {
889 if (x.is_equal(_ex1))
893 if (x.is_equal(_ex_1))
896 if (x.is_equal(I) || x.is_equal(-I))
897 throw (pole_error("atan_eval(): logarithmic pole",0));
899 // atan(float) -> float
900 if (!x.info(info_flags::crational))
901 return atan(ex_to<numeric>(x));
904 if (x.info(info_flags::negative))
908 return atan(x).hold();
911 static ex atan_deriv(const ex & x, unsigned deriv_param)
913 GINAC_ASSERT(deriv_param==0);
915 // d/dx atan(x) -> 1/(1+x^2)
916 return power(_ex1+power(x,_ex2), _ex_1);
919 static ex atan_series(const ex &arg,
920 const relational &rel,
924 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
926 // Taylor series where there is no pole or cut falls back to atan_deriv.
927 // There are two branch cuts, one runnig from I up the imaginary axis and
928 // one running from -I down the imaginary axis. The points I and -I are
930 // On the branch cuts and the poles series expand
931 // (log(1+I*x)-log(1-I*x))/(2*I)
933 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
934 if (!(I*arg_pt).info(info_flags::real))
935 throw do_taylor(); // Re(x) != 0
936 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
937 throw do_taylor(); // Re(x) == 0, but abs(x)<1
938 // care for the poles, using the defining formula for atan()...
939 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
940 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
941 if (!(options & series_options::suppress_branchcut)) {
943 // This is the branch cut: assemble the primitive series manually and
944 // then add the corresponding complex step function.
945 const symbol &s = ex_to<symbol>(rel.lhs());
946 const ex &point = rel.rhs();
948 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
949 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
951 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
953 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
957 seq.push_back(expair(Order0correction, _ex0));
959 seq.push_back(expair(Order(_ex1), order));
960 return series(replarg - pseries(rel, std::move(seq)), rel, order);
965 static ex atan_conjugate(const ex & x)
967 // conjugate(atan(x))==atan(conjugate(x)) unless on the branch cuts which
968 // run along the imaginary axis outside the interval [-I, +I].
969 if (x.info(info_flags::real))
971 if (is_exactly_a<numeric>(x)) {
972 const numeric x_re = ex_to<numeric>(x.real_part());
973 const numeric x_im = ex_to<numeric>(x.imag_part());
974 if (!x_re.is_zero() ||
975 (x_im > *_num_1_p && x_im < *_num1_p))
976 return atan(x.conjugate());
978 return conjugate_function(atan(x)).hold();
981 REGISTER_FUNCTION(atan, eval_func(atan_eval).
982 evalf_func(atan_evalf).
983 derivative_func(atan_deriv).
984 series_func(atan_series).
985 conjugate_func(atan_conjugate).
986 latex_name("\\arctan"));
989 // inverse tangent (atan2(y,x))
992 static ex atan2_evalf(const ex &y, const ex &x)
994 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
995 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
997 return atan2(y, x).hold();
1000 static ex atan2_eval(const ex & y, const ex & x)
1008 // atan2(0, x), x real and positive -> 0
1009 if (x.info(info_flags::positive))
1012 // atan2(0, x), x real and negative -> Pi
1013 if (x.info(info_flags::negative))
1019 // atan2(y, 0), y real and positive -> Pi/2
1020 if (y.info(info_flags::positive))
1023 // atan2(y, 0), y real and negative -> -Pi/2
1024 if (y.info(info_flags::negative))
1028 if (y.is_equal(x)) {
1030 // atan2(y, y), y real and positive -> Pi/4
1031 if (y.info(info_flags::positive))
1034 // atan2(y, y), y real and negative -> -3/4*Pi
1035 if (y.info(info_flags::negative))
1036 return numeric(-3, 4)*Pi;
1039 if (y.is_equal(-x)) {
1041 // atan2(y, -y), y real and positive -> 3*Pi/4
1042 if (y.info(info_flags::positive))
1043 return numeric(3, 4)*Pi;
1045 // atan2(y, -y), y real and negative -> -Pi/4
1046 if (y.info(info_flags::negative))
1050 // atan2(float, float) -> float
1051 if (is_a<numeric>(y) && !y.info(info_flags::crational) &&
1052 is_a<numeric>(x) && !x.info(info_flags::crational))
1053 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
1055 // atan2(real, real) -> atan(y/x) +/- Pi
1056 if (y.info(info_flags::real) && x.info(info_flags::real)) {
1057 if (x.info(info_flags::positive))
1060 if (x.info(info_flags::negative)) {
1061 if (y.info(info_flags::positive))
1062 return atan(y/x)+Pi;
1063 if (y.info(info_flags::negative))
1064 return atan(y/x)-Pi;
1068 return atan2(y, x).hold();
1071 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
1073 GINAC_ASSERT(deriv_param<2);
1075 if (deriv_param==0) {
1077 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1080 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1083 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
1084 evalf_func(atan2_evalf).
1085 derivative_func(atan2_deriv));
1088 // hyperbolic sine (trigonometric function)
1091 static ex sinh_evalf(const ex & x)
1093 if (is_exactly_a<numeric>(x))
1094 return sinh(ex_to<numeric>(x));
1096 return sinh(x).hold();
1099 static ex sinh_eval(const ex & x)
1101 if (x.info(info_flags::numeric)) {
1107 // sinh(float) -> float
1108 if (!x.info(info_flags::crational))
1109 return sinh(ex_to<numeric>(x));
1112 if (x.info(info_flags::negative))
1116 if ((x/Pi).info(info_flags::numeric) &&
1117 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
1120 if (is_exactly_a<function>(x)) {
1121 const ex &t = x.op(0);
1123 // sinh(asinh(x)) -> x
1124 if (is_ex_the_function(x, asinh))
1127 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
1128 if (is_ex_the_function(x, acosh))
1129 return sqrt(t-_ex1)*sqrt(t+_ex1);
1131 // sinh(atanh(x)) -> x/sqrt(1-x^2)
1132 if (is_ex_the_function(x, atanh))
1133 return t*power(_ex1-power(t,_ex2),_ex_1_2);
1136 return sinh(x).hold();
1139 static ex sinh_deriv(const ex & x, unsigned deriv_param)
1141 GINAC_ASSERT(deriv_param==0);
1143 // d/dx sinh(x) -> cosh(x)
1147 static ex sinh_real_part(const ex & x)
1149 return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1152 static ex sinh_imag_part(const ex & x)
1154 return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1157 static ex sinh_conjugate(const ex & x)
1159 // conjugate(sinh(x))==sinh(conjugate(x))
1160 return sinh(x.conjugate());
1163 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
1164 evalf_func(sinh_evalf).
1165 derivative_func(sinh_deriv).
1166 real_part_func(sinh_real_part).
1167 imag_part_func(sinh_imag_part).
1168 conjugate_func(sinh_conjugate).
1169 latex_name("\\sinh"));
1172 // hyperbolic cosine (trigonometric function)
1175 static ex cosh_evalf(const ex & x)
1177 if (is_exactly_a<numeric>(x))
1178 return cosh(ex_to<numeric>(x));
1180 return cosh(x).hold();
1183 static ex cosh_eval(const ex & x)
1185 if (x.info(info_flags::numeric)) {
1191 // cosh(float) -> float
1192 if (!x.info(info_flags::crational))
1193 return cosh(ex_to<numeric>(x));
1196 if (x.info(info_flags::negative))
1200 if ((x/Pi).info(info_flags::numeric) &&
1201 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
1204 if (is_exactly_a<function>(x)) {
1205 const ex &t = x.op(0);
1207 // cosh(acosh(x)) -> x
1208 if (is_ex_the_function(x, acosh))
1211 // cosh(asinh(x)) -> sqrt(1+x^2)
1212 if (is_ex_the_function(x, asinh))
1213 return sqrt(_ex1+power(t,_ex2));
1215 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1216 if (is_ex_the_function(x, atanh))
1217 return power(_ex1-power(t,_ex2),_ex_1_2);
1220 return cosh(x).hold();
1223 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1225 GINAC_ASSERT(deriv_param==0);
1227 // d/dx cosh(x) -> sinh(x)
1231 static ex cosh_real_part(const ex & x)
1233 return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1236 static ex cosh_imag_part(const ex & x)
1238 return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1241 static ex cosh_conjugate(const ex & x)
1243 // conjugate(cosh(x))==cosh(conjugate(x))
1244 return cosh(x.conjugate());
1247 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1248 evalf_func(cosh_evalf).
1249 derivative_func(cosh_deriv).
1250 real_part_func(cosh_real_part).
1251 imag_part_func(cosh_imag_part).
1252 conjugate_func(cosh_conjugate).
1253 latex_name("\\cosh"));
1256 // hyperbolic tangent (trigonometric function)
1259 static ex tanh_evalf(const ex & x)
1261 if (is_exactly_a<numeric>(x))
1262 return tanh(ex_to<numeric>(x));
1264 return tanh(x).hold();
1267 static ex tanh_eval(const ex & x)
1269 if (x.info(info_flags::numeric)) {
1275 // tanh(float) -> float
1276 if (!x.info(info_flags::crational))
1277 return tanh(ex_to<numeric>(x));
1280 if (x.info(info_flags::negative))
1284 if ((x/Pi).info(info_flags::numeric) &&
1285 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1288 if (is_exactly_a<function>(x)) {
1289 const ex &t = x.op(0);
1291 // tanh(atanh(x)) -> x
1292 if (is_ex_the_function(x, atanh))
1295 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1296 if (is_ex_the_function(x, asinh))
1297 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1299 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1300 if (is_ex_the_function(x, acosh))
1301 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1304 return tanh(x).hold();
1307 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1309 GINAC_ASSERT(deriv_param==0);
1311 // d/dx tanh(x) -> 1-tanh(x)^2
1312 return _ex1-power(tanh(x),_ex2);
1315 static ex tanh_series(const ex &x,
1316 const relational &rel,
1320 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1322 // Taylor series where there is no pole falls back to tanh_deriv.
1323 // On a pole simply expand sinh(x)/cosh(x).
1324 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1325 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1326 throw do_taylor(); // caught by function::series()
1327 // if we got here we have to care for a simple pole
1328 return (sinh(x)/cosh(x)).series(rel, order, options);
1331 static ex tanh_real_part(const ex & x)
1333 ex a = GiNaC::real_part(x);
1334 ex b = GiNaC::imag_part(x);
1335 return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1338 static ex tanh_imag_part(const ex & x)
1340 ex a = GiNaC::real_part(x);
1341 ex b = GiNaC::imag_part(x);
1342 return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1345 static ex tanh_conjugate(const ex & x)
1347 // conjugate(tanh(x))==tanh(conjugate(x))
1348 return tanh(x.conjugate());
1351 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1352 evalf_func(tanh_evalf).
1353 derivative_func(tanh_deriv).
1354 series_func(tanh_series).
1355 real_part_func(tanh_real_part).
1356 imag_part_func(tanh_imag_part).
1357 conjugate_func(tanh_conjugate).
1358 latex_name("\\tanh"));
1361 // inverse hyperbolic sine (trigonometric function)
1364 static ex asinh_evalf(const ex & x)
1366 if (is_exactly_a<numeric>(x))
1367 return asinh(ex_to<numeric>(x));
1369 return asinh(x).hold();
1372 static ex asinh_eval(const ex & x)
1374 if (x.info(info_flags::numeric)) {
1380 // asinh(float) -> float
1381 if (!x.info(info_flags::crational))
1382 return asinh(ex_to<numeric>(x));
1385 if (x.info(info_flags::negative))
1389 return asinh(x).hold();
1392 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1394 GINAC_ASSERT(deriv_param==0);
1396 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1397 return power(_ex1+power(x,_ex2),_ex_1_2);
1400 static ex asinh_conjugate(const ex & x)
1402 // conjugate(asinh(x))==asinh(conjugate(x)) unless on the branch cuts which
1403 // run along the imaginary axis outside the interval [-I, +I].
1404 if (x.info(info_flags::real))
1406 if (is_exactly_a<numeric>(x)) {
1407 const numeric x_re = ex_to<numeric>(x.real_part());
1408 const numeric x_im = ex_to<numeric>(x.imag_part());
1409 if (!x_re.is_zero() ||
1410 (x_im > *_num_1_p && x_im < *_num1_p))
1411 return asinh(x.conjugate());
1413 return conjugate_function(asinh(x)).hold();
1416 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1417 evalf_func(asinh_evalf).
1418 derivative_func(asinh_deriv).
1419 conjugate_func(asinh_conjugate));
1422 // inverse hyperbolic cosine (trigonometric function)
1425 static ex acosh_evalf(const ex & x)
1427 if (is_exactly_a<numeric>(x))
1428 return acosh(ex_to<numeric>(x));
1430 return acosh(x).hold();
1433 static ex acosh_eval(const ex & x)
1435 if (x.info(info_flags::numeric)) {
1437 // acosh(0) -> Pi*I/2
1439 return Pi*I*numeric(1,2);
1442 if (x.is_equal(_ex1))
1445 // acosh(-1) -> Pi*I
1446 if (x.is_equal(_ex_1))
1449 // acosh(float) -> float
1450 if (!x.info(info_flags::crational))
1451 return acosh(ex_to<numeric>(x));
1453 // acosh(-x) -> Pi*I-acosh(x)
1454 if (x.info(info_flags::negative))
1455 return Pi*I-acosh(-x);
1458 return acosh(x).hold();
1461 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1463 GINAC_ASSERT(deriv_param==0);
1465 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1466 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1469 static ex acosh_conjugate(const ex & x)
1471 // conjugate(acosh(x))==acosh(conjugate(x)) unless on the branch cut
1472 // which runs along the real axis from +1 to -inf.
1473 if (is_exactly_a<numeric>(x) &&
1474 (!x.imag_part().is_zero() || x > *_num1_p)) {
1475 return acosh(x.conjugate());
1477 return conjugate_function(acosh(x)).hold();
1480 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1481 evalf_func(acosh_evalf).
1482 derivative_func(acosh_deriv).
1483 conjugate_func(acosh_conjugate));
1486 // inverse hyperbolic tangent (trigonometric function)
1489 static ex atanh_evalf(const ex & x)
1491 if (is_exactly_a<numeric>(x))
1492 return atanh(ex_to<numeric>(x));
1494 return atanh(x).hold();
1497 static ex atanh_eval(const ex & x)
1499 if (x.info(info_flags::numeric)) {
1505 // atanh({+|-}1) -> throw
1506 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1507 throw (pole_error("atanh_eval(): logarithmic pole",0));
1509 // atanh(float) -> float
1510 if (!x.info(info_flags::crational))
1511 return atanh(ex_to<numeric>(x));
1514 if (x.info(info_flags::negative))
1518 return atanh(x).hold();
1521 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1523 GINAC_ASSERT(deriv_param==0);
1525 // d/dx atanh(x) -> 1/(1-x^2)
1526 return power(_ex1-power(x,_ex2),_ex_1);
1529 static ex atanh_series(const ex &arg,
1530 const relational &rel,
1534 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1536 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1537 // There are two branch cuts, one runnig from 1 up the real axis and one
1538 // one running from -1 down the real axis. The points 1 and -1 are poles
1539 // On the branch cuts and the poles series expand
1540 // (log(1+x)-log(1-x))/2
1542 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1543 if (!(arg_pt).info(info_flags::real))
1544 throw do_taylor(); // Im(x) != 0
1545 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1546 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1547 // care for the poles, using the defining formula for atanh()...
1548 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1549 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1550 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1551 if (!(options & series_options::suppress_branchcut)) {
1553 // This is the branch cut: assemble the primitive series manually and
1554 // then add the corresponding complex step function.
1555 const symbol &s = ex_to<symbol>(rel.lhs());
1556 const ex &point = rel.rhs();
1558 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1559 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1561 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1563 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1567 seq.push_back(expair(Order0correction, _ex0));
1569 seq.push_back(expair(Order(_ex1), order));
1570 return series(replarg - pseries(rel, std::move(seq)), rel, order);
1575 static ex atanh_conjugate(const ex & x)
1577 // conjugate(atanh(x))==atanh(conjugate(x)) unless on the branch cuts which
1578 // run along the real axis outside the interval [-1, +1].
1579 if (is_exactly_a<numeric>(x) &&
1580 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
1581 return atanh(x.conjugate());
1583 return conjugate_function(atanh(x)).hold();
1586 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1587 evalf_func(atanh_evalf).
1588 derivative_func(atanh_deriv).
1589 series_func(atanh_series).
1590 conjugate_func(atanh_conjugate));
1593 } // namespace GiNaC