o
    *ήc                     @   s   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
mZ d dlmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ d dlm Z 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- d dl.m/Z/ d dl0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ d dlAmBZB d dlmCZC d dlDmEZEmFZFmGZGmHZHmIZI d dlJmKZKmLZL d dlMmNZNmOZO d dlPmQZQmRZR d dlSmTZT d dlUmVZV dd ZWdd ZXd d! ZYd"d# ZZd$d% Z[d&d' Z\d(d) Z]d*d+ Z^d,d- Z_d.d/ Z`d0d1 Zad2d3 Zbd4d5 Zcd6d7 Zdd8d9 Zed:d; Zfd<d= Zgd>d? Zhd@dA ZidBdC ZjdDdE ZkdFdG ZldHdI ZmdJdK ZndLdM ZodNdO ZpdPdQ ZqdRdS ZrdTdU ZsdVdW ZtdXdY ZudZd[ Zvd\d] Zwd^d_ Zxd`da Zydbdc Zzddde Z{dfdg Z|dhdi Z}djdk Z~dldm Zdndo ZdpS )q    )Add)FunctionLambdadiff)Mod)Catalan
EulerGammaGoldenRatio)EFloatIIntegerRationalpi)Eq)S)Dummysymbols)	factorial)	conjugatesign)explog)sqrt)	Piecewise)atan2cossin)gamma)Integral)Range)For
Assignment
aug_assign)DeclarationVariablefloat32float64value_constrealbool_WhileFunctionPrototypeFunctionDefinitionintegerReturn)UnevaluatedExpr)
Relational)AndOrNot
EquivalentXor)MatrixMatrixSymbol)fcodeFCodePrinter)IndexedBaseIdx)implemented_function)raisesc            
      C   s   t ddd\} }}t|| }tt| | }t|dddks!J t d\}}}t|| }tt|| }	t|	dd	d
  dksDJ t|	ddid dksRJ d S )Nzp q rT)r)   freesource_formatzexp(p + (q + r))x y zFhuman   zexp(re(x) + re(y + z))rerealpartuser_functionsz"exp(realpart(x) + realpart(y + z)))r   r0   absr   r9   lstrip)
pqrq_rexprxyzy_zexpr2 rV   H/tmp/pip-target-vg8gfxp4/lib/python/sympy/printing/tests/test_fortran.pytest_UnevaluatedExpr!   s    rX   c                  C   s0   t d} G dd dt}t|| dksJ d S )NrQ   c                   @   s   e Zd Zdd ZdS )ztest_printmethod.<locals>.nintc                 S   s   d| | jd  S )Nznint(%s)r   )_printargs)selfprinterrV   rV   rW   _fcode1   s   z%test_printmethod.<locals>.nint._fcodeN)__name__
__module____qualname__r]   rV   rV   rV   rW   nint0   s    ra   z      nint(x))r   r   r9   )rQ   ra   rV   rV   rW   test_printmethod-   s   rb   c                     s   t d t ddd} t ddd}tt ddd	d
ksJ tt| ddd	dks*J tt|ddd	dks7J tt fdd d S )NrQ   rR   Tr.   rS   )complex_   r?   standardrA   z#merge(0d0, dsign(1d0, x), x == 0d0)zmerge(0, isign(1, y), y == 0)z/merge(cmplx(0d0, 0d0), z/abs(z), abs(z) == 0d0)c                      s   t t S N)r9   r   rV   rQ   rV   rW   <lambda>=   s    z!test_fcode_sign.<locals>.<lambda>)r   r9   r   r>   NotImplementedError)rR   rS   rV   ri   rW   test_fcode_sign6   s   rl   c                  C   s(  t d\} }t ddd}t| d dksJ t| |d  dks"J tdt| d	 | ||     | d
 |  dks<J tt| dksFJ tt|dksPJ t| d dksZJ tt| dksdJ ttddksnJ t| d dksxJ t| d ddddksJ t| tdd dksJ d S )Nx,ynTrc      z
      x**3z      x**(y**3)   g      @rE   z,      (3.5d0*sin(x))**(-x + y**x)/(x**2 + y)z      sqrt(x)z      sqrt(dble(n))g      ?
   z      sqrt(10.0d0)g      z      1d0/xg       rR   r?   r@   zy = x**(-2.0d0)   z      x**(3.0d0/7.0d0))r   r9   r   r   r   )rQ   rR   rn   rV   rV   rW   test_fcode_Pow@   s   *rs   c                  C   s   t d} ttdddksJ ttdddksJ ttddd	ks%J ttd
