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.Sat, 27 May 2017 23:00:32 +0200minimize_constrained for a one dimensional functionhttps://ask.sagemath.org/question/9526/minimize_constrained-for-a-one-dimensional-function/Hello all,
I have a bit of a problem with the minimize_constrained function: When I call
minimize_constrained(lambda x: x^2, [lambda x: 1], 99.6)
I get a `TypeError: iteration over a 0-d array` (why?). If I call
minimize_constrained(lambda x: x^2, \
[lambda x: vector({0:1,1:x}).inner_product(vector((0,0)))], 99.6)
I get a `TypeError: unable to find a common ring for all elements`.
This seems to be the case because the the constraint function is sometimes called
with a `numpy.ndarray` and at other times with a `sage.modules.vector_real_double_dense.Vector_real_double_dense`. In the first case
I need to call `float(x[0])`, in the latter I can use `x[0]` directly.
Is it be possible to convert the data to sage vectors throughout the iterations or
do I have to set up a handler to sanitize the input myself?Mon, 12 Nov 2012 08:45:16 +0100https://ask.sagemath.org/question/9526/minimize_constrained-for-a-one-dimensional-function/Answer by mforets for <p>Hello all,</p>
<p>I have a bit of a problem with the minimize_constrained function: When I call</p>
<pre><code>minimize_constrained(lambda x: x^2, [lambda x: 1], 99.6)
</code></pre>
<p>I get a <code>TypeError: iteration over a 0-d array</code> (why?). If I call</p>
<pre><code>minimize_constrained(lambda x: x^2, \
[lambda x: vector({0:1,1:x}).inner_product(vector((0,0)))], 99.6)
</code></pre>
<p>I get a <code>TypeError: unable to find a common ring for all elements</code>.
This seems to be the case because the the constraint function is sometimes called
with a <code>numpy.ndarray</code> and at other times with a <code>sage.modules.vector_real_double_dense.Vector_real_double_dense</code>. In the first case
I need to call <code>float(x[0])</code>, in the latter I can use <code>x[0]</code> directly.</p>
<p>Is it be possible to convert the data to sage vectors throughout the iterations or
do I have to set up a handler to sanitize the input myself?</p>
https://ask.sagemath.org/question/9526/minimize_constrained-for-a-one-dimensional-function/?answer=37720#post-id-37720The function (`func` argument) passed to `minimize_constrained` can be either a symbolic function,
sage: x = var('x')
sage: minimize_constrained(x^2, [None], [99.6])
(0.0)
or a Python function whose argument is a tuple,
sage: minimize_constrained(lambda x: x[0]^2, [None], [99.6])
(0.0)
The type error in the OP is avoided passing the initial point as a list.Sat, 27 May 2017 23:00:32 +0200https://ask.sagemath.org/question/9526/minimize_constrained-for-a-one-dimensional-function/?answer=37720#post-id-37720