Logo ROOT   6.07/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<Double_t> ARef(m,k), A2Ref(m,k), ATRef(k,m) , BRef(k,n),
42  BTRef(n,k), CRef(m,n);
48  Matrix_t A(ARef), A2(A2Ref), AT(ATRef), B(BRef), BT(BTRef), C(CRef);
49 
50  // A * B
51  CRef.Mult(ARef,BRef);
52  Architecture_t::Multiply(C, A, B);
53  Scalar_t error = TMVA::DNN::maximumRelativeError((TMatrixT<Double_t>) C, CRef);
54  maximumError = std::max(error, maximumError);
55 
56  // A^T * B
57  CRef.TMult(ATRef,BRef);
58  Architecture_t::TransposeMultiply(C, AT, B);
60  maximumError = std::max(error, maximumError);
61 
62  // A * B^T
63  CRef.MultT(ARef,BTRef);
64  Architecture_t::MultiplyTranspose(C, A, BT);
66  maximumError = std::max(error, maximumError);
67 
68  // A .* B
69  for (size_t i = 0; i < (size_t) ARef.GetNrows(); i++) {
70  for (size_t j = 0; j < (size_t) ARef.GetNcols(); j++) {
71  ARef(i,j) *= A2Ref(i,j);
72  }
73  }
74  Architecture_t::Hadamard(A, A2);
76  maximumError = std::max(error, maximumError);
77  }
78 
79  return maximumError;
80 }
81 
82 /** Test the summing over columns by summing by the sums obtained
83  * from a matrix filled with column indices as elements.
84  */
85 //______________________________________________________________________________
86 template<typename Architecture_t>
87 auto testSumColumns(size_t ntests)
88  -> typename Architecture_t::Scalar_t
89 {
90 
91  using Scalar_t = typename Architecture_t::Scalar_t;
92  using Matrix_t = typename Architecture_t::Matrix_t;
93 
94  Scalar_t maximumError = 0.0;
95  for (size_t t = 0; t < ntests; t++) {
96 
97  Scalar_t error;
98 
99  size_t m, n;
100  m = rand() % 100 + 1;
101  n = rand() % 100 + 1;
102 
103  TMatrixT<Double_t> ARef(m,n), BRef(n,1);
104 
105  for (size_t i = 0; i < (size_t) ARef.GetNrows(); i++) {
106  for (size_t j = 0; j < (size_t) ARef.GetNcols(); j++) {
107  ARef(i,j) = j;
108  if (i == 0) BRef(j, 0) = m * j;
109  }
110  }
111 
112  Matrix_t A(ARef), B(n, 1);
113  Architecture_t::SumColumns(B, A);
114 
116  maximumError = std::max(error, maximumError);
117  }
118  return maximumError;
119 }
static double B[]
void randomMatrix(AMatrix &X)
Fill matrix with random, Gaussian-distributed values.
Definition: Utility.h:59
auto maximumRelativeError(const AMatrix &X, const AMatrix &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:213
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[]
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