Mathematica How to append functions to a list of functions?

Click For Summary
The discussion centers on creating a list of functions in Mathematica and ensuring that a variable can be used consistently across these functions. The original code attempts to append functions to a list but encounters issues with variable scope and function definitions. Suggestions include using anonymous functions, known as pure functions in Mathematica, to properly manage the variable 'a'. The original poster ultimately resolved their issue by defining the main function, Hyfield, as a function of 'a' and not declaring auxList as a function of 'a'. This adjustment allowed for successful appending and usage of the functions.
RicardoMP
Messages
48
Reaction score
2
My objective is to make a list of functions and afterwards be able to make operations with those functions.


Code:
  Hyfield[list_, bits_] := Module[{i, auxList, Hy},
      auxList[a_] := List[];
      For[i = 1, i <= bits*2, i++,
       auxList[a] =
        Append[auxList[a],
         c1*ArcCos[(list[a][[i]].list[a][[i + 1]])/(Norm[list[a][[i]]]*
              Norm[list[a][[i + 1]]])]]
       ];
      Return[auxList[a]]
      ]
In the above code, I receive a list of functions like this one:


Code:
bits2[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)}
   ];

These are vectors which I'll want to use to build other functions using dot product and norms. However, while testing this, I wanted to make sure that variable a is always a variable of these functions, so I can, for example, plot my functions later. If I use, for example,
Code:
    Hyfield[bits2, 2]

The function returns the list of the functions I appended.
However, if I return auxList[1], for example, (or any other number), I get

Code:
  {}
How can I keep adding (appending) my functions to the list and still be able to use variable a? Basically, can I still use my list of functions as a function of variable a, after appending other functions, also of variable a?
Thank you
 
Physics news on Phys.org
Sorry, there are errors in your code (a for loop is never coded that way).

Otherwise read up on pointers to functions (https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work). They prefer typedefs (and so do I)

Code:
typedef int (*myFuncDef)(int, int);

With that in mind, you can create a struct containing a pointer to a function and the necessary parameters. You can even create a union denoting different function types and different sets of parameters. Arrays or linked lists of those...
 
I think the OP is using another tool, not C. Although C is ideal for that sort of thing.
 
Last edited:
Don't know Mathematica, but maybe it's worth checking if some of what you've written really means what you think it does. For example,
RicardoMP said:
In the above code, I receive a list of functions like this one:
Code:
bits2[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)}
   ];
According to the Mathematica documentation f[a_] := ... is the syntax for defining a function taking a parameter a, so what you've written above would define bits2 as a single function that returns a list (depending on the value of the parameter a), and not a list of functions. Likewise in your code, you have
Code:
     auxList[a_] := List[]
... which would make auxList a function that evaluates to an empty list regardless of what the parameter a is, after which I have no idea what you're expecting
Code:
       auxList[a] = ...
in your loop to do.

If you actually want to create lots of new functions in a loop and return a list of them then maybe the thing to look up is how to create anonymous functions. Apparently these are called "pure functions" in Mathematica: https://reference.wolfram.com/language/tutorial/PureFunctions.html.
 
jim mcnamara said:
I think the OP is using another tool, not C. Although C is ideal for that sort of thing.
Indeed! I'm using Mathematica! Meanwhile, I recently solved my problem. I simply defined Hyfield as a function of "a" and I avoid declaring auxlist as a function of a. That did the trick!
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 0 ·
Replies
0
Views
837
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 13 ·
Replies
13
Views
2K