SparseMatrix.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #pragma once
  11. #include <sparse/general.h>
  12. #include <stdbool.h>
  13. #include <stddef.h>
  14. #include <stdio.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define SYMMETRY_EPSILON 0.0000001
  19. enum {FORMAT_CSR, FORMAT_COORD};
  20. enum {UNMASKED = -10, MASKED = 1};
  21. enum {BIPARTITE_RECT = 0, BIPARTITE_PATTERN_UNSYM, BIPARTITE_UNSYM, BIPARTITE_ALWAYS};
  22. struct SparseMatrix_struct {
  23. int m; /* row dimension */
  24. int n; /* column dimension */
  25. int nz;/* The actual length used is nz, for CSR/CSC matrix this is the same as ia[n] */
  26. int nzmax; /* the current length of ja and a (if exists) allocated.*/
  27. int type; /* whether it is real/complex matrix, or pattern only */
  28. int *ia; /* row pointer for CSR format, or row indices for coordinate format. 0-based */
  29. int *ja; /* column indices. 0-based */
  30. void *a; /* entry values. If NULL, pattern matrix */
  31. int format;/* whether it is CSR, CSC, COORD. By default it is in CSR format */
  32. bool is_pattern_symmetric :1;
  33. bool is_symmetric :1;
  34. bool is_undirected :1;
  35. size_t size;/* size of each entry. This allows for general matrix where each entry is, say, a matrix itself */
  36. };
  37. typedef struct SparseMatrix_struct* SparseMatrix;
  38. enum {MATRIX_TYPE_REAL = 1<<0, MATRIX_TYPE_COMPLEX = 1<<1, MATRIX_TYPE_INTEGER = 1<<2, MATRIX_TYPE_PATTERN = 1<<3, MATRIX_TYPE_UNKNOWN = 1<<4};
  39. /* SparseMatrix_general is more general and allow elements to be
  40. any data structure, not just real/int/complex etc */
  41. SparseMatrix SparseMatrix_new(int m, int n, int nz, int type, int format);
  42. SparseMatrix SparseMatrix_general_new(int m, int n, int nz, int type, size_t sz, int format);
  43. /* this version sum repeated entries */
  44. SparseMatrix SparseMatrix_from_coordinate_format(SparseMatrix A);
  45. SparseMatrix SparseMatrix_from_coordinate_format_not_compacted(SparseMatrix A);
  46. SparseMatrix SparseMatrix_from_coordinate_arrays(int nz, int m, int n, int *irn, int *jcn, void *val, int type, size_t sz);
  47. SparseMatrix SparseMatrix_from_coordinate_arrays_not_compacted(int nz, int m, int n, int *irn, int *jcn, void *val, int type, size_t sz);
  48. void SparseMatrix_export(FILE *f, SparseMatrix A);/* export into MM format except the header */
  49. void SparseMatrix_delete(SparseMatrix A);
  50. SparseMatrix SparseMatrix_add(SparseMatrix A, SparseMatrix B);
  51. SparseMatrix SparseMatrix_multiply(SparseMatrix A, SparseMatrix B);
  52. SparseMatrix SparseMatrix_multiply3(SparseMatrix A, SparseMatrix B, SparseMatrix C);
  53. enum {SUM_REPEATED_NONE = 0, SUM_REPEATED_ALL, };
  54. SparseMatrix SparseMatrix_sum_repeat_entries(SparseMatrix A);
  55. SparseMatrix SparseMatrix_coordinate_form_add_entry(SparseMatrix A, int irn,
  56. int jcn, const void *val);
  57. bool SparseMatrix_is_symmetric(SparseMatrix A, bool test_pattern_symmetry_only);
  58. SparseMatrix SparseMatrix_transpose(SparseMatrix A);
  59. SparseMatrix SparseMatrix_symmetrize(SparseMatrix A,
  60. bool pattern_symmetric_only);
  61. void SparseMatrix_multiply_vector(SparseMatrix A, double *v, double **res);/* if v = NULL, v is assumed to be {1,1,...,1}*/
  62. SparseMatrix SparseMatrix_remove_diagonal(SparseMatrix A);
  63. SparseMatrix SparseMatrix_remove_upper(SparseMatrix A);/* remove diag and upper diag */
  64. SparseMatrix SparseMatrix_divide_row_by_degree(SparseMatrix A);
  65. SparseMatrix SparseMatrix_get_real_adjacency_matrix_symmetrized(SparseMatrix A); /* symmetric, all entries to 1, diaginal removed */
  66. void SparseMatrix_multiply_dense(SparseMatrix A, const double *v, double *res,
  67. int dim);
  68. SparseMatrix SparseMatrix_apply_fun(SparseMatrix A, double (*fun)(double x));/* for real only! */
  69. SparseMatrix SparseMatrix_copy(SparseMatrix A);
  70. bool SparseMatrix_has_diagonal(SparseMatrix A);
  71. SparseMatrix SparseMatrix_make_undirected(SparseMatrix A);/* make it strictly low diag only, and set flag to undirected */
  72. int *SparseMatrix_weakly_connected_components(SparseMatrix A0, int *ncomp,
  73. int **comps);
  74. void SparseMatrix_decompose_to_supervariables(SparseMatrix A, int *ncluster, int **cluster, int **clusterp);
  75. SparseMatrix SparseMatrix_get_submatrix(SparseMatrix A, int nrow, int ncol, int *rindices, int *cindices);
  76. SparseMatrix SparseMatrix_get_augmented(SparseMatrix A);
  77. /* bipartite_options:
  78. BIPARTITE_RECT -- turn rectangular matrix into square),
  79. BIPARTITE_PATTERN_UNSYM -- pattern unsummetric as bipartite
  80. BIPARTITE_UNSYM -- unsymmetric as square
  81. BIPARTITE_ALWAYS -- always as square
  82. */
  83. SparseMatrix SparseMatrix_to_square_matrix(SparseMatrix A, int bipartite_options);
  84. SparseMatrix SparseMatrix_sort(SparseMatrix A);
  85. SparseMatrix SparseMatrix_set_entries_to_real_one(SparseMatrix A);
  86. void SparseMatrix_distance_matrix(SparseMatrix A, double **dist_matrix);
  87. SparseMatrix SparseMatrix_from_dense(int m, int n, double *x);
  88. #ifdef __cplusplus
  89. }
  90. #endif