o
    jgQ                     @   s\  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ ddlm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"eZ#G dd de"eZ$edZ%edZ&e!dZ'e d Z(ed!Z)ed"Z*d#S )$z&Simple Harmonic Oscillator 1-Dimension    )IInteger)S)Symbol)sqrt)hbar)Operator)BraKetState)QExpr)XPx)KroneckerDelta)ComplexSpace)matrix_zerosc                   @   s(   e Zd ZdZedd Zedd ZdS )SHOOpz_A base class for the SHO Operators.

    We are limiting the number of arguments to be 1.

    c                 C   s"   t |}t|dkr|S td)N   zToo many arguments)r   
_eval_argslen
ValueError)clsargs r   S/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/physics/quantum/sho1d.pyr      s   
zSHOOp._eval_argsc                 C   
   t tjS Nr   r   Infinityr   labelr   r   r   _eval_hilbert_space!      
zSHOOp._eval_hilbert_spaceN)__name__
__module____qualname____doc__classmethodr   r!   r   r   r   r   r      s    
r   c                   @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )	RaisingOpa  The Raising Operator or a^dagger.

    When a^dagger acts on a state it raises the state up by one. Taking
    the adjoint of a^dagger returns 'a', the Lowering Operator. a^dagger
    can be rewritten in terms of position and momentum. We can represent
    a^dagger as a matrix, which will be its default basis.

    Parameters
    ==========

    args : tuple
        The list of numbers or parameters that uniquely specify the
        operator.

    Examples
    ========

    Create a Raising Operator and rewrite it in terms of position and
    momentum, and show that taking its adjoint returns 'a':

        >>> from sympy.physics.quantum.sho1d import RaisingOp
        >>> from sympy.physics.quantum import Dagger

        >>> ad = RaisingOp('a')
        >>> ad.rewrite('xp').doit()
        sqrt(2)*(m*omega*X - I*Px)/(2*sqrt(hbar)*sqrt(m*omega))

        >>> Dagger(ad)
        a

    Taking the commutator of a^dagger with other Operators:

        >>> from sympy.physics.quantum import Commutator
        >>> from sympy.physics.quantum.sho1d import RaisingOp, LoweringOp
        >>> from sympy.physics.quantum.sho1d import NumberOp

        >>> ad = RaisingOp('a')
        >>> a = LoweringOp('a')
        >>> N = NumberOp('N')
        >>> Commutator(ad, a).doit()
        -1
        >>> Commutator(ad, N).doit()
        -RaisingOp(a)

    Apply a^dagger to a state:

        >>> from sympy.physics.quantum import qapply
        >>> from sympy.physics.quantum.sho1d import RaisingOp, SHOKet

        >>> ad = RaisingOp('a')
        >>> k = SHOKet('k')
        >>> qapply(ad*k)
        sqrt(k + 1)*|k + 1>

    Matrix Representation

        >>> from sympy.physics.quantum.sho1d import RaisingOp
        >>> from sympy.physics.quantum.represent import represent
        >>> ad = RaisingOp('a')
        >>> represent(ad, basis=N, ndim=4, format='sympy')
        Matrix([
        [0,       0,       0, 0],
        [1,       0,       0, 0],
        [0, sqrt(2),       0, 0],
        [0,       0, sqrt(3), 0]])

    c                 O   s8   t jttdt t t  t jt t	 tt t
   S N   )r   Oner   r   r   momegaNegativeOner   r   r   selfr   kwargsr   r   r   _eval_rewrite_as_xpj   s   zRaisingOp._eval_rewrite_as_xpc                 C   
   t | j S r   )
LoweringOpr   r0   r   r   r   _eval_adjointn      
zRaisingOp._eval_adjointc                 C      t jS r   r   r.   r0   otherr   r   r   _eval_commutator_LoweringOpq      z%RaisingOp._eval_commutator_LoweringOpc                 C   s
   t j|  S r   r9   r:   r   r   r   _eval_commutator_NumberOpt   r7   z#RaisingOp._eval_commutator_NumberOpc                 K   s   |j tj }t|t| S r   )nr   r+   r   SHOKetr0   ketoptionstempr   r   r   _apply_operator_SHOKetw   s   z RaisingOp._apply_operator_SHOKetc                 K      | j di |S Nr   _represent_NumberOpr0   rC   r   r   r   _represent_default_basis{      z"RaisingOp._represent_default_basisc                 K      t dNz*Position representation is not implementedNotImplementedErrorr0   basisrC   r   r   r   _represent_XOp~      zRaisingOp._represent_XOpc                 K   s|   | dd}| dd}t||fi |}t|d D ]}t|d }|dkr+t|}|||d |f< q|dkr<| }|S Nndim   formatsympyr   scipy.sparsegetr   ranger   floattocsrr0   rR   rC   	ndim_inforX   matrixivaluer   r   r   rI         zRaisingOp._represent_NumberOpc                 G   s(   |j | jd g|R  }d| jj|f S )Nr   z%s(%s))_printr   	__class__r#   )r0   printerr   arg0r   r   r   _print_contents   s   zRaisingOp._print_contentsc                 G   s4   ddl m} |j| jd g|R  }||d }|S )Nr   )
