2
$\begingroup$

[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!

$\endgroup$
  • $\begingroup$ 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)? $\endgroup$ – aardvark2012 Oct 8 '17 at 21:56
  • $\begingroup$ And how does this question differ from this one? $\endgroup$ – aardvark2012 Oct 8 '17 at 22:11
  • $\begingroup$ 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 $\endgroup$ – RicardoP Oct 8 '17 at 22:12
  • $\begingroup$ 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! $\endgroup$ – RicardoP Oct 8 '17 at 22:16
  • $\begingroup$ 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? $\endgroup$ – bill s Oct 8 '17 at 22:28
1
$\begingroup$

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.

$\endgroup$
0
$\begingroup$
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];
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Ruslan Feb 6 '18 at 15:56
  • $\begingroup$ @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. $\endgroup$ – Chris Degnen Feb 6 '18 at 16:36

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.