3
O4W^²)  ã               @   s  d Z ddlZddddddd	d
dddddddddddddddgZG dd„ deƒZG dd„ deƒZdd„ ZG dd„ deeƒ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G d(d)„ d)eƒZG d*d+„ d+eƒZG d,d-„ d-eƒZG d.d/„ d/eƒZG d0d1„ d1eƒZG d2d3„ d3eƒZG d4d5„ d5eƒZG d6d7„ d7eƒZG d8d9„ d9eƒZG d:d;„ d;eƒZG d<d=„ d=eƒZG d>d?„ d?eƒZG d@dA„ dAeƒZeƒ ZeZe	ƒ ZedƒZedBƒZe
Zeƒ Z eƒ Z!edCƒZ"eZ#eZ$eZ%edCdDƒZ&eZ'eZ(eƒ Z)eƒ Z*eƒ Z+eƒ Z,eZ-eZ.dS )Eaj  
The following constraints are implemented:

- ``constraints.boolean``
- ``constraints.cat``
- ``constraints.dependent``
- ``constraints.greater_than(lower_bound)``
- ``constraints.integer_interval(lower_bound, upper_bound)``
- ``constraints.interval(lower_bound, upper_bound)``
- ``constraints.lower_cholesky``
- ``constraints.lower_triangular``
- ``constraints.nonnegative_integer``
- ``constraints.positive``
- ``constraints.positive_definite``
- ``constraints.positive_integer``
- ``constraints.real``
- ``constraints.real_vector``
- ``constraints.simplex``
- ``constraints.stack``
- ``constraints.unit_interval``
é    NÚ
ConstraintÚbooleanÚcatÚ	dependentÚdependent_propertyÚgreater_thanÚgreater_than_eqÚinteger_intervalÚintervalÚhalf_open_intervalÚis_dependentÚ	less_thanÚlower_choleskyÚlower_triangularÚnonnegative_integerÚpositiveÚpositive_definiteÚpositive_integerÚrealÚreal_vectorÚsimplexÚstackÚunit_intervalc               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   z®
    Abstract base class for constraints.

    A constraint object represents a region over which a variable is valid,
    e.g. within which a variable can be optimized.
    c             C   s   t ‚dS )z‘
        Returns a byte tensor of `sample_shape + batch_shape` indicating
        whether each event in value satisfies this constraint.
        N)ÚNotImplementedError)ÚselfÚvalue© r   ú@/tmp/pip-build-e66j97b2/torch/torch/distributions/constraints.pyÚcheck;   s    zConstraint.checkc             C   s   | j jdd … d S )Né   z())Ú	__class__Ú__name__)r   r   r   r   Ú__repr__B   s    zConstraint.__repr__N)r!   Ú
__module__Ú__qualname__Ú__doc__r   r"   r   r   r   r   r   4   s   c               @   s   e Zd ZdZdd„ ZdS )Ú
_Dependentz�
    Placeholder for variables whose support depends on other variables.
    These variables obey no simple coordinate-wise constraints.
    c             C   s   t dƒ‚d S )Nz1Cannot determine validity of dependent constraint)Ú
ValueError)r   Úxr   r   r   r   K   s    z_Dependent.checkN)r!   r#   r$   r%   r   r   r   r   r   r&   F   s   r&   c             C   s
   t | tƒS )N)Ú
isinstancer&   )Ú
constraintr   r   r   r   O   s    c               @   s   e Zd ZdZdS )Ú_DependentPropertyaÈ  
    Decorator that extends @property to act like a `Dependent` constraint when
    called on a class and act like a property when called on an object.

    Example::

        class Uniform(Distribution):
            def __init__(self, low, high):
                self.low = low
                self.high = high
            @constraints.dependent_property
            def support(self):
                return constraints.interval(self.low, self.high)
    N)r!   r#   r$   r%   r   r   r   r   r+   S   s   r+   c               @   s   e Zd ZdZdd„ ZdS )Ú_Booleanz/
    Constrain to the two values `{0, 1}`.
    c             C   s   |dk|dkB S )Nr   r   r   )r   r   r   r   r   r   i   s    z_Boolean.checkN)r!   r#   r$   r%   r   r   r   r   r   r,   e   s   r,   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_IntegerIntervalzH
    Constrain to an integer interval `[lower_bound, upper_bound]`.
    c             C   s   || _ || _d S )N)Úlower_boundÚupper_bound)r   r.   r/   r   r   r   Ú__init__q   s    z_IntegerInterval.__init__c             C   s    |d dk| j |k@ || jk@ S )Nr   r   )r.   r/   )r   r   r   r   r   r   u   s    z_IntegerInterval.checkc             C   s(   | j jdd … }|dj| j| jƒ7 }|S )Nr   z (lower_bound={}, upper_bound={}))r    r!   Úformatr.   r/   )r   Ú
