File: sparse_rcv.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (111 lines) | stat: -rw-r--r-- 3,077 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
{xrst_begin sparse_rcv.cpp}

sparse_rcv: Example and Test
############################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end sparse_rcv.cpp}
*/

// BEGIN C++
# include <cppad/utility/sparse_rcv.hpp>
# include <vector>

bool sparse_rcv(void)
{  bool ok = true;
   typedef std::vector<size_t> SizeVector;
   typedef std::vector<double> ValueVector;

   // sparsity pattern for a 5 by 5 diagonal matrix
   size_t nr  = 5;
   size_t nc  = 5;
   size_t nnz = 5;
   CppAD::sparse_rc<SizeVector> pattern(nr, nc, nnz);
   for(size_t k = 0; k < nnz; k++)
   {  size_t r = nnz - k - 1; // reverse or column-major order
      size_t c = nnz - k - 1;
      pattern.set(k, r, c);
   }

   // sparse matrix
   CppAD::sparse_rcv<SizeVector, ValueVector> matrix(pattern);
   for(size_t k = 0; k < nnz; k++)
   {  double v = double(k);
      matrix.set(nnz - k - 1, v);
   }

   // row, column, and value vectors
   const SizeVector&  row( matrix.row() );
   const SizeVector&  col( matrix.row() );
   const ValueVector& val( matrix.val() );
   SizeVector col_major = matrix.col_major();

   // check row,  column, and value
   for(size_t k = 0; k < nnz; k++)
   {  ok &= row[ col_major[k] ] == k;
      ok &= col[ col_major[k] ] == k;
      ok &= val[ col_major[k] ] == double(k);
   }

   // pat
   const CppAD::sparse_rc<SizeVector> pat( matrix.pat() );
   ok &= nnz == pat.nnz();
   ok &= nr  == pat.nr();
   ok &= nc  == pat.nc();
   for(size_t k = 0; k < nnz; k++)
   {  ok &= row[k] == pat.row()[k];
      ok &= col[k] == pat.col()[k];
   }

   // create an empty matrix
   CppAD::sparse_rcv<SizeVector, ValueVector> other;
   ok &= other.nnz() == 0;
   ok &= other.nr()  == 0;
   ok &= other.nc()  == 0;

   // now swap other with matrix
   matrix.swap(other);
   ok &= matrix.nnz() == 0;
   ok &= matrix.nr()  == 0;
   ok &= matrix.nc()  == 0;
   for(size_t k = 0; k < nnz; k++)
   {  ok &= other.row()[ col_major[k] ] == k;
      ok &= other.col()[ col_major[k] ] == k;
      ok &= other.val()[ col_major[k] ] == double(k);
   }

   // now use the assignment statement
   matrix = other;
   ok    &= other.nr()  == matrix.nr();
   ok    &= other.nc()  == matrix.nc();
   ok    &= other.nnz() == matrix.nnz();
   for(size_t k = 0; k < nnz; k++)
   {  ok &= matrix.row()[k] == other.row()[k];
      ok &= matrix.col()[k] == other.col()[k];
      ok &= matrix.val()[k] == other.val()[k];
   }

   // now use the copy constructor
   CppAD::sparse_rcv<SizeVector, ValueVector> copy(matrix);
   ok    &= copy.nr()  == matrix.nr();
   ok    &= copy.nc()  == matrix.nc();
   ok    &= copy.nnz() == matrix.nnz();
   for(size_t k = 0; k < nnz; k++)
   {  ok &= matrix.row()[k] == copy.row()[k];
      ok &= matrix.col()[k] == copy.col()[k];
      ok &= matrix.val()[k] == copy.val()[k];
   }
   return ok;
}

// END C++