1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2015 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) {
246 ex acc = dynallocate<pseries>(rel, epv);
248 epv.push_back(expair(-1, _ex0));
249 epv.push_back(expair(Order(_ex1), order));
250 ex rest = pseries(rel, std::move(epv)).add_series(argser);
251 for (int i = order-1; i>0; --i) {
252 epvector cterm { expair(i%2 ? _ex1/i : _ex_1/i, _ex0) };
253 acc = pseries(rel, std::move(cterm)).add_series(ex_to<pseries>(acc));
254 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
258 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
259 return pseries(rel, std::move(seq)).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
260 } else // it was a monomial
261 return pseries(rel, std::move(seq));
263 if (!(options & series_options::suppress_branchcut) &&
264 arg_pt.info(info_flags::negative)) {
266 // This is the branch cut: assemble the primitive series manually and
267 // then add the corresponding complex step function.
268 const symbol &s = ex_to<symbol>(rel.lhs());
269 const ex &point = rel.rhs();
271 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
272 epvector seq { expair(-I*csgn(arg*I)*Pi, _ex0),
273 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;
935 epvector seq { expair(Order0correction, _ex0),
936 expair(Order(_ex1), order) };
937 return series(replarg - pseries(rel, std::move(seq)), rel, order);
942 static ex atan_conjugate(const ex & x)
944 // conjugate(atan(x))==atan(conjugate(x)) unless on the branch cuts which
945 // run along the imaginary axis outside the interval [-I, +I].
946 if (x.info(info_flags::real))
948 if (is_exactly_a<numeric>(x)) {
949 const numeric x_re = ex_to<numeric>(x.real_part());
950 const numeric x_im = ex_to<numeric>(x.imag_part());
951 if (!x_re.is_zero() ||
952 (x_im > *_num_1_p && x_im < *_num1_p))
953 return atan(x.conjugate());
955 return conjugate_function(atan(x)).hold();
958 REGISTER_FUNCTION(atan, eval_func(atan_eval).
959 evalf_func(atan_evalf).
960 derivative_func(atan_deriv).
961 series_func(atan_series).
962 conjugate_func(atan_conjugate).
963 latex_name("\\arctan"));
966 // inverse tangent (atan2(y,x))
969 static ex atan2_evalf(const ex &y, const ex &x)
971 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
972 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
974 return atan2(y, x).hold();
977 static ex atan2_eval(const ex & y, const ex & x)
985 // atan2(0, x), x real and positive -> 0
986 if (x.info(info_flags::positive))
989 // atan2(0, x), x real and negative -> Pi
990 if (x.info(info_flags::negative))
996 // atan2(y, 0), y real and positive -> Pi/2
997 if (y.info(info_flags::positive))
1000 // atan2(y, 0), y real and negative -> -Pi/2
1001 if (y.info(info_flags::negative))
1005 if (y.is_equal(x)) {
1007 // atan2(y, y), y real and positive -> Pi/4
1008 if (y.info(info_flags::positive))
1011 // atan2(y, y), y real and negative -> -3/4*Pi
1012 if (y.info(info_flags::negative))
1013 return numeric(-3, 4)*Pi;
1016 if (y.is_equal(-x)) {
1018 // atan2(y, -y), y real and positive -> 3*Pi/4
1019 if (y.info(info_flags::positive))
1020 return numeric(3, 4)*Pi;
1022 // atan2(y, -y), y real and negative -> -Pi/4
1023 if (y.info(info_flags::negative))
1027 // atan2(float, float) -> float
1028 if (is_a<numeric>(y) && !y.info(info_flags::crational) &&
1029 is_a<numeric>(x) && !x.info(info_flags::crational))
1030 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
1032 // atan2(real, real) -> atan(y/x) +/- Pi
1033 if (y.info(info_flags::real) && x.info(info_flags::real)) {
1034 if (x.info(info_flags::positive))
1037 if (x.info(info_flags::negative)) {
1038 if (y.info(info_flags::positive))
1039 return atan(y/x)+Pi;
1040 if (y.info(info_flags::negative))
1041 return atan(y/x)-Pi;
1045 return atan2(y, x).hold();
1048 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
1050 GINAC_ASSERT(deriv_param<2);
1052 if (deriv_param==0) {
1054 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1057 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1060 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
1061 evalf_func(atan2_evalf).
1062 derivative_func(atan2_deriv));
1065 // hyperbolic sine (trigonometric function)
1068 static ex sinh_evalf(const ex & x)
1070 if (is_exactly_a<numeric>(x))
1071 return sinh(ex_to<numeric>(x));
1073 return sinh(x).hold();
1076 static ex sinh_eval(const ex & x)
1078 if (x.info(info_flags::numeric)) {
1084 // sinh(float) -> float
1085 if (!x.info(info_flags::crational))
1086 return sinh(ex_to<numeric>(x));
1089 if (x.info(info_flags::negative))
1093 if ((x/Pi).info(info_flags::numeric) &&
1094 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
1097 if (is_exactly_a<function>(x)) {
1098 const ex &t = x.op(0);
1100 // sinh(asinh(x)) -> x
1101 if (is_ex_the_function(x, asinh))
1104 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
1105 if (is_ex_the_function(x, acosh))
1106 return sqrt(t-_ex1)*sqrt(t+_ex1);
1108 // sinh(atanh(x)) -> x/sqrt(1-x^2)
1109 if (is_ex_the_function(x, atanh))
1110 return t*power(_ex1-power(t,_ex2),_ex_1_2);
1113 return sinh(x).hold();
1116 static ex sinh_deriv(const ex & x, unsigned deriv_param)
1118 GINAC_ASSERT(deriv_param==0);
1120 // d/dx sinh(x) -> cosh(x)
1124 static ex sinh_real_part(const ex & x)
1126 return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1129 static ex sinh_imag_part(const ex & x)
1131 return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1134 static ex sinh_conjugate(const ex & x)
1136 // conjugate(sinh(x))==sinh(conjugate(x))
1137 return sinh(x.conjugate());
1140 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
1141 evalf_func(sinh_evalf).
1142 derivative_func(sinh_deriv).
1143 real_part_func(sinh_real_part).
1144 imag_part_func(sinh_imag_part).
1145 conjugate_func(sinh_conjugate).
1146 latex_name("\\sinh"));
1149 // hyperbolic cosine (trigonometric function)
1152 static ex cosh_evalf(const ex & x)
1154 if (is_exactly_a<numeric>(x))
1155 return cosh(ex_to<numeric>(x));
1157 return cosh(x).hold();
1160 static ex cosh_eval(const ex & x)
1162 if (x.info(info_flags::numeric)) {
1168 // cosh(float) -> float
1169 if (!x.info(info_flags::crational))
1170 return cosh(ex_to<numeric>(x));
1173 if (x.info(info_flags::negative))
1177 if ((x/Pi).info(info_flags::numeric) &&
1178 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
1181 if (is_exactly_a<function>(x)) {
1182 const ex &t = x.op(0);
1184 // cosh(acosh(x)) -> x
1185 if (is_ex_the_function(x, acosh))
1188 // cosh(asinh(x)) -> sqrt(1+x^2)
1189 if (is_ex_the_function(x, asinh))
1190 return sqrt(_ex1+power(t,_ex2));
1192 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1193 if (is_ex_the_function(x, atanh))
1194 return power(_ex1-power(t,_ex2),_ex_1_2);
1197 return cosh(x).hold();
1200 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1202 GINAC_ASSERT(deriv_param==0);
1204 // d/dx cosh(x) -> sinh(x)
1208 static ex cosh_real_part(const ex & x)
1210 return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1213 static ex cosh_imag_part(const ex & x)
1215 return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1218 static ex cosh_conjugate(const ex & x)
1220 // conjugate(cosh(x))==cosh(conjugate(x))
1221 return cosh(x.conjugate());
1224 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1225 evalf_func(cosh_evalf).
1226 derivative_func(cosh_deriv).
1227 real_part_func(cosh_real_part).
1228 imag_part_func(cosh_imag_part).
1229 conjugate_func(cosh_conjugate).
1230 latex_name("\\cosh"));
1233 // hyperbolic tangent (trigonometric function)
1236 static ex tanh_evalf(const ex & x)
1238 if (is_exactly_a<numeric>(x))
1239 return tanh(ex_to<numeric>(x));
1241 return tanh(x).hold();
1244 static ex tanh_eval(const ex & x)
1246 if (x.info(info_flags::numeric)) {
1252 // tanh(float) -> float
1253 if (!x.info(info_flags::crational))
1254 return tanh(ex_to<numeric>(x));
1257 if (x.info(info_flags::negative))
1261 if ((x/Pi).info(info_flags::numeric) &&
1262 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1265 if (is_exactly_a<function>(x)) {
1266 const ex &t = x.op(0);
1268 // tanh(atanh(x)) -> x
1269 if (is_ex_the_function(x, atanh))
1272 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1273 if (is_ex_the_function(x, asinh))
1274 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1276 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1277 if (is_ex_the_function(x, acosh))
1278 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1281 return tanh(x).hold();
1284 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1286 GINAC_ASSERT(deriv_param==0);
1288 // d/dx tanh(x) -> 1-tanh(x)^2
1289 return _ex1-power(tanh(x),_ex2);
1292 static ex tanh_series(const ex &x,
1293 const relational &rel,
1297 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1299 // Taylor series where there is no pole falls back to tanh_deriv.
1300 // On a pole simply expand sinh(x)/cosh(x).
1301 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1302 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1303 throw do_taylor(); // caught by function::series()
1304 // if we got here we have to care for a simple pole
1305 return (sinh(x)/cosh(x)).series(rel, order, options);
1308 static ex tanh_real_part(const ex & x)
1310 ex a = GiNaC::real_part(x);
1311 ex b = GiNaC::imag_part(x);
1312 return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1315 static ex tanh_imag_part(const ex & x)
1317 ex a = GiNaC::real_part(x);
1318 ex b = GiNaC::imag_part(x);
1319 return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1322 static ex tanh_conjugate(const ex & x)
1324 // conjugate(tanh(x))==tanh(conjugate(x))
1325 return tanh(x.conjugate());
1328 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1329 evalf_func(tanh_evalf).
1330 derivative_func(tanh_deriv).
1331 series_func(tanh_series).
1332 real_part_func(tanh_real_part).
1333 imag_part_func(tanh_imag_part).
1334 conjugate_func(tanh_conjugate).
1335 latex_name("\\tanh"));
1338 // inverse hyperbolic sine (trigonometric function)
1341 static ex asinh_evalf(const ex & x)
1343 if (is_exactly_a<numeric>(x))
1344 return asinh(ex_to<numeric>(x));
1346 return asinh(x).hold();
1349 static ex asinh_eval(const ex & x)
1351 if (x.info(info_flags::numeric)) {
1357 // asinh(float) -> float
1358 if (!x.info(info_flags::crational))
1359 return asinh(ex_to<numeric>(x));
1362 if (x.info(info_flags::negative))
1366 return asinh(x).hold();
1369 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1371 GINAC_ASSERT(deriv_param==0);
1373 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1374 return power(_ex1+power(x,_ex2),_ex_1_2);
1377 static ex asinh_conjugate(const ex & x)
1379 // conjugate(asinh(x))==asinh(conjugate(x)) unless on the branch cuts which
1380 // run along the imaginary axis outside the interval [-I, +I].
1381 if (x.info(info_flags::real))
1383 if (is_exactly_a<numeric>(x)) {
1384 const numeric x_re = ex_to<numeric>(x.real_part());
1385 const numeric x_im = ex_to<numeric>(x.imag_part());
1386 if (!x_re.is_zero() ||
1387 (x_im > *_num_1_p && x_im < *_num1_p))
1388 return asinh(x.conjugate());
1390 return conjugate_function(asinh(x)).hold();
1393 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1394 evalf_func(asinh_evalf).
1395 derivative_func(asinh_deriv).
1396 conjugate_func(asinh_conjugate));
1399 // inverse hyperbolic cosine (trigonometric function)
1402 static ex acosh_evalf(const ex & x)
1404 if (is_exactly_a<numeric>(x))
1405 return acosh(ex_to<numeric>(x));
1407 return acosh(x).hold();
1410 static ex acosh_eval(const ex & x)
1412 if (x.info(info_flags::numeric)) {
1414 // acosh(0) -> Pi*I/2
1416 return Pi*I*numeric(1,2);
1419 if (x.is_equal(_ex1))
1422 // acosh(-1) -> Pi*I
1423 if (x.is_equal(_ex_1))
1426 // acosh(float) -> float
1427 if (!x.info(info_flags::crational))
1428 return acosh(ex_to<numeric>(x));
1430 // acosh(-x) -> Pi*I-acosh(x)
1431 if (x.info(info_flags::negative))
1432 return Pi*I-acosh(-x);
1435 return acosh(x).hold();
1438 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1440 GINAC_ASSERT(deriv_param==0);
1442 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1443 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1446 static ex acosh_conjugate(const ex & x)
1448 // conjugate(acosh(x))==acosh(conjugate(x)) unless on the branch cut
1449 // which runs along the real axis from +1 to -inf.
1450 if (is_exactly_a<numeric>(x) &&
1451 (!x.imag_part().is_zero() || x > *_num1_p)) {
1452 return acosh(x.conjugate());
1454 return conjugate_function(acosh(x)).hold();
1457 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1458 evalf_func(acosh_evalf).
1459 derivative_func(acosh_deriv).
1460 conjugate_func(acosh_conjugate));
1463 // inverse hyperbolic tangent (trigonometric function)
1466 static ex atanh_evalf(const ex & x)
1468 if (is_exactly_a<numeric>(x))
1469 return atanh(ex_to<numeric>(x));
1471 return atanh(x).hold();
1474 static ex atanh_eval(const ex & x)
1476 if (x.info(info_flags::numeric)) {
1482 // atanh({+|-}1) -> throw
1483 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1484 throw (pole_error("atanh_eval(): logarithmic pole",0));
1486 // atanh(float) -> float
1487 if (!x.info(info_flags::crational))
1488 return atanh(ex_to<numeric>(x));
1491 if (x.info(info_flags::negative))
1495 return atanh(x).hold();
1498 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1500 GINAC_ASSERT(deriv_param==0);
1502 // d/dx atanh(x) -> 1/(1-x^2)
1503 return power(_ex1-power(x,_ex2),_ex_1);
1506 static ex atanh_series(const ex &arg,
1507 const relational &rel,
1511 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1513 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1514 // There are two branch cuts, one runnig from 1 up the real axis and one
1515 // one running from -1 down the real axis. The points 1 and -1 are poles
1516 // On the branch cuts and the poles series expand
1517 // (log(1+x)-log(1-x))/2
1519 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1520 if (!(arg_pt).info(info_flags::real))
1521 throw do_taylor(); // Im(x) != 0
1522 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1523 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1524 // care for the poles, using the defining formula for atanh()...
1525 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1526 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1527 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1528 if (!(options & series_options::suppress_branchcut)) {
1530 // This is the branch cut: assemble the primitive series manually and
1531 // then add the corresponding complex step function.
1532 const symbol &s = ex_to<symbol>(rel.lhs());
1533 const ex &point = rel.rhs();
1535 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1536 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1538 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1540 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1541 epvector seq { expair(Order0correction, _ex0),
1542 expair(Order(_ex1), order) };
1543 return series(replarg - pseries(rel, std::move(seq)), rel, order);
1548 static ex atanh_conjugate(const ex & x)
1550 // conjugate(atanh(x))==atanh(conjugate(x)) unless on the branch cuts which
1551 // run along the real axis outside the interval [-1, +1].
1552 if (is_exactly_a<numeric>(x) &&
1553 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
1554 return atanh(x.conjugate());
1556 return conjugate_function(atanh(x)).hold();
1559 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1560 evalf_func(atanh_evalf).
1561 derivative_func(atanh_deriv).
1562 series_func(atanh_series).
1563 conjugate_func(atanh_conjugate));
1566 } // namespace GiNaC