| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <sparse/general.h> |
| 14 |
#include <stdbool.h> |
| 15 |
#include <stddef.h> |
| 16 |
#include <stdio.h> |
| 17 |
|
| 18 |
#ifdef __cplusplus |
| 19 |
extern "C" { |
| 20 |
#endif |
| 21 |
|
| 22 |
#define SYMMETRY_EPSILON 0.0000001 |
| 23 |
enum {FORMAT_CSR, FORMAT_COORD}; |
| 24 |
enum {UNMASKED = -10, MASKED = 1}; |
| 25 |
enum {BIPARTITE_RECT = 0, BIPARTITE_PATTERN_UNSYM, BIPARTITE_UNSYM, BIPARTITE_ALWAYS}; |
| 26 |
|
| 27 |
|
| 28 |
struct SparseMatrix_struct { |
| 29 |
int m; /* row dimension */ |
| 30 |
int n; /* column dimension */ |
| 31 |
size_t nz; ///< the actual length used is nz, for CSR/CSC matrix this is the same as ia[n] |
| 32 |
size_t nzmax; ///< the current length of ja and a (if exists) allocated |
| 33 |
int type; /* whether it is real/complex matrix, or pattern only */ |
| 34 |
int *ia; /* row pointer for CSR format, or row indices for coordinate format. 0-based */ |
| 35 |
int *ja; /* column indices. 0-based */ |
| 36 |
void *a; /* entry values. If NULL, pattern matrix */ |
| 37 |
int format;/* whether it is CSR, CSC, COORD. By default it is in CSR format */ |
| 38 |
bool is_pattern_symmetric :1; |
| 39 |
bool is_symmetric :1; |
| 40 |
bool is_undirected :1; |
| 41 |
size_t size;/* size of each entry. This allows for general matrix where each entry is, say, a matrix itself */ |
| 42 |
}; |
| 43 |
|
| 44 |
typedef struct SparseMatrix_struct* SparseMatrix; |
| 45 |
|
| 46 |
enum {MATRIX_TYPE_REAL = 1<<0, MATRIX_TYPE_COMPLEX = 1<<1, MATRIX_TYPE_INTEGER = 1<<2, MATRIX_TYPE_PATTERN = 1<<3}; |
| 47 |
|
| 48 |
SparseMatrix SparseMatrix_new(int m, int n, size_t nz, int type, int format); |
| 49 |
|
| 50 |
/* this version sum repeated entries */ |
| 51 |
SparseMatrix SparseMatrix_from_coordinate_format(SparseMatrix A); |
| 52 |
SparseMatrix SparseMatrix_from_coordinate_format_not_compacted(SparseMatrix A); |
| 53 |
|
| 54 |
SparseMatrix SparseMatrix_from_coordinate_arrays(size_t nz, int m, int n, |
| 55 |
int *irn, int *jcn, |
| 56 |
const void *val, int type, |
| 57 |
size_t sz); |
| 58 |
|
| 59 |
SparseMatrix SparseMatrix_from_coordinate_arrays_not_compacted(size_t nz, int m, |
| 60 |
int n, int *irn, |
| 61 |
int *jcn, |
| 62 |
void *val, |
| 63 |
int type, |
| 64 |
size_t sz); |
| 65 |
|
| 66 |
void SparseMatrix_export(FILE *f, SparseMatrix A);/* export into MM format except the header */ |
| 67 |
|
| 68 |
void SparseMatrix_delete(SparseMatrix A); |
| 69 |
|
| 70 |
SparseMatrix SparseMatrix_add(SparseMatrix A, SparseMatrix B); |
| 71 |
SparseMatrix SparseMatrix_multiply(SparseMatrix A, SparseMatrix B); |
| 72 |
SparseMatrix SparseMatrix_multiply3(SparseMatrix A, SparseMatrix B, SparseMatrix C); |
| 73 |
|
| 74 |
enum {SUM_REPEATED_NONE = 0, SUM_REPEATED_ALL, }; |
| 75 |
SparseMatrix SparseMatrix_sum_repeat_entries(SparseMatrix A); |
| 76 |
SparseMatrix SparseMatrix_coordinate_form_add_entry(SparseMatrix A, int irn, |
| 77 |
int jcn, const void *val); |
| 78 |
bool SparseMatrix_is_symmetric(SparseMatrix A, bool test_pattern_symmetry_only); |
| 79 |
SparseMatrix SparseMatrix_transpose(SparseMatrix A); |
| 80 |
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, |
| 81 |
bool pattern_symmetric_only); |
| 82 |
void SparseMatrix_multiply_vector(SparseMatrix A, double *v, double **res);/* if v = NULL, v is assumed to be {1,1,...,1}*/ |
| 83 |
SparseMatrix SparseMatrix_remove_diagonal(SparseMatrix A); |
| 84 |
SparseMatrix SparseMatrix_remove_upper(SparseMatrix A);/* remove diag and upper diag */ |
| 85 |
SparseMatrix SparseMatrix_divide_row_by_degree(SparseMatrix A); |
| 86 |
SparseMatrix SparseMatrix_get_real_adjacency_matrix_symmetrized(SparseMatrix A); /* symmetric, all entries to 1, diaginal removed */ |
| 87 |
void SparseMatrix_multiply_dense(SparseMatrix A, const double *v, double *res, |
| 88 |
int dim); |
| 89 |
SparseMatrix SparseMatrix_apply_fun(SparseMatrix A, double (*fun)(double x));/* for real only! */ |
| 90 |
SparseMatrix SparseMatrix_copy(SparseMatrix A); |
| 91 |
bool SparseMatrix_has_diagonal(SparseMatrix A); |
| 92 |
SparseMatrix SparseMatrix_make_undirected(SparseMatrix A);/* make it strictly low diag only, and set flag to undirected */ |
| 93 |
int *SparseMatrix_weakly_connected_components(SparseMatrix A0, int *ncomp, |
| 94 |
int **comps); |
| 95 |
void SparseMatrix_decompose_to_supervariables(SparseMatrix A, int *ncluster, int **cluster, int **clusterp); |
| 96 |
SparseMatrix SparseMatrix_get_submatrix(SparseMatrix A, int nrow, int ncol, int *rindices, int *cindices); |
| 97 |
|
| 98 |
SparseMatrix SparseMatrix_get_augmented(SparseMatrix A); |
| 99 |
|
| 100 |
/* bipartite_options: |
| 101 |
BIPARTITE_RECT -- turn rectangular matrix into square), |
| 102 |
BIPARTITE_PATTERN_UNSYM -- pattern unsummetric as bipartite |
| 103 |
BIPARTITE_UNSYM -- unsymmetric as square |
| 104 |
BIPARTITE_ALWAYS -- always as square |
| 105 |
*/ |
| 106 |
SparseMatrix SparseMatrix_to_square_matrix(SparseMatrix A, int bipartite_options); |
| 107 |
|
| 108 |
SparseMatrix SparseMatrix_sort(SparseMatrix A); |
| 109 |
|
| 110 |
SparseMatrix SparseMatrix_set_entries_to_real_one(SparseMatrix A); |
| 111 |
|
| 112 |
void SparseMatrix_distance_matrix(SparseMatrix A, double **dist_matrix); |
| 113 |
|
| 114 |
/// wrap a m×n matrix into a sparse matrix |
| 115 |
/// |
| 116 |
/// The {i,j}-th entry of the matrix is in x[i×n+j], 0≤i<m; 0≤j<n |
| 117 |
SparseMatrix SparseMatrix_from_dense(int m, int n, double *x); |
| 118 |
|
| 119 |
#ifdef __cplusplus |
| 120 |
} |
| 121 |
#endif |