prettyFormu   †) sympy.printing.pretty.stringpictrk   rf   r   )r0   rh   r   rk   pformr   r   r   _print_contents_pretty   s   z RaisingOp._print_contents_prettyc                 G   s   | | jd }d| S )Nr   z%s^{\dagger})rf   r   )r0   rh   r   argr   r   r   _print_contents_latex   s   zRaisingOp._print_contents_latexN)r#   r$   r%   r&   r2   r6   r<   r>   rE   rK   rS   rI   rj   rn   rp   r   r   r   r   r(   %   s    Dr(   c                   @   P   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )r4   a  The Lowering Operator or 'a'.

    When 'a' acts on a state it lowers the state up by one. Taking
    the adjoint of 'a' returns a^dagger, the Raising Operator. 'a'
    can be rewritten in terms of position and momentum. We can
    represent 'a' as a matrix, which will be its default basis.

    Parameters
    ==========

    args : tuple
        The list of numbers or parameters that uniquely specify the
        operator.

    Examples
    ========

    Create a Lowering Operator and rewrite it in terms of position and
    momentum, and show that taking its adjoint returns a^dagger:

        >>> from sympy.physics.quantum.sho1d import LoweringOp
        >>> from sympy.physics.quantum import Dagger

        >>> a = LoweringOp('a')
        >>> a.rewrite('xp').doit()
        sqrt(2)*(m*omega*X + I*Px)/(2*sqrt(hbar)*sqrt(m*omega))

        >>> Dagger(a)
        RaisingOp(a)

    Taking the commutator of 'a' with other Operators:

        >>> from sympy.physics.quantum import Commutator
        >>> from sympy.physics.quantum.sho1d import LoweringOp, RaisingOp
        >>> from sympy.physics.quantum.sho1d import NumberOp

        >>> a = LoweringOp('a')
        >>> ad = RaisingOp('a')
        >>> N = NumberOp('N')
        >>> Commutator(a, ad).doit()
        1
        >>> Commutator(a, N).doit()
        a

    Apply 'a' to a state:

        >>> from sympy.physics.quantum import qapply
        >>> from sympy.physics.quantum.sho1d import LoweringOp, SHOKet

        >>> a = LoweringOp('a')
        >>> k = SHOKet('k')
        >>> qapply(a*k)
        sqrt(k)*|k - 1>

    Taking 'a' of the lowest state will return 0:

        >>> from sympy.physics.quantum import qapply
        >>> from sympy.physics.quantum.sho1d import LoweringOp, SHOKet

        >>> a = LoweringOp('a')
        >>> k = SHOKet(0)
        >>> qapply(a*k)
        0

    Matrix Representation

        >>> from sympy.physics.quantum.sho1d import LoweringOp
        >>> from sympy.physics.quantum.represent import represent
        >>> a = LoweringOp('a')
        >>> represent(a, basis=N, ndim=4, format='sympy')
        Matrix([
        [0, 1,       0,       0],
        [0, 0, sqrt(2),       0],
        [0, 0,       0, sqrt(3)],
        [0, 0,       0,       0]])

    c                 O   s2   t jttdt t t  tt tt t	   S r)   )
