[Insight-developers] NIfTI, DiffusionTensor3D Pixels, SymmetricSecondRankTensor

kent williams norman-k-williams at uiowa.edu
Thu Mar 12 11:25:38 EDT 2009


Before I read Tom V's suggestion, I'd implemented this incredibly stupid yet
straightforward way to change one ordering to another.  I'd go with his but
looking at that formula and trying to interpret it via "My Dear Aunt Sally"
was making my ears itch.

My version works by creating a symmetric matrix, filling each location with
it's order in which the triangular half-matrix gets unrolled. Then I create
the ordering by walking through it in the way the opposite triangular is
ordered.

This is not an efficient way to deal with it, but I desk-checked it and
wrote a little test program to verify the results.  Similarly, to get the
Matrix Size from the length of the unrolled triangular half-matrix, I
iterate and count dimensions.

Both of these operations obviously have direct algebraic solutions but that
isn't how my brain works ;-)

And I came up with my own inelegant algebraic solution for Matrix Size:

// 2 * numComponents = MatDim * (MatDim + 1)
int MatDim = (int)sqrt(2.0 * (double)numComponents);

But that was so stupid looking I went with my iterative approach.



Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged.  If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited.  Please reply to the sender that you have received the message in error, then delete it.  Thank you.
------------------------------------------
// returns an ordering array for converting upper
// triangular symmetric matrix to lower triangular symmetric
// matrix
int *
UpperToLowerOrder(int dim)
{
  int **mat = new int *[dim];
  for(unsigned i = 0; i < dim; i++)
    {
    mat[i] = new int[dim];
    }
  // fill in
  int index(0);
  for(unsigned i = 0; i < dim; i++)
    {
    for(unsigned j = i; j < dim; j++)
      {
      mat[i][j] = index;
      mat[j][i] = index;
      index++;
      }
    }
  int *rval = new int[index+1];
  int index2(0);
  for(unsigned i = 0; i < dim; i++)
    {
    for(unsigned j = 0; j <= i; j++,index2++)
      {
      rval[index2] = mat[i][j];
      }
    }
  rval[index2] = -1;
  for(unsigned i = 0; i < dim; i++)
    {
    delete [] mat[i];
    }
  delete [] mat;
  return rval;
}
// returns an ordering array for converting lower triangular
// symmetric matrix to upper triangular symmetric matrix
int *
LowerToUpperOrder(int dim)
{
  int **mat = new int *[dim];
  for(unsigned i = 0; i < dim; i++)
    {
    mat[i] = new int[dim];
    }
  // fill in
  int index(0);
  for(unsigned i = 0; i < dim; i++)
    {
    for(unsigned j = 0; j <= i; j++,index++)
      {
      mat[i][j] = index;
      mat[j][i] = index;
      }
    }
  int *rval = new int[index+1];
  int index2(0);;
  for(unsigned i = 0; i < dim; i++)
    {
    for(unsigned j = i; j < dim; j++,index2++)
      {
      rval[index2] = mat[i][j];
      }
    }
  rval[index2] = -1;
  for(unsigned i = 0; i < dim; i++)
    {
    delete [] mat[i];
    }
  delete [] mat;
  return rval;
}
// compute the rank of the symmetric matrix from
// the count of the triangular matrix elements
int SymMatDim(int count)
{
  int dim = 0;
  int row = 1;
  while(count > 0)
    {
    count -= row;
    dim++;
    row++;
    }
  return dim;
}




More information about the Insight-developers mailing list