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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:42:56+00:00 2026-06-13T11:42:56+00:00

I have been using CHOLMOD to factorise the matrix A and solve the system

  • 0

I have been using CHOLMOD to factorise the matrix A and solve the system Ax = b, for A being the Hessian matrix (printed below) and b = [1, 1, 1] created by the cholmod_ones function.

Unfortunately, the solution for x is incorrect (should be [1.5, 2.0, 1.5]) and to confirm I then multiplied A and x back together and don’t get [1, 1, 1]. I don’t quite understand what I am doing wrong.

Additionally, I’ve looked at the factor and the values of the matrix elements don’t make sense either.

Output

Hessian:
   2.000   -1.000    0.000 
  -1.000    2.000   -1.000 
   0.000   -1.000    2.000 
Solution:
   2.500    0.000    0.000 
   3.500    0.000    0.000 
   2.500    0.000    0.000 
B vector:
   1.500    0.000    0.000 
   2.000    0.000    0.000 
   1.500    0.000    0.000 

Code

iterate_hessian() is an external function that returns doubles which are read into the CHOLMOD hessian matrix.

The entry point for the code is cholesky_determinant which is called with an argument which gives the dimension of the (square) matrix.

#include <cholmod.h>
#include <string.h>

// Function prototype that gives the next value of the Hessian
double iterate_hessian();

cholmod_sparse *cholmod_hessian(double *hessian, size_t dimension, cholmod_common *common) {
  // This function assigns the Hessian matrix from OPTIM to a dense matrix for CHOLMOD to use.
  // Allocate a dense cholmod matrix of appropriate size
  cholmod_triplet *triplet_hessian;
  triplet_hessian = cholmod_allocate_triplet(dimension, dimension, dimension*dimension, 0, CHOLMOD_REAL, common);
  // Loop through values of hessian and assign their row/column index and values to triplet_hessian.
  size_t loop;
  for (loop = 0; loop < (dimension * dimension); loop++) {
    if (hessian[loop] == 0) {
      continue;
    }
    ((int*)triplet_hessian->i)[triplet_hessian->nnz] = loop / dimension;
    ((int*)triplet_hessian->j)[triplet_hessian->nnz] = loop % dimension;
    ((double*)triplet_hessian->x)[triplet_hessian->nnz] = hessian[loop];
    triplet_hessian->nnz++;
  }
  // Convert the triplet to a sparse matrix and return.
  cholmod_sparse *sparse_hessian;
  sparse_hessian = cholmod_triplet_to_sparse(triplet_hessian, (dimension * dimension), common);
  return sparse_hessian;
}

void print_matrix(cholmod_dense *matrix, size_t dimension) {
  // matrix->x is a void pointer, so first copy it to a double pointer
  // of an appropriate size
  double *y = malloc(sizeof(matrix->x));
  y = matrix->x;
  // Loop variables
  size_t i, j;
  // Row
  for(i = 0; i < dimension; i++) {
  // Column
    for(j = 0; j < dimension; j++) {
      printf("% 8.3f ", y[i + j * dimension]);
    }
    printf("\n");
  }
}

cholmod_dense *factorized(cholmod_sparse *sparse_hessian, cholmod_common *common) {
  cholmod_factor *factor;
  factor = cholmod_analyze(sparse_hessian, common);
  cholmod_factorize(sparse_hessian, factor, common);
  cholmod_dense *b, *x;
  b = cholmod_ones(sparse_hessian->nrow, 1, sparse_hessian->xtype, common);
  x = cholmod_solve(CHOLMOD_LDLt, factor, b, common);
  cholmod_free_factor(&factor, common);
  // Return the solution, x
  return x;
}   

double cholesky_determinant(int *dimension) {
  // Declare variables
  double determinant;
  cholmod_sparse *A;
  cholmod_dense *B, *Y;
  cholmod_common common;
  // Start CHOLMOD
  cholmod_start (&common);
  // Allocate storage for the hessian (we want to copy it)
  double *hessian = malloc(*dimension * *dimension * sizeof(hessian)); 
  // Get the hessian from OPTIM
  int i = 0;
  for (i = 0; i < (*dimension * *dimension); i++) {
    hessian[i] = iterate_hessian();
  }
  A = cholmod_hessian(hessian, *dimension, &common);
  printf("Hessian:\n");
  print_matrix(cholmod_sparse_to_dense(A, &common), *dimension);
  B = factorized(A, &common);
  printf("Solution:\n");
  print_matrix(B, *dimension);
  double alpha[] = {1, 0};
  double beta[] = {0, 0};
  Y = cholmod_allocate_dense(*dimension, 1, *dimension, CHOLMOD_REAL, &common);
  cholmod_sdmult(A, 0, alpha, beta, B, Y, &common);
  printf("B vector:\n");
  print_matrix(Y, *dimension);
  determinant = 0.0;
  // Free up memory and finish CHOLMOD
  cholmod_free_sparse (&A, &common);
  cholmod_free_dense (&B, &common);
  cholmod_finish (&common);           
  return determinant;
}
  • 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-13T11:42:57+00:00Added an answer on June 13, 2026 at 11:42 am

    It turns out that I hadn’t set the stype for my sparse matrix properly. The stype determines the symmetry (and thus the subsequent behaviour of calls to cholmod_factorize). It was in fact factorising and solving for AA’.

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

Sidebar

Related Questions

I have been using PHP's simple XML function to work with an XML file.
I have been using TortoiseSVN for some time and I really like it. I
I have been using Stanford POS Tagger to tag parts of speech in a
I have been using play 1.2.5rc4 for development of one app and I have
I have been using an API to do some work. This is how I
I have been using some Javascript to create a text field once an certain
I have been using the storyboard to make an application and currently there are
I have been using CI just fine using the MySQL driver. I want to
I have been using Matlab 2011b and contourf/contourfm to plot 2D data on a
I have been using the following sort: var query = _cityRepository.GetAll( .OrderBy(item => item.RowKey.Substring(0,

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.