[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/boostorg/python/develop/example/numpy/ndarray.cpp [Back]  [Original]

// Copyright Ankit Daftery 2011-2012.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

/**
 *  @brief An example to show how to create ndarrays using arbitrary Python sequences.
 *
 *  The Python sequence could be any object whose __array__ method returns an array, or any
 *  (nested) sequence.  This example also shows how to create arrays using both unit and
 *  non-unit strides.      
 */

#include 
#include 

namespace p = boost::python;
namespace np = boost::python::numpy;

#if _MSC_VER
using boost::uint8_t;
#endif

int main(int argc, char **argv)
{
  // Initialize the Python runtime.
  Py_Initialize();
  // Initialize NumPy
  np::initialize();
  // Create an ndarray from a simple tuple
  p::object tu = p::make_tuple('a','b','c') ;
  np::ndarray example_tuple = np::array (tu) ; 
  // and from a list
  p::list l ;
  np::ndarray example_list = np::array (l) ; 
  // Optionally, you can also specify a dtype
  np::dtype dt = np::dtype::get_builtin();
  np::ndarray example_list1 = np::array (l,dt);
  // You can also create an array by supplying data.First,create an integer array
  int data[] = {1,2,3,4} ;
  // Create a shape, and strides, needed by the function
  p::tuple shape = p::make_tuple(4) ;
  p::tuple stride = p::make_tuple(4) ; 
  // The function also needs an owner, to keep track of the data array passed. Passing none is dangerous
  p::object own ;
  // The from_data function takes the data array, datatype,shape,stride and owner as arguments
  // and returns an ndarray
  np::ndarray data_ex = np::from_data(data,dt,shape,stride,own);
  // Print the ndarray we created
  std::cout 

Web Proxy Viewer  |  New URL  |  Original Page