Question about updating functions

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Functions
Click For Summary

Discussion Overview

The discussion revolves around the behavior of function definitions in Mathematica, specifically regarding the use of the delayed assignment operator (:=) and the immediate assignment operator (=). Participants explore how changes to the function definition affect previously computed outputs.

Discussion Character

  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant describes their experience with defining a function using the delayed assignment operator and observes that changing the function definition does not affect the output of previously computed calls.
  • Another participant explains that the behavior is expected, as the function stores the computed value upon first execution and does not recalculate unless the stored value is cleared.
  • A further clarification is provided about the difference between the := and = operators, detailing how they handle evaluation and mapping of function definitions.
  • One participant confirms their understanding of the explanation regarding the behavior of function calls and mappings in Mathematica.

Areas of Agreement / Disagreement

Participants generally agree on the behavior of function definitions and the implications of using different assignment operators in Mathematica. There is no significant disagreement noted in the discussion.

Contextual Notes

None.

Who May Find This Useful

This discussion may be useful for users of Mathematica who are learning about function definitions, particularly those interested in understanding the implications of using different assignment operators.

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
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.
 
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.
 
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   Reactions: member 428835
Very good explanation! Thanks so much!
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K