r   r+   r   r   r   r,   r-   r   r   r   r/   r   r   r   r2      s   zLoweringOp._eval_rewrite_as_xpc                 C   r3   r   )r(   r   r5   r   r   r   r6      r7   zLoweringOp._eval_adjointc                 C   r8   r   )r   r+   r:   r   r   r   _eval_commutator_RaisingOp   r=   z%LoweringOp._eval_commutator_RaisingOpc                 C   s   | S r   r   r:   r   r   r   r>         z$LoweringOp._eval_commutator_NumberOpc                 K   s2   |j td }|j tju rtjS t|j t| S )Nr   )r?   r   r   Zeror   r@   rA   r   r   r   rE     s   z!LoweringOp._apply_operator_SHOKetc                 K   rF   rG   rH   rJ   r   r   r   rK     rL   z#LoweringOp._represent_default_basisc                 K   rM   rN   rO   rQ   r   r   r   rS     rT   zLoweringOp._represent_XOpc                 K   s|   | dd}| dd}t||fi |}t|d D ]}t|d }|dkr+t|}||||d f< q|dkr<| }|S rU   r[   r`   r   r   r   rI     re   zLoweringOp._represent_NumberOpN)r#   r$   r%   r&   r2   r6   rr   r>   rE   rK   rS   rI   r   r   r   r   r4      s    Nr4   c                   @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )NumberOpab  The Number Operator is simply a^dagger*a

    It is often useful to write a^dagger*a as simply the Number Operator
    because the Number Operator commutes with the Hamiltonian. And can be
    expressed using the Number Operator. Also the Number Operator can be
    applied to states. We can represent the Number Operator as a matrix,
    which will be its default basis.

    Parameters
    ==========

    args : tuple
        The list of numbers or parameters that uniquely specify the
        operator.

    Examples
    ========

    Create a Number Operator and rewrite it in terms of the ladder
    operators, position and momentum operators, and Hamiltonian:

        >>> from sympy.physics.quantum.sho1d import NumberOp

        >>> N = NumberOp('N')
        >>> N.rewrite('a').doit()
        RaisingOp(a)*a
        >>> N.rewrite('xp').doit()
        -1/2 + (m**2*omega**2*X**2 + Px**2)/(2*hbar*m*omega)
        >>> N.rewrite('H').doit()
        -1/2 + H/(hbar*omega)

    Take the Commutator of the Number Operator with other Operators:

        >>> from sympy.physics.quantum import Commutator
        >>> from sympy.physics.quantum.sho1d import NumberOp, Hamiltonian
        >>> from sympy.physics.quantum.sho1d import RaisingOp, LoweringOp

        >>> N = NumberOp('N')
        >>> H = Hamiltonian('H')
        >>> ad = RaisingOp('a')
        >>> a = LoweringOp('a')
        >>> Commutator(N,H).doit()
        0
        >>> Commutator(N,ad).doit()
        RaisingOp(a)
        >>> Commutator(N,a).doit()
        -a

    Apply the Number Operator to a state:

        >>> from sympy.physics.quantum import qapply
        >>> from sympy.physics.quantum.sho1d import NumberOp, SHOKet

        >>> N = NumberOp('N')
        >>> k = SHOKet('k')
        >>> qapply(N*k)
        k*|k>

    Matrix Representation

        >>> from sympy.physics.quantum.sho1d import NumberOp
        >>> from sympy.physics.quantum.represent import represent
        >>> N = NumberOp('N')
        >>> represent(N, basis=N, ndim=4, format='sympy')
        Matrix([
        [0, 0, 0, 0],
        [0, 1, 0, 0],
        [0, 0, 2, 0],
        [0, 0, 0, 3]])

    c                 O   s   t t S r   )adar/   r   r   r   _eval_rewrite_as_aj  s   zNumberOp._eval_rewrite_as_ac                 O   s8   t jtdt t t  td tt t d   t j S r)   )	r   r+   r   r,   r   r-   r   r   Halfr/   r   r   r   r2   m  s
   zNumberOp._eval_rewrite_as_xpc                 O   s   t tt  tj S r   )Hr   r-   r   ry   r/   r   r   r   _eval_rewrite_as_Hq     zNumberOp._eval_rewrite_as_Hc                 K   s
   |j | S r   )r?   r0   rB   rC   r   r   r   rE   t  r7   zNumberOp._apply_operator_SHOKetc                 C   r8   r   r   rt   r:   r   r   r   _eval_commutator_Hamiltonianw  r=   z%NumberOp._eval_commutator_Hamiltonianc                 C   s   |S r   r   r:   r   r   r   rr   z  rs   z#NumberOp._eval_commutator_RaisingOpc                 C   s
   t j| S r   r9   r:   r   r   r   r<   }  r7   z$NumberOp._eval_commutator_LoweringOpc                 K   rF   rG   rH   rJ   r   r   r   rK     rL   z!NumberOp._represent_default_basisc                 K   rM   rN   rO   rQ   r   r   r   rS     rT   zNumberOp._represent_XOpc                 K   sl   | dd}| dd}t||fi |}t|D ]}|}|dkr%t|}||||f< q|dkr4| }|S NrV   rW   rX   rY   rZ   )r\   r   r]   r^   r_   r`   r   r   r   rI     s   zNumberOp._represent_NumberOpN)r#   r$   r%   r&   rx   r2   r{   rE   r   rr   r<   rK   rS   rI   r   r   r   r   ru   !  s    Hru   c                   @   rq   )Hamiltoniana.  The Hamiltonian Operator.

    The Hamiltonian is used to solve the time-independent Schrodinger
    equation. The Hamiltonian can be expressed using the ladder operators,
    as well as by position and momentum. We can represent the Hamiltonian
    Operator as a matrix, which will be its default basis.

    Parameters
    ==========

    args : tuple
        The list of numbers or parameters that uniquely specify the
        operator.

    Examples
    ========

    Create a Hamiltonian Operator and rewrite it in terms of the ladder
    operators, position and momentum, and the Number Operator:

        >>> from sympy.physics.quantum.sho1d import Hamiltonian

        >>> H = Hamiltonian('H')
        >>> H.rewrite('a').doit()
        hbar*omega*(1/2 + RaisingOp(a)*a)
        >>> H.rewrite('xp').doit()
        (m**2*omega**2*X**2 + Px**2)/(2*m)
        >>> H.rewrite('N').doit()
        hbar*omega*(1/2 + N)

    Take the Commutator of the Hamiltonian and the Number Operator:

        >>> from sympy.physics.quantum import Commutator
        >>> from sympy.physics.quantum.sho1d import Hamiltonian, NumberOp

        >>> H = Hamiltonian('H')
        >>> N = NumberOp('N')
        >>> Commutator(H,N).doit()
        0

    Apply the Hamiltonian Operator to a state:

        >>> from sympy.physics.quantum import qapply
        >>> from sympy.physics.quantum.sho1d import Hamiltonian, SHOKet

        >>> H = Hamiltonian('H')
        >>> k = SHOKet('k')
        >>> qapply(H*k)
        hbar*k*omega*|k> + hbar*omega*|k>/2

    Matrix Representation

        >>> from sympy.physics.quantum.sho1d import Hamiltonian
        >>> from sympy.physics.quantum.represent import represent

        >>> H = Hamiltonian('H')
        >>> represent(H, basis=N, ndim=4, format='sympy')
        Matrix([
        [hbar*omega/2,              0,              0,              0],
        [           0, 3*hbar*omega/2,              0,              0],
        [           0,              0, 5*hbar*omega/2,              0],
        [           0,              0,              0, 7*hbar*omega/2]])

    c                 O   s   t t tt tj  S r   )r   r-   rv   rw   r   ry   r/   r   r   r   rx     s   zHamiltonian._eval_rewrite_as_ac                 O   s*   t jtdt  td tt t d   S r)   )r   r+   r   r,   r   r-   r   r/   r   r   r   r2     s   *zHamiltonian._eval_rewrite_as_xpc                 O   s   t t ttj  S r   )r   r-   Nr   ry   r/   r   r   r   _eval_rewrite_as_N  r|   zHamiltonian._eval_rewrite_as_Nc                 K   s   t t |jtj  | S r   )r   r-   r?   r   ry   r}   r   r   r   rE     s   z"Hamiltonian._apply_operator_SHOKetc                 C   r8   r   r~   r:   r   r   r   r>     r=   z%Hamiltonian._eval_commutator_NumberOpc                 K   rF   rG   rH   rJ   r   r   r   rK     rL   z$Hamiltonian._represent_default_basisc                 K   rM   rN   rO   rQ   r   r   r   rS     rT   zHamiltonian._represent_XOpc                 K   sz   | dd}| dd}t||fi |}t|D ]}|tj }|dkr(t|}||||f< q|dkr7| }tt | S r   )	r\   r   r]   r   ry   r^   r_   r   r-   r`   r   r   r   rI     s   
