File: print_matrix.hh

package info (click to toggle)
lapackpp 2024.10.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,500 kB
  • sloc: cpp: 80,181; ansic: 27,660; python: 4,838; xml: 182; perl: 99; makefile: 53; sh: 23
file content (168 lines) | stat: -rw-r--r-- 5,396 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2017-2023, University of Tennessee. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// This program is free software: you can redistribute it and/or modify it under
// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.

#ifndef PRINT_HH
#define PRINT_HH

#include <assert.h>
#include <complex>

// -----------------------------------------------------------------------------
template< typename T >
void print_matrix( int64_t m, int64_t n, T *A, int64_t lda,
                   const char* format="%9.4f",
                   const char* format_int="%9.0f" )
{
    #define A(i_, j_) A[ (i_) + size_t(lda)*(j_) ]

    require( m >= 0 );
    require( n >= 0 );
    require( lda >= m );
    char format_[32], format_int_[32];
    snprintf( format_,     sizeof(format_),     " %s", format     );
    snprintf( format_int_, sizeof(format_int_), " %s", format_int );

    printf( "[\n" );
    for (int64_t i = 0; i < m; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            T re = A(i, j);
            if (re == int(re))
                printf( format_int_, re );
            else
                printf( format_, re );
        }
        printf( "\n" );
    }
    printf( "];\n" );

    #undef A
}

// -----------------------------------------------------------------------------
/// Overload for complex.
template< typename T >
void print_matrix( int64_t m, int64_t n, std::complex<T>* A, int64_t lda,
                   const char* format="%9.4f",
                   const char* format_int="%9.0f" )
{
    #define A(i_, j_) A[ (i_) + size_t(lda)*(j_) ]

    require( m >= 0 );
    require( n >= 0 );
    require( lda >= m );
    char format_[32], format_i_[32], format_int_[32], format_int_i_[32];
    snprintf( format_,       sizeof(format_),       " %s",    format     );
    snprintf( format_int_,   sizeof(format_int_),   " %s",    format_int );
    snprintf( format_i_,     sizeof(format_i_),     " + %si", format     );
    snprintf( format_int_i_, sizeof(format_int_i_), " + %si", format_int );

    printf( "[\n" );
    for (int64_t i = 0; i < m; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            T re = std::real( A(i, j) );
            if (re == int(re))
                printf( format_int_, re );
            else
                printf( format_, re );

            T im = std::imag( A(i, j) );
            if (im == int(im))
                printf( format_int_i_, im );
            else
                printf( format_i_, im );
        }
        printf( "\n" );
    }
    printf( "];\n" );

    #undef A
}

// -----------------------------------------------------------------------------
/// Overload with name.
template< typename T >
void print_matrix( const char* name,
                   int64_t m, int64_t n, T *A, int64_t lda,
                   const char* format="%9.4f",
                   const char* format_int="%9.0f" )
{
    printf( "%s = ", name );
    print_matrix( m, n, A, lda, format, format_int );
}

// -----------------------------------------------------------------------------
template< typename T >
void print_vector( int64_t n, T *x, int64_t incx,
                   const char* format="%9.4f",
                   const char* format_int="%9.0f" )
{
    require( n >= 0 );
    require( incx != 0 );
    char format_[32], format_int_[32];
    snprintf( format_,     sizeof(format_),     " %s", format     );
    snprintf( format_int_, sizeof(format_int_), " %s", format_int );

    printf( "[" );
    int64_t ix = (incx > 0 ? 0 : (-n + 1)*incx);
    for (int64_t i = 0; i < n; ++i) {
        T re = x[ix];
        if (re == int(re))
            printf( format_int_, re );
        else
            printf( format_, re );

        ix += incx;
    }
    printf( " ]';\n" );
}

// -----------------------------------------------------------------------------
/// Overload for complex.
template< typename T >
void print_vector( int64_t n, std::complex<T>* x, int64_t incx,
                   const char* format="%9.4f",
                   const char* format_int="%9.0f" )
{
    require( n >= 0 );
    require( incx != 0 );
    char format_[32], format_i_[32], format_int_[32], format_int_i_[32];
    snprintf( format_,       sizeof(format_),       " %s",    format     );
    snprintf( format_int_,   sizeof(format_int_),   " %s",    format_int );
    snprintf( format_i_,     sizeof(format_i_),     " + %si", format     );
    snprintf( format_int_i_, sizeof(format_int_i_), " + %si", format_int );

    printf( "[" );
    int64_t ix = (incx > 0 ? 0 : (-n + 1)*incx);
    for (int64_t i = 0; i < n; ++i) {
        T re = std::real( x[ix] );
        if (re == int(re))
            printf( format_int_, re );
        else
            printf( format_, re );

        T im = std::imag( x[ix] );
        if (im == int(im))
            printf( format_int_i_, im );
        else
            printf( format_i_, im );

        ix += incx;
    }
    printf( " ]';\n" );
}

// -----------------------------------------------------------------------------
/// Overload with name.
template< typename T >
void print_vector( const char* name,
                   int64_t n, T *x, int64_t incx,
                   const char* format="%9.4f",
                   const char* format_int="%9.0f" )
{
    printf( "%s = ", name );
    print_vector( n, x, incx, format, format_int );
}

#endif        //  #ifndef PRINT_HH