Rationalize the elements in an array if they are not rationals

I want to rationalize the elements in an array if they are not rationals. I tried with the following, but failed:

In[22]:= aa = {{1, 2}, {3, 4, 6.5}};
If[ Element[aa, Rationals] != True, aa = aa // Rationalize]
aa

Out[23]= If[(6.5 \[Element] Rationals) != True, aa = Rationalize[aa]]

Out[24]= {{1, 2}, {3, 4, 6.5}}

Any hints for achieving this goal?

Regards,
Zhao

In cases where the rationalization is “obvious”, one can do it like so.

In[140]:= aa = Rationalize[{{1, 2}, {3, 4, 6.5}}]

Out[140]= {{1, 2}, {3, 4, 13/2}}

This is just an example. In my actual situation, I need to determine the data in the array list provided to the user-defined function in real time, and then do the corresponding processing described here.

If for some unforeseen reason the method in the response will not work, feel free to post a new question showing the actual case of interest rather than a case that’s not relevant.

What I mean is like the following:

In[5]:= RationalizeitIfNot[l_list] := l

In[4]:= gensSG141ITA1 = {
    {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}},
    {{-1, 0, 0, 1/2}, {0, -1, 0, 1/2}, {0, 0, 1, 0.68}, {0, 0, 0, 
     1}},
    {{0, -1, 0, 0}, {1, 0, 0, 0.54}, {0, 0, 1, 1/4}, {0, 0, 0, 1}},
    {{-1, 0, 0, 1/2}, {0,  1, 0,  0}, {0,  0,  -1, 0.47}, {0, 0, 0, 
     1}},
    {{-1, 0, 0, 0}, {0, -1, 0, 1/2}, {0, 0, -1, 1/4}, {0, 0, 0, 1}},
    {{1, 0, 0, 1/2}, {0, 1, 0, 1/2}, {0, 0, 1, 1/2}, {0, 0, 0, 1}}
   }; 

In[6]:= RationalizeitIfNot[gensSG141ITA1]

Out[6]= RationalizeitIfNot[{{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 
    0}, {0, 0, 0, 1}}, {{-1, 0, 0, 1/2}, {0, -1, 0, 1/2}, {0, 0, 1, 
    0.68}, {0, 0, 0, 1}}, {{0, -1, 0, 0}, {1, 0, 0, 0.54}, {0, 0, 1, 
    1/4}, {0, 0, 0, 1}}, {{-1, 0, 0, 1/2}, {0, 1, 0, 0}, {0, 0, -1, 
    0.47}, {0, 0, 0, 1}}, {{-1, 0, 0, 0}, {0, -1, 0, 1/2}, {0, 0, -1, 
    1/4}, {0, 0, 0, 1}}, {{1, 0, 0, 1/2}, {0, 1, 0, 1/2}, {0, 0, 1, 1/
    2}, {0, 0, 0, 1}}}]

Zhao, your question is not very clear. The quote below is impossible to understand:

In my actual situation, I need to determine the data in the array list provided to the user-defined function in real time, and then do the corresponding processing described here.

As always, it would help if expected outputs were provided. All we can go on right now is your hand-wavy description. It sounds like you want to rationalize a list of numeric data. The function to do that is Rationalize. DanielL has already demonstrated how to use it. In your new example, you could again just do

Rationalize[gensSG141ITA1]

If that does not produce the desired output, then you’ll need to explain the difference between what Rationalize gives you and what you want.

If I allow myself to speculate, then based on the name you gave your function, RationalizeitIfNot, it sounds like you might be thinking that if a number is already rational then you don’t want to do anything to it. But applying Rationalize to a number that’s already rational is safe. So there’s no reason to apply it conditionally. But that’s just speculation. You should really provide expected outputs.

Hi Eric Rimbey,

Your analysis and conjecture are exactly right, they’re exactly what I’m trying to do and I didn’t give you a full example because I don’t know how to give you a short version of an example, because the functions that I’m actually using are too cumbersome to reduce to a short representation of the problem that I’m describing here.