Beispiel #1
0
value value_drop(value op, value n)
{
	if (op.type == VALUE_NIL) {
		return value_init_nil();
	} else if (op.type == VALUE_ARY) {
		if (n.type == VALUE_MPZ) {
			value length = value_set_long(op.core.u_a.length);
			value res = value_range(op, n, length);
			value_clear(&length);
			return res;
		}
	} else if (op.type == VALUE_LST) {
		if (n.type == VALUE_MPZ) {
			if (value_lt(n, value_zero)) {
				value_error(1, "Domain Error: drop() is undefined where n is %s (>= 0 expected).", n);
				return value_init_error();
			} else if (value_gt(n, value_int_max)) {
				value_error(1, "Domain Error: drop() is undefined where n is %s (<= %s expected).", n, value_int_max);
				return value_init_error();				
			}
			size_t i, max = value_get_long(n);
			value ptr = op;
			for (i = 0; i < max && ptr.type == VALUE_LST; ++i) {
				ptr = ptr.core.u_l[1];
			}
			
			return value_set(ptr);
		}
	} else if (op.type == VALUE_PAR) {
		if (n.type == VALUE_MPZ) {
			if (value_lt(n, value_zero)) {
				value_error(1, "Domain Error: drop() is undefined where n is %s (>= 0 expected).", n);
				return value_init_error();
			} else if (value_gt(n, value_int_max)) {
				value_error(1, "Domain Error: drop() is undefined where n is %s (<= %s expected).", n, value_int_max);
				return value_init_error();				
			}
			size_t i, max = value_get_long(n);
			value ptr = op;
			for (i = 0; i < max && ptr.type == VALUE_PAR; ++i) {
				ptr = ptr.core.u_p->tail;
			}
			
			return value_set(ptr);
		}
	} else {
		value_error(1, "Type Error: drop() is undefined where op1 is %ts (array or list expected).", op);
		if (n.type == VALUE_MPZ)
			return value_init_error();
	}
	
	value_error(1, "Type Error: drop() is undefined where op2 is %ts (integer expected).", n);
	return value_init_error();
}
enum lp_result PL_polyhedron_opt(Polyhedron *P, Value *obj, Value denom,
				enum lp_dir dir, Value *opt)
{
    int i;
    int first = 1;
    Value val, d;
    enum lp_result res = lp_empty;

    POL_ENSURE_VERTICES(P);
    if (emptyQ(P))
	return res;

    value_init(val);
    value_init(d);
    for (i = 0; i < P->NbRays; ++ i) {
	Inner_Product(P->Ray[i]+1, obj, P->Dimension+1, &val);
	if (value_zero_p(P->Ray[i][0]) && value_notzero_p(val)) {
	    res = lp_unbounded;
	    break;
	}
	if (value_zero_p(P->Ray[i][1+P->Dimension])) {
	    if ((dir == lp_min && value_neg_p(val)) ||
		(dir == lp_max && value_pos_p(val))) {
		res = lp_unbounded;
		break;
	    }
	} else {
	    res = lp_ok;
	    value_multiply(d, denom, P->Ray[i][1+P->Dimension]);
	    if (dir == lp_min)
		mpz_cdiv_q(val, val, d);
	    else
		mpz_fdiv_q(val, val, d);
	    if (first || (dir == lp_min ? value_lt(val, *opt) :
				          value_gt(val, *opt)))
		value_assign(*opt, val);
	    first = 0;
	}
    }
    value_clear(d);
    value_clear(val);

    return res;
}
Beispiel #3
0
value value_take(value op, value n)
{
	if (op.type == VALUE_NIL) {
		return value_init_nil();
	} else if (op.type == VALUE_ARY) {
		if (n.type == VALUE_MPZ) {
			value start = value_set_long(0);
			value res = value_range(op, start, n);
			value_clear(&start);
			return res;
		}
	} else if (op.type == VALUE_LST) {
		if (n.type == VALUE_MPZ) {
			if (value_lt(n, value_zero)) {
				value_error(1, "Domain Error: drop() is undefined where n is %s (>= 0 expected).", n);
				return value_init_error();
			} else if (value_gt(n, value_int_max)) {
				value_error(1, "Domain Error: drop() is undefined where n is %s (<= %s expected).", n, value_int_max);
				return value_init_error();				
			}
			
			value res = value_init_nil();
			size_t i, max = value_get_long(n);
			value ptr = op;
			for (i = 0; i < max && ptr.type == VALUE_LST; ++i) {
				value_cons_now(ptr.core.u_l[0], &res);
				ptr = ptr.core.u_l[1];
			}
			
			value_reverse_now(&res);
			
			return res;
		}
	} else if (op.type == VALUE_PAR) {
		if (n.type == VALUE_MPZ) {
			if (value_lt(n, value_zero)) {
				value_error(1, "Domain Error: drop() is undefined where n is %s (>= 0 expected).", n);
				return value_init_error();
			} else if (value_gt(n, value_int_max)) {
				value_error(1, "Domain Error: drop() is undefined where n is %s (<= %s expected).", n, value_int_max);
				return value_init_error();				
			}
			
			value res = value_init_nil();
			size_t i, max = value_get_long(n);
			value ptr = op;
			for (i = 0; i < max && ptr.type == VALUE_PAR; ++i) {
				value_cons_now(ptr.core.u_p->tail, &res);
				ptr = ptr.core.u_p->tail;
			}
			
			value_reverse_now(&res);
			
			return res;
		}
	} else {
		value_error(1, "Type Error: take() is undefined where op is %ts (array or list expected).", op);
		if (n.type == VALUE_MPZ)
			return value_init_error();
	}
	
	value_error(1, "Type Error: drop() is undefined where n is %ts (integer expected).", n);
	return value_init_error();

}