Linear algebra in python

Dear ROOT experts,

I am trying to migrate some simple linear-algebra ROOT code from c++ to python but it fails with on multiplication of TMatrixT and TVectorT. The code (based on rootfit output):

        eigen_vectors_inverted = decomposed_matrix.GetEigenVectors()
        eigen_vectors_inverted.Invert()
        diagonalized_values = eigen_vectors_inverted * ROOT.TVectorD(
            len(self.samples), array('d', [x.yield_var.getValV() for x in self.samples.values()]))

where nulllptr seems to be propagated from python to c++, leading to following error:

ValueError: none of the 2 overloaded methods succeeded. Full details:
  TVectorT<double> ::operator*<double>(const TMatrixT<double>& a, const TVectorT<double>& source) =>
    ValueError: nullptr result where temporary expected
  TVectorT<double> ::operator*<double>(const TMatrixT<double>& a, const TVectorT<double>& source) =>
    ValueError: nullptr result where temporary expected
> 

I checked the the two objects I am trying to multiply are properly defined so the error is probably in the binding?

ROOT Version: 6.28/10 (but also tried 6.26)
Platform: x86_64-centos7
Compiler: gcc11, python 3.9.12

Thanks for any help

Minimal working example to replicate:

import array, ROOT
a = ROOT.TVectorD(4, array.array('d', [1, 1, 1, 1]))
b = ROOT.TVectorD(4, array.array('d', [1, 1, 1, 1]))
a+b

Hi,

Thanks for posting and welcome to the ROOT community!
I acknowledge you stumbled on an issue. PyROOT is not able to find the templated operator+ for TVectorT.: I am sorry you experienced this inconvenience.

I can propose an easy workaround while we properly fix this, i.e. by explicitly instantiating the operator. You can achieve this with a simple line:

import array, ROOT

ROOT.gInterpreter.Declare("TVectorD operator+(const TVectorD &v1,const TVectorD  &v2){return TMatrixTAutoloadOps::operator+<double>(v1, v2);}")

a = ROOT.TVectorD(4, array.array('d', [1, 1, 1, 1]))
b = ROOT.TVectorD(4, array.array('d', [1, 1, 1, 1]))
c = a+b

for v in (a,b,c):
    v.Print()

Cheers,
D

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