Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plus-minus object #5305

Open
asmeurer opened this issue Mar 6, 2011 · 17 comments
Open

plus-minus object #5305

asmeurer opened this issue Mar 6, 2011 · 17 comments

Comments

@asmeurer
Copy link
Member

asmeurer commented Mar 6, 2011

Do others think this would be useful?

I think we should have an object that represents plus-minus (±), and another to represent minus-plus (∓) .
They would probably be called pm and mp or something. The behavior would be

>>> a = pm*x
>>> a
±⋅x
>>> -a
∓⋅x

There are many cases where you want to keep track of two expressions that differ only by a plus/minus sign somewhere in the expression (like conjugates or something), and I think an object like this would be useful.

Original issue for #5305: http://code.google.com/p/sympy/issues/detail?id=2206
Original author: https://code.google.com/u/asmeurer@gmail.com/
Original owner: https://code.google.com/u/asmeurer@gmail.com/

@rlamy
Copy link
Member

rlamy commented Mar 6, 2011

I think it's probably more trouble than it's worth. You'd have to have to make sure that everything handles correctly expressions that hold two values at once. For instance, what would happen with limit(x**pm, x, 0)? Also, how would you get back from expressions with pm to ordinary expressions, e.g. pm * x -> [x, -x]?

Original comment: http://code.google.com/p/sympy/issues/detail?id=2206#c1
Original author: https://code.google.com/u/101272611947379421629/

@asmeurer
Copy link
Member Author

asmeurer commented Mar 6, 2011

I didn't think of things like limit(x**pm). I suppose that anything that doesn't know how to handle it explicitly should just treat it as a Symbol. My problem was mainly one of algebraic manipulation.

For pm*x => [x, -x], I suppose there should just be a function or a method pm_to_list() that would do it (or pm_to_Tuple()). Actually, it would also be useful to have a function that could convert two expressions that differ only by minus signs on terms to a single expression with pm. Then, for example, you could take the result of solve(a*x**2 + b*x + c, x) and deal with it as a single expression (this is essentially what I was doing when I realized that pm would be nice to have).

By the way, you can think of it as being similar to having something like sign(t) multiplying the terms instead of pm, where t is just some parameter. So really computing limit(x**pm, x, 0) is no different from computing limit(x**a, x, 0), where a is Real, except pm is even easier to deal with than a assumptions wise because it only has two possible values.

Actually, the only thing I see as really a potential problem is that mp should always equal -pm. But I'm not entirely sure if that would even ever come up as an issue or not.

Original comment: http://code.google.com/p/sympy/issues/detail?id=2206#c2
Original author: https://code.google.com/u/asmeurer@gmail.com/

@sherjilozair
Copy link
Contributor

sherjilozair commented Apr 22, 2011

+1 on the cool idea. Surprising how its not already there. So should stuff like limit(x**pm, x, 0) return a 2-tuple, and maybe check to see if the tuple can again be converted back to a single expr with pm in it.

Sage doesn't have this support. It might be for a reason.

Aaron, pm differs from other symbols because, other symbols are expected to have ONE value, while pm has two. So we can't treat it as just another Symbol.

But, I think its worth it, A symbolic mathematics library should have this, even if the implementation is somewhat non-trivial.

Original comment: http://code.google.com/p/sympy/issues/detail?id=2206#c3
Original author: https://code.google.com/u/107298455532294735855/

@asmeurer
Copy link
Member Author

asmeurer commented Apr 23, 2011

When you say x = Symbol('x', real=True), you are saying essentially "let x be an arbitrary element of the set R". This is exactly the same thing with pm, except it is an arbitrary element of the set {-1, 1}.

Now, since the latter is a finite set, we can expand it out to all possibilities, but as just pm, it is no different from a Symbol. The only special thing here is mp, which is equal to negative of whatever pm is.

In fact, if we ever get our new assumptions in, we might be able to extend the idea to a Symbol that is assumed to be an arbitrary element of any set that can be symbolically represented by the system (finite or infinite).