ddks0J t| tdd dks=J ttdd|  dksJJ d S )NrQ   ro   rr   z      3.0d0/7.0d0   	   z      2iz      -3.0d0/7.0d0z      x + 3.0d0/7.0d0z      (3.0d0/7.0d0)*x)r   r9   r   ri   rV   rV   rW   test_fcode_RationalR   s   rw   c                   C   ,   t tddks
J t tddksJ d S )NC   z      67z      -1)r9   r   rV   rV   rV   rW   test_fcode_Integer\      r{   c                   C   rx   )Ng      E@z      42.0000000000000d0g@xz      -1.00000000000000d+20)r9   r   rV   rV   rV   rW   test_fcode_Floata   r|   r}   c                     s   t d\ tt t dksJ tt fdd tt fdd tt fdd tt fdd dD ]} tt | d	d
ksMJ t  | d	d
ksYJ q>d S )Nrm   z      sin(x)**cos(y)c                         t t ddS NB   rg   r9   r   rV   rQ   rR   rV   rW   rj   i       z&test_fcode_functions.<locals>.<lambda>c                         t   ddS r   r9   rV   r   rV   rW   rj   j       c                      r~   NM   r   r   rV   r   rV   rW   rj   k   r   c                      r   r   r   rV   r   rV   rW   rj   l   r   )Z   re   i  i  r   z      modulo(x, y))r   r9   r   r   r>   rk   r   r   rV   r   rW   test_fcode_functionsf   s   r   c                  C   s  t  } td\}}}}}}}tt|t||  t||  dks$J tt|d| | |tdd   dks;J tt|t||  t||  dddksSJ t|t| ddd	ksaJ | j|t| | d
ddksrJ | j|t| dddksJ | j|dddksJ tddd\}}	td}
td}td}t	d|	}t	d|}t|
||f ||  || dddksJ d S )Nzx,x_,x__,y,X,X_,Yz&      exp(x_) + sin(x*y) + cos(X__*Y_)rE   rr   z(      2*X_**(7.0d0/2.0d0)*Y*x + exp(x__)F)name_manglingz#      exp(x_) + sin(x*y) + cos(X*Y)z      x - cos(X)me	assign_toz      me = X*sin(x_) + x__muz      mu = X*sin(x_)adz      ad = x__n,mTrc   ArQ   rR   ir   r?   r   rA   zndo i = 1, m
   y(i) = 0
end do
do i = 1, m
   do I_ = 1, n
      y(i) = A(i, I_)*x(I_) + y(i)
   end do
end do)
r:   r   r9   r   r   r   r   doprintr;   r<   )obrQ   x_x__rR   XX_Yrn   mr   r   r   rV   rV   rW   	test_caser   s0   "$&"

 r   c                  C   s   t d} tdd}d}t| td d| ksJ t| td d| ks)J t| ttd d| ks9J tttdd| ksGJ ttddtdd ksXJ t| ttd d| kshJ t| tttd d| kszJ d S )NrQ   rq      z0.8340324452479558d0z      x*%sd0z
      %sd0z
      x*%s)r   r   evalfr9   r   r   )rQ   log10_17loglog10_17rV   rV   rW   "test_fcode_functions_with_integers   s    " (r   c                  C   s  d} t  }ttdt|  ksJ ttdt|  ksJ ttdt|  ks,J ttdt|  ks9J ttdt|  ksFJ ttdddtd ksUJ ttd	d
t|t| fht	 dfkslJ ttd	d
t|t| fht	 dfksJ ttd	d
t|t| fht	 dfksJ ttd	d
t|t| fht	 dfksJ ttd	d
t|t| fht	 dfksJ ttdd	dt|tdfht	 dfksJ d S )Nr   z.      parameter (Catalan = %sd0)
      Catalanz4      parameter (EulerGamma = %sd0)
      EulerGammaz"      parameter (E = %sd0)
      Ez6      parameter (GoldenRatio = %sd0)
      GoldenRatioz$      parameter (pi = %sd0)
      pi   )	precisionFrC   z      Catalanz      EulerGammaz      Ez      GoldenRatioz      pi)r   rD   )
