1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999-2011 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 (must_expand_arg) {
209 // This is the branch point: Series expand the argument first, then
210 // trivially factorize it to isolate that part which has constant
211 // leading coefficient in this fashion:
212 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
213 // Return a plain n*log(x) for the x^n part and series expand the
214 // other part. Add them together and reexpand again in order to have
215 // one unnested pseries object. All this also works for negative n.
216 pseries argser; // series expansion of log's argument
217 unsigned extra_ord = 0; // extra expansion order
219 // oops, the argument expanded to a pure Order(x^something)...
220 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
222 } while (!argser.is_terminating() && argser.nops()==1);
224 const symbol &s = ex_to<symbol>(rel.lhs());
225 const ex &point = rel.rhs();
226 const int n = argser.ldegree(s);
228 // construct what we carelessly called the n*log(x) term above
229 const ex coeff = argser.coeff(s, n);
230 // expand the log, but only if coeff is real and > 0, since otherwise
231 // it would make the branch cut run into the wrong direction
232 if (coeff.info(info_flags::positive))
233 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
235 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
237 if (!argser.is_terminating() || argser.nops()!=1) {
238 // in this case n more (or less) terms are needed
239 // (sadly, to generate them, we have to start from the beginning)
240 if (n == 0 && coeff == 1) {
242 ex acc = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
244 epv.push_back(expair(-1, _ex0));
245 epv.push_back(expair(Order(_ex1), order));
246 ex rest = pseries(rel, epv).add_series(argser);
247 for (int i = order-1; i>0; --i) {
250 cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
251 acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
252 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
256 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
257 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
258 } else // it was a monomial
259 return pseries(rel, seq);
261 if (!(options & series_options::suppress_branchcut) &&
262 arg_pt.info(info_flags::negative)) {
264 // This is the branch cut: assemble the primitive series manually and
265 // then add the corresponding complex step function.
266 const symbol &s = ex_to<symbol>(rel.lhs());
267 const ex &point = rel.rhs();
269 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));
272 seq.push_back(expair(Order(_ex1), order));
273 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
275 throw do_taylor(); // caught by function::series()
278 static ex log_real_part(const ex & x)
280 if (x.info(info_flags::nonnegative))
281 return log(x).hold();
285 static ex log_imag_part(const ex & x)
287 if (x.info(info_flags::nonnegative))
289 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
292 static ex log_expand(const ex & arg, unsigned options)
294 if ((options & expand_options::expand_transcendental)
295 && is_exactly_a<mul>(arg) && !arg.info(info_flags::indefinite)) {
298 sumseq.reserve(arg.nops());
299 prodseq.reserve(arg.nops());
302 // searching for positive/negative factors
303 for (const_iterator i = arg.begin(); i != arg.end(); ++i) {
305 if (options & expand_options::expand_function_args)
306 e=i->expand(options);
309 if (e.info(info_flags::positive))
310 sumseq.push_back(log(e));
311 else if (e.info(info_flags::negative)) {
312 sumseq.push_back(log(-e));
315 prodseq.push_back(e);
318 if (sumseq.size() > 0) {
320 if (options & expand_options::expand_function_args)
321 newarg=((possign?_ex1:_ex_1)*mul(prodseq)).expand(options);
323 newarg=(possign?_ex1:_ex_1)*mul(prodseq);
324 ex_to<basic>(newarg).setflag(status_flags::purely_indefinite);
326 return add(sumseq)+log(newarg);
328 if (!(options & expand_options::expand_function_args))
329 ex_to<basic>(arg).setflag(status_flags::purely_indefinite);
333 if (options & expand_options::expand_function_args)
334 return log(arg.expand(options)).hold();
336 return log(arg).hold();
339 static ex log_conjugate(const ex & x)
341 // conjugate(log(x))==log(conjugate(x)) unless on the branch cut which
342 // runs along the negative real axis.
343 if (x.info(info_flags::positive)) {
346 if (is_exactly_a<numeric>(x) &&
347 !x.imag_part().is_zero()) {
348 return log(x.conjugate());
350 return conjugate_function(log(x)).hold();
353 REGISTER_FUNCTION(log, eval_func(log_eval).
354 evalf_func(log_evalf).
355 expand_func(log_expand).
356 derivative_func(log_deriv).
357 series_func(log_series).
358 real_part_func(log_real_part).
359 imag_part_func(log_imag_part).
360 conjugate_func(log_conjugate).
364 // sine (trigonometric function)
367 static ex sin_evalf(const ex & x)
369 if (is_exactly_a<numeric>(x))
370 return sin(ex_to<numeric>(x));
372 return sin(x).hold();
375 static ex sin_eval(const ex & x)
377 // sin(n/d*Pi) -> { all known non-nested radicals }
378 const ex SixtyExOverPi = _ex60*x/Pi;
380 if (SixtyExOverPi.info(info_flags::integer)) {
381 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
383 // wrap to interval [0, Pi)
388 // wrap to interval [0, Pi/2)
391 if (z.is_equal(*_num0_p)) // sin(0) -> 0
393 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
394 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
395 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
396 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
397 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
399 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
400 return sign*_ex1_2*sqrt(_ex2);
401 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
402 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
403 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
404 return sign*_ex1_2*sqrt(_ex3);
405 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
406 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
407 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
411 if (is_exactly_a<function>(x)) {
412 const ex &t = x.op(0);
415 if (is_ex_the_function(x, asin))
418 // sin(acos(x)) -> sqrt(1-x^2)
419 if (is_ex_the_function(x, acos))
420 return sqrt(_ex1-power(t,_ex2));
422 // sin(atan(x)) -> x/sqrt(1+x^2)
423 if (is_ex_the_function(x, atan))
424 return t*power(_ex1+power(t,_ex2),_ex_1_2);
427 // sin(float) -> float
428 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
429 return sin(ex_to<numeric>(x));
432 if (x.info(info_flags::negative))
435 return sin(x).hold();
438 static ex sin_deriv(const ex & x, unsigned deriv_param)
440 GINAC_ASSERT(deriv_param==0);
442 // d/dx sin(x) -> cos(x)
446 static ex sin_real_part(const ex & x)
448 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
451 static ex sin_imag_part(const ex & x)
453 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
456 static ex sin_conjugate(const ex & x)
458 // conjugate(sin(x))==sin(conjugate(x))
459 return sin(x.conjugate());
462 REGISTER_FUNCTION(sin, eval_func(sin_eval).
463 evalf_func(sin_evalf).
464 derivative_func(sin_deriv).
465 real_part_func(sin_real_part).
466 imag_part_func(sin_imag_part).
467 conjugate_func(sin_conjugate).
468 latex_name("\\sin"));
471 // cosine (trigonometric function)
474 static ex cos_evalf(const ex & x)
476 if (is_exactly_a<numeric>(x))
477 return cos(ex_to<numeric>(x));
479 return cos(x).hold();
482 static ex cos_eval(const ex & x)
484 // cos(n/d*Pi) -> { all known non-nested radicals }
485 const ex SixtyExOverPi = _ex60*x/Pi;
487 if (SixtyExOverPi.info(info_flags::integer)) {
488 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
490 // wrap to interval [0, Pi)
494 // wrap to interval [0, Pi/2)
498 if (z.is_equal(*_num0_p)) // cos(0) -> 1
500 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
501 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
502 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
503 return sign*_ex1_2*sqrt(_ex3);
504 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
505 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
506 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
507 return sign*_ex1_2*sqrt(_ex2);
508 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
510 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
511 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
512 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
513 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
514 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
518 if (is_exactly_a<function>(x)) {
519 const ex &t = x.op(0);
522 if (is_ex_the_function(x, acos))
525 // cos(asin(x)) -> sqrt(1-x^2)
526 if (is_ex_the_function(x, asin))
527 return sqrt(_ex1-power(t,_ex2));
529 // cos(atan(x)) -> 1/sqrt(1+x^2)
530 if (is_ex_the_function(x, atan))
531 return power(_ex1+power(t,_ex2),_ex_1_2);
534 // cos(float) -> float
535 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
536 return cos(ex_to<numeric>(x));
539 if (x.info(info_flags::negative))
542 return cos(x).hold();
545 static ex cos_deriv(const ex & x, unsigned deriv_param)
547 GINAC_ASSERT(deriv_param==0);
549 // d/dx cos(x) -> -sin(x)
553 static ex cos_real_part(const ex & x)
555 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
558 static ex cos_imag_part(const ex & x)
560 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
563 static ex cos_conjugate(const ex & x)
565 // conjugate(cos(x))==cos(conjugate(x))
566 return cos(x.conjugate());
569 REGISTER_FUNCTION(cos, eval_func(cos_eval).
570 evalf_func(cos_evalf).
571 derivative_func(cos_deriv).
572 real_part_func(cos_real_part).
573 imag_part_func(cos_imag_part).
574 conjugate_func(cos_conjugate).
575 latex_name("\\cos"));
578 // tangent (trigonometric function)
581 static ex tan_evalf(const ex & x)
583 if (is_exactly_a<numeric>(x))
584 return tan(ex_to<numeric>(x));
586 return tan(x).hold();
589 static ex tan_eval(const ex & x)
591 // tan(n/d*Pi) -> { all known non-nested radicals }
592 const ex SixtyExOverPi = _ex60*x/Pi;
594 if (SixtyExOverPi.info(info_flags::integer)) {
595 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
597 // wrap to interval [0, Pi)
601 // wrap to interval [0, Pi/2)
605 if (z.is_equal(*_num0_p)) // tan(0) -> 0
607 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
608 return sign*(_ex2-sqrt(_ex3));
609 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
610 return sign*_ex1_3*sqrt(_ex3);
611 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
613 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
614 return sign*sqrt(_ex3);
615 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
616 return sign*(sqrt(_ex3)+_ex2);
617 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
618 throw (pole_error("tan_eval(): simple pole",1));
621 if (is_exactly_a<function>(x)) {
622 const ex &t = x.op(0);
625 if (is_ex_the_function(x, atan))
628 // tan(asin(x)) -> x/sqrt(1+x^2)
629 if (is_ex_the_function(x, asin))
630 return t*power(_ex1-power(t,_ex2),_ex_1_2);
632 // tan(acos(x)) -> sqrt(1-x^2)/x
633 if (is_ex_the_function(x, acos))
634 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
637 // tan(float) -> float
638 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
639 return tan(ex_to<numeric>(x));
643 if (x.info(info_flags::negative))
646 return tan(x).hold();
649 static ex tan_deriv(const ex & x, unsigned deriv_param)
651 GINAC_ASSERT(deriv_param==0);
653 // d/dx tan(x) -> 1+tan(x)^2;
654 return (_ex1+power(tan(x),_ex2));
657 static ex tan_real_part(const ex & x)
659 ex a = GiNaC::real_part(x);
660 ex b = GiNaC::imag_part(x);
661 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
664 static ex tan_imag_part(const ex & x)
666 ex a = GiNaC::real_part(x);
667 ex b = GiNaC::imag_part(x);
668 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
671 static ex tan_series(const ex &x,
672 const relational &rel,
676 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
678 // Taylor series where there is no pole falls back to tan_deriv.
679 // On a pole simply expand sin(x)/cos(x).
680 const ex x_pt = x.subs(rel, subs_options::no_pattern);
681 if (!(2*x_pt/Pi).info(info_flags::odd))
682 throw do_taylor(); // caught by function::series()
683 // if we got here we have to care for a simple pole
684 return (sin(x)/cos(x)).series(rel, order, options);
687 static ex tan_conjugate(const ex & x)
689 // conjugate(tan(x))==tan(conjugate(x))
690 return tan(x.conjugate());
693 REGISTER_FUNCTION(tan, eval_func(tan_eval).
694 evalf_func(tan_evalf).
695 derivative_func(tan_deriv).
696 series_func(tan_series).
697 real_part_func(tan_real_part).
698 imag_part_func(tan_imag_part).
699 conjugate_func(tan_conjugate).
700 latex_name("\\tan"));
703 // inverse sine (arc sine)
706 static ex asin_evalf(const ex & x)
708 if (is_exactly_a<numeric>(x))
709 return asin(ex_to<numeric>(x));
711 return asin(x).hold();
714 static ex asin_eval(const ex & x)
716 if (x.info(info_flags::numeric)) {
723 if (x.is_equal(_ex1_2))
724 return numeric(1,6)*Pi;
727 if (x.is_equal(_ex1))
730 // asin(-1/2) -> -Pi/6
731 if (x.is_equal(_ex_1_2))
732 return numeric(-1,6)*Pi;
735 if (x.is_equal(_ex_1))
738 // asin(float) -> float
739 if (!x.info(info_flags::crational))
740 return asin(ex_to<numeric>(x));
743 if (x.info(info_flags::negative))
747 return asin(x).hold();
750 static ex asin_deriv(const ex & x, unsigned deriv_param)
752 GINAC_ASSERT(deriv_param==0);
754 // d/dx asin(x) -> 1/sqrt(1-x^2)
755 return power(1-power(x,_ex2),_ex_1_2);
758 static ex asin_conjugate(const ex & x)
760 // conjugate(asin(x))==asin(conjugate(x)) unless on the branch cuts which
761 // run along the real axis outside the interval [-1, +1].
762 if (is_exactly_a<numeric>(x) &&
763 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
764 return asin(x.conjugate());
766 return conjugate_function(asin(x)).hold();
769 REGISTER_FUNCTION(asin, eval_func(asin_eval).
770 evalf_func(asin_evalf).
771 derivative_func(asin_deriv).
772 conjugate_func(asin_conjugate).
773 latex_name("\\arcsin"));
776 // inverse cosine (arc cosine)
779 static ex acos_evalf(const ex & x)
781 if (is_exactly_a<numeric>(x))
782 return acos(ex_to<numeric>(x));
784 return acos(x).hold();
787 static ex acos_eval(const ex & x)
789 if (x.info(info_flags::numeric)) {
792 if (x.is_equal(_ex1))
796 if (x.is_equal(_ex1_2))
803 // acos(-1/2) -> 2/3*Pi
804 if (x.is_equal(_ex_1_2))
805 return numeric(2,3)*Pi;
808 if (x.is_equal(_ex_1))
811 // acos(float) -> float
812 if (!x.info(info_flags::crational))
813 return acos(ex_to<numeric>(x));
815 // acos(-x) -> Pi-acos(x)
816 if (x.info(info_flags::negative))
820 return acos(x).hold();
823 static ex acos_deriv(const ex & x, unsigned deriv_param)
825 GINAC_ASSERT(deriv_param==0);
827 // d/dx acos(x) -> -1/sqrt(1-x^2)
828 return -power(1-power(x,_ex2),_ex_1_2);
831 static ex acos_conjugate(const ex & x)
833 // conjugate(acos(x))==acos(conjugate(x)) unless on the branch cuts which
834 // run along the real axis outside the interval [-1, +1].
835 if (is_exactly_a<numeric>(x) &&
836 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
837 return acos(x.conjugate());
839 return conjugate_function(acos(x)).hold();
842 REGISTER_FUNCTION(acos, eval_func(acos_eval).
843 evalf_func(acos_evalf).
844 derivative_func(acos_deriv).
845 conjugate_func(acos_conjugate).
846 latex_name("\\arccos"));
849 // inverse tangent (arc tangent)
852 static ex atan_evalf(const ex & x)
854 if (is_exactly_a<numeric>(x))
855 return atan(ex_to<numeric>(x));
857 return atan(x).hold();
860 static ex atan_eval(const ex & x)
862 if (x.info(info_flags::numeric)) {
869 if (x.is_equal(_ex1))
873 if (x.is_equal(_ex_1))
876 if (x.is_equal(I) || x.is_equal(-I))
877 throw (pole_error("atan_eval(): logarithmic pole",0));
879 // atan(float) -> float
880 if (!x.info(info_flags::crational))
881 return atan(ex_to<numeric>(x));
884 if (x.info(info_flags::negative))
888 return atan(x).hold();
891 static ex atan_deriv(const ex & x, unsigned deriv_param)
893 GINAC_ASSERT(deriv_param==0);
895 // d/dx atan(x) -> 1/(1+x^2)
896 return power(_ex1+power(x,_ex2), _ex_1);
899 static ex atan_series(const ex &arg,
900 const relational &rel,
904 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
906 // Taylor series where there is no pole or cut falls back to atan_deriv.
907 // There are two branch cuts, one runnig from I up the imaginary axis and
908 // one running from -I down the imaginary axis. The points I and -I are
910 // On the branch cuts and the poles series expand
911 // (log(1+I*x)-log(1-I*x))/(2*I)
913 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
914 if (!(I*arg_pt).info(info_flags::real))
915 throw do_taylor(); // Re(x) != 0
916 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
917 throw do_taylor(); // Re(x) == 0, but abs(x)<1
918 // care for the poles, using the defining formula for atan()...
919 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
920 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
921 if (!(options & series_options::suppress_branchcut)) {
923 // This is the branch cut: assemble the primitive series manually and
924 // then add the corresponding complex step function.
925 const symbol &s = ex_to<symbol>(rel.lhs());
926 const ex &point = rel.rhs();
928 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
929 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
931 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
933 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
935 seq.push_back(expair(Order0correction, _ex0));
936 seq.push_back(expair(Order(_ex1), order));
937 return series(replarg - pseries(rel, 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;
1542 seq.push_back(expair(Order0correction, _ex0));
1543 seq.push_back(expair(Order(_ex1), order));
1544 return series(replarg - pseries(rel, seq), rel, order);
1549 static ex atanh_conjugate(const ex & x)
1551 // conjugate(atanh(x))==atanh(conjugate(x)) unless on the branch cuts which
1552 // run along the real axis outside the interval [-1, +1].
1553 if (is_exactly_a<numeric>(x) &&
1554 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
1555 return atanh(x.conjugate());
1557 return conjugate_function(atanh(x)).hold();
1560 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1561 evalf_func(atanh_evalf).
1562 derivative_func(atanh_deriv).
1563 series_func(atanh_series).
1564 conjugate_func(atanh_conjugate));
1567 } // namespace GiNaC