Original comment: http://code.google.com/p/sympy/issues/detail?id=2206#c4
Original author: https://code.google.com/u/asmeurer@gmail.com/

@asmeurer
Copy link
Member Author

We would also need to modify the printer to print

x ± y instead of x + ±⋅y.

Original comment: http://code.google.com/p/sympy/issues/detail?id=2206#c5
Original author: https://code.google.com/u/asmeurer@gmail.com/

@sympy-issue-migrator
Copy link

is this released ??

Original comment: http://code.google.com/p/sympy/issues/detail?id=2206#c6
Original author: https://code.google.com/u/104651149993919530338/

@asmeurer
Copy link
Member Author

No, this was never implemented. Feel free to give it a shot if you want.

Original comment: http://code.google.com/p/sympy/issues/detail?id=2206#c7
Original author: https://code.google.com/u/asmeurer@gmail.com/

@asmeurer
Copy link
Member Author

**Status:** Valid  

Original comment: http://code.google.com/p/sympy/issues/detail?id=2206#c8
Original author: https://code.google.com/u/asmeurer@gmail.com/

@asmeurer asmeurer self-assigned this Mar 7, 2014
@satels
Copy link
Contributor

satels commented Jul 25, 2014

Please, fix this.

@smichr
Copy link
Member

smichr commented Mar 14, 2016

What about introducing a PlusMinus fancy set: PlusMinus(foo) iterates as -foo, +foo?

@asmeurer
Copy link
Member Author

A set expression would work. One issue is that all instances of SetExpr({-1, 1}) will be independent of one another. So if you want two ± to be either both plus or both minus, a better way would be to use Symbol('±', real=True) (or Symbol(r'\pm', real=True) if you use LaTeX printing), and replace it with 1 or -1 at the end of the calculation as appropriate. More refined assumptions to let you specify that it's either 1 or -1 would be nice, but those are a long way off.

@meganly
Copy link
Contributor

meganly commented May 26, 2021

I'm not sure this a good idea since plus-minus isn't a mathematical function and can mean different things depending on the context. For example, in statistics it can represent the uncertainty in a confidence interval.

However, it is useful for printing. I subclassed Add to create a PlusMinus object that prints it's arguments in LaTeX with the \pm symbol. Then you can do PlusMinus(-b,sqrt(b**2-4*a*c), evaluate=False)/(2*a) to print solutions to the quadratic equation nicely. Is this something others might be interested in and I should create a pull request for?

@smichr
Copy link
Member

smichr commented Jun 12, 2021

better way would be to use Symbol('±', real=True)
should [I] create a pull request?

I like the idea of a special symbol.

@meganly, instead of subclassing Add (which can take more args and requires the evaluate=False) maybe a Function would be better.

@smichr smichr removed the Valid label Nov 19, 2021
@sylee957
Copy link
Member

  • How would you define the term algebra of PlusMinus? What kind of algorithms it can possibly be used to compute something more nontrivial?

  • Would there also be an algorithm to condense the expressions of PlusMinus or expand automatically?

  • Would plusminus have different meaning when used in equations and in algebra?

  • Should plusminus be related to more involved mathematics like set-valued function or multivalued function, than taught in elementary schools? What about building a more generic framework?

  • Would multiplication of plusminus be meaningful and whether not look strange? ()

@asmeurer
Copy link
Member Author

If the assumptions system supported arbitrary input domains (rather than just real, positive, etc.), then we could use Symbol('plusminus', {-1, 1}). That's the same thing as the SetExpr idea above, but would allow for distinct plusminus and minusplus.

@smichr
Copy link
Member

smichr commented Feb 25, 2022

but would allow for distinct plusminus and minusplus

With a set, how are you imagining that the two would be distinct?

@asmeurer
Copy link
Member Author

The point is that Symbol('plusminus') and Symbol('minusplus') are already distinct from one another, but always equal to themselves. The idea here is to also make it so that SymPy knows they are equal to either 1 or -1. I suppose there's also another question which is how to make SymPy know they are negative of each other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants