[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/boostorg/python/python313/example/numpy/ufunc.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 demonstrate use of universal functions or ufuncs
 *        
 *
 * @todo Calling the overloaded () operator is in a roundabout manner, find a simpler way
 *       None of the methods like np::add, np::multiply etc are supported as yet
 */

#include 
#include 

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


// Create the structs necessary to implement the ufuncs 
// The typedefs *must* be made

struct UnarySquare 
{
  typedef double argument_type;
  typedef double result_type;

  double operator()(double r) const { return r * r;}
};

struct BinarySquare
{
  typedef double first_argument_type;
  typedef double second_argument_type;
  typedef double result_type;

  double operator()(double a,double b) const { return (a*a + b*b) ; }
};

int main(int argc, char **argv)
{
  // Initialize the Python runtime.
  Py_Initialize();
  // Initialize NumPy
  np::initialize();
  // Expose the struct UnarySquare to Python as a class, and let ud be the class object
  p::object ud = p::class_("UnarySquare")
    .def("__call__", np::unary_ufunc::make());
  // Let inst be an instance of the class ud
  p::object inst = ud();
  // Use the "__call__" method to call the overloaded () operator and print the value
  std::cout 

Web Proxy Viewer  |  New URL  |  Original Page