ó
    [«Ñg¯  ã                   ó0   • S SK Jr  S SKJr   " S S5      rg)é    )Úsqrt©ÚBaseLunrExceptionc                   ól   • \ rS rSrSrSS jrS rS rS rS r	SS	 jr
S
 rS r\S 5       rS rS rSrg)ÚVectoré   a¼  A vector is used to construct the vector space of documents and queries.
These vectors support operations to determine the similarity between two
documents or a document and a query.

Normally no parameters are required for initializing a vector, but in the
case of loading a previously dumped vector the raw elements can be provided
to the constructor.

For performance reasons vectors are implemented with a flat array, where an
elements index is immediately followed by its value.
E.g. [index, value, index, value].

TODO: consider implemetation as 2-tuples.

This allows the underlying array to be as sparse as possible and still
offer decent performance when being used for vector calculations.
Nc                 ó2   • SU l         U=(       d    / U l        g )Nr   ©Ú
_magnitudeÚelements)Úselfr   s     Ú-/usr/lib/python3/dist-packages/lunr/vector.pyÚ__init__ÚVector.__init__   s   € ØˆŒØ Ÿ Bˆ�ó    c                 ó8   • SR                  U R                  5      $ )Nz<Vector magnitude={}>)ÚformatÚ	magnitude©r   s    r   Ú__repr__ÚVector.__repr__   s   € Ø&×-Ñ-¨d¯n©nÓ=Ð=r   c                 ó,   • [        U R                  5      $ ©N)Úiterr   r   s    r   Ú__iter__ÚVector.__iter__    s   € Ü�D—M‘MÓ"Ð"r   c                 óx  • U R                   (       d  gSn[        [        U R                   5      S-  5      nX2-
  n[        US-  5      nU R                   US-     nUS:”  a@  Xa:  a  UnO	Xa:”  a  UnOO/X2-
  nU[        US-  5      -   nU R                   US-     nUS:”  a  M@  Xa:X  a  US-  $ Xa:”  a  US-  $ US-   S-  $ )a;  Calculates the position within the vector to insert a given index.

This is used internally by insert and upsert. If there are duplicate
indexes then the position is returned as if the value for that index
were to be updated, but it is the callers responsibility to check
whether there is a duplicate at that index
r   é   é   )r   ÚintÚlen)r   ÚindexÚstartÚendÚslice_lengthÚpivot_pointÚpivot_indexs          r   Úposition_for_indexÚVector.position_for_index#   sÜ   € ð �}�}ØàˆÜ”#�d—m‘mÓ$ qÑ(Ó)ˆØ‘{ˆÜ˜,¨Ñ*Ó+ˆØ—m‘m K°!¡OÑ4ˆà˜QÓØÓ"Ø#‘ØÓ$Ø!‘àà™;ˆLØ¤# l°QÑ&6Ó"7Ñ7ˆKØŸ-™-¨°a©Ñ8ˆKð ˜QÕð ÓØ ‘?Ð"ØÓ Ø ‘?Ð"à !‘O qÑ(Ð(r   c                 ó.   • S nU R                  XU5        g)z�Inserts an element at an index within the vector.

Does not allow duplicates, will throw an error if there is already an
entry for this index.
c                 ó   • [        S5      e)NzDuplicate indexr   )r"   Úvals     r   Úprevent_duplicatesÚ)Vector.insert.<locals>.prevent_duplicatesN   s   € Ü#Ð$5Ó6Ð6r   N)Úupsert)r   Úinsert_indexr,   r-   s       r   ÚinsertÚVector.insertG   s   € ò	7ð 	�‰�LÐ'9Õ:r   c                 ód  • U=(       d    S nSU l         U R                  U5      nU[        U R                  5      :  a=  U R                  U   U:X  a*  U" U R                  US-      U5      U R                  US-   '   gU R                  R	                  XB5        U R                  R	                  XA5        g)a¡  Inserts or updates an existing index within the vector.

Args:
    - insert_index (int): The index at which the element should be
        inserted.
    - val (int|float): The value to be inserted into the vector.
    - fn (callable, optional): An optional callable taking two
        arguments, the current value and the passed value to generate
        the final inserted value at the position in case of collision.
c                 ó   • U$ r   © )ÚcurrentÚpasseds     r   Ú<lambda>ÚVector.upsert.<locals>.<lambda>^   s   € ©Fr   r   r   N)r   r(   r!   r   r1   )r   r0   r,   ÚfnÚpositions        r   r/   ÚVector.upsertS   s‘   € ð ×3Ñ2ˆØˆŒØ×*Ñ*¨<Ó8ˆØ”c˜$Ÿ-™-Ó(Ó(¨T¯]©]¸8Ñ-DÈÓ-TÙ*,¨T¯]©]¸8Àa¹<Ñ-HÈ#Ó*NˆD�M‰M˜( Q™,Ò'à�M‰M× Ñ  Ô/Ø�M‰M× Ñ  Õ8r   c                 ó”   • / n[        S[        U R                  5      S5       H!  nUR                  U R                  U   5        M#     U$ )zAConverts the vector to an array of the elements within the vectorr   r   )Úranger!   r   Úappend)r   ÚoutputÚis      r   Úto_listÚVector.to_listg   s=   € àˆÜ�qœ#˜dŸm™mÓ,¨aÖ0ˆAØ�M‰M˜$Ÿ-™-¨Ñ*Ö+ñ 1àˆr   c                 óZ   • U R                    Vs/ s H  n[        US5      PM     sn$ s  snf )Né   )r   Úround)r   Úelements     r   Ú	serializeÚVector.serializen   s'   € ð 26·²Ó?² g”�g˜qÖ!±Ñ?Ð?ùÒ?s   �(c                 óÚ   • U R                   (       dO  Sn[        S[        U R                  5      S5       H  nU R                  U   nXU-  -  nM     [	        U5      U l         U R                   $ )Nr   r   r   )r   r>   r!   r   r   )r   Úsum_of_squaresrA   Úvalues       r   r   ÚVector.magnitudes   s\   € à��ØˆNÜ˜1œc $§-¡-Ó0°!Ö4�ØŸ™ aÑ(�Ø¨%¡-Ñ/’ñ 5ô # >Ó2ˆDŒOà�‰Ðr   c                 ó  • SnU R                   nUR                   n[        U5      n[        U5      nS=pxXu:  aK  X†:  aF  X7   n	XH   n
Xš:  a  US-  nO'Xš:”  a  US-  nOX#US-      XHS-      -  -  nUS-  nUS-  nXu:  a  X†:  a  MF  U$ )z=Calculates the dot product of this vector and another vector.r   r   r   )r   r!   )r   ÚotherÚdot_productÚaÚbÚa_lenÚb_lenrA   ÚjÚa_valÚb_vals              r   ÚdotÚ
Vector.dot   s¥   € àˆØ�M‰MˆØ�N‰NˆÜ�A“ˆÜ�A“ˆØˆ	ˆà‹i˜A›IØ‘DˆEØ‘DˆEØ‹}Ø�Q‘‘Ø“Ø�Q‘‘à  Q¡™x¨!°©E©(Ñ2Ñ2�Ø�Q‘�Ø�Q‘�ð ‹i˜A�Ið Ðr   c                 ó€   • U R                   S:X  d  UR                   S:X  a  gU R                  U5      U R                   -  $ )zHCalculates the cosine similarity between this vector and another
vector.r   )r   rX   )r   rO   s     r   Ú
similarityÚVector.similarity–   s5   € ð �>‰>˜QÓ %§/¡/°QÓ"6Øà�x‰x˜‹ §¡Ñ/Ð/r   r
   r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r   r(   r1   r/   rB   rH   Úpropertyr   rX   r[   Ú__static_attributes__r5   r   r   r   r      sO   † ñô$'ò>ò#ò")òH
;ô9ò(ò@ð
 ñ	ó ð	òõ.0r   r   N)Úmathr   Úlunr.exceptionsr   r   r5   r   r   Ú<module>rf      s   ðÝ å -÷V0ò V0r   