/
lyapdy
/
skillbox_controllers
Обзор
Документация
Войти
/
lyapdy
/
skillbox_controllers
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
vectorAlgebra/src/main.cpp
133 строки
4 KB
lyapdy
task10
15 дек 2025, 16:32
15 дек 2025, 16:32
780f797
Код
Авторство
О чём код?
#include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <cmath> #include <vector> namespace py = pybind11; // Функции для работы с векторами std::vector<double> add_vectors(const std::vector<double>& v1, const std::vector<double>& v2) { if (v1.size() != v2.size()) { throw std::invalid_argument("Vectors must have the same size"); } std::vector<double> result(v1.size()); for (size_t i = 0; i < v1.size(); ++i) { result[i] = v1[i] + v2[i]; } return result; } std::vector<double> subtract_vectors(const std::vector<double>& v1, const std::vector<double>& v2) { if (v1.size() != v2.size()) { throw std::invalid_argument("Vectors must have the same size"); } std::vector<double> result(v1.size()); for (size_t i = 0; i < v1.size(); ++i) { result[i] = v1[i] - v2[i]; } return result; } double dot_product(const std::vector<double>& v1, const std::vector<double>& v2) { if (v1.size() != v2.size()) { throw std::invalid_argument("Vectors must have the same size"); } double result = 0.0; for (size_t i = 0; i < v1.size(); ++i) { result += v1[i] * v2[i]; } return result; } // Класс Vector3D class Vector3D { private: double x, y, z; public: Vector3D(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {} // Методы доступа double getX() const { return x; } double getY() const { return y; } double getZ() const { return z; } void setX(double val) { x = val; } void setY(double val) { y = val; } void setZ(double val) { z = val; } // Вычисление длины вектора double length() const { return std::sqrt(x * x + y * y + z * z); } // Нормализация вектора Vector3D normalize() const { double len = length(); if (len == 0) { throw std::runtime_error("Cannot normalize zero-length vector"); } return Vector3D(x / len, y / len, z / len); } // Сложение векторов Vector3D operator+(const Vector3D& other) const { return Vector3D(x + other.x, y + other.y, z + other.z); } // Вычитание векторов Vector3D operator-(const Vector3D& other) const { return Vector3D(x - other.x, y - other.y, z - other.z); } // Скалярное произведение double dot(const Vector3D& other) const { return x * other.x + y * other.y + z * other.z; } }; // Модуль Pybind11 PYBIND11_MODULE(vector_algebra, m) { m.doc() = R"pbdoc( Vector Algebra Module --------------------- This module provides vector algebra operations implemented in C++. )pbdoc"; // Экспорт функций m.def("add_vectors", &add_vectors, "Add two vectors element-wise"); m.def("subtract_vectors", &subtract_vectors, "Subtract two vectors element-wise"); m.def("dot_product", &dot_product, "Calculate dot product of two vectors"); // Экспорт класса Vector3D py::class_<Vector3D>(m, "Vector3D") .def(py::init<double, double, double>(), py::arg("x") = 0, py::arg("y") = 0, py::arg("z") = 0) .def_property("x", &Vector3D::getX, &Vector3D::setX) .def_property("y", &Vector3D::getY, &Vector3D::setY) .def_property("z", &Vector3D::getZ, &Vector3D::setZ) .def("length", &Vector3D::length, "Calculate the length of the vector") .def("normalize", &Vector3D::normalize, "Normalize the vector to unit length") .def("dot", &Vector3D::dot, "Calculate dot product with another vector") .def("__add__", &Vector3D::operator+) .def("__sub__", &Vector3D::operator-) .def("__repr__", [](const Vector3D& v) { return "Vector3D(" + std::to_string(v.getX()) + ", " + std::to_string(v.getY()) + ", " + std::to_string(v.getZ()) + ")"; }) .def("__str__", [](const Vector3D& v) { return "(" + std::to_string(v.getX()) + ", " + std::to_string(v.getY()) + ", " + std::to_string(v.getZ()) + ")"; }); // Добавляем информацию о версии m.attr("__version__") = "0.1.0"; }