-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
trigonometric.py
2440 lines (1963 loc) · 72.1 KB
/
trigonometric.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import typing
from ...core import (Add, Function, Integer, Rational, Symbol, cacheit,
expand_mul)
from ...core.function import ArgumentIndexError
from ...core.logic import fuzzy_and, fuzzy_not
from ...core.numbers import I, igcdex, nan, oo, pi, zoo
from ...core.sympify import sympify
from ...utilities import numbered_symbols
from ..combinatorial.factorials import RisingFactorial, factorial
from .exponential import exp, log
from .hyperbolic import acoth, asinh, atanh, cosh, coth, csch, sech, sinh, tanh
from .miscellaneous import sqrt
###############################################################################
# ######################## TRIGONOMETRIC FUNCTIONS ########################## #
###############################################################################
class TrigonometricFunction(Function):
"""Base class for trigonometric functions."""
unbranched = True
def _eval_expand_complex(self, deep=True, **hints):
re_part, im_part = self.as_real_imag(deep=deep, **hints)
return re_part + im_part*I
def _as_real_imag(self, deep=True, **hints):
if self.args[0].is_extended_real:
if deep:
hints['complex'] = False
return self.args[0].expand(deep, **hints), Integer(0)
else:
return self.args[0], Integer(0)
if deep:
re, im = self.args[0].expand(deep, **hints).as_real_imag()
else:
re, im = self.args[0].as_real_imag()
return re, im
def _peeloff_pi(arg):
"""
Split ARG into two parts, a "rest" and a multiple of pi/2.
This assumes ARG to be an Add.
The multiple of pi returned in the second position is always a Rational.
Examples
========
>>> _peeloff_pi(x + pi/2)
(x, pi/2)
>>> _peeloff_pi(x + 2*pi/3 + pi*y)
(x + pi*y + pi/6, pi/2)
"""
for a in Add.make_args(arg):
if a is pi:
K = Integer(1)
break
elif a.is_Mul:
K, p = a.as_two_terms()
if p is pi and K.is_Rational:
break
else:
return arg, Integer(0)
m1 = (K % Rational(1, 2)) * pi
m2 = K*pi - m1
return arg - m2, m2
def _pi_coeff(arg, cycles=1):
"""
When arg is a Number times pi (e.g. 3*pi/2) then return the Number
normalized to be in the range [0, 2], else None.
When an even multiple of pi is encountered, if it is multiplying
something with known parity then the multiple is returned as 0 otherwise
as 2.
Examples
========
>>> _pi_coeff(3*x*pi)
3*x
>>> _pi_coeff(11*pi/7)
11/7
>>> _pi_coeff(-11*pi/7)
3/7
>>> _pi_coeff(4*pi)
0
>>> _pi_coeff(5*pi)
1
>>> _pi_coeff(5.0*pi)
1
>>> _pi_coeff(5.5*pi)
3/2
>>> _pi_coeff(2 + pi)
>>> _pi_coeff(2*Dummy(integer=True)*pi)
2
>>> _pi_coeff(2*Dummy(even=True)*pi)
0
"""
arg = sympify(arg)
if arg is pi:
return Integer(1)
elif not arg:
return Integer(0)
elif arg.is_Mul:
cx = arg.coeff(pi)
if cx:
c, x = cx.as_coeff_Mul() # pi is not included as coeff
if c.is_Float:
# recast exact binary fractions to Rationals
f = abs(c) % 1
if f != 0:
p = -round(log(f, 2).evalf(strict=False))
m = 2**p
cm = c*m
i = int(cm)
if i == cm:
c = Rational(i, m)
cx = c*x
else:
c = Rational(int(c))
cx = c*x
if x.is_integer:
c2 = c % 2
if c2 == 1:
return x
elif not c2:
if x.is_even is not None: # known parity
return Integer(0)
return Integer(2)
else:
return c2*x
return cx
class sin(TrigonometricFunction):
"""
The sine function.
Returns the sine of x (measured in radians).
Notes
=====
This function will evaluate automatically in the
case x/pi is some rational number. For example,
if x is a multiple of pi, pi/2, pi/3, pi/4 and pi/6.
Examples
========
>>> sin(x**2).diff(x)
2*x*cos(x**2)
>>> sin(pi)
0
>>> sin(pi/2)
1
>>> sin(pi/6)
1/2
>>> sin(pi/12)
-sqrt(2)/4 + sqrt(6)/4
See Also
========
diofant.functions.elementary.trigonometric.csc
diofant.functions.elementary.trigonometric.cos
diofant.functions.elementary.trigonometric.sec
diofant.functions.elementary.trigonometric.tan
diofant.functions.elementary.trigonometric.cot
diofant.functions.elementary.trigonometric.asin
diofant.functions.elementary.trigonometric.acsc
diofant.functions.elementary.trigonometric.acos
diofant.functions.elementary.trigonometric.asec
diofant.functions.elementary.trigonometric.atan
diofant.functions.elementary.trigonometric.acot
diofant.functions.elementary.trigonometric.atan2
References
==========
* https://en.wikipedia.org/wiki/Trigonometric_functions
* https://dlmf.nist.gov/4.14
* http://functions.wolfram.com/ElementaryFunctions/Sin
* https://mathworld.wolfram.com/TrigonometryAngles.html
"""
def fdiff(self, argindex=1):
if argindex == 1:
return cos(self.args[0])
else:
raise ArgumentIndexError(self, argindex)
@classmethod
def eval(cls, arg):
if arg.is_Number:
if arg == 0:
return Integer(0)
elif arg in (oo, -oo):
return
if arg.could_extract_minus_sign():
return -cls(-arg)
i_coeff = arg.as_coefficient(I)
if i_coeff is not None:
return I * sinh(i_coeff)
pi_coeff = _pi_coeff(arg)
if pi_coeff is not None:
if pi_coeff.is_integer:
return Integer(0)
if (2*pi_coeff).is_odd:
return (-1)**(pi_coeff - Rational(1, 2))
if not pi_coeff.is_Rational:
narg = pi_coeff*pi
if narg != arg:
return cls(narg)
return
else:
# https://github.com/sympy/sympy/issues/6048
# transform a sine to a cosine, to avoid redundant code
x = pi_coeff % 2
if x > 1:
return -cls((x % 1)*pi)
if 2*x > 1:
return cls((1 - x)*pi)
narg = ((pi_coeff + Rational(3, 2)) % 2)*pi
result = cos(narg)
if not isinstance(result, cos):
return result
if pi_coeff*pi != arg:
return cls(pi_coeff*pi)
return
if arg.is_Add:
x, m = _peeloff_pi(arg)
if m:
return sin(m)*cos(x) + cos(m)*sin(x)
if isinstance(arg, asin):
return arg.args[0]
if isinstance(arg, atan):
x = arg.args[0]
return x / sqrt(1 + x**2)
if isinstance(arg, atan2):
y, x = arg.args
return y / sqrt(x**2 + y**2)
if isinstance(arg, acos):
x = arg.args[0]
return sqrt(1 - x**2)
if isinstance(arg, acot):
x = arg.args[0]
return 1 / (sqrt(1 + 1 / x**2) * x)
@staticmethod
@cacheit
def taylor_term(n, x, *previous_terms):
if n < 0 or n % 2 == 0:
return Integer(0)
else:
x = sympify(x)
if len(previous_terms) >= 2:
p = previous_terms[-2]
return -p * x**2 / (n*(n - 1))
else:
return (-1)**(n//2) * x**n/factorial(n)
def _eval_rewrite_as_exp(self, arg):
return (exp(arg*I) - exp(-arg*I)) / (2*I)
def _eval_rewrite_as_Pow(self, arg):
if isinstance(arg, log):
x = arg.args[0]
return I*x**-I / 2 - I*x**I / 2
def _eval_rewrite_as_cos(self, arg):
return -cos(arg + pi/2)
def _eval_rewrite_as_tan(self, arg):
tan_half = tan(arg/2)
return 2*tan_half/(1 + tan_half**2)
def _eval_rewrite_as_sincos(self, arg):
return sin(arg)*cos(arg)/cos(arg)
def _eval_rewrite_as_cot(self, arg):
cot_half = cot(arg/2)
return 2*cot_half/(1 + cot_half**2)
def _eval_rewrite_as_sqrt(self, arg):
return self.rewrite(cos).rewrite(sqrt)
def _eval_rewrite_as_csc(self, arg):
return 1/csc(arg)
def _eval_conjugate(self):
return self.func(self.args[0].conjugate())
def as_real_imag(self, deep=True, **hints):
re, im = self._as_real_imag(deep=deep, **hints)
return sin(re)*cosh(im), cos(re)*sinh(im)
def _eval_expand_trig(self, **hints):
from .. import chebyshevt, chebyshevu
arg = self.args[0]
x = None
if arg.is_Add: # TODO, implement more if deep stuff here
# TODO: Do this more efficiently for more than two terms
x, y = arg.as_two_terms()
sx = sin(x, evaluate=False)._eval_expand_trig()
sy = sin(y, evaluate=False)._eval_expand_trig()
cx = cos(x, evaluate=False)._eval_expand_trig()
cy = cos(y, evaluate=False)._eval_expand_trig()
return sx*cy + sy*cx
else:
n, x = arg.as_coeff_Mul(rational=True)
if n.is_Integer: # n will be positive because of .eval
# canonicalization
# See https://mathworld.wolfram.com/Multiple-AngleFormulas.html
if n.is_odd:
return (-1)**((n - 1)/2)*chebyshevt(n, sin(x))
else:
return expand_mul((-1)**(n/2 - 1)*cos(x)*chebyshevu(n -
1, sin(x)), deep=False)
pi_coeff = _pi_coeff(arg)
if pi_coeff is not None:
if pi_coeff.is_Rational:
return self.rewrite(sqrt)
return sin(arg)
def _eval_as_leading_term(self, x):
from ...series import Order
arg = self.args[0].as_leading_term(x)
if x in arg.free_symbols and Order(1, x).contains(arg):
return arg
else:
return self.func(arg)
def _eval_is_complex(self):
if self.args[0].is_complex:
return True
def _eval_is_real(self):
if self.args[0].is_real:
return True
def _eval_is_rational(self):
s = self.func(*self.args)
if s.func == self.func:
if s.args[0].is_zero:
return True
elif s.args[0].is_rational and s.args[0].is_nonzero:
return False
else:
return s.is_rational
def _eval_is_algebraic(self):
s = self.func(*self.args)
if s.func == self.func:
if self.args[0].is_algebraic and self.args[0].is_nonzero:
return False
pi_coeff = _pi_coeff(self.args[0])
if pi_coeff is not None and pi_coeff.is_rational:
return True
else:
return s.is_algebraic
class cos(TrigonometricFunction):
"""
The cosine function.
Returns the cosine of x (measured in radians).
Notes
=====
See :func:`~diofant.functions.elementary.trigonometric.sin`
for notes about automatic evaluation.
Examples
========
>>> cos(x**2).diff(x)
-2*x*sin(x**2)
>>> cos(pi)
-1
>>> cos(pi/2)
0
>>> cos(2*pi/3)
-1/2
>>> cos(pi/12)
sqrt(2)/4 + sqrt(6)/4
See Also
========
diofant.functions.elementary.trigonometric.sin
diofant.functions.elementary.trigonometric.csc
diofant.functions.elementary.trigonometric.sec
diofant.functions.elementary.trigonometric.tan
diofant.functions.elementary.trigonometric.cot
diofant.functions.elementary.trigonometric.asin
diofant.functions.elementary.trigonometric.acsc
diofant.functions.elementary.trigonometric.acos
diofant.functions.elementary.trigonometric.asec
diofant.functions.elementary.trigonometric.atan
diofant.functions.elementary.trigonometric.acot
diofant.functions.elementary.trigonometric.atan2
References
==========
* https://en.wikipedia.org/wiki/Trigonometric_functions
* https://dlmf.nist.gov/4.14
* http://functions.wolfram.com/ElementaryFunctions/Cos
"""
def fdiff(self, argindex=1):
if argindex == 1:
return -sin(self.args[0])
else:
raise ArgumentIndexError(self, argindex)
@classmethod
def eval(cls, arg):
from .. import chebyshevt
if arg.is_Number:
if arg == 0:
return Integer(1)
elif arg in (oo, -oo):
# In this cases, it is unclear if we should
# return nan or leave un-evaluated. One
# useful test case is how "limit(sin(x)/x,x,oo)"
# is handled.
# See test_sin_cos_with_infinity() an
# Test for issue sympy/sympy#3308
# https://github.com/sympy/sympy/issues/5196
# For now, we return un-evaluated.
return
if arg.could_extract_minus_sign():
return cls(-arg)
i_coeff = arg.as_coefficient(I)
if i_coeff is not None:
return cosh(i_coeff)
pi_coeff = _pi_coeff(arg)
if pi_coeff is not None:
if pi_coeff.is_integer:
return (-1)**pi_coeff
if (2*pi_coeff).is_odd:
return Integer(0)
if not pi_coeff.is_Rational:
narg = pi_coeff*pi
if narg != arg:
return cls(narg)
return
else:
# cosine formula #####################
# https://github.com/sympy/sympy/issues/6048
# explicit calculations are preformed for
# cos(k pi/n) for n = 8,10,12,15,20,24,30,40,60,120
# Some other exact values like cos(k pi/240) can be
# calculated using a partial-fraction decomposition
# by calling cos(X).rewrite(sqrt)
cst_table_some = {3: Rational(1, 2),
5: (sqrt(5) + 1)/4}
q = pi_coeff.denominator
p = pi_coeff.numerator % (2*q)
if p > q:
narg = (pi_coeff - 1)*pi
return -cls(narg)
if 2*p > q:
narg = (1 - pi_coeff)*pi
return -cls(narg)
# If nested sqrt's are worse than un-evaluation
# you can require q to be in (1, 2, 3, 4, 6, 12)
# q <= 12, q=15, q=20, q=24, q=30, q=40, q=60, q=120 return
# expressions with 2 or fewer sqrt nestings.
table2 = {
12: (3, 4),
20: (4, 5),
30: (5, 6),
15: (6, 10),
24: (6, 8),
40: (8, 10),
60: (20, 30),
120: (40, 60)
}
if q in table2:
a, b = p*pi/table2[q][0], p*pi/table2[q][1]
nvala, nvalb = cls(a), cls(b)
assert None not in (nvala, nvalb)
return nvala*nvalb + cls(pi/2 - a)*cls(pi/2 - b)
if q > 12:
return
if q in cst_table_some:
cts = cst_table_some[pi_coeff.denominator]
return chebyshevt(pi_coeff.numerator, cts).expand()
if 0 == q % 2:
narg = (pi_coeff*2)*pi
nval = cls(narg)
assert nval is not None
x = (2*pi_coeff + 1)/2
sign_cos = (-1)**((-1 if x < 0 else 1)*int(abs(x)))
return sign_cos*sqrt((1 + nval)/2)
return
if arg.is_Add:
x, m = _peeloff_pi(arg)
if m:
return cos(m)*cos(x) - sin(m)*sin(x)
if isinstance(arg, acos):
return arg.args[0]
if isinstance(arg, atan):
x = arg.args[0]
return 1 / sqrt(1 + x**2)
if isinstance(arg, atan2):
y, x = arg.args
return x / sqrt(x**2 + y**2)
if isinstance(arg, asin):
x = arg.args[0]
return sqrt(1 - x ** 2)
if isinstance(arg, acot):
x = arg.args[0]
return 1 / sqrt(1 + 1 / x**2)
@staticmethod
@cacheit
def taylor_term(n, x, *previous_terms):
if n < 0 or n % 2 == 1:
return Integer(0)
else:
x = sympify(x)
if len(previous_terms) >= 2:
p = previous_terms[-2]
return -p * x**2 / (n*(n - 1))
else:
return (-1)**(n//2)*x**n/factorial(n)
def _eval_rewrite_as_exp(self, arg):
return (exp(arg*I) + exp(-arg*I)) / 2
def _eval_rewrite_as_Pow(self, arg):
if isinstance(arg, log):
x = arg.args[0]
return x**I/2 + x**-I/2
def _eval_rewrite_as_sin(self, arg):
return sin(arg + pi/2)
def _eval_rewrite_as_tan(self, arg):
tan_half = tan(arg/2)**2
return (1 - tan_half)/(1 + tan_half)
def _eval_rewrite_as_sincos(self, arg):
return sin(arg)*cos(arg)/sin(arg)
def _eval_rewrite_as_cot(self, arg):
cot_half = cot(arg/2)**2
return (cot_half - 1)/(cot_half + 1)
def _eval_rewrite_as_sqrt(self, arg):
from .. import chebyshevt
def migcdex(x):
# recursive calcuation of gcd and linear combination
# for a sequence of integers.
# Given (x1, x2, x3)
# Returns (y1, y1, y3, g)
# such that g is the gcd and x1*y1+x2*y2+x3*y3 - g = 0
# Note, that this is only one such linear combination.
assert len(x) != 1
if len(x) == 2:
return igcdex(x[0], x[-1])
g = migcdex(x[1:])
u, v, h = igcdex(x[0], g[-1])
return tuple([u] + [v*i for i in g[0:-1]] + [h])
def ipartfrac(r, factors=None):
from ...ntheory import factorint
assert isinstance(r, Rational)
n = r.denominator
assert 2 < r.denominator*r.denominator
if factors is None:
a = [n//x**y for x, y in factorint(r.denominator).items()]
else:
a = [n//x for x in factors]
if len(a) == 1:
return [r]
h = migcdex(a)
ans = [r.numerator*Rational(i*j, r.denominator) for i, j in zip(h[:-1], a)]
assert r == sum(ans)
return ans
pi_coeff = _pi_coeff(arg)
if pi_coeff is None:
return
assert not pi_coeff.is_integer, 'should have been simplified already'
if not pi_coeff.is_Rational:
return
cst_table_some = {
3: Rational(1, 2),
5: (sqrt(5) + 1)/4,
17: sqrt((15 + sqrt(17))/32 + sqrt(2)*(sqrt(17 - sqrt(17)) +
sqrt(sqrt(2)*(-8*sqrt(17 + sqrt(17)) - (1 - sqrt(17))
* sqrt(17 - sqrt(17))) + 6*sqrt(17) + 34))/32)
# 65537 and 257 are the only other known Fermat primes
# Please add if you would like them
}
def fermatCoords(n):
assert n > 1 and n % 2
primes = {p: 0 for p in cst_table_some}
assert 1 not in primes
for p_i in primes:
while 0 == n % p_i:
n = n/p_i
primes[p_i] += 1
if 1 != n:
return False
if max(primes.values()) > 1:
return False
return tuple(p for p in primes if primes[p] == 1)
if pi_coeff.denominator in cst_table_some:
return chebyshevt(pi_coeff.numerator, cst_table_some[pi_coeff.denominator]).expand()
if 0 == pi_coeff.denominator % 2: # recursively remove powers of 2
narg = (pi_coeff*2)*pi
nval = cos(narg)
assert nval is not None
nval = nval.rewrite(sqrt)
x = (2*pi_coeff + 1)/2
sign_cos = (-1)**((-1 if x < 0 else 1)*int(abs(x)))
return sign_cos*sqrt((1 + nval)/2)
FC = fermatCoords(pi_coeff.denominator)
if FC:
decomp = ipartfrac(pi_coeff, FC)
X = [(x[1], x[0]*pi) for x in zip(decomp, numbered_symbols('z'))]
pcls = cos(sum(x[0] for x in X))._eval_expand_trig().subs(X)
return pcls.rewrite(sqrt)
else:
decomp = ipartfrac(pi_coeff)
X = [(x[1], x[0]*pi) for x in zip(decomp, numbered_symbols('z'))]
pcls = cos(sum(x[0] for x in X))._eval_expand_trig().subs(X)
return pcls
def _eval_rewrite_as_sec(self, arg):
return 1/sec(arg)
def _eval_conjugate(self):
return self.func(self.args[0].conjugate())
def as_real_imag(self, deep=True, **hints):
re, im = self._as_real_imag(deep=deep, **hints)
return cos(re)*cosh(im), -sin(re)*sinh(im)
def _eval_expand_trig(self, **hints):
from .. import chebyshevt
arg = self.args[0]
x = None
if arg.is_Add: # TODO: Do this more efficiently for more than two terms
x, y = arg.as_two_terms()
sx = sin(x, evaluate=False)._eval_expand_trig()
sy = sin(y, evaluate=False)._eval_expand_trig()
cx = cos(x, evaluate=False)._eval_expand_trig()
cy = cos(y, evaluate=False)._eval_expand_trig()
return cx*cy - sx*sy
else:
coeff, terms = arg.as_coeff_Mul(rational=True)
if coeff.is_Integer:
return chebyshevt(coeff, cos(terms))
pi_coeff = _pi_coeff(arg)
if pi_coeff is not None:
if pi_coeff.is_Rational:
return self.rewrite(sqrt)
return cos(arg)
def _eval_as_leading_term(self, x):
from ...series import Order
arg = self.args[0].as_leading_term(x)
if x in arg.free_symbols and Order(1, x).contains(arg):
return Integer(1)
else:
return self.func(arg)
def _eval_is_real(self):
if self.args[0].is_real:
return True
def _eval_is_complex(self):
if self.args[0].is_complex:
return True
def _eval_is_rational(self):
s = self.func(*self.args)
if s.func == self.func:
if s.args[0].is_zero:
return True
if s.args[0].is_rational and s.args[0].is_nonzero:
return False
else:
return s.is_rational
def _eval_is_algebraic(self):
s = self.func(*self.args)
if s.func == self.func:
if self.args[0].is_algebraic and self.args[0].is_nonzero:
return False
pi_coeff = _pi_coeff(self.args[0])
if pi_coeff is not None and pi_coeff.is_rational:
return True
else:
return s.is_algebraic
class tan(TrigonometricFunction):
"""
The tangent function.
Returns the tangent of x (measured in radians).
Notes
=====
See :func:`~diofant.functions.elementary.trigonometric.sin`
for notes about automatic evaluation.
Examples
========
>>> tan(x**2).diff(x)
2*x*(tan(x**2)**2 + 1)
>>> tan(pi/8).expand()
-1 + sqrt(2)
See Also
========
diofant.functions.elementary.trigonometric.sin
diofant.functions.elementary.trigonometric.csc
diofant.functions.elementary.trigonometric.cos
diofant.functions.elementary.trigonometric.sec
diofant.functions.elementary.trigonometric.cot
diofant.functions.elementary.trigonometric.asin
diofant.functions.elementary.trigonometric.acsc
diofant.functions.elementary.trigonometric.acos
diofant.functions.elementary.trigonometric.asec
diofant.functions.elementary.trigonometric.atan
diofant.functions.elementary.trigonometric.acot
diofant.functions.elementary.trigonometric.atan2
References
==========
* https://en.wikipedia.org/wiki/Trigonometric_functions
* https://dlmf.nist.gov/4.14
* http://functions.wolfram.com/ElementaryFunctions/Tan
"""
def fdiff(self, argindex=1):
if argindex == 1:
return 1 + self**2
else:
raise ArgumentIndexError(self, argindex)
def inverse(self, argindex=1):
"""Returns the inverse of this function."""
return atan
@classmethod
def eval(cls, arg):
if arg.is_Number:
if arg == 0:
return Integer(0)
if arg.could_extract_minus_sign():
return -cls(-arg)
i_coeff = arg.as_coefficient(I)
if i_coeff is not None:
return I * tanh(i_coeff)
pi_coeff = _pi_coeff(arg, 2)
if pi_coeff is not None:
if pi_coeff.is_integer:
return Integer(0)
if not pi_coeff.is_Rational:
narg = pi_coeff*pi
if narg != arg:
return cls(narg)
return
else:
if not pi_coeff.denominator % 2:
narg = pi_coeff*pi*2
cresult, sresult = cos(narg), cos(narg - pi/2)
if not isinstance(cresult, cos) \
and not isinstance(sresult, cos):
if sresult == 0:
return zoo
return (1 - cresult)/sresult
table2 = {
12: (3, 4),
20: (4, 5),
30: (5, 6),
15: (6, 10),
24: (6, 8),
40: (8, 10),
60: (20, 30),
120: (40, 60)
}
q = pi_coeff.denominator
p = pi_coeff.numerator % q
if q in table2:
nvala, nvalb = cls(p*pi/table2[q][0]), cls(p*pi/table2[q][1])
assert None not in (nvala, nvalb)
return (nvala - nvalb)/(1 + nvala*nvalb)
narg = ((pi_coeff + Rational(1, 2)) % 1 - Rational(1, 2))*pi
# see cos() to specify which expressions should be
# expanded automatically in terms of radicals
cresult, sresult = cos(narg), cos(narg - pi/2)
if not isinstance(cresult, cos) and not isinstance(sresult, cos):
return sresult/cresult
if narg != arg:
return cls(narg)
if arg.is_Add:
x, m = _peeloff_pi(arg)
if m:
tanm = tan(m)
tanx = tan(x)
if tanm is zoo:
return -cot(x)
return (tanm + tanx)/(1 - tanm*tanx)
if isinstance(arg, atan):
return arg.args[0]
if isinstance(arg, atan2):
y, x = arg.args
return y/x
if isinstance(arg, asin):
x = arg.args[0]
return x / sqrt(1 - x**2)
if isinstance(arg, acos):
x = arg.args[0]
return sqrt(1 - x**2) / x
if isinstance(arg, acot):
x = arg.args[0]
return 1 / x
@staticmethod
@cacheit
def taylor_term(n, x, *previous_terms):
from .. import bernoulli
if n < 0 or n % 2 == 0:
return Integer(0)
else:
x = sympify(x)
a, b = ((n - 1)//2), 2**(n + 1)
B = bernoulli(n + 1)
F = factorial(n + 1)
return (-1)**a * b*(b - 1) * B/F * x**n
def _eval_nseries(self, x, n, logx):
i = self.args[0].limit(x, 0)*2/pi
if i and i.is_Integer:
return self.rewrite(cos)._eval_nseries(x, n=n, logx=logx)
return Function._eval_nseries(self, x, n=n, logx=logx)
def _eval_rewrite_as_Pow(self, arg):
if isinstance(arg, log):
x = arg.args[0]
return I*(x**-I - x**I)/(x**-I + x**I)
def _eval_conjugate(self):
return self.func(self.args[0].conjugate())
def as_real_imag(self, deep=True, **hints):
re, im = self._as_real_imag(deep=deep, **hints)
if im:
denom = cos(2*re) + cosh(2*im)
return sin(2*re)/denom, sinh(2*im)/denom
else:
return self.func(re), Integer(0)
def _eval_expand_trig(self, **hints):
from .complexes import im, re
arg = self.args[0]
if arg.is_Add:
from ...polys import symmetric_poly
n = len(arg.args)
TX = []
for x in arg.args:
tx = tan(x, evaluate=False)._eval_expand_trig()
TX.append(tx)
Yg = numbered_symbols('Y')
Y = [next(Yg) for i in range(n)]
p = [0, 0]
for i in range(n + 1):
p[1 - i % 2] += symmetric_poly(i, *Y)*(-1)**((i % 4)//2)
return (p[0]/p[1]).subs(list(zip(Y, TX)))
else:
coeff, terms = arg.as_coeff_Mul(rational=True)
if coeff.is_Integer and coeff > 1:
z = Symbol('dummy', extended_real=True)
P = ((1 + I*z)**coeff).expand()
return (im(P)/re(P)).subs({z: tan(terms)})
return tan(arg)
def _eval_rewrite_as_exp(self, arg):
neg_exp, pos_exp = exp(-arg*I), exp(arg*I)
return I*(neg_exp - pos_exp)/(neg_exp + pos_exp)
def _eval_rewrite_as_sin(self, x):
return 2*sin(x)**2/sin(2*x)
def _eval_rewrite_as_cos(self, x):
return -cos(x + pi/2)/cos(x)
def _eval_rewrite_as_sincos(self, arg):
return sin(arg)/cos(arg)
def _eval_rewrite_as_cot(self, arg):
return 1/cot(arg)
def _eval_rewrite_as_sqrt(self, arg):
y = self.rewrite(cos).rewrite(sqrt)
if not y.has(cos):
return y
def _eval_as_leading_term(self, x):
from ...series import Order
arg = self.args[0].as_leading_term(x)
if x in arg.free_symbols and Order(1, x).contains(arg):
return arg
else:
return self.func(arg)
def _eval_is_real(self):
if (2*self.args[0]/pi).is_noninteger: