Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

Translate code from python to c++: import numpy as np def matrix_mult(a, b):

  1. User

    Translate code from python to c++:

    import numpy as np

    def matrix_mult(a, b):

  2. Assistant

    Here is the translation from Python to C++ code:

    #include <iostream>
    #include <vector>
    
    std::vector<std::vector<int>> matrix_mult(const std::vector<std::vector<int>>& a, const std::vector<std::vector<int>>& b) {
        size_t a_rows = a.size();
        size_t a_cols = a[0].size();
        size_t b_rows = b.size();
        size_t b_cols = b[0].size();
    
        if (a_cols != b_rows) {
            throw std::invalid_argument("Matrix dimensions do not match for multiplication.");
        }
    
        std::vector<std::vector<int>> result(a_rows, std::vector<int>(b_cols, 0));
    
        for (size_t i = 0; i < a_rows; ++i) {
            for (size_t j = 0; j < b_cols; ++j) {
                for (size_t k = 0; k < a_cols; ++k) {
                    result[i][j] += a[i][k] * b[k][j];
                }
            }
        }
    
        return result;
    }
    
    int main() {
        std::vector<std::vector<int>> a = {
            {1, 2, 3},
            {4, 5, 6}
        };
    
        std::vector<std::vector<int>> b = {
            {7, 8},
            {9, 10},
            {11, 12}
        };
    
        std::vector<std::vector<int>> result = matrix_mult(a, b);
    
        for (const auto& row : result) {
            for (int val : row) {
                std::cout << val << " ";
            }
            std::cout << std::endl;
        }
    
        return 0;
    }
    
  3. User

    Translate code from python to c++:

    import numpy as np

    def _select_boxes_by_area(self, boxes_xyxy: NDArray, **kwargs) -> NDArray: """ Selects bounding boxes with area within specified range.

        :param boxes: numpy array of bounding boxes coordinates with shape (N, 4) or (N, 6)
        :return: numpy array with indexes of selected boxes with shape (M,)
        """
        if self._area_range is None:
            raise ValueError("Area range must be specified to filter boxes by area")
        areas = (boxes_xyxy[:, 2] - boxes_xyxy[:, 0]) * (boxes_xyxy[:, 3] - boxes_xyxy[:, 1])
        return np.argwhere((areas >= self._area_range[0]) & (areas <= self._area_range[1])).ravel()
    
  4. Assistant

    Here is the translation from Python to C++ code:

    #include <iostream>
    #include <vector>
    #include <stdexcept>
    #include <algorithm>
    
    using NDArray = std::vector<std::vector<double>>;
    
    class MyClass {
    private:
        std::pair<double, double> _area_range;
    
    public:
        MyClass(std::pair<double, double> area_range) : _area_range(area_range) {}
    
        std::vector<size_t> select_boxes_by_area(const NDArray& boxes_xyxy) {
            if (_area_range.first < 0 || _area_range.second < 0) {
                throw std::invalid_argument("Area range must be specified to filter boxes by area");
            }
    
            std::vector<double> areas(boxes_xyxy.size());
            for (size_t i = 0; i < boxes_xyxy.size(); ++i) {
                areas[i] = (boxes_xyxy[i][2] - boxes_xyxy[i][0]) * (boxes_xyxy[i][3] - boxes_xyxy[i][1]);
            }
    
            std::vector<size_t> selected;
            for (size_t i = 0; i < areas.size(); ++i) {
                if (areas[i] >= _area_range.first && areas[i] <= _area_range.second) {
                    selected.push_back(i);
                }
            }
            return selected;
        }
    };
    
    int main() {
        MyClass obj(std::make_pair(1.0, 5.0));
        NDArray boxes = {
            {0.0, 1.0, 2.0, 2.0},
            {1.0, 2.0, 3.0, 3.0},
            {1.0, 0.0, 3.0, 2.0},
        };
        std::vector<size_t> selected = obj.select_boxes_by_area(boxes);
        for (const auto i : selected) {
            std::cout << i << " ";
        }
        return 0;
    }