As seen in the previous chapter, you can define your own variables in IFEFFIT simply like this:
Ifeffit> a = 1, b = 3 Ifeffit> c = (a + b)/2When you say something like this, it's pretty clear that the value of c should be 2. But a complication can arise when you redefine a variable. For example, if you now say
Ifeffit> b = 5Should c be 2 or 3? There are two slightly separate parts to this question, and both are important for understanding how IFEFFIT works: 1) should IFEFFIT store the value or the formula for a defined variable? and 2) if the formula is stored, how often should the value be re-evaluated?
The answer to question 1 is that IFEFFIT can store either the value or the formula for a variable. It stores the formula by default, which means that c will be 3. If you don't want the formula stored, but want the current value of an expression at the time of definition, use set() command should be used:
Ifeffit> set (c = (a + b)/2 )With set(), the formula is not stored, so no matter what values a or b are changed to, c will be 2.
This ``store the formula'' aspect of IFEFFIT is fairly unusual in data-processing programs and languages. IFEFFIT is primarily designed for complex data modeling, and this approach is extremely useful for setting up complex models for fitting.
Well, that brings us to the second question: if the formula is stored, when will c be re-evaluated? Normally, IFEFFIT automatically re-evaluates the variables for you. As you might imagine, if lots of interrelated formulas are stored, re-evaluating them to make sure they're all consistent can turn become complicated and inefficient. IFEFFIT tries to hide all this from you, and isn't as inefficient as you might guess3. It's possible that IFEFFIT can get confused about variables and their interdependencies. You can force the variables to be re-evaluated with the sync() command. sync() checks the dependencies of each variable (both scalars and arrays), re-orders the list of variables, and makes sure that the values are up-to-date. sync() is done internally at the beginning of many IFEFFIT commands. These are listed in chapter 9, and include def(), set(), plot(), write_data(), and the fitting commands (minimize() and feffit()). You should never need to use sync() explicitly, but it's there if you think you need it.
You can see all this in action with show @scalars and show @arrays, which list variable names, values, and formulas for scalars and arrays. In fact, the variable list shown is as re-ordered by sync() to reflect the order of inter-dependencies of variables, so that the variables can be correctly evaluated in one pass. Trivial formulas like `x = 1 + 1' are not stored, since IFEFFIT can easily tell that the value of x won't change.
Before moving on, let me try to clarify the distinction between set() and def() one more time. Everything you type at the command line is interpreted as a command. The default command is def(). When you type a = 1, b = 3, IFEFFIT first looks at a to see if it's a command. Recognizing that it is not, IFEFFIT translates the line to def(a = 1, b = 3). This has the consequences that you don't really need to type def(), though keeping in mind that it's really there all the time is generally a good idea.