Message Boards Message Boards

0
|
3163 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Message by applying a rule

Posted 11 years ago
Hello colleagues, please help me understand the message, I got from Mathematica.

I have a set of points imported from a file. It is list of { x , y } coordinates.
uzly = Import["nodes.dat"];
uzly[[1]] (* coordinatws of node no 1 *)
{0., 0.}
Dimensions[uzly]
{56, 2}

Second dataset describes which nodes are connected into a elemet (a tringle)
prvky = Import["elements.dat"];
prvky[[1]]  (* three nodes of element no 1 *)
{1, 5, 6}
Dimensions[prvky]
{78, 3}

I want to prepare polygons, that meansI want the three numbers in each element substitute with coordinates of the nodes with this numbers
I used a code

prvkyxy =
prvky /. {ni_, nj_, nk_} -> {uzly[[ni]], uzly[[nj]], uzly[[nk]]}
and the result seems to be OK:
prvkyxy[[1]] (* three {x y} coordinates of nodes of element no 1 *)
{{0., 0.}, {0.0111111, 0.}, {0.0111111, 0.0166667}}
Dimensions[prvkyxy]
{78, 3, 2}

But by applying the rule, I also got a message
Part::pspec: Part specification ni is neither a machine-sized integer nor a list of machine-sized integers. >>
Part::pspec: Part specification nj is neither a machine-sized integer nor a list of machine-sized integers. >>
Part::pspec: Part specification nk is neither a machine-sized integer nor a list of machine-sized integers. >>
General::stop: Further output of Part::pspec will be suppressed during this calculation. >>

which I cannot understand. The numbers are not out of bounds, the values of prvky are really integers and the result is what I am awaitng - so what is the problem? Why I got the message and what does it means?

Thanks!
POSTED BY: Tomáš Hruš
2 Replies
Use RuleDelayed (:>) instead of Rule (->).

Consider this simpler example of the problem:
myList = {1, 2, 3, 4};
1 /. x_ -> myList[[x]]
 
Compare with:
1 /. x_ :> myList[[x]]

Rule (->) causes the right hand side to immediately evaluate. It is similar to Set (=). RuleDelayed delays evaluation of the right hand side like SetDelayed (:=).
POSTED BY: Sean Clarke
Posted 11 years ago
Thank you Sean, It's clear!
POSTED BY: Tomáš Hruš
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract