K ipHddlmZmZddlmZddlmZmZddlmZm Z m Z ddl m Z ddl mZddlmZdd lmZdd lmZmZdd lmZdd lmZmZmZdd lmZmZddlm Z ddl!m"Z"ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;mm?Z?m@Z@ddlAmBZBmCZCmDZDmEZEmFZFddlGmHZHddlImJZJddlKmLZLddlMmNZNddlOmPZPdZQdZRdZSd%dZTdZUdZVejdd ZXejfd!ZYejfd"ZZejfd#Z[y$)&) AccumBoundsAccumulationBounds) singularities)PowS)diff expand_mulFunction) NumberKind)Mod) equal_valued) Relational)SymbolDummy)_sympify)Absimre)explog)frac) Piecewise) TrigonometricFunctionsincostancotcscsecasinacosacotatanasecacsc) sinhcoshtanhcothsechcschasinhacoshatanhacothasechacsch)degreelcm_list)Interval Intersection FiniteSetUnion Complement)ImageSet) ConditionSet) filldedent)iterable)hessiancddlm}|jtjst t dtttttttttt t"t$t&t(t*t,t.t0t2t4t6t8t:t<t>t@tBtDtFg|jItJDcgc] }|jM|s|jN"}}tQfd|Drt t dtSd}t|dkDft$|dk\|dkft&|dk\|dkft>|dk\ft@|dkD|dkftD|dkD|dkfi}t,|dk|dk\ft.|dk|dk\ftB|dk|dkDfi}|} |jItD]r} | jjUd} | jjVr | jXrC|| jZdk\|j]} t_| | } t|jItJD]} | jN|vrW|| jND]D} | ja|| jbd}|||j]}t_|| } Fi| jN|vrjtjd}|| jND];} | ja|| jbd}||||j]z }=t_|| } | jNt*k(r!dd l3m4}| || jbd|z} | jNtk(s*dd l3m4}tk| jbd||}t_|tjl}|jnr4tjd}|D]}||| jbd|z |z } n7tq|tjljs| jbd| }| |z} | tu|||z Scc}w) a Returns the domain on which the function expression f is continuous. This function is limited by the ability to determine the various singularities and discontinuities of the given function. The result is either given as a union of intervals or constructed using other set operations. Parameters ========== f : :py:class:`~.Expr` The concerned function. symbol : :py:class:`~.Symbol` The variable for which the intervals are to be determined. domain : :py:class:`~.Interval` The domain over which the continuity of the symbol has to be checked. Examples ======== >>> from sympy import Interval, Symbol, S, tan, log, pi, sqrt >>> from sympy.calculus.util import continuous_domain >>> x = Symbol('x') >>> continuous_domain(1/x, x, S.Reals) Union(Interval.open(-oo, 0), Interval.open(0, oo)) >>> continuous_domain(tan(x), x, Interval(0, pi)) Union(Interval.Ropen(0, pi/2), Interval.Lopen(pi/2, pi)) >>> continuous_domain(sqrt(x - 2), x, Interval(-5, 5)) Interval(2, 5) >>> continuous_domain(log(2*x - 1), x, S.Reals) Interval.open(1/2, oo) Returns ======= :py:class:`~.Interval` Union of all intervals where the function is continuous. Raises ====== NotImplementedError If the method to determine continuity of such a function has not yet been developed. rsolve_univariate_inequalityz= Domain must be a subset of S.Reals. c3&K|]}|v ywN).0func implementeds Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/calculus/util.py z$continuous_domain..Zs 4t4{ " 4zO Unable to determine the domain of the given function. xr) solveset_real);sympy.solvers.inequalitiesrA is_subsetrRealsNotImplementedErrorr<rrrrrrrrrr rr!r"r$r#r%r&r'r(r)r*r+r,r-r.r/r0r1r2atomsr hasrFanyras_numer_denom is_rationalis_oddbaseas_setr6subsargsEmptySetsympy.solvers.solvesetrMfunction_rangeIntegers is_finite_setr;containsr)fsymboldomainrAfctusedrK constraintsconstraints_union cont_domainatomden constraintcconstraint_relationalconstraint_setrMrdiscontnrGs @rHcontinuous_domainrss`G   AGG $!*.# S#tS#sCtT4tT4%ue =K !" 1 ESWWV_CHH ED E 4t 44!*.#  s A a!eX qBwQ qBwQ Qy BA AqAv K qBwQ qBwQ BA K @hh%%'* 88  CJJ 4TYY!^8>@@F &z;?K@!$# 99 # + H()q$))A,(?%!<)6"33968*>;G  H YY+ +ZZN&tyy1 <()q$))A,(?%"=)6#33968< <'~{CKYY$  < =1v> >KYY$  <tyy|VV>> from sympy import Interval, Symbol, S, exp, log, pi, sqrt, sin, tan >>> from sympy.calculus.util import function_range >>> x = Symbol('x') >>> function_range(sin(x), x, Interval(0, 2*pi)) Interval(-1, 1) >>> function_range(tan(x), x, Interval(-pi/2, pi/2)) Interval(-oo, oo) >>> function_range(1/x, x, S.Reals) Union(Interval.open(-oo, 0), Interval.open(0, oo)) >>> function_range(exp(x), x, S.Reals) Interval.open(0, oo) >>> function_range(log(x), x, S.Reals) Interval(-oo, oo) >>> function_range(sqrt(x), x, Interval(-5, 9)) Interval(0, 3) Returns ======= :py:class:`~.Interval` Union of all ranges for all intervals under domain where function is continuous. Raises ====== NotImplementedError If any of the intervals, in the given domain, for which function is continuous are not finite or real, OR if the critical points of the function on the domain cannot be found. r)limitsolvesetz*Unable to find range for the given domain.+-z%Unable to find critical points for {}z)Infinite number of critical points for {})FFT)rr\ periodicityZeror7expandsympy.series.limitsrur]rw isinstancer5infsup is_infiniter8r[rsrQrZ left_open right_openr r=formatr:)rbrcrdperiodrurwsub_dom intervals range_int interval_iterinterval singletonvalscritical_valuesboundsis_open limit_point directioncritical_pointscritical_pointrrs rHr^r^sfzz F #F $$)/  fh ' VZZ'44!!V,  &!;; 1gx0++ +88%a0F 1 "!VV4I I)Xy12" Iu %! !"NOO!)T h *% F &166&)+D!EEI F( +::DjjO))8<<=''s;=F4: C/i#yq&+y1Y'ZZOO+DIaffV[&ABBD  C'qvvf~vxHOO,)?FFqIKK/84)CJJ1MOO#2 B !&&"@AA B%1 !Izajj0"&&$((2 $I"&&$((2!%J $((DHHiL LI%&RS SS)TV c L t|dk(r td|tjurtjSt |t r2|j d}t t|j dg||St |tr|}tj}n|j d}|j d}t |tstdt|d|t|dk(r|d ntd|d fd}t |trt |Dcgc] }||| c}St |t rLtj}|j D]+}t |Dcgc] }||| c}} t || }-|Sy cc}wcc}w) a Finds the domain of the functions in ``finset_intersection`` in which the ``finite_set`` is not-empty. Parameters ========== finset_intersection : Intersection of FiniteSet The unevaluated intersection of FiniteSet containing real-valued functions with Union of Sets syms : Tuple of symbols Symbol for which domain is to be found Raises ====== NotImplementedError The algorithms to find the non-emptiness of the given FiniteSet are not yet implemented. ValueError The input is not valid. RuntimeError It is a bug, please report it to the github issue tracker (https://github.com/sympy/sympy/issues). Examples ======== >>> from sympy import FiniteSet, Interval, not_empty_in, oo >>> from sympy.abc import x >>> not_empty_in(FiniteSet(x/2).intersect(Interval(0, 1)), x) Interval(0, 2) >>> not_empty_in(FiniteSet(x, x**2).intersect(Interval(1, 2)), x) Union(Interval(1, 2), Interval(-sqrt(2), -1)) >>> not_empty_in(FiniteSet(x**2/(x + 2)).intersect(Interval(1, oo)), x) Union(Interval.Lopen(-2, -1), Interval(2, oo)) rz*One or more symbols must be given in syms.rzA FiniteSet must be given, not z: zmore than one variables z not handledchddlm}|j}|j}||j d t j }|jr@|t jurt j }n9|||k t j }n|||k t j }|jr@|t jurt j }n9|||kD t j }n|||k\ t j }t||}t||} | S)z9 Finds the domain of an expression in any given interval rrvrrd) r]rwstartendrUrrPrInfinityrNegativeInfinityr6r9) exprintrvlrw_start_end_singularities_domain1_domain2expr_with_sing expr_domainsymbs rH elm_domainz not_empty_in..elm_domainfs3zz!$"5"5"7":D)*2   qzz!77#D4KaggF d177CH   +++77#D6M4HQWWEH&h9 @ rN) len ValueErrorrr\r~r8r[ not_empty_inr7rPtyperQr5) finset_intersectionsyms elm_in_sets finite_set_setsrelement_domainr_domain_elementrs @rHrr sT 4yA~EFFajj(zz%u-)..q1 \"5":":1"=EE " "%y1( (--a0 #((+ j) ,z*J89 9 4yA~Aw!#'#*+ +<%"Lgz'51LMM%**jj 6F#5?&A*1'1&&A&ABOG_5G 6  M &As +F7F! cJ jturtdjztdd}|j |}|fd}|}d}t |t r|j|jz }|j}|jvrtjSt |tr |j}t |trx|j d}t |t"t$t&frt)|j d}t+|}|(t |t(rt|} |||dz St |t,s)|j.r|j0tj2k(rvt5tj2t7|j,}t9|dk7r;t+t;|} t+t9|} | | t=| | g}|j.r|j0tj2k7rm|j \} } | j?} | j?}| r|st+| }n|r| st+| }ntA|j }n|jBrX|jEd \}}t |ts tG|d st+|}nytA|j }na|jHrJ|jE\}}|tjur t+|StA|j }n t |tJr|j \}}|k(r|}nt |tr t+|}n|jMrtO|d k(r|jvrt||jQz }n{t |tRrnj|hdd l*m+}m,}||}t[|}|d kDrGt]t_|D]0\}}|d z |z }|||d}|||fvs!t+|}|0n| |r |||S|Sy#t$rYwxYw#t$r}|r t|Yd}~Fd}~wwxYw) a_ Tests the given function for periodicity in the given symbol. Parameters ========== f : :py:class:`~.Expr` The concerned function. symbol : :py:class:`~.Symbol` The variable for which the period is to be determined. check : bool, optional The flag to verify whether the value being returned is a period or not. Returns ======= period The period of the function is returned. ``None`` is returned when the function is aperiodic or has a complex period. The value of $0$ is returned as the period of a constant function. Raises ====== NotImplementedError The value of the period computed cannot be verified. Notes ===== Currently, we do not support functions with a complex period. The period of functions having complex periodic values such as ``exp``, ``sinh`` is evaluated to ``None``. The value returned might not be the "fundamental" period of the given function i.e. it may not be the smallest periodic value of the function. The verification of the period through the ``check`` flag is not reliable due to internal simplification of the given expression. Hence, it is set to ``False`` by default. Examples ======== >>> from sympy import periodicity, Symbol, sin, cos, tan, exp >>> x = Symbol('x') >>> f = sin(x) + sin(2*x) + sin(3*x) >>> periodicity(f, x) 2*pi >>> periodicity(sin(x)*cos(x), x) pi >>> periodicity(exp(tan(2*x) - 1), x) pi/2 >>> periodicity(sin(4*x)**cos(2*x), x) pi >>> periodicity(exp(x), x) zCannot use symbol of kind %srKT)realc|j|z}|j|r|Sttddd|d|d|d )z,Return the checked period or raise an error.z\ The period of the given function cannot be verified. When `z` was replaced with `z + z` in `z#`, the result was `z` which was not recognized as being the same as the original function. So either the period was wrong or the two forms were not recognized as being equal. Set check=False to obtain the value.)rZequalsrQr<)orig_frnew_frcs rH_checkzperiodicity.._checksN FFVO4 << M%j28'9: :rNrF)as_Addr)compogen decompogen)0kindr rQrrZr~rlhsrhssimplify free_symbolsrr{rrrr[r rrrrzris_PowrXExp1rr rrlcimrS _periodicityis_Mulas_independentris_Addr is_polynomialr3r rsympy.solvers.decompogenrrr enumeratereversed)rbrcchecktemprrrargerr period_real period_imagrXexpo base_has_sym expo_has_symcoeffgkarrrrg_s num_of_gsindex start_indexs ` rHrzrzst{{*$!"@6;;"NOO 4 D vtA F: F F!Z EEAEEM A Q^^#vv !*+ XXf%F!SffQi cCc? +chhqk"CS&)  *S#"6 XF 3ffQh//!Sahh166QVV+;  155) * a5A:%beV4K%beV4K&;+B{K89xxAFFaff$VV dxx' xx'   v.F , v.F"!&&&1F ##F5#9q a. /|E17M F+F!!&&&1F '1 AFF?q&) )afff- As vv1 ;F 0 1 F+Foof%&F*;q*@ !.. (Q/0 Ay ! AF#H q=%hsm4 q'!me3 S.7VQK'(F3F)   &&) ) }#   &' 3-c22 3s*>Q1 R1 Q>=Q> R" RR"cg}|D]6}t||}|y|tjus&|j|8t |dkDr t |S|r|dSy)a] Helper for `periodicity` to find the period of a list of simpler functions. It uses the `lcim` method to find the least common period of all the functions. Parameters ========== args : Tuple of :py:class:`~.Symbol` All the symbols present in a function. symbol : :py:class:`~.Symbol` The symbol over which the function is to be evaluated. Returns ======= period The least common period of the function for all the symbols of the function. ``None`` if for at least one of the symbols the function is aperiodic. Nrr)rzrr{appendrr)r[rcperiodsrbrs rHrrOsn2G #Q' >   NN6 " # 7|aG}qzrc d}td|Dr||Dcgc]}|j}}|Dcgc]}|j}}|dd t fd|Dr$ }|Dcgc]\}}| }}}t||z}|Std|Dr t|}|S |Scc}wcc}wcc}}w)aReturns the least common integral multiple of a list of numbers. The numbers can be rational or irrational or a mixture of both. `None` is returned for incommensurable numbers. Parameters ========== numbers : list Numbers (rational and/or irrational) for which lcim is to be found. Returns ======= number lcim if it exists, otherwise ``None`` for incommensurable numbers. Examples ======== >>> from sympy.calculus.util import lcim >>> from sympy import S, pi >>> lcim([S(1)/2, S(3)/4, S(5)/6]) 15/2 >>> lcim([2*pi, 3*pi, pi, pi/2]) 6*pi >>> lcim([S(1), 2*pi]) Nc34K|]}|jywrC) is_irrationalrEnums rHrIzlcim..s 03   0rrc3.K|] \}}|k(ywrCrD)rErfactorterms rHrIzlcim..s?-%v~?sc34K|]}|jywrC)rVrs rHrIzlcim..s 0S__ 0r)allr as_coeff_Mulr4) numbersresultrfactorized_nums factors_num common_termrrcoeffsrs @rHrrxs:F 0 003:;C3::<;;5DEcs'')E E1~a  ?;? ?K1<= ve=F=f% 3F M 0 0 0'" M M<E>sB4B93 B>rct|dkDrt||jSddlm}t |}|d}t fdt||Dry|j|ddk}|||dryy)aDetermines the convexity of the function passed in the argument. Parameters ========== f : :py:class:`~.Expr` The concerned function. syms : Tuple of :py:class:`~.Symbol` The variables with respect to which the convexity is to be determined. domain : :py:class:`~.Interval`, optional The domain over which the convexity of the function has to be checked. If unspecified, S.Reals will be the default domain. Returns ======= bool The method returns ``True`` if the function is convex otherwise it returns ``False``. Raises ====== NotImplementedError The check for the convexity of multivariate functions is not implemented yet. Notes ===== To determine concavity of a function pass `-f` as the concerned function. To determine logarithmic convexity of a function pass `\log(f)` as concerned function. To determine logarithmic concavity of a function pass `-\log(f)` as concerned function. Currently, convexity check of multivariate functions is not handled. Examples ======== >>> from sympy import is_convex, symbols, exp, oo, Interval >>> x = symbols('x') >>> is_convex(exp(x), x) True >>> is_convex(x**3, x, domain = Interval(-1, oo)) False >>> is_convex(1/x**2, x, domain=Interval.open(0, oo)) True References ========== .. [1] https://en.wikipedia.org/wiki/Convex_function .. [2] http://www.ifp.illinois.edu/~angelia/L3_convfunc.pdf .. [3] https://en.wikipedia.org/wiki/Logarithmically_convex_function .. [4] https://en.wikipedia.org/wiki/Logarithmically_concave_function .. [5] https://en.wikipedia.org/wiki/Concave_function rrr@c3&K|]}|v ywrCrD)rEsrds rHrIzis_convex..s 611; 6rJFrT) rr>is_positive_semidefiniterNrArrTrr )rbrdrrAvar conditions ` rH is_convexrsyx 4y1}q$888F A q'C 6 a 5 66sA"I"9c5&A rcddlm}|tjurtjSt |||}|t ||||}|S)a Returns the stationary points of a function (where derivative of the function is 0) in the given domain. Parameters ========== f : :py:class:`~.Expr` The concerned function. symbol : :py:class:`~.Symbol` The variable for which the stationary points are to be determined. domain : :py:class:`~.Interval` The domain over which the stationary points have to be checked. If unspecified, ``S.Reals`` will be the default domain. Returns ======= Set A set of stationary points for the function. If there are no stationary point, an :py:class:`~.EmptySet` is returned. Examples ======== >>> from sympy import Interval, Symbol, S, sin, pi, pprint, stationary_points >>> x = Symbol('x') >>> stationary_points(1/x, x, S.Reals) EmptySet >>> pprint(stationary_points(sin(x), x), use_unicode=False) pi 3*pi {2*n*pi + -- | n in Integers} U {2*n*pi + ---- | n in Integers} 2 2 >>> stationary_points(sin(x),x, Interval(0, 4*pi)) {pi/2, 3*pi/2, 5*pi/2, 7*pi/2} rrv)r]rwrr\rsr )rbrcrdrwsets rHstationary_pointsrsER0 zz q&& 1F 46?FF 3C Jrct|tr4|tjur t dt |||j St d|z)a# Returns the maximum value of a function in the given domain. Parameters ========== f : :py:class:`~.Expr` The concerned function. symbol : :py:class:`~.Symbol` The variable for maximum value needs to be determined. domain : :py:class:`~.Interval` The domain over which the maximum have to be checked. If unspecified, then the global maximum is returned. Returns ======= number Maximum value of the function in given domain. Examples ======== >>> from sympy import Interval, Symbol, S, sin, cos, pi, maximum >>> x = Symbol('x') >>> f = -x**2 + 2*x + 5 >>> maximum(f, x, S.Reals) 6 >>> maximum(sin(x), x, Interval(-pi, pi/4)) sqrt(2)/2 >>> maximum(sin(x)*cos(x), x) 1/2 z+Maximum value not defined for empty domain.%s is not a valid symbol.)r~rrr\rr^rrbrcrds rHmaximumr$ML&&! QZZ JK Ka04444v=>>rct|tr4|tjur t dt |||j St d|z)a Returns the minimum value of a function in the given domain. Parameters ========== f : :py:class:`~.Expr` The concerned function. symbol : :py:class:`~.Symbol` The variable for minimum value needs to be determined. domain : :py:class:`~.Interval` The domain over which the minimum have to be checked. If unspecified, then the global minimum is returned. Returns ======= number Minimum value of the function in the given domain. Examples ======== >>> from sympy import Interval, Symbol, S, sin, cos, minimum >>> x = Symbol('x') >>> f = x**2 + 2*x + 5 >>> minimum(f, x, S.Reals) 4 >>> minimum(sin(x), x, Interval(2, 3)) sin(3) >>> minimum(sin(x)*cos(x), x) -1/2 z+Minimum value not defined for empty domain.r)r~rrr\rr^rrs rHminimumrSrrN)F)\accumulationboundsrrr sympy.corerrsympy.core.functionr r r sympy.core.kindr sympy.core.modr sympy.core.numbersrsympy.core.relationalrsympy.core.symbolrrsympy.core.sympifyr$sympy.functions.elementary.complexesrrr&sympy.functions.elementary.exponentialrr#sympy.functions.elementary.integersr$sympy.functions.elementary.piecewiser(sympy.functions.elementary.trigonometricrrrrrrr r!r"r#r$r%r&%sympy.functions.elementary.hyperbolicr'r(r)r*r+r,r-r.r/r0r1r2sympy.polys.polytoolsr3r4sympy.sets.setsr5r6r7r8r9sympy.sets.fancysetsr:sympy.sets.conditionsetr;sympy.utilitiesr<sympy.utilities.iterablesr=sympy.matrices.denser>rsr^rrzrrrPrrrrrDrrHrs?(::&+,+'<<;4:((((::::2)))0&.(@:F|~m`|~&R-^ !wwFR)*1h gg,?^ gg,?r