r:   r9   r   r   r   r
   r	   r   rY   set)precrL   rV   rV   rW   test_fcode_NumberSymbol   sH   







r   c                  C   s   t tdksJ td} t dt dksJ t ddt  dks"J t ddt  |  dks0J t t|  dks:J t ddt  |  d	ksHJ tdd
d} t d|  dksXJ t t|  dksbJ t d|  dkslJ d S )Nz      cmplx(0,1)rQ      z      cmplx(0,4)ro   z      cmplx(3,4)z      cmplx(3,4) + xz      cmplx(0,1)*xz      cmplx(3,4) - xT)	imaginaryr   z	      5*xz      x + 3)r9   r   r   ri   rV   rV   rW   test_fcode_complex   s   r   c                  C   sN   t d\} }tt| dksJ tt| |dksJ tt| dks%J d S )Nrm   z      sin(x)z      atan2(x, y)z      conjg(x))r   r9   r   r   r   r   rV   rV   rW   test_implicit   s   r   c                  C   sX   t d} td}tt| }|dksJ ttt| dks J t|| dks*J d S )NrQ   gz:C     Not supported in Fortran:
C     gamma
      gamma(x)zHC     Not supported in Fortran:
C     Integral
      Integral(sin(x), x)z2C     Not supported in Fortran:
C     g
      g(x))r   r   r9   r   r   r   )rQ   r   gamma_frV   rV   rW   test_not_fortran   s   r   c                  C   s   t d} tt| ddiddksJ t d} tt| ddiddks$J td	}t|| d	d
iddks6J t ddd}tt|ddiddksJJ d S )NrQ   r   zsinrH   z      zsin(x)r   mygammaz      mygamma(x)r   greatz      great(x)rn   Trc   r   fctz      fct(n))r   r9   r   r   r   r   )rQ   r   rn   rV   rV   rW   test_user_functions   s    r   c                  C   s   t d} tdt| d|  }t|| dksJ tdt| dt |  }t|| dtd ks3J td}tdt d	d
d}tdt| | d|   d|   }t||| || ddks`J d S )NrQ   r   rE   z	      2*xz(      parameter (pi = %sd0)
      2*pi/xr   r   r   rn   Trc   rp   r   zI      do i = 1, n
         A(i) = (A(i) + 1)*(A(i) + 2)*A(i)
      end do)r   r=   r   r9   r   r   r;   r<   )rQ   r   r   r   rV   rV   rW   test_inline_function   s   

 r   c                  C   s$   t d} tt| dddksJ d S )NrQ   sr   z      s = sin(x))r   r9   r   ri   rV   rV   rW   test_assign_to   s   r   c                     sZ   t d\ } t |  d  dddksJ  fddtdD }tt| d	ks+J d S )
Nrm   rq   varr   z      var = x**10 + 10*x**9*y + 45*x**8*y**2 + 120*x**7*y**3 + 210*x**6*
     @ y**4 + 252*x**5*y**5 + 210*x**4*y**6 + 120*x**3*y**7 + 45*x**2*y
     @ **8 + 10*x*y**9 + y**10c                    s   g | ]} | qS rV   rV   ).0r   ri   rV   rW   
<listcomp>   s    z&test_line_wrapping.<locals>.<listcomp>   zR      x**10 + x**9 + x**8 + x**7 + x**6 + x**5 + x**4 + x**3 + x**2 + x
     @ + 1)r   r9   expandranger   )rR   erV   ri   rW   test_line_wrapping   s   
r   c                  C   s   t d\} }tt| |k || d k dddksJ tt| |k || d k dddks,J tt| |k || d k ddddd	ksAJ tt| |k || d k ddd
ksTJ d S )Nx yrp   r?   r@   zx < y .and. y < x + 1zx < y .or. y < x + 1Fevaluatezx < y .neqv. y < x + 1zx < y .eqv. y < x + 1)r   r9   r2   r3   r6   r5   r   rV   rV   rW   test_fcode_precedence  s   r   c                  C   st  t d\} }}tt| dddksJ tt| |dddks J tt| t|dddks/J ttt| |dddks>J ttt| t|dddksOJ ttt| |d	d
dddks`J tt| |dddksmJ tt| t|dddks|J ttt| |dddksJ ttt| t|dddksJ ttt| |d	d
dddksJ ttt||| dddksJ ttt|| |dddksJ ttt| ||dddksJ ttt||| dddksJ ttt|| |dddksJ ttt| ||dddksJ tt| ||dddksJ tt| |t|dddks.J tt| t||dddks?J ttt| ||dddksPJ ttt| ||d	d
dddkscJ tt| ||dddksrJ tt| |t|dddksJ tt| t||dddksJ ttt| ||dddksJ ttt| ||d	d
ddd ksJ d S )!NrB   r?   r@   z.not. xz	x .and. yzx .and. .not. yzy .and. .not. xz.not. x .and. .not. yFr   z.not. (x .and. y)zx .or. yzx .or. .not. yzy .or. .not. xz.not. x .or. .not. yz.not. (x .or. y)zx .and. (y .or. z)zy .and. (x .or. z)zz .and. (x .or. y)zx .or. y .and. zzy .or. x .and. zzz .or. x .and. yzx .and. y .and. zzx .and. y .and. .not. zzx .and. z .and. .not. yzy .and. z .and. .not. xz.not. (x .and. y .and. z)zx .or. y .or. zzx .or. y .or. .not. zzx .or. z .or. .not. yzy .or. z .or. .not. xz.not. (x .or. y .or. z))r   r9   r4   r2   r3   rQ   rR   rS   rV   rV   rW   test_fcode_Logical  sh        "






