This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
lua_scripting_tips [2021/12/12 15:09] stanzurek [Complex numbers] |
lua_scripting_tips [2025/03/09 10:59] (current) stanzurek [Rounding in LUA] |
||
|---|---|---|---|
| Line 2: | Line 2: | ||
| This page contains tips for using Lua 4.0 with FEMM 4.2. Some of these are documented in FEMM help file (User' | This page contains tips for using Lua 4.0 with FEMM 4.2. Some of these are documented in FEMM help file (User' | ||
| + | Full LUA 4.0 documentation: | ||
| ===== Comments ===== | ===== Comments ===== | ||
| Line 11: | Line 12: | ||
| ===== FOR loop and IF statement ===== | ===== FOR loop and IF statement ===== | ||
| + | |||
| + | The relational operators in Lua are | ||
| + | equal, not equal, smaller than, greater than, equal or smaller, equal or greater | ||
| + | == ~= < | ||
| + | |||
| + | |||
| Example of a FOR loop and an IF statement | Example of a FOR loop and an IF statement | ||
| <code lua> | <code lua> | ||
| Line 29: | Line 36: | ||
| There is no need to pre-declare the " | There is no need to pre-declare the " | ||
| + | |||
| + | ===== The functions of AND and OR ===== | ||
| + | < | ||
| + | if (n<15 and x>0) then | ||
| + | end | ||
| + | | ||
| + | if (n<15 or x>0) then | ||
| + | end | ||
| + | </ | ||
| ===== Multiple output values of function ===== | ===== Multiple output values of function ===== | ||
| Some functions return multiple values. | Some functions return multiple values. | ||
| Line 69: | Line 85: | ||
| '' | '' | ||
| - | ==== Sequential file names ==== | + | ===== Sequential file names ===== |
| Sometimes results need to be written into files whose numbers increase in a sequence. This can be achieved by using the structure as in the example below, where: ''" | Sometimes results need to be written into files whose numbers increase in a sequence. This can be achieved by using the structure as in the example below, where: ''" | ||
| Line 81: | Line 97: | ||
| end | end | ||
| + | | ||
| + | pause() -- this can be used for debugging | ||
| </ | </ | ||
| {{page> | {{page> | ||
| - | ==== Complex numbers ==== | + | ===== Complex numbers |
| Lua in FEMM handles complex numbers automatically, | Lua in FEMM handles complex numbers automatically, | ||
| - | < | + | < |
| a = 1 + I*2 | a = 1 + I*2 | ||
| b = 0.3 - I*0.4 | b = 0.3 - I*0.4 | ||
| Line 96: | Line 114: | ||
| im(b) = -0.4 | im(b) = -0.4 | ||
| abs(b) = 0.5 | abs(b) = 0.5 | ||
| + | </ | ||
| + | |||
| + | ===== Automatic limits in colour maps ===== | ||
| + | Full command executed with just the legend parameter set to -1 shows default limits for the legend. This seems to work in pure LUA, and mo_showdensityplot(-1) does not work even in pure LUA: | ||
| + | |||
| + | <code lua> | ||
| + | mo_showdensityplot(legend, | ||
| + | mo_showdensityplot(-1, | ||
| + | </ | ||
| + | |||
| + | legend: Set to 0 to hide the plot legend or 1 to show the plot legend. | ||
| + | |||
| + | (note that: '' | ||
| + | | ||
| + | |||
| + | ===== Rounding in LUA ===== | ||
| + | Rounding does not appear to be immediately available in LUA 4.0. Teh | ||
| + | |||
| + | <code lua> | ||
| + | function round_LUA(var_input, | ||
| + | |||
| + | var_floor = floor(var_input/ | ||
| + | var_ceil = ceil(var_input/ | ||
| + | var_rounded = var_floor | ||
| + | |||
| + | if (var_input > (var_floor + 5*round_precision/ | ||
| + | var_rounded = var_ceil | ||
| + | end | ||
| + | |||
| + | return(var_rounded) | ||
| + | |||
| + | end -- end function | ||
| + | |||
| + | var_input = 123.44900000003 | ||
| + | round_precision = 10 -- rounding precision, use: 100, 10, 1, 0.1, 0.01 etc. | ||
| + | |||
| + | var_output = round_LUA(var_input, | ||
| + | |||
| + | print(var_input) | ||
| + | print(var_output) | ||
| + | |||
| </ | </ | ||