# TMatrix left-handside multiplication

**URL:** <https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594>\
**Category:** ROOT\
**Created:** [July 8, 2023, 3:41am UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594 "2023-07-08T03:41:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [July 8, 2023, 3:41am UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594/1 "2023-07-08T03:41:36Z")

</div>

Hello,

I am playing with complex matrices and vectors and I couldn’t find that much documentation about matrices-vector operations. I would like to make the operation: B = (V\*)A(V), where A,B are TMatrix type and V is TVectorT vector.

I first define TMatrix/TVector conjugate as following

```auto
TVectorC Conjugate(const TVectorC &v)
{
    TVectorC v_c(v.GetNoElements());

    for(int i = 0, N = v.GetNoElements(); i < N; i++) {
        v_c(i) = TComplex::Conjugate(v(i));
    }

    return v_c;
}

TMatrixC Conjugate(const TMatrixC &m)
{
    TMatrixC m_c(m.GetNrows(), m.GetNcols());

    for(int i = 0, I = m.GetNrows(); i < I; i++) {
        for(int j = 0, J = m.GetNcols(); j < J; j++) {

            m_c(i,j) = TComplex::Conjugate(m(i,j));
        }
    }

    return m_c;
}

```

And then after that I was hoping to make the simple operation:

```auto
{
      using TMatrixC = TMatrixT<TComplex>;
      using TVectorC = TVectorT<TComplex>;
      TMatrixC A = /*[...]*/;
      TVectorC V = /*[...]*/;

      TMatrixC B = Conjugate(V)*A*V;
}

```

This throw me an error because of the left handside multiplication.

```auto
error: invalid operands to binary expression ('TVectorC' (aka 'TVectorT<TComplex>') and 'TMatrixC' (aka 'TMatrixT<TComplex>'))
    return TMath::Sqrt(Conjugate(this->Z) * this->P * this->Z);
                       ~~~~~~~~~~~~~~~~~~^~~~~~~~

```

This seems obvious… How should I modify my code here ?

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [July 8, 2023, 5:41am UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594/2 "2023-07-08T05:41:05Z")

</div>

This is not what I want to do, because my vectors are differents.  
But for the records… this might be useful for someone else. Naming doesn’t sound obvious.

```auto
TMatrixT<Element>
[...]
Element Similarity (const TVectorT< Element > &v) const
 	Calculate scalar v * (*this) * v^T.
[..]

```

NB: It seems complex algebra might not be supported yet. [Fail to compile TMatrixT\<TComplex\>](https://root-forum.cern.ch/t/fail-to-compile-tmatrixt-tcomplex/48232) @moneta Were there some progress in this regards ?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 10, 2023, 10:54am UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594/3 "2023-07-10T10:54:26Z")

</div>

Hi,

We are not supporting TMatrix or TMatruxstd::complex. You probably need to use another matrix package for performing operations on complex matrices

Lorenzo

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [July 11, 2023, 12:38am UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594/4 "2023-07-11T00:38:56Z")

</div>

Any plan for implementing that ? Or some arguments against it maybe ?

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [July 11, 2023, 9:20am UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594/5 "2023-07-11T09:20:12Z")

</div>

If you search for “matrix complex” you will see that there has been some interest for complex matrix algebra over the last 2 decades, but … implementing this feature in all elements of the linear package is a big task:

installing it

- in all the decomposition classes
- in the eigen value classes
- sparse methods
- ROOT dictionary etc.

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [July 11, 2023, 9:44am UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594/6 "2023-07-11T09:44:23Z")

</div>

your algebra syntax is not correct ! An expression is always interpreted from left to right unless parentheses are placed. V \* A \* V will result first in calculating V \* A.

We could have implemented to interpret this as V^T \* A . Just to keep the syntax  
clean and simple that is left out and the preferred way is, V \* (A \* V) which  
will result in a dot product of two vectors.

As far as the naming of “Similarity” goes. The code contains also the well-known matrix similarity operations and this one is with a vector.

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [July 11, 2023, 1:03pm UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594/7 "2023-07-11T13:03:22Z")

</div>

Pretty sure it doesn’t takes two decades to implement complex matrices… but I definitely need a robust/mature solution.

I definitely didn’t found any vector transpose function, so I assumed that multiplying a vector-matrix from the left was implicit. Additionally, I only know matrix similarities not vector-matrix.  
Many thanks for the clarifications !

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 25, 2023, 1:03pm UTC](https://root-forum.cern.ch/t/tmatrix-left-handside-multiplication/55594/8 "2023-07-25T13:03:27Z")

</div>

Closed on July 25, 2023, 1:03pm UTC

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