fmt_stringr   r   r   r"   x   s    z_IntegerInterval.__repr__N)r!   r#   r$   r%   r0   r   r"   r   r   r   r   r-   m   s   r-   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_IntegerLessThanzA
    Constrain to an integer interval `(-inf, upper_bound]`.
    c             C   s
   || _ d S )N)r/   )r   r/   r   r   r   r0   ‚   s    z_IntegerLessThan.__init__c             C   s   |d dk|| j k@ S )Nr   r   )r/   )r   r   r   r   r   r   …   s    z_IntegerLessThan.checkc             C   s$   | j jdd … }|dj| jƒ7 }|S )Nr   z(upper_bound={}))r    r!   r1   r/   )r   r2   r   r   r   r"   ˆ   s    z_IntegerLessThan.__repr__N)r!   r#   r$   r%   r0   r   r"   r   r   r   r   r3   ~   s   r3   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_IntegerGreaterThanz@
    Constrain to an integer interval `[lower_bound, inf)`.
    c             C   s
   || _ d S )N)r.   )r   r.   r   r   r   r0   ’   s    z_IntegerGreaterThan.__init__c             C   s   |d dk|| j k@ S )Nr   r   )r.   )r   r   r   r   r   r   •   s    z_IntegerGreaterThan.checkc             C   s$   | j jdd … }|dj| jƒ7 }|S )Nr   z(lower_bound={}))r    r!   r1   r.   )r   r2   r   r   r   r"   ˜   s    z_IntegerGreaterThan.__repr__N)r!   r#   r$   r%   r0   r   r"   r   r   r   r   r4   Ž   s   r4   c               @   s   e Zd ZdZdd„ ZdS )Ú_RealzF
    Trivially constrain to the extended real line `[-inf, inf]`.
    c             C   s   ||kS )Nr   )r   r   r   r   r   r   ¢   s    z_Real.checkN)r!   r#   r$   r%   r   r   r   r   r   r5   ž   s   r5   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_GreaterThanz=
    Constrain to a real half line `(lower_bound, inf]`.
    c             C   s
   || _ d S )N)r.   )r   r.   r   r   r   r0   ª   s    z_GreaterThan.__init__c             C   s
   | j |k S )N)r.   )r   r   r   r   r   r   ­   s    z_GreaterThan.checkc             C   s$   | j jdd … }|dj| jƒ7 }|S )Nr   z(lower_bound={}))r    r!   r1   r.   )r   r2   r   r   r   r"   °   s    z_GreaterThan.__repr__N)r!   r#   r$   r%   r0   r   r"   r   r   r   r   r6   ¦   s   r6   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_GreaterThanEqz=
    Constrain to a real half line `[lower_bound, inf)`.
    c             C   s
   || _ d S )N)r.   )r   r.   r   r   r   r0   º   s    z_GreaterThanEq.__init__c             C   s
   | j |kS )N)r.   )r   r   r   r   r   r   ½   s    z_GreaterThanEq.checkc             C   s$   | j jdd … }|dj| jƒ7 }|S )Nr   z(lower_bound={}))r    r!   r1   r.   )r   r2   r   r   r   r"   À   s    z_GreaterThanEq.__repr__N)r!   r#   r$   r%   r0   r   r"   r   r   r   r   r7   ¶   s   r7   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	_LessThanz>
    Constrain to a real half line `[-inf, upper_bound)`.
    c             C   s
   || _ d S )N)r/   )r   r/   r   r   r   r0   Ê   s    z_LessThan.__init__c             C   s
   || j k S )N)r/   )r   r   r   r   r   r   Í   s    z_LessThan.checkc             C   s$   | j jdd … }|dj| jƒ7 }|S )Nr   z(upper_bound={}))r    r!   r1   r/   )r   r2   r   r   r   r"   Ð   s    z_LessThan.__repr__N)r!   r#   r$   r%   r0   r   r"   r   r   r   r   r8   Æ   s   r8   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	_IntervalzD
    Constrain to a real interval `[lower_bound, upper_bound]`.
    c             C   s   || _ || _d S )N)r.   r/   )r   r.   r/   r   r   r   r0   Ú   s    z_Interval.__init__c             C   s   | j |k|| jk@ S )N)r.   r/   )r   r   r   r   r   r   Þ   s    z_Interval.checkc             C   s(   | j jdd … }|dj| j| jƒ7 }|S )Nr   z (lower_bound={}, upper_bound={}))r    r!   r1   r.   r/   )r   r2   r   r   r   r"   á   s    z_Interval.__repr__N)r!   r#   r$   r%   r0   r   r"   r   r   r   r   r9   Ö   s   r9   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_HalfOpenIntervalzD
    Constrain to a real interval `[lower_bound, upper_bound)`.
    c             C   s   || _ || _d S )N)r.   r/   )r   r.   r/   r   r   r   r0   ë   s    z_HalfOpenInterval.__init__c             C   s   | j |k|| jk @ S )N)r.   r/   )r   r   r   r   r   r   ï   s    z_HalfOpenInterval.checkc             C   s(   | j jdd … }|dj| j| jƒ7 }|S )Nr   z (lower_bound={}, upper_bound={}))r    r!   r1   r.   r/   )r   r2   r   r   r   r"   ò   s    z_HalfOpenInterval.__repr__N)r!   r#   r$   r%   r0   r   r"   r   r   r   r   r:   ç   s   r:   c               @   s   e Zd ZdZdd„ ZdS )Ú_Simplexz€
    Constrain to the unit simplex in the innermost (rightmost) dimension.
    Specifically: `x >= 0` and `x.sum(-1) == 1`.
    c             C   s(   t j|dkdd�|jdƒd jƒ dk @ S )Nr   r   )Údimg�íµ ÷Æ°>éÿÿÿÿr=   )ÚtorchÚallÚsumÚabs)r   r   r   r   r   r   ý   s    z_Simplex.checkN)r!   r#   r$   r%   r   r   r   r   r   r;   ø   s   r;   c               @   s   e Zd ZdZdd„ ZdS )Ú_LowerTriangularz8
    Constrain to lower-triangular square matrices.
    c             C   s.   |j ƒ }||kj|jd d… d ƒjdƒd S )Né   r   r   éþÿÿÿr=   )r=   r=   )ÚtrilÚviewÚshapeÚmin)r   r   Ú
