I wanted to make a specific post about this, as I know there are heavy/advanced users of the math parser here, including @Reptorian, @grosgood and @afre among others.
This morning, I’ve just added two new functions to the G’MIC math parser, and I believe they will be quite useful in practice.
Both are quite similar and have two valid signatures:
-
fill(target,loop_varname,expression)
andfill(target,loop_varname,expression)
fill the objecttarget
(useful mainly for vector-valued objects) with the given expression, with eventually specifying a name for the index counter.
For instance:
eval "
V = vector8();
fill(V,k,k<4?k^2:k-4);
print(V);
fill(V,round(u(100)));
print(V)"
which gives:
[gmic]-0./ Start G'MIC interpreter.
[gmic_math_parser] V = [ 0,1,4,9,0,1,2,3 ] (size: 8)
[gmic_math_parser] V = [ 40,41,15,23,14,26,81,30 ] (size: 8)
[gmic]-0./ End G'MIC interpreter.
- And the other function,
repeat()
, works almost the same, it is just equivalent to afor (k = 0, k<nb_iter, ++k)
(but more faster and shorter to write). It has also two possible signatures:
eval "
repeat (10,k,print(k)); # eq. to 'for (k = 0, k<10, ++k, print(k));'
repeat (10,print(u)); # eq. to 'for (k = 0, k<10, ++k, print(u));'
"
The second variant of these function is useful when no counter variable is actually needed.
Note that when specifying a variable, it is *not local to the command call, meaning that:
eval "
repeat (10,foo,print(foo));
echo('foo is now ',foo);
"
gives:
gmic]-0./ Start G'MIC interpreter.
[gmic_math_parser] foo = 0
[gmic_math_parser] foo = 1
[gmic_math_parser] foo = 2
[gmic_math_parser] foo = 3
[gmic_math_parser] foo = 4
[gmic_math_parser] foo = 5
[gmic_math_parser] foo = 6
[gmic_math_parser] foo = 7
[gmic_math_parser] foo = 8
[gmic_math_parser] foo = 9
foo is now 9
[gmic]-0./ End G'MIC interpreter.
Also functions break()
and continue()
can be used in the fill()
and repeat()
functions to stop at a given iteration, or continue to the next iteration (in the case of fill()
, using continue()
won’t set the value V[k]
for the kth iteration).
Well, that’s it for now. I’ll probably use those functions a lot in the future.
I’m currently trying to implement a neural-network library inside G’MIC, and these functions will help, for sure.