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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:25:21+00:00 2026-05-30T09:25:21+00:00

The code below calculates the correlation matrix given a covariance matrix. How can I

  • 0

The code below calculates the correlation matrix given a covariance matrix. How can I write this better? The issue is this section of code will run 1000s of times on matrices whose dimensions are about 100 x 100.

// Copy upper triangle of covariance matrix to correlation matrix
for(i = 0; i < rows; i++){
  for(j = i; j < rows; j++){
    corrmatrix.array[i * rows + j] = covmatrix.array[i * rows + j];
  }
}

// Calculate upper triangle of corr matrix
for(i = 0; i < rows; i++){

  root = sqrt(covmatrix.array[(i * rows) + i]);    

  for(j = 0; j <= i; j++){ // Move down 
    corrmatrix.array[ j * rows + i ] /= root;
  }

  k = i * rows;

  for(j = i; j < rows; j++){ // Move across
    corrmatrix.array[ k + j ] /= root;
  }

}

// Copy upper triangle to lower triangle
for(i = 0; i < rows; i++){
  k = i * rows;
  for(j = i; j < rows; j++){
    corrmatrix.array[ (j * rows) + i ] = corrmatrix.array[ k + j ];
  }
}

I have made checks that the rows and columns are equal etc, so I am just using rows everywhere. I want to optimize the speed (significantly).

PS:

  1. Matrices are stored in row-major, dense format
  2. I am not using packed storage for now.

Thank you

  • 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-05-30T09:25:23+00:00Added an answer on May 30, 2026 at 9:25 am

    The first thing that jumps out at me is that you’re doing division by the same number in your inner loops.

    Don’t do that. Division is slow.

    What you should do instead is to multiply by the reciprocal of root instead of dividing by it repeatedly:

    inv_root = 1. / sqrt(covmatrix.array[(i * rows) + i]);
    
    for(j = 0; j <= i; j++){ // Move down 
        corrmatrix.array[ j * rows + i ] *= inv_root;
    }
    
    k = i * rows;
    
    for(j = i; j < rows; j++){ // Move across
        corrmatrix.array[ k + j ] *= inv_root;
    }
    

    Although this optimization may seem obvious to a compiler, it may not be allowed to do this optimization due to floating-point strictness. You can try relaxing your floating-point settings with -ffast-math (in GCC) or something similar.

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

Sidebar

Related Questions

Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
The code below gives me this mysterious error, and i cannot fathom it. I
The code below is used to calculate the miles between two cities. In this
I have the code snippet below in C++ which basically calculates the pi using
When I run my code below I get a: ValueError: invalid literal for int()
The code below demonstrates the problematic joda-time implementation of week calculation. This behavior is
Code below is not working as expected to detect if it is in design
Code below is working well as long as I have class ClassSameAssembly in same
The code below shows a sample that I've used recently to explain the different
The code below works. But if I comment out the line Dim objRequest As

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.