# [SciPy-dev] Inconsistent behavior in optimize wrt the type of the initial guess (array versus matrix)

Nils Wagner nwagner at iam.uni-stuttgart.de
Thu Jan 11 03:52:16 CST 2007

``` Hi,

I have observed some inconsistent behavior of the optimization routines
wrt to the type of the initial guess.
I mean matrix versus array.

For example optimize.fmin_ncg works with a matrix input while fmin_bfgs
segfaults.
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 46912509653888 (LWP 30417)]
dotblas_matrixproduct (dummy=<value optimized out>, args=<value
optimized out>) at _dotblas.c:233
233             Py_DECREF(ap1);
(gdb) bt
#0  dotblas_matrixproduct (dummy=<value optimized out>, args=<value
optimized out>) at _dotblas.c:233

Nils

from scipy import *

def g(x):
return 1./(1-cos(x))

def g_p(x):
return -sin(x)/(1.-cos(x))**2

def d(x):
return pow(x,2)+pow((g(x)-1.0),2)
#   return sqrt(x**2+(g(x)-1.0)**2)

def d_p(x):
return 2*x+2*(g(x)-1.0)*g_p(x)

def f(x):
return x+(g(x)-1.)*g_p(x)

x_0 = matrix(0.3)
print x_0
x_opt = optimize.fmin_cg(d,x_0) # ValueError: The truth value of an
array with more than one element is ambiguous. Use a.any() or a.all()
#x_opt = optimize.fmin_powell(d,x_0) # ValueError: Initial guess must be
a scalar or rank-1 sequence.
#x_opt = optimize.fmin_bfgs(d,x_0) # Segfaults with a matrix input
#x_opt = optimize.fmin_ncg(d,x_0,d_p) # Works for me with a matrix input
print x_opt

```