someone
someone

Reputation: 132

How to make SymPy Max(2,n) be n?

I'm practicing SymPy and find out Max operator can not solve when value is greater than 1.

n = Symbol('n', integer=True, positive=True)
Max(1,n)    # this works fine
Max(2,n)    # output Max(2, n)

I'm confusing why Max can not solve it when the other value is greater than 1.

Upvotes: 0

Views: 85

Answers (2)

smichr
smichr

Reputation: 19093

You could try rewrite as Piecewise to see the conditions of Max explicitly:

>>> Max(2,n).rewrite(Piecewise)
Piecewise((2, n <= 2), (n, True))

Upvotes: 1

CryptoFool
CryptoFool

Reputation: 23119

Both results are correct. If n must be a positive integer, then the answer to Max(1, n) will be n for any n. But for Max(2, n), the answer will be n if n > 1, otherwise it will be 2. A way to state that is Max(2, n), and that's what SymPy is telling you.

Take off the positive constraint on n and then both answers will come out in the same form as the input, because now Max(1, n) will no longer be n for all possible values of n that meet the restrictions (all integers).

Upvotes: 1

Related Questions