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 // log(p^a) -> a*log(p), if p>0 and a is real
179 if (is_exactly_a<power>(x) && x.op(0).info(info_flags::positive) && x.op(1).info(info_flags::real)) {
180 return x.op(1)*log(x.op(0));
183 return log(x).hold();
186 static ex log_deriv(const ex & x, unsigned deriv_param)
188 GINAC_ASSERT(deriv_param==0);
190 // d/dx log(x) -> 1/x
191 return power(x, _ex_1);
194 static ex log_series(const ex &arg,
195 const relational &rel,
199 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
201 bool must_expand_arg = false;
202 // maybe substitution of rel into arg fails because of a pole
204 arg_pt = arg.subs(rel, subs_options::no_pattern);
205 } catch (pole_error) {
206 must_expand_arg = true;
208 // or we are at the branch point anyways
209 if (arg_pt.is_zero())
210 must_expand_arg = true;
212 if (must_expand_arg) {
214 // This is the branch point: Series expand the argument first, then
215 // trivially factorize it to isolate that part which has constant
216 // leading coefficient in this fashion:
217 // x^n + x^(n+1) +...+ Order(x^(n+m)) -> x^n * (1 + x +...+ Order(x^m)).
218 // Return a plain n*log(x) for the x^n part and series expand the
219 // other part. Add them together and reexpand again in order to have
220 // one unnested pseries object. All this also works for negative n.
221 pseries argser; // series expansion of log's argument
222 unsigned extra_ord = 0; // extra expansion order
224 // oops, the argument expanded to a pure Order(x^something)...
225 argser = ex_to<pseries>(arg.series(rel, order+extra_ord, options));
227 } while (!argser.is_terminating() && argser.nops()==1);
229 const symbol &s = ex_to<symbol>(rel.lhs());
230 const ex &point = rel.rhs();
231 const int n = argser.ldegree(s);
233 // construct what we carelessly called the n*log(x) term above
234 const ex coeff = argser.coeff(s, n);
235 // expand the log, but only if coeff is real and > 0, since otherwise
236 // it would make the branch cut run into the wrong direction
237 if (coeff.info(info_flags::positive))
238 seq.push_back(expair(n*log(s-point)+log(coeff), _ex0));
240 seq.push_back(expair(log(coeff*pow(s-point, n)), _ex0));
242 if (!argser.is_terminating() || argser.nops()!=1) {
243 // in this case n more (or less) terms are needed
244 // (sadly, to generate them, we have to start from the beginning)
245 if (n == 0 && coeff == 1) {
247 ex acc = (new pseries(rel, epv))->setflag(status_flags::dynallocated);
249 epv.push_back(expair(-1, _ex0));
250 epv.push_back(expair(Order(_ex1), order));
251 ex rest = pseries(rel, epv).add_series(argser);
252 for (int i = order-1; i>0; --i) {
255 cterm.push_back(expair(i%2 ? _ex1/i : _ex_1/i, _ex0));
256 acc = pseries(rel, cterm).add_series(ex_to<pseries>(acc));
257 acc = (ex_to<pseries>(rest)).mul_series(ex_to<pseries>(acc));
261 const ex newarg = ex_to<pseries>((arg/coeff).series(rel, order+n, options)).shift_exponents(-n).convert_to_poly(true);
262 return pseries(rel, seq).add_series(ex_to<pseries>(log(newarg).series(rel, order, options)));
263 } else // it was a monomial
264 return pseries(rel, seq);
266 if (!(options & series_options::suppress_branchcut) &&
267 arg_pt.info(info_flags::negative)) {
269 // This is the branch cut: assemble the primitive series manually and
270 // then add the corresponding complex step function.
271 const symbol &s = ex_to<symbol>(rel.lhs());
272 const ex &point = rel.rhs();
274 const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
276 seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0));
277 seq.push_back(expair(Order(_ex1), order));
278 return series(replarg - I*Pi + pseries(rel, seq), rel, order);
280 throw do_taylor(); // caught by function::series()
283 static ex log_real_part(const ex & x)
285 if (x.info(info_flags::nonnegative))
286 return log(x).hold();
290 static ex log_imag_part(const ex & x)
292 if (x.info(info_flags::nonnegative))
294 return atan2(GiNaC::imag_part(x), GiNaC::real_part(x));
297 static ex log_expand(const ex & arg, unsigned options)
299 if ((options & expand_options::expand_transcendental)
300 && is_exactly_a<mul>(arg) && !arg.info(info_flags::indefinite)) {
303 sumseq.reserve(arg.nops());
304 prodseq.reserve(arg.nops());
307 // searching for positive/negative factors
308 for (const_iterator i = arg.begin(); i != arg.end(); ++i) {
310 if (options & expand_options::expand_function_args)
311 e=i->expand(options);
314 if (e.info(info_flags::positive))
315 sumseq.push_back(log(e));
316 else if (e.info(info_flags::negative)) {
317 sumseq.push_back(log(-e));
320 prodseq.push_back(e);
323 if (sumseq.size() > 0) {
325 if (options & expand_options::expand_function_args)
326 newarg=((possign?_ex1:_ex_1)*mul(prodseq)).expand(options);
328 newarg=(possign?_ex1:_ex_1)*mul(prodseq);
329 ex_to<basic>(newarg).setflag(status_flags::purely_indefinite);
331 return add(sumseq)+log(newarg);
333 if (!(options & expand_options::expand_function_args))
334 ex_to<basic>(arg).setflag(status_flags::purely_indefinite);
338 if (options & expand_options::expand_function_args)
339 return log(arg.expand(options)).hold();
341 return log(arg).hold();
344 static ex log_conjugate(const ex & x)
346 // conjugate(log(x))==log(conjugate(x)) unless on the branch cut which
347 // runs along the negative real axis.
348 if (x.info(info_flags::positive)) {
351 if (is_exactly_a<numeric>(x) &&
352 !x.imag_part().is_zero()) {
353 return log(x.conjugate());
355 return conjugate_function(log(x)).hold();
358 REGISTER_FUNCTION(log, eval_func(log_eval).
359 evalf_func(log_evalf).
360 expand_func(log_expand).
361 derivative_func(log_deriv).
362 series_func(log_series).
363 real_part_func(log_real_part).
364 imag_part_func(log_imag_part).
365 conjugate_func(log_conjugate).
369 // sine (trigonometric function)
372 static ex sin_evalf(const ex & x)
374 if (is_exactly_a<numeric>(x))
375 return sin(ex_to<numeric>(x));
377 return sin(x).hold();
380 static ex sin_eval(const ex & x)
382 // sin(n/d*Pi) -> { all known non-nested radicals }
383 const ex SixtyExOverPi = _ex60*x/Pi;
385 if (SixtyExOverPi.info(info_flags::integer)) {
386 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
388 // wrap to interval [0, Pi)
393 // wrap to interval [0, Pi/2)
396 if (z.is_equal(*_num0_p)) // sin(0) -> 0
398 if (z.is_equal(*_num5_p)) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
399 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
400 if (z.is_equal(*_num6_p)) // sin(Pi/10) -> sqrt(5)/4-1/4
401 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
402 if (z.is_equal(*_num10_p)) // sin(Pi/6) -> 1/2
404 if (z.is_equal(*_num15_p)) // sin(Pi/4) -> sqrt(2)/2
405 return sign*_ex1_2*sqrt(_ex2);
406 if (z.is_equal(*_num18_p)) // sin(3/10*Pi) -> sqrt(5)/4+1/4
407 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
408 if (z.is_equal(*_num20_p)) // sin(Pi/3) -> sqrt(3)/2
409 return sign*_ex1_2*sqrt(_ex3);
410 if (z.is_equal(*_num25_p)) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
411 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
412 if (z.is_equal(*_num30_p)) // sin(Pi/2) -> 1
416 if (is_exactly_a<function>(x)) {
417 const ex &t = x.op(0);
420 if (is_ex_the_function(x, asin))
423 // sin(acos(x)) -> sqrt(1-x^2)
424 if (is_ex_the_function(x, acos))
425 return sqrt(_ex1-power(t,_ex2));
427 // sin(atan(x)) -> x/sqrt(1+x^2)
428 if (is_ex_the_function(x, atan))
429 return t*power(_ex1+power(t,_ex2),_ex_1_2);
432 // sin(float) -> float
433 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
434 return sin(ex_to<numeric>(x));
437 if (x.info(info_flags::negative))
440 return sin(x).hold();
443 static ex sin_deriv(const ex & x, unsigned deriv_param)
445 GINAC_ASSERT(deriv_param==0);
447 // d/dx sin(x) -> cos(x)
451 static ex sin_real_part(const ex & x)
453 return cosh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
456 static ex sin_imag_part(const ex & x)
458 return sinh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
461 static ex sin_conjugate(const ex & x)
463 // conjugate(sin(x))==sin(conjugate(x))
464 return sin(x.conjugate());
467 REGISTER_FUNCTION(sin, eval_func(sin_eval).
468 evalf_func(sin_evalf).
469 derivative_func(sin_deriv).
470 real_part_func(sin_real_part).
471 imag_part_func(sin_imag_part).
472 conjugate_func(sin_conjugate).
473 latex_name("\\sin"));
476 // cosine (trigonometric function)
479 static ex cos_evalf(const ex & x)
481 if (is_exactly_a<numeric>(x))
482 return cos(ex_to<numeric>(x));
484 return cos(x).hold();
487 static ex cos_eval(const ex & x)
489 // cos(n/d*Pi) -> { all known non-nested radicals }
490 const ex SixtyExOverPi = _ex60*x/Pi;
492 if (SixtyExOverPi.info(info_flags::integer)) {
493 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num120_p);
495 // wrap to interval [0, Pi)
499 // wrap to interval [0, Pi/2)
503 if (z.is_equal(*_num0_p)) // cos(0) -> 1
505 if (z.is_equal(*_num5_p)) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
506 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex1_3*sqrt(_ex3));
507 if (z.is_equal(*_num10_p)) // cos(Pi/6) -> sqrt(3)/2
508 return sign*_ex1_2*sqrt(_ex3);
509 if (z.is_equal(*_num12_p)) // cos(Pi/5) -> sqrt(5)/4+1/4
510 return sign*(_ex1_4*sqrt(_ex5)+_ex1_4);
511 if (z.is_equal(*_num15_p)) // cos(Pi/4) -> sqrt(2)/2
512 return sign*_ex1_2*sqrt(_ex2);
513 if (z.is_equal(*_num20_p)) // cos(Pi/3) -> 1/2
515 if (z.is_equal(*_num24_p)) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
516 return sign*(_ex1_4*sqrt(_ex5)+_ex_1_4);
517 if (z.is_equal(*_num25_p)) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
518 return sign*_ex1_4*sqrt(_ex6)*(_ex1+_ex_1_3*sqrt(_ex3));
519 if (z.is_equal(*_num30_p)) // cos(Pi/2) -> 0
523 if (is_exactly_a<function>(x)) {
524 const ex &t = x.op(0);
527 if (is_ex_the_function(x, acos))
530 // cos(asin(x)) -> sqrt(1-x^2)
531 if (is_ex_the_function(x, asin))
532 return sqrt(_ex1-power(t,_ex2));
534 // cos(atan(x)) -> 1/sqrt(1+x^2)
535 if (is_ex_the_function(x, atan))
536 return power(_ex1+power(t,_ex2),_ex_1_2);
539 // cos(float) -> float
540 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
541 return cos(ex_to<numeric>(x));
544 if (x.info(info_flags::negative))
547 return cos(x).hold();
550 static ex cos_deriv(const ex & x, unsigned deriv_param)
552 GINAC_ASSERT(deriv_param==0);
554 // d/dx cos(x) -> -sin(x)
558 static ex cos_real_part(const ex & x)
560 return cosh(GiNaC::imag_part(x))*cos(GiNaC::real_part(x));
563 static ex cos_imag_part(const ex & x)
565 return -sinh(GiNaC::imag_part(x))*sin(GiNaC::real_part(x));
568 static ex cos_conjugate(const ex & x)
570 // conjugate(cos(x))==cos(conjugate(x))
571 return cos(x.conjugate());
574 REGISTER_FUNCTION(cos, eval_func(cos_eval).
575 evalf_func(cos_evalf).
576 derivative_func(cos_deriv).
577 real_part_func(cos_real_part).
578 imag_part_func(cos_imag_part).
579 conjugate_func(cos_conjugate).
580 latex_name("\\cos"));
583 // tangent (trigonometric function)
586 static ex tan_evalf(const ex & x)
588 if (is_exactly_a<numeric>(x))
589 return tan(ex_to<numeric>(x));
591 return tan(x).hold();
594 static ex tan_eval(const ex & x)
596 // tan(n/d*Pi) -> { all known non-nested radicals }
597 const ex SixtyExOverPi = _ex60*x/Pi;
599 if (SixtyExOverPi.info(info_flags::integer)) {
600 numeric z = mod(ex_to<numeric>(SixtyExOverPi),*_num60_p);
602 // wrap to interval [0, Pi)
606 // wrap to interval [0, Pi/2)
610 if (z.is_equal(*_num0_p)) // tan(0) -> 0
612 if (z.is_equal(*_num5_p)) // tan(Pi/12) -> 2-sqrt(3)
613 return sign*(_ex2-sqrt(_ex3));
614 if (z.is_equal(*_num10_p)) // tan(Pi/6) -> sqrt(3)/3
615 return sign*_ex1_3*sqrt(_ex3);
616 if (z.is_equal(*_num15_p)) // tan(Pi/4) -> 1
618 if (z.is_equal(*_num20_p)) // tan(Pi/3) -> sqrt(3)
619 return sign*sqrt(_ex3);
620 if (z.is_equal(*_num25_p)) // tan(5/12*Pi) -> 2+sqrt(3)
621 return sign*(sqrt(_ex3)+_ex2);
622 if (z.is_equal(*_num30_p)) // tan(Pi/2) -> infinity
623 throw (pole_error("tan_eval(): simple pole",1));
626 if (is_exactly_a<function>(x)) {
627 const ex &t = x.op(0);
630 if (is_ex_the_function(x, atan))
633 // tan(asin(x)) -> x/sqrt(1+x^2)
634 if (is_ex_the_function(x, asin))
635 return t*power(_ex1-power(t,_ex2),_ex_1_2);
637 // tan(acos(x)) -> sqrt(1-x^2)/x
638 if (is_ex_the_function(x, acos))
639 return power(t,_ex_1)*sqrt(_ex1-power(t,_ex2));
642 // tan(float) -> float
643 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
644 return tan(ex_to<numeric>(x));
648 if (x.info(info_flags::negative))
651 return tan(x).hold();
654 static ex tan_deriv(const ex & x, unsigned deriv_param)
656 GINAC_ASSERT(deriv_param==0);
658 // d/dx tan(x) -> 1+tan(x)^2;
659 return (_ex1+power(tan(x),_ex2));
662 static ex tan_real_part(const ex & x)
664 ex a = GiNaC::real_part(x);
665 ex b = GiNaC::imag_part(x);
666 return tan(a)/(1+power(tan(a),2)*power(tan(b),2));
669 static ex tan_imag_part(const ex & x)
671 ex a = GiNaC::real_part(x);
672 ex b = GiNaC::imag_part(x);
673 return tanh(b)/(1+power(tan(a),2)*power(tan(b),2));
676 static ex tan_series(const ex &x,
677 const relational &rel,
681 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
683 // Taylor series where there is no pole falls back to tan_deriv.
684 // On a pole simply expand sin(x)/cos(x).
685 const ex x_pt = x.subs(rel, subs_options::no_pattern);
686 if (!(2*x_pt/Pi).info(info_flags::odd))
687 throw do_taylor(); // caught by function::series()
688 // if we got here we have to care for a simple pole
689 return (sin(x)/cos(x)).series(rel, order, options);
692 static ex tan_conjugate(const ex & x)
694 // conjugate(tan(x))==tan(conjugate(x))
695 return tan(x.conjugate());
698 REGISTER_FUNCTION(tan, eval_func(tan_eval).
699 evalf_func(tan_evalf).
700 derivative_func(tan_deriv).
701 series_func(tan_series).
702 real_part_func(tan_real_part).
703 imag_part_func(tan_imag_part).
704 conjugate_func(tan_conjugate).
705 latex_name("\\tan"));
708 // inverse sine (arc sine)
711 static ex asin_evalf(const ex & x)
713 if (is_exactly_a<numeric>(x))
714 return asin(ex_to<numeric>(x));
716 return asin(x).hold();
719 static ex asin_eval(const ex & x)
721 if (x.info(info_flags::numeric)) {
728 if (x.is_equal(_ex1_2))
729 return numeric(1,6)*Pi;
732 if (x.is_equal(_ex1))
735 // asin(-1/2) -> -Pi/6
736 if (x.is_equal(_ex_1_2))
737 return numeric(-1,6)*Pi;
740 if (x.is_equal(_ex_1))
743 // asin(float) -> float
744 if (!x.info(info_flags::crational))
745 return asin(ex_to<numeric>(x));
748 if (x.info(info_flags::negative))
752 return asin(x).hold();
755 static ex asin_deriv(const ex & x, unsigned deriv_param)
757 GINAC_ASSERT(deriv_param==0);
759 // d/dx asin(x) -> 1/sqrt(1-x^2)
760 return power(1-power(x,_ex2),_ex_1_2);
763 static ex asin_conjugate(const ex & x)
765 // conjugate(asin(x))==asin(conjugate(x)) unless on the branch cuts which
766 // run along the real axis outside the interval [-1, +1].
767 if (is_exactly_a<numeric>(x) &&
768 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
769 return asin(x.conjugate());
771 return conjugate_function(asin(x)).hold();
774 REGISTER_FUNCTION(asin, eval_func(asin_eval).
775 evalf_func(asin_evalf).
776 derivative_func(asin_deriv).
777 conjugate_func(asin_conjugate).
778 latex_name("\\arcsin"));
781 // inverse cosine (arc cosine)
784 static ex acos_evalf(const ex & x)
786 if (is_exactly_a<numeric>(x))
787 return acos(ex_to<numeric>(x));
789 return acos(x).hold();
792 static ex acos_eval(const ex & x)
794 if (x.info(info_flags::numeric)) {
797 if (x.is_equal(_ex1))
801 if (x.is_equal(_ex1_2))
808 // acos(-1/2) -> 2/3*Pi
809 if (x.is_equal(_ex_1_2))
810 return numeric(2,3)*Pi;
813 if (x.is_equal(_ex_1))
816 // acos(float) -> float
817 if (!x.info(info_flags::crational))
818 return acos(ex_to<numeric>(x));
820 // acos(-x) -> Pi-acos(x)
821 if (x.info(info_flags::negative))
825 return acos(x).hold();
828 static ex acos_deriv(const ex & x, unsigned deriv_param)
830 GINAC_ASSERT(deriv_param==0);
832 // d/dx acos(x) -> -1/sqrt(1-x^2)
833 return -power(1-power(x,_ex2),_ex_1_2);
836 static ex acos_conjugate(const ex & x)
838 // conjugate(acos(x))==acos(conjugate(x)) unless on the branch cuts which
839 // run along the real axis outside the interval [-1, +1].
840 if (is_exactly_a<numeric>(x) &&
841 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
842 return acos(x.conjugate());
844 return conjugate_function(acos(x)).hold();
847 REGISTER_FUNCTION(acos, eval_func(acos_eval).
848 evalf_func(acos_evalf).
849 derivative_func(acos_deriv).
850 conjugate_func(acos_conjugate).
851 latex_name("\\arccos"));
854 // inverse tangent (arc tangent)
857 static ex atan_evalf(const ex & x)
859 if (is_exactly_a<numeric>(x))
860 return atan(ex_to<numeric>(x));
862 return atan(x).hold();
865 static ex atan_eval(const ex & x)
867 if (x.info(info_flags::numeric)) {
874 if (x.is_equal(_ex1))
878 if (x.is_equal(_ex_1))
881 if (x.is_equal(I) || x.is_equal(-I))
882 throw (pole_error("atan_eval(): logarithmic pole",0));
884 // atan(float) -> float
885 if (!x.info(info_flags::crational))
886 return atan(ex_to<numeric>(x));
889 if (x.info(info_flags::negative))
893 return atan(x).hold();
896 static ex atan_deriv(const ex & x, unsigned deriv_param)
898 GINAC_ASSERT(deriv_param==0);
900 // d/dx atan(x) -> 1/(1+x^2)
901 return power(_ex1+power(x,_ex2), _ex_1);
904 static ex atan_series(const ex &arg,
905 const relational &rel,
909 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
911 // Taylor series where there is no pole or cut falls back to atan_deriv.
912 // There are two branch cuts, one runnig from I up the imaginary axis and
913 // one running from -I down the imaginary axis. The points I and -I are
915 // On the branch cuts and the poles series expand
916 // (log(1+I*x)-log(1-I*x))/(2*I)
918 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
919 if (!(I*arg_pt).info(info_flags::real))
920 throw do_taylor(); // Re(x) != 0
921 if ((I*arg_pt).info(info_flags::real) && abs(I*arg_pt)<_ex1)
922 throw do_taylor(); // Re(x) == 0, but abs(x)<1
923 // care for the poles, using the defining formula for atan()...
924 if (arg_pt.is_equal(I) || arg_pt.is_equal(-I))
925 return ((log(1+I*arg)-log(1-I*arg))/(2*I)).series(rel, order, options);
926 if (!(options & series_options::suppress_branchcut)) {
928 // This is the branch cut: assemble the primitive series manually and
929 // then add the corresponding complex step function.
930 const symbol &s = ex_to<symbol>(rel.lhs());
931 const ex &point = rel.rhs();
933 const ex replarg = series(atan(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
934 ex Order0correction = replarg.op(0)+csgn(arg)*Pi*_ex_1_2;
936 Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2;
938 Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2;
940 seq.push_back(expair(Order0correction, _ex0));
941 seq.push_back(expair(Order(_ex1), order));
942 return series(replarg - pseries(rel, seq), rel, order);
947 static ex atan_conjugate(const ex & x)
949 // conjugate(atan(x))==atan(conjugate(x)) unless on the branch cuts which
950 // run along the imaginary axis outside the interval [-I, +I].
951 if (x.info(info_flags::real))
953 if (is_exactly_a<numeric>(x)) {
954 const numeric x_re = ex_to<numeric>(x.real_part());
955 const numeric x_im = ex_to<numeric>(x.imag_part());
956 if (!x_re.is_zero() ||
957 (x_im > *_num_1_p && x_im < *_num1_p))
958 return atan(x.conjugate());
960 return conjugate_function(atan(x)).hold();
963 REGISTER_FUNCTION(atan, eval_func(atan_eval).
964 evalf_func(atan_evalf).
965 derivative_func(atan_deriv).
966 series_func(atan_series).
967 conjugate_func(atan_conjugate).
968 latex_name("\\arctan"));
971 // inverse tangent (atan2(y,x))
974 static ex atan2_evalf(const ex &y, const ex &x)
976 if (is_exactly_a<numeric>(y) && is_exactly_a<numeric>(x))
977 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
979 return atan2(y, x).hold();
982 static ex atan2_eval(const ex & y, const ex & x)
990 // atan2(0, x), x real and positive -> 0
991 if (x.info(info_flags::positive))
994 // atan2(0, x), x real and negative -> Pi
995 if (x.info(info_flags::negative))
1001 // atan2(y, 0), y real and positive -> Pi/2
1002 if (y.info(info_flags::positive))
1005 // atan2(y, 0), y real and negative -> -Pi/2
1006 if (y.info(info_flags::negative))
1010 if (y.is_equal(x)) {
1012 // atan2(y, y), y real and positive -> Pi/4
1013 if (y.info(info_flags::positive))
1016 // atan2(y, y), y real and negative -> -3/4*Pi
1017 if (y.info(info_flags::negative))
1018 return numeric(-3, 4)*Pi;
1021 if (y.is_equal(-x)) {
1023 // atan2(y, -y), y real and positive -> 3*Pi/4
1024 if (y.info(info_flags::positive))
1025 return numeric(3, 4)*Pi;
1027 // atan2(y, -y), y real and negative -> -Pi/4
1028 if (y.info(info_flags::negative))
1032 // atan2(float, float) -> float
1033 if (is_a<numeric>(y) && !y.info(info_flags::crational) &&
1034 is_a<numeric>(x) && !x.info(info_flags::crational))
1035 return atan(ex_to<numeric>(y), ex_to<numeric>(x));
1037 // atan2(real, real) -> atan(y/x) +/- Pi
1038 if (y.info(info_flags::real) && x.info(info_flags::real)) {
1039 if (x.info(info_flags::positive))
1042 if (x.info(info_flags::negative)) {
1043 if (y.info(info_flags::positive))
1044 return atan(y/x)+Pi;
1045 if (y.info(info_flags::negative))
1046 return atan(y/x)-Pi;
1050 return atan2(y, x).hold();
1053 static ex atan2_deriv(const ex & y, const ex & x, unsigned deriv_param)
1055 GINAC_ASSERT(deriv_param<2);
1057 if (deriv_param==0) {
1059 return x*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1062 return -y*power(power(x,_ex2)+power(y,_ex2),_ex_1);
1065 REGISTER_FUNCTION(atan2, eval_func(atan2_eval).
1066 evalf_func(atan2_evalf).
1067 derivative_func(atan2_deriv));
1070 // hyperbolic sine (trigonometric function)
1073 static ex sinh_evalf(const ex & x)
1075 if (is_exactly_a<numeric>(x))
1076 return sinh(ex_to<numeric>(x));
1078 return sinh(x).hold();
1081 static ex sinh_eval(const ex & x)
1083 if (x.info(info_flags::numeric)) {
1089 // sinh(float) -> float
1090 if (!x.info(info_flags::crational))
1091 return sinh(ex_to<numeric>(x));
1094 if (x.info(info_flags::negative))
1098 if ((x/Pi).info(info_flags::numeric) &&
1099 ex_to<numeric>(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
1102 if (is_exactly_a<function>(x)) {
1103 const ex &t = x.op(0);
1105 // sinh(asinh(x)) -> x
1106 if (is_ex_the_function(x, asinh))
1109 // sinh(acosh(x)) -> sqrt(x-1) * sqrt(x+1)
1110 if (is_ex_the_function(x, acosh))
1111 return sqrt(t-_ex1)*sqrt(t+_ex1);
1113 // sinh(atanh(x)) -> x/sqrt(1-x^2)
1114 if (is_ex_the_function(x, atanh))
1115 return t*power(_ex1-power(t,_ex2),_ex_1_2);
1118 return sinh(x).hold();
1121 static ex sinh_deriv(const ex & x, unsigned deriv_param)
1123 GINAC_ASSERT(deriv_param==0);
1125 // d/dx sinh(x) -> cosh(x)
1129 static ex sinh_real_part(const ex & x)
1131 return sinh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1134 static ex sinh_imag_part(const ex & x)
1136 return cosh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1139 static ex sinh_conjugate(const ex & x)
1141 // conjugate(sinh(x))==sinh(conjugate(x))
1142 return sinh(x.conjugate());
1145 REGISTER_FUNCTION(sinh, eval_func(sinh_eval).
1146 evalf_func(sinh_evalf).
1147 derivative_func(sinh_deriv).
1148 real_part_func(sinh_real_part).
1149 imag_part_func(sinh_imag_part).
1150 conjugate_func(sinh_conjugate).
1151 latex_name("\\sinh"));
1154 // hyperbolic cosine (trigonometric function)
1157 static ex cosh_evalf(const ex & x)
1159 if (is_exactly_a<numeric>(x))
1160 return cosh(ex_to<numeric>(x));
1162 return cosh(x).hold();
1165 static ex cosh_eval(const ex & x)
1167 if (x.info(info_flags::numeric)) {
1173 // cosh(float) -> float
1174 if (!x.info(info_flags::crational))
1175 return cosh(ex_to<numeric>(x));
1178 if (x.info(info_flags::negative))
1182 if ((x/Pi).info(info_flags::numeric) &&
1183 ex_to<numeric>(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
1186 if (is_exactly_a<function>(x)) {
1187 const ex &t = x.op(0);
1189 // cosh(acosh(x)) -> x
1190 if (is_ex_the_function(x, acosh))
1193 // cosh(asinh(x)) -> sqrt(1+x^2)
1194 if (is_ex_the_function(x, asinh))
1195 return sqrt(_ex1+power(t,_ex2));
1197 // cosh(atanh(x)) -> 1/sqrt(1-x^2)
1198 if (is_ex_the_function(x, atanh))
1199 return power(_ex1-power(t,_ex2),_ex_1_2);
1202 return cosh(x).hold();
1205 static ex cosh_deriv(const ex & x, unsigned deriv_param)
1207 GINAC_ASSERT(deriv_param==0);
1209 // d/dx cosh(x) -> sinh(x)
1213 static ex cosh_real_part(const ex & x)
1215 return cosh(GiNaC::real_part(x))*cos(GiNaC::imag_part(x));
1218 static ex cosh_imag_part(const ex & x)
1220 return sinh(GiNaC::real_part(x))*sin(GiNaC::imag_part(x));
1223 static ex cosh_conjugate(const ex & x)
1225 // conjugate(cosh(x))==cosh(conjugate(x))
1226 return cosh(x.conjugate());
1229 REGISTER_FUNCTION(cosh, eval_func(cosh_eval).
1230 evalf_func(cosh_evalf).
1231 derivative_func(cosh_deriv).
1232 real_part_func(cosh_real_part).
1233 imag_part_func(cosh_imag_part).
1234 conjugate_func(cosh_conjugate).
1235 latex_name("\\cosh"));
1238 // hyperbolic tangent (trigonometric function)
1241 static ex tanh_evalf(const ex & x)
1243 if (is_exactly_a<numeric>(x))
1244 return tanh(ex_to<numeric>(x));
1246 return tanh(x).hold();
1249 static ex tanh_eval(const ex & x)
1251 if (x.info(info_flags::numeric)) {
1257 // tanh(float) -> float
1258 if (!x.info(info_flags::crational))
1259 return tanh(ex_to<numeric>(x));
1262 if (x.info(info_flags::negative))
1266 if ((x/Pi).info(info_flags::numeric) &&
1267 ex_to<numeric>(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
1270 if (is_exactly_a<function>(x)) {
1271 const ex &t = x.op(0);
1273 // tanh(atanh(x)) -> x
1274 if (is_ex_the_function(x, atanh))
1277 // tanh(asinh(x)) -> x/sqrt(1+x^2)
1278 if (is_ex_the_function(x, asinh))
1279 return t*power(_ex1+power(t,_ex2),_ex_1_2);
1281 // tanh(acosh(x)) -> sqrt(x-1)*sqrt(x+1)/x
1282 if (is_ex_the_function(x, acosh))
1283 return sqrt(t-_ex1)*sqrt(t+_ex1)*power(t,_ex_1);
1286 return tanh(x).hold();
1289 static ex tanh_deriv(const ex & x, unsigned deriv_param)
1291 GINAC_ASSERT(deriv_param==0);
1293 // d/dx tanh(x) -> 1-tanh(x)^2
1294 return _ex1-power(tanh(x),_ex2);
1297 static ex tanh_series(const ex &x,
1298 const relational &rel,
1302 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1304 // Taylor series where there is no pole falls back to tanh_deriv.
1305 // On a pole simply expand sinh(x)/cosh(x).
1306 const ex x_pt = x.subs(rel, subs_options::no_pattern);
1307 if (!(2*I*x_pt/Pi).info(info_flags::odd))
1308 throw do_taylor(); // caught by function::series()
1309 // if we got here we have to care for a simple pole
1310 return (sinh(x)/cosh(x)).series(rel, order, options);
1313 static ex tanh_real_part(const ex & x)
1315 ex a = GiNaC::real_part(x);
1316 ex b = GiNaC::imag_part(x);
1317 return tanh(a)/(1+power(tanh(a),2)*power(tan(b),2));
1320 static ex tanh_imag_part(const ex & x)
1322 ex a = GiNaC::real_part(x);
1323 ex b = GiNaC::imag_part(x);
1324 return tan(b)/(1+power(tanh(a),2)*power(tan(b),2));
1327 static ex tanh_conjugate(const ex & x)
1329 // conjugate(tanh(x))==tanh(conjugate(x))
1330 return tanh(x.conjugate());
1333 REGISTER_FUNCTION(tanh, eval_func(tanh_eval).
1334 evalf_func(tanh_evalf).
1335 derivative_func(tanh_deriv).
1336 series_func(tanh_series).
1337 real_part_func(tanh_real_part).
1338 imag_part_func(tanh_imag_part).
1339 conjugate_func(tanh_conjugate).
1340 latex_name("\\tanh"));
1343 // inverse hyperbolic sine (trigonometric function)
1346 static ex asinh_evalf(const ex & x)
1348 if (is_exactly_a<numeric>(x))
1349 return asinh(ex_to<numeric>(x));
1351 return asinh(x).hold();
1354 static ex asinh_eval(const ex & x)
1356 if (x.info(info_flags::numeric)) {
1362 // asinh(float) -> float
1363 if (!x.info(info_flags::crational))
1364 return asinh(ex_to<numeric>(x));
1367 if (x.info(info_flags::negative))
1371 return asinh(x).hold();
1374 static ex asinh_deriv(const ex & x, unsigned deriv_param)
1376 GINAC_ASSERT(deriv_param==0);
1378 // d/dx asinh(x) -> 1/sqrt(1+x^2)
1379 return power(_ex1+power(x,_ex2),_ex_1_2);
1382 static ex asinh_conjugate(const ex & x)
1384 // conjugate(asinh(x))==asinh(conjugate(x)) unless on the branch cuts which
1385 // run along the imaginary axis outside the interval [-I, +I].
1386 if (x.info(info_flags::real))
1388 if (is_exactly_a<numeric>(x)) {
1389 const numeric x_re = ex_to<numeric>(x.real_part());
1390 const numeric x_im = ex_to<numeric>(x.imag_part());
1391 if (!x_re.is_zero() ||
1392 (x_im > *_num_1_p && x_im < *_num1_p))
1393 return asinh(x.conjugate());
1395 return conjugate_function(asinh(x)).hold();
1398 REGISTER_FUNCTION(asinh, eval_func(asinh_eval).
1399 evalf_func(asinh_evalf).
1400 derivative_func(asinh_deriv).
1401 conjugate_func(asinh_conjugate));
1404 // inverse hyperbolic cosine (trigonometric function)
1407 static ex acosh_evalf(const ex & x)
1409 if (is_exactly_a<numeric>(x))
1410 return acosh(ex_to<numeric>(x));
1412 return acosh(x).hold();
1415 static ex acosh_eval(const ex & x)
1417 if (x.info(info_flags::numeric)) {
1419 // acosh(0) -> Pi*I/2
1421 return Pi*I*numeric(1,2);
1424 if (x.is_equal(_ex1))
1427 // acosh(-1) -> Pi*I
1428 if (x.is_equal(_ex_1))
1431 // acosh(float) -> float
1432 if (!x.info(info_flags::crational))
1433 return acosh(ex_to<numeric>(x));
1435 // acosh(-x) -> Pi*I-acosh(x)
1436 if (x.info(info_flags::negative))
1437 return Pi*I-acosh(-x);
1440 return acosh(x).hold();
1443 static ex acosh_deriv(const ex & x, unsigned deriv_param)
1445 GINAC_ASSERT(deriv_param==0);
1447 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
1448 return power(x+_ex_1,_ex_1_2)*power(x+_ex1,_ex_1_2);
1451 static ex acosh_conjugate(const ex & x)
1453 // conjugate(acosh(x))==acosh(conjugate(x)) unless on the branch cut
1454 // which runs along the real axis from +1 to -inf.
1455 if (is_exactly_a<numeric>(x) &&
1456 (!x.imag_part().is_zero() || x > *_num1_p)) {
1457 return acosh(x.conjugate());
1459 return conjugate_function(acosh(x)).hold();
1462 REGISTER_FUNCTION(acosh, eval_func(acosh_eval).
1463 evalf_func(acosh_evalf).
1464 derivative_func(acosh_deriv).
1465 conjugate_func(acosh_conjugate));
1468 // inverse hyperbolic tangent (trigonometric function)
1471 static ex atanh_evalf(const ex & x)
1473 if (is_exactly_a<numeric>(x))
1474 return atanh(ex_to<numeric>(x));
1476 return atanh(x).hold();
1479 static ex atanh_eval(const ex & x)
1481 if (x.info(info_flags::numeric)) {
1487 // atanh({+|-}1) -> throw
1488 if (x.is_equal(_ex1) || x.is_equal(_ex_1))
1489 throw (pole_error("atanh_eval(): logarithmic pole",0));
1491 // atanh(float) -> float
1492 if (!x.info(info_flags::crational))
1493 return atanh(ex_to<numeric>(x));
1496 if (x.info(info_flags::negative))
1500 return atanh(x).hold();
1503 static ex atanh_deriv(const ex & x, unsigned deriv_param)
1505 GINAC_ASSERT(deriv_param==0);
1507 // d/dx atanh(x) -> 1/(1-x^2)
1508 return power(_ex1-power(x,_ex2),_ex_1);
1511 static ex atanh_series(const ex &arg,
1512 const relational &rel,
1516 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
1518 // Taylor series where there is no pole or cut falls back to atanh_deriv.
1519 // There are two branch cuts, one runnig from 1 up the real axis and one
1520 // one running from -1 down the real axis. The points 1 and -1 are poles
1521 // On the branch cuts and the poles series expand
1522 // (log(1+x)-log(1-x))/2
1524 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
1525 if (!(arg_pt).info(info_flags::real))
1526 throw do_taylor(); // Im(x) != 0
1527 if ((arg_pt).info(info_flags::real) && abs(arg_pt)<_ex1)
1528 throw do_taylor(); // Im(x) == 0, but abs(x)<1
1529 // care for the poles, using the defining formula for atanh()...
1530 if (arg_pt.is_equal(_ex1) || arg_pt.is_equal(_ex_1))
1531 return ((log(_ex1+arg)-log(_ex1-arg))*_ex1_2).series(rel, order, options);
1532 // ...and the branch cuts (the discontinuity at the cut being just I*Pi)
1533 if (!(options & series_options::suppress_branchcut)) {
1535 // This is the branch cut: assemble the primitive series manually and
1536 // then add the corresponding complex step function.
1537 const symbol &s = ex_to<symbol>(rel.lhs());
1538 const ex &point = rel.rhs();
1540 const ex replarg = series(atanh(arg), s==foo, order).subs(foo==point, subs_options::no_pattern);
1541 ex Order0correction = replarg.op(0)+csgn(I*arg)*Pi*I*_ex1_2;
1543 Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2;
1545 Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2;
1547 seq.push_back(expair(Order0correction, _ex0));
1548 seq.push_back(expair(Order(_ex1), order));
1549 return series(replarg - pseries(rel, seq), rel, order);
1554 static ex atanh_conjugate(const ex & x)
1556 // conjugate(atanh(x))==atanh(conjugate(x)) unless on the branch cuts which
1557 // run along the real axis outside the interval [-1, +1].
1558 if (is_exactly_a<numeric>(x) &&
1559 (!x.imag_part().is_zero() || (x > *_num_1_p && x < *_num1_p))) {
1560 return atanh(x.conjugate());
1562 return conjugate_function(atanh(x)).hold();
1565 REGISTER_FUNCTION(atanh, eval_func(atanh_eval).
1566 evalf_func(atanh_evalf).
1567 derivative_func(atanh_deriv).
1568 series_func(atanh_series).
1569 conjugate_func(atanh_conjugate));
1572 } // namespace GiNaC