Skip to content

Commit 067204a

Browse files
author
dirmeier
committed
Fixes for overhaul to 0.1.2
1 parent d819973 commit 067204a

File tree

14 files changed

+156
-182
lines changed

14 files changed

+156
-182
lines changed

NAMESPACE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
S3method(normalize.laplacian,numeric)
44
S3method(normalize.stochastic,numeric)
5-
export(insulated.heat.diffusion)
6-
export(laplacian.heat.diffusion)
5+
export(heat.diffusion)
76
export(nearest.neighbors)
87
export(normalize.laplacian)
98
export(normalize.stochastic)

R/heat_diffusion.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ setGeneric(
5353
"heat.diffusion",
5454
function(h0, graph, t=.5, ...)
5555
{
56-
standardGeneric("laplacian.heat.diffusion")
56+
standardGeneric("heat.diffusion")
5757
},
5858
package="diffusr"
5959
)

R/mrw.R

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
#' matrix representing the graph
3737
#' @param r a scalar between (0, 1). restart probability if a Markov random
3838
#' walk with restart is desired
39+
#' @param thresh threshold for breaking the iterative computation of the
40+
#' stationary distribution. If the absolute difference of the distribution at
41+
#' time point $t-1$ and $t$ is less than \code{thresh}, then the algorithm stops.
42+
#' If \code{thresh} is not reached before \code{niter}, then the algorithm stops
43+
#' as well.
44+
#' @param niter maximal number of iterations for computation of the
45+
#' Markov chain. If \code{thresh} is not reached, then \code{niter} is used as
46+
#' stop criterion.
47+
#' @param do.analytical boolean if the stationary distribution shall be
48+
#' computed solving the analytical solution or rather iteratively
3949
#' @param ... additional parameters
4050
#' @return returns the stationary distribution as numeric vector
4151
#'
@@ -57,7 +67,7 @@
5767
#' pt <- random.walk(p0, graph)
5868
setGeneric(
5969
"random.walk",
60-
function(p0, graph, r=.5, ...)
70+
function(p0, graph, r=.5, niter=1e4, thresh=1e-4, do.analytical=FALSE, ...)
6171
{
6272
standardGeneric("random.walk")
6373
},
@@ -69,10 +79,10 @@ setGeneric(
6979
setMethod(
7080
"random.walk",
7181
signature = signature(p0="numeric", graph="matrix"),
72-
function(p0, graph, r=.5, ...)
82+
function(p0, graph, r=.5, niter=1e4, thresh=1e-4, do.analytical=FALSE, ...)
7383
{
7484
p0 <- as.matrix(p0, ncol=1)
75-
random.walk(p0, graph, r, ...)
85+
random.walk(p0, graph, r, niter, thresh, do.analytical, ...)
7686
}
7787
)
7888

@@ -81,23 +91,32 @@ setMethod(
8191
setMethod(
8292
"random.walk",
8393
signature = signature(p0="matrix", graph="matrix"),
84-
function(p0, graph, r=.5, ...)
94+
function(p0, graph, r=.5, niter=1e4, thresh=1e-4, do.analytical=FALSE, ...)
8595
{
8696
stopifnot(length(r) == 1)
8797
.check.restart(r)
8898
.check.starting.matrix(p0)
8999
.check.graph(graph, p0)
100+
90101
if (any(diag(graph) != 0))
91102
{
92103
message("setting diag of graph to zero")
93104
diag(graph) <- 0
94105
}
106+
95107
stoch.graph <- normalize.stochastic(graph)
96108
if(!.is.ergodic(stoch.graph))
97109
stop("the provided graph has more than one component. It is likely not ergodic.")
98-
invisible(mrwr_(normalize.stochastic(p0),
99-
stoch.graph,
100-
r)
101-
)
110+
111+
print(p0)
112+
print(stoch.graph)
113+
print(r)
114+
print(thresh)
115+
print(niter)
116+
print(do.analytical)
117+
118+
invisible(
119+
mrwr_(normalize.stochastic(p0),
120+
stoch.graph, r, thresh, niter, do.analytical))
102121
}
103122
)

R/util.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
stop(paste0("'", name.graph, "' has to be non-negative"))
5151
if (dim(m)[1] != dim(m)[2])
5252
stop(paste(name.graph, "has to be of dimension (n x n)!"))
53-
if (!is.null(v) && dim(m)[1] != length(v))
53+
if (!is.null(v) && dim(m)[1] != dim(v)[1])
5454
stop(paste(name.vector, "has to have same dimension as", name.graph))
5555
}
5656

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/insulated-diffusion-methods.Rd

Lines changed: 0 additions & 45 deletions
This file was deleted.

man/random-walk-methods.Rd

Lines changed: 32 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/heat_diffusion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ Eigen::MatrixXd heat_diffusion_(const Eigen::MatrixXd& v0,
4747
Eigen::VectorXd co = V.transpose() * v0;
4848
for (int i = 0; i < co.rows(); ++i)
4949
{
50-
for (int j = 0; j < co.cols(); ++j) {
50+
for (int j = 0; j < co.cols(); ++j)
51+
{
5152
if (j % 25 == 0) Rcpp::checkUserInterrupt();
5253
co(i, j) *= std::exp(-D(i) * t);
5354
}

src/mat_util.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
* along with diffusr. If not, see <http://www.gnu.org/licenses/>.
2121
*/
2222

23+
2324
// [[Rcpp::depends(RcppEigen)]]
2425
#include <RcppEigen.h>
26+
// [[Rcpp::plugins(cpp11)]]
2527
#include <cmath>
2628

2729
//' Column normalize a matrix, so that it is stochastic.
@@ -42,7 +44,7 @@ Eigen::MatrixXd stoch_col_norm_(const Eigen::MatrixXd& W)
4244
if ((W.col(i)).sum() <= zero_col) res.col(i).fill(empt_col_val);
4345
else res.col(i) = W.col(i) / colsums(i);
4446
}
45-
47+
4648
return res;
4749
}
4850

src/mrw.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// [[Rcpp::depends(RcppEigen)]]
2424
#include <RcppEigen.h>
2525
#include <vector>
26+
#include <iostream>
2627

2728
//' Do a Markon random walk (with restart) on an column-normalised adjacency
2829
//' matrix.
@@ -49,7 +50,7 @@ Eigen::MatrixXd mrwr_(const Eigen::MatrixXd& p0,
4950
Eigen::MatrixXd pt;
5051
if (!do_analytical)
5152
{
52-
Eigen::MatrixXd pt = p0;
53+
pt = p0;
5354
Eigen::MatrixXd pold;
5455
int iter = 0;
5556
do
@@ -62,8 +63,9 @@ Eigen::MatrixXd mrwr_(const Eigen::MatrixXd& p0,
6263
}
6364
else
6465
{
66+
Eigen::MatrixXd I = Eigen::MatrixXd::Identity(W.rows(), W.cols());
6567
Eigen::MatrixXd T = r * (I - (1 - r) * W).inverse();
66-
pt = T %*% p0;
68+
pt = T * p0;
6769
}
6870

6971
return pt;

0 commit comments

Comments
 (0)