Title: | Utility Functions for Scientific Coding |
---|---|
Description: | Basic routines used in scientific coding, such as timing routines, vector/array handing functions and I/O support routines. |
Authors: | Danail Obreschkow |
Maintainer: | Danail Obreschkow <[email protected]> |
License: | GPL-3 |
Version: | 1.0 |
Built: | 2025-04-01 03:15:32 UTC |
Source: | https://github.com/cran/docore |
Circulates each dimension of an array. This routine is identical to circshift
, but works with arrays up to 5 dimensions.
cshift(x, s)
cshift(x, s)
x |
vector or array (up to rank 5) |
s |
scalar, if x is a vector, or a vector of length matching the rank of x, if x is an array |
Returns a vector or array of the same shape as x.
Danail Obreschkow
Returns the last element of a vector or the n-th element counting from the end of a vector.
last(x, n = 1)
last(x, n = 1)
x |
vector |
n |
optional integer specifying the n-th element from the end to be returned |
scalar of the same type as x
Danail Obreschkow
limits the values of a vector or array to a desired interval, while keeping the shape of the vector/array
lim(x, min = 0, max = 1, clip = NULL, na = NULL)
lim(x, min = 0, max = 1, clip = NULL, na = NULL)
x |
vector or array |
min |
minimum value |
max |
maximum value |
clip |
optional value specifying the value assigned to clipped data, e.g. |
na |
optional value specifying the value assigned to non-numbers (NA and NaN) |
vector/array of the same shape as x
Danail Obreschkow
Convert spaces in filenames (" ") to linux-type spaces "\ ", needed when calling system() on macOS.
linuxspaces(txt)
linuxspaces(txt)
txt |
filename, which may contain ordinary spaces, e.g. "my file 1.txt" |
filename with modified spaces, e.g. "my\ file\ 1.txt"
Danail Obreschkow
filename = '~/Desktop/my file 1.txt' command = sprintf('ls -l %s',linuxspaces(filename)) ## Not run: system(command) ## End(Not run)
filename = '~/Desktop/my file 1.txt' command = sprintf('ls -l %s',linuxspaces(filename)) ## Not run: system(command) ## End(Not run)
Reads binary data using the base function readBin
and recasts it into an array of custom dimensions.
loadbin( filename, dim, bytes = 4, type = "numeric", signed = FALSE, endian = "little" )
loadbin( filename, dim, bytes = 4, type = "numeric", signed = FALSE, endian = "little" )
filename |
path of the file to be loaded |
dim |
vector specifying the dimensions of the array |
bytes |
number of bytes per number in the binary file |
type |
character vector of length describing the data type: "numeric" (default), "double", "integer", "int", "logical", "complex", "character", "raw" |
signed |
logical. Only used for integers of sizes 1 and 2, when it determines if the quantity on file should be regarded as a signed or unsigned integer. |
endian |
endian-type ("big" or "little") of the file |
Returns an array of dimension dim.
Danail Obreschkow
compute the mid-point positions of a one-dimensional regular grid of n equal intervals.
midseq(min, max, n = 1)
midseq(min, max, n = 1)
min |
left boundary of first bin |
max |
right boundary of last bin |
n |
number of bins |
vector of mid points
Danail Obreschkow
Runs any routine or command while supressing in-routine console output
quiet(x)
quiet(x)
x |
routine to be called |
Returns whatever the called routine returns in invisible form.
Danail Obreschkow
# Test function test = function(x) { cat('This routine is likes to talk a lot!\n') return(x^2) } # Standard call call: y = test(5) print(y) # Quiet call: y = quiet(test(6)) print(y)
# Test function test = function(x) { cat('This routine is likes to talk a lot!\n') return(x^2) } # Standard call call: y = test(5) print(y) # Quiet call: y = quiet(test(6)) print(y)
Start timer and write a custom text into the console.
tick(txt = "Start")
tick(txt = "Start")
txt |
custom text |
None
Danail Obreschkow
tick('Sum 10 million random numbers') x = sum(runif(1e7)) tock()
tick('Sum 10 million random numbers') x = sum(runif(1e7)) tock()
Stop timer and write the computation in seconds since the last call of tick().
tock(txt = "")
tock(txt = "")
txt |
optional custom text to be displayed |
None
Danail Obreschkow
tick('Sum 10 million random numbers') x = sum(runif(1e7)) tock()
tick('Sum 10 million random numbers') x = sum(runif(1e7)) tock()
Turns a 64-bit integers into unique doubles for faster comparison. The output double values are completely different from the input values.
uniquedouble(int64)
uniquedouble(int64)
int64 |
input value (normally used with 64-bit integers, but also works with other types) |
Returns a double floating point value.
Danail Obreschkow
# The comparison of in-built types is very fast: int32 = as.integer(0) # (same as int32 = 0) system.time(for(i in seq(1e4)) comparison=int32==int32) # The comparison of 64-bit integers is very slow: int64 = bit64::as.integer64(0) system.time(for(i in seq(1e4)) comparison=int64==int64) # The comparison of converted 64-bit integers is again fast: int64d = uniquedouble(int64) system.time(for(i in seq(1e4)) comparison=int64d==int64d)
# The comparison of in-built types is very fast: int32 = as.integer(0) # (same as int32 = 0) system.time(for(i in seq(1e4)) comparison=int32==int32) # The comparison of 64-bit integers is very slow: int64 = bit64::as.integer64(0) system.time(for(i in seq(1e4)) comparison=int64==int64) # The comparison of converted 64-bit integers is again fast: int64d = uniquedouble(int64) system.time(for(i in seq(1e4)) comparison=int64d==int64d)