Tensor Product

Maged Elhajal Maged.Elhajal@ipt.unil.ch
Thu Feb 27 18:32:00 GMT 2003

Dear Gsl developpers, 

I have started using (and enjoying !) your library a couple of
days ago and found there was no function to perform tensor
products (sometimes called inner product ?) available, or maybe
I did not find it. I wrote mine and wondered if that would be
useful for you since it uses the gsl_matrix stuff...

I do not know what shape the contributions to your project
should have, but you can tell me more about that if you are
interested. Anyway, here is the (very short) code to perform a
tensor product:  

// m1 and m2 are the input matrices, 
// m is the output (tensor product of m1 and m2), 
// and the function tests that 
// m->size1 = m1->size1 * m2->size1
// m->size2 = m1->size2 * m2->size2

int pdtTens(gsl_matrix * m1, gsl_matrix * m2, gsl_matrix * m) {
  size_t i1, i2, i3, i4;
  if (m->size1 != m1->size1*m2->size1 || m->size2 !=
    GSL_ERROR ("Size of matrices do not fit together in
\"pdtTens\"", GSL_EBADLEN);
  else {
    for (i1 = 0; i1 < m1->size1; i1++)
      for (i2 = 0; i2 < m1->size2; i2++)
	for (i3 = 0; i3 < m2->size1; i3++)
	  for (i4 = 0; i4 < m2->size2; i4++)
	    m->data[(i1*m2->size1+i3)*m->tda + i2*m2->size2+i4] =
m1->data[i1*m1->tda+i2] * m2->data[i3*m2->tda+i4];
    return GSL_SUCCESS;

More information about the Gsl-discuss mailing list