feat(cpp-object-oriented-modular-refactoring): step 3 - foundation-google-style
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
#ifndef FESA_MATH_MATRIX_H_
|
||||
#define FESA_MATH_MATRIX_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/math/vector.h"
|
||||
|
||||
namespace fesa {
|
||||
|
||||
/// @brief Owns row-major contiguous storage independently of sparse matrices.
|
||||
class Matrix {
|
||||
public:
|
||||
/// @brief Constructs a row-major matrix initialized to one value.
|
||||
Matrix(std::size_t rows, std::size_t columns, double value = 0.0);
|
||||
|
||||
/// @brief Copies matrix values into independent contiguous storage.
|
||||
Matrix(const Matrix& other);
|
||||
|
||||
/// @brief Moves matrix storage and resets other to a zero-by-zero shape.
|
||||
Matrix(Matrix&& other) noexcept;
|
||||
|
||||
/// @brief Copies matrix values into independent contiguous storage.
|
||||
Matrix& operator=(const Matrix& other);
|
||||
|
||||
/// @brief Moves matrix storage and resets other to a zero-by-zero shape.
|
||||
Matrix& operator=(Matrix&& other) noexcept;
|
||||
|
||||
/// @brief Returns the row count.
|
||||
std::size_t Rows() const noexcept;
|
||||
|
||||
/// @brief Returns the column count.
|
||||
std::size_t Columns() const noexcept;
|
||||
|
||||
/// @brief Returns a bounds-checked mutable entry.
|
||||
/// @throws std::out_of_range if the index is outside the matrix.
|
||||
double& operator()(std::size_t row, std::size_t column);
|
||||
|
||||
/// @brief Returns a bounds-checked immutable entry.
|
||||
/// @throws std::out_of_range if the index is outside the matrix.
|
||||
const double& operator()(std::size_t row, std::size_t column) const;
|
||||
|
||||
/// @brief Multiplies this row-major matrix by a dense vector.
|
||||
/// @throws std::invalid_argument if the dimensions are incompatible.
|
||||
Vector Multiply(const Vector& rhs) const;
|
||||
|
||||
/// @brief Multiplies this row-major matrix by another dense matrix.
|
||||
/// @throws std::invalid_argument if the dimensions are incompatible.
|
||||
Matrix Multiply(const Matrix& rhs) const;
|
||||
|
||||
private:
|
||||
std::size_t rows_;
|
||||
std::size_t columns_;
|
||||
std::vector<double> values_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
|
||||
#endif // FESA_MATH_MATRIX_H_
|
||||
@@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "fesa/math/vector.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
// Owns row-major contiguous dense storage independently of sparse matrices.
|
||||
class Matrix {
|
||||
public:
|
||||
Matrix(std::size_t rows, std::size_t columns, double value = 0.0);
|
||||
Matrix(const Matrix& other);
|
||||
Matrix(Matrix&& other) noexcept;
|
||||
Matrix& operator=(const Matrix& other);
|
||||
Matrix& operator=(Matrix&& other) noexcept;
|
||||
|
||||
std::size_t rows() const noexcept;
|
||||
std::size_t columns() const noexcept;
|
||||
double& operator()(std::size_t row, std::size_t column);
|
||||
const double& operator()(std::size_t row, std::size_t column) const;
|
||||
Vector multiply(const Vector& rhs) const;
|
||||
Matrix multiply(const Matrix& rhs) const;
|
||||
|
||||
private:
|
||||
std::size_t rows_;
|
||||
std::size_t columns_;
|
||||
std::vector<double> values_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
@@ -0,0 +1,73 @@
|
||||
#ifndef FESA_MATH_SPARSE_MATRIX_H_
|
||||
#define FESA_MATH_SPARSE_MATRIX_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/core/status.h"
|
||||
#include "fesa/math/vector.h"
|
||||
|
||||
namespace fesa {
|
||||
|
||||
struct SparsePattern;
|
||||
|
||||
/// @brief Carries one deterministic element-local COO contribution.
|
||||
struct CooContribution {
|
||||
std::size_t row;
|
||||
std::size_t column;
|
||||
double value;
|
||||
std::size_t element_order;
|
||||
std::size_t local_order;
|
||||
};
|
||||
|
||||
/// @brief Owns canonical 0-based CSR independently of the dense Matrix type.
|
||||
class SparseMatrix {
|
||||
public:
|
||||
/// @brief Reduces ordered COO contributions into an expected CSR pattern.
|
||||
/// @return A validated matrix or a structured model failure.
|
||||
/// @note Duplicate sums use stable element and local contribution order.
|
||||
static Result<SparseMatrix> FromCoo(
|
||||
std::size_t rows, std::size_t columns,
|
||||
std::vector<CooContribution> contributions,
|
||||
const SparsePattern& expected_pattern);
|
||||
|
||||
/// @brief Returns the row count.
|
||||
std::size_t Rows() const noexcept;
|
||||
|
||||
/// @brief Returns the column count.
|
||||
std::size_t Columns() const noexcept;
|
||||
|
||||
/// @brief Returns the canonical 0-based CSR row offsets.
|
||||
const std::vector<std::size_t>& RowOffsets() const noexcept;
|
||||
|
||||
/// @brief Returns sorted unique 0-based CSR column indices.
|
||||
const std::vector<std::size_t>& ColumnIndices() const noexcept;
|
||||
|
||||
/// @brief Returns CSR values including preserved structural zeros.
|
||||
const std::vector<double>& Values() const noexcept;
|
||||
|
||||
/// @brief Multiplies this matrix by a dense vector in stable CSR order.
|
||||
/// @throws std::invalid_argument if the dimensions are incompatible.
|
||||
Vector Multiply(const Vector& rhs) const;
|
||||
|
||||
/// @brief Validates shape, indices, ordering, and finite CSR values.
|
||||
/// @return Success or a structured model failure.
|
||||
Status Validate() const;
|
||||
|
||||
private:
|
||||
/// @brief Constructs CSR storage after boundary validation.
|
||||
SparseMatrix(std::size_t rows, std::size_t columns,
|
||||
std::vector<std::size_t> row_offsets,
|
||||
std::vector<std::size_t> column_indices,
|
||||
std::vector<double> values);
|
||||
|
||||
std::size_t rows_;
|
||||
std::size_t columns_;
|
||||
std::vector<std::size_t> row_offsets_;
|
||||
std::vector<std::size_t> column_indices_;
|
||||
std::vector<double> values_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
|
||||
#endif // FESA_MATH_SPARSE_MATRIX_H_
|
||||
@@ -1,53 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "fesa/core/status.hpp"
|
||||
#include "fesa/math/vector.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
struct SparsePattern;
|
||||
|
||||
struct CooContribution {
|
||||
std::size_t row;
|
||||
std::size_t column;
|
||||
double value;
|
||||
std::size_t elementOrder;
|
||||
std::size_t localOrder;
|
||||
};
|
||||
|
||||
// Owns canonical 0-based CSR data independently of the dense Matrix adapter.
|
||||
class SparseMatrix {
|
||||
public:
|
||||
static Result<SparseMatrix> fromCoo(
|
||||
std::size_t rows,
|
||||
std::size_t columns,
|
||||
std::vector<CooContribution> contributions,
|
||||
const SparsePattern& expectedPattern);
|
||||
|
||||
std::size_t rows() const noexcept;
|
||||
std::size_t columns() const noexcept;
|
||||
const std::vector<std::size_t>& rowOffsets() const noexcept;
|
||||
const std::vector<std::size_t>& columnIndices() const noexcept;
|
||||
const std::vector<double>& values() const noexcept;
|
||||
Vector multiply(const Vector& rhs) const;
|
||||
Status validate() const;
|
||||
|
||||
private:
|
||||
SparseMatrix(
|
||||
std::size_t rows,
|
||||
std::size_t columns,
|
||||
std::vector<std::size_t> rowOffsets,
|
||||
std::vector<std::size_t> columnIndices,
|
||||
std::vector<double> values);
|
||||
|
||||
std::size_t rows_;
|
||||
std::size_t columns_;
|
||||
std::vector<std::size_t> rowOffsets_;
|
||||
std::vector<std::size_t> columnIndices_;
|
||||
std::vector<double> values_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
@@ -0,0 +1,64 @@
|
||||
#ifndef FESA_MATH_VECTOR_H_
|
||||
#define FESA_MATH_VECTOR_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
/// @brief Owns a contiguous dense vector while keeping MKL private.
|
||||
class Vector {
|
||||
public:
|
||||
/// @brief Constructs a vector with all entries initialized to one value.
|
||||
explicit Vector(std::size_t size, double value = 0.0);
|
||||
|
||||
/// @brief Copies vector values into independent contiguous storage.
|
||||
Vector(const Vector& other);
|
||||
|
||||
/// @brief Moves vector storage and leaves other empty.
|
||||
Vector(Vector&& other) noexcept;
|
||||
|
||||
/// @brief Copies vector values into independent contiguous storage.
|
||||
Vector& operator=(const Vector& other);
|
||||
|
||||
/// @brief Moves vector storage and leaves other empty.
|
||||
Vector& operator=(Vector&& other) noexcept;
|
||||
|
||||
/// @brief Returns the number of entries.
|
||||
std::size_t Size() const noexcept;
|
||||
|
||||
/// @brief Returns mutable contiguous storage.
|
||||
double* Data() noexcept;
|
||||
|
||||
/// @brief Returns immutable contiguous storage.
|
||||
const double* Data() const noexcept;
|
||||
|
||||
/// @brief Returns a bounds-checked mutable entry.
|
||||
/// @throws std::out_of_range if index is outside the vector.
|
||||
double& operator[](std::size_t index);
|
||||
|
||||
/// @brief Returns a bounds-checked immutable entry.
|
||||
/// @throws std::out_of_range if index is outside the vector.
|
||||
const double& operator[](std::size_t index) const;
|
||||
|
||||
/// @brief Computes the Euclidean dot product with rhs.
|
||||
/// @throws std::invalid_argument if the vector sizes differ.
|
||||
double Dot(const Vector& rhs) const;
|
||||
|
||||
/// @brief Computes the Euclidean norm.
|
||||
double Norm() const;
|
||||
|
||||
/// @brief Scales each entry by alpha through the dense backend.
|
||||
void Scale(double alpha);
|
||||
|
||||
/// @brief Accumulates alpha times x into this vector.
|
||||
/// @throws std::invalid_argument if the vector sizes differ.
|
||||
void Axpy(double alpha, const Vector& x);
|
||||
|
||||
private:
|
||||
std::vector<double> values_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
|
||||
#endif // FESA_MATH_VECTOR_H_
|
||||
@@ -1,31 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
// Owns a contiguous dense vector while keeping the MKL backend private.
|
||||
class Vector {
|
||||
public:
|
||||
explicit Vector(std::size_t size, double value = 0.0);
|
||||
Vector(const Vector& other);
|
||||
Vector(Vector&& other) noexcept;
|
||||
Vector& operator=(const Vector& other);
|
||||
Vector& operator=(Vector&& other) noexcept;
|
||||
|
||||
std::size_t size() const noexcept;
|
||||
double* data() noexcept;
|
||||
const double* data() const noexcept;
|
||||
double& operator[](std::size_t index);
|
||||
const double& operator[](std::size_t index) const;
|
||||
double dot(const Vector& rhs) const;
|
||||
double norm() const;
|
||||
void scale(double alpha);
|
||||
void axpy(double alpha, const Vector& x);
|
||||
|
||||
private:
|
||||
std::vector<double> values_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
Reference in New Issue
Block a user