Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:33:08+00:00 2026-06-13T18:33:08+00:00

Is it possible to vectorise the following function, ( f )? I have a

  • 0

Is it possible to vectorise the following function, (f)?

I have a vector x for which I want to maximise the output value of the function f by altering p.

But the function is quite slow as it is not vectorised in anyway and was wondering if there was a good way to do so. The idea is to parallelise this in the future, and also potentially use data.table to speed it up

my real data is significantly larger…so I’m providing a mock example….

# My mock data 
x <- data.frame(x=rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20))

# The function to optimise for
f <- function(p,x){
    # Generate columns before filling
    x$multiplier <- NA
    x$cumulative <- NA

    for(i in 1:nrow(x)){
        # Going through each row systematically
        if(i==1){
            # If first row do a slightly different set of commands
            x[i,'multiplier'] <- 1 * p
            x[i,'cumulative'] <- (x[i,'multiplier'] * x[i,'x']) + 1
        } else {
            # For the rest of the rows carry out these commands
            x[i,'multiplier'] <- x[i-1,'cumulative'] * p
            x[i,'cumulative'] <- (x[i,'multiplier'] * x[i,'x']) + x[i-1,'cumulative']
        }
    }

# output the final row's output for the cumulative column
as.numeric(x[nrow(x),'cumulative'])
}

# Checking the function works by putting in a test value of p = 0.5
f(0.5,x)

# Now optimise the function between the interval of p between 0 and 1
optim.p <- optimise(f=f, interval=c(0,1),x, maximum=TRUE)

# Viewing the output of optim.p
optim.p
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:33:10+00:00Added an answer on June 13, 2026 at 6:33 pm

    (Edit – forgot the first part of the post I had written, putting it in now).

    Your problem can be simplified by examining what your function f actually does. Since I’m lazy, I’m going to write x[i, 'multiplier'] as mi, x[i, 'cumulative'] as yi, and x[i, 'x'] as xi.

    Let’s look at your equation in f. We’ll look at the case i > 1 first:

    mi = yi-1 * p
    yi = mi * xi + yi-1

    Substitute m_i above:

    yi = (yi-1 * p) * xi + yi-1 // let’s factorise..
    yi = yi-1 * (p * xi + 1)

    This dispenses with the need to calculate the multipler column.

    Now looking a little closer at your i == 1 case, we see that if we put y0 to 1, the following works out for all i = 1, …, nrow(x):

    yi = yi-1(pxi + 1) ———- (1)

    Looking at your function f, what you want to calculate is yn:

    yn = yn-1(pxn + 1)

    What happens if we substitute the formula for yn-1 in the above using (1)?

    yn = yn-2(pxn-1 + 1)(pxn + 1)

    Now we substitute in yn-2‘s formula in the above:

    yn = yn-3(pxn-2 + 1)(pxn-1 + 1)(pxn + 1)

    You get the pattern, right? We substitute all the way down to y1:

    yn = y0(px1 + 1)(px2 + 1)…(pxn-1 + 1)(pxn + 1)

    But remember, y0 is just 1. So, to calculate the value of f(x, p), we just do:

    f(x, p) = (px1 + 1)(px2 + 1)…(pxn-1 + 1)(pxn + 1)

    where n is nrow(x). That is, calculate p * x[i, 'x'] + 1 for each i and multiply them all together.


    To multiply a vector of numbers together in R, you use prod. So, if x was just a vector:

    f_version2 <- function(p, x) {                                              
        return(prod(p * x + 1))                                                 
    }                                                                           
    

    Let’s test it on a few things:

    x <- rep(c(rep(c(0.2,-0.2),4),0.2,0.2,-0.2,0.2),20)                         
    
    > f(0.5, x)                                                                 
    [1] 16.56635                                                                
    > f_version2(0.5, x)                                                        
    [1] 16.56635                                                                
    

    In summary, sometimes you can achieve speedups simply by analysing the maths of the problem, as well as/opposed to the numerical implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation solved with a vector , but one of my
Possible Duplicate: How to do the vector of sets in C++? I want to
Is it possible to vectorise code like the following? length(x) <- 100; x[1] <-
I have the following issue: I extracted a set of data but part of
Is it possible to write a c++ template function which takes a variable number
I'm writing a Haskell library which uses Data.Vector 's. I successfully wrote library function,
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: check whether internet connection is available with C# I just want to
I want to vectorize the following MATLAB code. I think it must be simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.