[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: negative indexing of matrix or general access
From: |
Jaroslav Hajek |
Subject: |
Re: negative indexing of matrix or general access |
Date: |
Wed, 8 Oct 2008 07:58:18 +0200 |
On Tue, Oct 7, 2008 at 10:33 PM, Zbigniew Komarnicki <address@hidden> wrote:
> Hello,
>
> is possible to write a class in OCT file by which I will can make the
> following access by negative index to a matrix or general access for
> arbitrary index . For example
>
>
> % this will be create a matrix with zero values (for example by calling a
> % constructor in C++ code in OCT file)
> % or
> % inserting zero values to such matrix (by operator=)
> A(-3:3, 2:5) = 0
For this to work, you need to modify Octave itself, a package can't
override Octave's behaviour like that. A package would at least need a
special object constructor, like
A = nbizeros(-3:3, 2:5);
Afterwards, you could happily write
A(-2:2, 3:5) = whatever
>
> I can do it in Fortran 95, but i think it is also possible in octave by
> writing some function or classes to deal with such indexing, by simple adding
> offset internally. For example,
>
> this
> A(-3:3, 2:5) = 0
>
> internally can be written in C++ as
>
> ...
> {
> // if we set:
> // int min_idx_1=-3, min_idx_2=2;
> // int max_idx_1=3, max_idx_2=5;
>
> //scaling to 0, because the array start in C++ from 0
> int offset_min_idx_1 = - min_idx_1;
> int offset_min_idx_2 = - min_idx_2;
>
> for (int i=0; i<max_idx_1; ++i)
> for (int j=0; j<max_idx_2; ++j)
> A(i+offset_min_idx_1, j+offset_min_idx_2) = 0;
> end
> end
>
> }
> ...
>
>
> I have no idea how to start write such code to do it in OCT file, but i think
> it is possible. Then it can be used in octave very natural as in Fortran
> without making this scaling (in octave to 1, because octave start matrices
> from 1 index; in C++ to 0 index), because now i must to do it by scaling to 1
> in octave, for example in this way
>
>
> % my goal: A(-3:3, 2:5) = 0
>
> % but now I doing it for example in this way
> A(1:7, 1:4) = 0
>
>
> Could someone point me how to start write such code in OCT file or maybe
> someone write such code and will be share such code to octave?
>
> Exactly how to write a simple class in C++ by OCT files in octave and simple
> use it in octave? Could someone show it step by step how to do it? Especially
> how is invoked constructor and operator= for an example class in C++ by OCT
> file.
>
> Thank you for your time.
>
I have been thinking of writing such a package several times, so I'd
be willing to cooperate on this. However, it seems to me that you
have, so far, overlooked one important catch:
In Fortran 95, the index bases are "local", not a property of the
array (in other words, they are not get passed with the array
descriptor). That's whay they're so convenient: you always override
them on declaration.
In Octave, however, there are no declarations and the bounds will need
to be an inherent property of the object. Because a lot of code
assumes 1-based indexing, passing the object to many array functions
would not work properly.
Resetting the bounds on copy is out of question due to the reference
counting mechanism - you can't interfere that from within a package.
My idea was creating a "views" package, that would allow constructing
a view into an array, like this:
v = view (A, 2:3, [1:5, 0])
equivalent to Fortran 2003 pointer assignment:
v(:, 0:) => A(2:3, 1:5)
Indexing the view would give a regular array, not another view. Thus,
it would be possible to pass it like
y = myfunc (v(:,:))
but not
y = myfunc (v)
where myfunc expects a normal array. OTOH, if myfunc expected a view,
you could use it for pass-by-reference like this:
function zero1stelem (X)
if (isview (X))
X = view (X, 0, 0); # force 0-based indexing
X(0,0) = 0;
endif
Y = ones (10);
zero1stelem (view (Y));
Do you think something like this would be useful for you/others?
--
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz