K i ddlmZddlmZddlmZmZddlmZm Z ddl m Z ddl m Z mZddlmZddlmZmZmZmZmZmZmZmZmZmZmZmZmZdd lm Z  dd d d d d d d d ddZ!dZ"d defd ddZ#y ))Tuple)oo)GtLt)DummySymbol)Abs)MinMax)And) AssignmentAddAugmentedAssignmentbreak_ CodeBlock DeclarationFunctionDefinitionPrintReturnScopeWhileVariablePointerreal)isnanNgؗҼr!saRq \)rtoldebugitermaxcounterdelta_fncse handle_nanboundsc ~|t}t} d} nd} |j} |||}| rQddlm} | |j g\}\}|Dcgc]\}}t ||}}}|t ||gz }n t ||g}| '|tt|t| tgz }|t||gz }| *|t |tt|| d| dgz }|r.t||gdj|j| }||gz }t!t#|||t#|zz}t%t'|t(t*g}|q|xs td }t'j,|d}|j/t%||j/t|dt1|t3||}t|t|}|}|r5|j/t|gd j|j||gz }| t|Scc}}w) a Generates an AST for Newton-Raphson method (a root-finding algorithm). Explanation =========== Returns an abstract syntax tree (AST) based on ``sympy.codegen.ast`` for Netwon's method of root-finding. Parameters ========== expr : expression wrt : Symbol With respect to, i.e. what is the variable. atol : number or expression Absolute tolerance (stopping criterion) rtol : number or expression Relative tolerance (stopping criterion) delta : Symbol Will be a ``Dummy`` if ``None``. debug : bool Whether to print convergence information during iterations itermax : number or expr Maximum number of iterations. counter : Symbol Will be a ``Dummy`` if ``None``. delta_fn: Callable[[Expr, Symbol], Expr] computes the step, default is newtons method. For e.g. Halley's method use delta_fn=lambda e, x: -2*e*e.diff(x)/(2*e.diff(x)**2 - e*e.diff(x, 2)) cse: bool Perform common sub-expression elimination on delta expression handle_nan: Token How to handle occurrence of not-a-number (NaN). bounds: Optional[tuple[Expr, Expr]] Perform optimization within bounds Examples ======== >>> from sympy import symbols, cos >>> from sympy.codegen.ast import Assignment >>> from sympy.codegen.algorithms import newtons_method >>> x, dx, atol = symbols('x dx atol') >>> expr = cos(x) - x**3 >>> algo = newtons_method(expr, x, atol=atol, delta=dx) >>> algo.has(Assignment(dx, -expr/expr.diff(x))) True References ========== .. [1] https://en.wikipedia.org/wiki/Newton%27s_method deltac|Sr)rs r r!z newtons_method..PsAr"r)r(z{}=%12.5g {}=%12.5g\n)typevalueT)integerz {}=%12.5g\n)rrnamesympy.simplify.cse_mainr(factorr rrrrrr r rformatrr rrrrdeducedappendr r)exprwrtatolr,r#r$r%r&r'r(r)r*Wrappername_d delta_exprcsesreddumsub_ewhl_bdyprntreqdeclars v_counterwhlblcks r newtons_methodrJs#v }$$J /J--/01 fs<@Ajc5:c5)AAJuc*++eZ01E%, *f(EFGG &sE233G JsCC(;VAY$GHII c5\#;#B#B388V#TUD6 SZSX - .C8EB?@AG0U40$$Wa0 {9-.-gq9:#r'7+, Y( )C D  E3%!6!6sxx!@ABSEMD 9d# $$3BsH9ct|tr|jj}|St|tr |j}|Sr) isinstancervariablesymbolr)args r _symbol_ofrPss<#{#ll!! J C "jj Jr"newton)r,c ||f}|Dcic]?}t|tr-|jtd|jjzA}}|+td|jz}|j |rd}t ||fd|i|j|} t| tr | j} |jj|Dchc] }t|c}} | r+tddjtt | zt#d|D} t%| t'|} t)t*|| | |Scc}wcc}w) a Generates an AST for a function implementing the Newton-Raphson method. Parameters ========== expr : expression wrt : Symbol With respect to, i.e. what is the variable params : iterable of symbols Symbols appearing in expr that are taken as constants during the iterations (these will be accepted as parameters to the generated function). func_name : str Name of the generated function. attrs : Tuple Attribute instances passed as ``attrs`` to ``FunctionDefinition``. \*\*kwargs : Keyword arguments passed to :func:`sympy.codegen.algorithms.newtons_method`. Examples ======== >>> from sympy import symbols, cos >>> from sympy.codegen.algorithms import newtons_method_function >>> from sympy.codegen.pyutils import render_as_module >>> x = symbols('x') >>> expr = cos(x) - x**3 >>> func = newtons_method_function(expr, x) >>> py_mod = render_as_module(func) # source code as string >>> namespace = {} >>> exec(py_mod, namespace, namespace) >>> res = eval('newton(0.5)', namespace) >>> abs(res - 0.865474033102) < 1e-12 True See Also ======== sympy.codegen.algorithms.newtons_method Nz(*%s)d_r,zMissing symbols in params: %sz, c3<K|]}t|tywr)rr).0ps r z*newtons_method_function..s6!HQ%6s)attrs)rLrrNrr3hasrJxreplacerbody free_symbols differencerP ValueErrorjoinmapstrtuplerrrr) r9r:params func_namerXr,kwargsrV pointer_subsalgo not_in_paramsrFr[s r newtons_method_functionri{s4R~#?z!W'=HHfWqxx}}%<==?L? }tchh' 88E?E $ ;5 ;F ; D D\ RD$yy%%001PA*Q-1PQM8499SmE\;]]^^6v66G T6#; 'D dIwE JJ?2Qs AEE)g-q=N)$sympy.core.containersrsympy.core.numbersrsympy.core.relationalrrsympy.core.symbolrr$sympy.functions.elementary.complexesr (sympy.functions.elementary.miscellaneousr r sympy.logic.boolalgr sympy.codegen.astr rrrrrrrrrrrrsympy.codegen.cfunctionsrrJrPrir.r"r rssv'!*-4=#+U`%e58Q`%F/3heg9K`d9Kr"