# Symmetric Matrix

Hello i have a problem on how to compute a symmetric of 580x580 i have the matrix in a txt file but i don’t how to compute it ??

Hi,

can you elaborate about what you exactly mean with “calculate a symmetric”?

D

thank you very much for your replay, i don’t want to calculate the symmetric matrix in fact i want to perform a chi square minimization with chi^2=V^TC^-1V this the general formula for chi square now i have a covariance matrix (580x580) wich is not diagonal and it’s symmetric of course so how to create it with ROOT::Math::MatRepSym and how to fill it ?? thank you very much

Hi,

I think the best option, given the size of the matrix and the fact that SMatrix is templated on the matrix size, is to use TMatrixDSym (a typedef to the double version of root.cern/doc/master/classTMatrixTSym.html)

Cheers,
Danilo

thank you very much for your replay i dont’t know how to fill it using the txt file ?? that’s my problem thank again

AMINE

I guess you have to parse the file and fill the matrix with the operator (double, double).

``````// Loop on the values read from the txt
mymatrix(i,j)=mymatrix(j,i)=ijval;
...``````

there is no direct way to construct such matrix from a file.

Cheers,
D

thank your for your replay but with this way i should define 580 varaiables to be able to read from txt file ???
it’s right what i’m saying ??

AMINE

Hi Amine,

that is the way in which you specify the individual elements of the matrix, yes. The parsing of the file you have is custom to the format in which the coefficients are stored and can be tackled with standard C++ techniques.

Cheers,
D

Thank you very much for your help, i tried to define 580 variables and then read them:
double var;
file>>var>>var>>var>>…>>var;
but i have this error :
Error: Line too long in G__exec_statement when processing varible.C:61:
*** Interpreter error recovered ***
Any help will be appreciated thank you again
AMINE

Hi,

is this ROOT5?
If yes, I propose to try out on ROOT6. Alternatively, try to chop the line into smaller pieces or use aclic to compile your reading macro.

D

thank you very much in ROOT6 it work i stil have just one problem before filling the matrix, when i put the matrix in Notepad i see that the matrix is arranged for example i have a diagonal matrix when i put it in the Notepad it still diagonal but when i copy it in root the shape change and it’s not any more diagonal (even not a matrix) should i specify a specific extention other than (txt) ?
excuse me for my lot question an thank you very much for your help dpiparo

Hi,

what is the code and the input txt file? Can you post those on this thread? I think I do not get fully the question.

D

This is the covariance matrix 580x580 if you put it in notpad++ you can take a look at
supernova.lbl.gov/union/figures/ … at_sys.txt
but when i edit a txt file with gedit i don’t have 580x580 as notpad++
for the code i did’nt yet create the matrix and filled it so you can not run it
thank very dpiparo for your interst and help
variable.C (8.23 KB)

```{ int N = 580; TMatrixD mat(N, N); std::ifstream file("SCPUnion2.1_covmat_sys.txt"); // use ... mat(i, j) ... or ... mat[i][j] for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) file >> mat(i, j); file.close(); }```

Hi,

sorry I think I do not get the point. Could you iterate on the coefficients in the input file and fill the matrix, if not, why?
Here an example in Python (pretty handy when dealing with strings, the same can be done in C++):

``````import ROOT
import array

with open("SCPUnion2.1_covmat_sys.txt","r") as f:
elementsAsString = cont.split("\t")
# remove last element, which is "\n\n"
elementsAsString = elementsAsString[:-1]
elements = [float(el) for el in elementsAsString]
arr = array.array('d',elements)
m=ROOT.TMatrixDSym(580, arr)
m.Print()``````

Cheers,
D

… or like Pepe posted while I was typing hello thank you for your help i wanted to use TMatrixDSym but i’m unable to use transform the code from pyhton to c++ so i used TMatrixD mat(N, N) it’s fine now but my goal is to calculate the chi^2=V^TC^-1V that the general expression when the covariance matrix is not diagonal this forme chi^2=(f_th(xi)-f_ex(xi))^2/sigm^2 is
for a diagonal matrix, a defined and filled the vector v (SVector ) but when i i want to multiplay V^TC^-1V using the method Similarity(V,mat.Invert()) i had this error :
/home/ubuntu/Desktop/root-6.06.04/include/Math/MatrixFunctions.h:758:42: note:
candidate template ignored: could not match ‘Expr’ against 'SVector’
inline SMatrix<T,D1,D1,MatRepSym<T,D1> > Similarity(const Expr<A,T,D1,D2…
so should i use the symetric matrix or ???