+
    iA-              	          R t ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RI	H
t
Ht ^ RIHtHt ^ RIHtHtHt ^ RIHtHt ^ R	IHtHtHtHt ^ R
IHt ^ RIHt ^ RIH t  ^ RI!H"t"H#t# ^ RI$H%t%H&t& ^ RI'H(t( ^ RI)H*t* ^ RI+H,t,  ! R R4      t- ! R R]-4      t.R t/].! R R 4      t0]! RR .R7      t1]! RR .R7      t2]! R4      t3]! R4      t4]! R R! .R7      t5].! ]! ]44      ]4,          ]! ]44      4      t6].! ]! ]5]4,          4      ]4,          ]5]! ]5]4,          4      ,          4      t7]6]73t8].! ]3]! ]44      ,          ]! ^4      ,          ]3]! ]44      ,          R" R#7      t9].! ]! ^4      ]! ]44      ,          ]! ]44      4      t:].! R$ R% 4      t; ! R& R'].4      t<]<! ]
]4      t=]<! ]]%4      t>]<! ] ]&4      t?].! R( R) 4      t@R*R+ /R, ltAR- tBR. tC].! ]B]C4      tD].! ]! ]
! ]34      ]
! ]44      ,           4      ]"! ]3]44      4      tE].! ]! ] ! ^]34      ] ! ^]44      ,           4      ]#! ]3]44      ]! ^4      ,          4      tF]=]@]0]9]:3tG]G]E]F3,           ]8,           tH]>]?3tIR/# )0a  
Classes and functions useful for rewriting expressions for optimized code
generation. Some languages (or standards thereof), e.g. C99, offer specialized
math functions for better performance and/or precision.

Using the ``optimize`` function in this module, together with a collection of
rules (represented as instances of ``Optimization``), one can rewrite the
expressions for this purpose::

    >>> from sympy import Symbol, exp, log
    >>> from sympy.codegen.rewriting import optimize, optims_c99
    >>> x = Symbol('x')
    >>> optimize(3*exp(2*x) - 3, optims_c99)
    3*expm1(2*x)
    >>> optimize(exp(2*x) - 1 - exp(-33), optims_c99)
    expm1(2*x) - exp(-33)
    >>> optimize(log(3*x + 3), optims_c99)
    log1p(x) + log(3)
    >>> optimize(log(2*x + 3), optims_c99)
    log(2*x + 3)

The ``optims_c99`` imported above is tuple containing the following instances
(which may be imported from ``sympy.codegen.rewriting``):

- ``expm1_opt``
- ``log1p_opt``
- ``exp2_opt``
- ``log2_opt``
- ``log2const_opt``


)
expand_log)S)Wild)sign)explog)MaxMin)cossinsinc)Qask)log1plog2exp2expm1)MatrixSolve)UnevaluatedExpr)Pow)	logaddexp
logaddexp2)cosm1powm1)Mul)MatrixSymbol)siftc                   4   a  ] tR t^4t o RtRR ltR tRtV tR# )OptimizationzAbstract base class for rewriting optimization.

Subclasses should implement ``__call__`` taking an expression
as argument.

Parameters
==========
cost_function : callable returning number
priority : number

Nc                    Wn         W n        R # Ncost_functionpriority)selfr"   r#   s   &&&w/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/codegen/rewriting.py__init__Optimization.__init__@   s    *    c                .    \        WP                  R 7      # ))key)minr"   )r$   argss   &*r%   cheapestOptimization.cheapestD   s    4//00r(   r!   )N   )	__name__
__module____qualname____firstlineno____doc__r&   r-   __static_attributes____classdictcell__)__classdict__s   @r%   r   r   4   s     
1 1r(   r   c                   <   a a ] tR t^Ht oRtV 3R ltR tRtVtV ;t	# )ReplaceOptima|  Rewriting optimization calling replace on expressions.

Explanation
===========

The instance can be used as a function on expressions for which
it will apply the ``replace`` method (see
:meth:`sympy.core.basic.Basic.replace`).

Parameters
==========

query :
    First argument passed to replace.
value :
    Second argument passed to replace.

Examples
========

>>> from sympy import Symbol
>>> from sympy.codegen.rewriting import ReplaceOptim
>>> from sympy.codegen.cfunctions import exp2
>>> x = Symbol('x')
>>> exp2_opt = ReplaceOptim(lambda p: p.is_Pow and p.base == 2,
...     lambda p: exp2(p.exp))
>>> exp2_opt(2**x)
exp2(x)