r   c                  C   s(  t d\} }}tt| |dddddksJ tt| t|dddddks'J ttt| |dddddks8J ttt| t|ddddd	ksKJ ttt| |ddddddd
ks^J tt| |dddkskJ tt| t|dddkszJ ttt| |dddksJ ttt| t|dddksJ ttt| |dddddksJ ttt||| dddksJ ttt|| |dddksJ ttt| ||dddksJ ttt||| dddksJ ttt|| |dddksJ ttt| ||dddksJ ttt||| dddksJ ttt|| |dddks.J ttt| ||dddks?J ttt||| dddksPJ ttt|| |dddksaJ ttt| ||dddksrJ ttt||dd| dddksJ ttt|| dd|dddksJ ttt| |dd|dddksJ ttt||| dddddksJ ttt|| |ddddd ksJ ttt| ||ddddd!ksJ ttt||| ddddd"ksJ ttt|| |ddddd#ks
J ttt| ||ddddd$ksJ ttt||dd| ddd%ks0J ttt|| dd|ddd&ksCJ ttt| |dd|ddd'ksVJ ttt||| ddddd(ksiJ ttt|| |ddddd)ks|J ttt| ||ddddd*ksJ ttt||dd| ddd+ksJ ttt|| dd|ddd,ksJ ttt| |dd|ddd-ksJ tt| ||ddddd.ksJ tt| |t|ddddd/ksJ tt| t||ddddd0ksJ ttt| ||ddddd1ksJ d S )2NrB   Fr   r?   r@   z
x .neqv. yzx .neqv. .not. yzy .neqv. .not. xz.not. x .neqv. .not. yz.not. (x .neqv. y)z	x .eqv. yzx .eqv. .not. yzy .eqv. .not. xz.not. x .eqv. .not. yz.not. (x .eqv. y)zx .eqv. y .and. zzy .eqv. x .and. zzz .eqv. x .and. yzx .and. (y .eqv. z)zy .and. (x .eqv. z)zz .and. (x .eqv. y)zx .eqv. y .or. zzy .eqv. x .or. zzz .eqv. x .or. yzx .or. (y .eqv. z)zy .or. (x .eqv. z)zz .or. (x .eqv. y)zx .eqv. (y .neqv. z)zy .eqv. (x .neqv. z)zz .eqv. (x .neqv. y)zx .neqv. (y .eqv. z)zy .neqv. (x .eqv. z)zz .neqv. (x .eqv. y)zx .neqv. y .and. zzy .neqv. x .and. zzz .neqv. x .and. yzx .and. (y .neqv. z)zy .and. (x .neqv. z)zz .and. (x .neqv. y)zx .neqv. y .or. zzy .neqv. x .or. zzz .neqv. x .or. yzx .or. (y .neqv. z)zy .or. (x .neqv. z)zz .or. (x .neqv. y)zx .neqv. y .neqv. zzx .neqv. y .neqv. .not. zzx .neqv. z .neqv. .not. yzy .neqv. z .neqv. .not. x)r   r9   r6   r4   r5   r2   r3   r   rV   rV   rW   test_fcode_XlogicalD  s*  



























