# [R] Expedite scalar f(x) evaluation over vectors

Charles C. Berry cberry at tajo.ucsd.edu
Thu Aug 23 23:56:47 CEST 2007

```On Thu, 23 Aug 2007, Scott Stark wrote:

> Dear R community,
>
> I am trying to code a fairly complex equation for optim().  My current
> approach is too slow for optim().
>
> I have a function that takes a double integral (hopefully correctly) across
> two terms, e.g.,
>
> doubleint <- function(c1,c2) {integrate(function(y) {
>   sapply(y, function(y) {
>   integrate(function(x) {a*(c1+x)+b*(c2+x)}, boundsx[1], boundsx[2])\$value

function(x) {a*(c1+x)+b*(c2+x)} ????

There is only one argument here. So no need for a double integral.

And is the case you wish to solve really just a polynomial in x?

If so, I'd forget integrate() and solve this one by hand.

>   })
>   }, boundsy[1],boundsy[2])
>   }
>
> I would like to rapidly evaluate this function over vectors c1 and c2 of
> equal length where the double integral is calculated for each (matching)
> element c1_i & c2_i.  At present I get length mismatch errors.  Furthermore,
> mapply() takes too long.  Can I expedite this evaluation or do I need to
> reformulate my approach?
>
> Scott C. Stark
> University of Arizona
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                            (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

```