File: sinc.R

package info (click to toggle)
r-cran-foreach 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 648 kB
  • sloc: makefile: 2
file content (23 lines) | stat: -rw-r--r-- 690 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# simple foreach example that plots the sinc function

library(foreach)

# Define the coordinate grid to use
x <- seq(-10, 10, by=0.1)

# Compute starting indices for each task
nw <- getDoParWorkers()
cat(sprintf('Running with %d worker(s)\n', nw))
n <- ceiling(length(x) / nw)
ind <- seq(by=n, length=nw)

# Compute the value of the sinc function at each point in the grid
z <- foreach(i=ind, .combine=cbind) %dopar% {
  j <- min(i + n - 1, length(x))
  d <- expand.grid(x=x, y=x[i:j])
  r <- sqrt(d$x^2 + d$y^2)
  matrix(10 * sin(r) / r, length(x))
}

# Plot the results as a perspective plot
persp(x, x, z, ylab='y', theta=30, phi=30, expand=0.5, col="lightblue")