c                @   < \         SV `  ! R/ VB  Wn        W n        R # )N )superr&   queryvalue)r$   r=   r>   kwargs	__class__s   &&&,r%   r&   ReplaceOptim.__init__h   s    "6"

r(   c                N    VP                  V P                  V P                  4      # r    )replacer=   r>   )r$   exprs   &&r%   __call__ReplaceOptim.__call__m   s    ||DJJ

33r(   )r=   r>   )
r0   r1   r2   r3   r4   r&   rE   r5   r6   __classcell__r@   r7   s   @@r%   r9   r9   H   s     >
4 4r(   r9   c                    \        VR RR7       F.  pV! V 4      pVP                  f   Tp K  VP                  W4      p K0  	  V # )a  Apply optimizations to an expression.

Parameters
==========

expr : expression
optimizations : iterable of ``Optimization`` instances
    The optimizations will be sorted with respect to ``priority`` (highest first).

Examples
========

>>> from sympy import log, Symbol
>>> from sympy.codegen.rewriting import optims_c99, optimize
>>> x = Symbol('x')
>>> optimize(log(x+3)/log(2) + log(x**2 + 1), optims_c99)
log1p(x**2) + log2(x + 3)

c                     V P                   # r    )r#   )opts   &r%   <lambda>optimize.<locals>.<lambda>   s    s||r(   T)r*   reverse)sortedr"   r-   )rD   optimizationsoptimnew_exprs   &&  r%   optimizerS   q   sH    * +CTR;&D>>$1D S Kr(   c                 H    V P                   ;'       d    V P                  ^8H  # )   )is_Powbaseps   &r%   rL   rL      s    ahh&&166Q;&r(   c                 ,    \        V P                  4      # r    )r   r   rX   s   &r%   rL   rL      s    d155kr(   dc                     V P                   # r    )is_Dummyxs   &r%   rL   rL      s    QZZr(   )
propertiesuc                 V    V P                   '       * ;'       d    V P                  '       * # r    )	is_numberis_Addr^   s   &r%   rL   rL      s    _%E%EQXX%Er(   vwnc                     V P                   # r    rc   r^   s   &r%   rL   rL      s    Q[[r(   c                 &    V P                  R  4      # )c                     V P                   ;'       d    V P                  P                  ;'       gA    \        V \        \
        34      ;'       d#    V P                  ^ ,          P                  '       * #     )rV   r   is_negative
isinstancer   r   r,   rc   es   &r%   rL   <lambda>.<locals>.<lambda>   sT    	&&QUU&& 	D 	Dq3+&BBqvvay/B/B+B	Dr(   )count)rD   s   &r%   rL   rL      s    SWS]S]ETr(   r"   c                    \        V \        4      ;'       d    V P                  ^ ,          P                  ;'       d    \	        V P                  ^ ,          P                  4      ^8H  ;'       dk    \
        ;QJ d7    R V P                  ^ ,          P                   4       F  '       d   K   R# 	  R# ! R V P                  ^ ,          P                   4       4      # )rm   c              3   B   "   T F  p\        V\        4      x  K  	  R # 5ir    )ro   r   ).0ts   & r%   	<genexpr><lambda>.<locals>.<genexpr>   s     B>az!S))>s   FT)ro   r   r,   rd   lenallls   &r%   rL   rL      s    z!S! C C66!9##C Cqvvay~~&!+C C 3B166!9>>B33C C B166!9>>BBCr(   c                 T   \        V P                  ^ ,          P                   Uu. uF  qP                  ^ ,          NK  	  up!  \        \        \	        V P                  ^ ,          P                   Uu. uF  qP                  ^ ,          NK  	  up!  4      4      ,           # u upi u upi rl   )r   r,   r   r   r	   )r~   rq   s   & r%   rL   rL      sm    0AffQii01c#166!9>>:>aq		>:;<=	>0:s   B 2B%c                   R   a a ] tR t^t oRtRV 3R lltR tR tV 3R ltRt	Vt
V ;t# )FuncMinusOneOptima}  Specialization of ReplaceOptim for functions evaluating "f(x) - 1".

Explanation
===========

Numerical functions which go toward one as x go toward zero is often best
implemented by a dedicated function in order to avoid catastrophic
cancellation. One such example is ``expm1(x)`` in the C standard library
which evaluates ``exp(x) - 1``. Such functions preserves many more
significant digits when its argument is much smaller than one, compared
to subtracting one afterwards.

Parameters
==========

func :
    The function which is subtracted by one.
func_m_1 :
    The specialized function evaluating ``func(x) - 1``.
opportunistic : bool
    When ``True``, apply the transformation as long as the magnitude of the
    remaining number terms decreases. When ``False``, only apply the
    transformation if it completely eliminates the number term.

Examples
========

>>> from sympy import symbols, exp
>>> from sympy.codegen.rewriting import FuncMinusOneOptim
>>> from sympy.codegen.cfunctions import expm1
>>> x, y = symbols('x y')
>>> expm1_opt = FuncMinusOneOptim(exp, expm1)
>>> expm1_opt(exp(x) + 2*exp(5*y) - 3)
expm1(x) + 2*expm1(5*y)


c                z   <aa ^
o\         SV `  R V P                  VV3R lR7       Wn        SV n        W0n        R# )
   c                     V P                   # r    )rd   rp   s   &r%   rL   ,FuncMinusOneOptim.__init__.<locals>.<lambda>   s    188r(   c                 ^   < V P                  4       SV P                  S4      ,          ,
          # r    )	count_opsrs   )rD   func_m_1weights   &r%   rL   r      s     DNN4DvdjjYaNbGb4br(   rt   N)r<   r&   replace_in_Addfuncr   opportunistic)r$   r   r   r   r   r@   s   &&f&@r%   r&   FuncMinusOneOptim.__init__   s;    +T-@-@'b 	 	d	 *r(   c                   a  \        VP                  R  RR7      w  r#\        V4      p\        VV 3R lRR7      w  rVWEV3# )c                     V P                   # r    ri   args   &r%   rL   4FuncMinusOneOptim._group_Add_terms.<locals>.<lambda>   s    cmmr(   Tbinaryc                 :   < V P                  SP                  4      # r    )hasr   r   r$   s   &r%   rL   r      s    377499;Mr(   )r   r,   sum)r$   addnumbersnon_numnumsumterms_with_funcothers   f&     r%   _group_Add_terms"FuncMinusOneOptim._group_Add_terms   s@    *CDQW!%g/MVZ![--r(   c                0  a  S P                  V4      w  r#pV^ 8X  d   V# . . reV EFW  pVP                  '       dT   \        VP                  V 3R lRR7      w  r\	        V4      ^8X  d#   \	        V	4      ^8X  d   V^ ,          V	^ ,          rM2Rp	M/VP
                  S P
                  8X  d   T\        P                  rMRp	V	e   V	P                  '       d   \        V	4      \        V4      ) 8X  d   S P                  '       d   \        W,           4      \        V4      8  p
