! bn_sample_multi
! See the big discussion in issue 1924
!  4/8/2019  ACM
! 4/11/2018  more tests, can't sample 1-D var in another direction
! 4/12/2018  Both arguments must have an axis in the sample-direction
! 
! Tests for functions samplei_multi, etc as discussed in ticket 1924
! Same tests in all directions 

define axis/e=11:15:1 aax_a
define axis/e=1:4:1 aax_b
define axis/x=0:1:1 sax
let a_1 = _e[ge=aax_a]^2
let a_2 = (4-x[gx=sax])^2
let a_2d = a_1 + a_2
let b_1 = 5-_e[ge=aax_b]
let b_2 = 1+x[gx=sax]
let b_2d = b_1 + b_2 - 1
list a_2d
list b_2d
list samplem_multi(a_2d, b_2d)

! SAMPLEM_MULTI agrees with SAMPLEM for a 1D index list  in the E direction
list b_1
list samplem (a_2d, b_1)
list samplem_multi (a_2d, b_1) 

! Intentional errors
set mode ignore_error

! Cannot sample the x-e data along t 

define axis/t=3:4:1 sax2
let b_t = t[gt=sax2]
list samplem_multi(a_2d, b_t)

! Cannot sample the x data along e, but can if we add an e axis to arg 1

list samplem_multi(a_2, b_1)
list samplem_multi(a_2+esequence(0), b_1)
list samplem_multi(a_2+0*esequence({3,1,2}), b_1)

set mode/last ignore_error

! Not an error: The grid of the INDEX var in non-sample directions  
! must be conformable with the grid of to-be-sampled.   

define axis/x=0:3:1 bigax
let c_2 = 1+x[gx=bigax]
let c_2d = c_2 + b_1
list c_2d
list samplem_multi(a_2d, c_2d)

! Monte Carlo resamples of a dataset, as outlined in #1924
! Sample E data using an M-N st of indices.

let data = ESEQUENCE({59,91,26,37,72,43,87})
let ndat = `data,r=msize`
let na = 8
let vals = _n[n=1:`na`]
let samp_indices = 1 + int(ndat * randu(_e[ge=data, m=1:`ndat`]+vals))
let bootstrap_resamples = samplem_multi(data+0*samp_indices, samp_indices)

list data
list samp_indices
list bootstrap_resamples

! Sample 2D data in this way, samples in M as above for each Z

let data2d = data + z[z=0:6:6] 
list data2d

let data2d_samp = samplem_multi (data2d, samp_indices)
list/z=1 data2d_samp !same as the 1-d sample of "data" just above
list/z=6 data2d_samp

! SAMPLEI_multi

define axis/x=11:15:1 aax_a
define axis/x=1:4:1 aax_b
define axis/y=0:1:1 sax
let a_1 = x[gx=aax_a]^2
let a_2 = (4-y[gy=sax])^2
let a_2d = a_1 + a_2
let b_1 = 5-x[gx=aax_b]
let b_2 = 1+y[gy=sax]
let b_2d = b_1 + b_2 - 1
list samplei_multi(a_2d, b_2d)
list samplei_multi(a_2d, b_1)

! Intentional errors
set mode ignore_error

! Cannot sample the x-y data along t 

define axis/t=3:4:1 sax2
let b_t = t[gt=sax2]
list samplei_multi(a_2d, b_t)

! Cannot sample the y data along x, but can if we add an x axis to arg 1

list samplei_multi(a_2, b_1)
list samplei_multi(a_2+xsequence(0), b_1)
list samplei_multi(a_2+0*xsequence({3,1,2}), b_1)

set mode/last ignore_error

! Not an error: The grid of the INDEX var in non-sample directions  
! must be conformable with the grid of to-be-sampled. 

define axis/y=0:3:1 bigax
let c_2 = 1+y[gy=bigax]
let c_2d = c_2 + b_1
list c_2d
list samplei_multi(a_2d, c_2d)

! Monte Carlo resamples of a dataset, as outlined in #1924
! Sample I data using an I-M st of indices.

let data = {59,91,26,37,72,43,87}
let ndat = `data,r=isize`
let na = 8
let vals = _m[m=1:`na`]
let samp_indices = 1 + int(ndat * randu(x[gx=data,i=1:`ndat`]+vals))
let bootstrap_resamples = samplei_multi(data+0*samp_indices, samp_indices)

list data
list samp_indices
list bootstrap_resamples

