How to append functions to a list of functions?

Click For Summary

Discussion Overview

The discussion revolves around the challenge of appending functions to a list of functions in Mathematica, while ensuring that a variable remains a parameter of those functions. Participants explore the implications of the code provided and suggest potential corrections or alternatives.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster (OP) describes their objective of creating a list of functions that can be used for operations like dot products and norms, while maintaining a variable as a parameter.
  • Some participants point out errors in the OP's code, particularly criticizing the use of a for loop and suggesting that the syntax may not align with Mathematica standards.
  • One participant suggests looking into function pointers in C as a potential solution, though another clarifies that the OP is using Mathematica, not C.
  • Another participant questions the OP's understanding of function definitions in Mathematica, indicating that the syntax used may not create a list of functions as intended.
  • The OP later confirms that they resolved their issue by defining the function Hyfield with respect to "a" and not declaring auxList as a function of "a".

Areas of Agreement / Disagreement

Participants express disagreement regarding the correctness of the OP's code and its interpretation. While some provide corrections and alternative suggestions, the discussion remains unresolved regarding the best approach to achieve the OP's goal.

Contextual Notes

There are limitations in the OP's code related to function definitions and variable handling in Mathematica, which some participants highlight. The discussion does not resolve these issues fully, leaving room for further exploration.

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
5K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K