How to append functions to a list of functions?

In summary, 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]]
  • #1
RicardoMP
49
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
  • #2
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...
 
  • #3
I think the OP is using another tool, not C. Although C is ideal for that sort of thing.
 
Last edited:
  • #4
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.
 
  • #5
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!
 

Related to How to append functions to a list of functions?

1. How do I append a function to a list of functions in Python?

To append a function to a list of functions in Python, you can use the append() method. This method takes in a function as an argument and adds it to the end of the list. For example, my_list.append(my_function) will add the my_function to the end of the my_list.

2. Can I append multiple functions to a list of functions at once?

Yes, you can append multiple functions to a list of functions at once using the extend() method. This method takes in an iterable of functions and adds them to the end of the list. For example, my_list.extend([func1, func2, func3]) will add func1, func2, and func3 to the end of my_list.

3. How do I insert a function at a specific index in a list of functions?

You can use the insert() method to insert a function at a specific index in a list of functions. This method takes in two arguments - the index where you want to insert the function and the function itself. For example, my_list.insert(2, my_function) will insert my_function at index 2 in my_list.

4. Is it possible to remove a function from a list of functions?

Yes, you can remove a function from a list of functions using the remove() method. This method takes in the function you want to remove as an argument and removes the first occurrence of that function from the list. For example, my_list.remove(my_function) will remove the first occurrence of my_function from my_list.

5. Can I use a function as a key in a dictionary?

Yes, you can use a function as a key in a dictionary. Since functions are objects in Python, they can be used as dictionary keys just like any other object. However, keep in mind that functions are mutable, so if the function is modified, its key value may change as well.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
677
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
Back
Top