Skip to content
Closed
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
86 changes: 86 additions & 0 deletions R/aaa-auto.R
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,68 @@ adjacency_impl <- function(
res
}

sparse_adjacency_impl <- function(
adjmatrix,
mode = c("directed", "undirected", "upper", "lower", "min", "plus", "max"),
loops = c("once", "none", "twice")
) {
# Argument checks
requireNamespace("Matrix", quietly = TRUE); adjmatrix <- as(as(as(adjmatrix, "dMatrix"), "generalMatrix"), "CsparseMatrix")
mode <- switch_igraph_arg(
mode,
"directed" = 0L,
"undirected" = 1L,
"upper" = 2L,
"lower" = 3L,
"min" = 4L,
"plus" = 5L,
"max" = 6L
)
loops <- switch_igraph_arg(loops, "none" = 0L, "twice" = 1L, "once" = 2L)

on.exit(.Call(R_igraph_finalizer))
# Function call
res <- .Call(
R_igraph_sparse_adjacency,
adjmatrix,
mode,
loops
)

res
}

sparse_weighted_adjacency_impl <- function(
adjmatrix,
mode = c("directed", "undirected", "upper", "lower", "min", "plus", "max"),
loops = c("once", "none", "twice")
) {
# Argument checks
requireNamespace("Matrix", quietly = TRUE); adjmatrix <- as(as(as(adjmatrix, "dMatrix"), "generalMatrix"), "CsparseMatrix")
mode <- switch_igraph_arg(
mode,
"directed" = 0L,
"undirected" = 1L,
"upper" = 2L,
"lower" = 3L,
"min" = 4L,
"plus" = 5L,
"max" = 6L
)
loops <- switch_igraph_arg(loops, "none" = 0L, "twice" = 1L, "once" = 2L)

on.exit(.Call(R_igraph_finalizer))
# Function call
res <- .Call(
R_igraph_sparse_weighted_adjacency,
adjmatrix,
mode,
loops
)

res
}

