vendredi 29 mai 2015

Tensor product of N matrices in c++ and Armadillo

I am new in c++ and I want to write a program to calculate Tensor product of N matrices ,a1,a2,a3,....,aN, made in Armadillo. Of course, Armadillo is able to calculate Tensor product but it is possible just for two matrices, kron(a1,a2), and if we want to calculate tensor product of all of them we have to write:

kron(kron(kron(a1,a2),a3),...) 

I want to introduce a function which does a same work. Of course, I wrote it in python as:

import numpy as np
import scipy.sparse as sp

def tens(*args):
    for i, j in enumerate(args):
        if i == 0:
            output = j
        else:
            output = sp.kron(out, j)
    return output

and it works but when I want to change the program in c++, I think it sould take the following shape:

#include <iostream>
#include <armadillo>

using namespace std;
using namespace arma;

cx_mat tens(cx_mat argc,...)
{
cx_mat output;
for( cx_mat x = 0; x < argc; x++ ) 

        if (x == 0)
            output = argc[0];
        else
            output = kron(ttt, argc[x]);

return output;
}

and to test it for,a1,a2,a3, we can add the main function to the end of the program:

int main()
{
cx_mat ii(2,2,fill::eye);
cx_mat ee = ii.col(0); // extract a column vector
cx_mat gg = ii.col(1);

tens(ee,gg,gg);
return 0;
}

I think the problem is in 'if' and 'else' comments. Can anybody guide me?

Aucun commentaire:

Enregistrer un commentaire