管理员 on Sat, 10 Jan 2026 00:58:04 +0100


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

gtransall: recursive variant of gtrans


static GEN _gtransall_wrap(void *E, GEN x) {
	(void)E; return gtransall(x);
}

GEN gtransall(GEN x) {
	long i, j, l, lc, t = typ(x);
	GEN L, res;

	l = lg(x);
	switch (t) {
	case t_VEC: t = t_COL; break;
	case t_COL: t = t_VEC; break;
	case t_MAT:
		if (l < 2) return cgetg(1,t_MAT);
		lc = lgcols(x);
		res = cgetg(lc, t_MAT);
		for (i = 1; i < lc; i++) {
			GEN y = cgetg(l, t_COL);
			for (j = 1; j < l; j++) gel(y,j) = gtransall(gcoeff(x,i,j));
			gel(res,i) = y;
		}
		return res;
	case t_LIST:
		return genapply(NULL, _gtransall_wrap, x);
	default:
		/* t_CLOSURE uses t_VEC for internal, convert to t_COL is invalid */
		if (is_noncalc_t(t) || !is_recursive_t(t)) return gcopy(x);
	}

	res = cgetg(l, t);
	i = lontyp[t];
	memcpy(res + 1, x + 1, (i - 1)*sizeof(long));
	while (i < l) {
		gel(res,i) = gtransall(gel(x,i));
		i++;
	}
	return res;
}