zHamiltonian._represent_NumberOpN)r#   r$   r%   r&   rx   r2   r   rE   r>   rK   rS   rI   r   r   r   r   r     s    Ar   c                   @   s(   e Zd ZdZedd Zedd ZdS )SHOStatezState class for SHO statesc                 C   r   r   r   r   r   r   r   r!     r"   zSHOState._eval_hilbert_spacec                 C   s
   | j d S )Nr   )r   r5   r   r   r   r?     r"   z
SHOState.nN)r#   r$   r%   r&   r'   r!   propertyr?   r   r   r   r   r     s    
r   c                   @   s4   e Zd ZdZedd Zdd Zdd Zdd	 Zd
S )r@   a  1D eigenket.

    Inherits from SHOState and Ket.

    Parameters
    ==========

    args : tuple
        The list of numbers or parameters that uniquely specify the ket
        This is usually its quantum numbers or its symbol.

    Examples
    ========

    Ket's know about their associated bra:

        >>> from sympy.physics.quantum.sho1d import SHOKet

        >>> k = SHOKet('k')
        >>> k.dual
        <k|
        >>> k.dual_class()
        <class 'sympy.physics.quantum.sho1d.SHOBra'>

    Take the Inner Product with a bra:

        >>> from sympy.physics.quantum import InnerProduct
        >>> from sympy.physics.quantum.sho1d import SHOKet, SHOBra

        >>> k = SHOKet('k')
        >>> b = SHOBra('b')
        >>> InnerProduct(b,k).doit()
        KroneckerDelta(b, k)

    Vector representation of a numerical state ket:

        >>> from sympy.physics.quantum.sho1d import SHOKet, NumberOp
        >>> from sympy.physics.quantum.represent import represent

        >>> k = SHOKet(3)
        >>> N = NumberOp('N')
        >>> represent(k, basis=N, ndim=4)
        Matrix([
        [0],
        [0],
        [0],
        [1]])

    c                 C      t S r   )SHOBrar5   r   r   r   
