Charles Greathouse on Mon, 22 Feb 2010 05:54:25 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Getting the variable number of a lexical local variable


I still can't seem to make it work properly with multiple variables.
If I'm in a loop with formal variable y and I check polcoeff0(expr, 3,
-1) with expr = 3*y + 5*x, it gives me 5, not 3.  I would like a way
to get 3 when the formal variable is y and 5 when the formal variable
is x.  Can this be done?

I understand that there are no multivariable polynomials in Pari, so
that 3*y may simply be the constant term of a polynomial in x.  But at
the moment I can't even figure out whether my function was called with
sumformal(y=1, n, 3*y + 5*x)
or
sumformal(y=1, n, 3*y + 5*x)
.


I've attached my source, if this would help anyone understand what I'm
doing.  Faulhaber(n, a) returns the n-Faulhaber polynomial (in a).

GEN
sumformal(GEN start, GEN end, GEN expr)
{
	pari_sp ltop = avma;
	GEN c, F, res = gen_0, ret;
	GEN x = pol_x(-1);
	long t = typ(expr);
	if (t == t_INT || t == t_REAL || t == t_COMPLEX)
	{
		ret = gmul(gaddgs(gsub(end, start), 1), expr);
		ret = gerepileupto(ltop, ret);
		return ret;
	}
	if (t != t_POL)
		pari_err(notpoler, "sumformal; can only handle polynomials, not
arbitrary functions");

	pari_sp btop = avma, st_lim = stack_lim(btop, 1);
	long d = degree(expr) + 1;
	while (--d)
	{
		c = truecoeff(expr, d);
		F = Faulhaber(d, x);
		res = gadd(res, gmul(c, gsub(gsubstpol(F, x, end), gsubstpol(F, x,
gsubgs(start, 1)))));
		if (low_stack(st_lim, stack_lim(btop, 1)))
			gerepileall(btop, 4, &d, &c, &res, &expr);
	}
	ret = gadd(res, gmul(gaddgs(gsub(end, start), 1), truecoeff(expr, 0)));
	ret = gerepileupto(ltop, ret);
	return ret;
}

Charles Greathouse
Analyst/Programmer
Case Western Reserve University

On Wed, Feb 17, 2010 at 2:37 AM, Bill Allombert
<Bill.Allombert@math.u-bordeaux1.fr> wrote:
> On Tue, Feb 16, 2010 at 11:10:43PM -0500, Charles Greathouse wrote:
>> I'm trying to get the variable number of a lexical variable, but I
>> can't seem to figure out how to do that.
>>
>> If I have a function with a V in the prototype, like a custom for
>> loop, how do I know which variable was used?
>
> This is always -1.
>
> See
> ??"Functions to deal with lexical local variables"@5
>
>> In my case, I would like to be able to distinguish between calls to
>> sumformal(x=1, N, x^2 * y)
>> and
>> sumformal(y=1, N, x^2 * y)
>
> What is the difference with sum() ?
>
> Cheers,
> Bill
>