value_trilr   r   r   r     s    z_LowerTriangular.checkN)r!   r#   r$   r%   r   r   r   r   r   rB     s   rB   c               @   s   e Zd ZdZdd„ ZdS )Ú_LowerCholeskyzP
    Constrain to lower-triangular square matrices with positive diagonals.
    c             C   sR   |j ƒ }||kj|jd d… d ƒjdƒd }|jd	d
d�dkjdƒd }||@ S )NrC   r   r   )Zdim1Zdim2rD   r=   )r=   r=   rD   r=   r=   )rE   rF   rG   rH   Zdiagonal)r   r   rI   r   Zpositive_diagonalr   r   r   r     s    &z_LowerCholesky.checkN)r!   r#   r$   r%   r   r   r   r   r   rJ   
  s   rJ   c               @   s   e Zd ZdZdd„ ZdS )Ú_PositiveDefinitez2
    Constrain to positive-definite matrices.
    c             C   sJ   |j dd … }|jdƒj d d… }|jd	| ƒ}tjdd„ |D ƒƒj|ƒS )
NrC   r   r   c             S   s(   g | ] }|j d d�d dd… dk‘qS )F)Zeigenvectorsr   Nr   g        )Zsymeig)Ú.0Úvr   r   r   ú
<listcomp>   s   z+_PositiveDefinite.check.<locals>.<listcomp>rD   rD   r=   )r=   )rG   Z	unsqueezeZreshaper>   r   rF   )r   r   Zmatrix_shapeZbatch_shapeZflattened_valuer   r   r   r     s
    
