How to fix the erfz function from package pracma properly?
05:13 09 Oct 2025

I need to use the function erfz (error function that takes complex arguments) for fitting Voigt profiles. It seems like it is only implemented in R in the pracma package as erfz(z).

I tried to evaluate erfz(0), which throws an error, when it should output 0. My R is in french, so here's the error: "Erreur dans if (sum(work.i) == 0) break : valeur manquante là où TRUE / FALSE est requis". I created a function to replace erfz(x), that works with x being any vectors potentially containing 0. Here it is:

erfzfix <- function(x){
  if (0%in%x){
    ind=seq(1,length(x))
    x0<-x==0
    x0n<-ind[x0]
    xstar<-x[!x0]
    outputstar<-erfz(xstar)
    output<-append(outputstar,0,after = x0n-1)
  }
  else output=erfz(x)
  return(output)
}

I'm not very experienced with R but I guess this will considerably slow the evaluation of the function. Is there a better way to fix it?

EDIT: Other numerical issues are present in the evaluation of erfz, that make fitting procedures struggle a lot because (I guess) of the non-C1 character of the function. This plot shows a "noisy" part of erfz: Plot of the erfz functio from the pracma package showing numerical instabilities

r physics