forked from flintlib/python-flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharb.pyx
2456 lines (2102 loc) · 77.5 KB
/
arb.pyx
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
from cpython.version cimport PY_MAJOR_VERSION
from flint.flint_base.flint_context cimport getprec
from flint.flint_base.flint_base cimport flint_scalar
from flint.utils.typecheck cimport typecheck
from flint.utils.conversion cimport chars_from_str, str_from_chars
cdef _str_trunc(s, trunc=0):
if trunc > 0 and len(s) > 3 * trunc:
left = right = trunc
omitted = len(s) - left - right
return s[:left] + ("{...%s digits...}" % omitted) + s[-right:]
return s
cdef arb_from_str(str s):
s = s.strip()
if ("/" in s) and ("+/-" not in s):
return arb(fmpq(s))
s = s.replace("±", "+/-")
a = arb.__new__(arb)
if arb_set_str((<arb>a).val, chars_from_str(s), getprec()) == 0:
return a
else:
raise ValueError("invalid string for arb()")
cdef arb_set_mpmath_mpf(arb_t x, obj):
sgn, man, exp, bc = obj
if not man:
if not exp:
arb_zero(x)
else:
arb_indeterminate(x)
else:
man = fmpz(long(man))
exp = fmpz(exp)
arb_set_fmpz(x, (<fmpz>man).val)
arb_mul_2exp_fmpz(x, x, (<fmpz>exp).val)
if sgn:
arb_neg(x, x)
cdef int arb_set_python(arb_t x, obj, bint allow_conversion) except -1:
"""
Sets an arb_t given any Python object. If allow_conversion is set,
conversions for the arb() constructor are done (from tuples, strings, etc.)
"""
cdef fmpz_t t
if typecheck(obj, arb):
arb_set(x, (<arb>obj).val)
return 1
if typecheck(obj, arf):
arb_set_arf(x, (<arf>obj).val)
return 1
if typecheck(obj, fmpz):
arb_set_fmpz(x, (<fmpz>obj).val)
return 1
if typecheck(obj, fmpq):
arb_set_fmpq(x, (<fmpq>obj).val, getprec())
return 1
if PY_MAJOR_VERSION < 3 and PyInt_Check(<PyObject*>obj):
arb_set_si(x, PyInt_AS_LONG(<PyObject*>obj))
return 1
if PyLong_Check(<PyObject*>obj):
fmpz_init(t)
fmpz_set_pylong(t, obj)
arb_set_fmpz(x, t)
fmpz_clear(t)
return 1
if typecheck(obj, float):
arf_set_d(arb_midref(x), PyFloat_AS_DOUBLE(<PyObject*>obj))
mag_zero(arb_radref(x))
return 1
if hasattr(obj, "_mpf_"):
arb_set_mpmath_mpf(x, obj._mpf_)
return 1
if allow_conversion and typecheck(obj, tuple):
v = arf(obj)
arb_set_arf(x, (<arf>v).val)
return 1
if allow_conversion and typecheck(obj, str):
obj = arb_from_str(obj)
arb_set(x, (<arb>obj).val)
return 1
return 0
cdef inline int arb_set_any_ref(arb_t x, obj):
if typecheck(obj, arb):
x[0] = (<arb>obj).val[0]
return FMPZ_REF
arb_init(x)
if arb_set_python(x, obj, 0):
return FMPZ_TMP
return FMPZ_UNKNOWN
cdef any_as_arb(x):
cdef arb t
if typecheck(x, arb):
return x
t = arb()
if arb_set_python(t.val, x, 0) == 0:
raise TypeError("cannot create arb from type %s" % type(x))
return t
cdef any_as_arb_or_notimplemented(x):
cdef arb t
if typecheck(x, arb):
return x
t = arb()
if arb_set_python(t.val, x, 0) == 0:
return NotImplemented
return t
cdef class arb(flint_scalar):
ur"""
Represents a real number `x` by a midpoint `m` and a radius `r`
such that `x \in [m \pm r] = [m-r, m+r]`.
The midpoint and radius are both floating-point numbers. The radius
uses a fixed, implementation-defined precision (30 bits).
The precision used for midpoints is controlled by :attr:`ctx.prec` (bits)
or equivalently :attr:`ctx.dps` (digits).
The constructor accepts a midpoint *mid* and a radius *rad*, either of
which defaults to zero if omitted. The arguments can be tuples
`(a, b)` representing exact floating-point data `a 2^b`, integers,
floating-point numbers, rational strings, or decimal strings.
If the radius is nonzero, it might be rounded up to a slightly larger
value than the exact value passed by the user.
>>> arb(10.25)
10.2500000000000
>>> print(1 / arb(4)) # exact
0.250000000000000
>>> print(1 / arb(3)) # approximate
[0.333333333333333 +/- 3.71e-16]
>>> print(arb("3.0"))
3.00000000000000
>>> print(arb("0.1"))
[0.100000000000000 +/- 2.23e-17]
>>> print(arb("1/10"))
[0.100000000000000 +/- 2.23e-17]
>>> print(arb("3.14159 +/- 0.00001"))
[3.1416 +/- 2.01e-5]
>>> ctx.dps = 50
>>> print(arb("1/3"))
[0.33333333333333333333333333333333333333333333333333 +/- 3.78e-51]
>>> ctx.default()
Converting to or from decimal results in some loss of accuracy.
See :meth:`.arb.str` for details.
"""
cdef arb_t val
def __cinit__(self):
arb_init(self.val)
def __dealloc__(self):
arb_clear(self.val)
def __init__(self, mid=None, rad=None):
if mid is not None:
if arb_set_python(self.val, mid, 1) == 0:
raise TypeError("cannot create arb from type %s" % type(mid))
if rad is not None:
rad = arb(rad)
arb_add_error(self.val, (<arb>rad).val)
#rad = arf(rad)
#arb_add_error_arf(self.val, (<arf>rad).val)
cpdef bint is_zero(self):
return arb_is_zero(self.val)
cpdef bint is_finite(self):
return arb_is_finite(self.val)
cpdef bint is_nan(self):
return arf_is_nan(arb_midref(self.val))
cpdef bint is_exact(self):
return arb_is_exact(self.val)
def man_exp(self):
"""
Decomposes *self* into an integer mantissa and an exponent,
returning an *fmpz* pair. Requires that *self* is exact
and finite.
>>> arb("1.1").mid().man_exp()
(4953959590107545, -52)
>>> arb("1.1").rad().man_exp()
(1, -52)
>>> arb(0).man_exp()
(0, 0)
>>> arb("1.1").man_exp()
Traceback (most recent call last):
...
ValueError: man_exp requires an exact, finite value
>>> arb("+inf").man_exp()
Traceback (most recent call last):
...
ValueError: man_exp requires an exact, finite value
"""
cdef fmpz man, exp
if not self.is_finite() or not self.is_exact():
raise ValueError("man_exp requires an exact, finite value")
man = fmpz()
exp = fmpz()
arf_get_fmpz_2exp(man.val, exp.val, arb_midref(self.val))
return man, exp
def fmpq(self):
cdef fmpq res
if not self.is_finite() or not self.is_exact():
raise ValueError("fmpq requires an exact, finite value")
res = fmpq()
arf_get_fmpq(res.val, arb_midref(self.val))
return res
def fmpz(self):
cdef fmpz res
if not self.is_integer():
raise ValueError("fmpz requires an exact, integer value")
res = fmpz()
arf_get_fmpz(res.val, arb_midref(self.val), ARF_RND_DOWN)
return res
cpdef bint is_integer(self):
return arb_is_int(self.val)
def mid(self):
"""
Returns the midpoint of *self* as an exact *arb*:
>>> arb("1 +/- 0.3").mid()
1.00000000000000
"""
cdef arb x = arb()
arf_set(arb_midref(x.val), arb_midref(self.val))
return x
def rad(self):
"""
Returns the radius of *self* as an exact *arb*:
>>> print(arb("1 +/- 0.3").rad().str(5, radius=False))
0.30000
"""
cdef arb x = arb()
arf_set_mag(arb_midref(x.val), arb_radref(self.val))
return x
def abs_lower(self):
"""
Lower bound for the absolute value of *self*.
The output is an *arb* holding an exact floating-point number
that has been rounded down to the current precision.
>>> print(arb("-5 +/- 2").abs_lower().str(5, radius=False))
3.0000
"""
cdef arb x = arb()
arb_get_abs_lbound_arf(arb_midref(x.val), self.val, getprec())
return x
def abs_upper(self):
"""
Upper bound for the absolute value of *self*.
The output is an *arb* holding an exact floating-point number
that has been rounded up to the current precision.
>>> print(arb("-5 +/- 2").abs_upper().str(5, radius=False))
7.0000
"""
cdef arb x = arb()
arb_get_abs_ubound_arf(arb_midref(x.val), self.val, getprec())
return x
def lower(self):
"""
Lower bound for *self* (towards `-\infty`).
The output is an *arb* holding an exact floating-point number
that has been rounded down to the current precision.
>>> print(arb("-5 +/- 2").lower().str(5, radius=False))
-7.0000
"""
cdef arb x = arb()
arb_get_lbound_arf(arb_midref(x.val), self.val, getprec())
return x
def upper(self):
"""
Upper bound for *self* (towards `+\infty`).
The output is an *arb* holding an exact floating-point number
that has been rounded up to the current precision.
>>> print(arb("-5 +/- 2").upper().str(5, radius=False))
-3.0000
"""
cdef arb x = arb()
arb_get_ubound_arf(arb_midref(x.val), self.val, getprec())
return x
def mid_rad_10exp(self, long n=0):
"""
Returns an *fmpz* triple (*mid*, *rad*, *exp*) where the larger of *mid*
and *rad* has *n* digits plus a few digits (*n* defaults to the current
precision), such that *self* is contained in
`[\operatorname{mid} \pm \operatorname{rad}] 10^{\operatorname{exp}}`.
>>> (arb(1) / 3).mid_rad_10exp(10)
(333333333333333, 2, -15)
>>> (arb(1) / 3).mid_rad_10exp(20)
(3333333333333333148296162, 555111516, -25)
>>> arb(0,1e-100).mid_rad_10exp(10)
(0, 100000000376832, -114)
>>> arb(-1,1e100).mid_rad_10exp()
(0, 10000000083585662976, 81)
"""
cdef fmpz mid, rad, exp
if n <= 0:
n = ctx.dps
mid = fmpz()
rad = fmpz()
exp = fmpz()
arb_get_fmpz_mid_rad_10exp(mid.val, rad.val, exp.val, self.val, n)
return mid, rad, exp
@property
def _mpf_(self):
try:
import mpmath
mpmath_mpz = mpmath.libmp.MPZ
except ImportError:
mpmath_mpz = long
if not self.is_finite():
return (0, mpmath_mpz(0), -123, -1)
man, exp = self.mid().man_exp()
man = mpmath_mpz(long(man))
if man < 0:
return (1, -man, long(exp), man.bit_length())
else:
return (0, man, long(exp), man.bit_length())
def repr(self):
mid = self.mid()
rad = self.rad()
if rad.is_zero():
return "arb(%s)" % mid._repr_str()
else:
return "arb(%s, %s)" % (mid._repr_str(), rad._repr_str())
def str(self, long n=0, bint radius=True, bint more=False, long condense=0):
ur"""
Produces a human-readable decimal representation of self, with
up to *n* printed digits (which defaults to the current precision)
for the midpoint. The output can be parsed by the *arb* constructor.
Since the internal representation is binary, conversion
to decimal (and back from decimal) is generally inexact.
Binary-decimal-binary roundtrips may result in significantly
larger intervals, and should therefore be done sparingly.
>>> print(arb.pi().str())
[3.14159265358979 +/- 3.34e-15]
>>> print(arb.pi().str(5))
[3.1416 +/- 7.35e-6]
>>> print(arb.pi().str(5, radius=False))
3.1416
By default, the output is truncated so that all displayed digits
are guaranteed to be correct, up to adding or subtracting 1 in the
last displayed digit (as a special case, if the output ends with a
string of 0s, the correct decimal expansion to infinite precision
could have a string of 9s).
>>> print((arb(1) - arb("1e-10")).str(5))
[1.0000 +/- 4e-10]
>>> print((arb(1) - arb("1e-10")).str(10))
[0.9999999999 +/- 3e-15]
To force more digits, set *more* to *True*.
>>> print(arb("0.1").str(30))
[0.100000000000000 +/- 2.23e-17]
>>> print(arb("0.1").str(30, more=True))
[0.0999999999999999916733273153113 +/- 1.39e-17]
Note that setting *more* to *True* results in a smaller printed radius,
since there is less error from the conversion back to decimal.
>>> x = arb.pi().sin()
>>> print(x.str())
[+/- 3.46e-16]
>>> print(x.str(more=True))
[1.22460635382238e-16 +/- 2.23e-16]
The error indicated in the output may be much larger than the actual
error in the internal representation of *self*. For example, if *self*
is accurate to 1000 digits and printing is done at 10-digit precision,
the output might only appear as being accurate to 10 digits. It is
even possible for *self* to be exact and have an inexact decimal
representation.
The *condense* option can be passed to show only leading and trailing
digits of the fractional, integer and exponent parts of the output.
>>> ctx.dps = 1000
>>> print(arb.pi().str(condense=10))
[3.1415926535{...979 digits...}9216420199 +/- 9.28e-1001]
>>> print(arb.fac_ui(300).str(condense=10))
3060575122{...595 digits...}0000000000.0000000000{...365 digits...}0000000000
>>> print(arb(10**100).exp().str(condense=5))
[1.53837{...989 digits...}96534e+43429{...90 digits...}17483 +/- 4.84e+43429{...90 digits...}16483]
>>> ctx.default()
"""
cdef ulong flags
cdef char * s
flags = 0
if not radius:
flags |= ARB_STR_NO_RADIUS
if more:
flags |= ARB_STR_MORE
if condense > 0:
flags |= ARB_STR_CONDENSE * condense
if n <= 0:
n = ctx.dps
s = arb_get_str(self.val, n, flags)
try:
res = str_from_chars(s)
finally:
libc.stdlib.free(s)
if ctx.unicode:
res = res.replace("+/-", "±")
return res
def __float__(self):
return arf_get_d(arb_midref(self.val), ARF_RND_NEAR)
def __richcmp__(s, t, int op):
cdef bint res
cdef arb_struct sval[1]
cdef arb_struct tval[1]
cdef int stype, ttype
stype = arb_set_any_ref(sval, s)
if stype == FMPZ_UNKNOWN:
return NotImplemented
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
res = 0
if op == 2: res = arb_eq(sval, tval)
elif op == 3: res = arb_ne(sval, tval)
elif op == 0: res = arb_lt(sval, tval)
elif op == 1: res = arb_le(sval, tval)
elif op == 4: res = arb_gt(sval, tval)
elif op == 5: res = arb_ge(sval, tval)
if stype == FMPZ_TMP: arb_clear(sval)
if ttype == FMPZ_TMP: arb_clear(tval)
return res
def __contains__(self, other):
other = any_as_arb(other)
return arb_contains(self.val, (<arb>other).val)
def contains(self, other):
other = any_as_arb(other)
return bool(arb_contains(self.val, (<arb>other).val))
def contains_interior(self, other):
other = any_as_arb(other)
return bool(arb_contains_interior(self.val, (<arb>other).val))
def overlaps(self, other):
other = any_as_arb(other)
return bool(arb_overlaps((<arb>self).val, (<arb>other).val))
def contains_integer(self):
return bool(arb_contains_int(self.val))
@property
def real(self):
return self
@property
def imag(self):
return arb()
def __pos__(self):
res = arb.__new__(arb)
arb_set_round((<arb>res).val, (<arb>self).val, getprec())
return res
def __neg__(self):
res = arb.__new__(arb)
arb_neg_round((<arb>res).val, (<arb>self).val, getprec())
return res
def neg(self, bint exact=False):
res = arb.__new__(arb)
if exact:
arb_set((<arb>res).val, (<arb>self).val)
else:
arb_set_round((<arb>res).val, (<arb>self).val, getprec())
return res
def __abs__(self):
res = arb.__new__(arb)
arb_abs((<arb>res).val, (<arb>self).val)
arb_set_round((<arb>res).val, (<arb>res).val, getprec())
return res
def sgn(self):
"""
Sign function, returning an *arb*.
>>> arb(-3).sgn()
-1.00000000000000
>>> arb(0).sgn()
0
>>> arb("0 +/- 1").sgn()
[+/- 1.01]
"""
res = arb.__new__(arb)
arb_sgn((<arb>res).val, (<arb>self).val)
return res
def __add__(s, t):
cdef arb_struct tval[1]
cdef int ttype
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_add((<arb>u).val, (<arb>s).val, tval, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __radd__(s, t):
cdef arb_struct tval[1]
cdef int ttype
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_add((<arb>u).val, tval, s.val, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __sub__(s, t):
cdef arb_struct tval[1]
cdef int ttype
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_sub((<arb>u).val, (<arb>s).val, tval, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __rsub__(s, t):
cdef arb_struct tval[1]
cdef int ttype
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_sub((<arb>u).val, tval, s.val, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __mul__(s, t):
cdef arb_struct tval[1]
cdef int ttype
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_mul((<arb>u).val, (<arb>s).val, tval, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __rmul__(s, t):
cdef arb_struct tval[1]
cdef int ttype
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_mul((<arb>u).val, tval, s.val, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __truediv__(s, t):
cdef arb_struct tval[1]
cdef int ttype
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_div((<arb>u).val, (<arb>s).val, tval, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __rtruediv__(s, t):
cdef arb_struct tval[1]
cdef int ttype
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_div((<arb>u).val, tval, s.val, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __pow__(s, t, modulus):
cdef arb_struct tval[1]
cdef int ttype
if modulus is not None:
raise TypeError("three-argument pow() not supported by arb type")
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_pow((<arb>u).val, (<arb>s).val, tval, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def __rpow__(s, t, modulus):
cdef arb_struct tval[1]
cdef int ttype
if modulus is not None:
raise TypeError("three-argument pow() not supported by arb type")
ttype = arb_set_any_ref(tval, t)
if ttype == FMPZ_UNKNOWN:
return NotImplemented
u = arb.__new__(arb)
arb_pow((<arb>u).val, tval, s.val, getprec())
if ttype == FMPZ_TMP: arb_clear(tval)
return u
def floor(s):
ur"""
Floor function `\lfloor s \rfloor`.
>>> print(arb.pi().floor())
3.00000000000000
>>> print((arb.pi() - arb.pi()).floor().str(more=True))
[-0.500000000000000 +/- 0.501]
"""
u = arb.__new__(arb)
arb_floor((<arb>u).val, (<arb>s).val, getprec())
return u
def ceil(s):
ur"""
Ceiling function `\lceil s \rceil`.
>>> print(arb.pi().ceil())
4.00000000000000
>>> print((arb.pi() - arb.pi()).ceil().str(more=True))
[0.500000000000000 +/- 0.501]
"""
u = arb.__new__(arb)
arb_ceil((<arb>u).val, (<arb>s).val, getprec())
return u
def sqrt(s):
r"""
Square root `\sqrt{s}`.
>>> showgood(lambda: arb(3).sqrt(), dps=25)
1.732050807568877293527446
>>> showgood(lambda: arb(0).sqrt(), dps=25)
0
>>> showgood(lambda: arb(-1).sqrt(), dps=25)
Traceback (most recent call last):
...
ValueError: no convergence (maxprec=960, try higher maxprec)
This function is undefined for negative input.
Use :meth:`.acb.sqrt` for the complex extension.
"""
u = arb.__new__(arb)
arb_sqrt((<arb>u).val, (<arb>s).val, getprec())
return u
def rsqrt(s):
r"""
Reciprocal square root `1/\sqrt{s}`.
>>> showgood(lambda: arb(3).rsqrt(), dps=25)
0.5773502691896257645091488
>>> showgood(lambda: arb(0).rsqrt(), dps=25)
Traceback (most recent call last):
...
ValueError: no convergence (maxprec=960, try higher maxprec)
>>> showgood(lambda: arb(-1).rsqrt(), dps=25)
Traceback (most recent call last):
...
ValueError: no convergence (maxprec=960, try higher maxprec)
This function is undefined for negative input.
Use :meth:`.acb.rsqrt` for the complex extension.
"""
u = arb.__new__(arb)
arb_rsqrt((<arb>u).val, (<arb>s).val, getprec())
return u
def exp(s):
r"""
Exponential function `\exp(s)`.
>>> showgood(lambda: arb(1).exp(), dps=25)
2.718281828459045235360287
"""
u = arb.__new__(arb)
arb_exp((<arb>u).val, (<arb>s).val, getprec())
return u
def expm1(s):
r"""
Exponential function `\exp(s) - 1`, computed accurately for small *s*.
>>> showgood(lambda: (arb(10) ** -8).expm1(), dps=25)
1.000000005000000016666667e-8
"""
u = arb.__new__(arb)
arb_expm1((<arb>u).val, (<arb>s).val, getprec())
return u
def log(s):
r"""
Natural logarithm `\log(s)`.
>>> showgood(lambda: arb(2).log(), dps=25)
0.6931471805599453094172321
>>> showgood(lambda: arb(100).exp().log(), dps=25)
100.0000000000000000000000
>>> showgood(lambda: arb(-1).sqrt(), dps=25)
Traceback (most recent call last):
...
ValueError: no convergence (maxprec=960, try higher maxprec)
This function is undefined for negative input.
Use :meth:`.acb.log` for the complex extension.
"""
u = arb.__new__(arb)
arb_log((<arb>u).val, (<arb>s).val, getprec())
return u
def log1p(s):
r"""
Natural logarithm `\log(1+s)`, computed accurately for small *s*.
>>> showgood(lambda: acb(1).log1p(), dps=25)
0.6931471805599453094172321
>>> showgood(lambda: arb("1e-100000000000000000").log1p(), dps=25)
1.000000000000000000000000e-100000000000000000
This function is undefined for `s \le -1`.
Use :meth:`.acb.log1p` for the complex extension.
"""
u = arb.__new__(arb)
arb_log1p((<arb>u).val, (<arb>s).val, getprec())
return u
def sin(s):
r"""
Sine function `\sin(s)`.
>>> showgood(lambda: arb(1).sin(), dps=25)
0.8414709848078965066525023
"""
u = arb.__new__(arb)
arb_sin((<arb>u).val, (<arb>s).val, getprec())
return u
def cos(s):
r"""
Cosine function `\cos(s)`.
>>> showgood(lambda: arb(1).cos(), dps=25)
0.5403023058681397174009366
"""
u = arb.__new__(arb)
arb_cos((<arb>u).val, (<arb>s).val, getprec())
return u
def sin_cos(s):
r"""
Computes `\sin(s)` and `\cos(s)` simultaneously.
>>> showgood(lambda: arb(1).sin_cos(), dps=25)
(0.8414709848078965066525023, 0.5403023058681397174009366)
"""
u = arb.__new__(arb)
v = arb.__new__(arb)
arb_sin_cos((<arb>u).val, (<arb>v).val, (<arb>s).val, getprec())
return u, v
def sin_pi(s):
r"""
Sine function `\sin(\pi s)`.
>>> showgood(lambda: arb(0.75).sin_pi(), dps=25)
0.7071067811865475244008444
>>> showgood(lambda: arb(1).sin_pi(), dps=25)
0
"""
u = arb.__new__(arb)
arb_sin_pi((<arb>u).val, (<arb>s).val, getprec())
return u
def cos_pi(s):
r"""
Cosine function `\cos(\pi s)`.
>>> showgood(lambda: arb(0.75).cos_pi(), dps=25)
-0.7071067811865475244008444
>>> showgood(lambda: arb(0.5).cos_pi(), dps=25)
0
"""
u = arb.__new__(arb)
arb_cos_pi((<arb>u).val, (<arb>s).val, getprec())
return u
def sin_cos_pi(s):
r"""
Computes `\sin(\pi s)` and `\cos(\pi s)` simultaneously.
>>> showgood(lambda: arb(0.75).sin_cos_pi(), dps=25)
(0.7071067811865475244008444, -0.7071067811865475244008444)
"""
u = arb.__new__(arb)
v = arb.__new__(arb)
arb_sin_cos_pi((<arb>u).val, (<arb>v).val, (<arb>s).val, getprec())
return u, v
def tan(s):
r"""
Tangent function `\tan(s)`.
>>> showgood(lambda: arb(1).tan(), dps=25)
1.557407724654902230506975
"""
u = arb.__new__(arb)
arb_tan((<arb>u).val, (<arb>s).val, getprec())
return u
def cot(s):
r"""
Cotangent function `\cot(s)`.
>>> showgood(lambda: arb(1).cot(), dps=25)
0.6420926159343307030064200
"""
u = arb.__new__(arb)
arb_cot((<arb>u).val, (<arb>s).val, getprec())
return u
def tan_pi(s):
r"""
Tangent function `\tan(\pi s)`.
>>> showgood(lambda: arb(0.125).tan_pi(), dps=25)
0.4142135623730950488016887
>>> showgood(lambda: arb(1).tan_pi(), dps=25)
0
"""
u = arb.__new__(arb)
arb_tan_pi((<arb>u).val, (<arb>s).val, getprec())
return u
def cot_pi(s):
r"""
Cotangent function `\cot(\pi s)`.
>>> showgood(lambda: arb(0.125).cot_pi(), dps=25)
2.414213562373095048801689
>>> showgood(lambda: arb(0.5).cot_pi(), dps=25)
0
"""
u = arb.__new__(arb)
arb_cot_pi((<arb>u).val, (<arb>s).val, getprec())
return u
@staticmethod
def sin_pi_fmpq(fmpq s):
r"""
Returns the algebraic sine value `\sin(\pi s)`.
>>> showgood(lambda: arb.sin_pi_fmpq(fmpq(3,4)), dps=25)
0.7071067811865475244008444
"""
u = arb.__new__(arb)
arb_sin_pi_fmpq((<arb>u).val, (<fmpq>s).val, getprec())
return u
@staticmethod
def cos_pi_fmpq(fmpq s):
r"""
Returns the algebraic cosine value `\cos(\pi s)`.
>>> showgood(lambda: arb.cos_pi_fmpq(fmpq(3,4)), dps=25)
-0.7071067811865475244008444
"""
u = arb.__new__(arb)
arb_cos_pi_fmpq((<arb>u).val, (<fmpq>s).val, getprec())
return u
@staticmethod
def sin_cos_pi_fmpq(fmpq s):
r"""
Computes `\sin(\pi s)` and `\cos(\pi s)` simultaneously.
>>> showgood(lambda: arb.sin_cos_pi_fmpq(fmpq(3,4)), dps=25)
(0.7071067811865475244008444, -0.7071067811865475244008444)
"""
u = arb.__new__(arb)
v = arb.__new__(arb)
arb_sin_cos_pi_fmpq((<arb>u).val, (<arb>v).val, (<fmpq>s).val, getprec())
return u, v
def sec(s):
"""
Secant function `\operatorname{sec}(s)`.
>>> showgood(lambda: arb(1).sec(), dps=25)
1.850815717680925617911753
"""
u = arb.__new__(arb)
arb_sec((<arb>u).val, (<arb>s).val, getprec())
return u
def csc(s):
"""
Cosecant function `\operatorname{csc}(s)`.
>>> showgood(lambda: arb(1).csc(), dps=25)
1.188395105778121216261599
"""
u = arb.__new__(arb)
arb_csc((<arb>u).val, (<arb>s).val, getprec())
return u
def sinc(s):
r"""
Sinc function, `\operatorname{sinc}(x) = \sin(x)/x`.
>>> showgood(lambda: arb(3).sinc(), dps=25)
0.04704000268662240736691493
"""
u = arb.__new__(arb)
arb_sinc((<arb>u).val, (<arb>s).val, getprec())
return u
def sinc_pi(s):
r"""
Normalized sinc function, `\operatorname{sinc}(\pi x) = \sin(\pi x)/(\pi x)`.
>>> showgood(lambda: arb(1.5).sinc_pi(), dps=25)
-0.2122065907891937810251784
>>> showgood(lambda: arb(2).sinc_pi(), dps=25)
0
"""
u = arb.__new__(arb)
arb_sinc_pi((<arb>u).val, (<arb>s).val, getprec())
return u
def atan(s):
r"""
Inverse tangent `\operatorname{atan}(s)`.
>>> showgood(lambda: arb(1).atan(), dps=25)
0.7853981633974483096156608
"""
u = arb.__new__(arb)
arb_atan((<arb>u).val, (<arb>s).val, getprec())
return u
@staticmethod
def atan2(s, t):
r"""
Two-argument inverse tangent `\operatorname{atan2}(s,t)`.