4. Examples of use#
For various examples, refer to test case ZZZZ100A.
4.1. A formula is used like a tabulated function#
Definition of the Sia formula:
SIa = FORMULE (NOM_PARA =”X”, VALE =”sin (X) “)
Equivalent tabulated function IF:
LR = DEFI_LIST_REEL (DEBUT = 0. ,
INTERVALLE = _F (JUSQU_A = pi, PAS = 0.01)
IF = CALC_FONC_INTERP (FONCTION = SIa,
LIST_PARA = LR,
NOM_PARA = “X”,
NOM_RESU = “DEPL”,)
To define a tabulated function based on an interpretable formula in this way, see CALC_FONC_INTERP [U4.32.01].
Use of SI or SIa in a simple keyword waiting for a function or a formula:
field= CREA_CHAMP (… AFFE = _F (… VALE_F = SI or SIa,))
4.2. A formula can be evaluated as a real#
In the body of the command file:
SIa = FORMULE (NOM_PARA =”X”, VALE =”sin (X) “)
X = SIa (1.57)
print SIa (1.57)
Behind a simple keyword waiting for a real one:
LR = DEFI_LIST_REEL (DEBUT = SIa (0. ),
INTERVALLE =_F (JUSQU_A = SIa (ft/2.), PAS =0.01))
In another formula:
SIb = FORMULE (NOM_PARA =”X”, VALE =”X* SIa (5. ) “, SIa = SIa)
4.3. Invoke a formula or function in another formula#
SIa = FORMULE (NOM_PARA =”X”, VALE =”sin (X) “)
Be careful to remember to put the argument (X here) in the call to function SIa:
SIb = FORMULE (NOM_PARA =”X”, VALE =”X* SIa (X) “, SIa = SIa)
4.4. Formula with several parameters#
NAP = FORMULE (NOM_PARA =( “AMOR”, “FREQ”),
VALE = »” (1./ ((2. pi FREQ) **2 - OMEGA**2) **2
(2). AMOR*2. *pi FREQ *OMEGA)**2) « ”,
OMEGA = OMEGA)
In this example, we define a formula with 2 parameters. Given the length of the expression, it is written for convenience over several lines with triple quotes to delimit it it. The constant pi is a standard constant (see paragraph [§3.2]), the constant OMEGAdoit must be provided explicitly.
In the current state, only the formulas for \(\mathrm{ℝ}\) in \(\mathrm{ℝ}\) or \(\mathrm{ℂ}\) are possible: a single scalar product.
4.5. Formula resulting from programming a Python function#
You can refer to functions programmed in Python in a formula, which allows formulas that are much more complex than simple algebraic expressions.
For example a Heaviside function:
\(\mathrm{HEAVYSIDE}(x)=\{\begin{array}{c}0.\mathrm{si}x<0.\\ 1.\mathrm{si}x\ge 0.\end{array}\)
The Python function is programmed as follows:
by heaviside (s):
if x < 0. :
Return 0.
Else:
Return 1.
F_ HVS = FORMULE (NOM_PARA =” INST “,
VALE =”heaviside (INST) “,
heaviside=heaviside)
Attention:
The use of Python programming in the command file (here the heaviside method) is incompatible with editing this file in graphical mode with asterStudy.
For the F_ HVS formula to be evaluated in POURSUITE, you need to redefine the Heaviside function and declare it to the formula:
by heaviside (s):
if x < 0. :
Return 0.
Else:
Return 1.
F_ HVS .setContext (dict (heaviside=heaviside))
4.6. Example of defining formulas in a Python loop#
When you define, in a loop, formulas whose expression depends on the index of the loop, you see the advantage of explicitly passing the constants.
Example:
For in range (3):
FO [i] = FORMULE (VALE =”cos (i* INST) “, NOM_PARA =” INST”, i=i)
CH [i] = CREA_CHAMP (OPERATION =” AFFE “,…, VALE_F =FO [i])
With these instructions, we defined 3 formulas that all have the same expression.
At each iteration, a different value is stored for argument i. When the formula is evaluated and then assigned the values in the field, there is no ambiguity about the value of i. This is the value attached to the formula when it was created, not the current value of i.
In fact, in PAR_LOT =” OUI “mode, all commands are created, and only then executed. In this mode, i is 2 during the actual execution of the commands.