fork download
  1. /* to get the segfault compile with
  2.  
  3.   gcc ex.c -lgsl -lgscblas -O2
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <signal.h>
  11. #include <math.h>
  12.  
  13. #include <gsl/gsl_linalg.h>
  14. #include <gsl/gsl_matrix.h>
  15. #include <gsl/gsl_eigen.h>
  16.  
  17. #define N 16
  18.  
  19. #define NTOT 76
  20. #define VAL 30
  21.  
  22. #define SC_EIG 2
  23.  
  24. #define EPS 0.000001
  25.  
  26. #define G6LEN(n) (((n)*((n)-1)/2+5)/6+1)
  27. #define BIAS6 63
  28. #define TOPBIT6 32
  29.  
  30. static gsl_matrix *adj;
  31. static gsl_matrix *partitioned_adj;
  32.  
  33. static gsl_matrix *adj_ext;
  34.  
  35. static gsl_vector_complex *eval_ext;
  36. static gsl_matrix_complex *evec_ext;
  37. static gsl_eigen_nonsymmv_workspace *w_ext;
  38.  
  39. static char gcode[G6LEN(N)+3];
  40.  
  41. const int eigenvalues[NTOT] = {30, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  42. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  43. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8, -8,
  44. -8, -8, -8, -8};
  45.  
  46.  
  47. char *adjtog6(gsl_matrix *adj, unsigned n) {
  48.  
  49. unsigned i,j;
  50. int k;
  51.  
  52. char *p,x;
  53.  
  54. p = gcode;
  55. *p++ = BIAS6+n;
  56.  
  57. k = 6;
  58. x = 0;
  59.  
  60. for (j = 1; j < n; ++j) {
  61. for (i = 0; i < j; ++i) {
  62. x <<= 1;
  63. if (gsl_matrix_get(adj, j,i) == 1) x |= 1;
  64.  
  65. if (--k == 0) {
  66. *p++ = BIAS6 + x;
  67. k = 6;
  68. x = 0;
  69. }
  70. }
  71. }
  72.  
  73. if (k != 6) *p++ = BIAS6 + (x << k);
  74.  
  75. *p++ = '\n';
  76. *p = '\0';
  77.  
  78. return gcode;
  79. }
  80.  
  81. static gsl_matrix *partitioned_am(gsl_matrix *adj, gsl_matrix *partitioned_adj, unsigned n) {
  82.  
  83. unsigned i,j;
  84.  
  85. unsigned total_edges = 0;
  86.  
  87. gsl_matrix_set_zero(partitioned_adj);
  88.  
  89. for (i= 0; i < n; i++) {
  90. unsigned deg = 0;
  91. for (j = 0; j < n; j++) {
  92. unsigned el = gsl_matrix_get(adj,i,j);
  93. if (el) {
  94. gsl_matrix_set(partitioned_adj, i,j,1);
  95. deg+=1;
  96. }
  97. }
  98. gsl_matrix_set(partitioned_adj,i, n, VAL-deg);
  99. gsl_matrix_set(partitioned_adj, n,i, (double)(VAL-deg)/(NTOT-n));
  100. total_edges += deg;
  101. }
  102. gsl_matrix_set(partitioned_adj,n, n, (double) 2*(NTOT*VAL/2 + total_edges/2 - n*VAL)/(NTOT-n));
  103.  
  104. return partitioned_adj;
  105. }
  106.  
  107. static int cmp(const void* elem1, const void* elem2) {
  108.  
  109. const double *a = elem1, *b = elem2;
  110.  
  111. return *a > *b ? -1 : *a < *b ? 1 : 0;
  112. }
  113.  
  114.  
  115. static double *spectrum(gsl_matrix *m, gsl_vector_complex *eval, gsl_matrix_complex *evec,
  116. gsl_eigen_nonsymmv_workspace *w, unsigned size) {
  117.  
  118. static double eigs[N+2];
  119. unsigned i;
  120.  
  121. gsl_eigen_nonsymmv (m, eval, evec, w);
  122.  
  123. for(i = 0; i < size ; i++) {
  124. gsl_complex eval_i = gsl_vector_complex_get (eval, i);
  125. eigs[i] = GSL_REAL(eval_i);
  126. }
  127.  
  128. qsort(eigs,size, sizeof(double),cmp);
  129.  
  130. return eigs;
  131. }
  132.  
  133. static unsigned does_interlace(double eigs_sc[N+2], unsigned n) {
  134.  
  135. unsigned i;
  136.  
  137. for (i = 0; i < n; i++) {
  138.  
  139. double expr = eigenvalues[i]-eigs_sc[i];
  140.  
  141. if (expr <= EPS && fabs(expr) >= EPS) {
  142. return 0;
  143. }
  144.  
  145. expr = eigs_sc[i] - eigenvalues[NTOT-n+i];
  146.  
  147. if (expr <= EPS && fabs(expr) >= EPS) {
  148. return 0;
  149. }
  150. }
  151.  
  152. return 1;
  153. }
  154.  
  155.  
  156. static unsigned isInterlaced(gsl_matrix *adj) {
  157.  
  158. gsl_matrix *m = partitioned_am(adj,partitioned_adj,N+1);
  159.  
  160. double *eigs = spectrum(m, eval_ext, evec_ext, w_ext, N+2);
  161.  
  162. return does_interlace(eigs,N+2);
  163. }
  164.  
  165. static void expand(gsl_matrix *adj, unsigned n) {
  166.  
  167. unsigned i,j;
  168.  
  169. gsl_matrix *adj_ext = gsl_matrix_alloc(n+1,n+1);
  170. gsl_matrix_set_zero(adj_ext);
  171.  
  172. for (i = 0; i < n; i++) {
  173. for (j = i+1; j < n; j++) {
  174. double val = gsl_matrix_get(adj,i,j);
  175. if (val) {
  176. gsl_matrix_set(adj_ext, i,j, 1.0);
  177. gsl_matrix_set(adj_ext, j,i, 1.0);
  178. }
  179. }
  180. }
  181.  
  182. for (i = 0; i < (1U <<n); i++) {
  183.  
  184. for (j = 0; j < n; j++) {
  185. if (i & (1U << j) ) {
  186. gsl_matrix_set(adj_ext, n, j,1);
  187. gsl_matrix_set(adj_ext, j, n,1);
  188. }
  189. }
  190.  
  191. if (isInterlaced(adj_ext)) {
  192. printf("%s", adjtog6(adj_ext, n+1));
  193. }
  194.  
  195. for (j = 0; j < n; j++) {
  196. if (i & (1 << j) ) {
  197. gsl_matrix_set(adj_ext, n, j,0);
  198. gsl_matrix_set(adj_ext, j, n,0);
  199. }
  200. }
  201.  
  202. }
  203. gsl_matrix_free(adj_ext);
  204. }
  205.  
  206. static void stringtoadj(char *s) {
  207.  
  208. char *p;
  209. int i,j,k,x = 0;
  210.  
  211. gsl_matrix_set_zero(adj);
  212.  
  213. p = s + 1;
  214.  
  215. k = 1;
  216. for (j = 1; j < N; ++j) {
  217. for (i = 0; i < j; ++i) {
  218. if (--k == 0) {
  219. k = 6;
  220. x = *(p++) - BIAS6;
  221. }
  222.  
  223. if (x & TOPBIT6) {
  224. gsl_matrix_set(adj,i,j,1);
  225. gsl_matrix_set(adj,j,i,1);
  226. }
  227. x <<= 1;
  228. }
  229. }
  230. }
  231.  
  232. int main(int argc, char *argv[]) {
  233.  
  234. adj = gsl_matrix_calloc(N, N);
  235. partitioned_adj = gsl_matrix_calloc(N+2, N+2);
  236.  
  237. adj_ext = gsl_matrix_alloc(N+1, N+1);
  238.  
  239. eval_ext = gsl_vector_complex_alloc(N+2);
  240. evec_ext = gsl_matrix_complex_alloc (N+2, N+2);
  241. w_ext = gsl_eigen_nonsymmv_alloc(N+2);
  242.  
  243. if (!w_ext || !evec_ext || !eval_ext ||!adj_ext) {
  244. puts("Errrr");
  245. return 1;
  246. }
  247.  
  248. if (!adj || !partitioned_adj) {
  249. puts("Some error somewhere");
  250. return 1;
  251. }
  252.  
  253. stringtoadj("O??O@BI@?Ej@hBAt}YFwa");
  254. expand(adj,N);
  255.  
  256. return 0;
  257.  
  258. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/SaNhCt/ccDWWfFn.o: In function `adjtog6':
prog.c:(.text+0xa5): undefined reference to `gsl_matrix_get'
/home/SaNhCt/ccDWWfFn.o: In function `main':
prog.c:(.text.startup+0x19): undefined reference to `gsl_matrix_calloc'
prog.c:(.text.startup+0x29): undefined reference to `gsl_matrix_calloc'
prog.c:(.text.startup+0x39): undefined reference to `gsl_matrix_alloc'
prog.c:(.text.startup+0x4a): undefined reference to `gsl_vector_complex_alloc'
prog.c:(.text.startup+0x5a): undefined reference to `gsl_matrix_complex_alloc'
prog.c:(.text.startup+0x6b): undefined reference to `gsl_eigen_nonsymmv_alloc'
prog.c:(.text.startup+0xce): undefined reference to `gsl_matrix_set_zero'
prog.c:(.text.startup+0x125): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x141): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x16a): undefined reference to `gsl_matrix_alloc'
prog.c:(.text.startup+0x174): undefined reference to `gsl_matrix_set_zero'
prog.c:(.text.startup+0x19f): undefined reference to `gsl_matrix_get'
prog.c:(.text.startup+0x1c4): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x1da): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x225): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x23a): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x255): undefined reference to `gsl_matrix_set_zero'
prog.c:(.text.startup+0x287): undefined reference to `gsl_matrix_get'
prog.c:(.text.startup+0x2be): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x2f5): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x312): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x353): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x371): undefined reference to `gsl_eigen_nonsymmv'
prog.c:(.text.startup+0x38a): undefined reference to `gsl_vector_complex_get'
prog.c:(.text.startup+0x485): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x49a): undefined reference to `gsl_matrix_set'
prog.c:(.text.startup+0x4bd): undefined reference to `gsl_matrix_free'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty