[sac-user] SaC syntax for array boundaries

Scholz, Sven-Bodo S.Scholz at hw.ac.uk
Mon Feb 15 17:44:05 CET 2016


You would have to define your own generic mechanism, e.g.:


use Array: all;
use StdIO: all;


inline
int[*] shiftRight( int dimension, int[*] hyperplane, int[*] array)
{
  dim_cut = modarray( 0*shape(array), dimension, 1);
  res = with {
         (0*shape( array) <= iv < dim_cut) : hyperplane[drop([dimension+1], iv)];
         (dim_cut <= iv < shape(array) ) : array[modarray( iv, dimension, iv[dimension]-1)];
        } : modarray( array);
  return res;
}

inline
int[*] shiftLeft( int dimension, int[*] hyperplane, int[*] array)
{
  dim_cut = modarray( shape(array), dimension, shape(array)[dimension]-1);
  res = with {
         (0*shape( array) <= iv < dim_cut) : array[modarray( iv, dimension, iv[dimension]+1)];
         (dim_cut <= iv < shape(array) ) : hyperplane[drop([dimension+1], iv)];
        } : modarray( array);
  return res;
}

int main() {

  int[10,10] a;

  a = reshape([10,10], iota( 100));

  print(a);

  d = shiftRight( 0, a[0], a) + a + shiftLeft( 0, a[9], a) ;

  print(d);

  return 0;
}


One could envision arbitrary versions of shift that perform the insertion you want.


Alternatively, you could use a[ max( [o,o], iv-1) ]    for the selection and then define all elements that way…..
most likely with some performance penalty though….

Bodo


> On 15 Feb 2016, at 15:46, Stewart, Robert <R.Stewart at hw.ac.uk> wrote:
> 
> Hi Bodo, Artem,
> 
> Say I wanted to implemented blur_x in SaC, which at each pixel does:
> 
> blur_x(x, y) = (input(x-1, y) + input(x, y) + input(x+1, y))/3;
> 
> The important bit: at the edges of the array I want to mirror the
> nearest array element.
> 
> On a 10x10 array. I could do this for the sub region between (1,1) and
> (8,8):
> 
> b = with {
>      ( [1,0] <= [x,y] < [9,10]) : a[x-1,y] + a[x,y] + a[x+1,y] ;
>    } : modarray(a);
> 
> Then:
> 
> c = with {
>      ( [0,0] <= [x,y] < [0,10]) : a[x,y] + a[x,y] + a[x+1,y] ;
>    } : modarray(b);
> 
> d = with {
>      ( [9,0] <= [x,y] < [10,10]) : a[x-1,y] + a[x,y] + a[x,y] ;
>    } : modarray(c);
> 
> Is there shorthand in SaC for clamping nearest pixel elements at the
> border of a 2D array, to apply stencils that fall beyond array
> boundaries?
> 
> Thanks,
> 
> --
> Rob



More information about the sac-user mailing list