A fast, parallel implementation of convolutions for grid-type data (matrices, rasters and other grid based objects).

pfocal(
  data,
  kernel,
  edge_value = 0,
  transform_function = "MULTIPLY",
  reduce_function = "SUM",
  mean_divider = "ONE",
  variance = FALSE,
  na.rm = NA,
  mp = TRUE,
  debug_use_r_implementation = FALSE,
  ...
)

Arguments

data

[matrix-type] Grid to compute onto.

kernel

[matrix] Computation kernel (neighborhood).

edge_value

[numeric] Numeric value, NA or NaN.The value to give at the edge of the kernel matrix when the moving window overflows the data grid.

transform_function

[character] The function to apply to the cell values covered by the kernel. For possible values, see pfocal_info_transform(). Default to "MULTIPLY".

reduce_function

[character] The function to apply to the kernel values after the function passed in transform_function has been applied (the function that summarize the data). For possible values, see pfocal_info_reduce(). Default to "SUM".

mean_divider

[character] Optional, allows to specify how the final value at each cell is divided by a value that can be function of the intermediate data resulting of applying transform_function. For possible values, see pfocal_info_mean_divisor(). Default to "ONE" (for no division).

variance

[logical] Whether to return the "variance" of the intermediate values at each point (for more details please see pfocal_info_variance()). Default to FALSE (just returns the value at each point).

na.rm

[NA OR logical] The behavior to adopt for dealing with missing values, default to NA (faster option). For possible values see pfocal_info_nan_policy().

mp

[logical] Whether to use the open_mp implementation, default to TRUE.

debug_use_r_implementation

[logical] Used for debugging purposes whether to use the slow R implementation instead of the fast C++ code. Default to FALSE.

...

None used at the moment .

Value

The updated, convoluted grid.

Details

Note that the memory allocation for the output is of size sizeof(double) * ncol * nrow and for the intermediate values, sizeof(double) * (ncol + kernel_ncol) * (nrow + kernel_nrow/2).

Examples


data <- matrix(nrow = 10, ncol = 10, data = runif(10 * 10))
kernel <- matrix(1 / 9, nrow = 3, ncol = 3)
pfocal(data = data, kernel = kernel)
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 0.2285926 0.2951474 0.2763311 0.2372036 0.2985669 0.2820163 0.3574262
#>  [2,] 0.3027311 0.3843756 0.3394490 0.3145925 0.3897613 0.4031863 0.4949989
#>  [3,] 0.3687406 0.4543967 0.2874890 0.3156626 0.3555754 0.4911681 0.4091207
#>  [4,] 0.4773641 0.5965926 0.4031947 0.3954769 0.3529333 0.4722018 0.4303290
#>  [5,] 0.5000832 0.6907276 0.5679706 0.5279750 0.4893596 0.5271352 0.5360728
#>  [6,] 0.3974991 0.6391141 0.6899843 0.6067384 0.5473578 0.4899447 0.6583965
#>  [7,] 0.2924307 0.4767984 0.5583493 0.4510320 0.4555969 0.3713118 0.5743569
#>  [8,] 0.2777646 0.4330394 0.4395382 0.4289817 0.4110808 0.4182516 0.5503627
#>  [9,] 0.2972601 0.4757979 0.3589176 0.4791076 0.4618566 0.4816549 0.4401215
#> [10,] 0.1888714 0.3337978 0.2574628 0.4190167 0.3984089 0.4372688 0.3455227
#>            [,8]      [,9]     [,10]
#>  [1,] 0.3156938 0.3350045 0.2082362
#>  [2,] 0.4864994 0.5086019 0.3316020
#>  [3,] 0.4915868 0.4460993 0.3679924
#>  [4,] 0.5858833 0.5601644 0.3975677
#>  [5,] 0.5878059 0.5781569 0.3807547
#>  [6,] 0.6287471 0.6136441 0.3189849
#>  [7,] 0.5520795 0.5290815 0.2866641
#>  [8,] 0.6058640 0.5902536 0.3294094
#>  [9,] 0.4451678 0.5354335 0.3608133
#> [10,] 0.2825306 0.3493058 0.2254005