How can I test if a number is irrational?

조회 수: 52(최근 30일)
Alexandre Piccini
Alexandre Piccini 2017년 1월 11일
댓글: Walter Roberson 2020년 10월 11일
Hello there,
In digital signal processing, one indicator that a signal may be quasi-periodic is that the ratio between the pair of frequencies (f1/f2) provides an irrational number as result. What I want to do is check if the result is irrational or not in the form of a function, just as ge(a,b) returns a boolean relative to 'a' being greater or equal to 'b' or not. However, I was not able to find any function appropriate for this (and no other posts regarding this too).
P.S.: I know that the user will know if (s)he is inputing an irrational number. As silly as it seems, I still want to display a message saying whether it is periodic or quasi-periodic. By the way, of course, there is still the possibility of inputing two irrational numbers, one for each frequency, and having a rational result. This is why such a check becomes helpful.
EDIT 1: As an addition, I guess the aformentioned may have mislead most of you to believe that I wanted MATLAB to tell me if a number is rational or not only by its double or int value. However, I'm thinking about what is going in the Symbolic Toolbox level.
Take, for instance, these parameters:
f1 = 2;
f2 = sqrt(2);
a = f1/f2;
Provides (or must provide) in symbolical level...
a = sqrt(2);
Since I can work, for instance, with functions. I'm considering that this is different from
a = double(f1/f2);
We know that if we type this, say, in the command window, f1/f2 will yield the same as double(f1/f2), but the processing is not exactly the same for these cases. My knowledge on MATLAB may fail at this point, but I believe that in some point in the processing of data, f1/f2 still is a symbolic value before it is converted to double to be displayed. That is the level of processing that I'm trying to work with. It seems reasonable to me that, at this stage, it may be easy to infer the "class" of our symbolic number... or isn't it?
  댓글 수: 3
Alexandre Piccini
Alexandre Piccini 2017년 1월 14일
Thank you Walter,
This actually sheds some light in part of the problem, which is having a symmbolic value to analyse.
Ok, so, answering what you asked before:
"Is it correct that in your situation, the user might have communicated to you two arbitrary symbolic constant expressions (that is, something that might be a formula but does not have free variables) and the task is to determine whether the ratio is irrational?" - Yes
If so then can it be further restricted to there being no variables at all, not just no free variables (so for example no definite integration over a formula for which there might not happen to be a closed form solution?) - Yes
Are there any other restrictions? For example can you guarantee that none of the values will be complex? Can there be roots of a higher order polynomial? - Only positive and real numbers are to be considered for f1 and f2. No variables, only constants of any kind within real numbers. In terms of roots, I am only considering roots of integers or rationals. There is no restriction that it has to be specifically a square or cubic (or further) root.

댓글을 달려면 로그인하십시오.


Guillaume 2017년 1월 11일
편집: Guillaume 2017년 1월 11일
I don't think you've thought this through properly:
  1. Nobody knows a way to find whether an arbitrary number is irrational.
  2. By definition, all numbers stored on a computer (in IEE754 format) are rationals, since they're all fractions of powers of 2.
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 10월 11일
No-one has been able to prove whether pi+e is rational or not. If there was an existing algorithm then the question would have been answered by using that algorithm.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2017년 1월 11일
In MATLAB, all numbers that can be expressed in the data types uint8, uint16, uint32, uint64, int8, int16, int32, int64, and logical are definitely rational. For the data types single and double, the only values that can be expressed that are not rational are -inf, +inf, and NaN, but those values are also not irrational (they are not, strictly speaking, numbers.)
Therefore the only way numbers could be entered that might potentially be irrational is if they are entered as strings, either as named constants or as expressions like sqrt(2) and you would have to proceed from there.
  댓글 수: 8
Christopher Creutzig
Christopher Creutzig 2018년 3월 27일

> Given two symbolic constants, f1 and f2, that involve only numbers and roots of integers or rationals, then MATLAB will automatically factor the rationals inside of roots and remove as much outside the root as possible.

That is not exactly true. E.g., MATLAB will not cancel sqrt(sym(6))/sqrt(sym(2)) by default. A call to simplify will.

댓글을 달려면 로그인하십시오.

