# Multiply two TVectorD

**URL:** <https://root-forum.cern.ch/t/multiply-two-tvectord/52004>\
**Category:** Newbie\
**Created:** [October 17, 2022, 2:49pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004 "2022-10-17T14:49:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [October 17, 2022, 2:49pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/1 "2022-10-17T14:49:41Z")

</div>

Hello all,

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

```auto
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

```auto
(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.

```auto
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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 17, 2022, 5:29pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/2 "2022-10-17T17:29:16Z")

</div>

In this case, the [multiplication of two vectors](https://en.wikipedia.org/wiki/Multiplication_of_vectors) is a “dot product” (“scalar product”), which returns a scalar quantity (i.e., a single value).

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [October 17, 2022, 5:43pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/3 "2022-10-17T17:43:09Z")

</div>

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

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

```

Any idea would be appreciated.

Thank You  
Dil

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 17, 2022, 5:47pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/4 "2022-10-17T17:47:12Z")

</div>

[The ROOT Mathematical Libraries → Matrix Linear Algebra](https://root.cern/doc/master/group__Matrix.html)

[TMatrixTAutoloadOps::Dot](https://root.cern/doc/master/namespaceTMatrixTAutoloadOps.html#ab923558d7badffa52715c2174708c91f)

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [October 17, 2022, 5:57pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/5 "2022-10-17T17:57:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 17, 2022, 6:00pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/6 "2022-10-17T18:00:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [October 17, 2022, 6:07pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/7 "2022-10-17T18:07:35Z")

</div>

I got following.

```auto
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)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 17, 2022, 6:19pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/8 "2022-10-17T18:19:06Z")

</div>

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

See also: [`operator=()`](https://root.cern/doc/master/classTVectorT.html#ae70eef27a70876b8221743bc1d56db5e)

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [October 17, 2022, 6:27pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/9 "2022-10-17T18:27:24Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 17, 2022, 6:39pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/10 "2022-10-17T18:39:30Z")

</div>

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

See also: [ElementMult](https://root.cern/doc/master/namespaceTMatrixTAutoloadOps.html#a7b5685da3b8dae84191ccaf3da3ab2aa)

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [October 17, 2022, 6:49pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/11 "2022-10-17T18:49:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 17, 2022, 6:50pm UTC](https://root-forum.cern.ch/t/multiply-two-tvectord/52004/12 "2022-10-17T18:50:55Z")

</div>

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