Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assignment Problem

12 views
Skip to first unread message

Thomas D. Dean

unread,
Mar 12, 2013, 2:16:55 AM3/12/13
to
I think an earlier post went away...

I am having problems with this, again.

restart; with(geom3d):
data:={ [1,2,3,4],
[5,6,7,8],
[9,10,11,12]
};
######################
## both these fail
for i from 1 to nops(data) do
point(A, data[i][2 .. 4]);
P[i] := A;
print(i, A) end do;
for i from 1 to nops(data) do print(i,form(P[i]),coordinates(P[i])); end do;
##

The problem is in the assignment to P[i]. All the P[i]'s have the same
value.

I am doing this wrong. All the p[i]'s have the same value. The
description in Heck says this. I tried P[i]:=evaln(A) but this did not
correct the problem.


p := [seq(evaln(point(A, data[i][2..4])), i = 1..nops(data))];
seq(print(i, coordinates(p[i])), i = 1..nops(data));

This produced the correct values.

What is wrong with the loop approach?

Tom Dean

Mate

unread,
Mar 13, 2013, 4:30:19 PM3/13/13
to
In geom3d the points (and other objects) are implemented
as names with associated properties (attributes).

In the first part you re-use the same name (A) three times.
Of course, only the last definition survives.

The second part is equivalent to

for i from 1 to nops(data) do
P[i]:='point'(A, data[i][2 .. 4]);
end do;
for i from 1 to nops(data) do print(i,form(P[i]),coordinates(P[i]));
end do;

Now, P[i] (and also A) is redefined each time it is used.
This is not efficient and in geom3d only a single point (name), A, is
defined.
Probably you will have big problems later using P[i] with other
functions.

The correct solution would be to define the points P1,P2,... using

for i from 1 to nops(data) do
point(P||i, data[i][2 .. 4]) end do;

Now,

for i from 1 to nops(data) do print(i,form(P||i),coordinates(P||i));
end do;

works ok.


Mate




0 new messages