John D'Errico
John D'Errico 2017년 1월 12일
편집: John D'Errico 2017년 1월 13일
You say there should be some simple way to know if a symbolic toolbox result is rational or not. Note that as simple a question of whether pi+exp(1) is rational is unproven as far as I can see.
You can want a nice simple solution to exist. But nice simple solutions are not always available.
You talk about a ratio between a pair of frequencies as a rational number. To me, this is silly, that you are worried about something being EXACTLY representable as a rational number, beyond 16 significant digits. Instead, you might just look if the ratio is well approximated by a fraction with a reasonably small denominator.
[N,D] = rat(.2324343434232,0.001)
N =
D =
ans =
[N,D] = rat(0.30000001,0.001)
N =
D =
Pick some reasonable tolerance, and don't worry about 40 digits.
  댓글 수: 2
Alexandre Piccini
Alexandre Piccini 2017년 1월 14일
It is more a matter of finding out if what I said already exists/is possible to be done in MATLAB. There are several ways I could get to the same conclusion (the signal is quasiperiodic), through countless methods of tackling the same problem. I could, for instance, take two cycles of quantized data and compare them...
cont = 1;
for i = 1:samplingrate:lambda
x1[cont] = num2str(signal(i));
cont = cont + 1;
for i = 1:samplingrate:lambda
x2[cont] = num2str(signal(i+lambda));
cont = cont + 1;
notquasi = strcmp(1,2)
If notquasi = 1, means that both strings are equal, and hence the signal is periodic. This code is just hypothetical - there might be a detail here and there not clicking.
Anyway, as I said, this is one amongst several ways of tackling this question. It is more about mathematical "righteousness" than actual practicality.
Thanks for the help.

댓글을 달려면 로그인하십시오.

David Goodmanson
David Goodmanson 2017년 1월 16일
편집: David Goodmanson 2017년 1월 16일
Hello Alexandre, although your interest is along conceptual lines, still it's fun to look at practical consequences. Supposing the two frequencies are high, up around the GHz cell phone range, and have all 16 digits of double precision:
f1 = 1234567890123456e-6
f1 = 1234567890123457e-6
If f1 and f2 are incommensurate (gcd = 1) as in this example, and supposing you had frequency stability of better than one part in 10^16 (which isn't going to happen outside of a metrology lab) then it will take 1e6 seconds before the carrier pattern repeats, or about 12 days. Not even Heloise and Abelard would be on the phone that long.
There is of course a big difference philosophically, but when you get enough digits the distinction between incommensurate and 'irrational ratio' becomes moot.
  댓글 수: 4
David Goodmanson
David Goodmanson 2018년 3월 26일

댓글을 달려면 로그인하십시오.

LALE ASIK 2018년 3월 23일
편집: LALE ASIK 2018년 3월 23일
May I ask you a question? Do you find a method how to test it?
  댓글 수: 6
Walter Roberson
Walter Roberson 2018년 3월 27일
For example, consider 43 and 179 with an fft bin width of 1. 43/179 is 0.240223463687151, which needs all of the decimals to express the ratio of frequencies as far as humans can easily perceive. But with the bin width of 1, the value could be between 42/180 and 44/178, and 42/180 is 0.233333333333333 which is 7/30 which people probably would consider rational.
Can we get a measure of rationality by using rat() and looking at the complexity of the continued fraction?
>> rat(43/179)
ans =
'0 + 1/(4 + 1/(6 + 1/(7)))'
>> rat(42/180)
ans =
'0 + 1/(4 + 1/(4 + 1/(-2)))'
>> rat(44/178)
ans =
'0 + 1/(4 + 1/(22))'
>> rat(pi)
ans =
'3 + 1/(7 + 1/(16))'
Ummm... No, apparently not. When rat(pi) shows up as less complex than the other values, we are in trouble. Unless, that is, we start adding in the tolerance to the rat() call:
>> rat(pi,1e-16)
ans =
'3 + 1/(7 + 1/(16 + 1/(-294 + 1/(3 + 1/(-4 + 1/(5 + 1/(-15)))))))'
But still, what we would tend to see as simple, 42/180, 0.23* comes out more complex than 44/178, 0.247191011235955, that we would struggle to see rationality in.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by