GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python/doc/numpy/tutorial/simple.rst at develop · boostorg/python

Expand file tree

/

simple.rst

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
41 lines (26 loc) · 1.33 KB
/

simple.rst

Copy path

File metadata and controls

41 lines (26 loc) · 1.33 KB
Raw
Copy raw file
Download raw file
Edit and raw actions

A simple tutorial on Arrays

Let's start with a simple tutorial to create and modify arrays.

Get the necessary headers for numpy components and set up necessary namespaces:

#include <boost/python/numpy.hpp>
#include <iostream>

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

Initialise the Python runtime, and the numpy module. Failure to call these results in segmentation errors:

int main(int argc, char **argv)
{
  Py_Initialize();
  np::initialize();

Zero filled n-dimensional arrays can be created using the shape and data-type of the array as a parameter. Here, the shape is 3x3 and the datatype is the built-in float type:

p::tuple shape = p::make_tuple(3, 3);
np::dtype dtype = np::dtype::get_builtin<float>();
np::ndarray a = np::zeros(shape, dtype);

You can also create an empty array like this

np::ndarray b = np::empty(shape,dtype);

Print the original and reshaped array. The array a which is a list is first converted to a string, and each value in the list is extracted using extract< >:

  std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;

  // Reshape the array into a 1D array
  a = a.reshape(p::make_tuple(9));
  // Print it again.
  std::cout << "Reshaped array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.