Question about updating functions

  • Mathematica
  • Thread starter member 428835
  • Start date
  • Tags
    Functions
In summary, when using Mathematica, the := operator creates a mapping between a function and its value without immediately evaluating the right side. This allows for the function to be recalled and evaluated later. The = operator, on the other hand, immediately evaluates the right side and maps the result to the function. So when using the := operator, the first time a function is called, it calculates and stores the value, and all subsequent calls simply recall the stored value without re-calculation. This is not a bug, but rather the correct behavior.
  • #1
member 428835
Hi PF!

When using Mathematica I input the code
Code:
f1[a_, n_] := 
 f1[a, n] = 
  Join[Table[LegendreP[k, x], {k, 0, n, 1}], 
   Table[LegendreP[k, x], {k, 1, n, 1}]]
Then when I type ##f1[1, 3] ## I get an output. I then change the second table of ##f1## to start at 0 instead of 1, recompute that, and then recompute ##f1[1, 3] ## yet I get the same output. Is there something I need to understand here or is this a bug?
 
Physics news on Phys.org
  • #2
This is not a bug, this is correct behavior. When you type f1[1,3] the first time then it calculates it and stores the value. Then it simply recalls the stored value next time you type f1[1,3], so it never re calculates it. The fact that you changed the calculation doesn't clear the stored value.
 
  • #3
Dale said:
This is not a bug, this is correct behavior. When you type f1[1,3] the first time then it calculates it and stores the value. Then it simply recalls the stored value next time you type f1[1,3], so it never re calculates it. The fact that you changed the calculation doesn't clear the stored value.
Sorry if this is redundant, but I want to make sure I'm understanding you: I type the ##f1:=...##, execute the command, then execute ##f1[1,3]##. Then change ##f1:=...## and re-execute that command, then again execute ##f1[1,3]##, and the answer will always be the same as the first time?

Thanks for your speedy response.
 
  • #4
joshmccraney said:
the answer will always be the same as the first time?
Yes.

The := operator and the = operator both set a mapping from the symbol on the left to the symbol on the right. The difference is that the = symbol immediately evaluates the right side and then maps the left side to the result of he evaluation. The := symbol does not evaluate the right side, but makes the mapping and waits for the left side to appear before evaluation.

So when I type f[x_] := f[x] = 2x it makes a mapping between f[x_] and f[x] = 2*4. The right side is not evaluated, but is kept just as code. Later, if I write f[4] then the computer recognizes that as a symbol matching f[x_] with x set to 4. So it pulls up the right side that is associated with f[x_] and only now evaluates it but with all occurrences of x replaced by 4. So it now evaluates f[4]=2*4. This time the right side is evaluated immediately returning 8, and a new mapping from f[4] to 8 is stored.

Now, if f[4] is called again then that matches both f[x_] and f[4] but the rule is that the most specific mapping takes precedence, so the symbol is matched to 8, which is returned immediately without recalculation.
 
  • Like
Likes member 428835
  • #5
Very good explanation! Thanks so much!
 

Related to Question about updating functions

What is the purpose of updating functions?

The purpose of updating functions is to modify the behavior or output of an existing function. This can be useful for fixing bugs, improving performance, or adding new features to the function.

How do you update a function?

To update a function, you can either manually make changes to the code within the function or create a new function that includes the desired updates. The updated function can then be called in place of the old function.

What are some common reasons for updating functions?

Some common reasons for updating functions include fixing errors or bugs, optimizing the code for better performance, adding new functionality, or making the function more user-friendly.

Do you need to update all functions in a program?

No, it is not necessary to update all functions in a program. You should only update functions that require changes or improvements to their behavior. If a function is working properly, there is no need to update it.

What should you consider before updating a function?

Before updating a function, you should consider the potential impact of the changes on other parts of the program. You should also test the updated function thoroughly to ensure it works as intended and does not introduce any new errors or bugs.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
850
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
419
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
550
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
596
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top