Inefficient sorting in R

Somewhat inspired by this beautiful video of computer sorting algorithms, I set out to write my own. Knowing full well that I would likely discover a method that performs worse, I leaned into the wind.

My sorting algorithm takes a random arrangement of the integers up to the vector’s length and rearranges the original list in this order, repeating until the list is in order.

At worst, this should perform in the order of O(n!)!

 inefficient_sort <- function(x) {
 
    if (length(x) == 1) {
        return(x)
    }
 
    solve <- x
    in_order <- all(solve[1:(length(x) - 1)] < solve[2:length(x)])
 
    while (!in_order) {
        solve <- x[sample(x = 1:length(x), size = length(x),
                          replace = FALSE)]
        in_order <- all(solve[1:(length(x) - 1)] < solve[2:length(x)])
    }
 
    return(solve)
 
}

Using the microbenchmark package, this is, on average, circa 300,000 times slower than the default sort function.

library(microbenchmark)
 
microbenchmark(inefficient_sort(x), sort(x), times = 10)
Unit: microseconds
                 expr         min          lq         mean      median           uq          max
 inefficient_order(x) 3454323.589 5831976.813 25708513.570 9451126.667 32137622.969 96802481.587
              sort(x)      48.641      65.599       96.256      96.613      132.982      137.444
 neval
    10
    10
Still works though.

Extending the incremental operator

I wanted to extend the infix incremental operator to allow it to be equivalent to either the += operator from python or the eponymous ++ operator from C++ where the operator would add one to the current value if a second argument wasn’t entered.

 pp <- function(x, y) {
    if (missing(y)) {
        eval.parent(substitute(x <- (x + 1)))
    } else {
        eval.parent(substitute(x <- x + y))
    }
}

This gives:

 x <- 1
pp(x)
[1] 2
 
pp(x, 10)
[1] 12

What I would really love is an infix operator that can run when one of the two required arguments are missing, ie, a world in which this code runs:

 `%+%` <- function(x, y) {
    if (missing(y)) {
        eval.parent(substitute(x <- (x + 1)))
    } else {
        eval.parent(substitute(x <- x + y))
    }
}
 
x <- 1
x %+% 
x
[1] 2

Visualising the Riemann Zeta Function

I was playing around with the Riemann zeta function from the pracma package – trying to find the best way to visualise it and specifically, its zeros. The zeta function, of course, is given by:

     \begin{equation*}\begin{aligned} &\zeta(z) = \sum_{n=1}^{\infty} \frac{1}{n^{z}}\\ \end{aligned}\end{equation}

All non-trivial values of \zeta(z) exist for z with Re(z)=0.5. The way I choose to visualise it was the polar plot of \zeta(z) for values Im(z) \in (0,100) and Re(z)=0.5.

library(pracma)
library(ggplot2)
library(gganimate)
library(dplyr)
 
#### Create complex set vector ####
    # Imaginary parts
    y <- seq(0, 100, len = 10001)
 
    # Complex vector
    z <- 0.5 + y * 1i
 
#### Evaluation of Zeta function ####
    # Real part
    real_ <- Re(zeta(z))
 
    # Imaginary part
    imaginary <- Im(zeta(z))
 
#### Plotting ####
    zeta_df <- data.frame(real_ = real_,
                             imag = imaginary,
                             frame_ = 1:10001)
 
    g <- zeta_df %>% 
            ggplot(aes(x = real_, y = imaginary, frame = frame_)) +
                geom_path(size = 1.2, aes(cumulative = T)) +
                geom_point(size = 2, aes(x = 0, y = 0)) 
    gganimate(p = g, filename = "riemann.mp4", interval = 0.001, title_frame = F)
}

Compounded arithmetic in R

I was looking for an equivalent to the += python operator in R; firstly out of curiosity and then to just not be beaten by something so simple.

In R, infix operators (operators sandwiched with %, ie, %.%) take arguments on the left and right sides to be closer to standard arithmetic functions. Using the infix structure, I created a simple compounded arithmetic operator for addition:

`%+%` < - function(x, y) {
    eval.parent(substitute(x <- x + y))
}

Read more