Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ export(make_neighborhood_graph)
export(make_ring)
export(make_star)
export(make_tree)
export(make_tri_lattice)
export(make_undirected_graph)
export(match_vertices)
export(max_bipartite_match)
Expand Down Expand Up @@ -867,6 +868,7 @@ export(traits)
export(traits_callaway)
export(transitivity)
export(tree)
export(tri_lattice)
export(triad.census)
export(triad_census)
export(triangles)
Expand Down
60 changes: 60 additions & 0 deletions R/make.R
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,66 @@ lattice <- function(...) constructor_spec(make_lattice, ...)

## -----------------------------------------------------------------

#' Create a triangular lattice graph
#'
#' `make_tri_lattice()` creates a triangular lattice with a specified shape.
#' The shape can be triangular (1 dimension), quasi-rectangular (2 dimensions),
#' or hexagonal (3 dimensions).
#'
#' @concept Lattice
#' @param dims Integer vector defining the shape of the lattice.
#' - If `dims` is of length 1, the resulting lattice has a triangular shape
#' where each side of the triangle contains `dims[1]` vertices.
#' - If `dims` is of length 2, the resulting lattice has a "quasi rectangular"
#' shape with the sides containing `dims[1]` and `dims[2]` vertices, respectively.
#' - If `dims` is of length 3, the resulting lattice has a hexagonal shape
#' where the sides of the hexagon contain `dims[1]`, `dims[2]`, and `dims[3]`
#' vertices.
#' All dimensions must be non-negative.
#' @param directed Logical scalar, whether to create a directed graph.
#' If the `mutual` argument is not set to `TRUE`, edges will be directed
#' from lower-index vertices towards higher-index ones.
#' @param mutual Logical scalar, if the graph is directed, this gives whether
#' to create all connections as mutual.
#' @return An igraph graph.
#'
#' @family deterministic constructors
#' @export
#' @examples
#' # Triangular lattice (1D case - creates a triangle)
#' g1 <- make_tri_lattice(5)
#' plot(g1)
#'
#' # Quasi-rectangular lattice (2D case)
#' g2 <- make_tri_lattice(c(3, 4))
#' plot(g2)
#'
#' # Hexagonal lattice (3D case)
#' g3 <- make_tri_lattice(c(3, 3, 3))
#' plot(g3)
#'
#' # Directed triangular lattice with mutual edges
#' g4 <- make_tri_lattice(c(3, 3), directed = TRUE, mutual = TRUE)
#' @cdocs igraph_triangular_lattice
make_tri_lattice <- function(dims, directed = FALSE, mutual = FALSE) {
on.exit(.Call(R_igraph_finalizer))
res <- triangular_lattice_impl(dims, directed, mutual)
if (igraph_opt("add.params")) {
res$name <- "Triangular lattice"
res$dims <- dims
res$directed <- directed
res$mutual <- mutual
}
res
}

#' @rdname make_tri_lattice
#' @param ... Passed to `make_tri_lattice()`.
#' @export
tri_lattice <- function(...) constructor_spec(make_tri_lattice, ...)

## -----------------------------------------------------------------

#' Create a ring graph
#'
#' A ring is a one-dimensional lattice and this function is a special case
Expand Down
3 changes: 2 additions & 1 deletion man/graph_from_atlas.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/graph_from_edgelist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/graph_from_literal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_chordal_ring.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_empty_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_full_citation_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_full_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_lattice.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_ring.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_star.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/make_tree.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions man/make_tri_lattice.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/aaa-auto.new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# version_impl errors

Code
x
Condition
Error in `version_impl()`:
! could not find function "version_impl"

41 changes: 41 additions & 0 deletions tests/testthat/test-make.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,47 @@ test_that("make_lattice prints a warning for fractional length)", {
expect_identical_graphs(lattice_rounded, lattice_integer)
})

test_that("make_tri_lattice works with 1D (triangular shape)", {
g <- make_tri_lattice(3)
expect_vcount(g, 6)
expect_ecount(g, 9)
expect_false(is_directed(g))
})

test_that("make_tri_lattice works with 2D (quasi-rectangular shape)", {
g <- make_tri_lattice(c(3, 3))
expect_vcount(g, 9)
expect_ecount(g, 16)
expect_false(is_directed(g))
})

test_that("make_tri_lattice works with 3D (hexagonal shape)", {
g <- make_tri_lattice(c(2, 2, 2))
expect_vcount(g, 7)
expect_ecount(g, 12)
expect_false(is_directed(g))
})

test_that("make_tri_lattice works with directed and mutual parameters", {
g_undirected <- make_tri_lattice(c(2, 2))
g_directed <- make_tri_lattice(c(2, 2), directed = TRUE)
g_mutual <- make_tri_lattice(c(2, 2), directed = TRUE, mutual = TRUE)

expect_false(is_directed(g_undirected))
expect_true(is_directed(g_directed))
expect_true(is_directed(g_mutual))

# Mutual should have twice the edges
expect_equal(ecount(g_mutual), 2 * ecount(g_undirected))
})

test_that("tri_lattice constructor spec works with make_()", {
g1 <- make_tri_lattice(c(3, 3))
g2 <- make_(tri_lattice(c(3, 3)))

expect_identical_graphs(g1, g2)
})

test_that("make_graph works", {
graph_make <- make_graph(1:10)
graph_elist <- make_empty_graph(n = 10) + edges(1:10)
Expand Down
Binary file modified tests/testthat/testthat-problems.rds
Binary file not shown.
Loading