dual_classC     zSHOKet.dual_classc                 K   s   t | j|j}|S r   )r   r?   )r0   brahintsresultr   r   r   _eval_innerproduct_SHOBraG  s   z SHOKet._eval_innerproduct_SHOBrac                 K   rF   rG   rH   rJ   r   r   r   rK   K  rL   zSHOKet._represent_default_basisc                 K   s   | dd}| dd}d|d< t|dfi |}t| jtrT| j|kr(tdS |d	kr;d
|t| jdf< | }|S |dkrJd
|t| jdf< |S tj	|| jdf< |S tdS NrV   rW   rX   rY   lilspmatrixr   zN-Dimension too smallrZ   g      ?r   numpyzNot Numerical State
r\   r   
isinstancer?   r   r   intr_   r   r+   r0   rR   rC   ra   rX   vectorr   r   r   rI   N  "   
zSHOKet._represent_NumberOpN)	r#   r$   r%   r&   r'   r   r   rK   rI   r   r   r   r   r@     s    2
r@   c                   @   s,   e Zd ZdZedd Zdd Zdd ZdS )	r   aK  A time-independent Bra in SHO.

    Inherits from SHOState and Bra.

    Parameters
    ==========

    args : tuple
        The list of numbers or parameters that uniquely specify the ket
        This is usually its quantum numbers or its symbol.

    Examples
    ========

    Bra's know about their associated ket:

        >>> from sympy.physics.quantum.sho1d import SHOBra

        >>> b = SHOBra('b')
        >>> b.dual
        |b>
        >>> b.dual_class()
        <class 'sympy.physics.quantum.sho1d.SHOKet'>

    Vector representation of a numerical state bra:

        >>> from sympy.physics.quantum.sho1d import SHOBra, NumberOp
        >>> from sympy.physics.quantum.represent import represent

        >>> b = SHOBra(3)
        >>> N = NumberOp('N')
        >>> represent(b, basis=N, ndim=4)
        Matrix([[0, 0, 0, 1]])

    c                 C   r   r   )r@   r5   r   r   r   r     r   zSHOBra.dual_classc                 K   rF   rG   rH   rJ   r   r   r   rK     rL   zSHOBra._represent_default_basisc                 K   s   | dd}| dd}d|d< td|fi |}t| jtrT| j|kr(tdS |d	kr;d
|dt| jf< | }|S |dkrJd
|dt| jf< |S tj	|d| jf< |S tdS r   r   r   r   r   r   rI     r   zSHOBra._represent_NumberOpN)r#   r$   r%   r&   r'   r   rK   rI   r   r   r   r   r   b  s    $
r   rw   rz   r   r-   r,   N)+r&   sympy.core.numbersr   r   sympy.core.singletonr   sympy.core.symbolr   (sympy.functions.elementary.miscellaneousr   sympy.physics.quantum.constantsr   sympy.physics.quantum.operatorr   sympy.physics.quantum.stater	   r
   r   sympy.physics.quantum.qexprr   sympy.physics.quantum.cartesianr   r   (sympy.functions.special.tensor_functionsr   sympy.physics.quantum.hilbertr   !sympy.physics.quantum.matrixutilsr   r   r(   r4   ru   r   r   r@   r   rv   rw   rz   r   r-   r,   r   r   r   r   <module>   s8     |xkR@