aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authory-jan137 <yousefjan24000@gmail.com>2026-03-12 15:01:35 +0300
committery-jan137 <yousefjan24000@gmail.com>2026-03-12 15:01:35 +0300
commit8ac4d298a209a2b381f203c2b63e59ace9736846 (patch)
treefb1fbda3739afb60f76dd4bb48241957a5a3baa0 /include
parentbfd7f0d6a9f81e093a390be968058732cc7562c6 (diff)
Add matrix, vector classes and tests
Diffstat (limited to 'include')
-rw-r--r--include/linalg_error.hpp19
-rw-r--r--include/matrix.hpp41
-rw-r--r--include/vector.hpp46
3 files changed, 106 insertions, 0 deletions
diff --git a/include/linalg_error.hpp b/include/linalg_error.hpp
new file mode 100644
index 0000000..8258899
--- /dev/null
+++ b/include/linalg_error.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <stdexcept>
+#include <string>
+
+namespace linalg {
+
+class LinAlgError : public std::runtime_error {
+public:
+ using std::runtime_error::runtime_error;
+};
+
+class DimensionMismatchError : public LinAlgError {
+public:
+ explicit DimensionMismatchError(const std::string& message)
+ : LinAlgError(message) {}
+};
+
+} // namespace linalg
diff --git a/include/matrix.hpp b/include/matrix.hpp
new file mode 100644
index 0000000..ba3e4ae
--- /dev/null
+++ b/include/matrix.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <cstddef>
+#include <initializer_list>
+#include <vector>
+
+namespace linalg {
+
+class Matrix {
+public:
+ Matrix() = default;
+ Matrix(std::size_t rows, std::size_t cols);
+ Matrix(std::size_t rows, std::size_t cols, double value);
+ Matrix(std::initializer_list<std::initializer_list<double>> values);
+
+ [[nodiscard]] std::size_t rows() const noexcept;
+ [[nodiscard]] std::size_t cols() const noexcept;
+ [[nodiscard]] bool empty() const noexcept;
+
+ double& operator()(std::size_t i, std::size_t j);
+ const double& operator()(std::size_t i, std::size_t j) const;
+
+ void fill(double value);
+
+ double* data() noexcept;
+ const double* data() const noexcept;
+
+ static Matrix identity(std::size_t n);
+ static Matrix zeros(std::size_t rows, std::size_t cols);
+
+private:
+ [[nodiscard]] std::size_t index(std::size_t i, std::size_t j) const;
+ void check_bounds(std::size_t i, std::size_t j) const;
+
+ std::size_t rows_ = 0;
+ std::size_t cols_ = 0;
+ std::vector<double> data_;
+};
+
+} // namespace linalg
+
diff --git a/include/vector.hpp b/include/vector.hpp
new file mode 100644
index 0000000..e6bc0ce
--- /dev/null
+++ b/include/vector.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <cstddef>
+#include <initializer_list>
+#include <vector>
+
+namespace linalg {
+
+class Vector {
+public:
+ Vector() = default;
+ explicit Vector(std::size_t n);
+ Vector(std::size_t n, double value);
+ Vector(std::initializer_list<double> values);
+
+ [[nodiscard]] std::size_t size() const noexcept;
+ [[nodiscard]] bool empty() const noexcept;
+
+ double& operator[](std::size_t i);
+ const double& operator[](std::size_t i) const;
+
+ void fill(double value);
+
+ double* data() noexcept;
+ const double* data() const noexcept;
+
+ auto begin() noexcept { return data_.begin(); }
+ auto end() noexcept { return data_.end(); }
+ auto begin() const noexcept { return data_.begin(); }
+ auto end() const noexcept { return data_.end(); }
+ auto cbegin() const noexcept { return data_.cbegin(); }
+ auto cend() const noexcept { return data_.cend(); }
+
+private:
+ void check_index(std::size_t i) const;
+
+ std::vector<double> data_;
+};
+
+Vector operator+(const Vector& lhs, const Vector& rhs);
+Vector operator-(const Vector& lhs, const Vector& rhs);
+Vector operator*(const Vector& v, double scalar);
+Vector operator*(double scalar, const Vector& v);
+double dot(const Vector& lhs, const Vector& rhs);
+
+} // namespace linalg