� ��g׊���ddlmZddlmZddlmZddlmZddlm Z ddl m Z m Z ddl mZddlmZdd lmZdd lmZmZdd lmZdd lmZmZmZdd lmZddlmZddl m!Z!m"Z"ddl#m$Z$m%Z%ddl&m'Z'ddl(m)Z)m*Z*m+Z+Gd�de��Z,Gd�de,e���Z-Gd�de,��Z.Gd�de.��Z/Gd�de.��Z0Gd�de,��Z1d(d!�Z2Gd"�d#e,��Z3Gd$�d%e3��Z4Gd&�d'e3��Z5d S))�)�Basic)�cacheit)�Tuple)�call_highest_priority)�global_parameters)� AppliedUndef�expand��Mul)�Integer)�Eq)�S� Singleton)�ordered)�Dummy�Symbol�Wild��sympify)�Matrix)�lcm�factor)�Interval� Intersection)�Idx)�flatten� is_sequence�iterablec��eZdZdZdZdZed���Zd�Ze d���Z e d���Z e d���Z e d ���Z e d ���Ze d ���Ze d ���Zed ���Zd�Zd�Zd�Zd�Zd�Zd�Zed��d���Zd�Zed��d���Zd�Zd�Zed��d���Zd�Z d�Z!d!d �Z"dS)"�SeqBasezBase class for sequencesT�c�P� |j}n#t$rtj}YnwxYw|S)z[Return start (if possible) else S.Infinity. adapted from Set._infimum_key )�start�NotImplementedErrorr�Infinity)�exprr#s �f/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sympy/series/sequences.py� _start_keyzSeqBase._start_key s;��  ��J�E�E��"� � � ��J�E�E�E� ����� s � �#�#c�R�t|j|j��}|j|jfS)zTReturns start and stop. Takes intersection over the two intervals. )r�interval�inf�sup)�self�otherr*s r'�_intersect_intervalzSeqBase._intersect_interval,s&��  �� �u�~�>�>���|�X�\�)�)�c�&�td|z���)z&Returns the generator for the sequencez(%s).gen�r$�r-s r'�genz SeqBase.gen4s��"�*�t�"3�4�4�4r0c�&�td|z���)z-The interval on which the sequence is definedz (%s).intervalr2r3s r'r*zSeqBase.interval9s��"�/�D�"8�9�9�9r0c�&�td|z���)�:The starting point of the sequence. This point is includedz (%s).startr2r3s r'r#z SeqBase.start>s��"�,��"5�6�6�6r0c�&�td|z���)z8The ending point of the sequence. This point is includedz (%s).stopr2r3s r'�stopz SeqBase.stopCs��"�+��"4�5�5�5r0c�&�td|z���)zLength of the sequencez (%s).lengthr2r3s r'�lengthzSeqBase.lengthHs��"�-�$�"6�7�7�7r0c��dS)z-Returns a tuple of variables that are bounded�r=r3s r'� variableszSeqBase.variablesMs ���rr0c�*���fd��jD��S)aG This method returns the symbols in the object, excluding those that take on a specific value (i.e. the dummy symbols). Examples ======== >>> from sympy import SeqFormula >>> from sympy.abc import n, m >>> SeqFormula(m*n**2, (n, 0, 5)).free_symbols {m} c�X��h|]&}|j��j��D]}|���'Sr=)� free_symbols� differencer>)�.0�i�jr-s �r'� <setcomp>z'SeqBase.free_symbols.<locals>.<setcomp>`sI���0�0�0�q�q�~��J�t�~�.�.�0�0�!��0�0�0�0r0��argsr3s`r'rAzSeqBase.free_symbolsRs/���0�0�0�0�D�I�0�0�0� 1r0c��||jks ||jkrtd|�d|j�����|�|��S)z#Returns the coefficient at point ptzIndex z out of bounds )r#r9� IndexErrorr*� _eval_coeff�r-�pts r'�coeffz SeqBase.coeffcsJ�� �� �?�?�b�4�9�n�n��*�B�B�B�� � �N�O�O� O�����#�#�#r0c�0�td|jz���)NzhThe _eval_coeff method should be added to%s to return coefficient so it is availablewhen coeff calls it.)r$�funcrLs r'rKzSeqBase._eval_coeffjs%��!�#9�%)�I�#.�/�/� /r0c��|jtjur|j}n|j}|jtjurd}nd}|||zzS)a�Returns the i'th point of a sequence. Explanation =========== If start point is negative infinity, point is returned from the end. Assumes the first point to be indexed zero. Examples ========= >>> from sympy import oo >>> from sympy.series.sequences import SeqPer bounded >>> SeqPer((1, 2, 3), (-10, 10))._ith_point(0) -10 >>> SeqPer((1, 2, 3), (-10, 10))._ith_point(5) -5 End is at infinity >>> SeqPer((1, 2, 3), (0, oo))._ith_point(5) 5 Starts at negative infinity >>> SeqPer((1, 2, 3), (-oo, 0))._ith_point(5) -5 ������)r#r�NegativeInfinityr9)r-rD�initial�steps r'� _ith_pointzSeqBase._ith_pointpsP��@ �:��+� +� +��i�G�G��j�G� �:��+� +� +��D�D��D���4���r0c��dS)aI Should only be used internally. Explanation =========== self._add(other) returns a new, term-wise added sequence if self knows how to add with other, otherwise it returns ``None``. ``other`` should only be a sequence object. Used within :class:`SeqAdd` class. Nr=�r-r.s r'�_addz SeqBase._add�� ���tr0c��dS)aS Should only be used internally. Explanation =========== self._mul(other) returns a new, term-wise multiplied sequence if self knows how to multiply with other, otherwise it returns ``None``. ``other`` should only be a sequence object. Used within :class:`SeqMul` class. Nr=rYs r'�_mulz SeqBase._mul�r[r0c�"�t||��S)a� Should be used when ``other`` is not a sequence. Should be defined to define custom behaviour. Examples ======== >>> from sympy import SeqFormula >>> from sympy.abc import n >>> SeqFormula(n**2).coeff_mul(2) SeqFormula(2*n**2, (n, 0, oo)) Notes ===== '*' defines multiplication of sequences with sequences only. r rYs r'� coeff_mulzSeqBase.coeff_mul�s��$�4����r0c��t|t��stdt|��z���t ||��S)a4Returns the term-wise addition of 'self' and 'other'. ``other`` should be a sequence. Examples ======== >>> from sympy import SeqFormula >>> from sympy.abc import n >>> SeqFormula(n**2) + SeqFormula(n**3) SeqFormula(n**3 + n**2, (n, 0, oo)) zcannot add sequence and %s�� isinstancer � TypeError�type�SeqAddrYs r'�__add__zSeqBase.__add__�sA���%��)�)� H��8�4��;�;�F�G�G� G��d�E�"�"�"r0rfc� �||zS�Nr=rYs r'�__radd__zSeqBase.__radd__�� ���e�|�r0c��t|t��stdt|��z���t || ��S)a7Returns the term-wise subtraction of ``self`` and ``other``. ``other`` should be a sequence. Examples ======== >>> from sympy import SeqFormula >>> from sympy.abc import n >>> SeqFormula(n**2) - (SeqFormula(n)) SeqFormula(n**2 - n, (n, 0, oo)) zcannot subtract sequence and %srarYs r'�__sub__zSeqBase.__sub__�sC���%��)�)� M��=��U� � �K�L�L� L��d�U�F�#�#�#r0rlc��| |zSrhr=rYs r'�__rsub__zSeqBase.__rsub__�s������r0c�,�|�d��S)z�Negates the sequence. Examples ======== >>> from sympy import SeqFormula >>> from sympy.abc import n >>> -SeqFormula(n**2) SeqFormula(-n**2, (n, 0, oo)) rR)r_r3s r'�__neg__zSeqBase.__neg__�s���~�~�b�!�!�!r0c��t|t��stdt|��z���t ||��S)a{Returns the term-wise multiplication of 'self' and 'other'. ``other`` should be a sequence. For ``other`` not being a sequence see :func:`coeff_mul` method. Examples ======== >>> from sympy import SeqFormula >>> from sympy.abc import n >>> SeqFormula(n**2) * (SeqFormula(n)) SeqFormula(n**3, (n, 0, oo)) zcannot multiply sequence and %s)rbr rcrd�SeqMulrYs r'�__mul__zSeqBase.__mul__sA���%��)�)� M��=��U� � �K�L�L� L��d�E�"�"�"r0rsc� �||zSrhr=rYs r'�__rmul__zSeqBase.__rmul__rjr0c#�K�t|j��D].}|�|��}|�|��V��/dSrh)�ranger;rWrN)r-rDrMs r'�__iter__zSeqBase.__iter__sS�����t�{�#�#� !� !�A�����#�#�B��*�*�R�.�.� � � � � !� !r0c�.��t|t��r*��|��}��|��St|t��r?|j|j}}|�d}|��j}�fd�t|||j pd��D��SdS)Nrc�`��g|]*}����|������+Sr=)rNrW)rCrDr-s �r'� <listcomp>z'SeqBase.__getitem__.<locals>.<listcomp>,s=���9�9�9�q�D�J�J�t���q�1�1�2�2�9�9�9r0rS) rb�intrWrN�slicer#r9r;rwrV)r-�indexr#r9s` r'� __getitem__zSeqBase.__getitem__"s���� �e�S� !� !� 9��O�O�E�*�*�E��:�:�e�$�$� $� ��u� %� %� 9��+�u�z�4�E��}����|��{��9�9�9�9��%��u�z��Q�7�7�9�9�9� 9�  9� 9r0Nc ����ddlm��fd�|d|�D��}t|��}|�|dz}nt||dz��}g}t d|dz��D�]5}d|z} g} t |��D]"} | �|| | |z����#t | ��} | ���dkr��| �t ||| �������} || krt| ddd���}n�g} t |||z ��D]"} | �|| | |z����#t | ��} | | zt || d���krt| ddd���}n��7|�|St|��}|dkrgdfS||dz ||dz zzd||dz ||zzz }}t |dz ��D]_}|||||zzz }t ||z dz ��D]"}|||||z|||zdzzzz}�#|||||dzzzz}�`|�t|��t|��z ��fS)a� Finds the shortest linear recurrence that satisfies the first n terms of sequence of order `\leq` ``n/2`` if possible. If ``d`` is specified, find shortest linear recurrence of order `\leq` min(d, n/2) if possible. Returns list of coefficients ``[b(1), b(2), ...]`` corresponding to the recurrence relation ``x(n) = b(1)*x(n-1) + b(2)*x(n-2) + ...`` Returns ``[]`` if no recurrence is found. If gfvar is specified, also returns ordinary generating function as a function of gfvar. Examples ======== >>> from sympy import sequence, sqrt, oo, lucas >>> from sympy.abc import n, x, y >>> sequence(n**2).find_linear_recurrence(10, 2) [] >>> sequence(n**2).find_linear_recurrence(10) [3, -3, 1] >>> sequence(2**n).find_linear_recurrence(10) [2] >>> sequence(23*n**4+91*n**2).find_linear_recurrence(10) [5, -10, 10, -5, 1] >>> sequence(sqrt(5)*(((1 + sqrt(5))/2)**n - (-(1 + sqrt(5))/2)**(-n))/5).find_linear_recurrence(10) [1, 1] >>> sequence(x+y*(-2)**(-n), (n, 0, oo)).find_linear_recurrence(30) [1/2, 1/2] >>> sequence(3*5**n + 12).find_linear_recurrence(20,gfvar=x) ([6, -5], 3*(5 - 21*x)/((x - 1)*(5*x - 1))) >>> sequence(lucas(n)).find_linear_recurrence(15,gfvar=x) ([1, 1], (x - 2)/(x**2 + x - 1)) r)�simplifyc�@��g|]}�t|������Sr=)r )rC�tr�s �r'r{z2SeqBase.find_linear_recurrence.<locals>.<listcomp>Rs)��� 3� 3� 3�Q�X�X�f�Q�i�i� � � 3� 3� 3r0N�rSrR) �sympy.simplifyr��len�minrw�appendr�det�LUsolverr)r-�n�d�gfvar�x�lx�r�coeffs�l�l2�mlist�k�m�yrDrEr�s @r'�find_linear_recurrencezSeqBase.find_linear_recurrence/s����D ,�+�+�+�+�+� 3� 3� 3� 3�$�r��r�(� 3� 3� 3�� ��V�V�� �9��A��A�A��A�b�!�e� � �A����q�!�A�#��� � �A��1��B��E��1�X�X� '� '��� � �Q�q��1��u�X�&�&�&�&��u� � �A��u�u�w�w�!�|�|��H�Q�Y�Y�v�a��"��g���7�7�8�8����8�8�$�Q�t�t��t�W�-�-�F��E����q��A����+�+�A��L�L��1�Q�q�S�5��*�*�*�*��5�M�M���Q�3�&��2�3�3��.�.�(�(�$�Q�t�t��t�W�-�-�F��E�� �=��M��F� � �A��A�v�v��4�x����1��v�e�a��c�l�*�A��q��s� �E�1�H�0D�,D�1���q��s���0�0�A���1��e�Q�h��&�A�"�1�Q�3�q�5�\�\�;�;���V�A�Y�q��t�^�E�A�a�C��E�N�:�:�������5�1�Q�3�<�/�/�A�A��x�x��q� � �&��)�)�(;�<�<�<�<r0)NN)#�__name__� __module__� __qualname__�__doc__�is_commutative� _op_priority� staticmethodr(r/�propertyr4r*r#r9r;r>rArrNrKrWrZr]r_rfrrirlrnrprsrurxrr�r=r0r'r r sY������"�"��N��L�� � ��\� �*�*�*��5�5��X�5��:�:��X�:��7�7��X�7��6�6��X�6��8�8��X�8�����X���1�1��X�1�  �$�$� �W�$� /�/�/� * �* �* �X��� ���  � � �(#�#�#�"��9�%�%���&�%��$�$�$�"��9�%�%���&�%�� "� "� "�#�#�#�$��9�%�%���&�%��!�!�!� 9� 9� 9�I=�I=�I=�I=�I=�I=r0r c�J�eZdZdZed���Zed���Zd�Zd�ZdS)� EmptySequencea�Represents an empty sequence. The empty sequence is also available as a singleton as ``S.EmptySequence``. Examples ======== >>> from sympy import EmptySequence, SeqPer >>> from sympy.abc import x >>> EmptySequence EmptySequence >>> SeqPer((1, 2), (x, 0, 10)) + EmptySequence SeqPer((1, 2), (x, 0, 10)) >>> SeqPer((1, 2)) * EmptySequence EmptySequence >>> EmptySequence.coeff_mul(-1) EmptySequence c��tjSrh)r�EmptySetr3s r'r*zEmptySequence.interval�s ���z�r0c��tjSrh)r�Zeror3s r'r;zEmptySequence.length�s ���v� r0c��|S)�"See docstring of SeqBase.coeff_mulr=)r-rNs r'r_zEmptySequence.coeff_mul�s��� r0c� �tg��Srh)�iterr3s r'rxzEmptySequence.__iter__�s ���B�x�x�r0N) r�r�r�r�r�r*r;r_rxr=r0r'r�r�zsr��������(����X������X���������r0r�)� metaclassc��eZdZdZed���Zed���Zed���Zed���Zed���Z ed���Z dS) �SeqExpra�Sequence expression class. Various sequences should inherit from this class. Examples ======== >>> from sympy.series.sequences import SeqExpr >>> from sympy.abc import x >>> from sympy import Tuple >>> s = SeqExpr(Tuple(1, 2, 3), Tuple(x, 0, 10)) >>> s.gen (1, 2, 3) >>> s.interval Interval(0, 10) >>> s.length 11 See Also ======== sympy.series.sequences.SeqPer sympy.series.sequences.SeqFormula c��|jdS�NrrGr3s r'r4z SeqExpr.gen�s���y��|�r0c�f�t|jdd|jdd��S)NrSr�)rrHr3s r'r*zSeqExpr.interval�s&���� �!� �Q����1��a��9�9�9r0c��|jjSrh�r*r+r3s r'r#z SeqExpr.start�� ���}� � r0c��|jjSrh�r*r,r3s r'r9z SeqExpr.stop�r�r0c�&�|j|jz dzS�NrS�r9r#r3s r'r;zSeqExpr.length�����y�4�:�%��)�)r0c�*�|jddfS)NrSrrGr3s r'r>zSeqExpr.variables�s��� �!� �Q��!�!r0N) r�r�r�r�r�r4r*r#r9r;r>r=r0r'r�r��s���������2����X���:�:��X�:��!�!��X�!��!�!��X�!��*�*��X�*��"�"��X�"�"�"r0r�c�^�eZdZdZd d�Zed���Zed���Zd�Zd�Z d�Z d �Z dS) �SeqPera� Represents a periodic sequence. The elements are repeated after a given period. Examples ======== >>> from sympy import SeqPer, oo >>> from sympy.abc import k >>> s = SeqPer((1, 2, 3), (0, 5)) >>> s.periodical (1, 2, 3) >>> s.period 3 For value at a particular point >>> s.coeff(3) 1 supports slicing >>> s[:] [1, 2, 3, 1, 2, 3] iterable >>> list(s) [1, 2, 3, 1, 2, 3] sequence starts from negative infinity >>> SeqPer((1, 2, 3), (-oo, 0))[0:6] [1, 2, 3, 1, 2, 3] Periodic formulas >>> SeqPer((k, k**2, k**3), (k, 0, oo))[0:6] [0, 1, 8, 3, 16, 125] See Also ======== sympy.series.sequences.SeqFormula Nc�:�t|��}d�}d\}}}|�||��dtj}}}t|t��r=t |��dkr|\}}}n#t |��dkr||��}|\}}t |ttf��r|�|�tdt|��z���|tj ur|tjurtd���t|||f��}t|t��r*ttt|������}ntd|z���t|d |d��tjur tjSt#j|||��S) Nc��|j}t|j��dkr|���Std��S)NrSr�)rAr��popr)� periodical�frees r'�_find_xzSeqPer.__new__.<locals>._find_xs:���*�D��:�*�+�+�q�0�0��x�x�z�z�!��S�z�z�!r0�NNNr�r��Invalid limits given: %sz/Both the start and end valuecannot be unboundedz6invalid period %s should be something like e.g (1, 2) rS)rrr%rrr�rbrr� ValueError�strrT�tuplerrr�r�r�__new__)�clsr��limitsr�r�r#r9s r'r�zSeqPer.__new__s����Z�(�(� � "� "� "�*���5�$� �>�$�W�Z�0�0�!�Q�Z�d�u�A� �v�u� %� %� %��6�{�{�a���!'���5�$�$��V����!�!��G�J�'�'��$� ��t��!�f�c�]�+�+� G�u�}�� ��7�#�f�+�+�E�F�F� F� �A�&� &� &�4�1�:�+=�+=� �"7�8�8�8��!�U�D�)�*�*�� �z�5� )� )� >� ��w�z�':�':�!;�!;�<�<�J�J��0�2<�=�>�>� >� �F�1�I�v�a�y� )� )�Q�Z� 7� 7��?� "��}�S�*�f�5�5�5r0c�*�t|j��Srh)r�r4r3s r'�periodz SeqPer.period+s���4�8�}�}�r0c��|jSrh�r4r3s r'r�zSeqPer.periodical/� ���x�r0c���|jtjur|j|z |jz}n||jz |jz}|j|�|jd|��Sr�)r#rrTr9r�r��subsr>)r-rM�idxs r'rKzSeqPer._eval_coeff3s\�� �:��+� +� +��9�r�>�T�[�0�C�C��� �?�d�k�1�C���s�#�(�(����):�B�?�?�?r0c�x�t|t��r�|j|j}}|j|j}}t ||��}g}t |��D]0}|||z} |||z} |�| | z���1|�|��\} } t||jd| | f��SdS�zSee docstring of SeqBase._addrN� rbr�r�r�rrwr�r/r>� r-r.�per1�lper1�per2�lper2� per_length�new_perr��ele1�ele2r#r9s r'rZz SeqPer._add:���� �e�V� $� $� E��/�4�;�%�D��*�E�L�%�D��U�E�*�*�J��G��:�&�&� ,� ,���A��I����A��I������t�d�{�+�+�+�+��2�2�5�9�9�K�E�4��'�D�N�1�$5�u�d�#C�D�D� D� E� Er0c�x�t|t��r�|j|j}}|j|j}}t ||��}g}t |��D]0}|||z} |||z} |�| | z���1|�|��\} } t||jd| | f��SdS�zSee docstring of SeqBase._mulrNr�r�s r'r]z SeqPer._mulKr�r0c�~��t�����fd�|jD��}t||jd��S)r�c���g|]}|�z��Sr=r=)rCr�rNs �r'r{z$SeqPer.coeff_mul.<locals>.<listcomp>_s���2�2�2�Q�q�5�y�2�2�2r0rS)rr�r�rH)r-rN�pers ` r'r_zSeqPer.coeff_mul\s?��������2�2�2�2�$�/�2�2�2���c�4�9�Q�<�(�(�(r0rh) r�r�r�r�r�r�r�r�rKrZr]r_r=r0r'r�r��s�������.�.�`&6�&6�&6�&6�P����X������X��@�@�@�E�E�E�"E�E�E�")�)�)�)�)r0r�c�N�eZdZdZd d�Zed���Zd�Zd�Zd�Z d�Z d �Z dS) � SeqFormulaaf Represents sequence based on a formula. Elements are generated using a formula. Examples ======== >>> from sympy import SeqFormula, oo, Symbol >>> n = Symbol('n') >>> s = SeqFormula(n**2, (n, 0, 5)) >>> s.formula n**2 For value at a particular point >>> s.coeff(3) 9 supports slicing >>> s[:] [0, 1, 4, 9, 16, 25] iterable >>> list(s) [0, 1, 4, 9, 16, 25] sequence starts from negative infinity >>> SeqFormula(n**2, (-oo, 0))[0:6] [0, 1, 4, 9, 16, 25] See Also ======== sympy.series.sequences.SeqPer Nc��t|��}d�}d\}}}|�||��dtj}}}t|t��r=t |��dkr|\}}}n#t |��dkr||��}|\}}t |ttf��r|�|�tdt|��z���|tj ur|tjurtd���t|||f��}t|d|d��tj ur tjStj|||��S) Nc��|j}t|��dkr|���S|std��St d|z���)NrSr�z� specify dummy variables for %s. If the formula contains more than one free symbol, a dummy variable should be supplied explicitly e.g., SeqFormula(m*n**2, (n, 0, 5)))rAr�r�rr�)�formular�s r'r�z#SeqFormula.__new__.<locals>._find_x�s]���'�D��4�y�y�A�~�~��x�x�z�z�!�� ��S�z�z�!� �O�����r0r�rr�r�r�z0Both the start and end value cannot be unboundedrS)rrr%rrr�rbrrr�r�rTrr�r�rr�)r�r�r�r�r�r#r9s r'r�zSeqFormula.__new__�s[���'�"�"�� � � �*���5�$� �>�$�W�W�-�-�q�!�*�d�u�A� �v�u� %� %� %��6�{�{�a���!'���5�$�$��V����!�!��G�G�$�$��$� ��t��!�f�c�]�+�+� G�u�}�� ��7�#�f�+�+�E�F�F� F� �A�&� &� &�4�1�:�+=�+=� �"7�8�8�8��!�U�D�)�*�*�� �F�1�I�v�a�y� )� )�Q�Z� 7� 7��?� "��}�S�'�6�2�2�2r0c��|jSrhr�r3s r'r�zSeqFormula.formula�r�r0c�R�|jd}|j�||��Sr�)r>r�r�)r-rMr�s r'rKzSeqFormula._eval_coeff�s&�� �N�1� ���|� � ��B�'�'�'r0c��t|t��rl|j|jd}}|j|jd}}||�||��z}|�|��\}}t||||f��SdSr��rbr�r�r>r�r/� r-r.�form1�v1�form2�v2r�r#r9s r'rZzSeqFormula._add���� �e�Z� (� (� :�� �d�n�Q�&7�2�E�� �u��q�'9�2�E��e�j�j��R�0�0�0�G��2�2�5�9�9�K�E�4��g��E�4�'8�9�9� 9�  :� :r0c��t|t��rl|j|jd}}|j|jd}}||�||��z}|�|��\}}t||||f��SdSr�r�r�s r'r]zSeqFormula._mul�r�r0c�j�t|��}|j|z}t||jd��S)r�rS)rr�r�rH)r-rNr�s r'r_zSeqFormula.coeff_mul�s/��������,��&���'�4�9�Q�<�0�0�0r0c�^�tt|jg|�Ri|��|jd��Sr�)r�r r�rH)r-rH�kwargss r'r zSeqFormula.expand�s2���&���?��?�?�?��?�?���1��N�N�Nr0rh) r�r�r�r�r�r�r�rKrZr]r_r r=r0r'r�r�cs�������&�&�P%3�%3�%3�%3�N����X��(�(�(�:�:�:�:�:�:�1�1�1� O�O�O�O�Or0r�c���eZdZdZdd�Zed���Zed���Zed���Zed���Z ed ���Z ed ���Z ed ���Z ed ���Z ed ���Zd�Zd�ZdS)� RecursiveSeqa� A finite degree recursive sequence. Explanation =========== That is, a sequence a(n) that depends on a fixed, finite number of its previous values. The general form is a(n) = f(a(n - 1), a(n - 2), ..., a(n - d)) for some fixed, positive integer d, where f is some function defined by a SymPy expression. Parameters ========== recurrence : SymPy expression defining recurrence This is *not* an equality, only the expression that the nth term is equal to. For example, if :code:`a(n) = f(a(n - 1), ..., a(n - d))`, then the expression should be :code:`f(a(n - 1), ..., a(n - d))`. yn : applied undefined function Represents the nth term of the sequence as e.g. :code:`y(n)` where :code:`y` is an undefined function and `n` is the sequence index. n : symbolic argument The name of the variable that the recurrence is in, e.g., :code:`n` if the recurrence function is :code:`y(n)`. initial : iterable with length equal to the degree of the recurrence The initial values of the recurrence. start : start value of sequence (inclusive) Examples ======== >>> from sympy import Function, symbols >>> from sympy.series.sequences import RecursiveSeq >>> y = Function("y") >>> n = symbols("n") >>> fib = RecursiveSeq(y(n - 1) + y(n - 2), y(n), n, [0, 1]) >>> fib.coeff(3) # Value at a particular point 2 >>> fib[:6] # supports slicing [0, 1, 1, 2, 3, 5] >>> fib.recurrence # inspect recurrence Eq(y(n), y(n - 2) + y(n - 1)) >>> fib.degree # automatically determine degree 2 >>> for x in zip(range(10), fib): # supports iteration ... print(x) (0, 0) (1, 1) (2, 1) (3, 2) (4, 3) (5, 5) (6, 8) (7, 13) (8, 21) (9, 34) See Also ======== sympy.series.sequences.SeqFormula Nrc�,�� �t|t��s"td�|�����t|t��r|js"td�|�����|j|fkrtd���|j� td|f���}d}|� � ��}|D]�} t| j��dkrtd���| jd� ||z��|} | � ��r | j r| dks"td �| �����| |kr| }��|sd �t|��D��}t|��|krtd ���t!|��}t#����t%d �|D���}t j|||||���} �� fd �t)|��D��| _|| _| S)NzErecurrence sequence must be an applied undefined function, found `{}`z0recurrence variable must be a symbol, found `{}`z)recurrence sequence does not match symbolr�)�excluderrSz)Recurrence should be in a single variablezDRecurrence should have constant, negative, integer shifts (found {})c�R�g|]$}td�|������%S)zc_{})r�format)rCr�s r'r{z(RecursiveSeq.__new__.<locals>.<listcomp>Gs,��F�F�F�1�u�V�]�]�1�-�-�.�.�F�F�Fr0z)Number of initial terms must equal degreec3�4K�|]}t|��V��dSrhr)rCr�s r'� <genexpr>z'RecursiveSeq.__new__.<locals>.<genexpr>Os(����6�6��'�!�*�*�6�6�6�6�6�6r0c�4��i|]\}}��|z��|��Sr=r=)rCr��initr#r�s ��r'� <dictcomp>z(RecursiveSeq.__new__.<locals>.<dictcomp>Ss+���J�J�J�G�A�t�Q�Q�u�q�y�\�\�4�J�J�Jr0)rbrrcr�r� is_symbolrHrPr�findr��match� is_constant� is_integerrwr�r rrr�� enumerate�cache�degree) r�� recurrence�ynr�rUr#r�r �prev_ys�prev_y�shift�seqr�s ` @r'r�zRecursiveSeq.__new__#s6�����"�l�+�+� 7��+�+1�6�"�:�:�7�7� 7��!�U�#�#� 6�1�;� 6��+�+1�6�!�9�9�6�6� 6� �7�q�d�?�?��G�H�H� H� �G�� ��q�d� #� #� #���� �/�/�!�$�$��� � �F��6�;���1�$�$�� K�L�L�L��K��N�(�(��Q��/�/��2�E��%�%�'�'� >�E�,<� >������!.�.4�f�V�n�n�>�>�>��v��������� G�F�F��f� � �F�F�F�G� �w�<�<�6� !� !��H�I�I� I������������6�6�g�6�6�6�7���m�C��R��G�U�C�C��J�J�J�J�J�y��7I�7I�J�J�J�� ��� �� r0c��|jdS�zEquation defining recurrence.rrGr3s r'� _recurrencezRecursiveSeq._recurrenceX����y��|�r0c�B�t|j|jd��Sr)r r rHr3s r'r zRecursiveSeq.recurrence]s���$�'�4�9�Q�<�(�(�(r0c��|jdS)z*Applied function representing the nth termrSrGr3s r'r zRecursiveSeq.ynbrr0c��|jjS)z3Undefined function for the nth term of the sequence)r rPr3s r'r�zRecursiveSeq.ygs���w�|�r0c��|jdS)zSequence index symbolr�rGr3s r'r�zRecursiveSeq.nlrr0c��|jdS)z"The initial values of the sequencer�rGr3s r'rUzRecursiveSeq.initialqrr0c��|jdS)r7�rGr3s r'r#zRecursiveSeq.startvrr0c��tjS)z&The ending point of the sequence. (oo))rr%r3s r'r9zRecursiveSeq.stop{s ���z�r0c�(�|jtjfS)z&Interval on which sequence is defined.)r#rr%r3s r'r*zRecursiveSeq.interval�s��� �A�J�'�'r0c���||jz t|j��kr |j|�|��St t|j��|dz��D]d}|j|z}|j�|j|i��}|�|j��}||j|�|��<�e|j|�|j|z��Sr�)r#r�r r�rwr�xreplacer�)r-r~�current� seq_index�current_recurrence�new_terms r'rKzRecursiveSeq._eval_coeff�s��� �4�:� ��D�J��� /� /��:�d�f�f�U�m�m�,� ,��S���_�_�e�a�i�8�8� 5� 5�G�� �W�,�I�!%�!1�!:�!:�D�F�I�;N�!O�!O� �)�2�2�4�:�>�>�H�,4�D�J�t�v�v�i�(�(� )� )��z�$�&�&���g�!5�6�6�7�7r0c#�PK�|j} |�|��V�|dz }�)NTrS)r#rK)r-r~s r'rxzRecursiveSeq.__iter__�s:����� �� ��"�"�5�)�)� )� )� )� �Q�J�E� r0r�)r�r�r�r�r�r�rr r r�r�rUr#r9r*rKrxr=r0r'r�r��sG������J�J�X3�3�3�3�j����X���)�)��X�)�����X������X������X������X������X������X���(�(��X�(� 8� 8� 8�����r0r�Nc��t|��}t|t��rt||��St ||��S)a Returns appropriate sequence object. Explanation =========== If ``seq`` is a SymPy sequence, returns :class:`SeqPer` object otherwise returns :class:`SeqFormula` object. Examples ======== >>> from sympy import sequence >>> from sympy.abc import n >>> sequence(n**2, (n, 0, 5)) SeqFormula(n**2, (n, 0, 5)) >>> sequence((1, 2, 3), (n, 0, 5)) SeqPer((1, 2, 3), (n, 0, 5)) See Also ======== sympy.series.sequences.SeqPer sympy.series.sequences.SeqFormula )rrrr�r�)rr�s r'�sequencer'�sA��4 �#�,�,�C��3����'��c�6�"�"�"��#�v�&�&�&r0c��eZdZdZed���Zed���Zed���Zed���Zed���Z ed���Z dS) � SeqExprOpa� Base class for operations on sequences. Examples ======== >>> from sympy.series.sequences import SeqExprOp, sequence >>> from sympy.abc import n >>> s1 = sequence(n**2, (n, 0, 10)) >>> s2 = sequence((1, 2, 3), (n, 5, 10)) >>> s = SeqExprOp(s1, s2) >>> s.gen (n**2, (1, 2, 3)) >>> s.interval Interval(5, 10) >>> s.length 6 See Also ======== sympy.series.sequences.SeqAdd sympy.series.sequences.SeqMul c�>�td�|jD����S)zjGenerator for the sequence. returns a tuple of generators of all the argument sequences. c3�$K�|] }|jV�� dSrhr��rC�as r'rz SeqExprOp.gen.<locals>.<genexpr>�s$����.�.�q�Q�U�.�.�.�.�.�.r0)r�rHr3s r'r4z SeqExprOp.gen�s#�� �.�.�D�I�.�.�.�.�.�.r0c�2�td�|jD���S)zeSequence is defined on the intersection of all the intervals of respective sequences c3�$K�|] }|jV�� dSrh�r*r,s r'rz%SeqExprOp.interval.<locals>.<genexpr>�s$����<�<�Q�a�j�<�<�<�<�<�<r0)rrHr3s r'r*zSeqExprOp.interval�s �� �<�<�$�)�<�<�<�=�=r0c��|jjSrhr�r3s r'r#zSeqExprOp.start�r�r0c��|jjSrhr�r3s r'r9zSeqExprOp.stop�r�r0c�X�ttd�|jD������S)z%Cumulative of all the bound variablesc��g|] }|j�� Sr=)r>r,s r'r{z'SeqExprOp.variables.<locals>.<listcomp>�s��=�=�=�a�a�k�=�=�=r0)r�rrHr3s r'r>zSeqExprOp.variables�s+���W�=�=�4�9�=�=�=�>�>�?�?�?r0c�&�|j|jz dzSr�r�r3s r'r;zSeqExprOp.length�r�r0N) r�r�r�r�r�r4r*r#r9r>r;r=r0r'r)r)�s���������0�/�/��X�/��>�>��X�>� �!�!��X�!��!�!��X�!��@�@��X�@��*�*��X�*�*�*r0r)c�4�eZdZdZd�Zed���Zd�ZdS)rea�Represents term-wise addition of sequences. Rules: * The interval on which sequence is defined is the intersection of respective intervals of sequences. * Anything + :class:`EmptySequence` remains unchanged. * Other rules are defined in ``_add`` methods of sequence classes. Examples ======== >>> from sympy import EmptySequence, oo, SeqAdd, SeqPer, SeqFormula >>> from sympy.abc import n >>> SeqAdd(SeqPer((1, 2), (n, 0, oo)), EmptySequence) SeqPer((1, 2), (n, 0, oo)) >>> SeqAdd(SeqPer((1, 2), (n, 0, 5)), SeqPer((1, 2), (n, 6, 10))) EmptySequence >>> SeqAdd(SeqPer((1, 2), (n, 0, oo)), SeqFormula(n**2, (n, 0, oo))) SeqAdd(SeqFormula(n**2, (n, 0, oo)), SeqPer((1, 2), (n, 0, oo))) >>> SeqAdd(SeqFormula(n**3), SeqFormula(n**2)) SeqFormula(n**3 + n**2, (n, 0, oo)) See Also ======== sympy.series.sequences.SeqMul c���|�dtj��}t|��}�fd���|��}d�|D��}|s tjSt d�|D���tjur tjS|rt� |��Stt|tj ����}tj|g|�R�S)N�evaluatec���t|t��r;t|t��r#tt �|j��g��S|gSt |��rtt �|��g��Std����Nz2Input must be Sequences or iterables of Sequences)rbr re�sum�maprHrrc��arg�_flattens �r'r?z SeqAdd.__new__.<locals>._flatten s�����#�w�'�'� !��c�6�*�*�!��s�8�S�X�6�6��;�;�;��5�L���}�}� 3��3�x��-�-�r�2�2�2��6�7�7� 7r0c�.�g|]}|tju�|��Sr=)rr�r,s r'r{z"SeqAdd.__new__.<locals>.<listcomp>,s$��<�<�<�a�1�A�O�#;�#;��#;�#;�#;r0c3�$K�|] }|jV�� dSrhr0r,s r'rz!SeqAdd.__new__.<locals>.<genexpr>2�$����3�3��!�*�3�3�3�3�3�3r0)�getrr8�listrr�rr�re�reducerr r(rr��r�rHr�r8r?s @r'r�zSeqAdd.__new__s�����:�:�j�*;�*D�E�E���D�z�z�� 7� 7� 7� 7� 7��x��~�~��<�<�4�<�<�<��� #��?� "� �3�3�d�3�3�3� 4�� � B� B��?� "� � '��=�=��&�&� &��G�D�'�"4�5�5�6�6���}�S�(�4�(�(�(�(r0c�n���d}|rxt|��D]f\}�d}t|��D]I\}�||kr� �����}|�&��fd�|D��}|�|��n�J|r|}n�g|�xt|��dkr|���St |d���S)aSimplify :class:`SeqAdd` using known rules. Iterates through all pairs and ask the constituent sequences if they can simplify themselves with any other constituent. Notes ===== adapted from ``Union.reduce`` TFNc� ��g|] }|��fv�|�� Sr=r=�rCr-�sr�s ��r'r{z!SeqAdd.reduce.<locals>.<listcomp>U�"���#G�#G�#G�!�q��A����A���r0rS�r8)r rZr�r�r�re�rH�new_args�id1�id2�new_seqrJr�s @@r'rEz SeqAdd.reduce=s�������� �#�D�/�/� � ���Q� ��'��o�o� � �F�C���c�z�z� ��f�f�Q�i�i�G��*�#G�#G�#G�#G�#G�t�#G�#G�#G�� ����0�0�0���+���#�D��E��� �" �t�9�9��>�>��8�8�:�:� ��$��/�/�/� /r0c�D��t�fd�|jD����S)z9adds up the coefficients of all the sequences at point ptc3�B�K�|]}|����V��dSrh)rN)rCr-rMs �r'rz%SeqAdd._eval_coeff.<locals>.<genexpr>cs-�����2�2�1�1�7�7�2�;�;�2�2�2�2�2�2r0)r;rHrLs `r'rKzSeqAdd._eval_coeffas(����2�2�2�2�� �2�2�2�2�2�2r0N�r�r�r�r�r�r�rErKr=r0r'rere�sY��������8")�")�")�H�!0�!0��\�!0�F3�3�3�3�3r0rec�4�eZdZdZd�Zed���Zd�ZdS)rra'Represents term-wise multiplication of sequences. Explanation =========== Handles multiplication of sequences only. For multiplication with other objects see :func:`SeqBase.coeff_mul`. Rules: * The interval on which sequence is defined is the intersection of respective intervals of sequences. * Anything \* :class:`EmptySequence` returns :class:`EmptySequence`. * Other rules are defined in ``_mul`` methods of sequence classes. Examples ======== >>> from sympy import EmptySequence, oo, SeqMul, SeqPer, SeqFormula >>> from sympy.abc import n >>> SeqMul(SeqPer((1, 2), (n, 0, oo)), EmptySequence) EmptySequence >>> SeqMul(SeqPer((1, 2), (n, 0, 5)), SeqPer((1, 2), (n, 6, 10))) EmptySequence >>> SeqMul(SeqPer((1, 2), (n, 0, oo)), SeqFormula(n**2)) SeqMul(SeqFormula(n**2, (n, 0, oo)), SeqPer((1, 2), (n, 0, oo))) >>> SeqMul(SeqFormula(n**3), SeqFormula(n**2)) SeqFormula(n**5, (n, 0, oo)) See Also ======== sympy.series.sequences.SeqAdd c���|�dtj��}t|��}�fd���|��}|s tjSt d�|D���tjur tjS|rt� |��Stt|tj ����}tj|g|�R�S)Nr8c���t|t��r;t|t��r#tt �|j��g��S|gSt |��rtt �|��g��Std���r:)rbr rrr;r<rHrrcr=s �r'r?z SeqMul.__new__.<locals>._flatten�s�����#�w�'�'� 3��c�6�*�*�!��s�8�S�X�6�6��;�;�;��5�L��#��� 3��3�x��-�-�r�2�2�2��6�7�7� 7r0c3�$K�|] }|jV�� dSrhr0r,s r'rz!SeqMul.__new__.<locals>.<genexpr>�rBr0)rCrr8rDrr�rr�rrrErr r(rr�rFs @r'r�zSeqMul.__new__�s�����:�:�j�*;�*D�E�E���D�z�z�� 7� 7� 7� 7� 7��x��~�~��� #��?� "� �3�3�d�3�3�3� 4�� � B� B��?� "� � '��=�=��&�&� &��G�D�'�"4�5�5�6�6���}�S�(�4�(�(�(�(r0c�n���d}|rxt|��D]f\}�d}t|��D]I\}�||kr� �����}|�&��fd�|D��}|�|��n�J|r|}n�g|�xt|��dkr|���St |d���S)a.Simplify a :class:`SeqMul` using known rules. Explanation =========== Iterates through all pairs and ask the constituent sequences if they can simplify themselves with any other constituent. Notes ===== adapted from ``Union.reduce`` TFNc� ��g|] }|��fv�|�� Sr=r=rIs ��r'r{z!SeqMul.reduce.<locals>.<listcomp>�rKr0rSrL)r r]r�r�r�rrrMs @@r'rEz SeqMul.reduce�s����� ��� �#�D�/�/� � ���Q� ��'��o�o� � �F�C���c�z�z� ��f�f�Q�i�i�G��*�#G�#G�#G�#G�#G�t�#G�#G�#G�� ����0�0�0���+���#�D��E��� �" �t�9�9��>�>��8�8�:�:� ��$��/�/�/� /r0c�N�d}|jD]}||�|��z}�|S)z<multiplies the coefficients of all the sequences at point ptrS)rHrN)r-rM�valr-s r'rKzSeqMul._eval_coeff�s3������ � �A� �1�7�7�2�;�;� �C�C�� r0NrTr=r0r'rrrrfsZ������ � �D )� )� )�D�$0�$0��\�$0�L����r0rrrh)6�sympy.core.basicr�sympy.core.cacher�sympy.core.containersr�sympy.core.decoratorsr�sympy.core.parametersr�sympy.core.functionrr �sympy.core.mulr �sympy.core.numbersr �sympy.core.relationalr �sympy.core.singletonrr�sympy.core.sortingr�sympy.core.symbolrrr�sympy.core.sympifyr�sympy.matricesr� sympy.polysrr�sympy.sets.setsrr�sympy.tensor.indexedr�sympy.utilities.iterablesrrrr r�r�r�r�r�r'r)rerrr=r0r'�<module>ros[��"�"�"�"�"�"�$�$�$�$�$�$�'�'�'�'�'�'�7�7�7�7�7�7�3�3�3�3�3�3�4�4�4�4�4�4�4�4�������&�&�&�&�&�&�$�$�$�$�$�$�-�-�-�-�-�-�-�-�&�&�&�&�&�&�1�1�1�1�1�1�1�1�1�1�&�&�&�&�&�&�!�!�!�!�!�!�#�#�#�#�#�#�#�#�2�2�2�2�2�2�2�2�$�$�$�$�$�$�D�D�D�D�D�D�D�D�D�D�^=�^=�^=�^=�^=�e�^=�^=�^=�@ "�"�"�"�"�G�y�"�"�"�"�J0"�0"�0"�0"�0"�g�0"�0"�0"�fN)�N)�N)�N)�N)�W�N)�N)�N)�bqO�qO�qO�qO�qO��qO�qO�qO�fB�B�B�B�B�7�B�B�B�J'�'�'�'�N7*�7*�7*�7*�7*��7*�7*�7*�tg3�g3�g3�g3�g3�Y�g3�g3�g3�Tq�q�q�q�q�Y�q�q�q�q�qr0
Memory