File: mat_sum_sq.cpp

package info (click to toggle)
cppad 2017.00.00.4-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 26,780 kB
  • ctags: 12,220
  • sloc: xml: 216,822; cpp: 63,586; sh: 9,233; makefile: 1,321; python: 637; ansic: 170
file content (74 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// $Id: mat_sum_sq.cpp 3788 2016-02-09 15:50:06Z bradbell $
/* --------------------------------------------------------------------------
CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell

CppAD is distributed under multiple licenses. This distribution is under
the terms of the
                    GNU General Public License Version 3.

A copy of this license is included in the COPYING file of this distribution.
Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
-------------------------------------------------------------------------- */

/*
$begin mat_sum_sq.cpp$$
$spell
	sq
$$

$section Sum of the Elements of the Square of a Matrix: Example and Test$$
$mindex mat_sum_sq$$


$code
$srcfile%speed/example/mat_sum_sq.cpp%0%// BEGIN C++%// END C++%1%$$
$$

$end
*/
// BEGIN C++
# include <vector>
# include <cstddef>
# include <cppad/utility/vector.hpp>
# include <cppad/speed/mat_sum_sq.hpp>

bool mat_sum_sq()
{	bool   ok = true;
	double x_00, x_01, x_10, x_11, check;

	// dimension of the matrices x, y, and the result z
	size_t n = 2;
	CppAD::vector<double> x(n * n), y(n * n), z(1);

	// x = [ 1 2 ; 3 4 ]
	x[0] = x_00 = 1.;
	x[1] = x_01 = 2.;
	x[2] = x_10 = 3.;
	x[3] = x_11 = 4.;

	// compute y = x * x and z = sum of elements in y
	CppAD::mat_sum_sq(n, x, y, z);

	// check y_00
	check = x_00 * x_00 + x_01 * x_10;
	ok   &= (check == y[0]);

	// check y_01
	check = x_00 * x_01 + x_01 * x_11;
	ok   &= (check == y[1]);

	// check y_10
	check = x_10 * x_00 + x_11 * x_10;
	ok   &= (check == y[2]);

	// check y_11
	check = x_10 * x_01 + x_11 * x_11;
	ok   &= (check == y[3]);

	// check z
	check = y[0] + y[1] + y[2] + y[3];
	ok   &= (check == z[0]);

	return ok;
}
// END C++