weighted_adjacency_impl <- function(
adjmatrix,
mode = c("directed", "undirected", "upper", "lower", "min", "plus", "max"),
Expand Down Expand Up @@ -1257,6 +1319,30 @@ turan_impl <- function(
res
}

weighted_sparsemat_impl <- function(
A,
directed,
attr,
loops = FALSE
) {
# Argument checks
requireNamespace("Matrix", quietly = TRUE); A <- as(as(as(A, "dMatrix"), "generalMatrix"), "CsparseMatrix")
directed <- as.logical(directed)
loops <- as.logical(loops)

on.exit(.Call(R_igraph_finalizer))
# Function call
res <- .Call(
R_igraph_weighted_sparsemat,
A,
directed,
attr,
loops
)

res
}

barabasi_game_impl <- function(
n,
power = 1.0,
Expand Down
6 changes: 6 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ extern SEXP R_igraph_sir(SEXP, SEXP, SEXP, SEXP);
extern SEXP R_igraph_site_percolation(SEXP, SEXP);
extern SEXP R_igraph_solve_lsap(SEXP, SEXP);
extern SEXP R_igraph_spanner(SEXP, SEXP, SEXP);
extern SEXP R_igraph_sparse_adjacency(SEXP, SEXP, SEXP);
extern SEXP R_igraph_sparse_weighted_adjacency(SEXP, SEXP, SEXP);
extern SEXP R_igraph_split_join_distance(SEXP, SEXP);
extern SEXP R_igraph_square_lattice(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP R_igraph_st_edge_connectivity(SEXP, SEXP, SEXP);
Expand Down Expand Up @@ -482,6 +484,7 @@ extern SEXP R_igraph_watts_strogatz_game(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP R_igraph_weighted_adjacency(SEXP, SEXP, SEXP);
extern SEXP R_igraph_weighted_clique_number(SEXP, SEXP);
extern SEXP R_igraph_weighted_cliques(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP R_igraph_weighted_sparsemat(SEXP, SEXP, SEXP, SEXP);
extern SEXP R_igraph_wheel(SEXP, SEXP, SEXP);
extern SEXP R_igraph_widest_path_widths_dijkstra(SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP R_igraph_widest_path_widths_floyd_warshall(SEXP, SEXP, SEXP, SEXP, SEXP);
Expand Down Expand Up @@ -1029,6 +1032,8 @@ static const R_CallMethodDef CallEntries[] = {
{"R_igraph_site_percolation", (DL_FUNC) &R_igraph_site_percolation, 2},
{"R_igraph_solve_lsap", (DL_FUNC) &R_igraph_solve_lsap, 2},
{"R_igraph_spanner", (DL_FUNC) &R_igraph_spanner, 3},
{"R_igraph_sparse_adjacency", (DL_FUNC) &R_igraph_sparse_adjacency, 3},
{"R_igraph_sparse_weighted_adjacency", (DL_FUNC) &R_igraph_sparse_weighted_adjacency, 3},
{"R_igraph_split_join_distance", (DL_FUNC) &R_igraph_split_join_distance, 2},
{"R_igraph_square_lattice", (DL_FUNC) &R_igraph_square_lattice, 5},
{"R_igraph_st_edge_connectivity", (DL_FUNC) &R_igraph_st_edge_connectivity, 3},
Expand Down Expand Up @@ -1076,6 +1081,7 @@ static const R_CallMethodDef CallEntries[] = {
{"R_igraph_weighted_adjacency", (DL_FUNC) &R_igraph_weighted_adjacency, 3},
{"R_igraph_weighted_clique_number", (DL_FUNC) &R_igraph_weighted_clique_number, 2},
{"R_igraph_weighted_cliques", (DL_FUNC) &R_igraph_weighted_cliques, 5},
{"R_igraph_weighted_sparsemat", (DL_FUNC) &R_igraph_weighted_sparsemat, 4},
{"R_igraph_wheel", (DL_FUNC) &R_igraph_wheel, 3},
{"R_igraph_widest_path_widths_dijkstra", (DL_FUNC) &R_igraph_widest_path_widths_dijkstra, 5},
{"R_igraph_widest_path_widths_floyd_warshall", (DL_FUNC) &R_igraph_widest_path_widths_floyd_warshall, 5},
Expand Down
154 changes: 132 additions & 22 deletions src/rinterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,96 @@ SEXP R_igraph_adjacency(SEXP adjmatrix, SEXP mode, SEXP loops) {
return(r_result);
}

/*-------------------------------------------/
/ igraph_sparse_adjacency /
/-------------------------------------------*/
SEXP R_igraph_sparse_adjacency(SEXP adjmatrix, SEXP mode, SEXP loops) {
/* Declarations */
igraph_t c_graph;
igraph_sparsemat_t c_adjmatrix;
igraph_adjacency_t c_mode;
igraph_loops_t c_loops;
SEXP graph;

SEXP r_result, r_names;
/* Convert input */
Rz_SEXP_to_sparsemat(adjmatrix, &c_adjmatrix);
c_mode = (igraph_adjacency_t) Rf_asInteger(mode);
c_loops = (igraph_loops_t) Rf_asInteger(loops);
/* Call igraph */
IGRAPH_R_CHECK(igraph_sparse_adjacency(&c_graph, &c_adjmatrix, c_mode, c_loops));

/* Convert output */
PROTECT(r_result=NEW_LIST(2));
PROTECT(r_names=NEW_CHARACTER(2));
IGRAPH_FINALLY(igraph_destroy, &c_graph);
PROTECT(graph=Ry_igraph_to_SEXP(&c_graph));
IGRAPH_I_DESTROY(&c_graph);
IGRAPH_FINALLY_CLEAN(1);
PROTECT(adjmatrix=Ry_igraph_sparsemat_to_SEXP(&c_adjmatrix));
igraph_sparsemat_destroy(&c_adjmatrix);
IGRAPH_FINALLY_CLEAN(1);
SET_VECTOR_ELT(r_result, 0, graph);
SET_VECTOR_ELT(r_result, 1, adjmatrix);
SET_STRING_ELT(r_names, 0, Rf_mkChar("graph"));
SET_STRING_ELT(r_names, 1, Rf_mkChar("adjmatrix"));
SET_NAMES(r_result, r_names);
UNPROTECT(3);

UNPROTECT(1);
return(r_result);
}

/*-------------------------------------------/
/ igraph_sparse_weighted_adjacency /
/-------------------------------------------*/
SEXP R_igraph_sparse_weighted_adjacency(SEXP adjmatrix, SEXP mode, SEXP loops) {
/* Declarations */
igraph_t c_graph;
igraph_sparsemat_t c_adjmatrix;
igraph_adjacency_t c_mode;
igraph_vector_t c_weights;
igraph_loops_t c_loops;
SEXP graph;
SEXP weights;

SEXP r_result, r_names;
/* Convert input */
Rz_SEXP_to_sparsemat(adjmatrix, &c_adjmatrix);
c_mode = (igraph_adjacency_t) Rf_asInteger(mode);
IGRAPH_R_CHECK(igraph_vector_init(&c_weights, 0));
IGRAPH_FINALLY(igraph_vector_destroy, &c_weights);
weights=R_GlobalEnv; /* hack to have a non-NULL value */
c_loops = (igraph_loops_t) Rf_asInteger(loops);
/* Call igraph */
IGRAPH_R_CHECK(igraph_sparse_weighted_adjacency(&c_graph, &c_adjmatrix, c_mode, &c_weights, c_loops));

/* Convert output */
PROTECT(r_result=NEW_LIST(3));
PROTECT(r_names=NEW_CHARACTER(3));
IGRAPH_FINALLY(igraph_destroy, &c_graph);
PROTECT(graph=Ry_igraph_to_SEXP(&c_graph));
IGRAPH_I_DESTROY(&c_graph);
IGRAPH_FINALLY_CLEAN(1);
PROTECT(adjmatrix=Ry_igraph_sparsemat_to_SEXP(&c_adjmatrix));
igraph_sparsemat_destroy(&c_adjmatrix);
IGRAPH_FINALLY_CLEAN(1);
PROTECT(weights=Ry_igraph_0orvector_to_SEXP(&c_weights));
igraph_vector_destroy(&c_weights);
IGRAPH_FINALLY_CLEAN(1);
SET_VECTOR_ELT(r_result, 0, graph);
SET_VECTOR_ELT(r_result, 1, adjmatrix);
SET_VECTOR_ELT(r_result, 2, weights);
SET_STRING_ELT(r_names, 0, Rf_mkChar("graph"));
SET_STRING_ELT(r_names, 1, Rf_mkChar("adjmatrix"));
SET_STRING_ELT(r_names, 2, Rf_mkChar("weights"));
SET_NAMES(r_result, r_names);
UNPROTECT(4);

UNPROTECT(1);
return(r_result);
}

/*-------------------------------------------/
/ igraph_weighted_adjacency /
/-------------------------------------------*/
Expand Down Expand Up @@ -1775,6 +1865,40 @@ SEXP R_igraph_turan(SEXP n, SEXP r) {
return(r_result);
}

/*-------------------------------------------/
/ igraph_weighted_sparsemat /
/-------------------------------------------*/
SEXP R_igraph_weighted_sparsemat(SEXP A, SEXP directed, SEXP attr, SEXP loops) {
/* Declarations */
igraph_t c_graph;
igraph_sparsemat_t c_A;
igraph_bool_t c_directed;
const char* c_attr;
igraph_bool_t c_loops;
SEXP graph;

SEXP r_result;
/* Convert input */
Rz_SEXP_to_sparsemat(A, &c_A);
IGRAPH_R_CHECK_BOOL(directed);
c_directed = LOGICAL(directed)[0];
c_attr = Rf_translateCharUTF8(STRING_ELT(attr, 0));
IGRAPH_R_CHECK_BOOL(loops);
c_loops = LOGICAL(loops)[0];
/* Call igraph */
IGRAPH_R_CHECK(igraph_weighted_sparsemat(&c_graph, &c_A, c_directed, c_attr, c_loops));

/* Convert output */
IGRAPH_FINALLY(igraph_destroy, &c_graph);
PROTECT(graph=Ry_igraph_to_SEXP(&c_graph));
IGRAPH_I_DESTROY(&c_graph);
IGRAPH_FINALLY_CLEAN(1);
r_result = graph;

UNPROTECT(1);
return(r_result);
}

/*-------------------------------------------/
/ igraph_barabasi_game /
/-------------------------------------------*/
Expand Down Expand Up @@ -13267,15 +13391,12 @@ SEXP R_igraph_read_graph_edgelist(SEXP instream, SEXP n, SEXP directed) {
/-------------------------------------------*/
SEXP R_igraph_read_graph_ncol(SEXP instream, SEXP predefnames, SEXP names, SEXP weights, SEXP directed) {
/* Declarations */
igraph_t c_graph;
FILE* c_instream;
igraph_strvector_t c_predefnames;
igraph_bool_t c_names;
igraph_add_weights_t c_weights;
igraph_bool_t c_directed;
SEXP graph;

SEXP r_result;
/* Convert input */
c_instream = Ry_igraph_fopen_read(instream);
IGRAPH_FINALLY(fclose, c_instream);
Expand All @@ -13288,32 +13409,25 @@ SEXP R_igraph_read_graph_ncol(SEXP instream, SEXP predefnames, SEXP names, SEXP
IGRAPH_R_CHECK_BOOL(directed);
c_directed = LOGICAL(directed)[0];
/* Call igraph */
IGRAPH_R_CHECK(igraph_read_graph_ncol(&c_graph, c_instream, (Rf_isNull(predefnames) ? 0 : &c_predefnames), c_names, c_weights, c_directed));
IGRAPH_R_CHECK(igraph_read_graph_ncol(c_instream, (Rf_isNull(predefnames) ? 0 : &c_predefnames), c_names, c_weights, c_directed));

/* Convert output */
IGRAPH_FINALLY(igraph_destroy, &c_graph);
PROTECT(graph=Ry_igraph_to_SEXP(&c_graph));
IGRAPH_I_DESTROY(&c_graph);
IGRAPH_FINALLY_CLEAN(1);
r_result = graph;

UNPROTECT(1);
return(r_result);


return(R_NilValue);
}

/*-------------------------------------------/
/ igraph_read_graph_lgl /
/-------------------------------------------*/
SEXP R_igraph_read_graph_lgl(SEXP instream, SEXP names, SEXP weights, SEXP directed) {
/* Declarations */
igraph_t c_graph;
FILE* c_instream;
igraph_bool_t c_names;
igraph_add_weights_t c_weights;
igraph_bool_t c_directed;
SEXP graph;

SEXP r_result;
/* Convert input */
c_instream = Ry_igraph_fopen_read(instream);
IGRAPH_FINALLY(fclose, c_instream);
Expand All @@ -13323,17 +13437,13 @@ SEXP R_igraph_read_graph_lgl(SEXP instream, SEXP names, SEXP weights, SEXP direc
IGRAPH_R_CHECK_BOOL(directed);
c_directed = LOGICAL(directed)[0];
/* Call igraph */
IGRAPH_R_CHECK(igraph_read_graph_lgl(&c_graph, c_instream, c_names, c_weights, c_directed));
IGRAPH_R_CHECK(igraph_read_graph_lgl(c_instream, c_names, c_weights, c_directed));

/* Convert output */
IGRAPH_FINALLY(igraph_destroy, &c_graph);
PROTECT(graph=Ry_igraph_to_SEXP(&c_graph));
IGRAPH_I_DESTROY(&c_graph);
IGRAPH_FINALLY_CLEAN(1);
r_result = graph;

UNPROTECT(1);
return(r_result);


return(R_NilValue);
}

/*-------------------------------------------/
Expand Down
1 change: 1 addition & 0 deletions src/rinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ igraph_error_t Rz_SEXP_to_vector_bool_copy(SEXP sv, igraph_vector_bool_t *v);
igraph_error_t Rz_SEXP_to_vector_int_copy(SEXP sv, igraph_vector_int_t *v);
igraph_error_t Rz_SEXP_to_hrg(SEXP shrg, igraph_hrg_t *hrg);
igraph_error_t Rz_SEXP_to_hrg_copy(SEXP shrg, igraph_hrg_t *hrg);
void Rz_SEXP_to_sparsemat(SEXP sm, igraph_sparsemat_t *res);
void Rz_SEXP_to_igraph_layout_drl_options(SEXP in, igraph_layout_drl_options_t *opt);
igraph_error_t Rz_SEXP_to_igraph_eigen_which(SEXP in, igraph_eigen_which_t *out);
void Rz_SEXP_to_igraph_arpack_options(SEXP in, igraph_arpack_options_t *opt);
Expand Down
Loading
Loading