-
Notifications
You must be signed in to change notification settings - Fork 9
/
arithmetic_operators.hpp
105 lines (88 loc) · 4.09 KB
/
arithmetic_operators.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//==================================================================================================
// File: arithmetic_operators.hpp
//
// Summary: This header defines the overloaded operators that implement basic arithmetic
// operations on vectors and matrices.
//==================================================================================================
//
#ifndef LINEAR_ALGEBRA_ARITHMETIC_OPERATORS_HPP_DEFINED
#define LINEAR_ALGEBRA_ARITHMETIC_OPERATORS_HPP_DEFINED
namespace STD_LA {
//==================================================================================================
// Addition operator, which employs the addition arithmetic traits to do the work.
//==================================================================================================
//
template<class ET1, class COT1, class ET2, class COT2> inline constexpr
auto
operator +(matrix<ET1, COT1> const& m1, matrix<ET2, COT2> const& m2)
{
using op_traits = select_matrix_operation_traits_t<COT1, COT2>;
using op1_type = matrix<ET1, COT1>;
using op2_type = matrix<ET2, COT2>;
using add_traits = matrix_addition_arithmetic_traits_t<op_traits, op1_type, op2_type>;
return add_traits::add(m1, m2);
}
//==================================================================================================
// Subtraction operator, which employs the subtraction arithmetic traits to do the work.
//==================================================================================================
//
template<class ET1, class COT1, class ET2, class COT2> inline constexpr
auto
operator -(matrix<ET1, COT1> const& m1, matrix<ET2, COT2> const& m2)
{
using op_traits = select_matrix_operation_traits_t<COT1, COT2>;
using op1_type = matrix<ET1, COT1>;
using op2_type = matrix<ET2, COT2>;
using sub_traits = matrix_subtraction_arithmetic_traits_t<op_traits, op1_type, op2_type>;
return sub_traits::subtract(m1, m2);
}
//==================================================================================================
// Multiplication operators, which employ the multiplication arithmetic traits to do the work.
//==================================================================================================
//
template<class ET1, class COT1, class ET2, class COT2> inline constexpr
auto
operator *(matrix<ET1, COT1> const& m1, matrix<ET2, COT2> const& m2)
{
using op_traits = select_matrix_operation_traits_t<COT1, COT2>;
using op1_type = matrix<ET1, COT1>;
using op2_type = matrix<ET2, COT2>;
using mul_traits = matrix_multiplication_arithmetic_traits_t<op_traits, op1_type, op2_type>;
return mul_traits::multiply(m1, m2);
}
template<class ET1, class COT1, class S2> inline constexpr
auto
operator *(matrix<ET1, COT1> const& m1, S2 const& s2)
{
using op_traits = COT1;
using op1_type = matrix<ET1, COT1>;
using op2_type = S2;
using mul_traits = matrix_multiplication_arithmetic_traits_t<op_traits, op1_type, op2_type>;
return mul_traits::multiply(m1, s2);
}
template<class S1, class ET2, class COT2> inline constexpr
auto
operator *(S1 const& s1, matrix<ET2, COT2> const& m2)
{
using op_traits = COT2;
using op1_type = S1;
using op2_type = matrix<ET2, COT2>;
using mul_traits = matrix_multiplication_arithmetic_traits_t<op_traits, op1_type, op2_type>;
return mul_traits::multiply(s1, m2);
}
//==================================================================================================
// Scalar division operator, which employs the division arithmetic traits to do the work.
//==================================================================================================
//
template<class ET1, class COT1, class S2> inline constexpr
auto
operator /(matrix<ET1, COT1> const& m1, S2 const& s2)
{
using op_traits = COT1;
using op1_type = matrix<ET1, COT1>;
using op2_type = S2;
using div_traits = matrix_division_arithmetic_traits_t<op_traits, op1_type, op2_type>;
return div_traits::divide(m1, s2);
}
} //- STD_LA namespace
#endif //- LINEAR_ALGEBRA_ARITHMETIC_OPERATORS_HPP_DEFINED