4
$\begingroup$

Following up on this question I would like to understand the way MMA represents rational numbers.

First of all, as Carl Woll suggested, x/y is not a simple rational number. Instead it is a much more convoluted sequence of operators. Indeed, as the documentation for Rational suggests, numbers entered in the form n/m only become Rational numbers on evaluation, and further, the unevaluated form is expressed in terms of Times and Power:

ClearAll[x,y]
x/y // FullForm

Times[x,Power[y,-1]]

Naively, we would expect it to be a rational number, but, following the a.m. logic, it's not:

Head[x/y]

Times

Accordingly, this evaluates to False

Rational[x,y]===x/y

Fine, there are not the same, because they are constructed differently. I am not sure I understand why this particular representation was chosen, so the question number one would be: why not simply /?

Moving forward, I would like to question the following sequence, which I don't understand:

FindInstance[Rational[x, y] =!= x/y, {x, y}, Integers]

{{x->33,y->16}}

a = Rational[33, 16]
b = 33/16
Head[a]
Head[b]

33/16

33/16

Rational

Rational

So, they are both rational, and they are the same

a===b

True

So, the question number two is what did FindInstance mean?

The plot thickens if we use the following command:

FindInstance[Rational[x, y] =!= x/y, {x, y}, Reals]

{{x->33/10,y->8/5}}

Let's substitute:

a = Rational[33/10, 8/5]
b = (33/10)/(8/5)

Rational[33/10,8/5]

33/16

Here are our 33 and 16 back! Well, let's see what they are closer:

Head[a]
Head[b]

Rational

Rational

However, not only they are not the same, they are not equal!

a===b
a==b

False

False

Well, in this case, we sort of expected this, as we explicitly asked for cases where they are not the same. But, they are! What happened to the idea that MMA should simplify 33/10 over 8/5 to 33 over 16? Even inside Rational. In the end, as Rational documentation suggests, this is just a representation of n/m, therefore, it should be simplified according to the elementary rules. But we can't even evaluate it:

Evaluate@a

Rational[33/10, 8/5]

So, the question number three is: what's going on here?

$\endgroup$
  • $\begingroup$ Consider that Rational[33/10, 8/5] // NumericQ gives False. $\endgroup$ – m_goldberg Jan 24 '17 at 2:04
  • $\begingroup$ @m_goldberg Right, and I thought it is a strange solution, because Rational is defined as integer/integer, but FindInstance sort of suggested it. Anyways, even if so, outside of the fact that there should be no solution, I think it should at least be able to simplify it to the case of integer/integer when the arguments are rationals themselves. $\endgroup$ – Stitch Jan 24 '17 at 2:20
8
$\begingroup$

Rational is for exact numeric values, not symbolic expressions, i.e. its arguments must be integers. Rational[x,y] is syntactically valid input but no more meaningful than foo[x,y].

FindInstance[foo[x, y] =!= x/y, {x, y}, Integers]
{{x -> 33, y -> 16}}

FindInstance["meaningless" =!= x/y, {x, y}, Integers] gives the same output. Why values 33 and 16 are chosen as a starting point I do not know.

In evaluated expressions Mathematica does not use Divide or Subtract, replacing these with "equivalents" using Times. I suppose this was chosen to reduce the number of rules needed for transformations but there are apparent consequences, e.g. see my own question:

$\endgroup$
  • $\begingroup$ Setting RandomSeed to something different will of course change the result: FindInstance[foo[x, y] =!= x/y, {x, y}, Integers, RandomSeed -> 9] $\endgroup$ – J. M. is away Jan 24 '17 at 3:30
  • $\begingroup$ @J.M. That is great! Thank you. $\endgroup$ – Stitch Jan 24 '17 at 3:35
  • $\begingroup$ @Mr. Wizard Brilliant explanation. Thanks! I knew there is something there in that madness. $\endgroup$ – Stitch Jan 24 '17 at 3:37
4
$\begingroup$

I am not sure why

FindInstance[Rational[x, y] =!= x/y, {x, y}, Reals]

returned

{{x -> 33/10, y -> 8/5}}

but that result certainly satisfies your query. But the result is not equivalent to asserting Rational[33/10, 8/5] is an answer to your query because Rational[33/10, 8/5] is semantic nonsense.

Further, I would argue that your query is ill-posed. You should have written

FindInstance[Rational[x, y] != x/y, {x, y}, Integers]

Then you would have gotten the message

msg

Perhaps this would have been more meaningful to you.

$\endgroup$
  • $\begingroup$ Right, I thought Rational can deal with any reasonable inputs, but apparently it is not designed for this. $\endgroup$ – Stitch Jan 24 '17 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.