Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Extract row/column of a matrix and subvector form a vector.

Anas_A_
Beginner
1,715 Views

Hi! I would like to ask if there are routines for the following issues:

1) Extract a row/column of a matrix.

2) If theta is a vector extract a subvector. For example extract the elements 4 until 8 of  theta.

3) Merge 2 vectors. For example if a, b be row vectors I want to create a new vector c with the first row be the a vector and the second row will be the b vector.

I know that the above can be done using "for", but I' m interested in using routines (if there are).

 

Thank you in advance.

0 Kudos
1 Solution
Ying_H_Intel
Employee
1,715 Views

Hi Anas,

Sorry, it seems i may mistake your quesiton as the one http://software.intel.com/en-us/forums/topic/506612.

But the basic idea is same

As the function provided by MKL are mainly based on some kind of standard interface, like BLAS ( matrix, vector operation). It may not flexiable like Matlab and R.  But it can accept C array and these operations you mentioned are embeded in the computation.

for example, in your case,

 // Note: A can be accessed by pointer with regular pattern by incx, tranpose.etc.

#include <stdio.h>
#include <stdlib.h>
#include <mkl.h>

int main ()
{
    double a[2][2] = {{1, 2}, {3, 4}};
    double b[2] = {11, 12};
    double res;

    res =cblas_ddot(2, &a[0][0] , 2, b, 1); // Compute a vector-vector dot product of the first column of the matrix and the vector b. The first column is the vector{1,3}
 printf(" %0.2f\n", res);
  
 //Compute a vector-vector dot product of the first column of the matrix and the vector b. The second column is the vector{2,4}
 res =cblas_ddot(2, &a[0][1] , 2, b, 1);
     printf(" %0.2f\n", res);

  cblas_dgemv(CblasRowMajor, CblasTrans, 2, 2, 1.0, &a[0][0], 2, b, 1, 0.0, y, 1);
  printf(" %0.2f\n", y[0], y[1]);

  cblas_dgemv(CblasColMajor, CblasNoTrans, 2, 2, 1.0, &a[0][0], 2, b, 1, 0.0, y, 1);
  printf(" %0.2f\n", y[0], y[1]);

return 0;

}

Best Regrads,

Ying
 

  return 0;
}

View solution in original post

0 Kudos
5 Replies
Ying_H_Intel
Employee
1,715 Views

Hi Anas,

It seems Array Notation be a suitable tool for these operations.  Please See  http://software.intel.com/en-us/articles/about-intel-cilk-plus-and-how-to-get-started.

A[:] - refers the entire array
A[10:5] - refers A[10], A[11], A[12], A[13], A[14]
A[1:4:2] - refers A[1], A[3], A[5], A[7]
multiple dimensions: A[<lower bound>:<length>:<stride>][<lower bound>:<length>:<stride>]
A[:][:]- refers the entire 2 dimensional array

You can write code  like  ColumN= A[:],   SubVector=theta[4:4 ],  in your program.

It support C /Fortran lanaguage. and if you are using Intel C/Fortran Composer XE.

Best Regards,

Ying

0 Kudos
Anas_A_
Beginner
1,715 Views

Thank you for your help. I tried to use the array notation with  the function ddot but it doesn' t work. Could you tell me please what is wrong?

int main ()
{
    double a[2][2] = {{1, 2}, {3, 4}};
    double b[2] = {11, 12};
    double res;
    res =cblas_ddot(2, a[:][1] , 1, b, 1); // Compute a vector-vector dot product of the first column of the matrix and the vector b. The first column is the vector{1,3}

 

printf(" %0.2f\n", res);
 

  return 0;
}

0 Kudos
Zhang_Z_Intel
Employee
1,715 Views

MKL has functions that allow you to extract/copy elements of a vector, indexed using different methods (increments, masks, or index arrays). Check out the "VML Pack/Unpack Functions" (http://software.intel.com/en-us/node/470564).

For matrices, please note that matrices passed to MKL computation routines are stored as 1-dimensional arrays in memory.

 

 

 

 

0 Kudos
Ying_H_Intel
Employee
1,716 Views

Hi Anas,

Sorry, it seems i may mistake your quesiton as the one http://software.intel.com/en-us/forums/topic/506612.

But the basic idea is same

As the function provided by MKL are mainly based on some kind of standard interface, like BLAS ( matrix, vector operation). It may not flexiable like Matlab and R.  But it can accept C array and these operations you mentioned are embeded in the computation.

for example, in your case,

 // Note: A can be accessed by pointer with regular pattern by incx, tranpose.etc.

#include <stdio.h>
#include <stdlib.h>
#include <mkl.h>

int main ()
{
    double a[2][2] = {{1, 2}, {3, 4}};
    double b[2] = {11, 12};
    double res;

    res =cblas_ddot(2, &a[0][0] , 2, b, 1); // Compute a vector-vector dot product of the first column of the matrix and the vector b. The first column is the vector{1,3}
 printf(" %0.2f\n", res);
  
 //Compute a vector-vector dot product of the first column of the matrix and the vector b. The second column is the vector{2,4}
 res =cblas_ddot(2, &a[0][1] , 2, b, 1);
     printf(" %0.2f\n", res);

  cblas_dgemv(CblasRowMajor, CblasTrans, 2, 2, 1.0, &a[0][0], 2, b, 1, 0.0, y, 1);
  printf(" %0.2f\n", y[0], y[1]);

  cblas_dgemv(CblasColMajor, CblasNoTrans, 2, 2, 1.0, &a[0][0], 2, b, 1, 0.0, y, 1);
  printf(" %0.2f\n", y[0], y[1]);

return 0;

}

Best Regrads,

Ying
 

  return 0;
}

0 Kudos
Anas_A_
Beginner
1,715 Views

Hi Ying! I appreciate your help, thank you very much.

0 Kudos
Reply