Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.1K Views
|
7 Replies
|
1 Total Like
View groups...
Share
Share this post:

Rationalize the elements in an array if they are not rationals

Posted 3 years ago

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

POSTED BY: Hongyi Zhao
7 Replies
Posted 3 years ago

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.

POSTED BY: Hongyi Zhao
Posted 3 years ago

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.

POSTED BY: Eric Rimbey
Posted 3 years ago

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.

POSTED BY: Hans Milton
Posted 3 years ago

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}}}]
POSTED BY: Hongyi Zhao

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.

POSTED BY: Daniel Lichtblau
Posted 3 years ago

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.

POSTED BY: Hongyi 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}}
POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard