<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>HippoDraw Class Library</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> <link rel="shortcut icon" href="hippoApp.ico"> </head><body> <!-- Generated by Doxygen 1.6.2 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_def71da3bddda44674e9e2d37979faa1.html">numarray</a> </div> </div> <div class="contents"> <h1>num_util.h</h1><a href="num__util_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef NUM_UTIL_H__</span> <a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define NUM_UTIL_H__</span> <a name="l00003"></a>00003 <span class="preprocessor"></span> <a name="l00004"></a>00004 <span class="comment">// Copyright 2006 Phil Austin (http://www.eos.ubc.ca/personal/paustin)</span> <a name="l00005"></a>00005 <span class="comment">// Distributed under the Boost Software License, Version 1.0. (See</span> <a name="l00006"></a>00006 <span class="comment">// accompanying file LICENSE_1_0.txt or copy at</span> <a name="l00007"></a>00007 <span class="comment">// http://www.boost.org/LICENSE_1_0.txt)</span> <a name="l00008"></a>00008 <a name="l00009"></a>00009 <span class="comment">//</span> <a name="l00010"></a>00010 <span class="comment">// $Id: num_util.h,v 1.18 2007/07/02 18:17:15 pfkeb Exp $</span> <a name="l00011"></a>00011 <span class="comment">//</span> <a name="l00012"></a>00012 <a name="l00013"></a><a class="code" href="num__util_8h.html#a7d399fcbebb74300921248d831d6ab6f">00013</a> <span class="preprocessor">#define PY_ARRAY_UNIQUE_SYMBOL HippoPyArrayHandle</span> <a name="l00014"></a>00014 <span class="preprocessor"></span><span class="preprocessor">#define NO_IMPORT_ARRAY</span> <a name="l00015"></a>00015 <span class="preprocessor"></span> <a name="l00016"></a>00016 <span class="comment">// for have numarray etc</span> <a name="l00017"></a>00017 <span class="preprocessor">#ifdef HAVE_CONFIG_H</span> <a name="l00018"></a>00018 <span class="preprocessor"></span><span class="preprocessor">#include "config.h"</span> <a name="l00019"></a>00019 <span class="preprocessor">#endif</span> <a name="l00020"></a>00020 <span class="preprocessor"></span> <a name="l00021"></a>00021 <span class="comment">//#define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle</span> <a name="l00022"></a><a class="code" href="num__util_8h.html#ad2cd12e09a08db7cbcae2e74f723d6c4">00022</a> <span class="preprocessor">#define NO_IMPORT_ARRAY</span> <a name="l00023"></a>00023 <span class="preprocessor"></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <boost/python.hpp></span> <a name="l00025"></a>00025 <span class="preprocessor">#ifdef HAVE_NUMPY</span> <a name="l00026"></a>00026 <span class="preprocessor"></span><span class="preprocessor">#include <numpy/noprefix.h></span> <a name="l00027"></a>00027 <span class="preprocessor">#else</span> <a name="l00028"></a>00028 <span class="preprocessor"></span><span class="preprocessor">#ifdef HAVE_NUMERIC</span> <a name="l00029"></a>00029 <span class="preprocessor"></span><span class="preprocessor">#include <Numeric/arrayobject.h></span> <a name="l00030"></a>00030 <span class="preprocessor">#else</span> <a name="l00031"></a>00031 <span class="preprocessor"></span><span class="preprocessor">#include <numarray/arrayobject.h></span> <a name="l00032"></a><a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">00032</a> <span class="keyword">typedef</span> <span class="keywordtype">int</span> <a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">intp</a>; <a name="l00033"></a>00033 <span class="preprocessor">#endif</span> <a name="l00034"></a>00034 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00035"></a>00035 <span class="preprocessor"></span> <a name="l00036"></a>00036 <span class="preprocessor">#include <iostream></span> <a name="l00037"></a>00037 <span class="preprocessor">#include <sstream></span> <a name="l00038"></a>00038 <span class="preprocessor">#include <vector></span> <a name="l00039"></a>00039 <span class="preprocessor">#include <numeric></span> <a name="l00040"></a>00040 <span class="preprocessor">#include <map></span> <a name="l00041"></a>00041 <span class="preprocessor">#include <complex></span> <a name="l00042"></a>00042 <a name="l00043"></a>00043 <a name="l00044"></a>00044 <a name="l00045"></a>00045 <span class="keyword">namespace </span>num_util{ <a name="l00047"></a>00047 <a name="l00052"></a>00052 boost::python::numeric::array <a class="code" href="namespacenum__util.html#a60c82c3b5ac76cd97b322ad93b443f82">makeNum</a>(boost::python::object x); <a name="l00053"></a>00053 <a name="l00061"></a>00061 boost::python::numeric::array <a class="code" href="namespacenum__util.html#a60c82c3b5ac76cd97b322ad93b443f82">makeNum</a>(<a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">intp</a> n, PyArray_TYPES t); <a name="l00062"></a>00062 <a name="l00070"></a>00070 boost::python::numeric::array <a class="code" href="namespacenum__util.html#a60c82c3b5ac76cd97b322ad93b443f82">makeNum</a>(std::vector<int> dimens, <a name="l00071"></a>00071 PyArray_TYPES t); <a name="l00072"></a>00072 <a name="l00080"></a>00080 <span class="preprocessor">#ifdef HAVE_NUMPY</span> <a name="l00081"></a>00081 <span class="preprocessor"></span><span class="preprocessor">#ifdef _MSC_VER</span> <a name="l00082"></a>00082 <span class="preprocessor"></span> <span class="keyword">template</span> <<span class="keyword">typename</span> T> PyArray_TYPES <a class="code" href="namespacenum__util.html#aa7ca794cf44b56df75cc1e97d68ca049" title="Function template returns PyArray_Type for C++ type See num_util.cpp for specializations...">getEnum</a> (); <a name="l00083"></a>00083 <span class="preprocessor">#else</span> <a name="l00084"></a>00084 <span class="preprocessor"></span> <span class="keyword">template</span><<span class="keyword">typename</span> T> PyArray_TYPES <a class="code" href="namespacenum__util.html#aa7ca794cf44b56df75cc1e97d68ca049" title="Function template returns PyArray_Type for C++ type See num_util.cpp for specializations...">getEnum</a>(<span class="keywordtype">void</span>) <a name="l00085"></a>00085 { <a name="l00086"></a>00086 PyErr_SetString(PyExc_ValueError, <span class="stringliteral">"no mapping available for this type"</span>); <a name="l00087"></a>00087 boost::python::throw_error_already_set(); <a name="l00088"></a>00088 <span class="keywordflow">return</span> PyArray_VOID; <a name="l00089"></a>00089 } <a name="l00090"></a>00090 <span class="preprocessor">#endif</span> <a name="l00091"></a>00091 <span class="preprocessor"></span><span class="preprocessor">#else</span> <a name="l00092"></a>00092 <span class="preprocessor"></span> <span class="keyword">template</span> <<span class="keyword">typename</span> T> PyArray_TYPES <a class="code" href="namespacenum__util.html#aa7ca794cf44b56df75cc1e97d68ca049" title="Function template returns PyArray_Type for C++ type See num_util.cpp for specializations...">getEnum</a> (); <a name="l00093"></a>00093 <span class="preprocessor">#endif</span> <a name="l00094"></a>00094 <span class="preprocessor"></span> <a name="l00103"></a><a class="code" href="namespacenum__util.html#aef7545fec8807cf8c51d1c1ed845fff1">00103</a> <span class="keyword">template</span> <<span class="keyword">typename</span> T> boost::python::numeric::array <a class="code" href="namespacenum__util.html#a60c82c3b5ac76cd97b322ad93b443f82">makeNum</a>(<a class="code" href="namespacehippodraw_1_1Axes.html#a1d1cfd8ffb84e947f82999c682b666a7a21f40778b7db9343a6ca75ec2c41ccce" title="title">T</a>* <a class="code" href="namespacenum__util.html#a0c664ca7e08d03a2b3275f4f35efa933">data</a>, <span class="keywordtype">int</span> n = 0){ <a name="l00104"></a>00104 boost::python::object obj(boost::python::handle<>(PyArray_FromDims(1, &n, getEnum<T>()))); <a name="l00105"></a>00105 <span class="preprocessor">#ifdef HAVE_NUMPY</span> <a name="l00106"></a>00106 <span class="preprocessor"></span> <span class="keywordtype">void</span> *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr()); <a name="l00107"></a>00107 memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * n); <span class="comment">// copies the input data to </span> <a name="l00108"></a>00108 <span class="preprocessor">#else</span> <a name="l00109"></a>00109 <span class="preprocessor"></span> <span class="keywordtype">char</span> *arr_data = ((PyArrayObject*) obj.ptr())->data; <a name="l00110"></a>00110 memcpy(arr_data, data, <span class="keyword">sizeof</span>(<a class="code" href="namespacehippodraw_1_1Axes.html#a1d1cfd8ffb84e947f82999c682b666a7a21f40778b7db9343a6ca75ec2c41ccce" title="title">T</a>) * n); <span class="comment">// copies the input data to </span> <a name="l00111"></a>00111 <span class="comment">// PyArrayObject->data</span> <a name="l00112"></a>00112 <span class="preprocessor">#endif</span> <a name="l00113"></a>00113 <span class="preprocessor"></span> <a name="l00114"></a>00114 <span class="keywordflow">return</span> boost::python::extract<boost::python::numeric::array>(obj); <a name="l00115"></a>00115 } <a name="l00116"></a>00116 <a name="l00127"></a><a class="code" href="namespacenum__util.html#af3b58c588eee664db736cc556a98540e">00127</a> <span class="keyword">template</span> <<span class="keyword">typename</span> T> boost::python::numeric::array <a class="code" href="namespacenum__util.html#a60c82c3b5ac76cd97b322ad93b443f82">makeNum</a>(<a class="code" href="namespacehippodraw_1_1Axes.html#a1d1cfd8ffb84e947f82999c682b666a7a21f40778b7db9343a6ca75ec2c41ccce" title="title">T</a> * <a class="code" href="namespacenum__util.html#a0c664ca7e08d03a2b3275f4f35efa933">data</a>, std::vector<int> dims){ <a name="l00128"></a>00128 <a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">intp</a> total = std::accumulate(dims.begin(),dims.end(),1,std::multiplies<intp>()); <a name="l00129"></a>00129 boost::python::object obj(boost::python::handle<>(PyArray_FromDims(dims.size(),&dims[0], getEnum<T>()))); <a name="l00130"></a>00130 <span class="preprocessor">#ifdef HAVE_NUMPY</span> <a name="l00131"></a>00131 <span class="preprocessor"></span> <span class="keywordtype">void</span> *arr_data = PyArray_DATA((PyArrayObject*) obj.ptr()); <a name="l00132"></a>00132 memcpy(arr_data, data, PyArray_ITEMSIZE((PyArrayObject*) obj.ptr()) * total); <a name="l00133"></a>00133 <span class="preprocessor">#else</span> <a name="l00134"></a>00134 <span class="preprocessor"></span> <span class="keywordtype">char</span> *arr_data = ((PyArrayObject*) obj.ptr())->data; <a name="l00135"></a>00135 memcpy(arr_data, data, <span class="keyword">sizeof</span>(<a class="code" href="namespacehippodraw_1_1Axes.html#a1d1cfd8ffb84e947f82999c682b666a7a21f40778b7db9343a6ca75ec2c41ccce" title="title">T</a>) * total); <span class="comment">// copies the input data to </span> <a name="l00136"></a>00136 <span class="comment">// PyArrayObject->data</span> <a name="l00137"></a>00137 <span class="preprocessor">#endif</span> <a name="l00138"></a>00138 <span class="preprocessor"></span> <a name="l00139"></a>00139 <span class="keywordflow">return</span> boost::python::extract<boost::python::numeric::array>(obj); <a name="l00140"></a>00140 } <a name="l00141"></a>00141 <a name="l00142"></a>00142 <span class="keyword">template</span> <> boost::python::numeric::array <a class="code" href="namespacenum__util.html#a44fe559b76f89b5c700710ac387aa819">makeNum<double></a> ( <span class="keywordtype">double</span> * <a class="code" href="namespacenum__util.html#a0c664ca7e08d03a2b3275f4f35efa933">data</a>, <a name="l00143"></a>00143 std::vector < int> dims ); <a name="l00144"></a>00144 <a name="l00150"></a>00150 boost::python::numeric::array <a class="code" href="namespacenum__util.html#a60c82c3b5ac76cd97b322ad93b443f82">makeNum</a>(<span class="keyword">const</span> <a name="l00151"></a>00151 boost::python::numeric::array& arr); <a name="l00152"></a>00152 <a name="l00158"></a>00158 PyArray_TYPES <a class="code" href="namespacenum__util.html#af7e893a8c6a5f6a426480fc85cda4fa1">type</a>(boost::python::numeric::array arr); <a name="l00159"></a>00159 <a name="l00167"></a>00167 <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#a3d2ee3d1dec0fb24b40a8e8e7a386f84" title="Throws an exception if the actual array type is not equal to the expected type.">check_type</a>(boost::python::numeric::array arr, <a name="l00168"></a>00168 PyArray_TYPES expected_type); <a name="l00169"></a>00169 <a name="l00175"></a>00175 <span class="keywordtype">int</span> <a class="code" href="namespacenum__util.html#a59851d76f9624ac3de899f818f9e0a6b">rank</a>(boost::python::numeric::array arr); <a name="l00176"></a>00176 <a name="l00183"></a>00183 <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#a53f5031bede76e4e5bcc9b7b787e0ab7" title="Throws an exception if the actual rank is not equal to the expected rank.">check_rank</a>(boost::python::numeric::array arr, <span class="keywordtype">int</span> expected_rank); <a name="l00184"></a>00184 <a name="l00190"></a>00190 <a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">intp</a> <a class="code" href="namespacenum__util.html#a137f48a49d12a7dae8ba7c86db2a0c01">size</a>(boost::python::numeric::array arr); <a name="l00191"></a>00191 <a name="l00199"></a>00199 <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#a961c6962ff3c1a9d17dcdc3e0f6d5694" title="Throw an exception if the actual total size of the array is not equal to the expected...">check_size</a>(boost::python::numeric::array arr, <a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">intp</a> expected_size); <a name="l00200"></a>00200 <a name="l00206"></a>00206 std::vector<intptr_t> <a class="code" href="namespacenum__util.html#a8570d9b34e8e9d714823c1b1986764f7">shape</a>(boost::python::numeric::array arr); <a name="l00207"></a>00207 <a name="l00214"></a>00214 <a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">intp</a> <a class="code" href="namespacenum__util.html#a1ef5e293536c0e817f7e1ee745a44f75" title="Returns the size of a specific dimension.">get_dim</a>(boost::python::numeric::array arr, <span class="keywordtype">int</span> dimnum); <a name="l00215"></a>00215 <a name="l00223"></a>00223 <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#a66475fb50b21e5af3f8386f3c24b5445">check_shape</a>(boost::python::numeric::array arr, <a name="l00224"></a>00224 std::vector<intp> expected_dims); <a name="l00225"></a>00225 <a name="l00234"></a>00234 <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#a7fdbb1774eb7733325ff303a97c2d899" title="Throws an exception if a specific dimension from a numpy array does not match the...">check_dim</a>(boost::python::numeric::array arr, <span class="keywordtype">int</span> dimnum, <a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">intp</a> dimsize); <a name="l00235"></a>00235 <a name="l00241"></a>00241 <span class="keywordtype">bool</span> <a class="code" href="namespacenum__util.html#a4187547181a3e6d65ba8ebd0e27763e1">iscontiguous</a>(boost::python::numeric::array arr); <a name="l00242"></a>00242 <a name="l00248"></a>00248 <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#ac4f566b08b963a9d20db882d348b1666">check_contiguous</a>(boost::python::numeric::array arr); <a name="l00249"></a>00249 <a name="l00255"></a>00255 <span class="keywordtype">void</span>* <a class="code" href="namespacenum__util.html#a0c664ca7e08d03a2b3275f4f35efa933">data</a>(boost::python::numeric::array arr); <a name="l00256"></a>00256 <a name="l00263"></a>00263 <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#a68d01fd4e7dad61e1210e5e3bfc67a8d" title="Copies data into the array.">copy_data</a>(boost::python::numeric::array arr, <span class="keywordtype">char</span>* new_data); <a name="l00264"></a>00264 <a name="l00270"></a>00270 boost::python::numeric::array <a class="code" href="namespacenum__util.html#a8d145d73a173083711a3829a2bc75f5c">clone</a>(boost::python::numeric::array arr); <a name="l00271"></a>00271 <a name="l00278"></a>00278 boost::python::numeric::array <a class="code" href="namespacenum__util.html#aa4535762e876a986d233209048b2ab66" title="Returns a clone of this array with a new type.">astype</a>(boost::python::numeric::array arr, <a name="l00279"></a>00279 PyArray_TYPES t); <a name="l00280"></a>00280 <a name="l00281"></a>00281 <a name="l00282"></a>00282 <span class="comment">/* *Returns the reference count of the array. */</span> <a name="l00283"></a>00283 <span class="comment">/* *@param arr a Boost/Python numeric array. */</span> <a name="l00284"></a>00284 <span class="comment">/* *@return the reference count of the array. */</span> <a name="l00285"></a>00285 <a name="l00286"></a>00286 <span class="keywordtype">int</span> <a class="code" href="namespacenum__util.html#a418045c387446fe62303a3f3501f27f4">refcount</a>(boost::python::numeric::array arr); <a name="l00287"></a>00287 <a name="l00293"></a>00293 std::vector<intp> <a class="code" href="namespacenum__util.html#ae361da3e047407c0f63f46c5cea041e3">strides</a>(boost::python::numeric::array arr); <a name="l00294"></a>00294 <a name="l00301"></a>00301 <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#aac9973835c54e4c451cd49dde63ade06">check_PyArrayElementType</a>(boost::python::object newo); <a name="l00302"></a>00302 <a name="l00306"></a><a class="code" href="namespacenum__util.html#a5131ca8350fd27b5fd692103f382e442">00306</a> <span class="keyword">typedef</span> std::map<PyArray_TYPES, std::string> <a class="code" href="namespacenum__util.html#a5131ca8350fd27b5fd692103f382e442" title="Mapping from a PyArray_TYPE to its corresponding name in string.">KindStringMap</a>; <a name="l00307"></a>00307 <a name="l00311"></a><a class="code" href="namespacenum__util.html#a2506da57549ce7451e2e0441e9b451d4">00311</a> <span class="keyword">typedef</span> std::map<PyArray_TYPES, char> <a class="code" href="namespacenum__util.html#a2506da57549ce7451e2e0441e9b451d4" title="Mapping from a PyArray_TYPE to its corresponding typeID in char.">KindCharMap</a>; <a name="l00312"></a>00312 <a name="l00316"></a><a class="code" href="namespacenum__util.html#aea55a4011a6604163d129a8e38a8134a">00316</a> <span class="keyword">typedef</span> std::map<char, PyArray_TYPES> <a class="code" href="namespacenum__util.html#aea55a4011a6604163d129a8e38a8134a" title="Mapping from a typeID to its corresponding PyArray_TYPE.">KindTypeMap</a>; <a name="l00317"></a>00317 <a name="l00323"></a>00323 std::string <a class="code" href="namespacenum__util.html#a32710d433aec97899b77e185f1139dd0" title="Converts a PyArray_TYPE to its name in string.">type2string</a>(PyArray_TYPES t_type); <a name="l00324"></a>00324 <a name="l00330"></a>00330 <span class="keywordtype">char</span> <a class="code" href="namespacenum__util.html#a7cb182d747e37882c38df2bacbe9749e" title="Converts a PyArray_TYPE to its single character typecode.">type2char</a>(PyArray_TYPES t_type); <a name="l00331"></a>00331 <a name="l00337"></a>00337 PyArray_TYPES <a class="code" href="namespacenum__util.html#a3b349b23faed27fe7f79cc2346e425a9" title="Coverts a single character typecode to its PyArray_TYPES.">char2type</a>(<span class="keywordtype">char</span> e_type); <a name="l00338"></a>00338 <a name="l00345"></a>00345 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00346"></a>00346 <span class="keyword">inline</span> std::string <a class="code" href="namespacenum__util.html#a3657af9b3e71e36dee0d30fda01e985a" title="Constructs a string which contains a list of elements extracted from the input vector...">vector_str</a>(<span class="keyword">const</span> std::vector<T>& <a class="codeRef" doxygen="Minuit.tag:http://seal.web.cern.ch/seal/MathLibs/Minuit/html/" href="http://seal.web.cern.ch/seal/MathLibs/Minuit/html/classvec.html">vec</a>); <a name="l00347"></a>00347 <a name="l00355"></a>00355 <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="namespacenum__util.html#acf90204e0058f5a341ab276beb9c0d20" title="Throws an exception if the total size computed from a vector of integer does not...">check_size_match</a>(std::vector<intp> dims, <a class="code" href="num__util_8h.html#a4c6d8524229a43d1feecf98da4777b6a">intp</a> n); <a name="l00356"></a>00356 <a name="l00357"></a>00357 } <span class="comment">// namespace num_util</span> <a name="l00358"></a>00358 <a name="l00359"></a>00359 <span class="preprocessor">#endif</span> </pre></div></div> <hr size="1"><address style="align: right;"><small> Generated for HippoDraw Class Library by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border=0> </a> </small></address> </body> </html>