管理员 on Fri, 02 Jan 2026 14:30:21 +0100


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

Implementation of gabscmp function


static int is_complex(GEN x) {
	long t = typ(x);
	return t == t_COMPLEX || (t == t_QUAD && signe(gel(gel(x,1),2)) > 0);
}

static int abscmpfrac(GEN x, GEN y) {
	pari_sp av = get_avma();
	return gc_int(av, abscmpii(mulii(gel(x,1),gel(y,2)),mulii(gel(y,1),gel(x,2))));
}

int gabscmp(GEN x, GEN y) {
	pari_sp av;
	long tx, ty, sx, sy;
	int res = 0;

	tx = typ(x); ty = typ(y);

	if (tx == ty) {
		switch (tx) {
		case t_INT: return abscmpii(x, y);
		case t_REAL: return abscmprr(x, y);
		case t_FRAC: return abscmpfrac(x, y);
		/* case t_PADIC: use valcmp instead */
		/* case t_STR: use cmp instead */
		case t_INFINITY: return 0;
		}
	}

	if (tx == t_INFINITY) return 1;
	if (ty == t_INFINITY) return -1;

	/* generic case */
	av = get_avma();
	sx = is_complex(x);
	sy = is_complex(y);
	if (sx || sy) {
		x = sx ? gnorm(x) : gsqr(x);
		y = sy ? gnorm(y) : gsqr(y);
		res = gcmp(x, y);
	} else {
		/* more efficient than gcmp(gsqr(x),gsqr(y)) */
		if (tx == t_QUAD || ty == t_QUAD) {
			sx = gsigne(gadd(x, y));
		} else {
			sx = gsigne(x);
			sy = gsigne(y);
			if (!sx) return sy ? -1 : 0;
			if (!sy) return 1;
			if (sx != sy) y = gneg_i(y);
		}
		res = sx*gcmp(x, y);
	}

	return gc_int(av, res);
}