#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    C# rectangular arrays


    Hi all,

    Are there any functions for creating and working with rectangular multi-dimensional arrays other than the [,,,,] syntax? If I pass one of these arrays to a function that generalizes to n-dimensions, (like gaussian blur) it would be nice to be able to do any of the following:

    index the array with a function or more general syntax, as in array[pos[]] rather than array[pos0,pos1,pos2,....]

    create a new array with something like array = array(ndim,dims[])

    it would be nice to do this without dropping multidimensional arrays down to one dimension and indexing manually. It would also be nice to work with with the built-in C# multi-dimensional arrays for niceness sake. For example some functions might not generalize to n-dimensions, in which case these functions can use the [,] syntax.


    Thanks.
  2. #2
  3. No Profile Picture
    Closet coder
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2005
    Location
    Plantation, FL <---south florida
    Posts
    1,431
    Rep Power
    153
    Originally Posted by craigyk
    Hi all,

    Are there any functions for creating and working with rectangular multi-dimensional arrays other than the [,,,,] syntax? If I pass one of these arrays to a function that generalizes to n-dimensions, (like gaussian blur) it would be nice to be able to do any of the following:

    index the array with a function or more general syntax, as in array[pos[]] rather than array[pos0,pos1,pos2,....]

    create a new array with something like array = array(ndim,dims[])

    it would be nice to do this without dropping multidimensional arrays down to one dimension and indexing manually. It would also be nice to work with with the built-in C# multi-dimensional arrays for niceness sake. For example some functions might not generalize to n-dimensions, in which case these functions can use the [,] syntax.


    Thanks.
    second link from a quick google
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0
    Uh, that doesn't help, unless I'm being obtuse and completely missing something. And my cursory pre-post google search took me to at least page 9 of a couple differently phrased search queries, of which one was that one. The examples you have given don't describe how to create a built-in rectangular array of arbitrary dimensions 'n' specified at run-time. As you can see, all the examples use the [,] notation with the number of ',' hard coded in. What I'm wondering is if there are functions these syntax features match too. In many languages convenient syntax features map into more generic calls. Such as accessing instance variables in Obj-C

    object.variable
    maps into something like
    [object getObjectForKey:"variable"]

    or [2,3,4,5] mapping into 2:3:4:5:[] equivalent in languages like Haskell

    sometimes one version is more useful than the other

    I'd like to know if
    double[,,,] array = new double[10,10,10,10]
    maps to something like
    object array = System.Array(4,[10,10,10,10])

  6. #4
  7. No Profile Picture
    Closet coder
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2005
    Location
    Plantation, FL <---south florida
    Posts
    1,431
    Rep Power
    153
    so you want to pass an array position from your rectangular array into a method rather than passing the entire thing in? from my understanding a rectangular array is built as a single object compared to a jagged array that is basically an array of array objects.
    so i would think for what you want would be a jagged and not rectangular since i think you want to manipulate individual arrays in your methods rather than the whole matrix. is this in general what yer hinting at?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by nattylife
    so you want to pass an array position from your rectangular array into a method rather than passing the entire thing in? from my understanding a rectangular array is built as a single object compared to a jagged array that is basically an array of array objects.
    so i would think for what you want would be a jagged and not rectangular since i think you want to manipulate individual arrays in your methods rather than the whole matrix. is this in general what yer hinting at?
    In part, yes. In part no, because a jagged array implies so many other complications and possible performance issues. Anyway there still isn't a way to conveniently access a general n-dimensional jagged array[][][] without using recursive function calls, i.e.:

    Code:
    function(array) {
        if ( array.Rank == 1 ) {
           do work...on this dimension
        } else {
           data = function(array[])
           merge work...
        }
    }
    For a jagged array it seems you can cut off a portion of array[][] by specifying just one dimension: array[0]. Since I don't think you can do array[0,:] in C# (like in matlab, python, lua, ruby?) even recursion doesn't seem straight-forward (not that I would want to use recursion for many of these functions. I've implemented some recursive versions in C for fun, but they are usually slower and harder to understand).

    So I have coded quite a few low-level C functions with very general arguments. If you have ever seen the interface to fftw whose n-dimensional transforms take arguments in the form of function(int ndim, int dims[], double data[]) I basically followed that model for my n-dimensional functions, i.e. gaussian_nd(...), bin_nd(...), mser_segmentation(...), watershed_segmentation(...), etc.

    Note that the array is being passed in as a 1-dimensional array and the function needs to access the data as required by converting between n-dimensional position and the 1-dimensional index when necessary. Now it's time I make all this code I've written more useful to the group I am now working with, which happens to mean me learning some C# so I can work with our data infrastructure and GUI stuff more easily. I'm glad I kept my C functions very general in their interfaces since so far It's been pretty easy to call them using P/Invoke. Learning C#, however, I was delighted to see it already had a built-in multi-dimensional array type with data stored contiguously in memory (since at the lowest level, that's all these functions need to work). C# also seems fast enough that I could probably port a lot of these C functions to native C# pretty easily.

    The problem, so far, is I'm not sure how to use the built-in array type with this framework, since I haven't found a way to manage these arrays with the control I would like:

    1. I'd like to be able to access the data in a ND C# array in a couple of different ways such as using an ND list of coordinates: setArray(list_of_coordinates[]) = 0, or even as a 1D array: array[calculated_index] = 0.
    Of course the nice thing about sticking with the built-in arrays is I could still use the [10,20,10,30] notation if it was the most convenient.

    2. Have a way of having a C# function return an arbitrary rectangular array. Some of my functions take n-dimensional data and return it in a n+1 dimensional form. but how do I do that with the [,,,] notation of instantiating arrays? I can take a generic array of type [(,)*n] by just treating the argument as a generic array object, but how do I create a return array of type [(,)*(n+1)]?

    I could write my own array class, I suppose, but that seems silly since the built-in type must be doing essentially everything I want already, plus doing nice bounds checking safeness, etc. If I'm forced down that route then this aspect of the C# code is no better than the C code, or even worse because of the speed hit. Plus other users of these C# functions, who most definitely will probably want to work with the built-in array type, will have to contend with an extra layer of abstraction (converting [,...]->[]->[,...]) . Essentially, given that in so many respects C# seems like a well thought out language, I can't believe the designers would so cripple a type which seems to have been put into place precisely for these kinds of scientific computing applications. My initial guess was that this kind of language syntax would be implemented by converting the special syntax to a more general function call at some point of compilation, and that these equivalent functions would be accessible, but maybe not?.
  10. #6
  11. No Profile Picture
    Closet coder
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2005
    Location
    Plantation, FL <---south florida
    Posts
    1,431
    Rep Power
    153
    hmm, while im no expert i dont believe that functionality is available in the current .net framework. this sounds like you would need to create your own wrapper to handle this.
    are you looking to take as a param some sized array [,,n] and return an array of some some order higher ([,,n+n]) or is it always gonna be n+1? if its always +1 a static declaration of that return type or out param will do you.

    something that comes to mind (which im not sure it will work either) is that all types derive from the most basic type Object. so while you can pass in whatever array you want into your function, you could always pass it back as a casted Object. but in doing that you would also need to know what kinda array you are passing back so you can cast it back to the appropriate size.

    there maybe even be libraries out there already handling your situation... maybe.
    the .net framework wasnt exactly created in a way to benefit any particular area. it was more of a way to aggregate multiple languages functionality to basically compile to 1 machine code type and allow interoperability between those languages (a c# dll can be called by a visual basic.net app). so its got your basic functionality needed and nothing more.

    it sounds like yer gonna need to code something or do a workaround.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by nattylife
    hmm, while im no expert i dont believe that functionality is available in the current .net framework. this sounds like you would need to create your own wrapper to handle this.
    are you looking to take as a param some sized array [,,n] and return an array of some some order higher ([,,n+n]) or is it always gonna be n+1? if its always +1 a static declaration of that return type or out param will do you.
    90% of the functions return data with the same dimensionality, so in this case the second part about creating a new instance wouldn't be a problem since I could probably clone/copy the input array and work with that for output. I would still like to be able to access the elements using a position array though.
    Also some of the functions might take as input an array of any n dimensions and return one any n dimensions. This is academic at moment since I haven't written anything yet that is this general, but I can easily imagine eventually wanting to write, for instance, a projection function so that I could give it a 3-dimensional volume, a vector, and it would project down onto a 2d space (but just as easily project 4D onto 2D , or 2D onto 1D, etc..

    there maybe even be libraries out there already handling your situation... maybe.
    the .net framework wasnt exactly created in a way to benefit any particular area. it was more of a way to aggregate multiple languages functionality to basically compile to 1 machine code type and allow interoperability between those languages (a c# dll can be called by a visual basic.net app). so its got your basic functionality needed and nothing more.

    it sounds like yer gonna need to code something or do a workaround.
    Good suggestion. I was hoping that someone close to the .Net or Mono implementations would pipe in with, oh sure, the functions is:_______________, but it looks like that might not be the case. There are a couple of C# scientific computing packages, maybe I should querying their maintainers to see what they think. I can totally see .Net adding this capability at some later date, if it isn't already in there, since I can't be the only person that would find it useful, and you would think it would be pretty trivial to provide access too.

    Cheers,
    -Craig
  14. #8
  15. No Profile Picture
    Closet coder
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2005
    Location
    Plantation, FL <---south florida
    Posts
    1,431
    Rep Power
    153
    also like to apologize for my first response. i dont think i understood your question very well and gave you a ****ty response.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2009
    Posts
    837
    Rep Power
    527
    Here's a pretty good writeup and C# jagged arrays, including the IL (Intermediate Language) that is generated:

    http://dotnetperls.com/jagged-array
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0
    Misunderstanding is partially my fault too.

    Check it out, I think I've figured out some of my problems, that of creating rectangular arrays of arbitrary dimensions at run time and manipulating elements

    Code:
    int ndim = 5;
    int[] dims = new int[ndim];
    for (int i = 0; i < ndim; i++) dims[i] = 10;
    
    System.Array array1 = System.Array.CreateInstance(typeof(double), dims );
    is equivalent to:

    Code:
    double[,,,,] array2 = new double[10,10,10,10,10];
    as you can see the syntax version is much simpler, but I can do more with the other version since ndim is explicit.

    The following are also equivalent then:
    Code:
    array2[5,5,5,5,5] = 10;
    int[] position = {5,5,5,5,5};
    array1.SetValue(10,position);
    the only slight pain in the *** is that I need to explicitly type cast array1 to use the [,,,,,] notation on it, because otherwise if I just do:

    Code:
    array1[5,5,5,5,5] = 10;
    I get: Cannot apply indexing with [] to an expression of type 'System.Array'

    but this isn't a biggie. Now if only I could figure out how to change the shape of the array, ie. convert a [,,,,] into a [] with the same number of elements...
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0
    oh jeez,

    setting the array values of
    Code:
     double[,,,] array
    using

    Code:
    array.setValue(double value, int[] {5,5,5,5})
    is like 9X slower on my machine than setting it using

    Code:
    array[5,5,5,5] = value;
    *sigh

IMN logo majestic logo threadwatch logo seochat tools logo