r   c                  C   s   t d\} }tt| |ddddksJ tt| |ddddks"J tt| |dddd	ks0J tt| |d
dddks>J tt| |ddddksLJ tt| |ddddksZJ d S )Nr   z==r?   r@   zx == yz!=zx /= yz>=zx >= yz<=zx <= y>zx > y<zx < y)r   r9   r1   r   rV   rV   rW   test_fcode_Relational  s    r   c                     sd  t d} t| | dk f| d df tt fdd t dd}d	}||ks(J tt| | dk f| d dfd
ddks=J t| |  }t| |  }tdD ]}t|| }t|| }qMd}tt|| dk f|dfdd}||ksqJ tt| | dk f| d | dkft| dfdd}d}||ksJ t| | dk f| d | dkft| | dkf tt	 fdd d S )NrQ   rp   rE   Tc                         t  S rh   r   rV   rP   rV   rW   rj         z&test_fcode_Piecewise.<locals>.<lambda>re   r   z      merge(x, x**2, x < 1)r   r   zR      if (x < 1) then
         var = x
      else
         var = x**2
      end ifrq   a/        if (x < 0) then
         weird_name = -cos(x)/x + 10*sin(x)/x**2 + 90*cos(x)/x**3 - 720*
     @ sin(x)/x**4 - 5040*cos(x)/x**5 + 30240*sin(x)/x**6 + 151200*cos(x
     @ )/x**7 - 604800*sin(x)/x**8 - 1814400*cos(x)/x**9 + 3628800*sin(x
     @ )/x**10 + 3628800*cos(x)/x**11
      else
         weird_name = -sin(x)/x - 10*cos(x)/x**2 + 90*sin(x)/x**3 + 720*
     @ cos(x)/x**4 - 5040*sin(x)/x**5 - 30240*cos(x)/x**6 + 151200*sin(x
     @ )/x**7 + 604800*cos(x)/x**8 - 1814400*sin(x)/x**9 - 3628800*cos(x
     @ )/x**10 + 3628800*sin(x)/x**11
      end ifr   
