Best way to index Variables

Hi, i have a rather basic question. Both code pieces do the same:

restart:
 for i from 1 to 5 do  a[i]:=i;  end do;

a[3];

3

restart:
 for i from 1 to 5 do  a(i):=i;  end do;

a(3);

3

 

In which case should  i use round brackets and when square brackets for indexing ?

Does it matter ?

 

Thomas

acer's picture

indexing notation

First case: a is a Matrix/Vector/Array (ie. and rtable underneath). In that case, square brackets are the "usual" indexing way, and round brackets allows some new indexing facility introduced in Maple 12. See the first item in the ?updates,Maple12,programming help-page. I personally would prefer to use the square brackets, unless I were using the new functionality to extend the size of the rtable dynamically or to get some (no-copy) efficiency with it on the rhs of an assignment. People familiar with Maple would recognize the square bracket code as "indexing". Which you prefer might depend on how much you wish to kowtow to Matlab syntax.

While I understand that using round brackets for rtable indexing (new in Maple 12) has appeal, I'm not sure yet whether I like the choice of syntax. Efficiency is good, and so is the new ability to extend sizes dynamically. But those could be gotten with other syntax choices too. The choice of round brackets is "Matlab compatible", but it also precludes the possibility of extending round-bracket syntax to allow application by, say, a Matrix of operators.

Second case: a is not an rtable. In that case using the square brackets will treat a as a table, even if you have not used the table() command explicitly. For example,

> restart:
> a[5]:=17:
> a[5];
                                      17
> eval(a);
                                table([5 = 17])

If you instead use the round brackets, then you are using function application syntax. And when you use it in an assignment like that, you are assigning to the remember table of "function" a. Now, while that does also use a table underneath as the mechanism, it is less apparent what's going on. People may not recognize immediately what your code is doing and how it relies on this, even if the overall effect is the same. I would prefer square brackets, in this case.

> restart:
> a(5):=17:
> a(5);
                                      17
> eval(a);
               proc() option remember; 'procname(args)' end proc
 
> op(4,eval(a));
                                table([5 = 17])

acer

Doug Meade's picture

tables and procedures and remember tables

The two code pieces do not do the same thing!

In your first case, you create a as a table and access its values using subscripts (denoted by square brackets):

restart:
for i from 1 to 5 do  a[i]:=i;  end do:
map2( is, a, [table,procedure] );
                                [true, false]
print( a );
                         table([1=1,2=2,3=3,4=4,5=5])

In your second case, you create a as a procedure (function) with 5 pre-assigned values that are stored in a "remember table": (See ?remember)

restart:
for i from 1 to 5 do  a(i):=i;  end do:
map2( is, a, [table,procedure] );
                                [false, true]
print( a );
             proc()  option remember, 'procname(args)'  end proc;

The current contents of Maple's remember table for the function a is found to be:

op(4,eval(a));
                         table([1=1,2=2,3=3,4=4,5=5])

So, the function definition is really using a table. The first implication of this has to mean that there is more overhead in storing the procedure. I'd also suspect that it would be slightly slower to access information from the procedure than directly from a table. For an example like this, these differences are likely to be very slight. But, in other settings, I can see that these could become significant.

My recommendation is to use a table, with square brackets, when that is what you are really using.

Doug

---------------------------------------------------------------------
Douglas B. Meade  <><
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.ed

I see

okay, i see that these two ways of generating  index variables  have differences in the underlying data structure. I just thought there's a more fundamental reason to use one or the other. Thanks for pointing out the differences, Doug and Acer.

 

Thomas

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}