1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2019 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);
271 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
273 seq.push_back(expair(Order(_ex1), order));
274 return series(replarg - I*Pi + pseries(rel, std::move(seq)), rel, order);
276 throw do_taylor(); // caught by function::series()
279 static ex log_real_part(const ex & x)
281 if (x.info(info_flags::nonnegative))
282 return log(x).hold();
286 static ex log_imag_part(const ex & x)
288 if (x.info(info_flags::nonnegative))
290 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
293 static ex log_expand(const ex & arg, unsigned options)
295 if ((options & expand_options::expand_transcendental)
296 && is_exactly_a<mul>(arg) && !arg.info(info_flags::indefinite)) {
299 sumseq.reserve(arg.nops());
300 prodseq.reserve(arg.nops());
303 // searching for positive/negative factors
304 for (const_iterator i = arg.begin(); i != arg.end(); ++i) {
306 if (options & expand_options::expand_function_args)
307 e=i->expand(options);
310 if (e.info(info_flags::positive))
311 sumseq.push_back(log(e));
312 else if (e.info(info_flags::negative)) {
313 sumseq.push_back(log(-e));
316 prodseq.push_back(e);
319 if (sumseq.size() > 0) {
321 if (options & expand_options::expand_function_args)
322 newarg=((possign?_ex1:_ex_1)*mul(prodseq)).expand(options);
324 newarg=(possign?_ex1:_ex_1)*mul(prodseq);
325 ex_to<basic>(newarg).setflag(status_flags::purely_indefinite);
327 return add(sumseq)+log(newarg);
329 if (!(options & expand_options::expand_function_args))
330 ex_to<basic>(arg).setflag(status_flags::purely_indefinite);
334 if (options & expand_options::expand_function_args)
335 return log(arg.expand(options)).hold();
337 return log(arg).hold();
340 static ex log_conjugate(const ex & x)
342 // conjugate(log(x))==log(conjugate(x)) unless on the branch cut which
343 // runs along the negative real axis.
344 if (x.info(info_flags::positive)) {
347 if (is_exactly_a<numeric>(x) &&
348 !x.imag_part().is_zero()) {
349 return log(x.conjugate());
351 return conjugate_function(log(x)).hold();
354 REGISTER_FUNCTION(log, eval_func(log_eval).
355 evalf_func(log_evalf).
356 expand_func(log_expand).
357 derivative_func(log_deriv).
358 series_func(log_series).
359 real_part_func(log_real_part).
360 imag_part_func(log_imag_part).
361 conjugate_func(log_conjugate).
365 // sine (trigonometric function)
368 static ex sin_evalf(const ex & x)
370 if (is_exactly_a<numeric>(x))
371 return sin(ex_to<numeric>(x));
373 return sin(x).hold();
376 static ex sin_eval(const ex & x)
378 // sin(n/d*Pi) -> { all known non-nested radicals }
379 const ex SixtyExOverPi = _ex60*x/Pi;
381 if (SixtyExOverPi.info(info_flags::integer)) {
382 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
384 // wrap to interval [0, Pi)
389 // wrap to interval [0, Pi/2)
392 if (z.is_equal(*_num0_p)) // sin(0) -> 0
394 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
395 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
396 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
397 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
398 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
400 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
401 return sign*_ex1_2*sqrt(_ex2);
402 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
403 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
404 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
405 return sign*_ex1_2*sqrt(_ex3);
406 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
407 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
408 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
412 if (is_exactly_a<function>(x)) {
413 const ex &t = x.op(0);
416 if (is_ex_the_function(x, asin))
419 // sin(acos(x)) -> sqrt(1-x^2)
420 if (is_ex_the_function(x, acos))
421 return sqrt(_ex1-power(t,_ex2));
423 // sin(atan(x)) -> x/sqrt(1+x^2)
424 if (is_ex_the_function(x, atan))
425 return t*power(_ex1+power(t,_ex2),_ex_1_2);
428 // sin(float) -> float
429 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
430 return sin(ex_to<numeric>(x));
433 if (x.info(info_flags::negative))
436 return sin(x).hold();
439 static ex sin_deriv(const ex & x, unsigned deriv_param)
441 GINAC_ASSERT(deriv_param==0);
443 // d/dx sin(x) -> cos(x)
447 static ex sin_real_part(const ex & x)
449 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
452 static ex sin_imag_part(const ex & x)
454 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
457 static ex sin_conjugate(const ex & x)
459 // conjugate(sin(x))==sin(conjugate(x))
460 return sin(x.conjugate());
463 REGISTER_FUNCTION(sin, eval_func(sin_eval).
464 evalf_func(sin_evalf).
465 derivative_func(sin_deriv).
466 real_part_func(sin_real_part).
467 imag_part_func(sin_imag_part).
468 conjugate_func(sin_conjugate).
469 latex_name("\\sin"));
472 // cosine (trigonometric function)
475 static ex cos_evalf(const ex & x)
477 if (is_exactly_a<numeric>(x))
478 return cos(ex_to<numeric>(x));
480 return cos(x).hold();
483 static ex cos_eval(const ex & x)
485 // cos(n/d*Pi) -> { all known non-nested radicals }
486 const ex SixtyExOverPi = _ex60*x/Pi;
488 if (SixtyExOverPi.info(info_flags::integer)) {
489 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
491 // wrap to interval [0, Pi)
495 // wrap to interval [0, Pi/2)
499 if (z.is_equal(*_num0_p)) // cos(0) -> 1
501 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
502 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
503 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
504 return sign*_ex1_2*sqrt(_ex3);
505 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
506 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
507 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
508 return sign*_ex1_2*sqrt(_ex2);
509 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
511 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
512 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
513 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
514 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
515 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
519 if (is_exactly_a<function>(x)) {
520 const ex &t = x.op(0);
523 if (is_ex_the_function(x, acos))
526 // cos(asin(x)) -> sqrt(1-x^2)
527 if (is_ex_the_function(x, asin))
528 return sqrt(_ex1-power(t,_ex2));
530 // cos(atan(x)) -> 1/sqrt(1+x^2)
531 if (is_ex_the_function(x, atan))
532 return power(_ex1+power(t,_ex2),_ex_1_2);
535 // cos(float) -> float
536 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
537 return cos(ex_to<numeric>(x));
540 if (x.info(info_flags::negative))
543 return cos(x).hold();
546 static ex cos_deriv(const ex & x, unsigned deriv_param)
548 GINAC_ASSERT(deriv_param==0);
550 // d/dx cos(x) -> -sin(x)
554 static ex cos_real_part(const ex & x)
556 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
559 static ex cos_imag_part(const ex & x)
561 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
564 static ex cos_conjugate(const ex & x)
566 // conjugate(cos(x))==cos(conjugate(x))
567 return cos(x.conjugate());
570 REGISTER_FUNCTION(cos, eval_func(cos_eval).
571 evalf_func(cos_evalf).
572 derivative_func(cos_deriv).
573 real_part_func(cos_real_part).
574 imag_part_func(cos_imag_part).
575 conjugate_func(cos_conjugate).
576 latex_name("\\cos"));
579 // tangent (trigonometric function)
582 static ex tan_evalf(const ex & x)
584 if (is_exactly_a<numeric>(x))
585 return tan(ex_to<numeric>(x));
587 return tan(x).hold();
590 static ex tan_eval(const ex & x)
592 // tan(n/d*Pi) -> { all known non-nested radicals }
593 const ex SixtyExOverPi = _ex60*x/Pi;
595 if (SixtyExOverPi.info(info_flags::integer)) {
596 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
598 // wrap to interval [0, Pi)
602 // wrap to interval [0, Pi/2)
606 if (z.is_equal(*_num0_p)) // tan(0) -> 0
608 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
609 return sign*(_ex2-sqrt(_ex3));
610 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
611 return sign*_ex1_3*sqrt(_ex3);
612 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
614 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
615 return sign*sqrt(_ex3);
616 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
617 return sign*(sqrt(_ex3)+_ex2);
618 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
619 throw (pole_error("tan_eval(): simple pole",1));
622 if (is_exactly_a<function>(x)) {
623 const ex &t = x.op(0);
626 if (is_ex_the_function(x, atan))
629 // tan(asin(x)) -> x/sqrt(1+x^2)
630 if (is_ex_the_function(x, asin))
631 return t*power(_ex1-power(t,_ex2),_ex_1_2);
633 // tan(acos(x)) -> sqrt(1-x^2)/x
634 if (is_ex_the_function(x, acos))
635 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
638 // tan(float) -> float
639 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
640 return tan(ex_to<numeric>(x));
644 if (x.info(info_flags::negative))
647 return tan(x).hold();
650 static ex tan_deriv(const ex & x, unsigned deriv_param)
652 GINAC_ASSERT(deriv_param==0);
654 // d/dx tan(x) -> 1+tan(x)^2;
655 return (_ex1+power(tan(x),_ex2));
658 static ex tan_real_part(const ex & x)
660 ex a = GiNaC::real_part(x);
661 ex b = GiNaC::imag_part(x);
662 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
665 static ex tan_imag_part(const ex & x)
667 ex a = GiNaC::real_part(x);
668 ex b = GiNaC::imag_part(x);
669 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
672 static ex tan_series(const ex &x,
673 const relational &rel,
677 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
679 // Taylor series where there is no pole falls back to tan_deriv.
680 // On a pole simply expand sin(x)/cos(x).
681 const ex x_pt = x.subs(rel, subs_options::no_pattern);
682 if (!(2*x_pt/Pi).info(info_flags::odd))
683 throw do_taylor(); // caught by function::series()
684 // if we got here we have to care for a simple pole
685 return (sin(x)/cos(x)).series(rel, order, options);
688 static ex tan_conjugate(const ex & x)
690 // conjugate(tan(x))==tan(conjugate(x))
691 return tan(x.conjugate());
694 REGISTER_FUNCTION(tan, eval_func(tan_eval).
695 evalf_func(tan_evalf).
696 derivative_func(tan_deriv).
697 series_func(tan_series).
698 real_part_func(tan_real_part).
699 imag_part_func(tan_imag_part).
700 conjugate_func(tan_conjugate).
701 latex_name("\\tan"));
704 // inverse sine (arc sine)
707 static ex asin_evalf(const ex & x)
709 if (is_exactly_a<numeric>(x))
710 return asin(ex_to<numeric>(x));
712 return asin(x).hold();
715 static ex asin_eval(const ex & x)
717 if (x.info(info_flags::numeric)) {
724 if (x.is_equal(_ex1_2))
725 return numeric(1,6)*Pi;
728 if (x.is_equal(_ex1))
731 // asin(-1/2) -> -Pi/6
732 if (x.is_equal(_ex_1_2))
733 return numeric(-1,6)*Pi;
736 if (x.is_equal(_ex_1))
739 // asin(float) -> float
740 if (!x.info(info_flags::crational))
741 return asin(ex_to<numeric>(x));
744 if (x.info(info_flags::negative))
748 return asin(x).hold();
751 static ex asin_deriv(const ex & x, unsigned deriv_param)
753 GINAC_ASSERT(deriv_param==0);
755 // d/dx asin(x) -> 1/sqrt(1-x^2)
756 return power(1-power(x,_ex2),_ex_1_2);
759 static ex asin_conjugate(const ex & x)
761 // conjugate(asin(x))==asin(conjugate(x)) unless on the branch cuts which
762 // run along the real axis outside the interval [-1, +1].
763 if (is_exactly_a<numeric>(x) &&
764 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
765 return asin(x.conjugate());
767 return conjugate_function(asin(x)).hold();
770 REGISTER_FUNCTION(asin, eval_func(asin_eval).
771 evalf_func(asin_evalf).
772 derivative_func(asin_deriv).
773 conjugate_func(asin_conjugate).
774 latex_name("\\arcsin"));
777 // inverse cosine (arc cosine)
780 static ex acos_evalf(const ex & x)
782 if (is_exactly_a<numeric>(x))
783 return acos(ex_to<numeric>(x));
785 return acos(x).hold();
788 static ex acos_eval(const ex & x)
790 if (x.info(info_flags::numeric)) {
793 if (x.is_equal(_ex1))
797 if (x.is_equal(_ex1_2))
804 // acos(-1/2) -> 2/3*Pi
805 if (x.is_equal(_ex_1_2))
806 return numeric(2,3)*Pi;
809 if (x.is_equal(_ex_1))
812 // acos(float) -> float
813 if (!x.info(info_flags::crational))
814 return acos(ex_to<numeric>(x));
816 // acos(-x) -> Pi-acos(x)
817 if (x.info(info_flags::negative))
821 return acos(x).hold();
824 static ex acos_deriv(const ex & x, unsigned deriv_param)
826 GINAC_ASSERT(deriv_param==0);
828 // d/dx acos(x) -> -1/sqrt(1-x^2)
829 return -power(1-power(x,_ex2),_ex_1_2);
832 static ex acos_conjugate(const ex & x)
834 // conjugate(acos(x))==acos(conjugate(x)) unless on the branch cuts which
835 // run along the real axis outside the interval [-1, +1].
836 if (is_exactly_a<numeric>(x) &&
837 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
838 return acos(x.conjugate());
840 return conjugate_function(acos(x)).hold();
843 REGISTER_FUNCTION(acos, eval_func(acos_eval).
844 evalf_func(acos_evalf).
845 derivative_func(acos_deriv).
846 conjugate_func(acos_conjugate).
847 latex_name("\\arccos"));
850 // inverse tangent (arc tangent)
853 static ex atan_evalf(const ex & x)
855 if (is_exactly_a<numeric>(x))
856 return atan(ex_to<numeric>(x));
858 return atan(x).hold();
861 static ex atan_eval(const ex & x)
863 if (x.info(info_flags::numeric)) {
870 if (x.is_equal(_ex1))
874 if (x.is_equal(_ex_1))
877 if (x.is_equal(I) || x.is_equal(-I))
878 throw (pole_error("atan_eval(): logarithmic pole",0));
880 // atan(float) -> float
881 if (!x.info(info_flags::crational))
882 return atan(ex_to<numeric>(x));
885 if (x.info(info_flags::negative))
889 return atan(x).hold();
892 static ex atan_deriv(const ex & x, unsigned deriv_param)
894 GINAC_ASSERT(deriv_param==0);
896 // d/dx atan(x) -> 1/(1+x^2)
897 return power(_ex1+power(x,_ex2), _ex_1);
900 static ex atan_series(const ex &arg,
901 const relational &rel,
905 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
907 // Taylor series where there is no pole or cut falls back to atan_deriv.
908 // There are two branch cuts, one runnig from I up the imaginary axis and
909 // one running from -I down the imaginary axis. The points I and -I are
911 // On the branch cuts and the poles series expand
912 // (log(1+I*x)-log(1-I*x))/(2*I)
914 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
915 if (!(I*arg_pt).info(info_flags::real))
916 throw do_taylor(); // Re(x) != 0
917 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
918 throw do_taylor(); // Re(x) == 0, but abs(x)<1
919 // care for the poles, using the defining formula for atan()...
920 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
921 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
922 if (!(options & series_options::suppress_branchcut)) {
924 // This is the branch cut: assemble the primitive series manually and
925 // then add the corresponding complex step function.
926 const symbol &s = ex_to<symbol>(rel.lhs());
927 const ex &point = rel.rhs();
929 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
930 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
932 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
934 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
938 seq.push_back(expair(Order0correction, _ex0));
940 seq.push_back(expair(Order(_ex1), order));
941 return series(replarg - pseries(rel, std::move(seq)), rel, order);
946 static ex atan_conjugate(const ex & x)
948 // conjugate(atan(x))==atan(conjugate(x)) unless on the branch cuts which
949 // run along the imaginary axis outside the interval [-I, +I].
950 if (x.info(info_flags::real))
952 if (is_exactly_a<numeric>(x)) {
953 const numeric x_re = ex_to<numeric>(x.real_part());
954 const numeric x_im = ex_to<numeric>(x.imag_part());
955 if (!x_re.is_zero() ||
956 (x_im > *_num_1_p && x_im < *_num1_p))
957 return atan(x.conjugate());
959 return conjugate_function(atan(x)).hold();
962 REGISTER_FUNCTION(atan, eval_func(atan_eval).
963 evalf_func(atan_evalf).
964 derivative_func(atan_deriv).
965 series_func(atan_series).
966 conjugate_func(atan_conjugate).
967 latex_name("\\arctan"));
970 // inverse tangent (atan2(y,x))
973 static ex atan2_evalf(const ex &y, const ex &x)
975 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
976 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
978 return atan2(y, x).hold();
981 static ex atan2_eval(const ex & y, const ex & x)
989 // atan2(0, x), x real and positive -> 0
990 if (x.info(info_flags::positive))
993 // atan2(0, x), x real and negative -> Pi
994 if (x.info(info_flags::negative))
1000 // atan2(y, 0), y real and positive -> Pi/2
1001 if (y.info(info_flags::positive))
1004 // atan2(y, 0), y real and negative -> -Pi/2
1005 if (y.info(info_flags::negative))
1009 if (y.is_equal(x)) {
1011 // atan2(y, y), y real and positive -> Pi/4
1012 if (y.info(info_flags::positive))
1015 // atan2(y, y), y real and negative -> -3/4*Pi
1016 if (y.info(info_flags::negative))
1017 return numeric(-3, 4)*Pi;
1020 if (y.is_equal(-x)) {
1022 // atan2(y, -y), y real and positive -> 3*Pi/4
1023 if (y.info(info_flags::positive))
1024 return numeric(3, 4)*Pi;
1026 // atan2(y, -y), y real and negative -> -Pi/4
1027 if (y.info(info_flags::negative))
1031 // atan2(float, float) -> float
1032 if (is_a<numeric>(y) && !y.info(info_flags::crational) &&
1033 is_a<numeric>(x) && !x.info(info_flags::crational))
1034 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
1036 // atan2(real, real) -> atan(y/x) +/- Pi
1037 if (y.info(info_flags::real) && x.info(info_flags::real)) {
1038 if (x.info(info_flags::positive))
1041 if (x.info(info_flags::negative)) {
1042 if (y.info(info_flags::positive))
1043 return atan(y/x)+Pi;
1044 if (y.info(info_flags::negative))
1045 return atan(y/x)-Pi;
1049 return atan2(y, x).hold();
1052 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
1054 GINAC_ASSERT(deriv_param<2);
1056 if (deriv_param==0) {
1058 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1061 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1064 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
1065 evalf_func(atan2_evalf).
1066 derivative_func(atan2_deriv));
1069 // hyperbolic sine (trigonometric function)
1072 static ex sinh_evalf(const ex & x)
1074 if (is_exactly_a<numeric>(x))
1075 return sinh(ex_to<numeric>(x));
1077 return sinh(x).hold();
1080 static ex sinh_eval(const ex & x)
1082 if (x.info(info_flags::numeric)) {
1088 // sinh(float) -> float
1089 if (!x.info(info_flags::crational))
1090 return sinh(ex_to<numeric>(x));
1093 if (x.info(info_flags::negative))
1097 if ((x/Pi).info(info_flags::numeric) &&
1098 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
1101 if (is_exactly_a<function>(x)) {
1102 const ex &t = x.op(0);
1104 // sinh(asinh(x)) -> x
1105 if (is_ex_the_function(x, asinh))
1108 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
1109 if (is_ex_the_function(x, acosh))
1110 return sqrt(t-_ex1)*sqrt(t+_ex1);
1112 // sinh(atanh(x)) -> x/sqrt(1-x^2)
1113 if (is_ex_the_function(x, atanh))
1114 return t*power(_ex1-power(t,_ex2),_ex_1_2);
1117 return sinh(x).hold();
1120 static ex sinh_deriv(const ex & x, unsigned deriv_param)
1122 GINAC_ASSERT(deriv_param==0);
1124 // d/dx sinh(x) -> cosh(x)
1128 static ex sinh_real_part(const ex & x)
1130 return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1133 static ex sinh_imag_part(const ex & x)
1135 return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1138 static ex sinh_conjugate(const ex & x)
1140 // conjugate(sinh(x))==sinh(conjugate(x))
1141 return sinh(x.conjugate());
1144 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
1145 evalf_func(sinh_evalf).
1146 derivative_func(sinh_deriv).
1147 real_part_func(sinh_real_part).
1148 imag_part_func(sinh_imag_part).
1149 conjugate_func(sinh_conjugate).
1150 latex_name("\\sinh"));
1153 // hyperbolic cosine (trigonometric function)
1156 static ex cosh_evalf(const ex & x)
1158 if (is_exactly_a<numeric>(x))
1159 return cosh(ex_to<numeric>(x));
1161 return cosh(x).hold();
1164 static ex cosh_eval(const ex & x)
1166 if (x.info(info_flags::numeric)) {
1172 // cosh(float) -> float
1173 if (!x.info(info_flags::crational))
1174 return cosh(ex_to<numeric>(x));
1177 if (x.info(info_flags::negative))
1181 if ((x/Pi).info(info_flags::numeric) &&
1182 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
1185 if (is_exactly_a<function>(x)) {
1186 const ex &t = x.op(0);
1188 // cosh(acosh(x)) -> x
1189 if (is_ex_the_function(x, acosh))
1192 // cosh(asinh(x)) -> sqrt(1+x^2)
1193 if (is_ex_the_function(x, asinh))
1194 return sqrt(_ex1+power(t,_ex2));
1196 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1197 if (is_ex_the_function(x, atanh))
1198 return power(_ex1-power(t,_ex2),_ex_1_2);
1201 return cosh(x).hold();
1204 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1206 GINAC_ASSERT(deriv_param==0);
1208 // d/dx cosh(x) -> sinh(x)
1212 static ex cosh_real_part(const ex & x)
1214 return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1217 static ex cosh_imag_part(const ex & x)
1219 return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1222 static ex cosh_conjugate(const ex & x)
1224 // conjugate(cosh(x))==cosh(conjugate(x))
1225 return cosh(x.conjugate());
1228 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1229 evalf_func(cosh_evalf).
1230 derivative_func(cosh_deriv).
1231 real_part_func(cosh_real_part).
1232 imag_part_func(cosh_imag_part).
1233 conjugate_func(cosh_conjugate).
1234 latex_name("\\cosh"));
1237 // hyperbolic tangent (trigonometric function)
1240 static ex tanh_evalf(const ex & x)
1242 if (is_exactly_a<numeric>(x))
1243 return tanh(ex_to<numeric>(x));
1245 return tanh(x).hold();
1248 static ex tanh_eval(const ex & x)
1250 if (x.info(info_flags::numeric)) {
1256 // tanh(float) -> float
1257 if (!x.info(info_flags::crational))
1258 return tanh(ex_to<numeric>(x));
1261 if (x.info(info_flags::negative))
1265 if ((x/Pi).info(info_flags::numeric) &&
1266 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1269 if (is_exactly_a<function>(x)) {
1270 const ex &t = x.op(0);
1272 // tanh(atanh(x)) -> x
1273 if (is_ex_the_function(x, atanh))
1276 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1277 if (is_ex_the_function(x, asinh))
1278 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1280 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1281 if (is_ex_the_function(x, acosh))
1282 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1285 return tanh(x).hold();
1288 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1290 GINAC_ASSERT(deriv_param==0);
1292 // d/dx tanh(x) -> 1-tanh(x)^2
1293 return _ex1-power(tanh(x),_ex2);
1296 static ex tanh_series(const ex &x,
1297 const relational &rel,
1301 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1303 // Taylor series where there is no pole falls back to tanh_deriv.
1304 // On a pole simply expand sinh(x)/cosh(x).
1305 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1306 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1307 throw do_taylor(); // caught by function::series()
1308 // if we got here we have to care for a simple pole
1309 return (sinh(x)/cosh(x)).series(rel, order, options);
1312 static ex tanh_real_part(const ex & x)
1314 ex a = GiNaC::real_part(x);
1315 ex b = GiNaC::imag_part(x);
1316 return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1319 static ex tanh_imag_part(const ex & x)
1321 ex a = GiNaC::real_part(x);
1322 ex b = GiNaC::imag_part(x);
1323 return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1326 static ex tanh_conjugate(const ex & x)
1328 // conjugate(tanh(x))==tanh(conjugate(x))
1329 return tanh(x.conjugate());
1332 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1333 evalf_func(tanh_evalf).
1334 derivative_func(tanh_deriv).
1335 series_func(tanh_series).
1336 real_part_func(tanh_real_part).
1337 imag_part_func(tanh_imag_part).
1338 conjugate_func(tanh_conjugate).
1339 latex_name("\\tanh"));
1342 // inverse hyperbolic sine (trigonometric function)
1345 static ex asinh_evalf(const ex & x)
1347 if (is_exactly_a<numeric>(x))
1348 return asinh(ex_to<numeric>(x));
1350 return asinh(x).hold();
1353 static ex asinh_eval(const ex & x)
1355 if (x.info(info_flags::numeric)) {
1361 // asinh(float) -> float
1362 if (!x.info(info_flags::crational))
1363 return asinh(ex_to<numeric>(x));
1366 if (x.info(info_flags::negative))
1370 return asinh(x).hold();
1373 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1375 GINAC_ASSERT(deriv_param==0);
1377 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1378 return power(_ex1+power(x,_ex2),_ex_1_2);
1381 static ex asinh_conjugate(const ex & x)
1383 // conjugate(asinh(x))==asinh(conjugate(x)) unless on the branch cuts which
1384 // run along the imaginary axis outside the interval [-I, +I].
1385 if (x.info(info_flags::real))
1387 if (is_exactly_a<numeric>(x)) {
1388 const numeric x_re = ex_to<numeric>(x.real_part());
1389 const numeric x_im = ex_to<numeric>(x.imag_part());
1390 if (!x_re.is_zero() ||
1391 (x_im > *_num_1_p && x_im < *_num1_p))
1392 return asinh(x.conjugate());
1394 return conjugate_function(asinh(x)).hold();
1397 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1398 evalf_func(asinh_evalf).
1399 derivative_func(asinh_deriv).
1400 conjugate_func(asinh_conjugate));
1403 // inverse hyperbolic cosine (trigonometric function)
1406 static ex acosh_evalf(const ex & x)
1408 if (is_exactly_a<numeric>(x))
1409 return acosh(ex_to<numeric>(x));
1411 return acosh(x).hold();
1414 static ex acosh_eval(const ex & x)
1416 if (x.info(info_flags::numeric)) {
1418 // acosh(0) -> Pi*I/2
1420 return Pi*I*numeric(1,2);
1423 if (x.is_equal(_ex1))
1426 // acosh(-1) -> Pi*I
1427 if (x.is_equal(_ex_1))
1430 // acosh(float) -> float
1431 if (!x.info(info_flags::crational))
1432 return acosh(ex_to<numeric>(x));
1434 // acosh(-x) -> Pi*I-acosh(x)
1435 if (x.info(info_flags::negative))
1436 return Pi*I-acosh(-x);
1439 return acosh(x).hold();
1442 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1444 GINAC_ASSERT(deriv_param==0);
1446 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1447 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1450 static ex acosh_conjugate(const ex & x)
1452 // conjugate(acosh(x))==acosh(conjugate(x)) unless on the branch cut
1453 // which runs along the real axis from +1 to -inf.
1454 if (is_exactly_a<numeric>(x) &&
1455 (!x.imag_part().is_zero() || x > *_num1_p)) {
1456 return acosh(x.conjugate());
1458 return conjugate_function(acosh(x)).hold();
1461 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1462 evalf_func(acosh_evalf).
1463 derivative_func(acosh_deriv).
1464 conjugate_func(acosh_conjugate));
1467 // inverse hyperbolic tangent (trigonometric function)
1470 static ex atanh_evalf(const ex & x)
1472 if (is_exactly_a<numeric>(x))
1473 return atanh(ex_to<numeric>(x));
1475 return atanh(x).hold();
1478 static ex atanh_eval(const ex & x)
1480 if (x.info(info_flags::numeric)) {
1486 // atanh({+|-}1) -> throw
1487 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1488 throw (pole_error("atanh_eval(): logarithmic pole",0));
1490 // atanh(float) -> float
1491 if (!x.info(info_flags::crational))
1492 return atanh(ex_to<numeric>(x));
1495 if (x.info(info_flags::negative))
1499 return atanh(x).hold();
1502 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1504 GINAC_ASSERT(deriv_param==0);
1506 // d/dx atanh(x) -> 1/(1-x^2)
1507 return power(_ex1-power(x,_ex2),_ex_1);
1510 static ex atanh_series(const ex &arg,
1511 const relational &rel,
1515 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1517 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1518 // There are two branch cuts, one runnig from 1 up the real axis and one
1519 // one running from -1 down the real axis. The points 1 and -1 are poles
1520 // On the branch cuts and the poles series expand
1521 // (log(1+x)-log(1-x))/2
1523 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1524 if (!(arg_pt).info(info_flags::real))
1525 throw do_taylor(); // Im(x) != 0
1526 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1527 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1528 // care for the poles, using the defining formula for atanh()...
1529 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1530 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1531 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1532 if (!(options & series_options::suppress_branchcut)) {
1534 // This is the branch cut: assemble the primitive series manually and
1535 // then add the corresponding complex step function.
1536 const symbol &s = ex_to<symbol>(rel.lhs());
1537 const ex &point = rel.rhs();
1539 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1540 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1542 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1544 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1548 seq.push_back(expair(Order0correction, _ex0));
1550 seq.push_back(expair(Order(_ex1), order));
1551 return series(replarg - pseries(rel, std::move(seq)), rel, order);
1556 static ex atanh_conjugate(const ex & x)
1558 // conjugate(atanh(x))==atanh(conjugate(x)) unless on the branch cuts which
1559 // run along the real axis outside the interval [-1, +1].
1560 if (is_exactly_a<numeric>(x) &&
1561 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
1562 return atanh(x.conjugate());
1564 return conjugate_function(atanh(x)).hold();
1567 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1568 evalf_func(atanh_evalf).
1569 derivative_func(atanh_deriv).
1570 series_func(atanh_series).
1571 conjugate_func(atanh_conjugate));
1574 } // namespace GiNaC