Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 31 additions & 51 deletions R/centrality.R
Original file line number Diff line number Diff line change
Expand Up @@ -1760,11 +1760,12 @@ bonpow.dense <- function(
loops = FALSE,
exponent = 1,
rescale = FALSE,
tol = 1e-7
tol = 1e-7,
weights = NULL
) {
ensure_igraph(graph)

d <- as_adjacency_matrix(graph)
d <- as_adjacency_matrix(graph, weights = weights, sparse = FALSE)
if (!loops) {
diag(d) <- 0
}
Expand All @@ -1788,7 +1789,8 @@ bonpow.sparse <- function(
loops = FALSE,
exponent = 1,
rescale = FALSE,
tol = 1e-07
tol = 1e-07,
weights = NULL
) {
## remove loops if requested
if (!loops) {
Expand All @@ -1798,13 +1800,13 @@ bonpow.sparse <- function(
vg <- vcount(graph)

## sparse adjacency matrix
d <- as_adjacency_matrix(graph, sparse = TRUE)
d <- as_adjacency_matrix(graph, weights = weights, sparse = TRUE)

## sparse identity matrix
id <- as(Matrix::Matrix(diag(vg), doDiag = FALSE), "generalMatrix")

## solve it
ev <- Matrix::solve(id - exponent * d, degree(graph, mode = "out"), tol = tol)
ev <- Matrix::solve(id - exponent * d, Matrix::rowSums(d), tol = tol)

if (rescale) {
ev <- ev / sum(ev)
Expand Down Expand Up @@ -1884,6 +1886,7 @@ bonpow.sparse <- function(
#' @param sparse Logical scalar, whether to use sparse matrices for the
#' calculation. The \sQuote{Matrix} package is required for sparse matrix
#' support
#' @inheritParams as_adjacency_matrix
#' @return A vector, containing the centrality scores.
#' @note This function was ported (i.e. copied) from the SNA package.
#' @section Warning : Singular adjacency matrices cause no end of headaches for
Expand Down Expand Up @@ -1936,13 +1939,30 @@ power_centrality <- function(
exponent = 1,
rescale = FALSE,
tol = 1e-7,
sparse = TRUE
sparse = TRUE,
weights = NULL
) {
nodes <- as_igraph_vs(graph, nodes)
if (sparse) {
res <- bonpow.sparse(graph, nodes, loops, exponent, rescale, tol)
res <- bonpow.sparse(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since bonpow.sparse() is not exported, could we give it a better name? Or do we want to keep dot names for unexported functions?

graph,
nodes,
loops,
exponent,
rescale,
tol,
weights = weights
)
} else {
res <- bonpow.dense(graph, nodes, loops, exponent, rescale, tol)
res <- bonpow.dense(
graph,
nodes,
loops,
exponent,
rescale,
tol,
weights = weights
)
}

if (igraph_opt("add.vertex.names") && is_named(graph)) {
Expand All @@ -1966,25 +1986,7 @@ alpha.centrality.dense <- function(
exo <- rep(exo, length.out = vcount(graph))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does exo mean? In my French-speaking head it's an abbreviation of exercise so I'm confused.

exo <- matrix(exo, ncol = 1)

if (is.null(weights) && "weight" %in% edge_attr_names(graph)) {
## weights == NULL and there is a "weight" edge attribute
attr <- "weight"
} else if (is.null(weights)) {
## weights == NULL, but there is no "weight" edge attribute
attr <- NULL
} else if (is.character(weights) && length(weights) == 1) {
## name of an edge attribute, nothing to do
attr <- weights
} else if (!all(is.na(weights))) {
## weights != NULL and weights != rep(NA, x)
graph <- set_edge_attr(graph, "weight", value = as.numeric(weights))
attr <- "weight"
} else {
## weights != NULL, but weights == rep(NA, x)
attr <- NULL
}

d <- t(as_adjacency_matrix(graph, attr = attr, sparse = FALSE))
d <- t(as_adjacency_matrix(graph, weights = weights, sparse = FALSE))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better name than d?

if (!loops) {
diag(d) <- 0
}
Expand Down Expand Up @@ -2013,25 +2015,7 @@ alpha.centrality.sparse <- function(
graph <- simplify(graph, remove.multiple = FALSE, remove.loops = TRUE)
}

if (is.null(weights) && "weight" %in% edge_attr_names(graph)) {
## weights == NULL and there is a "weight" edge attribute
attr <- "weight"
} else if (is.null(weights)) {
## weights == NULL, but there is no "weight" edge attribute
attr <- NULL
} else if (is.character(weights) && length(weights) == 1) {
## name of an edge attribute, nothing to do
attr <- weights
} else if (!all(is.na(weights))) {
## weights != NULL and weights != rep(NA, x)
graph <- set_edge_attr(graph, "weight", value = as.numeric(weights))
attr <- "weight"
} else {
## weights != NULL, but weights == rep(NA, x)
attr <- NULL
}

M <- Matrix::t(as_adjacency_matrix(graph, attr = attr, sparse = TRUE))
M <- Matrix::t(as_adjacency_matrix(graph, weights = weights, sparse = TRUE))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or are these single letters sort of convention?


## Create an identity matrix
M2 <- Matrix::sparseMatrix(
Expand Down Expand Up @@ -2082,11 +2066,7 @@ alpha.centrality.sparse <- function(
#' the same factor for every node, or a vector giving the factor for every
#' vertex. Note that too long vectors will be truncated and too short vectors
#' will be replicated to match the number of vertices.
#' @param weights A character scalar that gives the name of the edge attribute
#' to use in the adjacency matrix. If it is `NULL`, then the
#' \sQuote{weight} edge attribute of the graph is used, if there is one.
#' Otherwise, or if it is `NA`, then the calculation uses the standard
#' adjacency matrix.
#' @inheritParams as_adjacency_matrix
#' @param tol Tolerance for near-singularities during matrix inversion, see
#' [solve()].
#' @param sparse Logical scalar, whether to use sparse matrices for the
Expand Down
Loading
Loading