# [R] How to vectorize this function

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Fri Sep 21 00:04:21 CEST 2018

```Sorry, misread your comment, I agree. 4/2 has one arithmetic operation, (1/2)*4 has two to accomplish the same calculation.

On September 20, 2018 1:53:03 PM PDT, "MacQueen, Don" <macqueen1 using llnl.gov> wrote:
>
>I prefer
>> 4/2
>[1] 2
>
>not
>> 1/2*4
>[1] 2
>
>(I think that's what I said)
>
>And if I did want to multiply by the reciprocal, which does happen from
>time to time, I'd certainly do it this way:
>   (1/2)*4
>
>-Don
>
>--
>Don MacQueen
>Lawrence Livermore National Laboratory
>7000 East Ave., L-627
>Livermore, CA 94550
>925-423-1062
>Lab cell 925-724-7509
>
>
>
>﻿On 9/20/18, 12:36 PM, "Jeff Newmiller" <jdnewmil using dcn.davis.ca.us>
>wrote:
>
>re: your last comment... why do you prefer to multiply by the
>reciprocal?
>
>On September 20, 2018 10:56:22 AM PDT, "MacQueen, Don via R-help"
><r-help using r-project.org> wrote:
>    >In addition to what the other said, if callM is a vector then an
>    >expression of the form
>    >   if (callM <= call0)
>>is inappropriate. Objects inside the parentheses of   if()  should
>have
>    >length one. For example,
>    >
>    >> if (1:5 < 3) 'a' else 'b'
>    >[1] "a"
>    >Warning message:
>    >In if (1:5 < 3) "a" else "b" :
>>  the condition has length > 1 and only the first element will be used
>    >
>    >
>    >instead of what you have:
>    >         if(callM <= call0){
>    >           sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))
>    >         }else{
>    >           sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))
>    >        }
>    >
>    >Here are a couple of (untested) possibilities:
>    >
>    >  M.gt.0 <- callM > call0
>    >  sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))
>> sig[M.gt.0] <- (1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma -
>y)))[M.gt.0]
>    >
>    >or
>    >
>  >sig <- 1/sqrt(T)*(sqrt(gamma + y)  + ifelse(callM <= call0, -1, 1) *
>    >sqrt(gamma - y))
>    >
>    >incidentally, I would write
>    >   sig <- (sqrt(gamma + y) - sqrt(gamma - y))/sqrt(T)
>    >   sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))
>    >
>    >--
>    >Don MacQueen
>    >Lawrence Livermore National Laboratory
>    >7000 East Ave., L-627
>    >Livermore, CA 94550
>    >925-423-1062
>    >Lab cell 925-724-7509
>    >
>    >
>    >
>    >On 9/20/18, 8:08 AM, "R-help on behalf of Lynette Chang"
>><r-help-bounces using r-project.org on behalf of momtoomax using gmail.com> wrote:
>    >
>    >    Hello everyone,
>    >
>>       I’ve a function with five input argument and one output number.
>
>    >    	  impVolC <- function(callM, K, T, F, r)
>    >
>  >I hope this function can take five vectors as input, then return one
>  >vector as output. My vectorization ran into problems with the nested
>   >if-else operation. As a result, I have to write another for loop to
> >call this function. Can anyone suggest some methods to overcome it? I
>    >put my code below, thanks.
>    >
>    >    impVolC <- function(callM, K, T, F, r){
>    >
>    >
>    >     if(y >= 0){
>    >         call0 <- K*exp(-r*T)*(exp(y)*polya(sqrt(2*y)) - 0.5)
>    >         if(callM <= call0){
>    >           sig <- 1/sqrt(T)*(sqrt(gamma + y) - sqrt(gamma - y))
>    >         }else{
>    >           sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))
>    >         }
>    >     }else{
>    >         call0 <- K*exp(-r*T)*(exp(y)/2 - polya(-sqrt(-2*y)))
>    >         if(callM <= call0){
>    >           sig <- 1/sqrt(T)*(-sqrt(gamma + y) + sqrt(gamma - y))
>    >         }else{
>    >           sig <- 1/sqrt(T)*(sqrt(gamma + y) + sqrt(gamma - y))
>    >         }
>    >     }
>    >     sig
>    >    }
>    >
>    >    for(i in 1:length(call)){
>>sigV[i] <- impVolC(callM = call[i], K = df\$Strike[i], T = T, F = F, r
>=
>    >r_m)
>    >    }
>    >
>    >    ______________________________________________
> >    R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>    >    https://stat.ethz.ch/mailman/listinfo/r-help
>    >http://www.R-project.org/posting-guide.html
>>    and provide commented, minimal, self-contained, reproducible code.
>    >
>    >
>    >______________________________________________
>    >R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>    >https://stat.ethz.ch/mailman/listinfo/r-help
>    >http://www.R-project.org/posting-guide.html
>    >and provide commented, minimal, self-contained, reproducible code.
>
>    --
>    Sent from my phone. Please excuse my brevity.
>

--
Sent from my phone. Please excuse my brevity.

```