MW,           ^ 8H  p
V
'       d<   W),          pVP                  V	S P                  ! XP                  !  ,          4       EKF  VP                  V4       EKZ  	  VP
                  ! V.VOVOVO5!  # )z0passed as second argument to Basic.replace(...) c                 6   < V P                   SP                   8H  # r    )r   r   s   &r%   rL   2FuncMinusOneOptim.replace_in_Add.<locals>.<lambda>   s    sxx499?Tr(   Tr   N)r   is_Mulr   r,   r{   r   r   Onerc   r   r   absappendr   )r$   rq   r   r   other_non_num_termssubstituted	untouched	with_funcr   coeffdo_substitutes   f&         r%   r    FuncMinusOneOptim.replace_in_Add   sI   7;7L7LQ7O4!4Q;H!#RY(I"9>>3T]abt9>c%jAo"&q'58% E499,'e U___ef9U%%%$'$5F$CM$)LA$5M OF&&uT]]DII-F'FGY'- )0 vvfM{MYM9LMMr(   c                ~   < \         SV `  V4      p\         SV `  VP                  4       4      pV P                  W#4      # r    )r<   rE   factorr-   )r$   rD   alt1alt2r@   s   &&  r%   rE   FuncMinusOneOptim.__call__  s6    w%w.}}T((r(   )r   r   r   )T)r0   r1   r2   r3   r4   r&   r   r   rE   r5   r6   rG   rH   s   @@r%   r   r      s&     $L+.N@) )r(   r   c                 "    \        V \        4      # r    )ro   r   rp   s   &r%   rL   rL     s    jC r(   c                     \        V P                  \        R  4      4      P                  \        \        ^,           4      \	        \        4      4      # )c                 4    \        V P                  4       4      # r    )r   r   r   s   &r%   rL   rr     s    SZZ\*r(   )r   rC   r   _ur   r}   s   &r%   rL   rL     s7    j* ws2a4y%)$%r(   base_reqc                     V P                   # r    )	is_symbol)bs   &r%   rL   rL     s    r(   c               *   a a \        VV 3R lR 4      # )a  Creates an instance of :class:`ReplaceOptim` for expanding ``Pow``.

