Multiply two TVectorD

Hello all,

I tried to multiply two TVectorD, that’s not working correctly. But Multiplying TMartixD *TvectorD is properly working.

void matrix_multiplication(){
    
    const int nx=3,nQ2=4,ntmin_t=10,n_weight=6;
   
    ofstream myfile1;
    
    myfile1.open("Result.txt");
    TFile *hFile1=TFile::Open("Inverse_matrix_Gen_1011.root");
    hFile1->ls();
    TFile *hFile2=TFile::Open("Data_vec.root");
    hFile2->ls();
    
    TFile *AnaFile = new TFile("Store_Ai.root", "RECREATE", "Test");
    char MatrixName_1[64];
    char MatrixName_2[64];
    
    TVectorD Data_array;
    TMatrixD MatrixInvGEN;
    TVectorD Extracted_coefficient;
    TVectorD Model_Matrix;
    
    
    
    for(int ixB=0; ixB<nx; ixB++){
            for(int iQ2=0; iQ2<nQ2; iQ2++){
            for(int itmin_t=0 ; itmin_t < ntmin_t; itmin_t++){
                sprintf(MatrixName_1,"MatrixInvGEN_xB%03d_iQ2%03d_tmin%03d",ixB,iQ2,itmin_t);
                sprintf(MatrixName_2,"Data_array_vec_xB%03d_iQ2%03d_tmin%03d",ixB,iQ2,itmin_t);
                (MatrixInvGEN).ResizeTo(6, 6);
                MatrixInvGEN = *((TMatrixD*)(hFile1->Get(MatrixName_1)));
                cout << "Matrix_InvGEN"<< endl;
                MatrixInvGEN.Print();
                                                                
               
               (Data_array).ResizeTo(6);
               Data_array  = *((TVectorD*)(hFile2->Get(MatrixName_2)));
                cout << "Data_array" << endl;
                Data_array.Print();
                
                (Extracted_coefficient).ResizeTo(6);
                Extracted_coefficient=MatrixInvGEN*Data_array;
                
                Extracted_coefficient.Print();
                
             Extracted_coefficient.Write(Form("Extracted_coefficient_xB%03d_iQ2%03d_tmin%03d",ixB,iQ2,itmin_t));
                
                (Model_Matrix).ResizeTo(6);
                Model_Matrix = Extracted_coefficient * Data_array;
                cout << "Model_Matrix" << endl;
                Model_Matrix.Print();
                
                
             
           
            }
            }
    }
    
    myfile1.close();
    AnaFile->Write();
    AnaFile->Close();
}


The problem is

(Model_Matrix).ResizeTo(6);
 Model_Matrix = Extracted_coefficient * Data_array;
 cout << "Model_Matrix" << endl;
Model_Matrix.Print();

My model matrix gives same number.

Model_Matrix

Vector (6) is as follows

 |        1  |

0 |0.71648
1 |0.71648
2 |0.71648
3 |0.71648
4 |0.71648
5 |0.71648

I tried to use transpose of one vector, But I cannot use that way.

error: no member named 'T' in 'TVectorT<double>'
                Model_Matrix = (Extracted_coefficient.T()) * Data_array;

Unfortunately, I can’t upload my .root files. Could someone please help me to figure out this?
Thanks
Dil

In this case, the multiplication of two vectors is a “dot product” (“scalar product”), which returns a scalar quantity (i.e., a single value).

Thank You @Wile_E_Coyote. This is a good catch.
But I am having some trouble to change my code according to take the Dot product.
I tried

 error: no member named 'Dot' in 'TVectorT<double>'
                Model_Matrix = (Extracted_coefficient.Dot(Data_array));
                                ~~~~~~~~~~~~~~~~~~~~~ ^

Any idea would be appreciated.

Thank You
Dil

The ROOT Mathematical Libraries → Matrix Linear Algebra

TMatrixTAutoloadOps::Dot

Thanks again @Wile_E_Coyote .But I am not sure that link helpful for above calculation/or I don’t understand which operation should I use.

Assuming “v1” and “v2” are vectors: “v1 * v2” or “Dot(v1, v2)

I got following.

TVectorD Data_array;
TMatrixD MatrixInvGEN;
TVectorD Extracted_coefficient;
TVectorD Model_Matrix;
    
Model_Matrix.ResizeTo(6);
Model_Matrix =Dot(Extracted_coefficient,Data_array);
cout << "Model_Matrix=" <<endl;
Model_Matrix.Print();

Model_Matrix

Vector (6) is as follows

 |        1  |

0 |0.71648
1 |0.71648
2 |0.71648
3 |0.71648
4 |0.71648
5 |0.71648

This is similar to my first post. This is what I need to figure out. v1*v2 (first post)and Dot(v1,v2) gave the same results.(It should be)

TVectorD v(6); v = 123456.; v.Print();

See also: operator=()

Now I am pretty sure something I did operator incorrectly.But don’t know how to fix this.
I need to multiply each elements in my two vectors.

[1,2,3] * [1,3,5]== [1,6,15]

Assuming “v1” and “v2” are vectors: “ElementMult(v1, v2)" (warning: the result is stored in “v1”)

See also: ElementMult

Thank you.This works.
I did as follows.
Model_matrix= ElementMult(v1,v2).

You’d better use: Model_matrix = v1; ElementMult(Model_matrix, v2);