weird_namez1      merge(x, merge(x**2, sin(x), x > 1), x < 1)c                      r   rh   r   rV   r   rV   rW   rj     r   )
r   r   r>   rk   r9   r   r   r   r   
ValueError)rQ   codeexpectedabr   rV   r   rW   test_fcode_Piecewise  s.    
0,r   c                  C   sr   t  } g d}| |}g d}|D ]
}t|dksJ qt||D ]
\}}||ks,J q"t|t|ks7J d S )N)zbC     This is a long comment on a single line that must be wrapped properly to produce nice outputzf      this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properlyzg      this = is + a + long + and + nasty + fortran + statement +  that * must + be + wrapped + properlyzh      this = is + a + long + and + nasty + fortran + statement +   that * must + be + wrapped + properlyzd      this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properlyzf      this = is + a + long + and + nasty + fortran + statement +   that*must + be + wrapped + properlyzg      this = is + a + long + and + nasty + fortran + statement +    that*must + be + wrapped + properlyzh      this = is + a + long + and + nasty + fortran + statement +     that*must + be + wrapped + properlyze      this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properlyzf      this = is + a + long + and + nasty + fortran + statement +  that**must + be + wrapped + properlyzg      this = is + a + long + and + nasty + fortran + statement +   that**must + be + wrapped + properlyzh      this = is + a + long + and + nasty + fortran + statement +    that**must + be + wrapped + properlyzi      this = is + a + long + and + nasty + fortran + statement +     that**must + be + wrapped + properlyzc      this = is + a + long + and + nasty + fortran + statement(that)/must + be + wrapped + properlyzg      this = is + a + long + and + nasty + fortran +     statement(that)/must + be + wrapped + properly)zBC     This is a long comment on a single line that must be wrappedz%C     properly to produce nice outputzG      this = is + a + long + and + nasty + fortran + statement + that *%     @ must + be + wrapped + properlyzH      this = is + a + long + and + nasty + fortran + statement +  that *r   G      this = is + a + long + and + nasty + fortran + statement +   thatz'     @ * must + be + wrapped + properlyzF      this = is + a + long + and + nasty + fortran + statement + that*r   zH      this = is + a + long + and + nasty + fortran + statement +   that*r   H      this = is + a + long + and + nasty + fortran + statement +    thatz&     @ *must + be + wrapped + properly@      this = is + a + long + and + nasty + fortran + statement +z*     @ that*must + be + wrapped + properlyzG      this = is + a + long + and + nasty + fortran + statement + that**r   zH      this = is + a + long + and + nasty + fortran + statement +  that**r   r   '     @ **must + be + wrapped + properlyr   r   r   z+     @ that**must + be + wrapped + properlyzE      this = is + a + long + and + nasty + fortran + statement(that)/r   zH      this = is + a + long + and + nasty + fortran +     statement(that)z&     @ /must + be + wrapped + properlyH   )r:   _wrap_fortranlenzip)r\   lineswrapped_linesexpected_lineslinewr   rV   rV   rW   test_wrap_fortran  s   
 r   c                  C   s,   t  } g d}g d}| ||ksJ d S )N)H      this_variable_is_very_long_because_we_try_to_test_line_break=1.0d0zI      this_variable_is_very_long_because_we_try_to_test_line_break =1.0d0zK      this_variable_is_very_long_because_we_try_to_test_line_break  = 1.0d0zL      this_variable_is_very_long_because_we_try_to_test_line_break   = 1.0d0zM      this_variable_is_very_long_because_we_try_to_test_line_break    = 1.0d0zK      this_variable_is_very_long_because_we_try_to_test_line_break = 10.0d0)r   D      this_variable_is_very_long_because_we_try_to_test_line_break =     @ 1.0d0zE      this_variable_is_very_long_because_we_try_to_test_line_break  =r   zF      this_variable_is_very_long_because_we_try_to_test_line_break   =r   zG      this_variable_is_very_long_because_we_try_to_test_line_break    =r   r   z     @ 10.0d0r:   r   r\   r   r   rV   rV   rW   test_wrap_fortran_keep_d0  s   r   c                   C   s   t tdd  d S )Nc                   S   s   t tdddS )Nr   garbage)method)r9   r   rV   rV   rV   rW   rj   3  r   ztest_settings.<locals>.<lambda>)r>   	TypeErrorrV   rV   rV   rW   test_settings2  s   r   c                  C   s0   t d\} }tt| t| dddksJ d S )Nrm   r?   r@   zsin(y) + cos(x))r   r9   r   r   r   rV   rV   rW   test_free_form_code_line6  s   $r   c                  C   s@   t d\} }tt| t| d  dd}d}||ksJ d S )Nrm   rr   r?   r@   zsin(y)**7 + 7*sin(y)**6*cos(x) + 21*sin(y)**5*cos(x)**2 + 35*sin(y)**4* &
      cos(x)**3 + 35*sin(y)**3*cos(x)**4 + 21*sin(y)**2*cos(x)**5 + 7* &
      sin(y)*cos(x)**6 + cos(x)**7)r   r9   r   r   r   )rQ   rR   resultr   rV   rV   rW    test_free_form_continuation_line;  s
    r   c                  C   s0   t ddi} dg}ddg}| ||ksJ d S )NrA   r?   z^! This is a long comment on a single line that must be wrapped properly to produce nice outputzG! This is a long comment on a single line that must be wrapped properlyz! to produce nice outputr   r   rV   rV   rW   test_free_form_comment_lineF  s   r   c            	      C   s   t ddd\} }td}td}td}td|}td| }d	}t|||f ||  || d
d}||ddi ksS||ddi ksU||ddi ksW||ddi ksYJ d S d S d S d S )Nr   Trc   r   rQ   rR   r   jz_do i = 1, m
   y(i) = 0
end do
do i = 1, m
   do j = 1, n
      y(i) = %(rhs)s
   end do
