On 5/24/07, Craig DeForest <[EMAIL PROTECTED]> wrote:


On May 24, 2007, at 6:52 AM, Saurabh Singhvi wrote:
> If there is an Nx3 matrix and another 3x3 matrix and if I wanted
> the last three rows
> of Nx3 to be replaced by 3x3 matrix, what would be the easiest way
> of doing it ??
>
> Specifying it like :
>
> $M(:,-1) = $A(:,2)
>
> isn't working out. It changes nothing. So if you could suggest how
> to do this, it would
> be great. Element by element replacement should work out, but it
> would be nicer
> if I could just replace the rows in one go.
>

Roban answered this nicely, but I'll reiterate that PDL separates the
operations of "create a piddle" and "stuff a piddle with values".
The "=" operator is used ONLY to create new PDLs.  The ".=" operator
is used whenever you want to modify an existing PDL.  Unfortunately,
Perl5 is not quite smart enough to allow us to merge the operators in
a sensible way.  That leads to a few warts surrounding assignment to
PDLs.

As far as Perl is concerned, a piddle is a pointer into memory
somewhere.  Straight assignment (with '=') just copies the pointer.
There is no straightforward way to get the Perl parser to know
whether the lvalue on the left side of the '=' is a normal Perl
scalar or a derived PDL, so we can't easily make the PDL engine do
the Right Thing in the case you described (assignment to a derived
PDL).  In your example, "$M(:,-1)" creates a temporary Perl scalar
that has its lvalue flag set (which allows us to use "=" or ".=" on
it).  Using '=' copies the pointer from "$A(:,2)" into the temporary
Perl scalar.  Since it's temporary, Perl drops it on the floor.

We'd like to eliminate that particular wart by either doing the Right
Thing (treating the assignment as ".=", and possibly emitting a
warning) or forbidding the construct, but unfortunately there is only
one lvalue flag -- we can't easily tell the parser that ".=" is OK
but "=" isn't.

Cheers,
Craig


Thanks a lot for the explaination Craig !! The ".=" syntax though IMO
is kind of misleading. It would "seem" to suggest dot product in notation
but it obviously isn't.

I hope things in syntax terms will change in perl6.

thanks
Saurabh
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to