casadi_print_canonical.hpp
1 //
2 // MIT No Attribution
3 //
4 // Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl, KU Leuven.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy of this
7 // software and associated documentation files (the "Software"), to deal in the Software
8 // without restriction, including without limitation the rights to use, copy, modify,
9 // merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13 // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 //
19 
20 // SYMBOL "print_canonical"
21 template<typename T1>
22 void casadi_print_canonical(const casadi_int* sp, const T1* x) {
23  // C-REPLACE "printf" "CASADI_PRINTF"
24  // C-REPLACE "static_cast<int>" "(int) "
25  if (x) {
26  casadi_int nrow = sp[0];
27  casadi_int ncol = sp[1];
28  const casadi_int* colind = sp+2;
29  const casadi_int* row = colind + ncol + 1;
30  casadi_int nnz = sp[2+ncol];
31  if (nrow==1 && ncol==1 && nnz==1) {
32  casadi_print_scalar(x[0]);
33  } else {
34  casadi_int i;
35  printf("%dx%d: ", static_cast<int>(nrow), static_cast<int>(ncol));
36  casadi_print_vector(nnz, x);
37  if (nnz!=nrow*ncol) {
38  printf(", colind: [");
39  for (i = 0; i < ncol; ++i) {
40  if (i > 0) printf(", ");
41  printf("%d", static_cast<int>(colind[i]));
42  }
43  printf("], row: [");
44  for (i = 0; i < nnz; ++i) {
45  if (i > 0) printf(", ");
46  printf("%d", static_cast<int>(row[i]));
47  }
48  printf("]");
49  }
50  }
51  } else {
52  printf("NULL");
53  }
54 
55 }