File: abs_print_mat.hpp

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 (119 lines) | stat: -rw-r--r-- 2,720 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
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
112
113
114
115
116
117
118
119
# ifndef CPPAD_EXAMPLE_ABS_NORMAL_ABS_PRINT_MAT_HPP
# define CPPAD_EXAMPLE_ABS_NORMAL_ABS_PRINT_MAT_HPP
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin abs_print_mat}
{xrst_spell
   cout
   nr
}
abs_normal: Print a Vector or Matrix
####################################

Syntax
******
| ``abs_print_mat`` ( *name* , *nr* , *nc* , *mat* )

Prototype
*********
{xrst_literal
   // BEGIN PROTOTYPE
   // END PROTOTYPE
}

Purpose
*******
This routine is used by the :ref:`abs_normal<example_abs_normal-name>` examples to print
vectors and matrices.
A new-line is printed at the end of this output.

name
****
This is a name that is printed before the vector or matrix.

nr
**
This is the number of rows in the matrix. Use *nr*  = 1 for
row vectors.

nc
**
This is the number of columns in the matrix. Use *nc*  = 1 for
column vectors.

mat
***
This is a
:ref:`row-major<glossary@Row-major Representation>` representation
of the matrix (hence a :ref:`SimpleVector-name` ).
The syntax

   *std::cout <<* ``mat`` [ ``i`` ]

must output the *i*-th element of the simple vector *mat* .

{xrst_end abs_print_mat}
-----------------------------------------------------------------------------
*/
# include <cppad/cppad.hpp>

namespace CppAD { // BEGIN_CPPAD_NAMESPACE

// BEGIN PROTOTYPE
template <class Vector>
void abs_print_mat(
   const std::string& name ,
   size_t             nr   ,
   size_t             nc   ,
   const Vector&      mat  )
// END PROTOTYPE
{
   CPPAD_ASSERT_KNOWN(
      size_t(mat.size()) == nr * nc,
      "abs_print_mat: size of mat is not nr * nc"
   );
   // output name
   std::cout << name << " =";
   //
   // handle empty case
   if( nr == 0 || nc == 0 )
   {  std::cout << " " << nr << " by " << nc << " empty matrix\n";
      return;
   }
   //
   // handle vector case
   if( nr == 1 || nc == 1 )
   {  std::cout << " [";
      for(size_t i = 0; i < nr * nc; i++)
      {  if( i > 0 )
            std::cout << ", ";
         std::cout << mat[i];
      }
      std::cout << "]";
      //
      // column vectors are printed as row vectors with a transpose at end
      if( nr > 1 )
         std::cout << "^T";
      //
      std::cout << "\n";
      return;
   }
   // non-empty matrix
   std::cout << "\n";
   for(size_t i = 0; i < nr; i++)
   {  std::cout << "[";
      for(size_t j = 0; j < nc; j++)
      {  if( j > 0 )
            std::cout << ", ";
         std::cout << mat[i * nc + j];
      }
      std::cout << "]\n";
   }
   return;
}

} // END_CPPAD_NAMESPACE
# endif