z_PositiveDefinite.checkN)r!   r#   r$   r%   r   r   r   r   r   rK     s   rK   c               @   s   e Zd ZdZdd„ ZdS )Ú_RealVectorz”
    Constrain to real-valued vectors. This is the same as `constraints.real`,
    but additionally reduces across the `event_shape` dimension.
    c             C   s   t j||kdd�S )Nr   )r<   r=   )r>   r?   )r   r   r   r   r   r   )  s    z_RealVector.checkN)r!   r#   r$   r%   r   r   r   r   r   rO   $  s   rO   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )	Ú_CatzÂ
    Constraint functor that applies a sequence of constraints
    `cseq` at the submatrices at dimension `dim`,
    each of size `lengths[dim]`, in a way compatible with :func:`torch.cat`.
    r   Nc             C   sd   t dd„ |D ƒƒst‚t|ƒ| _|d kr8dgt| jƒ }t|ƒ| _t| jƒt| jƒksZt‚|| _d S )Nc             s   s   | ]}t |tƒV  qd S )N)r)   r   )rL   Úcr   r   r   ú	<genexpr>4  s    z _Cat.__init__.<locals>.<genexpr>r   )r?   ÚAssertionErrorÚlistÚcseqÚlenÚlengthsr<   )r   rU   r<   rW   r   r   r   r0   3  s    

z_Cat.__init__c             C   s‚   |j ƒ  | j   ko|j ƒ k n  s(t‚g }d}xBt| j| jƒD ]0\}}|j| j ||ƒ}|j|j|ƒƒ || }q@W tj	|| j ƒS )Nr   )
r<   rS   ÚziprU   rW   ZnarrowÚappendr   r>   r   )r   r   ZchecksÚstartÚconstrÚlengthrM   r   r   r   r   <  s    (z
_Cat.check)r   N)r!   r#   r$   r%   r0   r   r   r   r   r   rP   -  s   
	rP   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )	Ú_Stackz§
    Constraint functor that applies a sequence of constraints
    `cseq` at the submatrices at dimension `dim`,
    in a way compatible with :func:`torch.stack`.
    r   c             C   s*   t dd„ |D ƒƒst‚t|ƒ| _|| _d S )Nc             s   s   | ]}t |tƒV  qd S )N)r)   r   )rL   rQ   r   r   r   rR   N  s    z"_Stack.__init__.<locals>.<genexpr>)r?   rS   rT   rU   r<   )r   rU   r<   r   r   r   r0   M  s    
z_Stack.__init__c                sh   ˆj ƒ  ˆ j   koˆj ƒ k n  s(t‚‡ ‡fdd„tˆjˆ j ƒƒD ƒ}tjdd„ t|ˆ jƒD ƒˆ j ƒS )Nc                s   g | ]}ˆj ˆ j|ƒ‘qS r   )Úselectr<   )rL   Úi)r   r   r   r   rN   T  s    z _Stack.check.<locals>.<listcomp>c             S   s   g | ]\}}|j |ƒ‘qS r   )r   )rL   rM   r[   r   r   r   rN   U  s   )r<   rS   ÚrangeÚsizer>   r   rX   rU   )r   r   Úvsr   )r   r   r   r   R  s    ( 
z_Stack.checkN)r   )r!   r#   r$   r%   r0   r   r   r   r   r   r]   G  s   
r]   r   g        g      ð?)/r%   r>   Ú__all__Úobjectr   r&   r   Úpropertyr+   r,   r-   r3   r4   r5   r6   r7   r8   r9   r:   r;   rB   rJ   rK   rO   rP   r]   r   r   r   r   r   r	   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   Ú<module>   s„   				