end dor?   r   rhszy(i) + A(i, j)*x(j)zy(i) + x(j)*A(i, j)zx(j)*A(i, j) + y(i)zA(i, j)*x(j) + y(i))r   r;   r<   r9   )	rn   r   r   rQ   rR   r   r   r   r   rV   rV   rW   
test_loopsO  s    

"r   c                  C   sf   t ddtd\} }td}td}t| |} d| jj|jd }t||  ||  dd	}||ks1J d S )
Nzi mT)r.   clsrQ   rR   zMdo i_%(icount)i = 1, m_%(mcount)i
   y(i_%(icount)i) = x(i_%(icount)i)
end do)icountmcountr?   r   )r   r   r;   r<   labeldummy_indexr9   )r   r   rQ   rR   r   r   rV   rV   rW   test_dummy_loopsi  s   
r   c                  C   s   d} t d| fd}t d| fd}t d| d fd}td| d }t|| ||d  ||  ||d  ||   }t|j|jdd	}|d
sIJ d S )Nr   rR   )shaperQ   Dyrp   r   F)r   contractz+Dy(i) = (y(i + 1) - y(i))/(x(i + 1) - x(i)))r;   r<   r   r9   r   lhsendswith)len_yrR   rQ   r   r   r   code0rV   rV   rW   2test_fcode_Indexed_without_looking_for_contractionw  s   2r  c                  C   s:   G dd dt } |  }td}|t|ddksJ d S )Nc                   @   s   e Zd Zej ZdS )z1test_derived_classes.<locals>.MyFancyFCodePrinterN)r^   r_   r`   r:   _default_settingscopyrV   rV   rV   rW   MyFancyFCodePrinter  s    r  rQ   borkz      bork = sin(x))r:   r   r   r   )r  r\   rQ   rV   rV   rW   test_derived_classes  s   r  c                  C   s.   d} d}t ddi}|| }||ksJ d S )Nau  subroutine test(a)
integer :: a, i, j

do
do 
do j = 1, 5
if (a>b) then
if(b>0) then
a = 3
donot_indent_me = 2
do_not_indent_me_either = 2
ifIam_indented_something_went_wrong = 2
if_I_am_indented_something_went_wrong = 2
end should not be unindented here
end if
endif
end do
end do
enddo
end subroutine

subroutine test2(a)
integer :: a
do
a = a + 1
end do 
end subroutine
a  subroutine test(a)
integer :: a, i, j

do
   do 
      do j = 1, 5
         if (a>b) then
            if(b>0) then
               a = 3
               donot_indent_me = 2
               do_not_indent_me_either = 2
               ifIam_indented_something_went_wrong = 2
               if_I_am_indented_something_went_wrong = 2
               end should not be unindented here
            end if
         endif
      end do
   end do
enddo
end subroutine

subroutine test2(a)
integer :: a
do
   a = a + 1
end do 
end subroutine
rA   r?   )r:   indent_code)	codelinesr   rL   r   rV   rV   rW   test_indent  s   
r	  c            	      C   s,  t d\} }}t| | td|  |dkf|dft|g}tddd}t||dks,J td|d	  | dkf|d	 dft|d
  |d  }t|dddksQJ tddd}tddd}tt|d
 dt|d	 g|d
 |d	  |d dgd|d  |d
  t|d d dgg}t||dksJ d S )Nzx,y,zrE   r   Tr   ro   rp   z      A(1, 1) = x*y
      if (y > 0) then
         A(2, 1) = x + 2
      else
         A(2, 1) = y
      end if
      A(3, 1) = sin(z))rE   r   )rp   r   r   r   re   r   z?      merge(2*A(3, 1), A(3, 1), x > 0) + sin(A(2, 1)) + A(1, 1)rM   r   M)ro   r   )r   r   r   z      M(1, 1) = sin(q(2, 1))
      M(2, 1) = q(2, 1) + q(3, 1)
      M(3, 1) = 2*q(5, 1)/q(2, 1)
      M(1, 2) = 0
      M(2, 2) = q(4, 1)
      M(3, 2) = sqrt(q(1, 1)) + 4
      M(1, 3) = cos(q(3, 1))
      M(2, 3) = 5
      M(3, 3) = 0)r   r7   r   r   r8   r9   r   r   )	rQ   rR   rS   matr   rP   rM   r  r   rV   rV   rW   test_Matrix_printing  s&   ,6	
$r  c                  C   sD   t d\} }t| tdddt|| | g}t|}|dks J d S )Nr   r   rq   rE   z3      do x = 0, 10, 2
         y = x*y
      end do)r   r!   r    r"   r9   )rQ   rR   fsolrV   rV   rW   test_fcode_For  s    r  c            
      C   s   dd } t ddd}t|}t|}| |d t d\}}t|tdthd	}t|}| |d
 t|td}t|}	| |	d | td | td | tdttid | tdttid d S )Nc                 [   s"   t | fddd||ksJ d S )Nre   r?   rf   r   )rP   refkwargsrV   rV   rW   check  s   "z%test_fcode_Declaration.<locals>.checkr   Trc   zinteger*4 :: ir   *   )valueattrszreal*4, parameter :: x = 42typezlogical :: yzreal*4zreal*8)type_aliases)	r   r%   deducedr$   r&   r(   r*   r'   r)   )
r  r   var1dcl1rQ   rR   var2dcl2bvar3dcl3rV   rV   rW   test_fcode_Declaration  s    





r!  c                  C   sx   t ddd} t ddd}t ddd}t| d dksJ td| d  dks(J |d || | }t|d	ks:J d S )
Nr   rp   ro   BCr
  z      A(1, 1)z      3*A(1, 1)z      (A - B)(1, 1))r8   r9   subs)r   r"  r#  FrV   rV   rW   test_MatrixElement_printing  s   r&  c                  C   s(   t d} tt| dddddksJ d S )NrQ   +rp   r?   r@   z	x = x + 1)r   r9   r#   ri   rV   rV   rW   test_aug_assign  s    r(  c                  C   s8   t d} ttt| dkt| ddgdddksJ d S )NrQ   rp   -r?   r@   z)do while (abs(x) > 1)
   x = x - 1
end do)r   r9   r+   rJ   r#   ri   rV   rV   rW   
test_While"  s   "r*  c                     sR   t d} t ddd}t| td}t|td}ttd||g tt fdd d S )	NrQ   rn   Trc   r  powerc                      r   rh   r   rV   fp1rV   rW   rj   3  r   z.test_FunctionPrototype_print.<locals>.<lambda>)r   r%   r)   r.   r,   r>   rk   )rQ   rn   vxvnrV   r,  rW   test_FunctionPrototype_print+  s   r0  c                     sj   t d} t ddd}t| td}t|td}t| | | t| g}ttd||g| tt fdd d S )	NrQ   rn   Trc   r  r+  c                      r   rh   r   rV   fd1rV   rW   rj   ?  r   z/test_FunctionDefinition_print.<locals>.<lambda>)	r   r%   r)   r.   r"   r/   r-   r>   rk   )rQ   rn   r.  r/  bodyrV   r1  rW   test_FunctionDefinition_print6  s   r4  N)sympy.core.addr   sympy.core.functionr   r   r   sympy.core.modr   
sympy.corer   r   r	   sympy.core.numbersr
   r   r   r   r   r   sympy.core.relationalr   sympy.core.singletonr   sympy.core.symbolr   r   (sympy.functions.combinatorial.factorialsr   $sympy.functions.elementary.complexesr   r   &sympy.functions.elementary.exponentialr   r   (sympy.functions.elementary.miscellaneousr   $sympy.functions.elementary.piecewiser   (sympy.functions.elementary.trigonometricr   r   r   'sympy.functions.special.gamma_functionsr   sympy.integrals.integralsr   sympy.sets.fancysetsr    sympy.codegenr!   r"   r#   sympy.codegen.astr$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   sympy.core.exprr0   r1   sympy.logic.boolalgr2   r3   r4   r5   r6   sympy.matricesr7   r8   sympy.printing.fortranr9   r:   sympy.tensorr;   r<   sympy.utilities.lambdifyr=   sympy.testing.pytestr>   rX   rb   rl   rs   rw   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   r  r  r	  r  r  r!  r&  r(  r*  r0  r4  rV   rV   rV   rW   <module>   s     8	

	1c
+<		?#
	