! Sample 2D data in this way, samples in I for each Z

let data2d = data + z[z=0:6:6] 
list data2d

let data2d_samp = samplei_multi (data2d, samp_indices)
list/z=1 data2d_samp !same as the 1-d sample of "data" just above
list/z=6 data2d_samp

! SAMPLJ_multi

define axis/y=11:15:1 aax_a
define axis/y=1:4:1 aax_b
define axis/x=0:1:1 sax
let a_1 = y[gy=aax_a]^2
let a_2 = (4-x[gx=sax])^2
let a_2d = a_1 + a_2
let b_1 = 5-y[gy=aax_b]
let b_2 = 1+x[gx=sax]
let b_2d = b_1 + b_2 - 1
list samplej_multi(a_2d, b_2d)
list samplej_multi(a_2d, b_1)

! Intentional errors
set mode ignore_error

! Cannot sample the x-y data along t 

define axis/t=3:4:1 sax2
let b_t = t[gt=sax2]
list samplej_multi(a_2d, b_t)

! Cannot sample the x data along y, but can if we add a y axis to arg 1

list samplej_multi(a_2, b_1)
list samplej_multi(a_2+ysequence(0), b_1)
list samplej_multi(a_2+0*ysequence({3,1,2}), b_1)

set mode/last ignore_error

! Not an error: The grid of the INDEX var in non-sample directions  
! must be conformable with the grid of to-be-sampled. 

define axis/x=0:3:1 bigax
let c_2 = 1+x[gx=bigax]
let c_2d = c_2 + b_1
list c_2d
list samplej_multi(a_2d, c_2d)

! Monte Carlo resamples of a dataset, as outlined in #1924
! Sample Y data using an Y-N st of indices.

let data = YSEQUENCE({59,91,26,37,72,43,87})
let ndat = `data,r=jsize`
let na = 8
let vals = _n[n=1:`na`]
let samp_indices = 1 + int(ndat * randu(_y[gy=data, j=1:`ndat`]+vals))
let bootstrap_resamples = samplej_multi(data+0*samp_indices, samp_indices)

list data
list samp_indices
list bootstrap_resamples

! Sample 2D data in this way, samples in M as above for each Z

let data2d = data + z[z=0:6:6] 
list data2d

let data2d_samp = samplej_multi (data2d, samp_indices)
list/z=1 data2d_samp !same as the 1-d sample of "data" just above
list/z=6 data2d_samp

! SAMPLEK_MULTI

define axis/z=11:15:1 aax_a
define axis/z=1:4:1 aax_b
define axis/x=0:1:1 sax
let a_1 = z[gz=aax_a]^2
let a_2 = (4-x[gx=sax])^2
let a_2d = a_1 + a_2
let b_1 = 5-z[gz=aax_b]
let b_2 = 1+x[gx=sax]
let b_2d = b_1 + b_2 - 1
list samplek_multi(a_2d, b_2d)
list samplek_multi(a_2d, b_1)

! Intentional errors
set mode ignore_error

! Cannot sample the x-z data along t 

define axis/t=3:4:1 sax2
let b_t = t[gt=sax2]
list samplek_multi(a_2d, b_t)

! Cannot sample the x data along x, but can if we add a z axis to arg 1

list samplek_multi(a_2, b_1)
list samplek_multi(a_2+zsequence(0), b_1)
list samplek_multi(a_2+0*zsequence({3,1,2}), b_1)

set mode/last ignore_error

! Not an error: The grid of the INDEX var in non-sample directions  
! must be conformable with the grid of to-be-sampled. 

define axis/x=0:3:1 bigax
let c_2 = 1+x[gx=bigax]
let c_2d = c_2 + b_1
list c_2d
list samplek_multi(a_2d, c_2d)

! Monte Carlo resamples of a dataset, as outlined in #1924
! Sample Z data using an Z-N st of indices.

let data = ZSEQUENCE({59,91,26,37,72,43,87})
let ndat = `data,r=ksize`
let na = 8
let vals = _n[n=1:`na`]
let samp_indices = 1 + int(ndat * randu(_z[gz=data, k=1:`ndat`]+vals))
let bootstrap_resamples = samplek_multi(data+0*samp_indices, samp_indices)

list data
list samp_indices
list bootstrap_resamples

! Sample 2D data in this way, samples in M as above for each Z

let data2d = data + y[y=0:6:6] 
list data2d

