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
33 #ifndef NO_GINAC_NAMESPACE
35 #endif // ndef NO_GINAC_NAMESPACE
38 // exponential function
41 static ex exp_evalf(ex const & x)
47 return exp(ex_to_numeric(x)); // -> numeric exp(numeric)
50 static ex exp_eval(ex const & x)
56 // exp(n*Pi*I/2) -> {+1|+I|-1|-I}
57 ex TwoExOverPiI=(2*x)/(Pi*I);
58 if (TwoExOverPiI.info(info_flags::integer)) {
59 numeric z=mod(ex_to_numeric(TwoExOverPiI),numeric(4));
60 if (z.is_equal(numZERO()))
62 if (z.is_equal(numONE()))
64 if (z.is_equal(numTWO()))
66 if (z.is_equal(numTHREE()))
70 if (is_ex_the_function(x, log))
74 if (x.info(info_flags::numeric) && !x.info(info_flags::rational))
80 static ex exp_diff(ex const & x, unsigned diff_param)
82 GINAC_ASSERT(diff_param==0);
87 REGISTER_FUNCTION(exp, exp_eval, exp_evalf, exp_diff, NULL);
93 static ex log_evalf(ex const & x)
99 return log(ex_to_numeric(x)); // -> numeric log(numeric)
102 static ex log_eval(ex const & x)
104 if (x.info(info_flags::numeric)) {
106 if (x.is_equal(exONE()))
109 if (x.is_equal(exMINUSONE()))
113 return (I*Pi*numeric(1,2));
114 // log(-I) -> -Pi*I/2
116 return (I*Pi*numeric(-1,2));
117 // log(0) -> throw singularity
118 if (x.is_equal(exZERO()))
119 throw(std::domain_error("log_eval(): log(0)"));
121 if (!x.info(info_flags::rational))
125 return log(x).hold();
128 static ex log_diff(ex const & x, unsigned diff_param)
130 GINAC_ASSERT(diff_param==0);
135 REGISTER_FUNCTION(log, log_eval, log_evalf, log_diff, NULL);
138 // sine (trigonometric function)
141 static ex sin_evalf(ex const & x)
145 END_TYPECHECK(sin(x))
147 return sin(ex_to_numeric(x)); // -> numeric sin(numeric)
150 static ex sin_eval(ex const & x)
154 if (xOverPi.info(info_flags::integer))
157 // sin((2n+1)*Pi/2) -> {+|-}1
158 ex xOverPiMinusHalf=xOverPi-exHALF();
159 if (xOverPiMinusHalf.info(info_flags::even))
161 else if (xOverPiMinusHalf.info(info_flags::odd))
164 if (is_ex_exactly_of_type(x, function)) {
167 if (is_ex_the_function(x, asin))
169 // sin(acos(x)) -> (1-x^2)^(1/2)
170 if (is_ex_the_function(x, acos))
171 return power(exONE()-power(t,exTWO()),exHALF());
172 // sin(atan(x)) -> x*(1+x^2)^(-1/2)
173 if (is_ex_the_function(x, atan))
174 return t*power(exONE()+power(t,exTWO()),exMINUSHALF());
177 // sin(float) -> float
178 if (x.info(info_flags::numeric) && !x.info(info_flags::rational))
181 return sin(x).hold();
184 static ex sin_diff(ex const & x, unsigned diff_param)
186 GINAC_ASSERT(diff_param==0);
191 REGISTER_FUNCTION(sin, sin_eval, sin_evalf, sin_diff, NULL);
194 // cosine (trigonometric function)
197 static ex cos_evalf(ex const & x)
201 END_TYPECHECK(cos(x))
203 return cos(ex_to_numeric(x)); // -> numeric cos(numeric)
206 static ex cos_eval(ex const & x)
208 // cos(n*Pi) -> {+|-}1
210 if (xOverPi.info(info_flags::even))
212 else if (xOverPi.info(info_flags::odd))
215 // cos((2n+1)*Pi/2) -> 0
216 ex xOverPiMinusHalf=xOverPi-exHALF();
217 if (xOverPiMinusHalf.info(info_flags::integer))
220 if (is_ex_exactly_of_type(x, function)) {
223 if (is_ex_the_function(x, acos))
225 // cos(asin(x)) -> (1-x^2)^(1/2)
226 if (is_ex_the_function(x, asin))
227 return power(exONE()-power(t,exTWO()),exHALF());
228 // cos(atan(x)) -> (1+x^2)^(-1/2)
229 if (is_ex_the_function(x, atan))
230 return power(exONE()+power(t,exTWO()),exMINUSHALF());
233 // cos(float) -> float
234 if (x.info(info_flags::numeric) && !x.info(info_flags::rational))
237 return cos(x).hold();
240 static ex cos_diff(ex const & x, unsigned diff_param)
242 GINAC_ASSERT(diff_param==0);
244 return numMINUSONE()*sin(x);
247 REGISTER_FUNCTION(cos, cos_eval, cos_evalf, cos_diff, NULL);
250 // tangent (trigonometric function)
253 static ex tan_evalf(ex const & x)
257 END_TYPECHECK(tan(x)) // -> numeric tan(numeric)
259 return tan(ex_to_numeric(x));
262 static ex tan_eval(ex const & x)
264 // tan(n*Pi/3) -> {0|3^(1/2)|-(3^(1/2))}
265 ex ThreeExOverPi=numTHREE()*x/Pi;
266 if (ThreeExOverPi.info(info_flags::integer)) {
267 numeric z=mod(ex_to_numeric(ThreeExOverPi),numeric(3));
268 if (z.is_equal(numZERO()))
270 if (z.is_equal(numONE()))
271 return power(exTHREE(),exHALF());
272 if (z.is_equal(numTWO()))
273 return -power(exTHREE(),exHALF());
276 // tan((2n+1)*Pi/2) -> throw
277 ex ExOverPiMinusHalf=x/Pi-exHALF();
278 if (ExOverPiMinusHalf.info(info_flags::integer))
279 throw (std::domain_error("tan_eval(): infinity"));
281 if (is_ex_exactly_of_type(x, function)) {
284 if (is_ex_the_function(x, atan))
286 // tan(asin(x)) -> x*(1+x^2)^(-1/2)
287 if (is_ex_the_function(x, asin))
288 return t*power(exONE()-power(t,exTWO()),exMINUSHALF());
289 // tan(acos(x)) -> (1-x^2)^(1/2)/x
290 if (is_ex_the_function(x, acos))
291 return power(t,exMINUSONE())*power(exONE()-power(t,exTWO()),exHALF());
294 // tan(float) -> float
295 if (x.info(info_flags::numeric) && !x.info(info_flags::rational)) {
299 return tan(x).hold();
302 static ex tan_diff(ex const & x, unsigned diff_param)
304 GINAC_ASSERT(diff_param==0);
306 return (1+power(tan(x),exTWO()));
309 REGISTER_FUNCTION(tan, tan_eval, tan_evalf, tan_diff, NULL);
312 // inverse sine (arc sine)
315 static ex asin_evalf(ex const & x)
319 END_TYPECHECK(asin(x))
321 return asin(ex_to_numeric(x)); // -> numeric asin(numeric)
324 static ex asin_eval(ex const & x)
326 if (x.info(info_flags::numeric)) {
331 if (x.is_equal(exHALF()))
332 return numeric(1,6)*Pi;
334 if (x.is_equal(exONE()))
335 return numeric(1,2)*Pi;
336 // asin(-1/2) -> -Pi/6
337 if (x.is_equal(exMINUSHALF()))
338 return numeric(-1,6)*Pi;
340 if (x.is_equal(exMINUSONE()))
341 return numeric(-1,2)*Pi;
342 // asin(float) -> float
343 if (!x.info(info_flags::rational))
344 return asin_evalf(x);
347 return asin(x).hold();
350 static ex asin_diff(ex const & x, unsigned diff_param)
352 GINAC_ASSERT(diff_param==0);
354 return power(1-power(x,exTWO()),exMINUSHALF());
357 REGISTER_FUNCTION(asin, asin_eval, asin_evalf, asin_diff, NULL);
360 // inverse cosine (arc cosine)
363 static ex acos_evalf(ex const & x)
367 END_TYPECHECK(acos(x))
369 return acos(ex_to_numeric(x)); // -> numeric acos(numeric)
372 static ex acos_eval(ex const & x)
374 if (x.info(info_flags::numeric)) {
376 if (x.is_equal(exONE()))
379 if (x.is_equal(exHALF()))
380 return numeric(1,3)*Pi;
383 return numeric(1,2)*Pi;
384 // acos(-1/2) -> 2/3*Pi
385 if (x.is_equal(exMINUSHALF()))
386 return numeric(2,3)*Pi;
388 if (x.is_equal(exMINUSONE()))
390 // acos(float) -> float
391 if (!x.info(info_flags::rational))
392 return acos_evalf(x);
395 return acos(x).hold();
398 static ex acos_diff(ex const & x, unsigned diff_param)
400 GINAC_ASSERT(diff_param==0);
402 return numMINUSONE()*power(1-power(x,exTWO()),exMINUSHALF());
405 REGISTER_FUNCTION(acos, acos_eval, acos_evalf, acos_diff, NULL);
408 // inverse tangent (arc tangent)
411 static ex atan_evalf(ex const & x)
415 END_TYPECHECK(atan(x))
417 return atan(ex_to_numeric(x)); // -> numeric atan(numeric)
420 static ex atan_eval(ex const & x)
422 if (x.info(info_flags::numeric)) {
424 if (x.is_equal(exZERO()))
426 // atan(float) -> float
427 if (!x.info(info_flags::rational))
428 return atan_evalf(x);
431 return atan(x).hold();
434 static ex atan_diff(ex const & x, unsigned diff_param)
436 GINAC_ASSERT(diff_param==0);
438 return power(1+x*x, -1);
441 REGISTER_FUNCTION(atan, atan_eval, atan_evalf, atan_diff, NULL);
444 // inverse tangent (atan2(y,x))
447 static ex atan2_evalf(ex const & y, ex const & x)
452 END_TYPECHECK(atan2(y,x))
454 return atan(ex_to_numeric(y),ex_to_numeric(x)); // -> numeric atan(numeric)
457 static ex atan2_eval(ex const & y, ex const & x)
459 if (y.info(info_flags::numeric) && !y.info(info_flags::rational) &&
460 x.info(info_flags::numeric) && !x.info(info_flags::rational)) {
461 return atan2_evalf(y,x);
464 return atan2(y,x).hold();
467 static ex atan2_diff(ex const & y, ex const & x, unsigned diff_param)
469 GINAC_ASSERT(diff_param<2);
473 return x*pow(pow(x,2)+pow(y,2),-1);
476 return -y*pow(pow(x,2)+pow(y,2),-1);
479 REGISTER_FUNCTION(atan2, atan2_eval, atan2_evalf, atan2_diff, NULL);
482 // hyperbolic sine (trigonometric function)
485 static ex sinh_evalf(ex const & x)
489 END_TYPECHECK(sinh(x))
491 return sinh(ex_to_numeric(x)); // -> numeric sinh(numeric)
494 static ex sinh_eval(ex const & x)
496 if (x.info(info_flags::numeric)) {
500 // sinh(float) -> float
501 if (!x.info(info_flags::rational))
502 return sinh_evalf(x);
505 if (is_ex_exactly_of_type(x, function)) {
507 // sinh(asinh(x)) -> x
508 if (is_ex_the_function(x, asinh))
510 // sinh(acosh(x)) -> (x-1)^(1/2) * (x+1)^(1/2)
511 if (is_ex_the_function(x, acosh))
512 return power(t-exONE(),exHALF())*power(t+exONE(),exHALF());
513 // sinh(atanh(x)) -> x*(1-x^2)^(-1/2)
514 if (is_ex_the_function(x, atanh))
515 return t*power(exONE()-power(t,exTWO()),exMINUSHALF());
518 return sinh(x).hold();
521 static ex sinh_diff(ex const & x, unsigned diff_param)
523 GINAC_ASSERT(diff_param==0);
528 REGISTER_FUNCTION(sinh, sinh_eval, sinh_evalf, sinh_diff, NULL);
531 // hyperbolic cosine (trigonometric function)
534 static ex cosh_evalf(ex const & x)
538 END_TYPECHECK(cosh(x))
540 return cosh(ex_to_numeric(x)); // -> numeric cosh(numeric)
543 static ex cosh_eval(ex const & x)
545 if (x.info(info_flags::numeric)) {
549 // cosh(float) -> float
550 if (!x.info(info_flags::rational))
551 return cosh_evalf(x);
554 if (is_ex_exactly_of_type(x, function)) {
556 // cosh(acosh(x)) -> x
557 if (is_ex_the_function(x, acosh))
559 // cosh(asinh(x)) -> (1+x^2)^(1/2)
560 if (is_ex_the_function(x, asinh))
561 return power(exONE()+power(t,exTWO()),exHALF());
562 // cosh(atanh(x)) -> (1-x^2)^(-1/2)
563 if (is_ex_the_function(x, atanh))
564 return power(exONE()-power(t,exTWO()),exMINUSHALF());
567 return cosh(x).hold();
570 static ex cosh_diff(ex const & x, unsigned diff_param)
572 GINAC_ASSERT(diff_param==0);
577 REGISTER_FUNCTION(cosh, cosh_eval, cosh_evalf, cosh_diff, NULL);
580 // hyperbolic tangent (trigonometric function)
583 static ex tanh_evalf(ex const & x)
587 END_TYPECHECK(tanh(x))
589 return tanh(ex_to_numeric(x)); // -> numeric tanh(numeric)
592 static ex tanh_eval(ex const & x)
594 if (x.info(info_flags::numeric)) {
598 // tanh(float) -> float
599 if (!x.info(info_flags::rational))
600 return tanh_evalf(x);
603 if (is_ex_exactly_of_type(x, function)) {
605 // tanh(atanh(x)) -> x
606 if (is_ex_the_function(x, atanh))
608 // tanh(asinh(x)) -> x*(1+x^2)^(-1/2)
609 if (is_ex_the_function(x, asinh))
610 return t*power(exONE()+power(t,exTWO()),exMINUSHALF());
611 // tanh(acosh(x)) -> (x-1)^(1/2)*(x+1)^(1/2)/x
612 if (is_ex_the_function(x, acosh))
613 return power(t-exONE(),exHALF())*power(t+exONE(),exHALF())*power(t,exMINUSONE());
616 return tanh(x).hold();
619 static ex tanh_diff(ex const & x, unsigned diff_param)
621 GINAC_ASSERT(diff_param==0);
623 return exONE()-power(tanh(x),exTWO());
626 REGISTER_FUNCTION(tanh, tanh_eval, tanh_evalf, tanh_diff, NULL);
629 // inverse hyperbolic sine (trigonometric function)
632 static ex asinh_evalf(ex const & x)
636 END_TYPECHECK(asinh(x))
638 return asinh(ex_to_numeric(x)); // -> numeric asinh(numeric)
641 static ex asinh_eval(ex const & x)
643 if (x.info(info_flags::numeric)) {
647 // asinh(float) -> float
648 if (!x.info(info_flags::rational))
649 return asinh_evalf(x);
652 return asinh(x).hold();
655 static ex asinh_diff(ex const & x, unsigned diff_param)
657 GINAC_ASSERT(diff_param==0);
659 return power(1+power(x,exTWO()),exMINUSHALF());
662 REGISTER_FUNCTION(asinh, asinh_eval, asinh_evalf, asinh_diff, NULL);
665 // inverse hyperbolic cosine (trigonometric function)
668 static ex acosh_evalf(ex const & x)
672 END_TYPECHECK(acosh(x))
674 return acosh(ex_to_numeric(x)); // -> numeric acosh(numeric)
677 static ex acosh_eval(ex const & x)
679 if (x.info(info_flags::numeric)) {
680 // acosh(0) -> Pi*I/2
682 return Pi*I*numeric(1,2);
684 if (x.is_equal(exONE()))
687 if (x.is_equal(exMINUSONE()))
689 // acosh(float) -> float
690 if (!x.info(info_flags::rational))
691 return acosh_evalf(x);
694 return acosh(x).hold();
697 static ex acosh_diff(ex const & x, unsigned diff_param)
699 GINAC_ASSERT(diff_param==0);
701 return power(x-1,exMINUSHALF())*power(x+1,exMINUSHALF());
704 REGISTER_FUNCTION(acosh, acosh_eval, acosh_evalf, acosh_diff, NULL);
707 // inverse hyperbolic tangent (trigonometric function)
710 static ex atanh_evalf(ex const & x)
714 END_TYPECHECK(atanh(x))
716 return atanh(ex_to_numeric(x)); // -> numeric atanh(numeric)
719 static ex atanh_eval(ex const & x)
721 if (x.info(info_flags::numeric)) {
725 // atanh({+|-}1) -> throw
726 if (x.is_equal(exONE()) || x.is_equal(exONE()))
727 throw (std::domain_error("atanh_eval(): infinity"));
728 // atanh(float) -> float
729 if (!x.info(info_flags::rational))
730 return atanh_evalf(x);
733 return atanh(x).hold();
736 static ex atanh_diff(ex const & x, unsigned diff_param)
738 GINAC_ASSERT(diff_param==0);
740 return power(exONE()-power(x,exTWO()),exMINUSONE());
743 REGISTER_FUNCTION(atanh, atanh_eval, atanh_evalf, atanh_diff, NULL);
745 #ifndef NO_GINAC_NAMESPACE
747 #endif // ndef NO_GINAC_NAMESPACE