File: other.R

package info (click to toggle)
r-cran-igraph 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,044 kB
  • sloc: ansic: 204,981; cpp: 21,711; fortran: 4,090; yacc: 1,229; lex: 519; sh: 52; makefile: 8
file content (212 lines) | stat: -rw-r--r-- 6,879 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

#' Running mean of a time series
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `running.mean()` was renamed to `running_mean()` to create a more
#' consistent API.
#' @inheritParams running_mean
#' @keywords internal
#' @export
running.mean <- function(v, binwidth) { # nocov start
  lifecycle::deprecate_soft("2.0.0", "running.mean()", "running_mean()")
  running_mean(v = v, binwidth = binwidth)
} # nocov end

#' Sampling a random integer sequence
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `igraph.sample()` was renamed to `sample_seq()` to create a more
#' consistent API.
#' @inheritParams sample_seq
#' @keywords internal
#' @export
igraph.sample <- function(low, high, length) { # nocov start
  lifecycle::deprecate_soft("2.0.0", "igraph.sample()", "sample_seq()")
  sample_seq(low = low, high = high, length = length)
} # nocov end

#' Convex hull of a set of vertices
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `convex.hull()` was renamed to `convex_hull()` to create a more
#' consistent API.
#' @inheritParams convex_hull
#' @keywords internal
#' @export
convex.hull <- function(data) { # nocov start
  lifecycle::deprecate_soft("2.0.0", "convex.hull()", "convex_hull()")
  convex_hull(data = data)
} # nocov end
#   IGraph R package
#   Copyright (C) 2005-2012  Gabor Csardi <csardi.gabor@gmail.com>
#   334 Harvard street, Cambridge, MA 02139 USA
#
#   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
#
###################################################################



#' Running mean of a time series
#'
#' `running_mean()` calculates the running mean in a vector with the given
#' bin width.
#'
#' The running mean of `v` is a `w` vector of length
#' `length(v)-binwidth+1`. The first element of `w` id the average of
#' the first `binwidth` elements of `v`, the second element of
#' `w` is the average of elements `2:(binwidth+1)`, etc.
#'
#' @param v The numeric vector.
#' @param binwidth Numeric constant, the size of the bin, should be meaningful,
#'   i.e. smaller than the length of `v`.
#' @return A numeric vector of length `length(v)-binwidth+1`
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
#' @family other
#' @export
#' @keywords manip
#' @examples
#'
#' running_mean(1:100, 10)
#'
running_mean <- function(v, binwidth) {
  v <- as.numeric(v)
  binwidth <- as.numeric(binwidth)
  if (length(v) < binwidth) {
    stop("Vector too short for this binwidth.")
  }

  on.exit(.Call(R_igraph_finalizer))
  .Call(R_igraph_running_mean, v, binwidth)
}



#' Sampling a random integer sequence
#'
#' This function provides a very efficient way to pull an integer random sample
#' sequence from an integer interval.
#'
#' The algorithm runs in `O(length)` expected time, even if
#' `high-low` is big. It is much faster (but of course less general) than
#' the builtin `sample` function of R.
#'
#' @param low The lower limit of the interval (inclusive).
#' @param high The higher limit of the interval (inclusive).
#' @param length The length of the sample.
#' @return An increasing numeric vector containing integers, the sample.
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
#' @references Jeffrey Scott Vitter: An Efficient Algorithm for Sequential
#' Random Sampling, *ACM Transactions on Mathematical Software*, 13/1,
#' 58--67.
#' @family other
#' @export
#' @keywords datagen
#' @examples
#'
#' rs <- sample_seq(1, 100000000, 10)
#' rs
#'
sample_seq <- function(low, high, length) {
  if (length > high - low + 1) {
    stop("length too big for this interval")
  }

  on.exit(.Call(R_igraph_finalizer))
  .Call(
    R_igraph_random_sample, as.numeric(low), as.numeric(high),
    as.numeric(length)
  )
}

#' Common handler for vertex type arguments in igraph functions
#'
#' This function takes the `types` and `graph` arguments from a
#' public igraph function call and validates the vertex type vector.
#'
#' When the provided vertex types are NULL and the graph has a `types`
#' vertex attribute, then the value of this vertex attribute will be used as
#' vertex types. Non-logical vertex type vectors are coerced into logical
#' vectors after printing a warning.
#'
#' @param types the vertex types
#' @param graph the graph
#' @param required whether the graph has to be bipartite
#' @return A logical vector representing the resolved vertex type for each
#'   vertex in the graph
#' @author Tamas Nepusz \email{ntamas@@gmail.com}
#' @dev
#'
handle_vertex_type_arg <- function(types, graph, required = T) {
  if (is.null(types) && "type" %in% vertex_attr_names(graph)) {
    types <- V(graph)$type
  }
  if (!is.null(types)) {
    if (!is.logical(types)) {
      cli::cli_warn("vertex types converted to logical.")
    }
    types <- as.logical(types)
    if (any(is.na(types))) {
      stop("`NA' is not allowed in vertex types")
    }
  }
  if (is.null(types) && required) {
    stop("Not a bipartite graph, supply `types' argument or add a vertex attribute named `type'")
  }
  return(types)
}

igraph.i.spMatrix <- function(M) {
  if (M$type == "triplet") {
    Matrix::sparseMatrix(dims = M$dim, i = M$i + 1L, j = M$p + 1L, x = M$x)
  } else {
    new("dgCMatrix",
      Dim = M$dim, Dimnames = list(NULL, NULL),
      factors = list(), i = M$i, p = M$p, x = M$x
    )
  }
}

#' Convex hull of a set of vertices
#'
#' Calculate the convex hull of a set of points, i.e. the covering polygon that
#' has the smallest area.
#'
#'
#' @param data The data points, a numeric matrix with two columns.
#' @return A named list with components: \item{resverts}{The indices of the
#'   input vertices that constritute the convex hull.} \item{rescoords}{The
#'   coordinates of the corners of the convex hull.}
#' @author Tamas Nepusz \email{ntamas@@gmail.com}
#' @references Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and
#' Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and
#' McGraw-Hill, 2001. ISBN 0262032937. Pages 949-955 of section 33.3: Finding
#' the convex hull.
#' @keywords graphs
#' @examples
#'
#' M <- cbind(runif(100), runif(100))
#' convex_hull(M)
#' @family other
#' @export
#' @cdocs igraph_convex_hull
convex_hull <- convex_hull_impl