Anurag Bhat 86b4fc623c
Merge numpy to pocketpy (#303)
* Merge numpy to pocketpy

* Add CI

* Fix CI
2024-09-02 16:22:41 +08:00
..
2024-09-02 16:22:41 +08:00
2024-09-02 16:22:41 +08:00
2024-09-02 16:22:41 +08:00
2024-09-02 16:22:41 +08:00
2024-09-02 16:22:41 +08:00

numpy

How to run numpy module programs with gsoc-2024-dev pybind11

  1. Prepare the python code file with the numpy operations you want to run.

    For example : let's try out numpy arange function in test_numpy.py
  import numpy_bindings as np
  
  def test_arange(n):
      a = np.arange(n)
      print(a.sum())
  
  test_arange(100)
  1. Read the script and execute it in test_numpy.cpp.
  #include <pybind11/embed.h>
  #include <fstream>
  #include <sstream>
  #include <string>
  
  namespace py = pybind11;
  using namespace pybind11;
  
  int main() {
      py::scoped_interpreter guard{};
      std::ifstream file("test_numpy.py");
      std::stringstream buffer;
      buffer << file.rdbuf();
      std::string script = buffer.str();
      py::exec(script);
  
      return 0;
  }
  1. Build the project at root to generate the executable at build/gsoc2024.
  cmake -B build
  cmake --build build
  1. Now run the executable to get the output.
  |base| gsoc-2024-dev ±|main ✗|→ build/gsoc2024 
  4950