/* -*- mode: C -*- */ /* IGraph R package. Copyright (C) 2007 Gabor Csardi MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "igraph.h" #include "memory.h" #include /***********************************************/ /* in-degree */ /***********************************************/ int igraph_revolver_d(const igraph_t *graph, igraph_integer_t niter, igraph_vector_t *kernel, igraph_vector_t *sd, igraph_vector_t *norm, igraph_vector_t *cites, igraph_vector_t *expected, igraph_real_t *logprob, igraph_real_t *lognull, const igraph_vector_t *debug, igraph_vector_ptr_t *debugres) { long int no_of_nodes=igraph_vcount(graph); igraph_vector_t st; long int i; igraph_integer_t maxdegree; IGRAPH_VECTOR_INIT_FINALLY(&st, no_of_nodes); for (i=0; i1) { MATRIX(ntkl, 0, 0)=1; } else { MATRIX(ntkl, 0, 1)=1; } for (node=0; node=0; k++) { long int shnode=node+1-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; MATRIX(ntkl, deg, k-1)--; if (MATRIX(ntkl, deg, k-1)==0) { MATRIX(*normfact, deg, k-1) += (edges-MATRIX(ch, deg, k-1)); } MATRIX(ntkl, deg, k) += 1; if (MATRIX(ntkl, deg, k)==1) { MATRIX(ch, deg, k)=edges; } } } /* Make normfact up to date, calculate mean, sd */ for (i=0; i1) { VECTOR(*st)[0]=MATRIX(*kernel, 0, 0); } else { VECTOR(*st)[0]=MATRIX(*kernel, 0, 1); } for (node=1; node= 0; k++) { long int shnode=node-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; VECTOR(*st)[node] += -MATRIX(*kernel, deg, k-1)+MATRIX(*kernel, deg, k); } } igraph_vector_destroy(&neis); igraph_vector_destroy(&indegree); IGRAPH_FINALLY_CLEAN(2); return 0; } int igraph_revolver_exp_ad(const igraph_t *graph, igraph_matrix_t *expected, const igraph_matrix_t *kernel, const igraph_vector_t *st, igraph_integer_t pmaxind, igraph_integer_t pagebins) { long int maxind=pmaxind, agebins=pagebins; long int no_of_nodes=igraph_vcount(graph); long int binwidth=no_of_nodes/agebins+1; igraph_vector_t indegree; igraph_vector_t outdegree; igraph_vector_t cumst; igraph_matrix_t ntkl; igraph_matrix_t ch; igraph_vector_t neis; long int node, i, j, k; IGRAPH_MATRIX_INIT_FINALLY(&ntkl, maxind+1, agebins); IGRAPH_MATRIX_INIT_FINALLY(&ch, maxind+1, agebins); IGRAPH_VECTOR_INIT_FINALLY(&cumst, no_of_nodes+1); IGRAPH_VECTOR_INIT_FINALLY(&indegree, no_of_nodes); IGRAPH_VECTOR_INIT_FINALLY(&neis, 0); IGRAPH_VECTOR_INIT_FINALLY(&outdegree, no_of_nodes); IGRAPH_CHECK(igraph_degree(graph, &outdegree, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS)); /* create cumulative sum of dt/S(t) */ VECTOR(cumst)[0]=0; for (i=0; i=0; k++) { long int shnode=node-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; MATRIX(ntkl, deg, k-1) -= 1; MATRIX(*expected, deg, k-1) += (MATRIX(ntkl, deg, k-1)+1) * (VECTOR(cumst)[node]-VECTOR(cumst)[(long int)MATRIX(ch, deg, k-1)]); MATRIX(ch, deg, k-1)=node; MATRIX(ntkl, deg, k) += 1; MATRIX(*expected, deg, k) += (MATRIX(ntkl, deg, k)-1) * (VECTOR(cumst)[node]-VECTOR(cumst)[(long int)MATRIX(ch, deg, k)]); MATRIX(ch, deg, k)=node; } } /* complete res */ for (i=0; i1) { ARRAY3(ntkl, (long int)VECTOR(*cats)[0], 0, 0)=1; } else { ARRAY3(ntkl, (long int)VECTOR(*cats)[0], 0, 1)=1; } for (node=0; node=0; k++) { long int shnode=node+1-binwidth*k+1; long int cidx=VECTOR(*cats)[shnode]; long int deg=VECTOR(indegree)[shnode]; ARRAY3(ntkl, cidx, deg, k-1) -= 1; if (ARRAY3(ntkl, cidx, deg, k-1)==0) { ARRAY3(*normfact, cidx, deg, k-1) += (edges-ARRAY3(ch, cidx, deg, k-1)); } ARRAY3(ntkl, cidx, deg, k) += 1; if (ARRAY3(ntkl, cidx, deg, k)==1) { ARRAY3(ch, cidx, deg, k)=edges; } } } /* Make normfact up to date, calculate mean, sd */ for (k=0; k 1 ? 0 : 1); for (node=1; node= 0; k++) { long int shnode=node-binwidth*k+1; long int cidx=VECTOR(*cats)[shnode]; long int deg=VECTOR(indegree)[shnode]; VECTOR(*st)[node] += -ARRAY3(*kernel, cidx, deg, k-1) + ARRAY3(*kernel, cidx, deg, k); } } igraph_vector_destroy(&neis); igraph_vector_destroy(&indegree); IGRAPH_FINALLY_CLEAN(2); return 0; } int igraph_revolver_exp_ade(const igraph_t *graph, igraph_array3_t *expected, const igraph_array3_t *kernel, const igraph_vector_t *st, const igraph_vector_t *cats, igraph_integer_t pnocats, igraph_integer_t pmaxind, igraph_integer_t pagebins) { /* TODO */ return 0; } int igraph_revolver_error_ade(const igraph_t *graph, const igraph_array3_t *kernel, const igraph_vector_t *st, const igraph_vector_t *cats, igraph_integer_t pnocats, igraph_integer_t pmaxdegree, igraph_integer_t pagebins, igraph_real_t *logprob, igraph_real_t *lognull) { long int agebins=pagebins; long int no_of_nodes=igraph_vcount(graph); long int binwidth=no_of_nodes/agebins+1; igraph_vector_t indegree; igraph_vector_t neis; long int node, i; igraph_real_t rlogprob, rlognull, *mylogprob=logprob, *mylognull=lognull; IGRAPH_VECTOR_INIT_FINALLY(&indegree, no_of_nodes); IGRAPH_VECTOR_INIT_FINALLY(&neis, 0); if (!logprob) { mylogprob=&rlogprob; } if (!lognull) { mylognull=&rlognull; } *mylogprob=0; *mylognull=0; for (node=0; node=0; k++) { long int shnode=node+1-binwidth*k+1; IGRAPH_CHECK(igraph_neighbors(graph, &neis, shnode, IGRAPH_OUT)); for (i=0; i= 0; k++) { long int shnode=node-binwidth*k+1; IGRAPH_CHECK(igraph_neighbors(graph, &neis, shnode, IGRAPH_OUT)); for (i=0; i=0; k++) { long int shnode=node+1-binwidth*k+1; IGRAPH_CHECK(igraph_neighbors(graph, &neis, shnode, IGRAPH_OUT)); for (i=0; i= 0; k++) { long int shnode=node-binwidth*k+1; IGRAPH_CHECK(igraph_neighbors(graph, &neis, shnode, IGRAPH_OUT)); for (i=0; i=0; k++) { long int shnode=node+1-binwidth*k+1; IGRAPH_CHECK(igraph_neighbors(graph, &neis, shnode, IGRAPH_OUT)); for (i=0; i= 0; k++) { long int shnode=node-binwidth*k+1; IGRAPH_CHECK(igraph_neighbors(graph, &neis, shnode, IGRAPH_OUT)); for (i=0; i=0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, i-window, IGRAPH_OUT)); for (j=0; j maxdegree) { maxdegree=VECTOR(st)[to]; } } } igraph_vector_destroy(&neis); IGRAPH_FINALLY_CLEAN(1); for (i=0; i= 0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node+1-window, IGRAPH_OUT)); for (i=0; i=0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node-window, IGRAPH_OUT)); for (i=0; i= 0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node-window+1, IGRAPH_OUT)); for (i=0; i=0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, i-window, IGRAPH_OUT)); for (j=0; j maxdegree) { maxdegree=VECTOR(st)[to]; } } } igraph_vector_destroy(&neis); IGRAPH_FINALLY_CLEAN(1); for (i=0; i1 ? 0 : 1, 0)=1; for (node=0; node= 0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node+1-window, IGRAPH_OUT)); for (i=0; i=0; k++) { long int shnode=node+1-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; MATRIX(ntk, k-1, deg)--; if (MATRIX(ntk, k-1, deg)==0) { MATRIX(*normfact, k-1, deg) += (edges-MATRIX(ch, k-1, deg)); } MATRIX(ntk, k, deg) += 1; if (MATRIX(ntk, k, deg)==1) { MATRIX(ch, k, deg)=edges; } } } /* Make normfact up to date, calculate mean, sd */ for (i=0; i1 ? 0 : 1, 0); for (node=1; node=0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node-window, IGRAPH_OUT)); for (i=0; i= 0; k++) { long int shnode=node-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; VECTOR(*st)[node] += -MATRIX(*kernel, k-1, deg)+MATRIX(*kernel, k, deg); } } igraph_vector_destroy(&neis); igraph_vector_destroy(&indegree); IGRAPH_FINALLY_CLEAN(2); return 0; } int igraph_revolver_exp_ar(const igraph_t *graph, igraph_matrix_t *expected, const igraph_matrix_t *kernel, const igraph_vector_t *st, igraph_integer_t agebins, igraph_integer_t window, igraph_integer_t pmaxind) { /* TODO */ return 0; } int igraph_revolver_error_ar(const igraph_t *graph, const igraph_matrix_t *kernel, const igraph_vector_t *st, igraph_integer_t pagebins, igraph_integer_t pwindow, igraph_integer_t maxind, igraph_real_t *logprob, igraph_real_t *lognull) { long int no_of_nodes=igraph_vcount(graph); long int agebins=pagebins; long int window=pwindow; long int binwidth=no_of_nodes/agebins+1; igraph_vector_t indegree; igraph_vector_t neis; long int node; long int i; igraph_real_t rlogprob, rlognull, *mylogprob=logprob, *mylognull=lognull; IGRAPH_VECTOR_INIT_FINALLY(&indegree, no_of_nodes); IGRAPH_VECTOR_INIT_FINALLY(&neis, 0); if (!mylogprob) { mylogprob=&rlogprob; } if (!mylognull) { mylognull=&rlognull; } *mylogprob=0; *mylognull=0; for (node=0; node= 0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node-window+1, IGRAPH_OUT)); for (i=0; i1 ? 0 : 1)=1; for (node=0; node=0; k++) { long int shnode=node+1-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; MATRIX(ntkl, deg, k-1) -= 1; if (MATRIX(ntkl, deg, k-1)==0) { for (j=0; j1 ? 0 : 1); } VECTOR(*st)[0]=MATRIX(allst, (long int) VECTOR(*cats)[0], 0); for (node=1; node= 0; k++) { long int shnode=node-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; for (j=0; j=0; k++) { long int shnode=node+1-binwidth*k+1; IGRAPH_CHECK(igraph_neighbors(graph, &neis, shnode, IGRAPH_OUT)); for (i=0; i= 0; k++) { long int shnode=node-binwidth*k+1; IGRAPH_CHECK(igraph_neighbors(graph, &neis, shnode, IGRAPH_OUT)); for (i=0; i=0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, i-window, IGRAPH_OUT)); for (j=0; j maxdegree) { maxdegree=VECTOR(st)[to]; } } } igraph_vector_destroy(&neis); IGRAPH_FINALLY_CLEAN(1); igraph_progress("Revolver di", 0, NULL); for (i=0; i= 0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node+1-window, IGRAPH_OUT)); for (i=0; i=0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node-window, IGRAPH_OUT)); for (i=0; i= 0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node-window+1, IGRAPH_OUT)); for (i=0; i=0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, i-window, IGRAPH_OUT)); for (j=0; j maxdegree) { maxdegree=VECTOR(st)[to]; } } } igraph_vector_destroy(&neis); IGRAPH_FINALLY_CLEAN(1); for (i=0; i1 ? 0 : 1)=1; for (node=0; node= 0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node+1-window, IGRAPH_OUT)); for (i=0; i=0; k++) { long int shnode=node+1-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; MATRIX(ntkl, deg, k-1) -= 1; if (MATRIX(ntkl, deg, k-1)==0) { for (j=0; j1 ? 0 : 1); } VECTOR(*st)[0]=MATRIX(allst, (long int) VECTOR(*cats)[0], 0); for (node=1; node=0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node-window, IGRAPH_OUT)); for (i=0; i= 0; k++) { long int shnode=node-binwidth*k+1; long int deg=VECTOR(indegree)[shnode]; for (j=0; j= 0) { IGRAPH_CHECK(igraph_neighbors(graph, &neis, node-window+1, IGRAPH_OUT)); for (i=0; i