ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 11 Aug 2019 21:12:28 +0200Is there a way to temporarily turn off sage's type checking?https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/ I'm having some difficulties writing sage code to solve linear programming problems using "natural variables", by substituting for them the variables required for MixedIntegerLinearProgram.
The code for "minimize_desired" is what I'd like to do, but it fails during the substitution with the error message "TypeError: no canonical coercion from Linear functions over Real Double Field to Symbolic Ring". What I'd like is for sage to just do the substitution and ignore the lack of coercion.
The code for "minimize_works" works, but I got past the type checking by changing everything to strings and then back into a sage object, which feels like a terrible kludge. However this does illustrate that, despite sage's worries about coercion, everything ends up fine for MixedIntegerLinearProgram.
Is there a way to turn off sage's type checking and just do the substitution?
Thanks,
Mike
Note: I simplified the examples below by eliminating all the code that dealt with constraints.
def minimize_desired(objective):
p = MixedIntegerLinearProgram(maximization=false)
pvar = p.new_variable(real=True, nonnegative=True)
variables=set(objective.arguments())
translation={v:pvar[v] for v in variables}
objective=objective.subs(translation) # FAILS HERE
p.set_objective(objective)
print p.solve()
def minimize_works(objective):
p = MixedIntegerLinearProgram(maximization=false)
pvar = p.new_variable(real=True, nonnegative=True)
variables=set(str(v) for v in objective.arguments())
translation={v:pvar[v] for v in variables}
objective=sageobj(str(objective), translation)
p.set_objective(objective)
print p.solve()
var('x y')
minimize_works(x+y)
print
minimize_desired(x+y)
Sun, 21 Jul 2019 02:24:18 +0200https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/Answer by vdelecroix for <p>I'm having some difficulties writing sage code to solve linear programming problems using "natural variables", by substituting for them the variables required for MixedIntegerLinearProgram.</p>
<p>The code for "minimize_desired" is what I'd like to do, but it fails during the substitution with the error message "TypeError: no canonical coercion from Linear functions over Real Double Field to Symbolic Ring". What I'd like is for sage to just do the substitution and ignore the lack of coercion.</p>
<p>The code for "minimize_works" works, but I got past the type checking by changing everything to strings and then back into a sage object, which feels like a terrible kludge. However this does illustrate that, despite sage's worries about coercion, everything ends up fine for MixedIntegerLinearProgram.</p>
<p>Is there a way to turn off sage's type checking and just do the substitution? </p>
<p>Thanks,</p>
<p>Mike</p>
<p>Note: I simplified the examples below by eliminating all the code that dealt with constraints. </p>
<pre><code>def minimize_desired(objective):
p = MixedIntegerLinearProgram(maximization=false)
pvar = p.new_variable(real=True, nonnegative=True)
variables=set(objective.arguments())
translation={v:pvar[v] for v in variables}
objective=objective.subs(translation) # FAILS HERE
p.set_objective(objective)
print p.solve()
def minimize_works(objective):
p = MixedIntegerLinearProgram(maximization=false)
pvar = p.new_variable(real=True, nonnegative=True)
variables=set(str(v) for v in objective.arguments())
translation={v:pvar[v] for v in variables}
objective=sageobj(str(objective), translation)
p.set_objective(objective)
print p.solve()
var('x y')
minimize_works(x+y)
print
minimize_desired(x+y)
</code></pre>
https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/?answer=47232#post-id-47232This is not really a problem with type checking. The substitution code of symbolic expression assumes that you want to create a symbolic expression. It does "evaluate" the expression as you would like.
sage: x,y = SR.var('x, y')
sage: expr_symb = x + y
sage: expr_synb.subs(x=1, y=3).parent()
Symbolic Ring
Note that the same behavior can be observed for multivariate polynomials
sage: R = ZZ['x,y']
sage: x,y = R.gens()
sage: expr_poly = x + y
sage: expr_poly..subs(x=1, y=3)parent()
Multivariate Polynomial Ring in x, y over Integer Ring
What you want is a form of "function call". It sort of works for polynomials
sage: expr_poly(1, 3).parent()
Integer Ring
sage: expr_poly(x=1, y=3).parent() # hum... inconsistency?
Multivariate Polynomial Ring in x, y over Integer Ring
But the symbolic ring never spits out an integer
sage: expr_symb(1, 3).parent()
Symbolic Ring
sage: expr_symb(x=1, y=3).parent()
Symbolic Ring
(My) conclusion: this is a desirable feature that is not available. The current behavior is a bit consistent. But we still want to distinguish an "internal substitution" and an "external substitution".Sun, 21 Jul 2019 15:17:33 +0200https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/?answer=47232#post-id-47232Answer by rburing for <p>I'm having some difficulties writing sage code to solve linear programming problems using "natural variables", by substituting for them the variables required for MixedIntegerLinearProgram.</p>
<p>The code for "minimize_desired" is what I'd like to do, but it fails during the substitution with the error message "TypeError: no canonical coercion from Linear functions over Real Double Field to Symbolic Ring". What I'd like is for sage to just do the substitution and ignore the lack of coercion.</p>
<p>The code for "minimize_works" works, but I got past the type checking by changing everything to strings and then back into a sage object, which feels like a terrible kludge. However this does illustrate that, despite sage's worries about coercion, everything ends up fine for MixedIntegerLinearProgram.</p>
<p>Is there a way to turn off sage's type checking and just do the substitution? </p>
<p>Thanks,</p>
<p>Mike</p>
<p>Note: I simplified the examples below by eliminating all the code that dealt with constraints. </p>
<pre><code>def minimize_desired(objective):
p = MixedIntegerLinearProgram(maximization=false)
pvar = p.new_variable(real=True, nonnegative=True)
variables=set(objective.arguments())
translation={v:pvar[v] for v in variables}
objective=objective.subs(translation) # FAILS HERE
p.set_objective(objective)
print p.solve()
def minimize_works(objective):
p = MixedIntegerLinearProgram(maximization=false)
pvar = p.new_variable(real=True, nonnegative=True)
variables=set(str(v) for v in objective.arguments())
translation={v:pvar[v] for v in variables}
objective=sageobj(str(objective), translation)
p.set_objective(objective)
print p.solve()
var('x y')
minimize_works(x+y)
print
minimize_desired(x+y)
</code></pre>
https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/?answer=47230#post-id-47230Personally I would try to find a way to avoid the symbolic ring (with `var`, etc.) altogether.
If you want to use the symbolic ring, then you have to do the conversion to a `LinearFunction` somehow.
Assuming this setup:
sage: var('x,y')
sage: expr = 2*x + y
sage: p = MixedIntegerLinearProgram(maximization=False)
sage: pvar = p.new_variable(real=True, nonnegative=True)
You can do it e.g. like this:
def linear_function_from_symbolic(expr, pvar):
constant_coeff = pvar.mip().base_ring()(expr.subs(dict(zip(expr.variables(), [0]*len(expr.variables())))))
return sum(expr.coefficient(v)*pvar[v] for v in expr.variables()) + constant_coeff
and use it as follows:
sage: linear_function_from_symbolic(expr, pvar)
2*x_0 + x_1
You can convert constraints too:
def linear_constraint_from_symbolic(expr, pvar):
return expr.operator()(linear_function_from_symbolic(expr.lhs(), pvar),
linear_function_from_symbolic(expr.rhs(), pvar))
for example:
sage: linear_constraint_from_symbolic(x <= 1, pvar)
x_0 <= 1
It is reasonable that there is no coercion from the Symbolic Ring to Linear functions over RDF, because it would not be well-defined with respect to the ordering of variables: e.g. coercion of two functions of different variables would depend on the order in which you do it. The function above "suffers" from the same (but probably you don't care).Sun, 21 Jul 2019 13:05:17 +0200https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/?answer=47230#post-id-47230Comment by millermj for <p>Personally I would try to find a way to avoid the symbolic ring (with <code>var</code>, etc.) altogether.</p>
<p>If you want to use the symbolic ring, then you have to do the conversion to a <code>LinearFunction</code> somehow.</p>
<p>Assuming this setup:</p>
<pre><code>sage: var('x,y')
sage: expr = 2*x + y
sage: p = MixedIntegerLinearProgram(maximization=False)
sage: pvar = p.new_variable(real=True, nonnegative=True)
</code></pre>
<p>You can do it e.g. like this:</p>
<pre><code>def linear_function_from_symbolic(expr, pvar):
constant_coeff = pvar.mip().base_ring()(expr.subs(dict(zip(expr.variables(), [0]*len(expr.variables())))))
return sum(expr.coefficient(v)*pvar[v] for v in expr.variables()) + constant_coeff
</code></pre>
<p>and use it as follows:</p>
<pre><code>sage: linear_function_from_symbolic(expr, pvar)
2*x_0 + x_1
</code></pre>
<p>You can convert constraints too:</p>
<pre><code>def linear_constraint_from_symbolic(expr, pvar):
return expr.operator()(linear_function_from_symbolic(expr.lhs(), pvar),
linear_function_from_symbolic(expr.rhs(), pvar))
</code></pre>
<p>for example:</p>
<pre><code>sage: linear_constraint_from_symbolic(x <= 1, pvar)
x_0 <= 1
</code></pre>
<p>It is reasonable that there is no coercion from the Symbolic Ring to Linear functions over RDF, because it would not be well-defined with respect to the ordering of variables: e.g. coercion of two functions of different variables would depend on the order in which you do it. The function above "suffers" from the same (but probably you don't care).</p>
https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/?comment=47461#post-id-47461Thanks! This does what I need.Sun, 11 Aug 2019 21:12:28 +0200https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/?comment=47461#post-id-47461