Mathematical Operations
12/19/25Less than 1 minute
Mathematical Operations
Note: This document is AI-translated. Please refer to the source code for specific APIs and behaviors.
Overview
MathOperator.hpp is the mathematical operation tool module in the ast project, providing a series of general vector and matrix operation functions.
This module adopts a templated design, supporting various data container types, including standard library containers, native arrays, and pointers.
Features
Supported Operation Types
- Basic mathematical functions:
sign,cot,acot,eps - Vector operations:
- Dot product (
dot) - Cross product (
cross) - Norm (
norm) - Normalization (
normalize,normalized)
- Dot product (
- Arithmetic operators: Support for vector-scalar addition, subtraction, multiplication, and division
- Matrix multiplication: Support for multiplication of fixed-size matrices
Multi-container Support
The module is designed to support various data container types:
- Standard library containers like
std::vector,std::array - Native C arrays
double[N] - Native pointers
double* - Any custom container that supports
size()and subscript access[]
Usage Examples
Basic Vector Operations
#include "AstCore/MathOperator.hpp"
#include <vector>
int main()
{
using namespace _AST math;
// 使用 std::vector
std::vector<double> v1 = {1, 2, 3};
std::vector<double> v2 = {4, 5, 6};
double dot_product = dot(v1, v2); // 点积
auto cross_product = cross(v1, v2); // 叉积
double magnitude = norm(v1); // 范数
auto normalized_v = normalized(v1); // 归一化
// 使用原生数组
double arr1[3] = {1, 2, 3};
double arr2[3] = {4, 5, 6};
dot_product = dot(arr1, arr2);
}Arithmetic Operations
#include "AstCore/MathOperator.hpp"
#include <vector>
int main()
{
using namespace _AST math;
std::vector<double> v = {1, 2, 3};
// 标量运算
auto v2 = 2.0 * v; // {2, 4, 6}
auto v3 = v + v; // {2, 4, 6}
auto v4 = v - 1.0; // {0, 1, 2}
}Matrix Operations
#include "AstMath/MathOperator.hpp"
#include "AstMath/Matrix.hpp"
#include <vector>
int main()
{
using namespace _AST math;
AST_USING_NAMESPACE
// 使用 MatrixMN 类
{
MatrixMN<double, 2, 3> A = {1,2,3,4,5,6};
MatrixMN<double, 3, 2> B = {7,8,9,10,11,12};
auto C = A * B; // 2x2 矩阵
}
// 使用原生数组
{
double A[2][3] = {{1,2,3}, {4,5,6}};
double B[3][2] = {{7,8}, {9,10}, {11,12}};
auto C = mtimes(A, B); // 2x2 矩阵
}
}Design Notes
Templated Design
The module is implemented using template functions. As long as a type supports standardized interfaces (size() and subscript access []), it can use these functions. This design provides great flexibility while maintaining type safety.
Performance Considerations
- For operations with known dimensions (e.g.,
dot3,cross3), specialized functions are provided to avoid loop overhead - Support for in-place operations (
normalize) and operations that return copies (normalized) - Compile-time size checks for native array versions
Error Handling
- Use
assertto check vector size consistency - Use
static_assertto check array sizes at compile time - Safe handling for normalization of zero vectors
Dependencies
AstGlobal.h: Project global definitionsmath.h: Mathematical functionsassert.h: Assertion checkstype_traits: Type traitsarray: Standard array containers