arraydesc = {}<EOL>arraydesc['<STR_LIT>'] = _read_long(f)<EOL>if arraydesc['<STR_LIT>'] == <NUM_LIT:8>:<EOL><INDENT>_skip_bytes(f, <NUM_LIT:4>)<EOL>arraydesc['<STR_LIT>'] = _read_long(f)<EOL>arraydesc['<STR_LIT>'] = _read_long(f)<EOL>arraydesc['<STR_LIT>'] = _read_long(f)<EOL>_skip_bytes(f, <NUM_LIT:8>)<EOL>arraydesc['<STR_LIT>'] = _read_long(f)<EOL>arraydesc['<STR_LIT>'] = []<EOL>for d in range(arraydesc['<STR_LIT>']):<EOL><INDENT>arraydesc['<STR_LIT>'].append(_read_long(f))<EOL><DEDENT><DEDENT>elif arraydesc['<STR_LIT>'] == <NUM_LIT>:<EOL><INDENT>warnings.warn("<STR_LIT>")<EOL>_skip_bytes(f, <NUM_LIT:8>)<EOL>arraydesc['<STR_LIT>'] = _read_uint64(f)<EOL>arraydesc['<STR_LIT>'] = _read_uint64(f)<EOL>arraydesc['<STR_LIT>'] = _read_long(f)<EOL>_skip_bytes(f, <NUM_LIT:8>)<EOL>arraydesc['<STR_LIT>'] = <NUM_LIT:8><EOL>arraydesc['<STR_LIT>'] = []<EOL>for d in range(arraydesc['<STR_LIT>']):<EOL><INDENT>v = _read_long(f)<EOL>if v != <NUM_LIT:0>:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>arraydesc['<STR_LIT>'].append(_read_long(f))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>" % arraydesc['<STR_LIT>'])<EOL><DEDENT>return arraydesc<EOL>


structdesc = {}<EOL>structstart = _read_long(f)<EOL>if structstart != <NUM_LIT:9>:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>structdesc['<STR_LIT:name>'] = _read_string(f)<EOL>predef = _read_long(f)<EOL>structdesc['<STR_LIT>'] = _read_long(f)<EOL>structdesc['<STR_LIT>'] = _read_long(f)<EOL>structdesc['<STR_LIT>'] = predef & <NUM_LIT:1><EOL>structdesc['<STR_LIT>'] = predef & <NUM_LIT:2><EOL>structdesc['<STR_LIT>'] = predef & <NUM_LIT:4><EOL>if not structdesc['<STR_LIT>']:<EOL><INDENT>structdesc['<STR_LIT>'] = []<EOL>for t in range(structdesc['<STR_LIT>']):<EOL><INDENT>structdesc['<STR_LIT>'].append(_read_tagdesc(f))<EOL><DEDENT>for tag in structdesc['<STR_LIT>']:<EOL><INDENT>tag['<STR_LIT:name>'] = _read_string(f)<EOL><DEDENT>structdesc['<STR_LIT>'] = {}<EOL>for tag in structdesc['<STR_LIT>']:<EOL><INDENT>if tag['<STR_LIT>']:<EOL><INDENT>structdesc['<STR_LIT>'][tag['<STR_LIT:name>']] = _read_arraydesc(f)<EOL><DEDENT><DEDENT>structdesc['<STR_LIT>'] = {}<EOL>for tag in structdesc['<STR_LIT>']:<EOL><INDENT>if tag['<STR_LIT>']:<EOL><INDENT>structdesc['<STR_LIT>'][tag['<STR_LIT:name>']] = _read_structdesc(f)<EOL><DEDENT><DEDENT>if structdesc['<STR_LIT>'] or structdesc['<STR_LIT>']:<EOL><INDENT>structdesc['<STR_LIT>'] = _read_string(f)<EOL>structdesc['<STR_LIT>'] = _read_long(f)<EOL>structdesc['<STR_LIT>'] = []<EOL>for s in range(structdesc['<STR_LIT>']):<EOL><INDENT>structdesc['<STR_LIT>'].append(_read_string(f))<EOL><DEDENT>structdesc['<STR_LIT>'] = []<EOL>for s in range(structdesc['<STR_LIT>']):<EOL><INDENT>structdesc['<STR_LIT>'].append(_read_structdesc(f))<EOL><DEDENT><DEDENT>STRUCT_DICT[structdesc['<STR_LIT:name>']] = structdesc<EOL><DEDENT>else:<EOL><INDENT>if not structdesc['<STR_LIT:name>'] in STRUCT_DICT:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>structdesc = STRUCT_DICT[structdesc['<STR_LIT:name>']]<EOL><DEDENT>return structdesc<EOL>

Function to read in a structure descriptor


tagdesc = {}<EOL>tagdesc['<STR_LIT>'] = _read_long(f)<EOL>if tagdesc['<STR_LIT>'] == -<NUM_LIT:1>:<EOL><INDENT>tagdesc['<STR_LIT>'] = _read_uint64(f)<EOL><DEDENT>tagdesc['<STR_LIT>'] = _read_long(f)<EOL>tagflags = _read_long(f)<EOL>tagdesc['<STR_LIT>'] = tagflags & <NUM_LIT:4> == <NUM_LIT:4><EOL>tagdesc['<STR_LIT>'] = tagflags & <NUM_LIT:32> == <NUM_LIT:32><EOL>tagdesc['<STR_LIT>'] = tagdesc['<STR_LIT>'] in DTYPE_DICT<EOL>return tagdesc<EOL>


def readsav(file_name, idict=None, python_dict=False,<EOL>uncompressed_file_name=None, verbose=False):

