o
    giN'                     @   s   d dl Z d dlZd dlmZ G dd dZG dd deZG dd deZG d	d
 d
eZG dd deZdd Z	G dd de
ZG dd deZG dd deZdddZdS )    N)defaultdictc                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )
Expressionc                 C   s   d S N selfr   r   S/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/einx/_src/util/solver.py__init__   s   zExpression.__init__c                 C      t | |gS r   Sumr   otherr   r   r   __add__
      zExpression.__add__c                 C      t || gS r   r   r   r   r   r   __radd__   r   zExpression.__radd__c                 C   r
   r   Productr   r   r   r   __mul__   r   zExpression.__mul__c                 C   r   r   r   r   r   r   r   __rmul__   r   zExpression.__rmul__N)__name__
__module____qualname__r	   r   r   r   r   r   r   r   r   r      s    r   c                   @   <   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )Variablec                 C   s   t |  || _|| _d S r   )r   r	   idname)r   r   r   r   r   r   r	      s   

zVariable.__init__c                 c       | V  d S r   r   r   r   r   r   __iter__      
zVariable.__iter__c                 C      t |to
| j|jkS r   
isinstancer   r   r   r   r   r   __eq__       zVariable.__eq__c                 C   
   t | jS r   )hashr   r   r   r   r   __hash__#      
zVariable.__hash__c                 C   s   | j  S r   r   r   r   r   r   __str__&   s   zVariable.__str__c                 C   s   t j| jdddS )NT)integernonnegative)sympySymbolr   r   r   r   r   r.   )   s   zVariable.sympyN	r   r   r   r	   r   r$   r(   r+   r.   r   r   r   r   r      s    r   c                   @   r   )Constantc                 C   s   t |  || _d S r   )r   r	   value)r   r2   r   r   r   r	   .   s   

zConstant.__init__c                 c   r   r   r   r   r   r   r   r   2   r    zConstant.__iter__c                 C   r!   r   )r#   r1   r2   r   r   r   r   r$   5   r%   zConstant.__eq__c                 C   r&   r   )r'   r2   r   r   r   r   r(   8   r)   zConstant.__hash__c                 C   r&   r   )strr2   r   r   r   r   r+   ;   r)   zConstant.__str__c                 C   s   | j S r   r2   r   r   r   r   r.   >   s   zConstant.sympyNr0   r   r   r   r   r1   -   s    r1   c                   @   H   e Zd Zedd Zdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )r   c                 C   sX   t | dkr
tdS t | dkr| d S tdd | D r(ttdd | D S t| S )Nr      c                 s       | ]}t |tV  qd S r   r#   r1   .0cr   r   r   	<genexpr>I       zSum.maybe.<locals>.<genexpr>c                 s       | ]}|j V  qd S r   r4   r9   r   r   r   r<   J       )lenr1   allsumr   childrenr   r   r   maybeC   s   z	Sum.maybec                 C      t |  dd |D | _d S )Nc                 S      g | ]}t |qS r   _to_exprr9   r   r   r   
<listcomp>P       z Sum.__init__.<locals>.<listcomp>r   r	   rD   r   rD   r   r   r   r	   N      
zSum.__init__c                 c   "    | V  | j D ]}|E d H  qd S r   rC   r   childr   r   r   r   R   
   
zSum.__iter__c                 C   r!   r   )r#   r   rD   r   r   r   r   r$   W   r%   z
Sum.__eq__c                 C      t t| jS r   r'   tuplerD   r   r   r   r   r(   Z      zSum.__hash__c                 C      d dd | jD S )Nz + c                 s       | ]}t |V  qd S r   r3   r9   r   r   r   r<   ^       zSum.__str__.<locals>.<genexpr>joinrD   r   r   r   r   r+   ]   r%   zSum.__str__c                 C   s   t dd | jD S )Nc                 s   s    | ]}|  V  qd S r   r.   r9   r   r   r   r<   a   rZ   zSum.sympy.<locals>.<genexpr>)rB   rD   r   r   r   r   r.   `   s   z	Sum.sympyNr   r   r   staticmethodrE   r	   r   r$   r(   r+   r.   r   r   r   r   r   B       

