Logo ROOT   6.10/09
Reference Guide
TestMatrixArithmetic.h
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$ // Author: Simon Pfreundschuh 20/07/16
2 
3 /*************************************************************************
4  * Copyright (C) 2016, Simon Pfreundschuh *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 ///////////////////////////////////////////////////////////////////
12 // Test arithmetic functions defined on matrices and compare the //
13 // results to the reference implementation. //
14 ///////////////////////////////////////////////////////////////////
15 
16 #include "TMatrix.h"
17 #include "Utility.h"
19 
20 /** Test multiplication (standard, transposed, hadamard) operation on
21  * architecture specific matrix types and compare with results
22  * obtained with TMatrixT.
23  */
24 //______________________________________________________________________________
25 template<typename Architecture_t>
26 auto testMultiplication(size_t ntests)
27  -> typename Architecture_t::Scalar_t
28 {
29 
30  using Scalar_t = typename Architecture_t::Scalar_t;
31  using Matrix_t = typename Architecture_t::Matrix_t;
32 
33  Scalar_t maximumError = 0.0;
34 
35  for (size_t t = 0; t < ntests; t++) {
36  size_t m, n, k;
37  m = rand() % 100 + 1;
38  n = rand() % 100 + 1;
39  k = rand() % 100 + 1;
40 
41  TMatrixT<Scalar_t> ARef(m,k), A2Ref(m,k), ATRef(k,m) , BRef(k,n), BTRef(n,k), CRef(m,n);
47  Matrix_t A(ARef), A2(A2Ref), AT(ATRef), B(BRef), BT(BTRef), C(CRef);
48 
49  // A * B
50  CRef.Mult(ARef,BRef);
51  Architecture_t::Multiply(C, A, B);
52  Scalar_t error = TMVA::DNN::maximumRelativeError(C, CRef);
53  maximumError = std::max(error, maximumError);
54 
55  // A^T * B
56  CRef.TMult(ATRef,BRef);
57  Architecture_t::TransposeMultiply(C, AT, B);
58  error = TMVA::DNN::maximumRelativeError(C, CRef);
59  maximumError = std::max(error, maximumError);
60 
61  // A * B^T
62  CRef.MultT(ARef,BTRef);
63  Architecture_t::MultiplyTranspose(C, A, BT);
64  error = TMVA::DNN::maximumRelativeError(C, CRef);
65  maximumError = std::max(error, maximumError);
66 
67  // A .* B
68  for (size_t i = 0; i < (size_t) ARef.GetNrows(); i++) {
69  for (size_t j = 0; j < (size_t) ARef.GetNcols(); j++) {
70  ARef(i,j) *= A2Ref(i,j);
71  }
72  }
73  Architecture_t::Hadamard(A, A2);
74  error = TMVA::DNN::maximumRelativeError(A, ARef);
75  maximumError = std::max(error, maximumError);
76  }
77 
78  return maximumError;
79 }
80 
81 /** Test the summing over columns by summing by the sums obtained
82  * from a matrix filled with column indices as elements.
83  */
84 //______________________________________________________________________________
85 template<typename Architecture_t>
86 auto testSumColumns(size_t ntests)
87  -> typename Architecture_t::Scalar_t
88 {
89 
90  using Scalar_t = typename Architecture_t::Scalar_t;
91  using Matrix_t = typename Architecture_t::Matrix_t;
92 
93  Scalar_t maximumError = 0.0;
94  for (size_t t = 0; t < ntests; t++) {
95 
96  Scalar_t error;
97 
98  size_t m, n;
99  m = rand() % 100 + 1;
100  n = rand() % 100 + 1;
101 
102  TMatrixT<Double_t> ARef(m,n), BRef(n,1);
103 
104  for (size_t i = 0; i < (size_t) ARef.GetNrows(); i++) {
105  for (size_t j = 0; j < (size_t) ARef.GetNcols(); j++) {
106  ARef(i,j) = j;
107  if (i == 0) BRef(j, 0) = m * j;
108  }
109  }
110 
111  Matrix_t A(ARef), B(n, 1);
112  Architecture_t::SumColumns(B, A);
113 
115  maximumError = std::max(error, maximumError);
116  }
117  return maximumError;
118 }
auto maximumRelativeError(const Matrix1 &X, const Matrix2 &Y) -> decltype(X(0, 0))
Compute the maximum, element-wise relative error of the matrices X and Y normalized by the element of...
Definition: Utility.h:200
static double B[]
void randomMatrix(AMatrix &X)
Fill matrix with random, Gaussian-distributed values.
Definition: Utility.h:60
auto testSumColumns(size_t ntests) -> typename Architecture_t::Scalar_t
Test the summing over columns by summing by the sums obtained from a matrix filled with column indice...
auto testMultiplication(size_t ntests) -> typename Architecture_t::Scalar_t
Test multiplication (standard, transposed, hadamard) operation on architecture specific matrix types ...
static double A[]
TMatrixT.
Definition: TMatrixDfwd.h:22
void MultT(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix multiplication. Create a matrix C such that C = A * B^T.
Definition: TMatrixT.cxx:951
void TMult(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
Create a matrix C such that C = A&#39; * B.
Definition: TMatrixT.cxx:852
static double C[]
TMarker * m
Definition: textangle.C:8
void Mult(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix multiplication. Create a matrix C such that C = A * B.
Definition: TMatrixT.cxx:648
const Int_t n
Definition: legend1.C:16