CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mat_sum_sq.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_SPEED_MAT_SUM_SQ_HPP
2 # define CPPAD_SPEED_MAT_SUM_SQ_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 /*
15 $begin mat_sum_sq$$
16 $spell
17  sq
18  namespace
19  const
20  CppAD
21  sq
22  cppad
23  hpp
24 $$
25 
26 $section Sum Elements of a Matrix Times Itself$$
27 $mindex mat_sum_sq multiply speed test$$
28 
29 $head Syntax$$
30 $codei%# include <cppad/speed/mat_sum_sq.hpp>
31 %$$
32 $icode%mat_sum_sq(%n%, %x%, %y%, %z%)%$$
33 
34 $head Purpose$$
35 This routine is intended for use with the matrix multiply speed tests;
36 to be specific, it computes
37 $latex \[
38 \begin{array}{rcl}
39  y_{i,j} & = & \sum_{k=0}^{n-1} x_{i,k} x_{k,j}
40  \\
41  z_0 & = & \sum_{i=0}^{n-1} \sum_{j=0}^{n-1} y_{i,j}
42 \end{array}
43 \] $$
44 see $cref link_mat_mul$$.
45 
46 $head Inclusion$$
47 The template function $code mat_sum_sq$$ is defined in the $code CppAD$$
48 namespace by including
49 the file $code cppad/speed/mat_sum_sq.hpp$$
50 (relative to the CppAD distribution directory).
51 
52 $head n$$
53 This argument has prototype
54 $codei%
55  size_t %n%
56 %$$
57 It specifies the size of the matrices.
58 
59 $head x$$
60 The argument $icode x$$ has prototype
61 $codei%
62  const %Vector% &%x%
63 %$$
64 and $icode%x%.size() == %n% * %n%$$.
65 It contains the elements of $latex x$$ in row major order; i.e.,
66 $latex \[
67  x_{i,j} = x [ i * n + j ]
68 \] $$
69 
70 $head y$$
71 The argument $icode y$$ has prototype
72 $codei%
73  %Vector%& %y%
74 %$$
75 and $icode%y%.size() == %n% * %n%$$.
76 The input value of its elements does not matter.
77 Upon return,
78 $latex \[
79 \begin{array}{rcl}
80  y_{i,j} & = & \sum_{k=0}^{n-1} x_{i,k} x_{k,j}
81  \\
82  y[ i * n + j ] & = & y_{i,j}
83 \end{array}
84 \] $$
85 
86 
87 $head z$$
88 The argument $icode d$$ has prototype
89 $codei%
90  %Vector%& %z%
91 %$$.
92 The input value of its element does not matter.
93 Upon return
94 $latex \[
95 \begin{array}{rcl}
96  z_0 & = & \sum_{i=0}^{n-1} \sum_{j=0}^n y_{i,j}
97  \\
98  z[0] & = & z_0
99 \end{array}
100 \] $$
101 
102 $head Vector$$
103 The type $icode Vector$$ is any
104 $cref SimpleVector$$, or it can be a raw pointer to the vector elements.
105 The element type must support
106 addition, multiplication, and assignment to both its own type
107 and to a double value.
108 
109 $children%
110  speed/example/mat_sum_sq.cpp%
111  omh/mat_sum_sq_hpp.omh
112 %$$
113 
114 
115 $head Example$$
116 The file
117 $cref mat_sum_sq.cpp$$
118 contains an example and test of $code mat_sum_sq.hpp$$.
119 It returns true if it succeeds and false otherwise.
120 
121 $head Source Code$$
122 The file
123 $cref mat_sum_sq.hpp$$
124 contains the source for this template function.
125 
126 $end
127 ------------------------------------------------------------------------------
128 */
129 // BEGIN C++
130 # include <cstddef>
131 //
132 namespace CppAD {
133  template <class Vector>
134  void mat_sum_sq(size_t n, Vector& x , Vector& y , Vector& z)
135  { size_t i, j, k;
136  // Very simple computation of y = x * x for speed comparison
137  for(i = 0; i < n; i++)
138  { for(j = 0; j < n; j++)
139  { y[i * n + j] = 0.;
140  for(k = 0; k < n; k++)
141  y[i * n + j] += x[i * n + k] * x[k * n + j];
142  }
143  }
144  z[0] = 0.;
145  for(i = 0; i < n; i++)
146  { for(j = 0; j < n; j++)
147  z[0] += y[i * n + j];
148  }
149  return;
150  }
151 
152 }
153 // END C++
154 # endif
void mat_sum_sq(size_t n, Vector &x, Vector &y, Vector &z)
Definition: mat_sum_sq.hpp:134