2

[This question has been rewritten]

To simple down my last question, I think this example will be more clear. Suppose I have this list of functions list1[a] and append them to another list of functions list2[a].

list1[a_] := List[
   {(w + a), -d},
   {-a, -d},
   {-(w + a), -(t + d)},
   {-a, -(t + d)},
   {-a, -d},
   {(w - a), -d},
   {-a, -(t + d)},
   {(w - a), -(t + d)}
   ];

list2[a_] := List[];

For[i = 1, i <= 8, i++,
  AppendTo[list2[a], list1[a][[i]]]
  ];

Now, for example, I want to make a=1 and see list2. My problem is, when I do this:

list2[1]

I get

{}

Thank you for you patience!

RicardoP
  • 61
  • 1
  • 6
  • Without more info about your argument list I can't figure out what your function is trying to do. But does With[{list = Table[i a, 5]}, Table[c1* ArcCos[(list[[i]].list[[i + 1]])/(Norm[list[[i]]]* Norm[list[[i + 1]]])], {i, Length[list] - 1}] ] output something like what you're looking for (ie: a list of functions)? – aardvark2012 Oct 08 '17 at 21:56
  • And how does this question differ from this one? – aardvark2012 Oct 08 '17 at 22:11
  • Sorry!I was not very clear.In the end I want to have a function which is the sum of other functions.These other functions are built using the vectors I mentioned from the list I input as the first argument and the expression inside the append. All of these are always functions of a. The auxList is the list of these functions. Later I would want to define another function which is the sum of each of auxList elements. And this last function is still a function of "a".Before defining this last function,I used auxList[1] to see if it substituted the variable "a by one but return the empty brackets – RicardoP Oct 08 '17 at 22:12
  • Concerning your other question, that is my fault! I thought I hadn't post the question, after some time of no answers and posted it again (I was getting used to this forums quick answers), but people just hadn't seen the question yet. My bad! – RicardoP Oct 08 '17 at 22:16
  • Your use of Append seems wrong, I think you may mean AppendTo. But honestly, I can't figure out what question you are asking. Can you pare it down to a simple version? – bill s Oct 08 '17 at 22:28
  • I rewrote my question to make it more clear! Thank you – RicardoP Oct 08 '17 at 22:54
  • Have you looked at this? – aardvark2012 Oct 09 '17 at 03:28
  • list2[a_] := List[]; creates a "function" named list2 which, by default, returns List[]. Your For loop then overloads list2 for a specific case where the argument is explicitly a. After running your code then typing list2[a] a result identical to typing list1[a] is returned. Note that the definitions for list2[1] or list2[someotherargument] have not been modified. – LLlAMnYP Oct 12 '17 at 11:49

2 Answers2

1

With the revised question, it seems that what you want to do is to append list1 to itself 8 times. Here is a way to do that:

list1[a_] := List[{(w + a), -d}, {-a, -d}, {-(w + a), -(t + d)}, {-a, -(t + d)}, 
     {-a, -d}, {(w - a), -d}, {-a, -(t + d)}, {(w - a), -(t + d)}];
list2[a_] := ConstantArray[list1[a], 8]

Now when you type list2[a] you get the list of functions 8 times.

bill s
  • 68,936
  • 4
  • 101
  • 191
0
list1[a_] := List[
  {(w + a), -d},
  {-a, -d},
  {-(w + a), -(t + d)},
  {-a, -(t + d)},
  {-a, -d},
  {(w - a), -d},
  {-a, -(t + d)},
  {(w - a), -(t + d)}]

list2[a_] := List[]

For[i = 1, i <= 8, i++,
 list2[a_] := Evaluate[Append[
    DownValues[list2][[1, 2]], list1[a][[i]]]]]

list2[1]

{{1 + w, -d}, {-1, -d}, {-1 - w, -d - t}, {-1, -d - t}, {-1, -d}, {-1 + w, -d}, {-1, -d - t}, {-1 + w, -d - t}}

list1[1] == list2[1]

True

As an alternative to a For loop Array can be used here, e.g.

Array[(list2[a_] := Evaluate[Append[
       DownValues[list2][[1, 2]], list1[a][[#]]]]) &, 8];
Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
  • 1
    Generally it's not recommended to use For loop when you don't need to change the iterator (here i) in the loop body and don't need to check its value after the loop. Instead it's better to use Do. – Ruslan Feb 06 '18 at 15:56
  • @Ruslan - Yes, my preference would be to use Array, but I tend to show solutions with minimal code changes to address only the main point of difficulty and not distract. – Chris Degnen Feb 06 '18 at 16:36