I think assumptions only work with variables and not functions of variables. You can specify x>0 so that |x| will simplify to x, but you cannot specify that cosh(x) > 0 for all real x. The cosh case is simple, consider the norm of a vector in a Minkowski space. It would be great if one could specify ahead of time that x_0**2-(x_1**2+x_2**2+x_3**2) was greater than zero or less than zero or zero as three separate cases.
On Tue, Feb 23, 2016 at 11:19 AM, Nolan Dyck <nolan.dyc...@gmail.com> wrote: > I understand that symbols are assumed complex by default. But when I tell > sympy the symbol is real, it does not make the appropriate simplification. > > Here is what the dev branch produces for me right now: > > >>> from sympy import * > >>> x = Symbol('x',real=True) > >>> simplify(abs(cosh(x))) > Abs(cosh(x)) > > Is it not possible to assess the 'realness' of the variable within the > simplify function? > > Nolan > > On Tuesday, February 23, 2016 at 7:43:34 AM UTC-5, brombo wrote: >> >> Sympy assumes symbols to be complex. For a real symbol you need >> >> x = Symbol('x',real=True) >> >> On Mon, Feb 22, 2016 at 9:44 PM, Nolan Dyck <nolan....@gmail.com> wrote: >> >>> Hi Everyone, >>> >>> I've been poking around in the sympy source, and I've noticed that the ` >>> simplify` command does not deal with expressions like the following: >>> >>> >>> from sympy import * >>> >>> from sympy.abc import x >>> >>> simplify(abs(cosh(x))) >>> Abs(cosh(x)) >>> >>> A simple glance at the graph of cosh(x) reveals that Abs(cosh(x)) == >>> cosh(x). So, in case it's not obvious the above expression should return: >>> >>> cosh(x) >>> >>> I'd like to take a stab at implementing this but I need some direction. >>> I hope this isn't duplicating something I have stupidly missed (the >>> inequality solvers don't seem to identify cases where absolute value >>> brackets are unnecessary. There are three key cases (as far as I can see): >>> >>> 1. The argument within the abs brackets is non-negative over all >>> combinations of all independent variables. Therefore, the absolute value >>> brackets redundant / unnecessary and may be removed. >>> 2. The argument within the abs brackets is non-positive over all >>> combinations of all independent variables. Therefore, the absolute value >>> brackets may be removed and the expression may be multiplied by -1 for >>> the >>> same effect. >>> 3. The argument within the abs brackets contains both positive and >>> negative values depending on the values of the independent variables. >>> >>> Ok, so to implement the above rules in the general sense it makes sense >>> to me to perform the following steps given the expression >>> Abs(f(x_1,x_2,...x_n)): >>> >>> 1. Determine the number of real roots of f(x). >>> 1. If there are one or more real roots then for each root: >>> 1. Determine whether the gradient of f(x) is zero: >>> 1. If non-zero slope at root, then argument expression >>> obtains opposite signed value at some point, so return Abs(f(x)) >>> 2. Slope of f(x) at root is 0. >>> 3. Determine if the root is an inflection point (need to >>> figure out exactly how to test for this over multiple variables in >>> the >>> expression) >>> 1. If at inflection point then the expression will still >>> become opposite signed on either side of the root, so return >>> Abs(f(x)) >>> 2. Any and all roots coincide with extrema values of f(x). >>> Therefore f(x) may be represented without absolute value brackets. >>> 2. If f(x) >= 0 remove the absolute value brackets and return the >>> argument expression. >>> 3. If f(x) < 0 remove the absolute value brackets, multiply the >>> expression by -1 and return it. >>> >>> There are a few things which I'm not sure how it will work out: >>> >>> - Imaginary numbers. Does anyone know if I will need to write >>> special code for this, or should the above procedure work out anyway? >>> - The case where a symbol in the expression has been defined with >>> the positive flag: >>> >>> >>> y = Symbol('y') >>> >>> simplify(abs(sinh(y))) >>> Abs(sinh(y)) >>> >>> >>> y = Symbol('y',positive=True) >>> >>> simplify(abs(sinh(y))) >>> sinh(y) >>> >>> - Are there sneaky ways of determining in a precise manner whether a >>> function which cannot be reduced (e.g. cosh(x)+cos(x)) has real roots, >>> even >>> if finding those roots would only be possible numerically? Is there >>> another >>> sympy module which can help with this? >>> - What about variables which produce no real roots over a given >>> range? Is there a way to handle those? E.g. >>> >>> >>> y = Symbol('y',range=[0,pi]) >>> >>> simplify(abs(sin(y))) >>> sin(y) >>> >>> - Redundant absolute value brackets are removed somewhere. Can >>> anyone tell me where exactly in the code this happens in the simplify >>> function? I can't seem to find it: >>> >>> >>> from sympy.abc import x >>> >>> simplify(abs(abs(x)+1)) >>> Abs(x) + 1 >>> >>> simplify(abs(x+1)) >>> Abs(x + 1) >>> >>> So, right now I have forked the sympy repo (see here >>> <https://github.com/NauticalMile64/sympy>) and set up my own little >>> function in sympy.symplify called abssimp.py (just copied combsimp.py and >>> started from there), and added an appropriate if-absolute check in the main >>> simplify function. Is this the right way to go about adding such a feature? >>> Would the code that I write here also be used in solve or something? >>> >>> Any guidance / advice would be appreciated. >>> >>> Thanks! >>> Nolan Dyck >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "sympy" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to sympy+un...@googlegroups.com. >>> To post to this group, send email to sy...@googlegroups.com. >>> Visit this group at https://groups.google.com/group/sympy. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/sympy/aeb34566-d4b8-46f4-8eb5-f7acabbd12f4%40googlegroups.com >>> <https://groups.google.com/d/msgid/sympy/aeb34566-d4b8-46f4-8eb5-f7acabbd12f4%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sympy+unsubscr...@googlegroups.com. > To post to this group, send email to sympy@googlegroups.com. > Visit this group at https://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/9128b7db-cb5b-4551-81fb-d7eb3b8195f4%40googlegroups.com > <https://groups.google.com/d/msgid/sympy/9128b7db-cb5b-4551-81fb-d7eb3b8195f4%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CALOxT-%3DNgzQNij8r83nMhG6WUvyyDSVjC9FvvYKCHHLvuD7WFg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.