Simplest way to get a matrix from a vector multiplication

Wondering what is the simplest way to multiply a vector by its transpose and get its characteristic matrix in ROOT.

Such as it could be TVectorD V;

TMatrixD M = V * V.T();

where V.T() would be the transpose, V would be a 3 component vector, and the product M would result in a 3x3 matrix.

I had a look here,

https://root.cern/doc/master/group__tutorial__matrix.html

but not clear to me what would be the simplest way. Do I need to work only with TMatrixD?

Int_t test() {
    Double_t d[] = {1.0, 2.0, 3.0};

    TVectorD a;
    a.Use(3, d);

    TMatrixD D(3, 3);
    TMatrixDColumn(D, 0) = a;

    TMatrixD Dt(3, 3);
    TMatrixDRow(Dt, 0) = a;

    D.Print();

    Dt.Print();

    TMatrixD P = D * Dt;

    P.Print();

    return 0;
}

There is any optimal way to interface with a TVector3?

Hi,
You can use TMatrixT::Rank1Update

Int_t test() {
    Double_t d[] = {1.0, 2.0, 3.0};
    TVectorD a(3,d);
   
    TMatrixD D(3, 3);
    D.Rank1Update(a,a);  
    D.Print();
    return 0;
}

Yes, thats perfect, thank you! There is just a minor typo on the third line.

It should be:

  TVectorD a(3,d);

And do you know a way to initialise Double_t d[] from a TVector3 directly?

Or a TVectorD from a TVector3 directly.

Thank you ! I have updated the code in the post above.

I think the simples way to do this (v3 is a TVector3 instance)

double d[3];
v3.GetXYZ(d);
TVectorD v(3,d);
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.