Closed mihaibujanca closed 7 years ago
When you say to iterate through all graph nodes, do you mean you have some Graph like:
G = Graph("G", 3, "v0", 4, "v1", 5, "v2", 6 --etcetera
and you want some iterator through G.v0,G.v1,G.v2, etc?
If so, we do not currently have a built-in way of doing that, but since the Energy is written directly in Lua, you could make it slightly less painful then unrolling your loop by something like:
nodes = {"v0","v1","v2",...etc}
for _,n in nodes do
-- Stuff with G[n]
end
Yeah I guess that works. Might be worth adding to the readme / wiki perhaps :).
Thanks!
Done. And its a good idea and straightforward, so I will add an iterator soon.
Hi. I tried the following code and am getting attempt to call a table value
local G = Graph("G", 7,
"v", {N}, 8,
"n0", {D}, 9,
"n1", {D}, 10,
"n2", {D}, 11,
"n3", {D}, 12,
"n4", {D}, 13,
"n5", {D}, 14,
"n6", {D}, 15,
"n7", {D}, 16)
local weightedTranslation = 0
nodes = {"n0","n1","n2","n3","n4","n5","n6","n7"}
for _,n in nodes do -- <--FAILS HERE
weightedTranslation = weightedTranslation + TranslationDeform(G[n])
end
Ah, thats what I get for writing pseudocode. To iterate through an array in lua you need to use ipairs().
for _,n in ipairs(nodes) do -- <--FAILS HERE
weightedTranslation = weightedTranslation + TranslationDeform(G[n])
end
Haha found it in the meanwhile and was about to post.
The other thing is that now the final costs seem to be different for some reason.
The initial code was
weightedTranslation = weightedTranslation + Weights(G.v)(0) * TranslationDeform(G.n0)
weightedTranslation = weightedTranslation + Weights(G.v)(1) * TranslationDeform(G.n1)
weightedTranslation = weightedTranslation + Weights(G.v)(2) * TranslationDeform(G.n2)
weightedTranslation = weightedTranslation + Weights(G.v)(3) * TranslationDeform(G.n3)
weightedTranslation = weightedTranslation + Weights(G.v)(4) * TranslationDeform(G.n4)
weightedTranslation = weightedTranslation + Weights(G.v)(5) * TranslationDeform(G.n5)
weightedTranslation = weightedTranslation + Weights(G.v)(6) * TranslationDeform(G.n6)
weightedTranslation = weightedTranslation + Weights(G.v)(7) * TranslationDeform(G.n7)
Now it is
nodes = {"0","1","2","3","4","5","6","7"}
for _,i in ipairs(nodes) do
weightedTranslation = weightedTranslation + Weights(G.v)(i) * TranslationDeform(G["n"..i])
end
Both times weightedTranslation is initialised to 0.
Ah never mind it's because I was using "0"..."7"
rather than 0...
I was about to guess that :)
But that is a good candidate for a useful error message, I'm surprised it compiled like that.
I was about to ask which one is the correct one to use, I'm guessing it's the integer rather than the string
Yes, integer indices to access component. You may need to write TranslationDeform(G["n"..tostring(i)])
Seems to work without tostring
but added it in for correctness
Hi there, Is there any easy way to iterate through all graps nodes? I checked the examples and the README, but couldn't find anything.