Shame on you, R... again! (But not really...)

Monday, January 17, 2011 at 2:25 PM Bookmark and Share
Remember how a few months ago I lamented the fact that the round() function in R uses a non-standard rule for rounding to the nearest integer?  Instead of rounding k+0.5 to k+1 (k being an integer) R rounds to whichever integer k or k+1 is even.  Well here's another example of R offending our mathematical sensibilities... R seems to think that even though
1 * Inf = Inf
somehow it can get away with telling us that
1 * (Inf + 0i) = Inf + NaNi?


"Gasp!" I know, insane, right? What's going on here? Whatever happened to "anything times one is equal to that same number"? Granted, infinity isn't really a number so sometimes we can't assign a value to an expression like Inf*0, but deep down inside I can't shake the feeling that 1 * Inf really should be Inf!

It turns out that R and I are both right - we're just making different assumptions about how we interpret all these 1s, 0s and Infs in these two statements. Let me explain...

Despite using sound, puppy-approved logic in this case, R gives the offending result because of how it implements everyone's favorite section in Calculus class: computing limits.  To understand why, take a closer look at how the multiplication is happening in each case above.  The first case is hopefully straightforward.  In the second case 1 is treated as a complex number instead of a scalar which gives 
1*(Inf+0i) = (1+0i)*(Inf+0i)  =           
           = Inf + (0*Inf+0)i = Inf + NaNi

We could also throw in a third case and multiply these two complex numbers in the more natural context of polar coordinates. Writing each in terms of their modulus r (distance from the origin) and argument θ (angle off of the positive real axis) instead of in terms of their real and imaginary parts, we have
   1*(Inf+0i) = (1+0i)*(Inf+0i)       
             = 1exp(i0)* Inf exp(i0)
        = (1*Inf) exp (i0)
   = Inf exp(i0)
= Inf + 0i
Whew! So what's "wrong" with multiplying things in x+yi form??

R recognizes that any computations involving infinity really require the algebra of limits, and acts appropriately (albeit conservatively) to evaluate such expressions. This discord then comes from what R assumes is the result of taking some limit and what is to be treated as a constant. Unless you've taught a calculus class recently some explanation might be in order.

In general, expressions involving infinity are treated as limits where some unspecified variable is going to infinity:  For example, statements like Inf*0 can't be assigned a value because in it's most general interpretation we're asking "What is the limit of the product of x*y as x→Inf and y→0?"  Here, whether y goes to 0 from above (e.g. y=1/x) or below (y=-1/x) or neither will determine where the limit of the product goes to zero, some non-zero number, plus or minus infinity, or will have no limit at all. (Open any calculus text to the sections on limits for examples leading to these different outcomes).  Note this example does have an answer if y is always assumed to be 0, since it's always the case that x*0=0.

That means that, depending on how we interepret the zero, our example might equal either
  Inf*0=NaN
Inf*0=0
This is exactly what's going on above.

Returning to the two statements at the top of this post, we can now understand why R gives these two different answers.  By making the zero implicit vs. explicit R treats these expressions differently. R interprets Inf as "the limit of x + 0i as x→Inf," allowing for the result that
1 * Inf = Inf
whereas in the second case R treats Inf + 0i as "the limit of x + yi as x → Inf and y → 0" which has no general answer  and therefore gets assigned a value of NaN.

The take-home message: as soon as there's an Inf in an expression, R proceeds assuming everything is a limit, even though it might be clear to the user that some of those key 1s and 0s should be treated as constants.

14 comments:

Posted by: Unknown | 6/28/2020 8:01 AM

EFFECTIVE LOVE SPELL TO GET EX HUSBAND/BOYFRIEND OR EX WIFE/GIRLFRIEND AND CURE HERPES AND INFERTILITY THAT WORKS WITHIN 24 HOURS. Contact Dr. Odunga at odungaspelltemple@gmail. com OR Whats App/CALL +2348167159012
I got my ex husband back to me and also got fertile and gave birth to twin girls after 12 years of marriage.
When I came online last year, I saw a testimony about Doctor ODUNGA and how he has been helping people with marriage issues and I decided to contact him. We spoke on email at odungaspelltemple@gmail. com and later he gave me his WhatsApp number which is +2348167159012. He assured me he would help me get my ex husband back after 3 years of no contact. Doctor Odunga is the best spell caster I must say. My husband called me after 24 hours of contact. The third day, I received natural herbs for fertility and after using it, I became fertile and conceived. I and my husband reconnected, had sex and I was pregnant. Presently I am happy to say I am the mother of twin girls and this is ALL THANKS TO DOCTOR ODUNGA. I came online to say this If you need help, I believe you are saved with this man. Contact his email at odungaspelltemple@gmail. com OR Whats App/Call +2348167159012 and you too will give a testimony too to help others

Posted by: Sophia Mason | 3/31/2021 10:39 AM

GET RICH WITH BLANK ATM CARD ... Whatsapp: +18033921735

I want to testify about Dark Web blank atm cards which can withdraw money from any atm machines around the world. I was very poor before and have no job. I saw so many testimony about how Dark Web hackers send them the atm blank card and use it to collect money in any atm machine and become rich. ( darkwebblankatmcard@gmail.com ) I email them also and they sent me the blank atm card. I have use it to get 90,000 dollars. withdraw the maximum of 5,000 USD daily. Dark Web is giving out the card just to help the poor. Hack and take money directly from any atm machine vault with the use of atm programmed card which runs in automatic mode.

Email: darkwebblankatmcard@gmail.com
Text & Call or WhatsApp: +18033921735

Posted by: Stephanie Sally | 4/30/2021 7:05 AM

Hello everyone, I am from Wembley, Britain. I want to write this testimony to tell others and thank Dr. Odunga for what he has done for me. The first 12 years of my marriage I have had 5 miscarriages and I was called all sorts of names by my mother-in-law and this made my marriage life very hectic and a burden of sorrow. I contacted Dr. Odunga for help and I will say that he is a very strong and honest man and he indeed helped me solve my problem. I saw his email in a testimony and i contacted him, little did i know it would be the end of all my problems. After 2 days of contact, I received a fertility herb and he told me to use it. The herb worked and my husband even loved me more and bought me expensive things. One afternoon, I went to a nearby hospital and came back home with the positive result of my pregnancy and after 9 months I gave birth to a baby boy. Ever since I contacted Dr. Odunga, my story has been different. I have 3 children at present and I am very happy in my marriage. Please, contact him at odungaspelltemple@gmail.com OR Whats App him +2348167159012 to help you too

Posted by: Anonymous | 4/30/2021 4:03 PM

Five weeks ago my boyfriend broke up with me. It all started when i went to summer camp i was trying to contact him but it was not going through. So when I came back from camp I saw him with a young lady kissing in his bed room, I was frustrated and it gave me a sleepless night. I thought he will come back to apologies but he didn't come for almost three week i was really hurt but i thank Dr.Azuka for all he did i met Dr.Azuka during my search at the internet i decided to contact him on his email dr.azukasolutionhome@gmail.com he brought my boyfriend back to me just within 48 hours i am really happy. What’s app contact : +44 7520 636249‬

Posted by: SIMON PATCHIN | 6/12/2021 4:55 PM

How To Get Your Ex Lover/Partner/Boyfriend/Girlfriend/Wife Or Husband Back

It breaks my heart to know that people are hurting so badly that they are desperate to get their love lives back. For anyone hurting right now, I want you to worry no more for your Ex lover/partner will come back to you so you can feel whole again and I truly mean that. When my wife left me, Lord Zakuza was the man I contacted that helped me to get my wife back with his powerful spell within 48 hours and I can never forget the day I meant Lord Zakuza for he's spells perform GREAT MIRACLES. Get in touch with Lord Zakuza now for he's like a God on earth that can get your EX lover back within 48 hours with his powerful spell. You can go get in touch with him through the below details.


WhatsApp/call/text: +1 740 573 9483
Website via: lordzakuzaspells.com
Email via: Lordzakuza7 @ gmail. com

Posted by: Unknown | 7/03/2021 1:00 PM

It is a very hard situation when playing the lottery and never won, or keep winning low fund not up to 100 bucks, i have been a victim of such a tough life, the biggest fund i have ever won was 100 bucks, and i have been playing lottery for almost 12 years now, things suddenly change the moment i came across a secret online, a testimony of a spell caster called DR EMU, who help people in any type of lottery numbers, i was not easily convinced, but i decided to give try, now i am a proud lottery winner with the help of DR EMU, i won $1,000.0000.00 and i am making this known to every one out there who have been trying all day to win the lottery, believe me this is the only way to win the lottery.

Contact him via email Emutemple@gmail.com
What's app +2347012841542
Https://emutemple.wordpress.com/

Posted by: Sarah Nicky | 9/17/2021 8:15 AM

My wish right now is that ALLAH should continue to bless Dr.John for his good works towards the life of those people who are heart broken. My name is Sarah Nicky and i am from Candad, it been a while since when my lover attitude changed from being the caring type he has been to not been caring at all. But not long, I later discovered that my lover was having an affair with someone else but just within 48 hours that i contacted Dr.John through these detail drjohnsoco@gmail.com and +1 706 871 4571, my lover returned back and broke up with the other girl he was having a relationship with.

Posted by: Jeffrey Collins | 5/31/2022 4:56 AM

BookMyEssay is one of the best platform in the field of Essay Structure Writing Help . They have talented experts who will share your burden off shoulders and provide with the best writing services withing the time limits.

Posted by: John Hardy | 9/08/2022 7:51 AM

Such a great blog nice to read your blog.I love science blog .In this blog we know about science is a very great knowledge for students .Thanks for sharing this blog and great experienceNow it's time to avail town car service Houston for more information.

Posted by: Anonymous | 1/09/2023 8:13 AM

I used the help of ………Robinsonbuckler @yahoo. com, to get back my wife after three years o breakup…

Posted by: Jonny Dave | 2/03/2023 4:52 AM

Your Blog is informative for mathematical students. I like it you such a amazing person. I will recommend this blog with other people. Now it's time to avail Luxury Limo Services in Long Beach CA for more information.

Posted by: Tim Steve | 2/09/2023 3:56 AM

Thanks for sharing such a beautiful blog. I love to read this blog. Keep doing this and stay update us. Now it's time to avail jupiter florida airport for more information.

Posted by: Shannon Mackle | 7/12/2023 10:21 PM

Get Your Lover Back & Avoid breakup again,

Really Work Fast,

Happy to have my boyfriend back after 3 months of breakup,

Thanks to (robinsonbuckler11 @)gmail com

Posted by: Michael White | 2/21/2024 4:35 AM

Thanks for sharing such a beautiful blog. I really love this blog, Writing is not your forte? We can understand. Assignments are not as easy as they sound. Many students lose a lot of their marks because they don’t have good penmanship. Come to us and allow our Assignment Help Online to help you score the highest grade.

Post a Comment