r   c                   @   r5   )r   c                 C   sZ   t | dkr
tdS t | dkr| d S tdd | D r)ttdd | D S t| S )Nr   r6   c                 s   r7   r   r8   r9   r   r   r   r<   k   r=   z Product.maybe.<locals>.<genexpr>c                 s   r>   r   r4   r9   r   r   r   r<   l   r?   )r@   r1   rA   mathprodr   rC   r   r   r   rE   e   s   zProduct.maybec                 C   rF   )Nc                 S   rG   r   rH   r9   r   r   r   rJ   r   rK   z$Product.__init__.<locals>.<listcomp>rL   rM   r   r   r   r	   p   rN   zProduct.__init__c                 c   rO   r   rC   rP   r   r   r   r   t   rR   zProduct.__iter__c                 C   r!   r   )r#   r   rD   r   r   r   r   r$   y   r%   zProduct.__eq__c                 C   rS   r   rT   r   r   r   r   r(   |   rV   zProduct.__hash__c                 C   rW   )Nz * c                 s   rX   r   rY   r9   r   r   r   r<      rZ   z"Product.__str__.<locals>.<genexpr>r[   r   r   r   r   r+      r%   zProduct.__str__c                 C   s   t dd | jD S )Nc                 S   s   g | ]}|  qS r   r]   r9   r   r   r   rJ      rK   z!Product.sympy.<locals>.<listcomp>)ra   rb   rD   r   r   r   r   r.      r%   zProduct.sympyNr^   r   r   r   r   r   d   r`   r   c                 C   s2   t | tr	t| S t | tstdt|  | S )NzExpected Expression, got )r#   intr1   r   	TypeErrortype)xr   r   r   rI      s
   

rI   c                       s   e Zd Z fddZ  ZS )SolveExceptionc                    s   t  | d S r   )superr	   r   message	__class__r   r   r	      s   zSolveException.__init__r   r   r   r	   __classcell__r   r   rk   r   rg      s    rg   c                       s   e Zd Zd fdd	Z  ZS )SolveExceptionNoSolution Nc                    sZ   |d u rg }|dd dd |D   }t | || _tdd |D s+tdd S )Nz
Contradicting variables: z, c                 S   rG   r   rY   r:   vr   r   r   rJ      rK   z5SolveExceptionNoSolution.__init__.<locals>.<listcomp>c                 s   r7   r   )r#   r   rq   r   r   r   r<      r=   z4SolveExceptionNoSolution.__init__.<locals>.<genexpr>z;Expected all contradicting variables to be of type Variable)r\   rh   r	   contradicting_variablesrA   rd   )r   rj   rs   rk   r   r   r	      s   z!SolveExceptionNoSolution.__init__)rp   Nrm   r   r   rk   r   ro          ro   c                       s   e Zd Zd fdd	Z  ZS )SolveExceptionTooManySolutionsNc                    s   t  | || _d S r   )rh   r	   rj   ri   rk   r   r   r	      s   
z'SolveExceptionTooManySolutions.__init__r   rm   r   r   rk   r   ru      rt   ru   Fc                    s  dd | D } dd | D } t t| } dd | D tt t }| D ]I\}}t|tr<t|tr< |j |j q$t|trPt|trP |j |j q$t|trmt|trm|j|jkrmt	d|j d|j g q$fdd	 
 D }t|d
krt	d|dd  
 D  dd D }| D ]2\}}t|trt|tr|j|v r|j|v sJ ||j }||j }|D ]}|||< || qqt dd | D  }dd |D i t }|D ]a}	t fdd|	D r  fdd	|	D }
