K i+dZddlmZddlZddlmZddlmZddlm Z ddl Z ddl m Z e jjZe jj ZGdd Zeeee fe_y) a PythonMPQ: Rational number type based on Python integers. This class is intended as a pure Python fallback for when gmpy2 is not installed. If gmpy2 is installed then its mpq type will be used instead. The mpq type is around 20x faster. We could just use the stdlib Fraction class here but that is slower: from fractions import Fraction from sympy.external.pythonmpq import PythonMPQ nums = range(1000) dens = range(5, 1005) rats = [Fraction(n, d) for n, d in zip(nums, dens)] sum(rats) # <--- 24 milliseconds rats = [PythonMPQ(n, d) for n, d in zip(nums, dens)] sum(rats) # <--- 7 milliseconds Both mpq and Fraction have some awkward features like the behaviour of division with // and %: >>> from fractions import Fraction >>> Fraction(2, 3) % Fraction(1, 4) 1/6 For the QQ domain we do not want this behaviour because there should be no remainder when dividing rational numbers. SymPy does not make use of this aspect of mpq when gmpy2 is installed. Since this class is a fallback for that case we do not bother implementing e.g. __mod__ so that we can be sure we are not using it when gmpy2 is installed either. ) annotationsN)gcd)Decimal)Fraction)TypeceZdZUdZdZd"dZedZefdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"d e#d!<xZ$S)# PythonMPQaRational number implementation that is intended to be compatible with gmpy2's mpq. Also slightly faster than fractions.Fraction. PythonMPQ should be treated as immutable although no effort is made to prevent mutation (since that might slow down calculations).  numerator denominatorc|Ht|trt|trt||}||z}||z}|j||St|tr|j |dSt|t r&|j |j |jSt|tttfr t|}t|tr&|j |j |jStd)z3Construct PythonMPQ with gcd computation and checksz/PythonMPQ() requires numeric or string argument) isinstanceintr _new_check_newr r r rfloatstrr TypeError)clsr r divisors ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/external/pythonmpq.py__new__zPythonMPQ.__new__9s  ")S)jc.Ji5g% ' ~~i==)S)xx 1--Iy1xx 3 3Y5J5JKK)guc%:;$Y/ )X.xx 3 3Y5J5JKKIJJcb|std|d||dkr| }| }|j||S)z@Construct PythonMPQ, check divide by zero and canonicalize signsz Zero divisor /r)ZeroDivisionErrorr)rr r s rrzPythonMPQ._new_check]sE#mI;a }$MN N 1_" I&,Kxx ;//rcBt||}||_||_|S)z+Construct PythonMPQ efficiently (no checks))superrr r )rr r obj __class__s rrzPythonMPQ._newgs&goc"! % rcT|j|j}}|dkr| |z S||zS)z'Convert to int (truncates towards zero)rr )selfpqs r__int__zPythonMPQ.__int__os2~~t//1 q5RU8O!t rc4|j|jz S)z Convert to float (approximately)r r#s r __float__zPythonMPQ.__float__vs~~ 0 000rc,t|jS)zTrue/False if nonzero/zero)boolr r(s r__bool__zPythonMPQ.__bool__zsDNN##rct|tr4|j|jk(xr|j|jk(St||jr|j t|St S)z=Compare equal with PythonMPQ, int, float, Decimal or Fraction)rr r r _compatible_types__eq__NotImplementedr#others rr/zPythonMPQ.__eq__~sd eY 'NNeoo5:$$(9(99 ; t55 6;;y/0 0! !rc t|jdt}ttt |j |z}|j dk\r|n| }|dk(rdS|S#t $r t}Y.wxYw)zhash - same as mpq/Fractionr)powr _PyHASH_MODULUShashabsr ValueError _PyHASH_INF)r#dinvhash_results r__hash__zPythonMPQ.__hash__su ;t''_=Dc$..12T9:E..A-E6r\r-v-  E sA$$A65A6cHt||j|jffS)zDeconstruct for pickling)typer r r(s r __reduce__zPythonMPQ.__reduce__s DzDNND,<,<===rcp|jdk7r|jd|jS|jS)Convert to stringrr)r r r(s r__str__zPythonMPQ.__str__s:   q nn%Qt'7'7&89 9nn% &rc<d|jd|jdS)rDzMPQ(,)r r(s r__repr__zPythonMPQ.__repr__s#dnn%Qt'7'7&8::rct||jstS|j|jz}|j|jz}|||S)zHelper for lt/le/gt/ge)rr.r0r r )r#r2oplhsrhss r_cmpzPythonMPQ._cmpsL%!7!78! !nnu000oo 0 00#s|rcB|j|tjS)z self < other)rNoperatorltr1s r__lt__zPythonMPQ.__lt__yy ,,rcB|j|tjS)z self <= other)rNrPler1s r__le__zPythonMPQ.__le__rSrcB|j|tjS)z self > other)rNrPgtr1s r__gt__zPythonMPQ.__gt__rSrcB|j|tjS)z self >= other)rNrPger1s r__ge__zPythonMPQ.__ge__rSrc`|jt|j|jS)zabs(q))rr9r r r(s r__abs__zPythonMPQ.__abs__s"yyT^^,d.>.>??rc|S)z+qr(s r__pos__zPythonMPQ.__pos__s rcP|j|j |jS)z-q)rr r r(s r__neg__zPythonMPQ.__neg__syy$..$*:*:;;rct|tr|j|j}}|j|j}}t ||}|dk(r||z||zz}||z}ns||z||z} } || z|| zz| | z}}t ||} || z||| zz}}n?t|t r)|j|j|zz}|j}nt S|j||S)zq1 + q2rrr r r rrr0r r#r2apaqbpbqgr$r%q1q2g2s r__add__zPythonMPQ.__add__s eY '^^T%5%5B__e&7&7BB AAvrEBrEMrEQAB"ur"u}be1AYR!qBw-1 s #!1!1E!99A  A! !yyArct|tr:|j|j|zz}|j}|j ||St S)zz1 + q2)rrr r rr0r#r2r$r%s r__radd__zPythonMPQ.__radd__sF eS !!1!1E!99A  A99Q? "! !rct|tr|j|j}}|j|j}}t ||}|dk(r||z||zz }||z}ns||z||z} } || z|| zz | | z}}t ||} || z||| zz}}n?t|t r)|j|j|zz }|j}nt S|j||S)zq1 - q2rrerfs r__sub__zPythonMPQ.__sub__s eY '^^T%5%5B__e&7&7BB AAvrEBrEMrEQAB"ur"u}be1AYR!qBw-1 s #!1!1%!77A  A! !yyArct|tr:|j|z|jz }|j}|j ||St S)zz1 - q2)rrr r rr0rqs r__rsub__zPythonMPQ.__rsub__sF eS !  5(4>>9A  A99Q? "! !rct|tr_|j|j}}|j|j}}t ||}t ||}||z||zz||z||zz} }nNt|t r8t ||j} |j|| zz}|j| z} nt S|j|| S)zq1 * q2re r#r2rgrhrirjx1x2r$r%xs r__mul__zPythonMPQ.__mul__s eY '^^T%5%5B__e&7&7BRBRBVb"f%BR'8qA s #E4++,Aq)A  !#A! !yyArct|trIt|j|}|j||zz}|j|z}|j ||St S)zz1 * q2)rrrr r rr0r#r2r{r$r%s r__rmul__zPythonMPQ.__rmul__ sV eS !D$$e,Aq)A  !#A99Q? "! !rcz|j|j}}|dkr||| }}}|j||z||zS)zq ** zr)r r r)r#expr$r%s r__pow__zPythonMPQ.__pow__*sD~~t//1 71sd#qAq#vq#v..rct|tr_|j|j}}|j|j}}t ||}t ||}||z||zz||z||zz} }nNt|t r8t ||j} |j| z}|j|| zz} nt S|j|| S)zq1 / q2)rr r r rrr0rrxs r __truediv__zPythonMPQ.__truediv__3s eY '^^T%5%5B__e&7&7BRBRBVb"f%BR'8qA s #E4>>*A!A  %(+A! !q!$$rct|trIt|j|}|j||zz}|j|z}|j ||St S)zz / q)rrrr r rr0r~s r __rtruediv__zPythonMPQ.__rtruediv__DsU eS !DNNE*A  %(+A!A??1a( (! !rr`ztuple[Type, ...]r.)N)%__name__ __module__ __qualname____doc__ __slots__r classmethodrrr&r)r,r/r?rBrErIrNrRrVrYr\r^rarcrorrrtrvr|rrrrr.__annotations__ __classcell__)r!s@rr r .s-I"KH001$" .>';----@<B","""/%""+-',rr )r __future__rrPmathrdecimalr fractionsrsystypingr hash_infomodulusr7infr;r rr.r`rrrsa># --''mm `-`-N )#wA r