Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 19, 2012 Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Hi, all! In advance, excuse me for my terrible english. I try to learn the D language. One of the areas of my work is numerical calculations, and there are lot of linear algebra objects and operations. For beginning I started with implementation of matrix calculations. Note, that I am doing this is only in order to learn a language. So, I try to implement operations with density and sparse matrixes with uniform interface. And was confused with slice operations. For example: uint rows = 100, cols = 100; auto A = new MatrixDensity!double(rows, cols); auto S = new MatrixSparse!double(rows, cols); auto x = new VectorColumn!double(rows); auto y = new VectorRow!double(cols); //and now it possible to implement: auto a_ij = A[i, j]; //opIndex(uint row, uint col) S[i, j] = a_ij; //opIndexAssign(T value, uint row, uint col) y = S[i]; //opIndex(uint row) A[i] = y; //opIndexAssign(VectorRow!T value, uint row) //Then I want to do something like x = S[0..$, 1]; //get column S[0..$, 2] = A[0..$, 2]; //get and set column auto B = A[0..10, 0..10]; //get submatrix block of matrix A I.e. I want to implement multimentional slices to realize simple, generalized and "human readable" syntax for matrix and vectors operations (like a Matlab syntax for example). Or may be I use not right way for realization? |
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rafael | On Wednesday, 19 December 2012 at 09:58:37 UTC, Rafael wrote:
> //Then I want to do something like
> x = S[0..$, 1]; //get column
> S[0..$, 2] = A[0..$, 2]; //get and set column
> auto B = A[0..10, 0..10]; //get submatrix block of matrix A
Warning the following is my total noob opinion.
Instead of using [0..$,2] for example, try [0..$][2] and see how that works out. D doesn't use multiple indexes in the same [] section as far as i know so give that a go.
|
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nekroze | On Wednesday, 19 December 2012 at 10:17:28 UTC, Nekroze wrote:
> On Wednesday, 19 December 2012 at 09:58:37 UTC, Rafael wrote:
>> //Then I want to do something like
>> x = S[0..$, 1]; //get column
>> S[0..$, 2] = A[0..$, 2]; //get and set column
>> auto B = A[0..10, 0..10]; //get submatrix block of matrix A
>
> Warning the following is my total noob opinion.
>
> Instead of using [0..$,2] for example, try [0..$][2] and see how that works out. D doesn't use multiple indexes in the same [] section as far as i know so give that a go.
Yes, this is the first solution, that come to mind. But there are some problems in that way. Firs of them (but not main) - not uniform syntax. The more important: which should result operation Matrix[0..$] in that case?
|
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rafael | On Wednesday, 19 December 2012 at 10:25:23 UTC, Rafael wrote:
> On Wednesday, 19 December 2012 at 10:17:28 UTC, Nekroze wrote:
>> On Wednesday, 19 December 2012 at 09:58:37 UTC, Rafael wrote:
>>> //Then I want to do something like
>>> x = S[0..$, 1]; //get column
>>> S[0..$, 2] = A[0..$, 2]; //get and set column
>>> auto B = A[0..10, 0..10]; //get submatrix block of matrix A
>>
>> Warning the following is my total noob opinion.
>>
>> Instead of using [0..$,2] for example, try [0..$][2] and see how that works out. D doesn't use multiple indexes in the same [] section as far as i know so give that a go.
>
> Yes, this is the first solution, that come to mind. But there are some problems in that way. Firs of them (but not main) - not uniform syntax. The more important: which should result operation Matrix[0..$] in that case?
Not uniform syntax in this case is the one simple thing:
A[i, j]; // get the a_ij
A[i][j]; // get the a_ij
but I like a first variant!
|
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rafael | On Wednesday, 19 December 2012 at 10:33:38 UTC, Rafael wrote:
> On Wednesday, 19 December 2012 at 10:25:23 UTC, Rafael wrote:
>> On Wednesday, 19 December 2012 at 10:17:28 UTC, Nekroze wrote:
>>> On Wednesday, 19 December 2012 at 09:58:37 UTC, Rafael wrote:
>>>> //Then I want to do something like
>>>> x = S[0..$, 1]; //get column
>>>> S[0..$, 2] = A[0..$, 2]; //get and set column
>>>> auto B = A[0..10, 0..10]; //get submatrix block of matrix A
>>>
>>> Warning the following is my total noob opinion.
>>>
>>> Instead of using [0..$,2] for example, try [0..$][2] and see how that works out. D doesn't use multiple indexes in the same [] section as far as i know so give that a go.
>>
>> Yes, this is the first solution, that come to mind. But there are some problems in that way. Firs of them (but not main) - not uniform syntax. The more important: which should result operation Matrix[0..$] in that case?
>
> Not uniform syntax in this case is the one simple thing:
> A[i, j]; // get the a_ij
> A[i][j]; // get the a_ij
>
> but I like a first variant!
Sorry it may be my lack of knowledge or something but no one else seems to be responding so i will keep trying to help as best i can.
What do you mean by uniform syntax? do you mean you want the syntax to be the same as in other languages? because as it says on the dlang page for arrays under rectangular arrays it says that a[i][j] is just the way that D does it where as in c++ is a[i, j] but that's just one of the differences between languages that cant be avoided as far as i know.
|
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nekroze | On Wednesday, 19 December 2012 at 11:59:24 UTC, Nekroze wrote:
> On Wednesday, 19 December 2012 at 10:33:38 UTC, Rafael wrote:
>> On Wednesday, 19 December 2012 at 10:25:23 UTC, Rafael wrote:
>>> On Wednesday, 19 December 2012 at 10:17:28 UTC, Nekroze wrote:
>>>> On Wednesday, 19 December 2012 at 09:58:37 UTC, Rafael wrote:
>>>>> //Then I want to do something like
>>>>> x = S[0..$, 1]; //get column
>>>>> S[0..$, 2] = A[0..$, 2]; //get and set column
>>>>> auto B = A[0..10, 0..10]; //get submatrix block of matrix A
>>>>
>>>> Warning the following is my total noob opinion.
>>>>
>>>> Instead of using [0..$,2] for example, try [0..$][2] and see how that works out. D doesn't use multiple indexes in the same [] section as far as i know so give that a go.
>>>
>>> Yes, this is the first solution, that come to mind. But there are some problems in that way. Firs of them (but not main) - not uniform syntax. The more important: which should result operation Matrix[0..$] in that case?
>>
>> Not uniform syntax in this case is the one simple thing:
>> A[i, j]; // get the a_ij
>> A[i][j]; // get the a_ij
>>
>> but I like a first variant!
>
> Sorry it may be my lack of knowledge or something but no one else seems to be responding so i will keep trying to help as best i can.
>
> What do you mean by uniform syntax? do you mean you want the syntax to be the same as in other languages? because as it says on the dlang page for arrays under rectangular arrays it says that a[i][j] is just the way that D does it where as in c++ is a[i, j] but that's just one of the differences between languages that cant be avoided as far as i know.
Ok.
1) It is possible to implement multiindex access using opIndex* methods, moreover this is the simplest way to multiindex access realization. So, we have [i, j, k] notation. Next step after it - slices implementation and it looks logical to save the same notation for it: [i1..i2, j1..j2, k]. But it impossible, if I understand correctly.
2) On the other hand. Syntax via [i][j] leads to some overhead: first index access [i] must return some light accessor object (like range (or iterator)) which supports the index access operation. But using this approach it is possible to implement multidimensional slices. And this means, that opIndex* methods with multiindex are not needed.
|
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rafael | On Wednesday, 19 December 2012 at 12:27:32 UTC, Rafael wrote: > 1) It is possible to implement multiindex access using opIndex* methods, moreover this is the simplest way to multiindex access realization. So, we have [i, j, k] notation. Next step after it - slices implementation and it looks logical to save the same notation for it: [i1..i2, j1..j2, k]. But it impossible, if I understand correctly. > > 2) On the other hand. Syntax via [i][j] leads to some overhead: first index access [i] must return some light accessor object (like range (or iterator)) which supports the index access operation. But using this approach it is possible to implement multidimensional slices. And this means, that opIndex* methods with multiindex are not needed. Well if you are looking at operator overloading you can overload opSlice as described here http://dlang.org/operatoroverloading.html#Slice however i am not sure if you can defined multiple slices delimited by commas in the same [] block but it is worth a try. |
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rafael | opIndex/opIndexAssign take multiple indices. http://dlang.org/operatoroverloading.html#Array |
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Chapman | On Wednesday, 19 December 2012 at 13:34:52 UTC, John Chapman wrote:
> opIndex/opIndexAssign take multiple indices. http://dlang.org/operatoroverloading.html#Array
Yes, but slices - not, and my initial question was about it.
|
December 19, 2012 Re: Multidimensional slice | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Chapman | On Wednesday, 19 December 2012 at 13:34:52 UTC, John Chapman wrote:
> opIndex/opIndexAssign take multiple indices. http://dlang.org/operatoroverloading.html#Array
Yes however i believe the OP also wants to provide multiple slices as well as multiple indices. Atleast as far as i can tell.
|
Copyright © 1999-2021 by the D Language Foundation