t|
d
ksJ t|
dkr|fdd|	D  ttt|
}ntdt|	 dfdd	|	D  }|	D ]}|vs?J ||< q6qt|d
krRt	d|fddg }t }| D ]?\}}|}|}t|trt|tr|j|jkrfdd||fD }|| q`||kr|||f q`|} t|d
krt	dt ||rtd | D ]\}}td| d|  qi }t| d
kr4d d | D }td!d |D ri }nNtj|d"d"d#}|g kri }n>t|tr0t|d$kr0|\}t|d
krt	d%t|dkrtd&tt|}d'd t|d(d)D }ntd*i }
 D ](\}}t|trK|j||< q:t|tr`|j|v r^||j ||< q:t |S )+Nc                 S   s    g | ]\}}t |t |fqS r   rH   r:   t1t2r   r   r   rJ           zsolve.<locals>.<listcomp>c                 S   s    g | ]\}}||kr||fqS r   r   rv   r   r   r   rJ      ry   c                 S   s2   i | ]}|D ]}|D ]}t |tr
|j|q
qqS r   r"   )r:   equationtermrr   r   r   r   
<dictcomp>   s   2 zsolve.<locals>.<dictcomp>z#Found contradictory input equation z != c                    s$   h | ]\}}t |d kr | qS )r6   )r@   )r:   variable_idvalues	variablesr   r   	<setcomp>      $ zsolve.<locals>.<setcomp>r   z5Found contradictory definitions for the same variablec                 S   s   i | ]	\}}||  qS r   )popr:   krr   r   r   r   r|      s    c                 S   s   i | ]}||hqS r   r   rq   r   r   r   r|      rK   c                 S   s   i | ]}t ||qS r   )r   )r:   sr   r   r   r|          c                 S   s   i | ]
}|D ]}||qqS r   r   )r:   r;   rr   r   r   r   r|          c                 3   s    | ]}| v V  qd S r   r   r:   n	constantsr   r   r<      rZ   zsolve.<locals>.<genexpr>c                    s   h | ]
}| v r | qS r   r   r   r   r   r   r      r   r6   c                 3   s    | ]} | V  qd S r   r   r:   vidr   r   r   r<      rZ   z__EquivalenceClass-zEquivalent expressions c                    s   h | ]} | j qS r   r*   r   r   r   r   r      r   z8Found contradictory constants for same equivalence classc                    sz   t | tr| j v r | j S t | tr| S t | tr(tfdd| jD S t | tr:tfdd| jD S t )Nc                       g | ]} |qS r   r   r9   replacer   r   rJ      rK   z*solve.<locals>.replace.<locals>.<listcomp>c                    r   r   r   r9   r   r   r   rJ      rK   )	r#   r   r   r1   r   rE   rD   r   AssertionError)t)origvar_to_solvevarr   r   r   r      s   



zsolve.<locals>.replacec                    s8   g | ]}|D ]}t |tr |j D ]}| qqqS r   r"   )r:   tov0r   )variableid_to_classr   r   r   rJ      s   8 z5Found contradictory values for same equivalence classz)Solving equations of equivalence classes:z  z = c                 S   s$   g | ]\}}t |  |  qS r   )r.   Eqrv   r   r   r   rJ     r   c                 s   s    | ]
}|j o
t|V  qd S r   )
is_Booleanbool)r:   eqr   r   r   r<     s    T)setmanual   zSympy returned no solutionsz*Sympy returned multiple possible solutionsc                 S   s$   i | ]\}}|j rt|t|qS r   )	is_numberr3   rc   r   r   r   r   r|     r   F)strictz Sympy returned unexpected result)listr   r   r#   r   r1   r   addr2   ro   itemsr@   r~   anyupdatenextiterappendprintrA   r.   solverU   ru   zipr   )	equationsverbosers   rw   rx   classesset1set2t_idequiv_classclass_constantsrr   r   
equations2t1ot2ovars	solutionssympy_equationsorig_solutionsr   r   )r   r   r   r   r   r   r      s   


(



r   )F)r.   ra   collectionsr   r   r   r1   r   r   rI   	Exceptionrg   ro   ru   r   r   r   r   r   <module>   s    ""	