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 (new mul(prodseq))->setflag(status_flags::dynallocated | 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 = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
248 epv.push_back(expair(-1, _ex0));
249 epv.push_back(expair(Order(_ex1), order));
250 ex rest = pseries(rel, epv).add_series(argser);
251 for (int i = order-1; i>0; --i) {
254 cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
255 acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
256 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
260 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
261 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
262 } else // it was a monomial
263 return pseries(rel, seq);
265 if (!(options & series_options::suppress_branchcut) &&
266 arg_pt.info(info_flags::negative)) {
268 // This is the branch cut: assemble the primitive series manually and
269 // then add the corresponding complex step function.
270 const symbol &s = ex_to<symbol>(rel.lhs());
271 const ex &point = rel.rhs();
273 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
275 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
276 seq.push_back(expair(Order(_ex1), order));
277 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
279 throw do_taylor(); // caught by function::series()
282 static ex log_real_part(const ex & x)
284 if (x.info(info_flags::nonnegative))
285 return log(x).hold();
289 static ex log_imag_part(const ex & x)
291 if (x.info(info_flags::nonnegative))
293 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
296 static ex log_expand(const ex & arg, unsigned options)
298 if ((options & expand_options::expand_transcendental)
299 && is_exactly_a<mul>(arg) && !arg.info(info_flags::indefinite)) {
302 sumseq.reserve(arg.nops());
303 prodseq.reserve(arg.nops());
306 // searching for positive/negative factors
307 for (const_iterator i = arg.begin(); i != arg.end(); ++i) {
309 if (options & expand_options::expand_function_args)
310 e=i->expand(options);
313 if (e.info(info_flags::positive))
314 sumseq.push_back(log(e));
315 else if (e.info(info_flags::negative)) {
316 sumseq.push_back(log(-e));
319 prodseq.push_back(e);
322 if (sumseq.size() > 0) {
324 if (options & expand_options::expand_function_args)
325 newarg=((possign?_ex1:_ex_1)*mul(prodseq)).expand(options);
327 newarg=(possign?_ex1:_ex_1)*mul(prodseq);
328 ex_to<basic>(newarg).setflag(status_flags::purely_indefinite);
330 return add(sumseq)+log(newarg);
332 if (!(options & expand_options::expand_function_args))
333 ex_to<basic>(arg).setflag(status_flags::purely_indefinite);
337 if (options & expand_options::expand_function_args)
338 return log(arg.expand(options)).hold();
340 return log(arg).hold();
343 static ex log_conjugate(const ex & x)
345 // conjugate(log(x))==log(conjugate(x)) unless on the branch cut which
346 // runs along the negative real axis.
347 if (x.info(info_flags::positive)) {
350 if (is_exactly_a<numeric>(x) &&
351 !x.imag_part().is_zero()) {
352 return log(x.conjugate());
354 return conjugate_function(log(x)).hold();
357 REGISTER_FUNCTION(log, eval_func(log_eval).
358 evalf_func(log_evalf).
359 expand_func(log_expand).
360 derivative_func(log_deriv).
361 series_func(log_series).
362 real_part_func(log_real_part).
363 imag_part_func(log_imag_part).
364 conjugate_func(log_conjugate).
368 // sine (trigonometric function)
371 static ex sin_evalf(const ex & x)
373 if (is_exactly_a<numeric>(x))
374 return sin(ex_to<numeric>(x));
376 return sin(x).hold();
379 static ex sin_eval(const ex & x)
381 // sin(n/d*Pi) -> { all known non-nested radicals }
382 const ex SixtyExOverPi = _ex60*x/Pi;
384 if (SixtyExOverPi.info(info_flags::integer)) {
385 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
387 // wrap to interval [0, Pi)
392 // wrap to interval [0, Pi/2)
395 if (z.is_equal(*_num0_p)) // sin(0) -> 0
397 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
398 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
399 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
400 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
401 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
403 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
404 return sign*_ex1_2*sqrt(_ex2);
405 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
406 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
407 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
408 return sign*_ex1_2*sqrt(_ex3);
409 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
410 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
411 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
415 if (is_exactly_a<function>(x)) {
416 const ex &t = x.op(0);
419 if (is_ex_the_function(x, asin))
422 // sin(acos(x)) -> sqrt(1-x^2)
423 if (is_ex_the_function(x, acos))
424 return sqrt(_ex1-power(t,_ex2));
426 // sin(atan(x)) -> x/sqrt(1+x^2)
427 if (is_ex_the_function(x, atan))
428 return t*power(_ex1+power(t,_ex2),_ex_1_2);
431 // sin(float) -> float
432 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
433 return sin(ex_to<numeric>(x));
436 if (x.info(info_flags::negative))
439 return sin(x).hold();
442 static ex sin_deriv(const ex & x, unsigned deriv_param)
444 GINAC_ASSERT(deriv_param==0);
446 // d/dx sin(x) -> cos(x)
450 static ex sin_real_part(const ex & x)
452 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
455 static ex sin_imag_part(const ex & x)
457 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
460 static ex sin_conjugate(const ex & x)
462 // conjugate(sin(x))==sin(conjugate(x))
463 return sin(x.conjugate());
466 REGISTER_FUNCTION(sin, eval_func(sin_eval).
467 evalf_func(sin_evalf).
468 derivative_func(sin_deriv).
469 real_part_func(sin_real_part).
470 imag_part_func(sin_imag_part).
471 conjugate_func(sin_conjugate).
472 latex_name("\\sin"));
475 // cosine (trigonometric function)
478 static ex cos_evalf(const ex & x)
480 if (is_exactly_a<numeric>(x))
481 return cos(ex_to<numeric>(x));
483 return cos(x).hold();
486 static ex cos_eval(const ex & x)
488 // cos(n/d*Pi) -> { all known non-nested radicals }
489 const ex SixtyExOverPi = _ex60*x/Pi;
491 if (SixtyExOverPi.info(info_flags::integer)) {
492 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
494 // wrap to interval [0, Pi)
498 // wrap to interval [0, Pi/2)
502 if (z.is_equal(*_num0_p)) // cos(0) -> 1
504 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
505 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
506 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
507 return sign*_ex1_2*sqrt(_ex3);
508 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
509 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
510 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
511 return sign*_ex1_2*sqrt(_ex2);
512 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
514 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
515 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
516 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
517 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
518 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
522 if (is_exactly_a<function>(x)) {
523 const ex &t = x.op(0);
526 if (is_ex_the_function(x, acos))
529 // cos(asin(x)) -> sqrt(1-x^2)
530 if (is_ex_the_function(x, asin))
531 return sqrt(_ex1-power(t,_ex2));
533 // cos(atan(x)) -> 1/sqrt(1+x^2)
534 if (is_ex_the_function(x, atan))
535 return power(_ex1+power(t,_ex2),_ex_1_2);
538 // cos(float) -> float
539 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
540 return cos(ex_to<numeric>(x));
543 if (x.info(info_flags::negative))
546 return cos(x).hold();
549 static ex cos_deriv(const ex & x, unsigned deriv_param)
551 GINAC_ASSERT(deriv_param==0);
553 // d/dx cos(x) -> -sin(x)
557 static ex cos_real_part(const ex & x)
559 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
562 static ex cos_imag_part(const ex & x)
564 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
567 static ex cos_conjugate(const ex & x)
569 // conjugate(cos(x))==cos(conjugate(x))
570 return cos(x.conjugate());
573 REGISTER_FUNCTION(cos, eval_func(cos_eval).
574 evalf_func(cos_evalf).
575 derivative_func(cos_deriv).
576 real_part_func(cos_real_part).
577 imag_part_func(cos_imag_part).
578 conjugate_func(cos_conjugate).
579 latex_name("\\cos"));
582 // tangent (trigonometric function)
585 static ex tan_evalf(const ex & x)
587 if (is_exactly_a<numeric>(x))
588 return tan(ex_to<numeric>(x));
590 return tan(x).hold();
593 static ex tan_eval(const ex & x)
595 // tan(n/d*Pi) -> { all known non-nested radicals }
596 const ex SixtyExOverPi = _ex60*x/Pi;
598 if (SixtyExOverPi.info(info_flags::integer)) {
599 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
601 // wrap to interval [0, Pi)
605 // wrap to interval [0, Pi/2)
609 if (z.is_equal(*_num0_p)) // tan(0) -> 0
611 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
612 return sign*(_ex2-sqrt(_ex3));
613 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
614 return sign*_ex1_3*sqrt(_ex3);
615 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
617 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
618 return sign*sqrt(_ex3);
619 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
620 return sign*(sqrt(_ex3)+_ex2);
621 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
622 throw (pole_error("tan_eval(): simple pole",1));
625 if (is_exactly_a<function>(x)) {
626 const ex &t = x.op(0);
629 if (is_ex_the_function(x, atan))
632 // tan(asin(x)) -> x/sqrt(1+x^2)
633 if (is_ex_the_function(x, asin))
634 return t*power(_ex1-power(t,_ex2),_ex_1_2);
636 // tan(acos(x)) -> sqrt(1-x^2)/x
637 if (is_ex_the_function(x, acos))
638 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
641 // tan(float) -> float
642 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
643 return tan(ex_to<numeric>(x));
647 if (x.info(info_flags::negative))
650 return tan(x).hold();
653 static ex tan_deriv(const ex & x, unsigned deriv_param)
655 GINAC_ASSERT(deriv_param==0);
657 // d/dx tan(x) -> 1+tan(x)^2;
658 return (_ex1+power(tan(x),_ex2));
661 static ex tan_real_part(const ex & x)
663 ex a = GiNaC::real_part(x);
664 ex b = GiNaC::imag_part(x);
665 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
668 static ex tan_imag_part(const ex & x)
670 ex a = GiNaC::real_part(x);
671 ex b = GiNaC::imag_part(x);
672 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
675 static ex tan_series(const ex &x,
676 const relational &rel,
680 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
682 // Taylor series where there is no pole falls back to tan_deriv.
683 // On a pole simply expand sin(x)/cos(x).
684 const ex x_pt = x.subs(rel, subs_options::no_pattern);
685 if (!(2*x_pt/Pi).info(info_flags::odd))
686 throw do_taylor(); // caught by function::series()
687 // if we got here we have to care for a simple pole
688 return (sin(x)/cos(x)).series(rel, order, options);
691 static ex tan_conjugate(const ex & x)
693 // conjugate(tan(x))==tan(conjugate(x))
694 return tan(x.conjugate());
697 REGISTER_FUNCTION(tan, eval_func(tan_eval).
698 evalf_func(tan_evalf).
699 derivative_func(tan_deriv).
700 series_func(tan_series).
701 real_part_func(tan_real_part).
702 imag_part_func(tan_imag_part).
703 conjugate_func(tan_conjugate).
704 latex_name("\\tan"));
707 // inverse sine (arc sine)
710 static ex asin_evalf(const ex & x)
712 if (is_exactly_a<numeric>(x))
713 return asin(ex_to<numeric>(x));
715 return asin(x).hold();
718 static ex asin_eval(const ex & x)
720 if (x.info(info_flags::numeric)) {
727 if (x.is_equal(_ex1_2))
728 return numeric(1,6)*Pi;
731 if (x.is_equal(_ex1))
734 // asin(-1/2) -> -Pi/6
735 if (x.is_equal(_ex_1_2))
736 return numeric(-1,6)*Pi;
739 if (x.is_equal(_ex_1))
742 // asin(float) -> float
743 if (!x.info(info_flags::crational))
744 return asin(ex_to<numeric>(x));
747 if (x.info(info_flags::negative))
751 return asin(x).hold();
754 static ex asin_deriv(const ex & x, unsigned deriv_param)
756 GINAC_ASSERT(deriv_param==0);
758 // d/dx asin(x) -> 1/sqrt(1-x^2)
759 return power(1-power(x,_ex2),_ex_1_2);
762 static ex asin_conjugate(const ex & x)
764 // conjugate(asin(x))==asin(conjugate(x)) unless on the branch cuts which
765 // run along the real axis outside the interval [-1, +1].
766 if (is_exactly_a<numeric>(x) &&
767 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
768 return asin(x.conjugate());
770 return conjugate_function(asin(x)).hold();
773 REGISTER_FUNCTION(asin, eval_func(asin_eval).
774 evalf_func(asin_evalf).
775 derivative_func(asin_deriv).
776 conjugate_func(asin_conjugate).
777 latex_name("\\arcsin"));
780 // inverse cosine (arc cosine)
783 static ex acos_evalf(const ex & x)
785 if (is_exactly_a<numeric>(x))
786 return acos(ex_to<numeric>(x));
788 return acos(x).hold();
791 static ex acos_eval(const ex & x)
793 if (x.info(info_flags::numeric)) {
796 if (x.is_equal(_ex1))
800 if (x.is_equal(_ex1_2))
807 // acos(-1/2) -> 2/3*Pi
808 if (x.is_equal(_ex_1_2))
809 return numeric(2,3)*Pi;
812 if (x.is_equal(_ex_1))
815 // acos(float) -> float
816 if (!x.info(info_flags::crational))
817 return acos(ex_to<numeric>(x));
819 // acos(-x) -> Pi-acos(x)
820 if (x.info(info_flags::negative))
824 return acos(x).hold();
827 static ex acos_deriv(const ex & x, unsigned deriv_param)
829 GINAC_ASSERT(deriv_param==0);
831 // d/dx acos(x) -> -1/sqrt(1-x^2)
832 return -power(1-power(x,_ex2),_ex_1_2);
835 static ex acos_conjugate(const ex & x)
837 // conjugate(acos(x))==acos(conjugate(x)) unless on the branch cuts which
838 // run along the real axis outside the interval [-1, +1].
839 if (is_exactly_a<numeric>(x) &&
840 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
841 return acos(x.conjugate());
843 return conjugate_function(acos(x)).hold();
846 REGISTER_FUNCTION(acos, eval_func(acos_eval).
847 evalf_func(acos_evalf).
848 derivative_func(acos_deriv).
849 conjugate_func(acos_conjugate).
850 latex_name("\\arccos"));
853 // inverse tangent (arc tangent)
856 static ex atan_evalf(const ex & x)
858 if (is_exactly_a<numeric>(x))
859 return atan(ex_to<numeric>(x));
861 return atan(x).hold();
864 static ex atan_eval(const ex & x)
866 if (x.info(info_flags::numeric)) {
873 if (x.is_equal(_ex1))
877 if (x.is_equal(_ex_1))
880 if (x.is_equal(I) || x.is_equal(-I))
881 throw (pole_error("atan_eval(): logarithmic pole",0));
883 // atan(float) -> float
884 if (!x.info(info_flags::crational))
885 return atan(ex_to<numeric>(x));
888 if (x.info(info_flags::negative))
892 return atan(x).hold();
895 static ex atan_deriv(const ex & x, unsigned deriv_param)
897 GINAC_ASSERT(deriv_param==0);
899 // d/dx atan(x) -> 1/(1+x^2)
900 return power(_ex1+power(x,_ex2), _ex_1);
903 static ex atan_series(const ex &arg,
904 const relational &rel,
908 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
910 // Taylor series where there is no pole or cut falls back to atan_deriv.
911 // There are two branch cuts, one runnig from I up the imaginary axis and
912 // one running from -I down the imaginary axis. The points I and -I are
914 // On the branch cuts and the poles series expand
915 // (log(1+I*x)-log(1-I*x))/(2*I)
917 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
918 if (!(I*arg_pt).info(info_flags::real))
919 throw do_taylor(); // Re(x) != 0
920 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
921 throw do_taylor(); // Re(x) == 0, but abs(x)<1
922 // care for the poles, using the defining formula for atan()...
923 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
924 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
925 if (!(options & series_options::suppress_branchcut)) {
927 // This is the branch cut: assemble the primitive series manually and
928 // then add the corresponding complex step function.
929 const symbol &s = ex_to<symbol>(rel.lhs());
930 const ex &point = rel.rhs();
932 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
933 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
935 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
937 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
939 seq.push_back(expair(Order0correction, _ex0));
940 seq.push_back(expair(Order(_ex1), order));
941 return series(replarg - pseries(rel, 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;
1546 seq.push_back(expair(Order0correction, _ex0));
1547 seq.push_back(expair(Order(_ex1), order));
1548 return series(replarg - pseries(rel, seq), rel, order);
1553 static ex atanh_conjugate(const ex & x)
1555 // conjugate(atanh(x))==atanh(conjugate(x)) unless on the branch cuts which
1556 // run along the real axis outside the interval [-1, +1].
1557 if (is_exactly_a<numeric>(x) &&
1558 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
1559 return atanh(x.conjugate());
1561 return conjugate_function(atanh(x)).hold();
1564 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1565 evalf_func(atanh_evalf).
1566 derivative_func(atanh_deriv).
1567 series_func(atanh_series).
1568 conjugate_func(atanh_conjugate));
1571 } // namespace GiNaC