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 7752257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:45:12+00:00 2026-06-01T11:45:12+00:00

I’m trying to speed up my code because it’s running very long. I already

  • 0

I’m trying to speed up my code because it’s running very long. I already found out where the problem lies. Consider the following example:

x<-c((2+2i),(3+1i),(4+1i),(5+3i),(6+2i),(7+2i))
P<-matrix(c(2,0,0,3),nrow=2)
out<-sum(c(0.5,0.5)%*%mtx.exp(P%*%(matrix(c(x,0,0,x),nrow=2)),5))

I have a vector x with complex values, the vector has 12^11 entries and then I want to calculate the sum in the third row. (I need the function mtx.exp because it’s a complex matrix power (the function is in the package Biodem). I found out that the %^% function does not support complex arguments.)

So my problem is that if I try

sum(c(0.5,0.5)%*%mtx.exp(P%*%(matrix(c(x,0,0,x),nrow=2)),5))

I get an error: “Error in pot %*% pot : non-conformable arguments.” So my solution was to use a loop:

tmp<-NULL
for (i in 1:length(x)){
  tmp[length(tmp)+1]<-sum(c(0.5,0.5)%*%mtx.exp(P%*%matrix(c(x[i],0,0,x[i]),nrow=2),5))
}

But as said, this takes very long. Do you have any ideas how to speed up the code? I also tried sapply but that takes just as long as the loop.

I hope you can help me, because i have to run this function approximatly 500 times and this took in first try more than 3 hours. Which is not very satisfying..

Thank u very much

  • 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-01T11:45:13+00:00Added an answer on June 1, 2026 at 11:45 am

    The code can be sped up by pre-allocating your vector,

    tmp <- rep(NA,length(x))
    

    but I do not really understand what you are trying to compute:
    in the first example,
    you are trying to take the power of a non-square matrix,
    in the second, you are taking the power of a diagonal matrix
    (which can be done with ^).

    The following seems to be equivalent to your computations:

    sum(P^5/2) * x^5
    

    EDIT

    If P is not diagonal and C not scalar,
    I do not see any easy simplification of mtx.exp( P %*% C, 5 ).

    You could try something like

    y <- sapply(x, function(u) 
      sum( 
        c(0.5,0.5) 
        %*% 
        mtx.exp( P %*% matrix(c(u,0,0,u),nrow=2), 5 )
      )
    )
    

    but if your vector really has 12^11 entries,
    that will take an insanely long time.

    Alternatively, since you have a very large number
    of very small (2*2) matrices,
    you can explicitely compute the product P %*% C
    and its 5th power (using some computer algebra system:
    Maxima, Sage, Yacas, Maple, etc.)
    and use the resulting formulas:
    these are just (50 lines of) straightforward operations on vectors.

    /* Maxima code */ 
    p: matrix([p11,p12], [p21,p22]);
    c: matrix([c1,0],[0,c2]);
    display2d: false;
    factor(p.c . p.c . p.c . p.c . p.c);
    

    I then copy and paste the result in R:

    c1 <- dnorm(abs(x),0,1); # C is still a diagonal matrix
    c2 <- dnorm(abs(x),1,3);
    p11 <- P[1,1]
    p12 <- P[1,2]
    p21 <- P[2,1]
    p22 <- P[2,2]
    # Result of the Maxima computations: 
    # I just add all the elements of the resulting 2*2 matrix,
    # but you may want to do something slightly different with them.
    
              c1*(c2^4*p12*p21*p22^3+2*c1*c2^3*p11*p12*p21*p22^2
                                    +2*c1*c2^3*p12^2*p21^2*p22
                                    +3*c1^2*c2^2*p11^2*p12*p21*p22
                                    +3*c1^2*c2^2*p11*p12^2*p21^2
                                    +4*c1^3*c2*p11^3*p12*p21+c1^4*p11^5)
              +
              c2*p12
                *(c2^4*p22^4+c1*c2^3*p11*p22^3+3*c1*c2^3*p12*p21*p22^2
                            +c1^2*c2^2*p11^2*p22^2+4*c1^2*c2^2*p11*p12*p21*p22
                            +c1^3*c2*p11^3*p22+c1^2*c2^2*p12^2*p21^2
                            +3*c1^3*c2*p11^2*p12*p21+c1^4*p11^4)
             +
             c1*p21
                *(c2^4*p22^4+c1*c2^3*p11*p22^3+3*c1*c2^3*p12*p21*p22^2
                            +c1^2*c2^2*p11^2*p22^2+4*c1^2*c2^2*p11*p12*p21*p22
                            +c1^3*c2*p11^3*p22+c1^2*c2^2*p12^2*p21^2
                            +3*c1^3*c2*p11^2*p12*p21+c1^4*p11^4)
             +
             c2*(c2^4*p22^5+4*c1*c2^3*p12*p21*p22^3
                            +3*c1^2*c2^2*p11*p12*p21*p22^2
                            +3*c1^2*c2^2*p12^2*p21^2*p22
                            +2*c1^3*c2*p11^2*p12*p21*p22
                            +2*c1^3*c2*p11*p12^2*p21^2+c1^4*p11^3*p12*p21)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.