Skip to content

Commit 66105e9

Browse files
author
Alexandre Hoffmann
committed
[WIP] added tensors
1 parent adfca6a commit 66105e9

31 files changed

Lines changed: 1327 additions & 77 deletions

demo/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ find_package(PkgConfig REQUIRED)
22

33
add_executable(demo_Vector demo_Vector.cpp)
44
add_executable(demo_Matrix demo_Matrix.cpp)
5+
add_executable(demo_Tensor demo_Tensor.cpp)
56

67
target_include_directories(demo_Vector PRIVATE ${PROJECT_SOURCE_DIR}/include)
78
target_include_directories(demo_Matrix PRIVATE ${PROJECT_SOURCE_DIR}/include)
9+
target_include_directories(demo_Tensor PRIVATE ${PROJECT_SOURCE_DIR}/include)
810

911
target_link_libraries(demo_Vector PRIVATE FSLinalg)
1012
target_link_libraries(demo_Matrix PRIVATE FSLinalg)
13+
target_link_libraries(demo_Tensor PRIVATE FSLinalg)
1114

demo/demo_Matrix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ int main()
55
FSLinalg::UnitRowVector<4> e0(0);
66
FSLinalg::RealRowVector<3> a({2, 3, 0});
77

8-
const auto expr1 = 0.5*FSLinalg::outer(e0, a)*a;
8+
const auto expr1 = BIC::fixed<double, 0.5>*FSLinalg::outer(e0, a)*a;
99

1010
fmt::print("expr = {}\n", FSLinalg::RealRowVector<4>(expr1));
1111
fmt::print("expr.createTemporaryMatrix = {}\n", expr1.createTemporaryLhs);

demo/demo_Tensor.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <FSLinalg/Tensor.hpp>
2+
3+
int main()
4+
{
5+
FSLinalg::RealTensor<3,3,3> a = FSLinalg::RealTensor<3,3,3>::random();
6+
FSLinalg::RealTensor<3,3,3> b = FSLinalg::RealTensor<3,3,3>::random();
7+
FSLinalg::RealTensor<3,3,3> c = FSLinalg::RealTensor<3,3,3>::random();
8+
FSLinalg::RealTensor<3,3,3> d = FSLinalg::RealTensor<3,3,3>::random();
9+
10+
const auto expr1 = (a + b)*(c + d);
11+
const auto expr2 = (a - b)*(c - d);
12+
13+
fmt::print("a =\n{}\n", a);
14+
fmt::print("b =\n{}\n", b);
15+
fmt::print("c =\n{}\n", c);
16+
fmt::print("d =\n{}\n", d);
17+
fmt::print("\n");
18+
fmt::print("expr1 =\n{}\n", expr1);
19+
fmt::print("expr2 =\n{}\n", expr2);
20+
fmt::print("\n");
21+
fmt::print("expr1 < expr2 =\n{}\n", expr1 < expr2);
22+
fmt::print("any(expr2) = {}\n", FSLinalg::any(expr1 < expr2));
23+
fmt::print("all(expr2) = {}\n", FSLinalg::all(expr1 < expr2));
24+
25+
return EXIT_SUCCESS;
26+
}

include/FSLinalg/BasicLinalg/Product.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ template<bool conjugateA, bool conjugateB>
1212
struct Product
1313
{
1414
template<RealScalar_concept A, RealScalar_concept B>
15-
using RealReturnType = std::common_type_t<A, B>;
15+
using RealReturnType = decltype(std::declval<A>() * std::declval<B>());
1616

1717
template<Scalar_concept A, Scalar_concept B>
1818
using CpxReturnType = std::complex<RealReturnType<

include/FSLinalg/Matrix/Formater.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ template<FSLinalg::Matrix_concept Expr>
99
class fmt::formatter< Expr > : public fmt::formatter< typename FSLinalg::MatrixBase<Expr>::Scalar >
1010
{
1111
public:
12-
static_assert(Expr::hasReadRandomAccess, "Vector must have a read random access Iterator");
12+
static_assert(Expr::hasReadRandomAccess, "Matrix must have a read random access Iterator");
1313

1414
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
1515

@@ -18,8 +18,8 @@ class fmt::formatter< Expr > : public fmt::formatter< typename FSLinalg::MatrixB
1818
{
1919
using Size = typename FSLinalg::MatrixBase<Expr>::Size;
2020

21-
Size rowsM1 = Size(A.getRows()-1);
22-
Size colsM1 = Size(A.getCols()-1);
21+
const Size rowsM1 = Size(A.getRows()-1);
22+
const Size colsM1 = Size(A.getCols()-1);
2323

2424
auto out = ctx.out();
2525

include/FSLinalg/Matrix/Matrix.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ template<typename T, unsigned int Nrows, unsigned Ncols> class Matrix;
1313
template<typename T, unsigned int Nrows, unsigned Ncols>
1414
struct MatrixTraits< Matrix<T, Nrows, Ncols> >
1515
{
16-
using Scalar = T;
17-
using Size = unsigned int;
16+
using Scalar = T;
17+
using Size = unsigned int;
1818

1919
static constexpr bool hasReadRandomAccess = true;
2020
static constexpr bool hasWriteRandomAccess = true;
@@ -45,19 +45,19 @@ class Matrix : public MatrixBase< Matrix<T, Nrows, Ncols> >
4545

4646
Matrix(const RealScalar& value = RealScalar(0)) requires(isScalarComplex) { for (Size i=0; i!=size; ++i) { m_data[i] = value; } }
4747
Matrix(std::initializer_list< std::initializer_list<RealScalar> > values) requires(isScalarComplex);
48-
Matrix(std::initializer_list<RealScalar> values) requires(isScalarComplex and isVector) : m_data(values) {}
48+
Matrix(std::initializer_list<RealScalar> values) requires(isScalarComplex and isVector) { std::copy(std::cbegin(values), std::cend(values), std::begin(m_data)); }
4949

5050
Matrix(const Scalar& value = Scalar(0)) { m_data.fill(value); }
5151
Matrix(std::initializer_list< std::initializer_list<Scalar> > values);
5252
Matrix(std::initializer_list<Scalar> values) requires(isVector) { std::copy(std::cbegin(values), std::cend(values), std::begin(m_data)); }
5353

5454
Matrix(const Matrix& other) : m_data(other.m_data) {}
5555

56-
template<class Expr> Matrix(const MatrixBase<Expr>& expr) requires(IsConstructibleFrom<Expr>::value) { expr.assignTo(BIC::fixed<bool, false>, Scalar(1), *this); }
56+
template<class Expr> Matrix(const MatrixBase<Expr>& expr) requires(IsConstructibleFrom<Expr>::value) { expr.assignTo(BIC::fixed<bool, false>, BIC::fixed<RealScalar, RealScalar(1)>, *this); }
5757

58-
template<class Expr> Matrix& operator= (const MatrixBase<Expr>& expr) requires(IsConstructibleFrom<Expr>::value) { expr.assignTo (BIC::fixed<bool, true>, Scalar(1), *this); return *this; }
59-
template<class Expr> Matrix& operator+=(const MatrixBase<Expr>& expr) requires(IsConstructibleFrom<Expr>::value) { expr.increment (BIC::fixed<bool, true>, Scalar(1), *this); return *this; }
60-
template<class Expr> Matrix& operator-=(const MatrixBase<Expr>& expr) requires(IsConstructibleFrom<Expr>::value) { expr.decrement (BIC::fixed<bool, true>, Scalar(1), *this); return *this; }
58+
template<class Expr> Matrix& operator= (const MatrixBase<Expr>& expr) requires(IsConstructibleFrom<Expr>::value) { expr.assignTo (BIC::fixed<bool, true>, BIC::fixed<RealScalar, RealScalar(1)>, *this); return *this; }
59+
template<class Expr> Matrix& operator+=(const MatrixBase<Expr>& expr) requires(IsConstructibleFrom<Expr>::value) { expr.increment (BIC::fixed<bool, true>, BIC::fixed<RealScalar, RealScalar(1)>, *this); return *this; }
60+
template<class Expr> Matrix& operator-=(const MatrixBase<Expr>& expr) requires(IsConstructibleFrom<Expr>::value) { expr.decrement (BIC::fixed<bool, true>, BIC::fixed<RealScalar, RealScalar(1)>, *this); return *this; }
6161

6262
Matrix& operator*=(const RealScalar& alpha) requires(isScalarComplex) { for (Size i=0; i!=size; ++i) { m_data[i] *= alpha; } return *this; }
6363
Matrix& operator/=(const RealScalar& alpha) requires(isScalarComplex) { for (Size i=0; i!=size; ++i) { m_data[i] /= alpha; } return *this; }
@@ -76,10 +76,10 @@ class Matrix : public MatrixBase< Matrix<T, Nrows, Ncols> >
7676
template<class Dst> bool isAliasedToImpl(const MatrixBase<Dst>& dst) const requires( CanBeAlisaedTo<Dst>::value) { return std::addressof(dst.derived()) == this; }
7777
template<class Dst> constexpr bool isAliasedToImpl(const MatrixBase<Dst>& ) const requires(not CanBeAlisaedTo<Dst>::value) { return false; }
7878

79-
static Matrix zero() { return Matrix(Scalar(0)); }
80-
static Matrix ones() { return Matrix(Scalar(1)); }
79+
static Matrix zero() { return Matrix(RealScalar(0)); }
80+
static Matrix ones() { return Matrix(RealScalar(1)); }
8181

82-
static Matrix random(const RealScalar& lb, const RealScalar& ub);
82+
static Matrix random(const RealScalar& lb = RealScalar(-1), const RealScalar& ub = RealScalar(1));
8383
private:
8484
std::array<Scalar, size> m_data;
8585
};

include/FSLinalg/Matrix/MatrixBase.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <FSLinalg/CRTPBase.hpp>
55
#include <FSLinalg/Scalar.hpp>
66
#include <FSLinalg/misc/Logical.hpp>
7-
#include <type_traits>
87

98
namespace FSLinalg
109
{
@@ -15,7 +14,7 @@ template<class Derived>
1514
class MatrixBase : public CRTPBase<Derived>
1615
{
1716
public:
18-
using Base = CRTPBase<Derived>;
17+
using CRTP = CRTPBase<Derived>;
1918
using DerivedTraits = MatrixTraits<Derived>;
2019
using Scalar = typename DerivedTraits::Scalar;
2120
using RealScalar = typename NumTraits<Scalar>::Real;
@@ -53,13 +52,13 @@ class MatrixBase : public CRTPBase<Derived>
5352
constexpr Size getCols() const { return nCols; }
5453
constexpr Size getSize() const { return size; }
5554

56-
const_ReturnType operator()(const Size& i, const Size j) const requires(hasReadRandomAccess) { return Base::derived().getImpl(i, j); }
57-
ReturnType operator()(const Size& i, const Size j) requires(hasWriteRandomAccess) { return Base::derived().getImpl(i, j); }
55+
const_ReturnType operator()(const Size i, const Size j) const requires(hasReadRandomAccess) { return CRTP::derived().getImpl(i, j); }
56+
ReturnType operator()(const Size i, const Size j) requires(hasWriteRandomAccess) { return CRTP::derived().getImpl(i, j); }
5857

59-
const_ReturnType operator[](const Size& i) const requires(hasReadRandomAccess and hasFlatRandomAccess) { return Base::derived().getImpl(i); }
60-
ReturnType operator[](const Size& i) requires(hasWriteRandomAccess and hasFlatRandomAccess) { return Base::derived().getImpl(i); }
58+
const_ReturnType operator[](const Size i) const requires(hasReadRandomAccess and hasFlatRandomAccess) { return CRTP::derived().getImpl(i); }
59+
ReturnType operator[](const Size i) requires(hasWriteRandomAccess and hasFlatRandomAccess) { return CRTP::derived().getImpl(i); }
6160

62-
template<class Dst> bool isAliasedTo(const MatrixBase<Dst>& other) const { return Base::derived().isAliasedToImpl(other); }
61+
template<class Dst> bool isAliasedTo(const MatrixBase<Dst>& other) const { return CRTP::derived().isAliasedToImpl(other); }
6362

6463
template<typename Bool, typename Alpha, class Dst>
6564
void assignTo(const Bool checkAliasing, const Alpha& alpha, MatrixBase<Dst>& dst) const requires(IsConvertibleTo<Dst>::value and IsScalar<Alpha>::value);

include/FSLinalg/Matrix/MatrixBase_impl.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ void MatrixBase<Derived>::assignTo(const Bool checkAliasing, const Alpha& alpha,
3232
{
3333
if constexpr (hasFlatRandomAccess)
3434
{
35-
for (Size i=0; i!=getSize(); ++i) { dst[i] = alpha*Base::derived().getImpl(i); }
35+
for (Size i=0; i!=getSize(); ++i) { dst[i] = alpha*CRTP::derived().getImpl(i); }
3636
}
3737
else
3838
{
39-
for (Size i=0; i!=getRows(); ++i) { for (Size j=0; j!=getCols(); ++j) { dst(i,j) = alpha*Base::derived().getImpl(i,j); }}
39+
for (Size i=0; i!=getRows(); ++i) { for (Size j=0; j!=getCols(); ++j) { dst(i,j) = alpha*CRTP::derived().getImpl(i,j); }}
4040
}
4141
}
4242
}
4343
else
4444
{
45-
Base::derived().assignToImpl(checkAliasing, alpha, dst);
45+
CRTP::derived().assignToImpl(checkAliasing, alpha, dst);
4646
}
4747
}
4848

@@ -71,17 +71,17 @@ void MatrixBase<Derived>::increment(const Bool checkAliasing, const Alpha& alpha
7171
{
7272
if constexpr (hasFlatRandomAccess)
7373
{
74-
for (Size i=0; i!=getSize(); ++i) { dst[i] += alpha*Base::derived().getImpl(i); }
74+
for (Size i=0; i!=getSize(); ++i) { dst[i] += alpha*CRTP::derived().getImpl(i); }
7575
}
7676
else
7777
{
78-
for (Size i=0; i!=getRows(); ++i) { for (Size j=0; j!=getCols(); ++j) { dst(i,j) += alpha*Base::derived().getImpl(i,j); }}
78+
for (Size i=0; i!=getRows(); ++i) { for (Size j=0; j!=getCols(); ++j) { dst(i,j) += alpha*CRTP::derived().getImpl(i,j); }}
7979
}
8080
}
8181
}
8282
else
8383
{
84-
Base::derived().incrementImpl(checkAliasing, alpha, dst);
84+
CRTP::derived().incrementImpl(checkAliasing, alpha, dst);
8585
}
8686
}
8787

@@ -110,17 +110,17 @@ void MatrixBase<Derived>::decrement(const Bool checkAliasing, const Alpha& alpha
110110
{
111111
if constexpr (hasFlatRandomAccess)
112112
{
113-
for (Size i=0; i!=getSize(); ++i) { dst[i] -= alpha*Base::derived().getImpl(i); }
113+
for (Size i=0; i!=getSize(); ++i) { dst[i] -= alpha*CRTP::derived().getImpl(i); }
114114
}
115115
else
116116
{
117-
for (Size i=0; i!=getRows(); ++i) { for (Size j=0; j!=getCols(); ++j) { dst(i,j) -= alpha*Base::derived().getImpl(i,j); }}
117+
for (Size i=0; i!=getRows(); ++i) { for (Size j=0; j!=getCols(); ++j) { dst(i,j) -= alpha*CRTP::derived().getImpl(i,j); }}
118118
}
119119
}
120120
}
121121
else
122122
{
123-
Base::derived().decrementImpl(checkAliasing, alpha, dst);
123+
CRTP::derived().decrementImpl(checkAliasing, alpha, dst);
124124
}
125125
}
126126

include/FSLinalg/Matrix/MatrixProductChain_impl.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
#define FSLINALG_MATRIX_PRODUCT_CHAIN_IMPL_HPP
33

44
#include <FSLinalg/Matrix/MatrixProductChain.hpp>
5-
#include <FSLinalg/misc/StaticFor.hpp>
65

76
namespace FSLinalg
87
{
98

10-
template<std::array dims> template<size_t i, size_t j>
11-
constexpr std::pair<size_t, size_t> MatrixProductChain<dims>::minMulCostAndSplitRec(BIC::Fixed<size_t, i>, BIC::Fixed<size_t, j>)
9+
template<std::array dims> template<size_t I, size_t J>
10+
constexpr std::pair<size_t, size_t> MatrixProductChain<dims>::minMulCostAndSplitRec(BIC::Fixed<size_t, I> i, BIC::Fixed<size_t, J> j)
1211
{
1312
if constexpr (i + 1 == j)
1413
{
@@ -19,7 +18,7 @@ constexpr std::pair<size_t, size_t> MatrixProductChain<dims>::minMulCostAndSplit
1918
size_t minCost = std::numeric_limits<size_t>::max();
2019
size_t optSpliting = i + 1;
2120

22-
misc::StaticFor<i + 1, j>::run([&minCost, &optSpliting](const auto k) -> void
21+
BIC::foreach(BIC::next(i), j, [i, j, &minCost, &optSpliting](const auto k) -> void
2322
{
2423
constexpr size_t curr = minCostAndSplit<i, k>.first + minCostAndSplit<k, j>.first + dims[i]*dims[k]*dims[j];
2524
if (curr < minCost)

include/FSLinalg/Matrix/MatrixProduct_impl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ void MatrixProduct<Lhs,Rhs>::assignToHelper(const Bool checkAliasing, const Alph
2626
StripSymbolsAndEvalMatrix<Lhs> strippedLhs(m_lhs);
2727
StripSymbolsAndEvalMatrix<Rhs> strippedRhs(m_rhs);
2828

29-
const auto beta = alpha*strippedLhs.getAlpha()*strippedRhs.getAlpha();
29+
const auto beta = alpha*strippedLhs.getAlpha()*strippedRhs.getAlpha();
30+
3031
const StrippedLhs& A = strippedLhs.getMatrix();
3132
const StrippedRhs& B = strippedRhs.getMatrix();
3233

0 commit comments

Comments
 (0)