File: Mongoose_Sanitize.cpp

package info (click to toggle)
suitesparse 1%3A5.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 138,928 kB
  • sloc: ansic: 389,614; cpp: 24,213; makefile: 5,965; fortran: 1,927; java: 1,808; csh: 1,750; ruby: 725; sh: 226; perl: 225; python: 209; sed: 164; awk: 60
file content (156 lines) | stat: -rw-r--r-- 3,751 bytes parent folder | download | duplicates (3)
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
/* ========================================================================== */
/* === Source/Mongoose_Sanitize.cpp ========================================= */
/* ========================================================================== */

/* -----------------------------------------------------------------------------
 * Mongoose Graph Partitioning Library  Copyright (C) 2017-2018,
 * Scott P. Kolodziej, Nuri S. Yeralan, Timothy A. Davis, William W. Hager
 * Mongoose is licensed under Version 3 of the GNU General Public License.
 * Mongoose is also available under other licenses; contact authors for details.
 * -------------------------------------------------------------------------- */

#include "Mongoose_Sanitize.hpp"
#include "Mongoose_Internal.hpp"

using namespace std;

namespace Mongoose
{

cs *sanitizeMatrix(cs *compressed_A, bool symmetricTriangular,
                   bool makeEdgeWeightsBinary)
{
    cs *cleanMatrix;
    if (symmetricTriangular)
    {
        cleanMatrix = mirrorTriangular(compressed_A);
    }
    else
    {
        cs *A_transpose = cs_transpose(compressed_A, 1);
        if (!A_transpose)
        {
            return NULL;
        }
        cleanMatrix = cs_add(compressed_A, A_transpose, 0.5, 0.5);
        cs_spfree(A_transpose);
    }

    if (!cleanMatrix)
    {
        return NULL;
    }

    removeDiagonal(cleanMatrix);

    cs *cleanMatrix_transpose = cs_transpose(cleanMatrix, 1);
    cs_spfree(cleanMatrix);

    if (!cleanMatrix_transpose)
    {
        return NULL;
    }
    cleanMatrix = cs_transpose(cleanMatrix_transpose, 1);
    cs_spfree(cleanMatrix_transpose);
    if (!cleanMatrix)
    {
        return NULL;
    }

    if (cleanMatrix->x)
    {
        for (Int p = 0; p < cleanMatrix->p[cleanMatrix->n]; p++)
        {
            if (makeEdgeWeightsBinary)
            {
                // Make edge weights binary
                if (cleanMatrix->x[p] != 0)
                {
                    cleanMatrix->x[p] = 1;
                }
            }
            else
            {
                // Force edge weights to be positive
                cleanMatrix->x[p] = fabs(cleanMatrix->x[p]);
            }
        }
    }

    return cleanMatrix;
}

void removeDiagonal(cs *A)
{
    Int n      = A->n;
    Int *Ap    = A->p;
    Int *Ai    = A->i;
    double *Ax = A->x;
    Int nz     = 0;
    Int old_Ap = Ap[0];

    for (Int j = 0; j < n; j++)
    {
        for (Int p = old_Ap; p < Ap[j + 1]; p++)
        {
            if (Ai[p] != j)
            {
                Ai[nz] = Ai[p];
                if (Ax)
                    Ax[nz] = Ax[p];
                nz++;
            }
        }
        old_Ap    = Ap[j + 1];
        Ap[j + 1] = nz;
    }
}

// Requires A to be a triangular matrix with no diagonal.
cs *mirrorTriangular(cs *A)
{
    if (!A)
        return NULL;
    Int A_n  = A->n;
    Int A_nz = A->p[A_n];
    Int B_nz = 2 * A_nz;

    bool values = (A->x != NULL);

    // allocate B in triplet form, with values Bx if A has values
    cs *B = cs_spalloc(A_n, A_n, B_nz, values, 1);
    if (!B)
        return NULL;

    Int *Ap    = A->p;
    Int *Ai    = A->i;
    double *Ax = A->x;
    Int *Bj    = B->p;
    Int *Bi    = B->i;
    double *Bx = B->x;
    Int nz     = 0;

    for (Int j = 0; j < A_n; j++)
    {
        for (Int p = Ap[j]; p < Ap[j + 1]; p++)
        {
            Bi[nz] = Ai[p];
            Bj[nz] = j;
            if (values)
                Bx[nz] = Ax[p];
            nz++;
            Bi[nz] = j;
            Bj[nz] = Ai[p];
            if (values)
                Bx[nz] = Ax[p];
            nz++;
        }
    }
    B->nz = nz;
    cs *C = cs_compress(B);
    cs_spfree(B);

    return C;
}

} // end namespace Mongoose