let data2d_samp = samplek_multi (data2d, samp_indices)
list/y=1 data2d_samp !same as the 1-d sample of "data" just above
list/y=6 data2d_samp

! SAMPLEL_MULTI

define axis/t=11:15:1 aax_a
define axis/t=1:4:1 aax_b
define axis/x=0:1:1 sax
let a_1 = t[gt=aax_a]^2
let a_2 = (4-x[gx=sax])^2
let a_2d = a_1 + a_2
let b_1 = 5-t[gt=aax_b]
let b_2 = 1+x[gx=sax]
let b_2d = b_1 + b_2 - 1
list samplel_multi(a_2d, b_2d)
list samplel_multi(a_2d, b_1)

! Intentional errors
set mode ignore_error

! Cannot sample the x-t data along F 

define axis/f=3:4:1 sax2
let b_f = _F[gf=sax2]
list samplel_multi(a_2d, b_f)

! Cannot sample the x data along t, but can if we add a t axis to arg 1

list samplel_multi(a_2, b_1)
list samplel_multi(a_2+tsequence(0), b_1)
list samplel_multi(a_2+0*tsequence({3,1,2}), b_1)

set mode/last ignore_error

! Not an error: The grid of the INDEX var in non-sample directions 
! must be conformable with the grid of to-be-sampled. 

define axis/x=0:3:1 bigax
let c_2 = 1+x[gx=bigax]
let c_2d = c_2 + b_1
list c_2d
list samplel_multi(a_2d, c_2d)

! Monte Carlo resamples of a dataset, as outlined in #1924
! Sample Z data using an T-N st of indices.

let data = TSEQUENCE({59,91,26,37,72,43,87})
let ndat = `data,r=lsize`
let na = 8
let vals = _n[n=1:`na`]
let samp_indices = 1 + int(ndat * randu(_t[gt=data, l=1:`ndat`]+vals))
let bootstrap_resamples = samplel_multi(data+0*samp_indices, samp_indices)

list data
list samp_indices
list bootstrap_resamples

! Sample 2D data in this way, samples in M as above for each Z

let data2d = data + y[y=0:6:6] 
list data2d

let data2d_samp = samplel_multi (data2d, samp_indices)
list/y=1 data2d_samp !same as the 1-d sample of "data" just above
list/y=6 data2d_samp

! SAMPLEN_MULTI

define axis/f=11:15:1 aax_a
define axis/f=1:4:1 aax_b
define axis/x=0:1:1 sax
let a_1 = _f[gf=aax_a]^2
let a_2 = (4-x[gx=sax])^2
let a_2d = a_1 + a_2
let b_1 = 5-_f[gf=aax_b]
let b_2 = 1+x[gx=sax]
let b_2d = b_1 + b_2 - 1
list samplen_multi(a_2d, b_2d)
list samplen_multi(a_2d, b_1)

! Intentional errors
set mode ignore_error

! Cannot sample the x-f data along T

define axis/t=3:4:1 sax2
let b_t = t[gt=sax2]
list samplen_multi(a_2d, b_t)

! Cannot sample the x data along f, but can if we add an f axis to arg 1

list samplen_multi(a_2, b_1)
list samplen_multi(a_2+fsequence(0), b_1)
list samplen_multi(a_2+0*fsequence({3,1,2}), b_1)

set mode/last ignore_error

! Not an error: The grid of the INDEX var in non-sample directions 
! must be conformable with the grid of to-be-sampled.  

define axis/x=0:3:1 bigax
let c_2 = 1+x[gx=bigax]
let c_2d = c_2 + b_1
list c_2d
list samplen_multi(a_2d, c_2d)

! Monte Carlo resamples of a dataset, as outlined in #1924
! Sample F data using an T-M st of indices.

let data = FSEQUENCE({59,91,26,37,72,43,87})
let ndat = `data,r=nsize`
let na = 8
let vals = _m[m=1:`na`]
let samp_indices = 1 + int(ndat * randu(_f[gy=data, n=1:`ndat`]+vals))
let bootstrap_resamples = samplen_multi(data+0*samp_indices, samp_indices)

list data
list samp_indices
list bootstrap_resamples

! Sample 2D data in this way, samples in M as above for each Z

let data2d = data + z[z=0:6:6] 
list data2d

let data2d_samp = samplen_multi (data2d, samp_indices)
list/z=1 data2d_samp !same as the 1-d sample of "data" just above
list/z=6 data2d_samp