<EOL>records = []<EOL>if python_dict or idict:<EOL><INDENT>variables = {}<EOL><DEDENT>else:<EOL><INDENT>variables = AttrDict()<EOL><DEDENT>f = open(file_name, '<STR_LIT:rb>')<EOL>signature = _read_bytes(f, <NUM_LIT:2>)<EOL>if signature != b'<STR_LIT>':<EOL><INDENT>raise Exception("<STR_LIT>" % signature)<EOL><DEDENT>recfmt = _read_bytes(f, <NUM_LIT:2>)<EOL>if recfmt == b'<STR_LIT>':<EOL><INDENT>pass<EOL><DEDENT>elif recfmt == b'<STR_LIT>':<EOL><INDENT>if verbose:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>if uncompressed_file_name:<EOL><INDENT>fout = open(uncompressed_file_name, '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>fout = tempfile.NamedTemporaryFile(suffix='<STR_LIT>')<EOL><DEDENT>if verbose:<EOL><INDENT>print("<STR_LIT>" % fout.name)<EOL><DEDENT>fout.write(b'<STR_LIT>')<EOL>while True:<EOL><INDENT>rectype = _read_long(f)<EOL>fout.write(struct.pack('<STR_LIT>', int(rectype)))<EOL>nextrec = _read_uint32(f)<EOL>nextrec += _read_uint32(f) * <NUM_LIT:2>**<NUM_LIT:32><EOL>unknown = f.read(<NUM_LIT:4>)<EOL>if RECTYPE_DICT[rectype] == '<STR_LIT>':<EOL><INDENT>fout.write(struct.pack('<STR_LIT>', int(nextrec) % <NUM_LIT:2>**<NUM_LIT:32>))<EOL>fout.write(struct.pack('<STR_LIT>', int((nextrec - (nextrec % <NUM_LIT:2>**<NUM_LIT:32>)) / <NUM_LIT:2>**<NUM_LIT:32>)))<EOL>fout.write(unknown)<EOL>break<EOL><DEDENT>pos = f.tell()<EOL>rec_string = zlib.decompress(f.read(nextrec-pos))<EOL>nextrec = fout.tell() + len(rec_string) + <NUM_LIT:12><EOL>fout.write(struct.pack('<STR_LIT>', int(nextrec % <NUM_LIT:2>**<NUM_LIT:32>)))<EOL>fout.write(struct.pack('<STR_LIT>', int((nextrec - (nextrec % <NUM_LIT:2>**<NUM_LIT:32>)) / <NUM_LIT:2>**<NUM_LIT:32>)))<EOL>fout.write(unknown)<EOL>fout.write(rec_string)<EOL><DEDENT>f.close()<EOL>f = fout<EOL>f.seek(<NUM_LIT:4>)<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>" % recfmt)<EOL><DEDENT>while True:<EOL><INDENT>r = _read_record(f)<EOL>records.append(r)<EOL>if '<STR_LIT:end>' in r:<EOL><INDENT>if r['<STR_LIT:end>']:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>f.close()<EOL>heap = {}<EOL>for r in records:<EOL><INDENT>if r['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>heap[r['<STR_LIT>']] = r['<STR_LIT:data>']<EOL><DEDENT><DEDENT>for r in records:<EOL><INDENT>if r['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>replace, new = _replace_heap(r['<STR_LIT:data>'], heap)<EOL>if replace:<EOL><INDENT>r['<STR_LIT:data>'] = new<EOL><DEDENT>variables[r['<STR_LIT>'].lower()] = r['<STR_LIT:data>']<EOL><DEDENT><DEDENT>if verbose:<EOL><INDENT>for record in records:<EOL><INDENT>if record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>print("<STR_LIT:->"*<NUM_LIT:50>)<EOL>print("<STR_LIT>" % record['<STR_LIT:date>'])<EOL>print("<STR_LIT>" % record['<STR_LIT:user>'])<EOL>print("<STR_LIT>" % record['<STR_LIT:host>'])<EOL>break<EOL><DEDENT><DEDENT>for record in records:<EOL><INDENT>if record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>print("<STR_LIT:->"*<NUM_LIT:50>)<EOL>print("<STR_LIT>" % record['<STR_LIT>'])<EOL>print("<STR_LIT>" % record['<STR_LIT>'])<EOL>print("<STR_LIT>" % record['<STR_LIT>'])<EOL>print("<STR_LIT>" % record['<STR_LIT>'])<EOL>break<EOL><DEDENT><DEDENT>for record in records:<EOL><INDENT>if record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>print("<STR_LIT:->"*<NUM_LIT:50>)<EOL>print("<STR_LIT>" % record['<STR_LIT>'])<EOL>print("<STR_LIT>" % record['<STR_LIT:title>'])<EOL>print("<STR_LIT>" % record['<STR_LIT>'])<EOL>break<EOL><DEDENT><DEDENT>for record in records:<EOL><INDENT>if record['<STR_LIT>'] == "<STR_LIT>":<EOL><INDENT>print("<STR_LIT:->"*<NUM_LIT:50>)<EOL>print("<STR_LIT>" % record['<STR_LIT:description>'])<EOL>break<EOL><DEDENT><DEDENT>print("<STR_LIT:->"*<NUM_LIT:50>)<EOL>print("<STR_LIT>" %<EOL>(len(records)))<EOL>rectypes = [r['<STR_LIT>'] for r in records]<EOL>for rt in set(rectypes):<EOL><INDENT>if rt != '<STR_LIT>':<EOL><INDENT>print("<STR_LIT>" % (rectypes.count(rt), rt))<EOL><DEDENT><DEDENT>print("<STR_LIT:->"*<NUM_LIT:50>)<EOL>if '<STR_LIT>' in rectypes:<EOL><INDENT>print("<STR_LIT>")<EOL>for var in variables:<EOL><INDENT>print("<STR_LIT>" % (var, type(variables[var])))<EOL><DEDENT>print("<STR_LIT:->"*<NUM_LIT:50>)<EOL><DEDENT><DEDENT>if idict:<EOL><INDENT>for var in variables:<EOL><INDENT>idict[var] = variables[var]<EOL><DEDENT>return idict<EOL><DEDENT>else:<EOL><INDENT>return variables<EOL><DEDENT>

Read an IDL .sav file

Parameters
----------
file_name : str
    Name of the IDL save file.
idict : dict, optional
    Dictionary in which to insert .sav file variables
python_dict : bool, optional
    By default, the object return is not a Python dictionary, but a
    case-insensitive dictionary with item, attribute, and call access
    to variables. To get a standard Python dictionary, set this option
    to True.
uncompressed_file_name : str, optional
    This option only has an effect for .sav files written with the
    /compress option. If a file name is specified, compressed .sav
    files are uncompressed to this file. Otherwise, readsav will use
    the `tempfile` module to determine a temporary filename
    automatically, and will remove the temporary file upon successfully
    reading it in.
verbose : bool, optional
    Whether to print out information about the save file, including
    the records read, and available variables.

Returns
-------
idl_dict : AttrDict or dict
    If `python_dict` is set to False (default), this function returns a
    case-insensitive dictionary with item, attribute, and call access
    to variables. If `python_dict` is set to True, this function
    returns a Python dictionary with all variable names in lowercase.
    If `idict` was specified, then variables are written to the
    dictionary specified, and the updated dictionary is returned.


def bytescale(data, cmin=None, cmax=None, high=<NUM_LIT:255>, low=<NUM_LIT:0>):

if data.dtype == uint8:<EOL><INDENT>return data<EOL><DEDENT>if high < low:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if cmin is None:<EOL><INDENT>cmin = data.min()<EOL><DEDENT>if cmax is None:<EOL><INDENT>cmax = data.max()<EOL><DEDENT>cscale = cmax - cmin<EOL>if cscale < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>elif cscale == <NUM_LIT:0>:<EOL><INDENT>cscale = <NUM_LIT:1><EOL><DEDENT>scale = float(high - low) / cscale<EOL>bytedata = (data * <NUM_LIT:1.0> - cmin) * scale + <NUM_LIT><EOL>bytedata[bytedata > high] = high<EOL>bytedata[bytedata < <NUM_LIT:0>] = <NUM_LIT:0><EOL>return cast[uint8](bytedata) + cast[uint8](low)<EOL>

Byte scales an array (image).

Byte scaling means converting the input image to uint8 dtype and scaling
the range to ``(low, high)`` (default 0-255).
If the input image already has dtype uint8, no scaling is done.

Parameters
----------
data : ndarray
    PIL image data array.
cmin : scalar, optional
    Bias scaling of small values. Default is ``data.min()``.
cmax : scalar, optional
    Bias scaling of large values. Default is ``data.max()``.
high : scalar, optional
    Scale max value to `high`.  Default is 255.
low : scalar, optional
    Scale min value to `low`.  Default is 0.

Returns
-------
img_array : uint8 ndarray
    The byte-scaled array.

Examples
--------
>>> img = array([[ 91.06794177,   3.39058326,  84.4221549 ],
                 [ 73.88003259,  80.91433048,   4.88878881],
                 [ 51.53875334,  34.45808177,  27.5873488 ]])
>>> bytescale(img)
array([[255,   0, 236],
       [205, 225,   4],
       [140,  90,  70]], dtype=uint8)
>>> bytescale(img, high=200, low=100)
array([[200, 100, 192],
       [180, 188, 102],
       [155, 135, 128]], dtype=uint8)
>>> bytescale(img, cmin=0, cmax=255)
array([[91,  3, 84],
       [74, 81,  5],
       [52, 34, 28]], dtype=uint8)


im = Image.open(name)<EOL>return fromimage(im, flatten=flatten)<EOL>

Read an image from a file as an array.

Parameters
----------
name : str or file object
    The file name or file object to be read.
flatten : bool, optional
    If True, flattens the color layers into a single gray-scale layer.

Returns
-------
imread : ndarray
    The array obtained by reading image from file `imfile`.

Notes
-----
The image is flattened by calling convert('F') on
the resulting image object.


im = toimage(arr)<EOL>if format is None:<EOL><INDENT>im.save(name)<EOL><DEDENT>else:<EOL><INDENT>im.save(name, format)<EOL><DEDENT>return<EOL>

Save an array as an image.

Parameters
----------
name : str or file object
    Output file name or file object.
arr : ndarray, MxN or MxNx3 or MxNx4
    Array containing image values.  If the shape is ``MxN``, the array
    represents a grey-level image.  Shape ``MxNx3`` stores the red, green
    and blue bands along the last dimension.  An alpha layer may be
    included, specified as the last colour band of an ``MxNx4`` array.
format : str
    Image format. If omitted, the format to use is determined from the
    file name extension. If a file object was used instead of a file name,
    this parameter should always be used.

Examples
--------
Construct an array of gradient intensity values and save to file:

>>> x = np.zeros((255, 255))
>>> x = np.zeros((255, 255), dtype=np.uint8)
>>> x[:] = np.arange(255)
>>> imsave('/tmp/gradient.png', x)

Construct an array with three colour bands (R, G, B) and store to file:

>>> rgb = np.zeros((255, 255, 3), dtype=np.uint8)
>>> rgb[..., 0] = np.arange(255)
>>> rgb[..., 1] = 55
>>> rgb[..., 2] = 1 - np.arange(255)
>>> imsave('/tmp/rgb_gradient.png', rgb)


if not Image.isImageType(im):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if flatten:<EOL><INDENT>im = im.convert('<STR_LIT:F>')<EOL><DEDENT>elif im.mode == '<STR_LIT:1>':<EOL><INDENT>im.convert('<STR_LIT:L>')<EOL><DEDENT>return array(im)<EOL>

Return a copy of a PIL image as a numpy array.

Parameters
----------
im : PIL image
    Input image.
flatten : bool
    If true, convert the output to grey-scale.

Returns
-------
fromimage : ndarray
    The different colour bands/channels are stored in the
    third dimension, such that a grey-image is MxN, an
    RGB-image MxNx3 and an RGBA-image MxNx4.


def toimage(arr, high=<NUM_LIT:255>, low=<NUM_LIT:0>, cmin=None, cmax=None, pal=None,<EOL>mode=None, channel_axis=None):

data = asarray(arr)<EOL>if iscomplexobj(data):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>shape = list(data.shape)<EOL>valid = len(shape) == <NUM_LIT:2> or ((len(shape) == <NUM_LIT:3>) and<EOL>((<NUM_LIT:3> in shape) or (<NUM_LIT:4> in shape)))<EOL>if not valid:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if len(shape) == <NUM_LIT:2>:<EOL><INDENT>shape = (shape[<NUM_LIT:1>], shape[<NUM_LIT:0>])  <EOL>if mode == '<STR_LIT:F>':<EOL><INDENT>data32 = data.astype(numpy.float32)<EOL>image = Image.frombytes(mode, shape, data32.tostring())<EOL>return image<EOL><DEDENT>if mode in [None, '<STR_LIT:L>', '<STR_LIT:P>']:<EOL><INDENT>bytedata = bytescale(data, high=high, low=low,<EOL>cmin=cmin, cmax=cmax)<EOL>image = Image.frombytes('<STR_LIT:L>', shape, bytedata.tostring())<EOL>if pal is not None:<EOL><INDENT>image.putpalette(asarray(pal, dtype=uint8).tostring())<EOL><DEDENT>elif mode == '<STR_LIT:P>':  <EOL><INDENT>pal = (arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:1>, dtype=uint8)[:, newaxis] *<EOL>ones((<NUM_LIT:3>,), dtype=uint8)[newaxis, :])<EOL>image.putpalette(asarray(pal, dtype=uint8).tostring())<EOL><DEDENT>return image<EOL><DEDENT>if mode == '<STR_LIT:1>':  <EOL><INDENT>bytedata = (data > high)<EOL>image = Image.frombytes('<STR_LIT:1>', shape, bytedata.tostring())<EOL>return image<EOL><DEDENT>if cmin is None:<EOL><INDENT>cmin = amin(ravel(data))<EOL><DEDENT>if cmax is None:<EOL><INDENT>cmax = amax(ravel(data))<EOL><DEDENT>data = (data*<NUM_LIT:1.0> - cmin)*(high - low)/(cmax - cmin) + low<EOL>if mode == '<STR_LIT:I>':<EOL><INDENT>data32 = data.astype(numpy.uint32)<EOL>image = Image.frombytes(mode, shape, data32.tostring())<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(_errstr)<EOL><DEDENT>return image<EOL><DEDENT>if channel_axis is None:<EOL><INDENT>if (<NUM_LIT:3> in shape):<EOL><INDENT>ca = numpy.flatnonzero(asarray(shape) == <NUM_LIT:3>)[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>ca = numpy.flatnonzero(asarray(shape) == <NUM_LIT:4>)<EOL>if len(ca):<EOL><INDENT>ca = ca[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>ca = channel_axis<EOL><DEDENT>numch = shape[ca]<EOL>if numch not in [<NUM_LIT:3>, <NUM_LIT:4>]:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>bytedata = bytescale(data, high=high, low=low, cmin=cmin, cmax=cmax)<EOL>if ca == <NUM_LIT:2>:<EOL><INDENT>strdata = bytedata.tostring()<EOL>shape = (shape[<NUM_LIT:1>], shape[<NUM_LIT:0>])<EOL><DEDENT>elif ca == <NUM_LIT:1>:<EOL><INDENT>strdata = transpose(bytedata, (<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:1>)).tostring()<EOL>shape = (shape[<NUM_LIT:2>], shape[<NUM_LIT:0>])<EOL><DEDENT>elif ca == <NUM_LIT:0>:<EOL><INDENT>strdata = transpose(bytedata, (<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:0>)).tostring()<EOL>shape = (shape[<NUM_LIT:2>], shape[<NUM_LIT:1>])<EOL><DEDENT>if mode is None:<EOL><INDENT>if numch == <NUM_LIT:3>:<EOL><INDENT>mode = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>mode = '<STR_LIT>'<EOL><DEDENT><DEDENT>if mode not in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>raise ValueError(_errstr)<EOL><DEDENT>if mode in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if numch != <NUM_LIT:3>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>if mode in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if numch != <NUM_LIT:4>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>image = Image.frombytes(mode, shape, strdata)<EOL>return image<EOL>

Takes a numpy array and returns a PIL image.

    The mode of the PIL image depends on the array shape and the `pal` and
    `mode` keywords.

    For 2-D arrays, if `pal` is a valid (N,3) byte-array giving the RGB values
    (from 0 to 255) then ``mode='P'``, otherwise ``mode='L'``, unless mode
    is given as 'F' or 'I' in which case a float and/or integer array is made.

    Notes
    -----
    For 3-D arrays, the `channel_axis` argument tells which dimension of the
    array holds the channel data.

    For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'
    by default or 'YCbCr' if selected.

    The numpy array must be either 2 dimensional or 3 dimensional.


def imrotate(arr, angle, interp='<STR_LIT>'):

arr = asarray(arr)<EOL>func = {'<STR_LIT>': <NUM_LIT:0>, '<STR_LIT>': <NUM_LIT:2>, '<STR_LIT>': <NUM_LIT:3>, '<STR_LIT>': <NUM_LIT:3>}<EOL>im = toimage(arr)<EOL>im = im.rotate(angle, resample=func[interp])<EOL>return fromimage(im)<EOL>

Rotate an image counter-clockwise by angle degrees.

Parameters
----------
arr : ndarray
    Input array of image to be rotated.
angle : float
    The angle of rotation.
interp : str, optional
    Interpolation

    - 'nearest' :  for nearest neighbor
    - 'bilinear' : for bilinear
    - 'cubic' : cubic
    - 'bicubic' : for bicubic

Returns
-------
imrotate : ndarray
    The rotated array of image.


im = toimage(arr)<EOL>fnum, fname = tempfile.mkstemp('<STR_LIT>')<EOL>try:<EOL><INDENT>im.save(fname)<EOL><DEDENT>except:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>import os<EOL>os.close(fnum)<EOL>cmd = os.environ.get('<STR_LIT>', '<STR_LIT>')<EOL>status = os.system("<STR_LIT>" % (cmd, fname))<EOL>os.unlink(fname)<EOL>if status != <NUM_LIT:0>:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

Simple showing of an image through an external viewer.

Uses the image viewer specified by the environment variable
SCIPY_PIL_IMAGE_VIEWER, or if that is not defined then `see`,
to view a temporary file generated from array data.

Parameters
----------
arr : ndarray
    Array of image data to show.

Returns
-------
None

Examples
--------
>>> a = np.tile(np.arange(255), (255,1))
>>> from scipy import misc
>>> misc.pilutil.imshow(a)


def imresize(arr, size, interp='<STR_LIT>', mode=None):

im = toimage(arr, mode=mode)<EOL>ts = type(size)<EOL>if issubdtype(ts, int):<EOL><INDENT>percent = size / <NUM_LIT><EOL>size = tuple((array(im.size)*percent).astype(int))<EOL><DEDENT>elif issubdtype(type(size), float):<EOL><INDENT>size = tuple((array(im.size)*size).astype(int))<EOL><DEDENT>else:<EOL><INDENT>size = (size[<NUM_LIT:1>], size[<NUM_LIT:0>])<EOL><DEDENT>func = {'<STR_LIT>': <NUM_LIT:0>, '<STR_LIT>': <NUM_LIT:2>, '<STR_LIT>': <NUM_LIT:3>, '<STR_LIT>': <NUM_LIT:3>}<EOL>imnew = im.resize(size, resample=func[interp])<EOL>return fromimage(imnew)<EOL>

Resize an image.

Parameters
----------
arr : ndarray
    The array of image to be resized.

size : int, float or tuple
    * int   - Percentage of current size.
    * float - Fraction of current size.
    * tuple - Size of the output image.

interp : str
    Interpolation to use for re-sizing ('nearest', 'bilinear', 'bicubic'
    or 'cubic').

mode : str
    The PIL image mode ('P', 'L', etc.).

Returns
-------
imresize : ndarray
    The resized array of image.


_tdict = {'<STR_LIT>': ImageFilter.BLUR,<EOL>'<STR_LIT>': ImageFilter.CONTOUR,<EOL>'<STR_LIT>': ImageFilter.DETAIL,<EOL>'<STR_LIT>': ImageFilter.EDGE_ENHANCE,<EOL>'<STR_LIT>': ImageFilter.EDGE_ENHANCE_MORE,<EOL>'<STR_LIT>': ImageFilter.EMBOSS,<EOL>'<STR_LIT>': ImageFilter.FIND_EDGES,<EOL>'<STR_LIT>': ImageFilter.SMOOTH,<EOL>'<STR_LIT>': ImageFilter.SMOOTH_MORE,<EOL>'<STR_LIT>': ImageFilter.SHARPEN<EOL>}<EOL>im = toimage(arr)<EOL>if ftype not in _tdict:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return fromimage(im.filter(_tdict[ftype]))<EOL>

Simple filtering of an image.

Parameters
----------
arr : ndarray
    The array of Image in which the filter is to be applied.
ftype : str
    The filter that has to be applied. Legal values are:
    'blur', 'contour', 'detail', 'edge_enhance', 'edge_enhance_more',
    'emboss', 'find_edges', 'smooth', 'smooth_more', 'sharpen'.

Returns
-------
imfilter : ndarray
    The array with filter applied.

Raises
------
ValueError
    *Unknown filter type.*  If the filter you are trying
    to apply is unsupported.


def logsumexp(a, axis=None, b=None, keepdims=False):

a = asarray(a)<EOL>if not _NUMPY_170:<EOL><INDENT>if axis is None:<EOL><INDENT>sh_keepdims = (<NUM_LIT:1>,) * a.ndim<EOL><DEDENT>else:<EOL><INDENT>sh_keepdims = list(a.shape)<EOL>sh_keepdims[axis] = <NUM_LIT:1><EOL><DEDENT>a_max = amax(a, axis=axis)<EOL>if a_max.ndim > <NUM_LIT:0>:<EOL><INDENT>a_max[~isfinite(a_max)] = <NUM_LIT:0><EOL><DEDENT>elif not isfinite(a_max):<EOL><INDENT>a_max = <NUM_LIT:0><EOL><DEDENT>if b is not None:<EOL><INDENT>b = asarray(b)<EOL>tmp = b * exp(a - reshape(a_max, sh_keepdims))<EOL><DEDENT>else:<EOL><INDENT>tmp = exp(a - reshape(a_max, sh_keepdims))<EOL><DEDENT>with np.errstate(divide='<STR_LIT:ignore>'):<EOL><INDENT>out = log(sum(tmp, axis=axis))<EOL><DEDENT>out += a_max<EOL>if keepdims:<EOL><INDENT>out = reshape(out, sh_keepdims)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>a_max = amax(a, axis=axis, keepdims=True)<EOL>if a_max.ndim > <NUM_LIT:0>:<EOL><INDENT>a_max[~isfinite(a_max)] = <NUM_LIT:0><EOL><DEDENT>elif not isfinite(a_max):<EOL><INDENT>a_max = <NUM_LIT:0><EOL><DEDENT>if b is not None:<EOL><INDENT>b = asarray(b)<EOL>tmp = b * exp(a - a_max)<EOL><DEDENT>else:<EOL><INDENT>tmp = exp(a - a_max)<EOL><DEDENT>with np.errstate(divide='<STR_LIT:ignore>'):<EOL><INDENT>out = log(sum(tmp, axis=axis, keepdims=keepdims))<EOL><DEDENT>if not keepdims:<EOL><INDENT>a_max = squeeze(a_max, axis=axis)<EOL><DEDENT>out += a_max<EOL><DEDENT>return out<EOL>

Compute the log of the sum of exponentials of input elements.

    Parameters
    ----------
    a : array_like
        Input array.
    axis : None or int or tuple of ints, optional
        Axis or axes over which the sum is taken. By default `axis` is None,
        and all elements are summed. Tuple of ints is not accepted if NumPy
        version is lower than 1.7.0.

        .. versionadded:: 0.11.0
    keepdims: bool, optional
        If this is set to True, the axes which are reduced are left in the
        result as dimensions with size one. With this option, the result
        will broadcast correctly against the original array.

        .. versionadded:: 0.15.0
    b : array-like, optional
        Scaling factor for exp(`a`) must be of the same shape as `a` or
        broadcastable to `a`.

        .. versionadded:: 0.12.0

    Returns
    -------
    res : ndarray
        The result, ``np.log(np.sum(np.exp(a)))`` calculated in a numerically
        more stable way. If `b` is given then ``np.log(np.sum(b*np.exp(a)))``
        is returned.

    See Also
    --------
    numpy.logaddexp, numpy.logaddexp2

    Notes
    -----
    Numpy has a logaddexp function which is very similar to `logsumexp`, but
    only handles two arguments. `logaddexp.reduce` is similar to this
    function, but may be less stable.

    Examples
    --------
    >>> from scipy.misc import logsumexp
    >>> a = np.arange(10)
    >>> np.log(np.sum(np.exp(a)))
    9.4586297444267107
    >>> logsumexp(a)
    9.4586297444267107

    With weights

    >>> a = np.arange(10)
    >>> b = np.arange(10, 0, -1)
    >>> logsumexp(a, b=b)
    9.9170178533034665
    >>> np.log(np.sum(b*np.exp(a)))
    9.9170178533034647


if Np < ndiv + <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if Np % <NUM_LIT:2> == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>from scipy import linalg<EOL>ho = Np >> <NUM_LIT:1><EOL>x = arange(-ho,ho+<NUM_LIT:1.0>)<EOL>x = x[:,newaxis]<EOL>X = x**<NUM_LIT:0.0><EOL>for k in range(<NUM_LIT:1>,Np):<EOL><INDENT>X = hstack([X,x**k])<EOL><DEDENT>w = product(arange(<NUM_LIT:1>,ndiv+<NUM_LIT:1>),axis=<NUM_LIT:0>)*linalg.inv(X)[ndiv]<EOL>return w<EOL>

Return weights for an Np-point central derivative.

Assumes equally-spaced function points.

If weights are in the vector w, then
derivative is w[0] * f(x-ho*dx) + ... + w[-1] * f(x+h0*dx)

Parameters
----------
Np : int
    Number of points for the central derivative.
ndiv : int, optional
    Number of divisions.  Default is 1.

Notes
-----
Can be inaccurate for large number of points.


def derivative(func, x0, dx=<NUM_LIT:1.0>, n=<NUM_LIT:1>, args=(), order=<NUM_LIT:3>):

if order < n + <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if order % <NUM_LIT:2> == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if n == <NUM_LIT:1>:<EOL><INDENT>if order == <NUM_LIT:3>:<EOL><INDENT>weights = array([-<NUM_LIT:1>,<NUM_LIT:0>,<NUM_LIT:1>])/<NUM_LIT><EOL><DEDENT>elif order == <NUM_LIT:5>:<EOL><INDENT>weights = array([<NUM_LIT:1>,-<NUM_LIT:8>,<NUM_LIT:0>,<NUM_LIT:8>,-<NUM_LIT:1>])/<NUM_LIT><EOL><DEDENT>elif order == <NUM_LIT:7>:<EOL><INDENT>weights = array([-<NUM_LIT:1>,<NUM_LIT:9>,-<NUM_LIT>,<NUM_LIT:0>,<NUM_LIT>,-<NUM_LIT:9>,<NUM_LIT:1>])/<NUM_LIT><EOL><DEDENT>elif order == <NUM_LIT:9>:<EOL><INDENT>weights = array([<NUM_LIT:3>,-<NUM_LIT:32>,<NUM_LIT>,-<NUM_LIT>,<NUM_LIT:0>,<NUM_LIT>,-<NUM_LIT>,<NUM_LIT:32>,-<NUM_LIT:3>])/<NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>weights = central_diff_weights(order,<NUM_LIT:1>)<EOL><DEDENT><DEDENT>elif n == <NUM_LIT:2>:<EOL><INDENT>if order == <NUM_LIT:3>:<EOL><INDENT>weights = array([<NUM_LIT:1>,-<NUM_LIT>,<NUM_LIT:1>])<EOL><DEDENT>elif order == <NUM_LIT:5>:<EOL><INDENT>weights = array([-<NUM_LIT:1>,<NUM_LIT:16>,-<NUM_LIT:30>,<NUM_LIT:16>,-<NUM_LIT:1>])/<NUM_LIT><EOL><DEDENT>elif order == <NUM_LIT:7>:<EOL><INDENT>weights = array([<NUM_LIT:2>,-<NUM_LIT>,<NUM_LIT>,-<NUM_LIT>,<NUM_LIT>,-<NUM_LIT>,<NUM_LIT:2>])/<NUM_LIT><EOL><DEDENT>elif order == <NUM_LIT:9>:<EOL><INDENT>weights = array([-<NUM_LIT:9>,<NUM_LIT>,-<NUM_LIT>,<NUM_LIT>,-<NUM_LIT>,<NUM_LIT>,-<NUM_LIT>,<NUM_LIT>,-<NUM_LIT:9>])/<NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>weights = central_diff_weights(order,<NUM_LIT:2>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>weights = central_diff_weights(order, n)<EOL><DEDENT>val = <NUM_LIT:0.0><EOL>ho = order >> <NUM_LIT:1><EOL>for k in range(order):<EOL><INDENT>val += weights[k]*func(x0+(k-ho)*dx,*args)<EOL><DEDENT>return val / product((dx,)*n,axis=<NUM_LIT:0>)<EOL>

Find the n-th derivative of a function at a point.

Given a function, use a central difference formula with spacing `dx` to
compute the `n`-th derivative at `x0`.

Parameters
----------
func : function
    Input function.
x0 : float
    The point at which `n`-th derivative is found.
dx : int, optional
    Spacing.
n : int, optional
    Order of the derivative. Default is 1.
args : tuple, optional
    Arguments
order : int, optional
    Number of points to use, must be odd.

Notes
-----
Decreasing the step size too small can result in round-off error.

Examples
--------
>>> def f(x):
...     return x**3 + x**2
...
>>> derivative(f, 1.0, dx=1e-6)
4.9999999999217337


from scipy import linalg<EOL>an = asarray(an)<EOL>N = len(an) - <NUM_LIT:1><EOL>n = N - m<EOL>if n < <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>Akj = eye(N+<NUM_LIT:1>, n+<NUM_LIT:1>)<EOL>Bkj = zeros((N+<NUM_LIT:1>, m), '<STR_LIT:d>')<EOL>for row in range(<NUM_LIT:1>, m+<NUM_LIT:1>):<EOL><INDENT>Bkj[row,:row] = -(an[:row])[::-<NUM_LIT:1>]<EOL><DEDENT>for row in range(m+<NUM_LIT:1>, N+<NUM_LIT:1>):<EOL><INDENT>Bkj[row,:] = -(an[row-m:row])[::-<NUM_LIT:1>]<EOL><DEDENT>C = hstack((Akj, Bkj))<EOL>pq = linalg.solve(C, an)<EOL>p = pq[:n+<NUM_LIT:1>]<EOL>q = r_[<NUM_LIT:1.0>, pq[n+<NUM_LIT:1>:]]<EOL>return poly1d(p[::-<NUM_LIT:1>]), poly1d(q[::-<NUM_LIT:1>])<EOL>

Return Pade approximation to a polynomial as the ratio of two polynomials.

Parameters
----------
an : (N,) array_like
    Taylor series coefficients.
m : int
    The order of the returned approximating polynomials.

Returns
-------
p, q : Polynomial class
    The pade approximation of the polynomial defined by `an` is
    `p(x)/q(x)`.

Examples
--------
>>> from scipy import misc
>>> e_exp = [1.0, 1.0, 1.0/2.0, 1.0/6.0, 1.0/24.0, 1.0/120.0]
>>> p, q = misc.pade(e_exp, 2)

>>> e_exp.reverse()
>>> e_poly = np.poly1d(e_exp)

Compare ``e_poly(x)`` and the pade approximation ``p(x)/q(x)``

>>> e_poly(1)
2.7166666666666668

>>> p(1)/q(1)
2.7179487179487181


import pickle<EOL>import os<EOL>fname = os.path.join(os.path.dirname(__file__),'<STR_LIT>')<EOL>f = open(fname,'<STR_LIT:rb>')<EOL>lena = array(pickle.load(f))<EOL>f.close()<EOL>return lena<EOL>

Get classic image processing example image, Lena, at 8-bit grayscale
bit-depth, 512 x 512 size.

Parameters
----------
None

Returns
-------
lena : ndarray
    Lena image

Examples
--------
>>> import scipy.misc
>>> lena = scipy.misc.lena()
>>> lena.shape
(512, 512)
>>> lena.max()
245
>>> lena.dtype
dtype('int32')

>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.imshow(lena)
>>> plt.show()


import pickle<EOL>import os<EOL>fname = os.path.join(os.path.dirname(__file__),'<STR_LIT>')<EOL>with open(fname, '<STR_LIT:rb>') as f:<EOL><INDENT>ascent = array(pickle.load(f))<EOL><DEDENT>return ascent<EOL>

Get an 8-bit grayscale bit-depth, 512 x 512 derived image for easy use in demos

The image is derived from accent-to-the-top.jpg at
http://www.public-domain-image.com/people-public-domain-images-pictures/

Parameters
----------
None

Returns
-------
ascent : ndarray
   convenient image to use for testing and demonstration

Examples
--------
>>> import scipy.misc
>>> ascent = scipy.misc.ascent()
>>> ascent.shape
(512, 512)
>>> ascent.max()
255

>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.imshow(ascent)
>>> plt.show()


import bz2<EOL>import os<EOL>with open(os.path.join(os.path.dirname(__file__), '<STR_LIT>'), '<STR_LIT:rb>') as f:<EOL><INDENT>rawdata = f.read()<EOL><DEDENT>data = bz2.decompress(rawdata)<EOL>face = fromstring(data, dtype='<STR_LIT>')<EOL>face.shape = (<NUM_LIT>, <NUM_LIT>, <NUM_LIT:3>)<EOL>if gray is True:<EOL><INDENT>face = (<NUM_LIT> * face[:,:,<NUM_LIT:0>] + <NUM_LIT> * face[:,:,<NUM_LIT:1>] + <NUM_LIT> * face[:,:,<NUM_LIT:2>]).astype('<STR_LIT>')<EOL><DEDENT>return face<EOL>

Get a 1024 x 768, color image of a raccoon face.

raccoon-procyon-lotor.jpg at http://www.public-domain-image.com

Parameters
----------
gray : bool, optional
    If True then return color image, otherwise return an 8-bit gray-scale

Returns
-------
face : ndarray
    image of a racoon face

Examples
--------
>>> import scipy.misc
>>> face = scipy.misc.face()
>>> face.shape
(768, 1024, 3)
>>> face.max()
230
>>> face.dtype
dtype('uint8')

>>> import matplotlib.pyplot as plt
>>> plt.gray()
>>> plt.imshow(face)
>>> plt.show()


if not docstring:<EOL><INDENT>return docstring<EOL><DEDENT>if docdict is None:<EOL><INDENT>docdict = {}<EOL><DEDENT>if not docdict:<EOL><INDENT>return docstring<EOL><DEDENT>lines = docstring.expandtabs().splitlines()<EOL>if len(lines) < <NUM_LIT:2>:<EOL><INDENT>icount = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>icount = indentcount_lines(lines[<NUM_LIT:1>:])<EOL><DEDENT>indent = '<STR_LIT:U+0020>' * icount<EOL>indented = {}<EOL>for name, dstr in docdict.items():<EOL><INDENT>lines = dstr.expandtabs().splitlines()<EOL>try:<EOL><INDENT>newlines = [lines[<NUM_LIT:0>]]<EOL>for line in lines[<NUM_LIT:1>:]:<EOL><INDENT>newlines.append(indent+line)<EOL><DEDENT>indented[name] = '<STR_LIT:\n>'.join(newlines)<EOL><DEDENT>except IndexError:<EOL><INDENT>indented[name] = dstr<EOL><DEDENT><DEDENT>return docstring % indented<EOL>

Fill a function docstring from variables in dictionary

    Adapt the indent of the inserted docs

    Parameters
    ----------
    docstring : string
        docstring from function, possibly with dict formatting strings
    docdict : dict
        dictionary with keys that match the dict formatting strings
        and values that are docstring fragments to be inserted.  The
        indentation of the inserted docstrings is set to match the
        minimum indentation of the ``docstring`` by adding this
        indentation to all lines of the inserted string, except the
        first

    Returns
    -------
    outstring : string
        string with requested ``docdict`` strings inserted

    Examples
    --------
    >>> docformat(' Test string with %(value)s', {'value':'inserted value'})
    ' Test string with inserted value'
    >>> docstring = 'First line\\n    Second line\\n    %(value)s'
    >>> inserted_string = "indented\\nstring"
    >>> docdict = {'value': inserted_string}
    >>> docformat(docstring, docdict)
    'First line\\n    Second line\\n    indented\\n    string'


def _doc(func):<EOL><INDENT>cls_docstring = getattr(cls, func.__name__).__doc__<EOL>func_docstring = func.__doc__<EOL>if func_docstring is None:<EOL><INDENT>func.__doc__ = cls_docstring<EOL><DEDENT>else:<EOL><INDENT>new_docstring = func_docstring % dict(super=cls_docstring)<EOL>func.__doc__ = new_docstring<EOL><DEDENT>return func<EOL><DEDENT>return _doc<EOL>

This decorator modifies the decorated function's docstring by
replacing occurrences of '%(super)s' with the docstring of the
method of the same name from the class `cls`.

If the decorated method has no docstring, it is simply given the
docstring of `cls`s method.

Parameters
----------
cls : Python class or instance
    A class with a method with the same name as the decorated method.
    The docstring of the method in this class replaces '%(super)s' in the
    docstring of the decorated method.

Returns
-------
f : function
    The decorator function that modifies the __doc__ attribute
    of its argument.

Examples
--------
In the following, the docstring for Bar.func created using the
docstring of `Foo.func`.

>>> class Foo(object):
...     def func(self):
...         '''Do something useful.'''
...         return
...
>>> class Bar(Foo):
...     @inherit_docstring_from(Foo)
...     def func(self):
...         '''%(super)s
...         Do it fast.
...         '''
...         return
...
>>> b = Bar()
>>> b.func.__doc__
'Do something useful.\n        Do it fast.\n        '


indentno = sys.maxsize<EOL>for line in lines:<EOL><INDENT>stripped = line.lstrip()<EOL>if stripped:<EOL><INDENT>indentno = min(indentno, len(line) - len(stripped))<EOL><DEDENT><DEDENT>if indentno == sys.maxsize:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>return indentno<EOL>

Minimum indent for all lines in line list

    >>> lines = [' one', '  two', '   three']
    >>> indentcount_lines(lines)
    1
    >>> lines = []
    >>> indentcount_lines(lines)
    0
    >>> lines = [' one']
    >>> indentcount_lines(lines)
    1
    >>> indentcount_lines(['    '])
    0


if unindent_params:<EOL><INDENT>docdict = unindent_dict(docdict)<EOL><DEDENT>def decorate(f):<EOL><INDENT>f.__doc__ = docformat(f.__doc__, docdict)<EOL>return f<EOL><DEDENT>return decorate<EOL>

Return docstring decorator using docdict variable dictionary

    Parameters
    ----------
    docdict : dictionary
        dictionary containing name, docstring fragment pairs
    unindent_params : {False, True}, boolean, optional
        If True, strip common indentation from all parameters in
        docdict

    Returns
    -------
    decfunc : function
        decorator that applies dictionary to input function docstring


can_dict = {}<EOL>for name, dstr in docdict.items():<EOL><INDENT>can_dict[name] = unindent_string(dstr)<EOL><DEDENT>return can_dict<EOL>


lines = docstring.expandtabs().splitlines()<EOL>icount = indentcount_lines(lines)<EOL>if icount == <NUM_LIT:0>:<EOL><INDENT>return docstring<EOL><DEDENT>return '<STR_LIT:\n>'.join([line[icount:] for line in lines])<EOL>

Set docstring to minimum indent for all lines, including first

    >>> unindent_string(' two')
    'two'
    >>> unindent_string('  two\\n   three')
    'two\\n three'


def fmin_l_bfgs_b(func, x0, fprime=None, args=(),<EOL>approx_grad=<NUM_LIT:0>,<EOL>bounds=None, m=<NUM_LIT:10>, factr=<NUM_LIT>, pgtol=<NUM_LIT>,<EOL>epsilon=<NUM_LIT>,<EOL>iprint=-<NUM_LIT:1>, maxfun=<NUM_LIT>, maxiter=<NUM_LIT>, disp=None,<EOL>callback=None):

<EOL>if approx_grad:<EOL><INDENT>fun = func<EOL>jac = None<EOL><DEDENT>elif fprime is None:<EOL><INDENT>fun = MemoizeJac(func)<EOL>jac = fun.derivative<EOL><DEDENT>else:<EOL><INDENT>fun = func<EOL>jac = fprime<EOL><DEDENT>if disp is None:<EOL><INDENT>disp = iprint<EOL><DEDENT>opts = {'<STR_LIT>': disp,<EOL>'<STR_LIT>': iprint,<EOL>'<STR_LIT>': m,<EOL>'<STR_LIT>': factr * np.finfo(float).eps,<EOL>'<STR_LIT>': pgtol,<EOL>'<STR_LIT>': epsilon,<EOL>'<STR_LIT>': maxfun,<EOL>'<STR_LIT>': maxiter,<EOL>'<STR_LIT>': callback}<EOL>res = _minimize_lbfgsb(fun, x0, args=args, jac=jac, bounds=bounds,<EOL>**opts)<EOL>d = {'<STR_LIT>': res['<STR_LIT>'],<EOL>'<STR_LIT>': res['<STR_LIT:message>'],<EOL>'<STR_LIT>': res['<STR_LIT>'],<EOL>'<STR_LIT>': res['<STR_LIT>'],<EOL>'<STR_LIT>': res['<STR_LIT:status>']}<EOL>f = res['<STR_LIT>']<EOL>x = res['<STR_LIT:x>']<EOL>return x, f, d<EOL>

Minimize a function func using the L-BFGS-B algorithm.

Parameters
----------
func : callable f(x,*args)
    Function to minimise.
x0 : ndarray
    Initial guess.
fprime : callable fprime(x,*args)
    The gradient of `func`.  If None, then `func` returns the function
    value and the gradient (``f, g = func(x, *args)``), unless
    `approx_grad` is True in which case `func` returns only ``f``.
args : sequence
    Arguments to pass to `func` and `fprime`.
approx_grad : bool
    Whether to approximate the gradient numerically (in which case
    `func` returns only the function value).
bounds : list
    ``(min, max)`` pairs for each element in ``x``, defining
    the bounds on that parameter. Use None for one of ``min`` or
    ``max`` when there is no bound in that direction.
m : int
    The maximum number of variable metric corrections
    used to define the limited memory matrix. (The limited memory BFGS
    method does not store the full hessian but uses this many terms in an
    approximation to it.)
factr : float
    The iteration stops when
    ``(f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr * eps``,
    where ``eps`` is the machine precision, which is automatically
    generated by the code. Typical values for `factr` are: 1e12 for
    low accuracy; 1e7 for moderate accuracy; 10.0 for extremely
    high accuracy.
pgtol : float
    The iteration will stop when
    ``max{|proj g_i | i = 1, ..., n} <= pgtol``
    where ``pg_i`` is the i-th component of the projected gradient.
epsilon : float
    Step size used when `approx_grad` is True, for numerically
    calculating the gradient
iprint : int
    Controls the frequency of output. ``iprint < 0`` means no output;
    ``iprint == 0`` means write messages to stdout; ``iprint > 1`` in
    addition means write logging information to a file named
    ``iterate.dat`` in the current working directory.
disp : int, optional
    If zero, then no output.  If a positive number, then this over-rides
    `iprint` (i.e., `iprint` gets the value of `disp`).
maxfun : int
    Maximum number of function evaluations.
maxiter : int
    Maximum number of iterations.
callback : callable, optional
    Called after each iteration, as ``callback(xk)``, where ``xk`` is the
    current parameter vector.

Returns
-------
x : array_like
    Estimated position of the minimum.
f : float
    Value of `func` at the minimum.
d : dict
    Information dictionary.

    * d['warnflag'] is

      - 0 if converged,
      - 1 if too many function evaluations or too many iterations,
      - 2 if stopped for another reason, given in d['task']

    * d['grad'] is the gradient at the minimum (should be 0 ish)
    * d['funcalls'] is the number of function calls made.
    * d['nit'] is the number of iterations.

See also
--------
minimize: Interface to minimization algorithms for multivariate
    functions. See the 'L-BFGS-B' `method` in particular.

Notes
-----
License of L-BFGS-B (FORTRAN code):

The version included here (in fortran code) is 3.0
(released April 25, 2011).  It was written by Ciyou Zhu, Richard Byrd,
and Jorge Nocedal <nocedal@ece.nwu.edu>. It carries the following
condition for use:

This software is freely available, but we expect that all publications
describing work using this software, or all commercial products using it,
quote at least one of the references given below. This software is released
under the BSD License.

References
----------
* R. H. Byrd, P. Lu and J. Nocedal. A Limited Memory Algorithm for Bound
  Constrained Optimization, (1995), SIAM Journal on Scientific and
  Statistical Computing, 16, 5, pp. 1190-1208.
* C. Zhu, R. H. Byrd and J. Nocedal. L-BFGS-B: Algorithm 778: L-BFGS-B,
  FORTRAN routines for large scale bound constrained optimization (1997),
  ACM Transactions on Mathematical Software, 23, 4, pp. 550 - 560.
* J.L. Morales and J. Nocedal. L-BFGS-B: Remark on Algorithm 778: L-BFGS-B,
  FORTRAN routines for large scale bound constrained optimization (2011),
  ACM Transactions on Mathematical Software, 38, 1.


def _minimize_lbfgsb(fun, x0, args=(), jac=None, bounds=None,<EOL>disp=None, maxcor=<NUM_LIT:10>, ftol=<NUM_LIT>,<EOL>gtol=<NUM_LIT>, eps=<NUM_LIT>, maxfun=<NUM_LIT>, maxiter=<NUM_LIT>,<EOL>iprint=-<NUM_LIT:1>, callback=None, **unknown_options):

_check_unknown_options(unknown_options)<EOL>m = maxcor<EOL>epsilon = eps<EOL>pgtol = gtol<EOL>factr = ftol / np.finfo(float).eps<EOL>x0 = asarray(x0).ravel()<EOL>n, = x0.shape<EOL>if bounds is None:<EOL><INDENT>bounds = [(None, None)] * n<EOL><DEDENT>if len(bounds) != n:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if disp is not None:<EOL><INDENT>if disp == <NUM_LIT:0>:<EOL><INDENT>iprint = -<NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>iprint = disp<EOL><DEDENT><DEDENT>n_function_evals, fun = wrap_function(fun, ())<EOL>if jac is None:<EOL><INDENT>def func_and_grad(x):<EOL><INDENT>f = fun(x, *args)<EOL>g = _approx_fprime_helper(x, fun, epsilon, args=args, f0=f)<EOL>return f, g<EOL><DEDENT><DEDENT>else:<EOL><INDENT>def func_and_grad(x):<EOL><INDENT>f = fun(x, *args)<EOL>g = jac(x, *args)<EOL>return f, g<EOL><DEDENT><DEDENT>nbd = zeros(n, int32)<EOL>low_bnd = zeros(n, float64)<EOL>upper_bnd = zeros(n, float64)<EOL>bounds_map = {(None, None): <NUM_LIT:0>,<EOL>(<NUM_LIT:1>, None): <NUM_LIT:1>,<EOL>(<NUM_LIT:1>, <NUM_LIT:1>): <NUM_LIT:2>,<EOL>(None, <NUM_LIT:1>): <NUM_LIT:3>}<EOL>for i in range(<NUM_LIT:0>, n):<EOL><INDENT>l, u = bounds[i]<EOL>if l is not None:<EOL><INDENT>low_bnd[i] = l<EOL>l = <NUM_LIT:1><EOL><DEDENT>if u is not None:<EOL><INDENT>upper_bnd[i] = u<EOL>u = <NUM_LIT:1><EOL><DEDENT>nbd[i] = bounds_map[l, u]<EOL><DEDENT>x = array(x0, float64)<EOL>f = array(<NUM_LIT:0.0>, float64)<EOL>g = zeros((n,), float64)<EOL>wa = zeros(<NUM_LIT:2>*m*n + <NUM_LIT:5>*n + <NUM_LIT:11>*m*m + <NUM_LIT:8>*m, float64)<EOL>iwa = zeros(<NUM_LIT:3>*n, int32)<EOL>task = zeros(<NUM_LIT:1>, '<STR_LIT>')<EOL>csave = zeros(<NUM_LIT:1>, '<STR_LIT>')<EOL>lsave = zeros(<NUM_LIT:4>, int32)<EOL>isave = zeros(<NUM_LIT>, int32)<EOL>dsave = zeros(<NUM_LIT>, float64)<EOL>task[:] = '<STR_LIT>'<EOL>n_iterations = <NUM_LIT:0><EOL>while <NUM_LIT:1>:<EOL><INDENT>_lbfgsb.setulb(m, x, low_bnd, upper_bnd, nbd, f, g, factr,<EOL>pgtol, wa, iwa, task, iprint, csave, lsave,<EOL>isave, dsave)<EOL>task_str = task.tostring()<EOL>if task_str.startswith(b'<STR_LIT>'):<EOL><INDENT>if n_function_evals[<NUM_LIT:0>] > maxfun:<EOL><INDENT>task[:] = ('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>f, g = func_and_grad(x)<EOL><DEDENT><DEDENT>elif task_str.startswith(b'<STR_LIT>'):<EOL><INDENT>if n_iterations > maxiter:<EOL><INDENT>task[:] = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>n_iterations += <NUM_LIT:1><EOL>if callback is not None:<EOL><INDENT>callback(x)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>task_str = task.tostring().strip(b'<STR_LIT:\x00>').strip()<EOL>if task_str.startswith(b'<STR_LIT>'):<EOL><INDENT>warnflag = <NUM_LIT:0><EOL><DEDENT>elif n_function_evals[<NUM_LIT:0>] > maxfun:<EOL><INDENT>warnflag = <NUM_LIT:1><EOL><DEDENT>elif n_iterations > maxiter:<EOL><INDENT>warnflag = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>warnflag = <NUM_LIT:2><EOL><DEDENT>return OptimizeResult(fun=f, jac=g, nfev=n_function_evals[<NUM_LIT:0>],<EOL>nit=n_iterations, status=warnflag, message=task_str,<EOL>x=x, success=(warnflag == <NUM_LIT:0>))<EOL>

Minimize a scalar function of one or more variables using the L-BFGS-B
algorithm.

Options for the L-BFGS-B algorithm are:
    disp : bool
       Set to True to print convergence messages.
    maxcor : int
        The maximum number of variable metric corrections used to
        define the limited memory matrix. (The limited memory BFGS
        method does not store the full hessian but uses this many terms
        in an approximation to it.)
    factr : float
        The iteration stops when ``(f^k -
        f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr * eps``, where ``eps``
        is the machine precision, which is automatically generated by
        the code. Typical values for `factr` are: 1e12 for low
        accuracy; 1e7 for moderate accuracy; 10.0 for extremely high
        accuracy.
    gtol : float
        The iteration will stop when ``max{|proj g_i | i = 1, ..., n}
        <= gtol`` where ``pg_i`` is the i-th component of the
        projected gradient.
    eps : float
        Step size used for numerical approximation of the jacobian.
    disp : int
        Set to True to print convergence messages.
    maxfun : int
        Maximum number of function evaluations.
    maxiter : int
        Maximum number of iterations.

This function is called by the `minimize` function with
`method=L-BFGS-B`. It is not supposed to be called directly.


def minimize(fun, x0, args=(), method=None, jac=None, hess=None,<EOL>hessp=None, bounds=None, constraints=(), tol=None,<EOL>callback=None, options=None):

if not isinstance(args, tuple):<EOL><INDENT>args = (args,)<EOL><DEDENT>if method is None:<EOL><INDENT>if constraints:<EOL><INDENT>method = '<STR_LIT>'<EOL><DEDENT>elif bounds is not None:<EOL><INDENT>method = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>method = '<STR_LIT>'<EOL><DEDENT><DEDENT>if callable(method):<EOL><INDENT>meth = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>meth = method.lower()<EOL><DEDENT>if meth == '<STR_LIT>':<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>DeprecationWarning)<EOL><DEDENT>if options is None:<EOL><INDENT>options = {}<EOL><DEDENT>if meth in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'] and bool(jac):<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>RuntimeWarning)<EOL><DEDENT>if meth not in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>') and hess is not None:<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>RuntimeWarning)<EOL><DEDENT>if meth not in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>') and hessp is not None:<EOL><INDENT>warn('<STR_LIT>'<EOL>'<STR_LIT>' % method, RuntimeWarning)<EOL><DEDENT>if (meth in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>'] and (bounds is not None or any(constraints))):<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>RuntimeWarning)<EOL><DEDENT>if meth in ['<STR_LIT>', '<STR_LIT>'] and any(constraints):<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>RuntimeWarning)<EOL><DEDENT>if meth == '<STR_LIT>' and bounds is not None:<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>RuntimeWarning)<EOL><DEDENT>if (meth in ['<STR_LIT>', '<STR_LIT>'] and callback is not None):<EOL><INDENT>warn('<STR_LIT>' % method, RuntimeWarning)<EOL><DEDENT>if (meth in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'] and<EOL>options.get('<STR_LIT>', False)):<EOL><INDENT>warn('<STR_LIT>' % method,<EOL>RuntimeWarning)<EOL><DEDENT>if not callable(jac):<EOL><INDENT>if bool(jac):<EOL><INDENT>fun = MemoizeJac(fun)<EOL>jac = fun.derivative<EOL><DEDENT>else:<EOL><INDENT>jac = None<EOL><DEDENT><DEDENT>if tol is not None:<EOL><INDENT>options = dict(options)<EOL>if meth in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>options.setdefault('<STR_LIT>', tol)<EOL><DEDENT>if meth in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>']:<EOL><INDENT>options.setdefault('<STR_LIT>', tol)<EOL><DEDENT>if meth in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>options.setdefault('<STR_LIT>', tol)<EOL><DEDENT>if meth in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>options.setdefault('<STR_LIT>', tol)<EOL><DEDENT><DEDENT>if meth == '<STR_LIT>':<EOL><INDENT>return method(fun, x0, args=args, jac=jac, hess=hess, hessp=hessp,<EOL>bounds=bounds, constraints=constraints,<EOL>callback=callback, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_neldermead(fun, x0, args, callback, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_powell(fun, x0, args, callback, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_cg(fun, x0, args, jac, callback, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_bfgs(fun, x0, args, jac, callback, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,<EOL>**options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_anneal(fun, x0, args, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_lbfgsb(fun, x0, args, jac, bounds,<EOL>callback=callback, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_tnc(fun, x0, args, jac, bounds, callback=callback,<EOL>**options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_cobyla(fun, x0, args, constraints, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_slsqp(fun, x0, args, jac, bounds,<EOL>constraints, callback=callback, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_dogleg(fun, x0, args, jac, hess,<EOL>callback=callback, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_trust_ncg(fun, x0, args, jac, hess, hessp,<EOL>callback=callback, **options)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % method)<EOL><DEDENT>

Minimization of scalar function of one or more variables.

Parameters
----------
fun : callable
    Objective function.
x0 : ndarray
    Initial guess.
args : tuple, optional
    Extra arguments passed to the objective function and its
    derivatives (Jacobian, Hessian).
method : str or callable, optional
    Type of solver.  Should be one of

        - 'Nelder-Mead'
        - 'Powell'
        - 'CG'
        - 'BFGS'
        - 'Newton-CG'
        - 'Anneal (deprecated as of scipy version 0.14.0)'
        - 'L-BFGS-B'
        - 'TNC'
        - 'COBYLA'
        - 'SLSQP'
        - 'dogleg'
        - 'trust-ncg'
        - custom - a callable object (added in version 0.14.0)

    If not given, chosen to be one of ``BFGS``, ``L-BFGS-B``, ``SLSQP``,
    depending if the problem has constraints or bounds.
jac : bool or callable, optional
    Jacobian (gradient) of objective function. Only for CG, BFGS,
    Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg.
    If `jac` is a Boolean and is True, `fun` is assumed to return the
    gradient along with the objective function. If False, the
    gradient will be estimated numerically.
    `jac` can also be a callable returning the gradient of the
    objective. In this case, it must accept the same arguments as `fun`.
hess, hessp : callable, optional
    Hessian (matrix of second-order derivatives) of objective function or
    Hessian of objective function times an arbitrary vector p.  Only for
    Newton-CG, dogleg, trust-ncg.
    Only one of `hessp` or `hess` needs to be given.  If `hess` is
    provided, then `hessp` will be ignored.  If neither `hess` nor
    `hessp` is provided, then the Hessian product will be approximated
    using finite differences on `jac`. `hessp` must compute the Hessian
    times an arbitrary vector.
bounds : sequence, optional
    Bounds for variables (only for L-BFGS-B, TNC and SLSQP).
    ``(min, max)`` pairs for each element in ``x``, defining
    the bounds on that parameter. Use None for one of ``min`` or
    ``max`` when there is no bound in that direction.
constraints : dict or sequence of dict, optional
    Constraints definition (only for COBYLA and SLSQP).
    Each constraint is defined in a dictionary with fields:
        type : str
            Constraint type: 'eq' for equality, 'ineq' for inequality.
        fun : callable
            The function defining the constraint.
        jac : callable, optional
            The Jacobian of `fun` (only for SLSQP).
        args : sequence, optional
            Extra arguments to be passed to the function and Jacobian.
    Equality constraint means that the constraint function result is to
    be zero whereas inequality means that it is to be non-negative.
    Note that COBYLA only supports inequality constraints.
tol : float, optional
    Tolerance for termination. For detailed control, use solver-specific
    options.
options : dict, optional
    A dictionary of solver options. All methods accept the following
    generic options:
        maxiter : int
            Maximum number of iterations to perform.
        disp : bool
            Set to True to print convergence messages.
    For method-specific options, see :func:`show_options()`.
callback : callable, optional
    Called after each iteration, as ``callback(xk)``, where ``xk`` is the
    current parameter vector.

Returns
-------
res : OptimizeResult
    The optimization result represented as a ``OptimizeResult`` object.
    Important attributes are: ``x`` the solution array, ``success`` a
    Boolean flag indicating if the optimizer exited successfully and
    ``message`` which describes the cause of the termination. See
    `OptimizeResult` for a description of other attributes.

See also
--------
minimize_scalar : Interface to minimization algorithms for scalar
    univariate functions
show_options : Additional options accepted by the solvers

Notes
-----
This section describes the available solvers that can be selected by the
'method' parameter. The default method is *BFGS*.

**Unconstrained minimization**

Method *Nelder-Mead* uses the Simplex algorithm [1]_, [2]_. This
algorithm has been successful in many applications but other algorithms
using the first and/or second derivatives information might be preferred
for their better performances and robustness in general.

Method *Powell* is a modification of Powell's method [3]_, [4]_ which
is a conjugate direction method. It performs sequential one-dimensional
minimizations along each vector of the directions set (`direc` field in
`options` and `info`), which is updated at each iteration of the main
minimization loop. The function need not be differentiable, and no
derivatives are taken.

Method *CG* uses a nonlinear conjugate gradient algorithm by Polak and
Ribiere, a variant of the Fletcher-Reeves method described in [5]_ pp.
120-122. Only the first derivatives are used.

Method *BFGS* uses the quasi-Newton method of Broyden, Fletcher,
Goldfarb, and Shanno (BFGS) [5]_ pp. 136. It uses the first derivatives
only. BFGS has proven good performance even for non-smooth
optimizations. This method also returns an approximation of the Hessian
inverse, stored as `hess_inv` in the OptimizeResult object.

Method *Newton-CG* uses a Newton-CG algorithm [5]_ pp. 168 (also known
as the truncated Newton method). It uses a CG method to the compute the
search direction. See also *TNC* method for a box-constrained
minimization with a similar algorithm.

Method *Anneal* uses simulated annealing, which is a probabilistic
metaheuristic algorithm for global optimization. It uses no derivative
information from the function being optimized.

Method *dogleg* uses the dog-leg trust-region algorithm [5]_
for unconstrained minimization. This algorithm requires the gradient
and Hessian; furthermore the Hessian is required to be positive definite.

Method *trust-ncg* uses the Newton conjugate gradient trust-region
algorithm [5]_ for unconstrained minimization. This algorithm requires
the gradient and either the Hessian or a function that computes the
product of the Hessian with a given vector.

**Constrained minimization**

Method *L-BFGS-B* uses the L-BFGS-B algorithm [6]_, [7]_ for bound
constrained minimization.

Method *TNC* uses a truncated Newton algorithm [5]_, [8]_ to minimize a
function with variables subject to bounds. This algorithm uses
gradient information; it is also called Newton Conjugate-Gradient. It
differs from the *Newton-CG* method described above as it wraps a C
implementation and allows each variable to be given upper and lower
bounds.

Method *COBYLA* uses the Constrained Optimization BY Linear
Approximation (COBYLA) method [9]_, [10]_, [11]_. The algorithm is
based on linear approximations to the objective function and each
constraint. The method wraps a FORTRAN implementation of the algorithm.

Method *SLSQP* uses Sequential Least SQuares Programming to minimize a
function of several variables with any combination of bounds, equality
and inequality constraints. The method wraps the SLSQP Optimization
subroutine originally implemented by Dieter Kraft [12]_. Note that the
wrapper handles infinite values in bounds by converting them into large
floating values.

**Custom minimizers**

It may be useful to pass a custom minimization method, for example
when using a frontend to this method such as `scipy.optimize.basinhopping`
or a different library.  You can simply pass a callable as the ``method``
parameter.

The callable is called as ``method(fun, x0, args, **kwargs, **options)``
where ``kwargs`` corresponds to any other parameters passed to `minimize`
(such as `callback`, `hess`, etc.), except the `options` dict, which has
its contents also passed as `method` parameters pair by pair.  Also, if
`jac` has been passed as a bool type, `jac` and `fun` are mangled so that
`fun` returns just the function values and `jac` is converted to a function
returning the Jacobian.  The method shall return an ``OptimizeResult``
object.

The provided `method` callable must be able to accept (and possibly ignore)
arbitrary parameters; the set of parameters accepted by `minimize` may
expand in future versions and then these parameters will be passed to
the method.  You can find an example in the scipy.optimize tutorial.

.. versionadded:: 0.11.0

References
----------
.. [1] Nelder, J A, and R Mead. 1965. A Simplex Method for Function
    Minimization. The Computer Journal 7: 308-13.
.. [2] Wright M H. 1996. Direct search methods: Once scorned, now
    respectable, in Numerical Analysis 1995: Proceedings of the 1995
    Dundee Biennial Conference in Numerical Analysis (Eds. D F
    Griffiths and G A Watson). Addison Wesley Longman, Harlow, UK.
    191-208.
.. [3] Powell, M J D. 1964. An efficient method for finding the minimum of
   a function of several variables without calculating derivatives. The
   Computer Journal 7: 155-162.
.. [4] Press W, S A Teukolsky, W T Vetterling and B P Flannery.
   Numerical Recipes (any edition), Cambridge University Press.
.. [5] Nocedal, J, and S J Wright. 2006. Numerical Optimization.
   Springer New York.
.. [6] Byrd, R H and P Lu and J. Nocedal. 1995. A Limited Memory
   Algorithm for Bound Constrained Optimization. SIAM Journal on
   Scientific and Statistical Computing 16 (5): 1190-1208.
.. [7] Zhu, C and R H Byrd and J Nocedal. 1997. L-BFGS-B: Algorithm
   778: L-BFGS-B, FORTRAN routines for large scale bound constrained
   optimization. ACM Transactions on Mathematical Software 23 (4):
   550-560.
.. [8] Nash, S G. Newton-Type Minimization Via the Lanczos Method.
   1984. SIAM Journal of Numerical Analysis 21: 770-778.
.. [9] Powell, M J D. A direct search optimization method that models
   the objective and constraint functions by linear interpolation.
   1994. Advances in Optimization and Numerical Analysis, eds. S. Gomez
   and J-P Hennart, Kluwer Academic (Dordrecht), 51-67.
.. [10] Powell M J D. Direct search algorithms for optimization
   calculations. 1998. Acta Numerica 7: 287-336.
.. [11] Powell M J D. A view of algorithms for optimization without
   derivatives. 2007.Cambridge University Technical Report DAMTP
   2007/NA03
.. [12] Kraft, D. A software package for sequential quadratic
   programming. 1988. Tech. Rep. DFVLR-FB 88-28, DLR German Aerospace
   Center -- Institute for Flight Mechanics, Koln, Germany.

Examples
--------
Let us consider the problem of minimizing the Rosenbrock function. This
function (and its respective derivatives) is implemented in `rosen`
(resp. `rosen_der`, `rosen_hess`) in the `scipy.optimize`.

>>> from scipy.optimize import minimize, rosen, rosen_der

A simple application of the *Nelder-Mead* method is:

>>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
>>> res = minimize(rosen, x0, method='Nelder-Mead')
>>> res.x
[ 1.  1.  1.  1.  1.]

Now using the *BFGS* algorithm, using the first derivative and a few
options:

>>> res = minimize(rosen, x0, method='BFGS', jac=rosen_der,
...                options={'gtol': 1e-6, 'disp': True})
Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 52
         Function evaluations: 64
         Gradient evaluations: 64
>>> res.x
[ 1.  1.  1.  1.  1.]
>>> print res.message
Optimization terminated successfully.
>>> res.hess
[[ 0.00749589  0.01255155  0.02396251  0.04750988  0.09495377]
 [ 0.01255155  0.02510441  0.04794055  0.09502834  0.18996269]
 [ 0.02396251  0.04794055  0.09631614  0.19092151  0.38165151]
 [ 0.04750988  0.09502834  0.19092151  0.38341252  0.7664427 ]
 [ 0.09495377  0.18996269  0.38165151  0.7664427   1.53713523]]

Next, consider a minimization problem with several constraints (namely
Example 16.4 from [5]_). The objective function is:

>>> fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2

There are three constraints defined as:

>>> cons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 2 * x[1] + 2},
...         {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
...         {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2})

And variables must be positive, hence the following bounds:

>>> bnds = ((0, None), (0, None))

The optimization problem is solved using the SLSQP method as:

>>> res = minimize(fun, (2, 0), method='SLSQP', bounds=bnds,
...                constraints=cons)

It should converge to the theoretical solution (1.4 ,1.7).


def minimize_scalar(fun, bracket=None, bounds=None, args=(),<EOL>method='<STR_LIT>', tol=None, options=None):

if not isinstance(args, tuple):<EOL><INDENT>args = (args,)<EOL><DEDENT>if callable(method):<EOL><INDENT>meth = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>meth = method.lower()<EOL><DEDENT>if options is None:<EOL><INDENT>options = {}<EOL><DEDENT>if tol is not None:<EOL><INDENT>options = dict(options)<EOL>if meth == '<STR_LIT>' and '<STR_LIT>' not in options:<EOL><INDENT>warn("<STR_LIT>"<EOL>"<STR_LIT>", RuntimeWarning)<EOL>options['<STR_LIT>'] = tol<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>options.setdefault('<STR_LIT>', tol)<EOL><DEDENT>else:<EOL><INDENT>options.setdefault('<STR_LIT>', tol)<EOL><DEDENT><DEDENT>if meth == '<STR_LIT>':<EOL><INDENT>return method(fun, args=args, bracket=bracket, bounds=bounds, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_scalar_brent(fun, bracket, args, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>if bounds is None:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>return _minimize_scalar_bounded(fun, bounds, args, **options)<EOL><DEDENT>elif meth == '<STR_LIT>':<EOL><INDENT>return _minimize_scalar_golden(fun, bracket, args, **options)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % method)<EOL><DEDENT>

Minimization of scalar function of one variable.

Parameters
----------
fun : callable
    Objective function.
    Scalar function, must return a scalar.
bracket : sequence, optional
    For methods 'brent' and 'golden', `bracket` defines the bracketing
    interval and can either have three items `(a, b, c)` so that `a < b
    < c` and `fun(b) < fun(a), fun(c)` or two items `a` and `c` which
    are assumed to be a starting interval for a downhill bracket search
    (see `bracket`); it doesn't always mean that the obtained solution
    will satisfy `a <= x <= c`.
bounds : sequence, optional
    For method 'bounded', `bounds` is mandatory and must have two items
    corresponding to the optimization bounds.
args : tuple, optional
    Extra arguments passed to the objective function.
method : str or callable, optional
    Type of solver.  Should be one of

        - 'Brent'
        - 'Bounded'
        - 'Golden'
        - custom - a callable object (added in version 0.14.0)
tol : float, optional
    Tolerance for termination. For detailed control, use solver-specific
    options.
options : dict, optional
    A dictionary of solver options.
        maxiter : int
            Maximum number of iterations to perform.
        disp : bool
            Set to True to print convergence messages.

    See :func:`show_options()` for solver-specific options.

Returns
-------
res : OptimizeResult
    The optimization result represented as a ``OptimizeResult`` object.
    Important attributes are: ``x`` the solution array, ``success`` a
    Boolean flag indicating if the optimizer exited successfully and
    ``message`` which describes the cause of the termination. See
    `OptimizeResult` for a description of other attributes.

See also
--------
minimize : Interface to minimization algorithms for scalar multivariate
    functions
show_options : Additional options accepted by the solvers

Notes
-----
This section describes the available solvers that can be selected by the
'method' parameter. The default method is *Brent*.

Method *Brent* uses Brent's algorithm to find a local minimum.
The algorithm uses inverse parabolic interpolation when possible to
speed up convergence of the golden section method.

Method *Golden* uses the golden section search technique. It uses
analog of the bisection method to decrease the bracketed interval. It
is usually preferable to use the *Brent* method.

Method *Bounded* can perform bounded minimization. It uses the Brent
method to find a local minimum in the interval x1 < xopt < x2.

**Custom minimizers**

It may be useful to pass a custom minimization method, for example
when using some library frontend to minimize_scalar.  You can simply
pass a callable as the ``method`` parameter.

The callable is called as ``method(fun, args, **kwargs, **options)``
where ``kwargs`` corresponds to any other parameters passed to `minimize`
(such as `bracket`, `tol`, etc.), except the `options` dict, which has
its contents also passed as `method` parameters pair by pair.  The method
shall return an ``OptimizeResult`` object.

The provided `method` callable must be able to accept (and possibly ignore)
arbitrary parameters; the set of parameters accepted by `minimize` may
expand in future versions and then these parameters will be passed to
the method.  You can find an example in the scipy.optimize tutorial.

.. versionadded:: 0.11.0

Examples
--------
Consider the problem of minimizing the following function.

>>> def f(x):
...     return (x - 2) * x * (x + 2)**2

Using the *Brent* method, we find the local minimum as:

>>> from scipy.optimize import minimize_scalar
>>> res = minimize_scalar(f)
>>> res.x
1.28077640403

Using the *Bounded* method, we find a local minimum with specified
bounds as:

>>> res = minimize_scalar(f, bounds=(-3, -1), method='bounded')
>>> res.x
-2.0000002026


def _check_secant(self, jac_cls, npoints=<NUM_LIT:1>, **kw):

jac = jac_cls(**kw)<EOL>jac.setup(self.xs[<NUM_LIT:0>], self.fs[<NUM_LIT:0>], None)<EOL>for j, (x, f) in enumerate(zip(self.xs[<NUM_LIT:1>:], self.fs[<NUM_LIT:1>:])):<EOL><INDENT>jac.update(x, f)<EOL>for k in xrange(min(npoints, j+<NUM_LIT:1>)):<EOL><INDENT>dx = self.xs[j-k+<NUM_LIT:1>] - self.xs[j-k]<EOL>df = self.fs[j-k+<NUM_LIT:1>] - self.fs[j-k]<EOL>assert_(np.allclose(dx, jac.solve(df)))<EOL><DEDENT>if j >= npoints:<EOL><INDENT>dx = self.xs[j-npoints+<NUM_LIT:1>] - self.xs[j-npoints]<EOL>df = self.fs[j-npoints+<NUM_LIT:1>] - self.fs[j-npoints]<EOL>assert_(not np.allclose(dx, jac.solve(df)))<EOL><DEDENT><DEDENT>

Check that the given Jacobian approximation satisfies secant
conditions for last `npoints` points.


x = d[<NUM_LIT:0>]<EOL>y = d[<NUM_LIT:1>]<EOL>return sign*(<NUM_LIT:2>*x*y + <NUM_LIT:2>*x - x**<NUM_LIT:2> - <NUM_LIT:2>*y**<NUM_LIT:2>)<EOL>

Arguments:
d     - A list of two elements, where d[0] represents x and d[1] represents y
         in the following equation.
sign - A multiplier for f.  Since we want to optimize it, and the scipy
       optimizers can only minimize functions, we need to multiply it by
       -1 to achieve the desired solution
Returns:
2*x*y + 2*x - x**2 - 2*y**2


x = d[<NUM_LIT:0>]<EOL>y = d[<NUM_LIT:1>]<EOL>dfdx = sign*(-<NUM_LIT:2>*x + <NUM_LIT:2>*y + <NUM_LIT:2>)<EOL>dfdy = sign*(<NUM_LIT:2>*x - <NUM_LIT:4>*y)<EOL>return np.array([dfdx, dfdy], float)<EOL>

This is the derivative of fun, returning a numpy array
representing df/dx and df/dy.


def f_eqcon(self, x, sign=<NUM_LIT:1.0>):

return np.array([x[<NUM_LIT:0>] - x[<NUM_LIT:1>]])<EOL>


def fprime_eqcon(self, x, sign=<NUM_LIT:1.0>):

return np.array([[<NUM_LIT:1>, -<NUM_LIT:1>]])<EOL>


def f_eqcon_scalar(self, x, sign=<NUM_LIT:1.0>):

return self.f_eqcon(x, sign)[<NUM_LIT:0>]<EOL>


def fprime_eqcon_scalar(self, x, sign=<NUM_LIT:1.0>):

return self.fprime_eqcon(x, sign)[<NUM_LIT:0>].tolist()<EOL>


def f_ieqcon(self, x, sign=<NUM_LIT:1.0>):

return np.array([x[<NUM_LIT:0>] - x[<NUM_LIT:1>] - <NUM_LIT:1.0>])<EOL>


def fprime_ieqcon(self, x, sign=<NUM_LIT:1.0>):


return np.asarray(x)<EOL>


return np.identity(x.shape[<NUM_LIT:0>])<EOL>

Vector inequality constraint, derivative


return np.ones(shape)<EOL>

A function that returns an array of ones of the given shape.
    `x` is ignored.


def pressure_network(flow_rates, Qtot, k):

P = k * flow_rates**<NUM_LIT:2><EOL>F = np.hstack((P[<NUM_LIT:1>:] - P[<NUM_LIT:0>], flow_rates.sum() - Qtot))<EOL>return F<EOL>

Evaluate non-linear equation system representing
    the pressures and flows in a system of n parallel pipes::

        f_i = P_i - P_0, for i = 1..n
        f_0 = sum(Q_i) - Qtot

    Where Q_i is the flow rate in pipe i and P_i the pressure in that pipe.
    Pressure is modeled as a P=kQ**2 where k is a valve coefficient and
    Q is the flow rate.

    Parameters
    ----------
    flow_rates : float
        A 1D array of n flow rates [kg/s].
    k : float
        A 1D array of n valve coefficients [1/kg m].
    Qtot : float
        A scalar, the total input flow rate [kg/s].

    Returns
    -------
    F : float
        A 1D array, F[i] == f_i.


def pressure_network_jacobian(flow_rates, Qtot, k):

n = len(flow_rates)<EOL>pdiff = np.diag(flow_rates[<NUM_LIT:1>:] * <NUM_LIT:2> * k[<NUM_LIT:1>:] - <NUM_LIT:2> * flow_rates[<NUM_LIT:0>] * k[<NUM_LIT:0>])<EOL>jac = np.empty((n, n))<EOL>jac[:n-<NUM_LIT:1>, :n-<NUM_LIT:1>] = pdiff * <NUM_LIT:0><EOL>jac[:n-<NUM_LIT:1>, n-<NUM_LIT:1>] = <NUM_LIT:0><EOL>jac[n-<NUM_LIT:1>, :] = np.ones(n)<EOL>return jac<EOL>

Return the jacobian of the equation system F(flow_rates)
    computed by `pressure_network` with respect to
    *flow_rates*. See `pressure_network` for the detailed
    description of parrameters.

    Returns
    -------
    jac : float
        *n* by *n* matrix ``df_i/dQ_i`` where ``n = len(flow_rates)``
        and *f_i* and *Q_i* are described in the doc for `pressure_network`


self.fun = (lambda x: np.cos(<NUM_LIT> * x - <NUM_LIT>) + (x + <NUM_LIT>) * x,<EOL>lambda x: (np.cos(<NUM_LIT> * x[<NUM_LIT:0>] - <NUM_LIT>) +<EOL>(x[<NUM_LIT:1>] + <NUM_LIT>) * x[<NUM_LIT:1>] + (x[<NUM_LIT:0>] + <NUM_LIT>) * x[<NUM_LIT:0>]))<EOL>self.x0 = (<NUM_LIT:1.0>, [<NUM_LIT:1.0>, <NUM_LIT:1.0>])<EOL>self.sol = (-<NUM_LIT>, np.array([-<NUM_LIT>, -<NUM_LIT:0.1>]))<EOL>self.upper = (<NUM_LIT>, [<NUM_LIT>, <NUM_LIT>])<EOL>self.lower = (-<NUM_LIT>, [-<NUM_LIT>, -<NUM_LIT>])<EOL>self.maxiter = <NUM_LIT:1000><EOL>np.random.seed(<NUM_LIT>)<EOL>

Tests setup.

        Define two tests, based on the example in anneal's source. Only the
        first one is used since the second fails for the 'fast' schedule at
        least.


def anneal_schedule(self, schedule='<STR_LIT>', use_wrapper=False):

n = <NUM_LIT:0>  <EOL>if use_wrapper:<EOL><INDENT>opts = {'<STR_LIT>': self.upper[n],<EOL>'<STR_LIT>': self.lower[n],<EOL>'<STR_LIT>': <NUM_LIT>,<EOL>'<STR_LIT>': self.maxiter,<EOL>'<STR_LIT>': schedule,<EOL>'<STR_LIT>': False}<EOL>with warnings.catch_warnings():<EOL><INDENT>warnings.simplefilter("<STR_LIT:ignore>", DeprecationWarning)<EOL>res = minimize(self.fun[n], self.x0[n], method='<STR_LIT>',<EOL>options=opts)<EOL><DEDENT>x, retval = res['<STR_LIT:x>'], res['<STR_LIT:status>']<EOL><DEDENT>else:<EOL><INDENT>with warnings.catch_warnings():<EOL><INDENT>warnings.simplefilter("<STR_LIT:ignore>", DeprecationWarning)<EOL>x, retval = anneal(self.fun[n], self.x0[n], full_output=False,<EOL>upper=self.upper[n], lower=self.lower[n],<EOL>feps=<NUM_LIT>, maxiter=self.maxiter,<EOL>schedule=schedule, disp=False)<EOL><DEDENT><DEDENT>assert_almost_equal(x, self.sol[n], <NUM_LIT:2>)<EOL>return retval<EOL>

Call anneal algorithm using specified schedule


x, y = p<EOL>a = x*x + y - <NUM_LIT:11><EOL>b = x + y*y - <NUM_LIT:7><EOL>return a*a + b*b<EOL>

R^2 -> R^1 test function for optimization.  The function has four local
minima where himmelblau(xopt) == 0.


return (x - a)**<NUM_LIT:2> - <NUM_LIT><EOL>


np.random.seed(<NUM_LIT:0>)<EOL>c = - np.random.exponential(size=(m,n))<EOL>Arow = np.zeros((m,m*n))<EOL>brow = np.zeros(m)<EOL>for j in range(m):<EOL><INDENT>j1 = j + <NUM_LIT:1><EOL>Arow[j,j*n:j1*n] = <NUM_LIT:1><EOL>brow[j] = n/m<EOL><DEDENT>Acol = np.zeros((n,m*n))<EOL>bcol = np.zeros(n)<EOL>for j in range(n):<EOL><INDENT>j1 = j + <NUM_LIT:1><EOL>Acol[j,j::n] = <NUM_LIT:1><EOL>bcol[j] = <NUM_LIT:1><EOL><DEDENT>A = np.vstack((Arow,Acol))<EOL>b = np.hstack((brow,bcol))<EOL>return A, b, c.ravel()<EOL>

-> A b c LP test: m*n vars, m+n constraints
        row sums == n/m, col sums == 1
        https://gist.github.com/denis-bz/8647461


s = <NUM_LIT:0.5><EOL>x += np.random.uniform(-s, s, np.shape(x))<EOL>return x<EOL>

redo RandomDisplacement in function form without the attribute stepsize
    to make sure still everything works ok


self.x0 = (<NUM_LIT:1.0>, [<NUM_LIT:1.0>, <NUM_LIT:1.0>])<EOL>self.sol = (-<NUM_LIT>, np.array([-<NUM_LIT>, -<NUM_LIT:0.1>]))<EOL>self.upper = (<NUM_LIT>, [<NUM_LIT>, <NUM_LIT>])<EOL>self.lower = (-<NUM_LIT>, [-<NUM_LIT>, -<NUM_LIT>])<EOL>self.tol = <NUM_LIT:3>  <EOL>self.niter = <NUM_LIT:100><EOL>self.disp = False<EOL>np.random.seed(<NUM_LIT>)<EOL>self.kwargs = {"<STR_LIT>": "<STR_LIT>", "<STR_LIT>": True}<EOL>self.kwargs_nograd = {"<STR_LIT>": "<STR_LIT>"}<EOL>

Tests setup.

        run tests based on the 1-D and 2-D functions described above.  These
        are the same functions as used in the anneal algorithm with some
        gradients added.


def assert_wolfe(s, phi, derphi, c1=<NUM_LIT>, c2=<NUM_LIT>, err_msg="<STR_LIT>"):

phi1 = phi(s)<EOL>phi0 = phi(<NUM_LIT:0>)<EOL>derphi0 = derphi(<NUM_LIT:0>)<EOL>derphi1 = derphi(s)<EOL>msg = "<STR_LIT>" % (<EOL>s, phi0, phi1, derphi0, derphi1, err_msg)<EOL>assert_(phi1 <= phi0 + c1*s*derphi0, "<STR_LIT>" + msg)<EOL>assert_(abs(derphi1) <= abs(c2*derphi0), "<STR_LIT>" + msg)<EOL>

Check that strong Wolfe conditions apply


def assert_armijo(s, phi, c1=<NUM_LIT>, err_msg="<STR_LIT>"):

phi1 = phi(s)<EOL>phi0 = phi(<NUM_LIT:0>)<EOL>msg = "<STR_LIT>" % (s, phi0, phi1, err_msg)<EOL>assert_(phi1 <= (<NUM_LIT:1> - c1*s)*phi0, msg)<EOL>


def assert_fp_equal(x, y, err_msg="<STR_LIT>", nulp=<NUM_LIT:50>):

try:<EOL><INDENT>assert_array_almost_equal_nulp(x, y, nulp)<EOL><DEDENT>except AssertionError as e:<EOL><INDENT>raise AssertionError("<STR_LIT>" % (e, err_msg))<EOL><DEDENT>

Assert two arrays are equal, up to some floating-point rounding error


def _minimize_trust_region(fun, x0, args=(), jac=None, hess=None, hessp=None,<EOL>subproblem=None, initial_trust_radius=<NUM_LIT:1.0>,<EOL>max_trust_radius=<NUM_LIT>, eta=<NUM_LIT>, gtol=<NUM_LIT>,<EOL>maxiter=None, disp=False, return_all=False,<EOL>callback=None, **unknown_options):

_check_unknown_options(unknown_options)<EOL>if jac is None:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if hess is None and hessp is None:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if subproblem is None:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if not (<NUM_LIT:0> <= eta < <NUM_LIT>):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if max_trust_radius <= <NUM_LIT:0>:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>if initial_trust_radius <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if initial_trust_radius >= max_trust_radius:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>x0 = np.asarray(x0).flatten()<EOL>nfun, fun = wrap_function(fun, args)<EOL>njac, jac = wrap_function(jac, args)<EOL>nhess, hess = wrap_function(hess, args)<EOL>nhessp, hessp = wrap_function(hessp, args)<EOL>if maxiter is None:<EOL><INDENT>maxiter = len(x0)*<NUM_LIT:200><EOL><DEDENT>warnflag = <NUM_LIT:0><EOL>trust_radius = initial_trust_radius<EOL>x = x0<EOL>if return_all:<EOL><INDENT>allvecs = [x]<EOL><DEDENT>m = subproblem(x, fun, jac, hess, hessp)<EOL>k = <NUM_LIT:0><EOL>while True:<EOL><INDENT>try:<EOL><INDENT>p, hits_boundary = m.solve(trust_radius)<EOL><DEDENT>except np.linalg.linalg.LinAlgError as e:<EOL><INDENT>warnflag = <NUM_LIT:3><EOL>break<EOL><DEDENT>predicted_value = m(p)<EOL>x_proposed = x + p<EOL>m_proposed = subproblem(x_proposed, fun, jac, hess, hessp)<EOL>actual_reduction = m.fun - m_proposed.fun<EOL>predicted_reduction = m.fun - predicted_value<EOL>if predicted_reduction <= <NUM_LIT:0>:<EOL><INDENT>warnflag = <NUM_LIT:2><EOL>break<EOL><DEDENT>rho = actual_reduction / predicted_reduction<EOL>if rho < <NUM_LIT>:<EOL><INDENT>trust_radius *= <NUM_LIT><EOL><DEDENT>elif rho > <NUM_LIT> and hits_boundary:<EOL><INDENT>trust_radius = min(<NUM_LIT:2>*trust_radius, max_trust_radius)<EOL><DEDENT>if rho > eta:<EOL><INDENT>x = x_proposed<EOL>m = m_proposed<EOL><DEDENT>if return_all:<EOL><INDENT>allvecs.append(x)<EOL><DEDENT>if callback is not None:<EOL><INDENT>callback(x)<EOL><DEDENT>k += <NUM_LIT:1><EOL>if m.jac_mag < gtol:<EOL><INDENT>warnflag = <NUM_LIT:0><EOL>break<EOL><DEDENT>if k >= maxiter:<EOL><INDENT>warnflag = <NUM_LIT:1><EOL>break<EOL><DEDENT><DEDENT>status_messages = (<EOL>_status_message['<STR_LIT:success>'],<EOL>_status_message['<STR_LIT>'],<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>)<EOL>if disp:<EOL><INDENT>if warnflag == <NUM_LIT:0>:<EOL><INDENT>print(status_messages[warnflag])<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>' + status_messages[warnflag])<EOL><DEDENT>print("<STR_LIT>" % m())<EOL>print("<STR_LIT>" % k)<EOL>print("<STR_LIT>" % nfun[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % njac[<NUM_LIT:0>])<EOL>print("<STR_LIT>" % nhess[<NUM_LIT:0>])<EOL><DEDENT>result = OptimizeResult(x=x, success=(warnflag == <NUM_LIT:0>), status=warnflag,<EOL>fun=m.fun, jac=m.jac, nfev=nfun[<NUM_LIT:0>], njev=njac[<NUM_LIT:0>],<EOL>nhev=nhess[<NUM_LIT:0>], nit=k,<EOL>message=status_messages[warnflag])<EOL>if hess is not None:<EOL><INDENT>result['<STR_LIT>'] = m.hess<EOL><DEDENT>if return_all:<EOL><INDENT>result['<STR_LIT>'] = allvecs<EOL><DEDENT>return result<EOL>

Minimization of scalar function of one or more variables using a
trust-region algorithm.

Options for the trust-region algorithm are:
    initial_trust_radius : float
        Initial trust radius.
    max_trust_radius : float
        Never propose steps that are longer than this value.
    eta : float
        Trust region related acceptance stringency for proposed steps.
    gtol : float
        Gradient norm must be less than `gtol`
        before successful termination.
    maxiter : int
        Maximum number of iterations to perform.
    disp : bool
        If True, print convergence message.

This function is called by the `minimize` function.
It is not supposed to be called directly.


if self._f is None:<EOL><INDENT>self._f = self._fun(self._x)<EOL><DEDENT>return self._f<EOL>

Value of objective function at current iteration.


if self._g is None:<EOL><INDENT>self._g = self._jac(self._x)<EOL><DEDENT>return self._g<EOL>

Value of jacobian of objective function at current iteration.


if self._h is None:<EOL><INDENT>self._h = self._hess(self._x)<EOL><DEDENT>return self._h<EOL>

Value of hessian of objective function at current iteration.


if self._g_mag is None:<EOL><INDENT>self._g_mag = scipy.linalg.norm(self.jac)<EOL><DEDENT>return self._g_mag<EOL>

Magniture of jacobian of objective function at current iteration.


def get_boundaries_intersections(self, z, d, trust_radius):

a = np.dot(d, d)<EOL>b = <NUM_LIT:2> * np.dot(z, d)<EOL>c = np.dot(z, z) - trust_radius**<NUM_LIT:2><EOL>sqrt_discriminant = math.sqrt(b*b - <NUM_LIT:4>*a*c)<EOL>ta = (-b - sqrt_discriminant) / (<NUM_LIT:2>*a)<EOL>tb = (-b + sqrt_discriminant) / (<NUM_LIT:2>*a)<EOL>return ta, tb<EOL>

Solve the scalar quadratic equation ||z + t d|| == trust_radius.
This is like a line-sphere intersection.
Return the two values of t, sorted from low to high.


x = asarray(x)<EOL>if not np.issubdtype(x.dtype, np.inexact):<EOL><INDENT>return asarray(x, dtype=np.float_)<EOL><DEDENT>return x<EOL>

Return `x` as an array, of either floats or complex floats


x = np.reshape(x, np.shape(x0))<EOL>wrap = getattr(x0, '<STR_LIT>', x.__array_wrap__)<EOL>return wrap(x)<EOL>

Return ndarray `x` as same array subclass and shape as `x0`


def nonlin_solve(F, x0, jacobian='<STR_LIT>', iter=None, verbose=False,<EOL>maxiter=None, f_tol=None, f_rtol=None, x_tol=None, x_rtol=None,<EOL>tol_norm=None, line_search='<STR_LIT>', callback=None,<EOL>full_output=False, raise_exception=True):

condition = TerminationCondition(f_tol=f_tol, f_rtol=f_rtol,<EOL>x_tol=x_tol, x_rtol=x_rtol,<EOL>iter=iter, norm=tol_norm)<EOL>x0 = _as_inexact(x0)<EOL>func = lambda z: _as_inexact(F(_array_like(z, x0))).flatten()<EOL>x = x0.flatten()<EOL>dx = np.inf<EOL>Fx = func(x)<EOL>Fx_norm = norm(Fx)<EOL>jacobian = asjacobian(jacobian)<EOL>jacobian.setup(x.copy(), Fx, func)<EOL>if maxiter is None:<EOL><INDENT>if iter is not None:<EOL><INDENT>maxiter = iter + <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>maxiter = <NUM_LIT:100>*(x.size+<NUM_LIT:1>)<EOL><DEDENT><DEDENT>if line_search is True:<EOL><INDENT>line_search = '<STR_LIT>'<EOL><DEDENT>elif line_search is False:<EOL><INDENT>line_search = None<EOL><DEDENT>if line_search not in (None, '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>gamma = <NUM_LIT><EOL>eta_max = <NUM_LIT><EOL>eta_treshold = <NUM_LIT:0.1><EOL>eta = <NUM_LIT><EOL>for n in xrange(maxiter):<EOL><INDENT>status = condition.check(Fx, x, dx)<EOL>if status:<EOL><INDENT>break<EOL><DEDENT>tol = min(eta, eta*Fx_norm)<EOL>dx = -jacobian.solve(Fx, tol=tol)<EOL>if norm(dx) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if line_search:<EOL><INDENT>s, x, Fx, Fx_norm_new = _nonlin_line_search(func, x, Fx, dx,<EOL>line_search)<EOL><DEDENT>else:<EOL><INDENT>s = <NUM_LIT:1.0><EOL>x = x + dx<EOL>Fx = func(x)<EOL>Fx_norm_new = norm(Fx)<EOL><DEDENT>jacobian.update(x.copy(), Fx)<EOL>if callback:<EOL><INDENT>callback(x, Fx)<EOL><DEDENT>eta_A = gamma * Fx_norm_new**<NUM_LIT:2> / Fx_norm**<NUM_LIT:2><EOL>if gamma * eta**<NUM_LIT:2> < eta_treshold:<EOL><INDENT>eta = min(eta_max, eta_A)<EOL><DEDENT>else:<EOL><INDENT>eta = min(eta_max, max(eta_A, gamma*eta**<NUM_LIT:2>))<EOL><DEDENT>Fx_norm = Fx_norm_new<EOL>if verbose:<EOL><INDENT>sys.stdout.write("<STR_LIT>" % (<EOL>n, norm(Fx), s, eta))<EOL>sys.stdout.flush()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if raise_exception:<EOL><INDENT>raise NoConvergence(_array_like(x, x0))<EOL><DEDENT>else:<EOL><INDENT>status = <NUM_LIT:2><EOL><DEDENT><DEDENT>if full_output:<EOL><INDENT>info = {'<STR_LIT>': condition.iteration,<EOL>'<STR_LIT>': Fx,<EOL>'<STR_LIT:status>': status,<EOL>'<STR_LIT:success>': status == <NUM_LIT:1>,<EOL>'<STR_LIT:message>': {<NUM_LIT:1>: '<STR_LIT>'<EOL>'<STR_LIT>',<EOL><NUM_LIT:2>: '<STR_LIT>'<EOL>'<STR_LIT>'<EOL>}[status]<EOL>}<EOL>return _array_like(x, x0), info<EOL><DEDENT>else:<EOL><INDENT>return _array_like(x, x0)<EOL><DEDENT>

Find a root of a function, in a way suitable for large-scale problems.

Parameters
----------
%(params_basic)s
jacobian : Jacobian
    A Jacobian approximation: `Jacobian` object or something that
    `asjacobian` can transform to one. Alternatively, a string specifying
    which of the builtin Jacobian approximations to use:

        krylov, broyden1, broyden2, anderson
        diagbroyden, linearmixing, excitingmixing

%(params_extra)s
full_output : bool
    If true, returns a dictionary `info` containing convergence
    information.
raise_exception : bool
    If True, a `NoConvergence` exception is raise if no solution is found.

See Also
--------
asjacobian, Jacobian

Notes
-----
This algorithm implements the inexact Newton method, with
backtracking or full line searches. Several Jacobian
approximations are available, including Krylov and Quasi-Newton
methods.

References
----------
.. [KIM] C. T. Kelley, \"Iterative Methods for Linear and Nonlinear
   Equations\". Society for Industrial and Applied Mathematics. (1995)
   http://www.siam.org/books/kelley/


spsolve = scipy.sparse.linalg.spsolve<EOL>if isinstance(J, Jacobian):<EOL><INDENT>return J<EOL><DEDENT>elif inspect.isclass(J) and issubclass(J, Jacobian):<EOL><INDENT>return J()<EOL><DEDENT>elif isinstance(J, np.ndarray):<EOL><INDENT>if J.ndim > <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>J = np.atleast_2d(np.asarray(J))<EOL>if J.shape[<NUM_LIT:0>] != J.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return Jacobian(matvec=lambda v: dot(J, v),<EOL>rmatvec=lambda v: dot(J.conj().T, v),<EOL>solve=lambda v: solve(J, v),<EOL>rsolve=lambda v: solve(J.conj().T, v),<EOL>dtype=J.dtype, shape=J.shape)<EOL><DEDENT>elif scipy.sparse.isspmatrix(J):<EOL><INDENT>if J.shape[<NUM_LIT:0>] != J.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return Jacobian(matvec=lambda v: J*v,<EOL>rmatvec=lambda v: J.conj().T * v,<EOL>solve=lambda v: spsolve(J, v),<EOL>rsolve=lambda v: spsolve(J.conj().T, v),<EOL>dtype=J.dtype, shape=J.shape)<EOL><DEDENT>elif hasattr(J, '<STR_LIT>') and hasattr(J, '<STR_LIT>') and hasattr(J, '<STR_LIT>'):<EOL><INDENT>return Jacobian(matvec=getattr(J, '<STR_LIT>'),<EOL>rmatvec=getattr(J, '<STR_LIT>'),<EOL>solve=J.solve,<EOL>rsolve=getattr(J, '<STR_LIT>'),<EOL>update=getattr(J, '<STR_LIT>'),<EOL>setup=getattr(J, '<STR_LIT>'),<EOL>dtype=J.dtype,<EOL>shape=J.shape)<EOL><DEDENT>elif callable(J):<EOL><INDENT>class Jac(Jacobian):<EOL><INDENT>def update(self, x, F):<EOL><INDENT>self.x = x<EOL><DEDENT>def solve(self, v, tol=<NUM_LIT:0>):<EOL><INDENT>m = J(self.x)<EOL>if isinstance(m, np.ndarray):<EOL><INDENT>return solve(m, v)<EOL><DEDENT>elif scipy.sparse.isspmatrix(m):<EOL><INDENT>return spsolve(m, v)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>def matvec(self, v):<EOL><INDENT>m = J(self.x)<EOL>if isinstance(m, np.ndarray):<EOL><INDENT>return dot(m, v)<EOL><DEDENT>elif scipy.sparse.isspmatrix(m):<EOL><INDENT>return m*v<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>def rsolve(self, v, tol=<NUM_LIT:0>):<EOL><INDENT>m = J(self.x)<EOL>if isinstance(m, np.ndarray):<EOL><INDENT>return solve(m.conj().T, v)<EOL><DEDENT>elif scipy.sparse.isspmatrix(m):<EOL><INDENT>return spsolve(m.conj().T, v)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>def rmatvec(self, v):<EOL><INDENT>m = J(self.x)<EOL>if isinstance(m, np.ndarray):<EOL><INDENT>return dot(m.conj().T, v)<EOL><DEDENT>elif scipy.sparse.isspmatrix(m):<EOL><INDENT>return m.conj().T * v<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>return Jac()<EOL><DEDENT>elif isinstance(J, str):<EOL><INDENT>return dict(broyden1=BroydenFirst,<EOL>broyden2=BroydenSecond,<EOL>anderson=Anderson,<EOL>diagbroyden=DiagBroyden,<EOL>linearmixing=LinearMixing,<EOL>excitingmixing=ExcitingMixing,<EOL>krylov=KrylovJacobian)[J]()<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>

Convert given object to one suitable for use as a Jacobian.


import inspect<EOL>args, varargs, varkw, defaults = inspect.getargspec(jac.__init__)<EOL>kwargs = list(zip(args[-len(defaults):], defaults))<EOL>kw_str = "<STR_LIT:U+002CU+0020>".join(["<STR_LIT>" % (k, v) for k, v in kwargs])<EOL>if kw_str:<EOL><INDENT>kw_str = "<STR_LIT:U+002CU+0020>" + kw_str<EOL><DEDENT>kwkw_str = "<STR_LIT:U+002CU+0020>".join(["<STR_LIT>" % (k, k) for k, v in kwargs])<EOL>if kwkw_str:<EOL><INDENT>kwkw_str = kwkw_str + "<STR_LIT:U+002CU+0020>"<EOL><DEDENT>wrapper =

Construct a solver wrapper with given name and jacobian approx.

It inspects the keyword arguments of ``jac.__init__``, and allows to
use the same arguments in the wrapper function, in addition to the
keyword arguments of `nonlin_solve`


@staticmethod<EOL><INDENT>def _solve(v, alpha, cs, ds):<DEDENT>

if len(cs) == <NUM_LIT:0>:<EOL><INDENT>return v/alpha<EOL><DEDENT>axpy, dotc = get_blas_funcs(['<STR_LIT>', '<STR_LIT>'], cs[:<NUM_LIT:1>] + [v])<EOL>c0 = cs[<NUM_LIT:0>]<EOL>A = alpha * np.identity(len(cs), dtype=c0.dtype)<EOL>for i, d in enumerate(ds):<EOL><INDENT>for j, c in enumerate(cs):<EOL><INDENT>A[i,j] += dotc(d, c)<EOL><DEDENT><DEDENT>q = np.zeros(len(cs), dtype=c0.dtype)<EOL>for j, d in enumerate(ds):<EOL><INDENT>q[j] = dotc(d, v)<EOL><DEDENT>q /= alpha<EOL>q = solve(A, q)<EOL>w = v/alpha<EOL>for c, qc in zip(cs, q):<EOL><INDENT>w = axpy(c, w, w.size, -qc)<EOL><DEDENT>return w<EOL>


if self.collapsed is not None:<EOL><INDENT>return np.dot(self.collapsed, v)<EOL><DEDENT>return LowRankMatrix._matvec(v, self.alpha, self.cs, self.ds)<EOL>


if self.collapsed is not None:<EOL><INDENT>return np.dot(self.collapsed.T.conj(), v)<EOL><DEDENT>return LowRankMatrix._matvec(v, np.conj(self.alpha), self.ds, self.cs)<EOL>


if self.collapsed is not None:<EOL><INDENT>return solve(self.collapsed, v)<EOL><DEDENT>return LowRankMatrix._solve(v, self.alpha, self.cs, self.ds)<EOL>


if self.collapsed is not None:<EOL><INDENT>return solve(self.collapsed.T.conj(), v)<EOL><DEDENT>return LowRankMatrix._solve(v, np.conj(self.alpha), self.ds, self.cs)<EOL>


self.collapsed = np.array(self)<EOL>self.cs = None<EOL>self.ds = None<EOL>self.alpha = None<EOL>

Collapse the low-rank matrix to a full-rank one.


if self.collapsed is not None:<EOL><INDENT>return<EOL><DEDENT>assert rank > <NUM_LIT:0><EOL>if len(self.cs) > rank:<EOL><INDENT>del self.cs[:]<EOL>del self.ds[:]<EOL><DEDENT>

Reduce the rank of the matrix by dropping all vectors.


if self.collapsed is not None:<EOL><INDENT>return<EOL><DEDENT>assert rank > <NUM_LIT:0><EOL>while len(self.cs) > rank:<EOL><INDENT>del self.cs[<NUM_LIT:0>]<EOL>del self.ds[<NUM_LIT:0>]<EOL><DEDENT>

Reduce the rank of the matrix by dropping oldest vectors.


def svd_reduce(self, max_rank, to_retain=None):

if self.collapsed is not None:<EOL><INDENT>return<EOL><DEDENT>p = max_rank<EOL>if to_retain is not None:<EOL><INDENT>q = to_retain<EOL><DEDENT>else:<EOL><INDENT>q = p - <NUM_LIT:2><EOL><DEDENT>if self.cs:<EOL><INDENT>p = min(p, len(self.cs[<NUM_LIT:0>]))<EOL><DEDENT>q = max(<NUM_LIT:0>, min(q, p-<NUM_LIT:1>))<EOL>m = len(self.cs)<EOL>if m < p:<EOL><INDENT>return<EOL><DEDENT>C = np.array(self.cs).T<EOL>D = np.array(self.ds).T<EOL>D, R = qr(D, mode='<STR_LIT>')<EOL>C = dot(C, R.T.conj())<EOL>U, S, WH = svd(C, full_matrices=False, compute_uv=True)<EOL>C = dot(C, inv(WH))<EOL>D = dot(D, WH.T.conj())<EOL>for k in xrange(q):<EOL><INDENT>self.cs[k] = C[:,k].copy()<EOL>self.ds[k] = D[:,k].copy()<EOL><DEDENT>del self.cs[q:]<EOL>del self.ds[q:]<EOL>

Reduce the rank of the matrix by retaining some SVD components.

This corresponds to the \"Broyden Rank Reduction Inverse\"
algorithm described in [vR]_.

Note that the SVD decomposition can be done by solving only a
problem whose size is the effective rank of this matrix, which
is viable even for large problems.

Parameters
----------
max_rank : int
    Maximum rank of this matrix after reduction.
to_retain : int, optional
    Number of SVD components to retain when reduction is done
    (ie. rank > max_rank). Default is ``max_rank - 2``.

References
----------
.. [vR] B.A. van der Rotten, PhD thesis,
   \"A limited memory Broyden method to solve high-dimensional
   systems of nonlinear equations\". Mathematisch Instituut,
   Universiteit Leiden, The Netherlands (2003).

   http://www.math.leidenuniv.nl/scripties/Rotten.pdf


return np.size(x) == <NUM_LIT:1><EOL>

Test whether `x` is either a scalar or an array scalar.


x = asarray(x)<EOL>r = numpy.sum(<NUM_LIT> * (x[<NUM_LIT:1>:] - x[:-<NUM_LIT:1>]**<NUM_LIT>)**<NUM_LIT> + (<NUM_LIT:1> - x[:-<NUM_LIT:1>])**<NUM_LIT>,<EOL>axis=<NUM_LIT:0>)<EOL>return r<EOL>

The Rosenbrock function.

The function computed is::

    sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0

Parameters
----------
x : array_like
    1-D array of points at which the Rosenbrock function is to be computed.

Returns
-------
f : float
    The value of the Rosenbrock function.

See Also
--------
rosen_der, rosen_hess, rosen_hess_prod


x = asarray(x)<EOL>xm = x[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL>xm_m1 = x[:-<NUM_LIT:2>]<EOL>xm_p1 = x[<NUM_LIT:2>:]<EOL>der = numpy.zeros_like(x)<EOL>der[<NUM_LIT:1>:-<NUM_LIT:1>] = (<NUM_LIT:200> * (xm - xm_m1**<NUM_LIT:2>) -<EOL><NUM_LIT> * (xm_p1 - xm**<NUM_LIT:2>) * xm - <NUM_LIT:2> * (<NUM_LIT:1> - xm))<EOL>der[<NUM_LIT:0>] = -<NUM_LIT> * x[<NUM_LIT:0>] * (x[<NUM_LIT:1>] - x[<NUM_LIT:0>]**<NUM_LIT:2>) - <NUM_LIT:2> * (<NUM_LIT:1> - x[<NUM_LIT:0>])<EOL>der[-<NUM_LIT:1>] = <NUM_LIT:200> * (x[-<NUM_LIT:1>] - x[-<NUM_LIT:2>]**<NUM_LIT:2>)<EOL>return der<EOL>

The derivative (i.e. gradient) of the Rosenbrock function.

Parameters
----------
x : array_like
    1-D array of points at which the derivative is to be computed.

Returns
-------
rosen_der : (N,) ndarray
    The gradient of the Rosenbrock function at `x`.

See Also
--------
rosen, rosen_hess, rosen_hess_prod


x = atleast_1d(x)<EOL>H = numpy.diag(-<NUM_LIT> * x[:-<NUM_LIT:1>], <NUM_LIT:1>) - numpy.diag(<NUM_LIT> * x[:-<NUM_LIT:1>], -<NUM_LIT:1>)<EOL>diagonal = numpy.zeros(len(x), dtype=x.dtype)<EOL>diagonal[<NUM_LIT:0>] = <NUM_LIT> * x[<NUM_LIT:0>]**<NUM_LIT:2> - <NUM_LIT> * x[<NUM_LIT:1>] + <NUM_LIT:2><EOL>diagonal[-<NUM_LIT:1>] = <NUM_LIT:200><EOL>diagonal[<NUM_LIT:1>:-<NUM_LIT:1>] = <NUM_LIT> + <NUM_LIT> * x[<NUM_LIT:1>:-<NUM_LIT:1>]**<NUM_LIT:2> - <NUM_LIT> * x[<NUM_LIT:2>:]<EOL>H = H + numpy.diag(diagonal)<EOL>return H<EOL>

The Hessian matrix of the Rosenbrock function.

Parameters
----------
x : array_like
    1-D array of points at which the Hessian matrix is to be computed.

Returns
-------
rosen_hess : ndarray
    The Hessian matrix of the Rosenbrock function at `x`.

See Also
--------
rosen, rosen_der, rosen_hess_prod


x = atleast_1d(x)<EOL>Hp = numpy.zeros(len(x), dtype=x.dtype)<EOL>Hp[<NUM_LIT:0>] = (<NUM_LIT> * x[<NUM_LIT:0>]**<NUM_LIT:2> - <NUM_LIT> * x[<NUM_LIT:1>] + <NUM_LIT:2>) * p[<NUM_LIT:0>] - <NUM_LIT> * x[<NUM_LIT:0>] * p[<NUM_LIT:1>]<EOL>Hp[<NUM_LIT:1>:-<NUM_LIT:1>] = (-<NUM_LIT> * x[:-<NUM_LIT:2>] * p[:-<NUM_LIT:2>] +<EOL>(<NUM_LIT> + <NUM_LIT> * x[<NUM_LIT:1>:-<NUM_LIT:1>]**<NUM_LIT:2> - <NUM_LIT> * x[<NUM_LIT:2>:]) * p[<NUM_LIT:1>:-<NUM_LIT:1>] -<EOL><NUM_LIT> * x[<NUM_LIT:1>:-<NUM_LIT:1>] * p[<NUM_LIT:2>:])<EOL>Hp[-<NUM_LIT:1>] = -<NUM_LIT> * x[-<NUM_LIT:2>] * p[-<NUM_LIT:2>] + <NUM_LIT:200>*p[-<NUM_LIT:1>]<EOL>return Hp<EOL>

Product of the Hessian matrix of the Rosenbrock function with a vector.

Parameters
----------
x : array_like
    1-D array of points at which the Hessian matrix is to be computed.
p : array_like
    1-D array, the vector to be multiplied by the Hessian matrix.

Returns
-------
rosen_hess_prod : ndarray
    The Hessian matrix of the Rosenbrock function at `x` multiplied
    by the vector `p`.

See Also
--------
rosen, rosen_der, rosen_hess