0
$\begingroup$

I have a function which gives me all the coordinates of a certain bloc in a lattice (with the help of another function for rows and columns):

(*
  len = length of line
x0,y0 = position of first site
lc: orientation ("r"=row, "c"=colomn)
*)
line[len_, x0_: 0, y0_: 0, rc_: "r"] := Block[{linelist, i},

  If[rc == "r",
   linelist = Table[{rx, y0}, {rx, x0, x0 + len - 1}];
   ,
   linelist = Table[{x0, ry}, {ry, y0, y0 + len - 1}];
   ];

  linelist

  ]

(*
  len(i) = length of bloc in i direction
x0,y0 = position of first site (down-left corner)
*)
bloc[lenx_, leny_, x0_: 0, y0_: 0] := Block[{bloclist},

  bloclist = {};

  For[i = y0, i < y0 + leny, i++,
   Block[{},
    bloclist = Level[Append[bloclist, line[lenx, x0, i, "r"]], {-2}];
    ]
   ];

  bloclist

  ]

The problem is, when I try do make this table

Table[bloc[a, i, 0, 7], {i, 1, 1}, {a, 1, 2}]

it gives me

{{{{0, 7}}, {{0, 7}, {1, 7}, {0, 8}, {1, 8}, {0, 9}, {1, 9}, {0, 
10}, {1, 10}, {0, 11}, {1, 11}, {0, 12}, {1, 12}, {0, 13}, {1, 
13}, {0, 14}, {1, 14}}}}

instead of

{{{{0,7}},{{0,7},{1,7}}}}

Where do all those other terms come from? How can I fix this?

(which is a simplified version of what I must do):

$\endgroup$
  • $\begingroup$ Maybe because of the way you did the Table. If you evaluate this: Table[bloc[a, 1, 0, 7], {a, 1, 2}], it gives you (mostly) what you want. There is one less level than what you originally expected. {{{0, 7}}, {{0, 7}, {1, 7}}} $\endgroup$ – Mark R Jun 12 '19 at 21:25
  • $\begingroup$ That said, the experiment: Table[myfun[a, i, 0, 7], {i, 1, 1}, {a, 1, 2}] yields the same as Table[myfun[a,1,0,7],{a,1,2}] so what you tried to do was reasonable. Namely, {myfun[1, 1, 0, 7], myfun[2, 1, 0, 7]} $\endgroup$ – Mark R Jun 12 '19 at 21:30
  • $\begingroup$ Actually, the outputs from the two are slightly different. Table[myfun[a, i, 0, 7], {i, 1, 1}, {a, 1, 2}] gives {{myfun[1, 1, 0, 7], myfun[2, 1, 0, 7]}} whereas Table[myfun[a, 1, 0, 7], {a, 1, 2}] gives {myfun[1, 1, 0, 7], myfun[2, 1, 0, 7]}. $\endgroup$ – Mark R Jun 12 '19 at 22:01
0
$\begingroup$

I figured it out, it was simply because I already defined the "i" in the bloc function and used it after to calculate my table. It worked when I changed it.

$\endgroup$
0
$\begingroup$

At any rate, changing up the functions a little gives the expected result.

line[len_, x0_: 0, y0_: 0, "r"] := Table[{rx, y0}, {rx, x0, x0 + len - 1}]
line[len_, x0_: 0, y0_: 0, rc_?Except["r"]] := Table[{rx, y0}, {rx, x0, x0 + len - 1}]
bloc[lenx_, leny_, x0_: 0, y0_: 0] := Block[{i = y0}, Nest[Level[Append[#, line[lenx, x0, i++, "r"]], {-2}] &, {}, leny]]

Table[bloc[a, i, 0, 7], {i, 1, 1}, {a, 1, 2}]

{{{{0, 7}}, {{0, 7}, {1, 7}}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.