1 /** @file inifcns_trans.cpp
3 * Implementation of transcendental (and trigonometric and hyperbolic)
7 * GiNaC Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "relational.h"
36 #ifndef NO_GINAC_NAMESPACE
38 #endif // ndef NO_GINAC_NAMESPACE
41 // exponential function
44 static ex exp_evalf(const ex & x)
50 return exp(ex_to_numeric(x)); // -> numeric exp(numeric)
53 static ex exp_eval(const ex & x)
59 // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
60 ex TwoExOverPiI=(_ex2()*x)/(Pi*I);
61 if (TwoExOverPiI.info(info_flags::integer)) {
62 numeric z=mod(ex_to_numeric(TwoExOverPiI),_num4());
63 if (z.is_equal(_num0()))
65 if (z.is_equal(_num1()))
67 if (z.is_equal(_num2()))
69 if (z.is_equal(_num3()))
73 if (is_ex_the_function(x, log))
77 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
83 static ex exp_diff(const ex & x, unsigned diff_param)
85 GINAC_ASSERT(diff_param==0);
87 // d/dx exp(x) -> exp(x)
91 REGISTER_FUNCTION(exp, exp_eval, exp_evalf, exp_diff, NULL);
97 static ex log_evalf(const ex & x)
101 END_TYPECHECK(log(x))
103 return log(ex_to_numeric(x)); // -> numeric log(numeric)
106 static ex log_eval(const ex & x)
108 if (x.info(info_flags::numeric)) {
109 if (x.is_equal(_ex1())) // log(1) -> 0
111 if (x.is_equal(_ex_1())) // log(-1) -> I*Pi
113 if (x.is_equal(I)) // log(I) -> Pi*I/2
114 return (Pi*I*_num1_2());
115 if (x.is_equal(-I)) // log(-I) -> -Pi*I/2
116 return (Pi*I*_num_1_2());
117 if (x.is_equal(_ex0())) // log(0) -> infinity
118 throw(std::domain_error("log_eval(): log(0)"));
120 if (!x.info(info_flags::crational))
123 // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
124 if (is_ex_the_function(x, exp)) {
126 if (t.info(info_flags::numeric)) {
127 numeric nt = ex_to_numeric(t);
133 return log(x).hold();
136 static ex log_diff(const ex & x, unsigned diff_param)
138 GINAC_ASSERT(diff_param==0);
140 // d/dx log(x) -> 1/x
141 return power(x, _ex_1());
144 REGISTER_FUNCTION(log, log_eval, log_evalf, log_diff, NULL);
147 // sine (trigonometric function)
150 static ex sin_evalf(const ex & x)
154 END_TYPECHECK(sin(x))
156 return sin(ex_to_numeric(x)); // -> numeric sin(numeric)
159 static ex sin_eval(const ex & x)
161 // sin(n/d*Pi) -> { all known non-nested radicals }
162 ex SixtyExOverPi = _ex60()*x/Pi;
164 if (SixtyExOverPi.info(info_flags::integer)) {
165 numeric z = mod(ex_to_numeric(SixtyExOverPi),_num120());
167 // wrap to interval [0, Pi)
172 // wrap to interval [0, Pi/2)
175 if (z.is_equal(_num0())) // sin(0) -> 0
177 if (z.is_equal(_num5())) // sin(Pi/12) -> sqrt(6)/4*(1-sqrt(3)/3)
178 return sign*_ex1_4()*power(_ex6(),_ex1_2())*(_ex1()+_ex_1_3()*power(_ex3(),_ex1_2()));
179 if (z.is_equal(_num6())) // sin(Pi/10) -> sqrt(5)/4-1/4
180 return sign*(_ex1_4()*power(_ex5(),_ex1_2())+_ex_1_4());
181 if (z.is_equal(_num10())) // sin(Pi/6) -> 1/2
182 return sign*_ex1_2();
183 if (z.is_equal(_num15())) // sin(Pi/4) -> sqrt(2)/2
184 return sign*_ex1_2()*power(_ex2(),_ex1_2());
185 if (z.is_equal(_num18())) // sin(3/10*Pi) -> sqrt(5)/4+1/4
186 return sign*(_ex1_4()*power(_ex5(),_ex1_2())+_ex1_4());
187 if (z.is_equal(_num20())) // sin(Pi/3) -> sqrt(3)/2
188 return sign*_ex1_2()*power(_ex3(),_ex1_2());
189 if (z.is_equal(_num25())) // sin(5/12*Pi) -> sqrt(6)/4*(1+sqrt(3)/3)
190 return sign*_ex1_4()*power(_ex6(),_ex1_2())*(_ex1()+_ex1_3()*power(_ex3(),_ex1_2()));
191 if (z.is_equal(_num30())) // sin(Pi/2) -> 1
195 if (is_ex_exactly_of_type(x, function)) {
198 if (is_ex_the_function(x, asin))
200 // sin(acos(x)) -> sqrt(1-x^2)
201 if (is_ex_the_function(x, acos))
202 return power(_ex1()-power(t,_ex2()),_ex1_2());
203 // sin(atan(x)) -> x*(1+x^2)^(-1/2)
204 if (is_ex_the_function(x, atan))
205 return t*power(_ex1()+power(t,_ex2()),_ex_1_2());
208 // sin(float) -> float
209 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
212 return sin(x).hold();
215 static ex sin_diff(const ex & x, unsigned diff_param)
217 GINAC_ASSERT(diff_param==0);
219 // d/dx sin(x) -> cos(x)
223 REGISTER_FUNCTION(sin, sin_eval, sin_evalf, sin_diff, NULL);
226 // cosine (trigonometric function)
229 static ex cos_evalf(const ex & x)
233 END_TYPECHECK(cos(x))
235 return cos(ex_to_numeric(x)); // -> numeric cos(numeric)
238 static ex cos_eval(const ex & x)
240 // cos(n/d*Pi) -> { all known non-nested radicals }
241 ex SixtyExOverPi = _ex60()*x/Pi;
243 if (SixtyExOverPi.info(info_flags::integer)) {
244 numeric z = mod(ex_to_numeric(SixtyExOverPi),_num120());
246 // wrap to interval [0, Pi)
250 // wrap to interval [0, Pi/2)
254 if (z.is_equal(_num0())) // cos(0) -> 1
256 if (z.is_equal(_num5())) // cos(Pi/12) -> sqrt(6)/4*(1+sqrt(3)/3)
257 return sign*_ex1_4()*power(_ex6(),_ex1_2())*(_ex1()+_ex1_3()*power(_ex3(),_ex1_2()));
258 if (z.is_equal(_num10())) // cos(Pi/6) -> sqrt(3)/2
259 return sign*_ex1_2()*power(_ex3(),_ex1_2());
260 if (z.is_equal(_num12())) // cos(Pi/5) -> sqrt(5)/4+1/4
261 return sign*(_ex1_4()*power(_ex5(),_ex1_2())+_ex1_4());
262 if (z.is_equal(_num15())) // cos(Pi/4) -> sqrt(2)/2
263 return sign*_ex1_2()*power(_ex2(),_ex1_2());
264 if (z.is_equal(_num20())) // cos(Pi/3) -> 1/2
265 return sign*_ex1_2();
266 if (z.is_equal(_num24())) // cos(2/5*Pi) -> sqrt(5)/4-1/4x
267 return sign*(_ex1_4()*power(_ex5(),_ex1_2())+_ex_1_4());
268 if (z.is_equal(_num25())) // cos(5/12*Pi) -> sqrt(6)/4*(1-sqrt(3)/3)
269 return sign*_ex1_4()*power(_ex6(),_ex1_2())*(_ex1()+_ex_1_3()*power(_ex3(),_ex1_2()));
270 if (z.is_equal(_num30())) // cos(Pi/2) -> 0
274 if (is_ex_exactly_of_type(x, function)) {
277 if (is_ex_the_function(x, acos))
279 // cos(asin(x)) -> (1-x^2)^(1/2)
280 if (is_ex_the_function(x, asin))
281 return power(_ex1()-power(t,_ex2()),_ex1_2());
282 // cos(atan(x)) -> (1+x^2)^(-1/2)
283 if (is_ex_the_function(x, atan))
284 return power(_ex1()+power(t,_ex2()),_ex_1_2());
287 // cos(float) -> float
288 if (x.info(info_flags::numeric) && !x.info(info_flags::crational))
291 return cos(x).hold();
294 static ex cos_diff(const ex & x, unsigned diff_param)
296 GINAC_ASSERT(diff_param==0);
298 // d/dx cos(x) -> -sin(x)
299 return _ex_1()*sin(x);
302 REGISTER_FUNCTION(cos, cos_eval, cos_evalf, cos_diff, NULL);
305 // tangent (trigonometric function)
308 static ex tan_evalf(const ex & x)
312 END_TYPECHECK(tan(x)) // -> numeric tan(numeric)
314 return tan(ex_to_numeric(x));
317 static ex tan_eval(const ex & x)
319 // tan(n/d*Pi) -> { all known non-nested radicals }
320 ex SixtyExOverPi = _ex60()*x/Pi;
322 if (SixtyExOverPi.info(info_flags::integer)) {
323 numeric z = mod(ex_to_numeric(SixtyExOverPi),_num60());
325 // wrap to interval [0, Pi)
329 // wrap to interval [0, Pi/2)
333 if (z.is_equal(_num0())) // tan(0) -> 0
335 if (z.is_equal(_num5())) // tan(Pi/12) -> 2-sqrt(3)
336 return sign*(_ex2()-power(_ex3(),_ex1_2()));
337 if (z.is_equal(_num10())) // tan(Pi/6) -> sqrt(3)/3
338 return sign*_ex1_3()*power(_ex3(),_ex1_2());
339 if (z.is_equal(_num15())) // tan(Pi/4) -> 1
341 if (z.is_equal(_num20())) // tan(Pi/3) -> sqrt(3)
342 return sign*power(_ex3(),_ex1_2());
343 if (z.is_equal(_num25())) // tan(5/12*Pi) -> 2+sqrt(3)
344 return sign*(power(_ex3(),_ex1_2())+_ex2());
345 if (z.is_equal(_num30())) // tan(Pi/2) -> infinity
346 throw (std::domain_error("tan_eval(): infinity"));
349 if (is_ex_exactly_of_type(x, function)) {
352 if (is_ex_the_function(x, atan))
354 // tan(asin(x)) -> x*(1+x^2)^(-1/2)
355 if (is_ex_the_function(x, asin))
356 return t*power(_ex1()-power(t,_ex2()),_ex_1_2());
357 // tan(acos(x)) -> (1-x^2)^(1/2)/x
358 if (is_ex_the_function(x, acos))
359 return power(t,_ex_1())*power(_ex1()-power(t,_ex2()),_ex1_2());
362 // tan(float) -> float
363 if (x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
367 return tan(x).hold();
370 static ex tan_diff(const ex & x, unsigned diff_param)
372 GINAC_ASSERT(diff_param==0);
374 // d/dx tan(x) -> 1+tan(x)^2;
375 return (_ex1()+power(tan(x),_ex2()));
378 static ex tan_series(const ex & x, const symbol & s, const ex & pt, int order)
381 // Taylor series where there is no pole falls back to tan_diff.
382 // On a pole simply expand sin(x)/cos(x).
383 const ex x_pt = x.subs(s==pt);
384 if (!(2*x_pt/Pi).info(info_flags::odd))
385 throw do_taylor(); // caught by function::series()
386 // if we got here we have to care for a simple pole
387 return (sin(x)/cos(x)).series(s, pt, order+2);
390 REGISTER_FUNCTION(tan, tan_eval, tan_evalf, tan_diff, tan_series);
393 // inverse sine (arc sine)
396 static ex asin_evalf(const ex & x)
400 END_TYPECHECK(asin(x))
402 return asin(ex_to_numeric(x)); // -> numeric asin(numeric)
405 static ex asin_eval(const ex & x)
407 if (x.info(info_flags::numeric)) {
412 if (x.is_equal(_ex1_2()))
413 return numeric(1,6)*Pi;
415 if (x.is_equal(_ex1()))
417 // asin(-1/2) -> -Pi/6
418 if (x.is_equal(_ex_1_2()))
419 return numeric(-1,6)*Pi;
421 if (x.is_equal(_ex_1()))
422 return _num_1_2()*Pi;
423 // asin(float) -> float
424 if (!x.info(info_flags::crational))
425 return asin_evalf(x);
428 return asin(x).hold();
431 static ex asin_diff(const ex & x, unsigned diff_param)
433 GINAC_ASSERT(diff_param==0);
435 // d/dx asin(x) -> 1/sqrt(1-x^2)
436 return power(1-power(x,_ex2()),_ex_1_2());
439 REGISTER_FUNCTION(asin, asin_eval, asin_evalf, asin_diff, NULL);
442 // inverse cosine (arc cosine)
445 static ex acos_evalf(const ex & x)
449 END_TYPECHECK(acos(x))
451 return acos(ex_to_numeric(x)); // -> numeric acos(numeric)
454 static ex acos_eval(const ex & x)
456 if (x.info(info_flags::numeric)) {
458 if (x.is_equal(_ex1()))
461 if (x.is_equal(_ex1_2()))
466 // acos(-1/2) -> 2/3*Pi
467 if (x.is_equal(_ex_1_2()))
468 return numeric(2,3)*Pi;
470 if (x.is_equal(_ex_1()))
472 // acos(float) -> float
473 if (!x.info(info_flags::crational))
474 return acos_evalf(x);
477 return acos(x).hold();
480 static ex acos_diff(const ex & x, unsigned diff_param)
482 GINAC_ASSERT(diff_param==0);
484 // d/dx acos(x) -> -1/sqrt(1-x^2)
485 return _ex_1()*power(1-power(x,_ex2()),_ex_1_2());
488 REGISTER_FUNCTION(acos, acos_eval, acos_evalf, acos_diff, NULL);
491 // inverse tangent (arc tangent)
494 static ex atan_evalf(const ex & x)
498 END_TYPECHECK(atan(x))
500 return atan(ex_to_numeric(x)); // -> numeric atan(numeric)
503 static ex atan_eval(const ex & x)
505 if (x.info(info_flags::numeric)) {
507 if (x.is_equal(_ex0()))
509 // atan(float) -> float
510 if (!x.info(info_flags::crational))
511 return atan_evalf(x);
514 return atan(x).hold();
517 static ex atan_diff(const ex & x, unsigned diff_param)
519 GINAC_ASSERT(diff_param==0);
521 // d/dx atan(x) -> 1/(1+x^2)
522 return power(_ex1()+power(x,_ex2()), _ex_1());
525 REGISTER_FUNCTION(atan, atan_eval, atan_evalf, atan_diff, NULL);
528 // inverse tangent (atan2(y,x))
531 static ex atan2_evalf(const ex & y, const ex & x)
536 END_TYPECHECK(atan2(y,x))
538 return atan(ex_to_numeric(y),ex_to_numeric(x)); // -> numeric atan(numeric)
541 static ex atan2_eval(const ex & y, const ex & x)
543 if (y.info(info_flags::numeric) && !y.info(info_flags::crational) &&
544 x.info(info_flags::numeric) && !x.info(info_flags::crational)) {
545 return atan2_evalf(y,x);
548 return atan2(y,x).hold();
551 static ex atan2_diff(const ex & y, const ex & x, unsigned diff_param)
553 GINAC_ASSERT(diff_param<2);
557 return x*power(power(x,_ex2())+power(y,_ex2()),_ex_1());
560 return -y*power(power(x,_ex2())+power(y,_ex2()),_ex_1());
563 REGISTER_FUNCTION(atan2, atan2_eval, atan2_evalf, atan2_diff, NULL);
566 // hyperbolic sine (trigonometric function)
569 static ex sinh_evalf(const ex & x)
573 END_TYPECHECK(sinh(x))
575 return sinh(ex_to_numeric(x)); // -> numeric sinh(numeric)
578 static ex sinh_eval(const ex & x)
580 if (x.info(info_flags::numeric)) {
581 if (x.is_zero()) // sinh(0) -> 0
583 if (!x.info(info_flags::crational)) // sinh(float) -> float
584 return sinh_evalf(x);
587 if ((x/Pi).info(info_flags::numeric) &&
588 ex_to_numeric(x/Pi).real().is_zero()) // sinh(I*x) -> I*sin(x)
591 if (is_ex_exactly_of_type(x, function)) {
593 // sinh(asinh(x)) -> x
594 if (is_ex_the_function(x, asinh))
596 // sinh(acosh(x)) -> (x-1)^(1/2) * (x+1)^(1/2)
597 if (is_ex_the_function(x, acosh))
598 return power(t-_ex1(),_ex1_2())*power(t+_ex1(),_ex1_2());
599 // sinh(atanh(x)) -> x*(1-x^2)^(-1/2)
600 if (is_ex_the_function(x, atanh))
601 return t*power(_ex1()-power(t,_ex2()),_ex_1_2());
604 return sinh(x).hold();
607 static ex sinh_diff(const ex & x, unsigned diff_param)
609 GINAC_ASSERT(diff_param==0);
611 // d/dx sinh(x) -> cosh(x)
615 REGISTER_FUNCTION(sinh, sinh_eval, sinh_evalf, sinh_diff, NULL);
618 // hyperbolic cosine (trigonometric function)
621 static ex cosh_evalf(const ex & x)
625 END_TYPECHECK(cosh(x))
627 return cosh(ex_to_numeric(x)); // -> numeric cosh(numeric)
630 static ex cosh_eval(const ex & x)
632 if (x.info(info_flags::numeric)) {
633 if (x.is_zero()) // cosh(0) -> 1
635 if (!x.info(info_flags::crational)) // cosh(float) -> float
636 return cosh_evalf(x);
639 if ((x/Pi).info(info_flags::numeric) &&
640 ex_to_numeric(x/Pi).real().is_zero()) // cosh(I*x) -> cos(x)
643 if (is_ex_exactly_of_type(x, function)) {
645 // cosh(acosh(x)) -> x
646 if (is_ex_the_function(x, acosh))
648 // cosh(asinh(x)) -> (1+x^2)^(1/2)
649 if (is_ex_the_function(x, asinh))
650 return power(_ex1()+power(t,_ex2()),_ex1_2());
651 // cosh(atanh(x)) -> (1-x^2)^(-1/2)
652 if (is_ex_the_function(x, atanh))
653 return power(_ex1()-power(t,_ex2()),_ex_1_2());
656 return cosh(x).hold();
659 static ex cosh_diff(const ex & x, unsigned diff_param)
661 GINAC_ASSERT(diff_param==0);
663 // d/dx cosh(x) -> sinh(x)
667 REGISTER_FUNCTION(cosh, cosh_eval, cosh_evalf, cosh_diff, NULL);
670 // hyperbolic tangent (trigonometric function)
673 static ex tanh_evalf(const ex & x)
677 END_TYPECHECK(tanh(x))
679 return tanh(ex_to_numeric(x)); // -> numeric tanh(numeric)
682 static ex tanh_eval(const ex & x)
684 if (x.info(info_flags::numeric)) {
685 if (x.is_zero()) // tanh(0) -> 0
687 if (!x.info(info_flags::crational)) // tanh(float) -> float
688 return tanh_evalf(x);
691 if ((x/Pi).info(info_flags::numeric) &&
692 ex_to_numeric(x/Pi).real().is_zero()) // tanh(I*x) -> I*tan(x);
695 if (is_ex_exactly_of_type(x, function)) {
697 // tanh(atanh(x)) -> x
698 if (is_ex_the_function(x, atanh))
700 // tanh(asinh(x)) -> x*(1+x^2)^(-1/2)
701 if (is_ex_the_function(x, asinh))
702 return t*power(_ex1()+power(t,_ex2()),_ex_1_2());
703 // tanh(acosh(x)) -> (x-1)^(1/2)*(x+1)^(1/2)/x
704 if (is_ex_the_function(x, acosh))
705 return power(t-_ex1(),_ex1_2())*power(t+_ex1(),_ex1_2())*power(t,_ex_1());
708 return tanh(x).hold();
711 static ex tanh_diff(const ex & x, unsigned diff_param)
713 GINAC_ASSERT(diff_param==0);
715 // d/dx tanh(x) -> 1-tanh(x)^2
716 return _ex1()-power(tanh(x),_ex2());
719 static ex tanh_series(const ex & x, const symbol & s, const ex & pt, int order)
722 // Taylor series where there is no pole falls back to tanh_diff.
723 // On a pole simply expand sinh(x)/cosh(x).
724 const ex x_pt = x.subs(s==pt);
725 if (!(2*I*x_pt/Pi).info(info_flags::odd))
726 throw do_taylor(); // caught by function::series()
727 // if we got here we have to care for a simple pole
728 return (sinh(x)/cosh(x)).series(s, pt, order+2);
731 REGISTER_FUNCTION(tanh, tanh_eval, tanh_evalf, tanh_diff, tanh_series);
734 // inverse hyperbolic sine (trigonometric function)
737 static ex asinh_evalf(const ex & x)
741 END_TYPECHECK(asinh(x))
743 return asinh(ex_to_numeric(x)); // -> numeric asinh(numeric)
746 static ex asinh_eval(const ex & x)
748 if (x.info(info_flags::numeric)) {
752 // asinh(float) -> float
753 if (!x.info(info_flags::crational))
754 return asinh_evalf(x);
757 return asinh(x).hold();
760 static ex asinh_diff(const ex & x, unsigned diff_param)
762 GINAC_ASSERT(diff_param==0);
764 // d/dx asinh(x) -> 1/sqrt(1+x^2)
765 return power(_ex1()+power(x,_ex2()),_ex_1_2());
768 REGISTER_FUNCTION(asinh, asinh_eval, asinh_evalf, asinh_diff, NULL);
771 // inverse hyperbolic cosine (trigonometric function)
774 static ex acosh_evalf(const ex & x)
778 END_TYPECHECK(acosh(x))
780 return acosh(ex_to_numeric(x)); // -> numeric acosh(numeric)
783 static ex acosh_eval(const ex & x)
785 if (x.info(info_flags::numeric)) {
786 // acosh(0) -> Pi*I/2
788 return Pi*I*numeric(1,2);
790 if (x.is_equal(_ex1()))
793 if (x.is_equal(_ex_1()))
795 // acosh(float) -> float
796 if (!x.info(info_flags::crational))
797 return acosh_evalf(x);
800 return acosh(x).hold();
803 static ex acosh_diff(const ex & x, unsigned diff_param)
805 GINAC_ASSERT(diff_param==0);
807 // d/dx acosh(x) -> 1/(sqrt(x-1)*sqrt(x+1))
808 return power(x+_ex_1(),_ex_1_2())*power(x+_ex1(),_ex_1_2());
811 REGISTER_FUNCTION(acosh, acosh_eval, acosh_evalf, acosh_diff, NULL);
814 // inverse hyperbolic tangent (trigonometric function)
817 static ex atanh_evalf(const ex & x)
821 END_TYPECHECK(atanh(x))
823 return atanh(ex_to_numeric(x)); // -> numeric atanh(numeric)
826 static ex atanh_eval(const ex & x)
828 if (x.info(info_flags::numeric)) {
832 // atanh({+|-}1) -> throw
833 if (x.is_equal(_ex1()) || x.is_equal(_ex1()))
834 throw (std::domain_error("atanh_eval(): infinity"));
835 // atanh(float) -> float
836 if (!x.info(info_flags::crational))
837 return atanh_evalf(x);
840 return atanh(x).hold();
843 static ex atanh_diff(const ex & x, unsigned diff_param)
845 GINAC_ASSERT(diff_param==0);
847 // d/dx atanh(x) -> 1/(1-x^2)
848 return power(_ex1()-power(x,_ex2()),_ex_1());
851 REGISTER_FUNCTION(atanh, atanh_eval, atanh_evalf, atanh_diff, NULL);
853 #ifndef NO_GINAC_NAMESPACE
855 #endif // ndef NO_GINAC_NAMESPACE