Explanation
===========

The requirements for expansions are that the base needs to be a symbol
and the exponent needs to be an Integer (and be less than or equal to
``limit``).

Parameters
==========

limit : int
     The highest power which is expanded into multiplication.
base_req : function returning bool
     Requirement on base for expansion to happen, default is to return
     the ``is_symbol`` attribute of the base.

Examples
========

>>> from sympy import Symbol, sin
>>> from sympy.codegen.rewriting import create_expand_pow_optimization
>>> x = Symbol('x')
>>> expand_opt = create_expand_pow_optimization(3)
>>> expand_opt(x**5 + x**3)
x**5 + x*x*x
>>> expand_opt(x**5 + x**3 + sin(x)**3)
x**5 + sin(x)**3 + x*x*x
>>> opt2 = create_expand_pow_optimization(3, base_req=lambda b: not b.is_Function)
>>> opt2((x+1)**2 + sin(x)**2)
sin(x)**2 + (x + 1)*(x + 1)

c                    < V P                   ;'       dQ    S! V P                  4      ;'       d7    V P                  P                  ;'       d    \	        V P                  4      S8*  # r    )rV   rW   r   
is_Integerr   )rq   r   limits   &r%   rL   0create_expand_pow_optimization.<locals>.<lambda>B  sE    !((\\x/\\AEE4D4D\\QUUW\I\\r(   c                     V P                   ^ 8  d2   \        \        V P                  .V P                   5,          RR/ 4      # ^\        \        V P                  .V P                   ) ,          RR/ 4      ,          # )rm   evaluateF)r   r   r   rW   rX   s   &r%   rL   r   C  sb    HIPQ	OC166(AEE6/CUCD GocQVVHaeeVOEuEFFGr(   )r9   )r   r   s   fdr%   create_expand_pow_optimizationr     s    F \	
 r(   c                 v   V P                   '       d   \        V P                  4      ^8X  d   V P                  w  rVP                  '       dm   VP                  ^,          ^8X  dU   VP
                  p\        V\        4      '       d3   \        \        \        P                  ! VP
                  4      4      4      # R# )rU   F)	is_MatMulr{   r,   
is_Inverseshaper   ro   r   boolr   r   fullrankrD   leftrightinv_args   &   r%   _matinv_predicater   I  ss    ~~~#dii.A-ii???u{{1~2hhG'<00C

488 4566r(   c                 L    V P                   w  rVP                  p\        W24      # r    )r,   r   r   r   s   &   r%   _matinv_transformr   T  s!    ))KDhhGw&&r(   N)Jr4   sympy.core.functionr   sympy.core.singletonr   sympy.core.symbolr   $sympy.functions.elementary.complexesr   &sympy.functions.elementary.exponentialr   r   (sympy.functions.elementary.miscellaneousr   r	   (sympy.functions.elementary.trigonometricr
   r   r   sympy.assumptionsr   r   sympy.codegen.cfunctionsr   r   r   r   sympy.codegen.matrix_nodesr   sympy.core.exprr   sympy.core.powerr   sympy.codegen.numpy_nodesr   r   sympy.codegen.scipy_nodesr   r   sympy.core.mulr   "sympy.matrices.expressions.matexprr   sympy.utilities.iterablesr   r   r9   rS   exp2_opt_dr   _v_w_n	sinc_opt1	sinc_opt2	sinc_optslog2_optlog2const_optlogsumexp_2terms_optr   	expm1_opt	cosm1_opt	powm1_opt	log1p_optr   r   r   
matinv_optlogaddexp_optlogaddexp2_opt
optims_c99optims_numpyoptims_scipyr;   r(   r%   <module>r      sa  @ + " " 5 = ? E E $ = = 2 +   ; 2  ; *1 1(&4< &4R< & 
#/01	#EFG	#Y	#Y	#012GBJR	 2JrM2d2b5k>	 	"	3r7
3q6)2d2h; G  SVDH_c"g6#D	 X) X)v c5)	c5)	c5)	 %	(6K (V	' +->?
 SRR19R3DEc#a*SBZ"78*R:LSQRV:ST HhF
]N<<yH9%r(   