#include PDF-Generate-0.inc

macro Gauss_(& x, & fwhm)
	{
		Exp(-4 Ln(2) (x / fwhm)^2)
	}
macro PDF_Generation_Intensity_Penalty(i_name, fn_name, & wt, & scale_peaks)
   {
		scale = scale_peaks;
		extra_X_left = Max(X1 - Max(X1 - 1, 0.1), 0);
		extra_X_right = Max(Min(X2 + 1, 179.9) - X2, 0);

		fn fn_name (x, a0, a1) = wt (a0 - a1)^2 / Max(a0 + a1, 1e-6);

		default_I_attributes 1e-6 min 0 val_on_continue = Val Rand(0.5, 2) + 1e-4;
      create_pks_fn fn_name
      create_pks_name % i_name
   }
macro PDF_conv_Gauss(c, v, fwhm_in_r, fwhm_in_Q,  & qmax_, & exp_constant)
	{
		#m_argu c
		If_Prm_Eqn_Rpt(c, v, min 1e-5 max 10, del 1e-3)
		local #m_unique !fwhm_in_Q_ = CeV(c, v) exp_constant qmax_; : fwhm_in_Q
		local #m_unique !fwhm_in_r_ = 8 Ln(2) / fwhm_in_Q_; :  fwhm_in_r
		pdf_convolute = Exp(-4 Ln(2) (X / fwhm_in_r_)^2);
			min_X = -fwhm_in_r_ 3;
			max_X =  fwhm_in_r_ 3;
	}
macro PDF_ft_conv_Gauss(c, v, fwhm_in_r, fwhm_in_Q, & qmax_, & exp_constant)
	{
		#m_argu c
		If_Prm_Eqn_Rpt(c, v, min 1e-5 max 10, del 1e-3)
		local #m_unique !fwhm_in_Q_ = CeV(c, v) exp_constant qmax_; : fwhm_in_Q
		local #m_unique !fwhm_in_r_ = 8 Ln(2) / fwhm_in_Q_; : fwhm_in_r
		pdf_ft_conv = Exp(-4 Ln(2) (X / fwhm_in_Q_)^2);
	}
macro PDF_ft_conv_SoperLorch(c, v, fwhm_in_r, & qmax_, & sl_const)
	{
		#m_argu c
		If_Prm_Eqn_Rpt(c, v, min 1e-5 max 10, del 1e-3)
		local #m_unique !fwhm_in_r_ = CeV(c, v) sl_const 4.4934 / qmax_; : fwhm_in_r
		local #m_unique !w = Max(X, 1e-3) fwhm_in_r_;
		pdf_ft_conv = (Sin(w) - w Cos(w)) / w^3;
	}
macro PDF_conv_SoperLorch(d_zero, d_zerov, d_one, d_onev, fwhm_in_r)
	{
		'Covolution to account for the use of the Soper-Lorch function prior to Fourier transform of the S(Q)
		'd_zero is the "Width of broadening in r space", as used in GudrunN
		'Ref: A. K. Soper and E. R. Barney, J. Appl. Cryst. (2012). 45, 1314–1317
		#m_argu d_zero
		#m_argu d_one
		If_Prm_Eqn_Rpt(d_zero, d_zerov, min 0.001 max 1.0, del 0.01)
		If_Prm_Eqn_Rpt(d_one, d_onev, min 0 max 0.1, del 0.01)
		local #m_unique !fwhm_in_r_ = 2.3548 (CeV(d_zero,d_zerov) / Ln(2) Ln(2 Cosh( (CeV(d_one, d_onev)/ CeV(d_zero, d_zerov)) Ln(2) Xo)));
			: fwhm_in_r
		pdf_convolute = Exp(-4 Ln(2) (X / fwhm_in_r_)^2);
			min_X = -fwhm_in_r_ 3;
			max_X =  fwhm_in_r_ 3;
	}
macro PDF_conv_SoperLorch(d_zero, d_zerov)
	{
		convolute_SoperLorch(d_zero,d_zerov,,0)
	}
