New to using ref()/unref() in G'MIC math parser

I wanted to use non-const variables, so how exactly how I can get this to work?

foo:

eval "
	const s=4;
	const n=10;
	m=expr('v(100)',prod(s,n));
	print(m);
	driver(a,b,c,nb_elts)=(
		ref(m[a,b],temp);
		temp=sort(temp,1,nb_elts,s,c);
		print(temp);
		unref(temp);
	);
	section_sort(a,b,c)=(
		driver(a*s,(b-a)*s,c,b-a);
	);
	v0=3;v1=6;
	section_sort(v0,v1,0);
	v0=4;v1=9;
	section_sort(a,b,1);
	"

The idea is to sort a section of a vector. I think the copy adjusting memory thing could be key to this. I know you can touch const with that.

Here is what I did:

foo:

eval "
	const v0=0;
	const v1=0;
	for(p=0,p<2,++p,
		copy(addr(v0),p);
		copy(addr(v1),p+1);
		print(v0);
		print(v1);
	);
	print(isconst(v0));
	print(isconst(v1));
	"

This proves you can touch const with copy(). But, I did noticed G’MIC only knows the initial declaration is a const.

I think I found my way around this issue. I can always use merge sort algorithm and insertion sort algorithm to sort pixels. It would be ideal if I didn’t had to do that, but well, ok.

FYI ref() and unref() are quite basic functions to deal with the naming of slots for the math parser memory.

ref(A,varname) is roughly equivalent to unref(varname); varname = A.
So, ref(m[a,b],temp) will not make temp shares its values with m (because m[a,b] is actually already a copy in memory of the subvector or m).

But you can use unref() to deal with constants, with the same name, but with different values (though for code readability, it’s not recommended).
Like :

foo:
  eval "
    driver(a,b,c,nb_elts)=(
      ref(m[a,b],temp);
      temp=sort(temp,1,nb_elts,s,c);
      print(temp);
      unref(temp);
    );

    section_sort(a,b,c)=(
      driver(a*s,(b-a)*s,c,b-a);
    );

    const s = 4;
    const n = 10;
    m = expr('v(100)',prod(s,n));
    print(m);

    const v0 = 3; const v1 = 6;
    section_sort(v0,v1,0); # Here, v0 and v1 are constant 3 and 6, when expanding macro 'section_sort'

    unref(v0); unref(v1);
    const v0 = 4; const v1 = 9;
    section_sort(v0,v1,1) # Here, v0 and v1 are constant 4 and 9, when expanding macro 'section_sort'
  "

It is not a way to override the const qualifier in general. It is just a way to tell the math compiler that const values have particular values during the compilation of the expression part where they are defined.

That might or might not very useful, but instead of using that, I use const as a condition to define other consts.

That’s better yes.
As long as you deal with const values and regular math operations (non stochastic), you can define new const values from previous const, like:

const x = $1;
const y = cos(x) + 3*(x - 2);  # OK, that can be evaluated at compile time
const z = x + u;  # Not OK. 'u' is not a const value