Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
matrix.h
1/*
2** Copyright 2007-2024, RTE (https://www.rte-france.com)
3** See AUTHORS.txt
4** SPDX-License-Identifier: MPL-2.0
5** This file is part of Antares-Simulator,
6** Adequacy and Performance assessment for interconnected energy networks.
7**
8** Antares_Simulator is free software: you can redistribute it and/or modify
9** it under the terms of the Mozilla Public Licence 2.0 as published by
10** the Mozilla Foundation, either version 2 of the License, or
11** (at your option) any later version.
12**
13** Antares_Simulator is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** Mozilla Public Licence 2.0 for more details.
17**
18** You should have received a copy of the Mozilla Public Licence 2.0
19** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20*/
21#ifndef __ANTARES_LIBS_ARRAY_MATRIX_H__
22#define __ANTARES_LIBS_ARRAY_MATRIX_H__
23
24#include <cassert>
25#include <set>
26
27#include <yuni/yuni.h>
28#include <yuni/io/file.h>
29
30#include <antares/memory/memory.h>
31#include "antares/jit/jit.h"
32
33namespace Antares
34{
42template<class T = double, class ReadWriteT = T>
43class Matrix
44{
45public:
47 using Type = T;
49 using TypePtr = T*;
52
54 using ReadWriteType = ReadWriteT;
55
59 using Vector = std::set<MatrixPtr>;
60
62 using ColumnType = typename Antares::Memory::Stored<T>::Type;
63
65 using BufferType = Yuni::Clob;
66
87
88 enum
89 {
91 filesizeHardLimit = 1536 * 1024 * 1024, // 1.5Go
92 };
93
94public:
96
97
100 Matrix();
104 Matrix(const Matrix& rhs);
105
109 Matrix(Matrix&& rhs) noexcept;
110
114 template<class U, class V>
115 Matrix(const Matrix<U, V>& rhs);
116
120 Matrix(uint w, uint h);
122 virtual ~Matrix();
124
126
127
130 template<class U, class V>
131 void copyFrom(const Matrix<U, V>& rhs);
132
133 template<class U, class V>
134 void copyFrom(const Matrix<U, V>* rhs);
136
138
141 void swap(MatrixType& rhs) noexcept;
143
145
146
163 virtual bool loadFromCSVFile(const AnyString& filename,
164 uint minWidth,
165 uint maxHeight,
166 uint options = optNone,
167 BufferType* buffer = NULL);
168
169 bool loadFromCSVFile(const AnyString& filename,
170 uint minWidth,
171 uint maxHeight,
172 BufferType* buffer);
173
174 bool loadFromCSVFile(const AnyString& filename);
175
183 virtual bool openFile(Yuni::IO::File::Stream& file, const AnyString& filename) const;
184
185 virtual void saveBufferToFile(std::string& buffer, Yuni::IO::File::Stream& f) const;
186
197 bool saveToCSVFile(const AnyString& filename,
198 uint precision = 6,
199 bool print_dimensions = false,
200 bool saveEvenIfAllZero = false) const;
201
213 template<class PredicateT>
214 bool saveToCSVFile(const AnyString& filename,
215 uint precision,
216 bool print_dimensions,
217 PredicateT& predicate,
218 bool saveEvenIfAllZero = false) const;
219
221
222 virtual Yuni::IO::Error loadFromFileToBuffer(BufferType& buffer,
223 const AnyString& filename) const
224 {
225 return Yuni::IO::File::LoadFromFile(buffer, filename, filesizeHardLimit);
226 }
227
228 template<class PredicateT>
229 void saveToFileDescriptor(std::string& data,
230 uint precision,
231 bool print_dimensions,
232 PredicateT& predicate) const
233 {
234 saveToBuffer(data, precision, print_dimensions, predicate, false);
235 }
236
238
239
246 void resize(uint w, uint h, bool fixedSize = false);
247
251 void resizeWithoutDataLost(uint x, uint y, const T& defVal = T());
252
256 void clear();
257
261 void reset();
262
270 void reset(uint w, uint h, bool fixedSize = false);
271
273 ColumnType& column(uint n);
275 const ColumnType& column(uint n) const;
276
280 void zero();
281
285 void fill(const T& v);
286
290 void fillUnit();
291
295 template<class U>
296 void multiplyAllEntriesBy(const U& c);
297
301 template<class U>
302 void multiplyColumnBy(uint x, const U& c);
303 template<class U>
304 void divideColumnBy(uint x, const U& c);
305
309 void averageTimeseries(bool roundValues = true);
310
314 void roundAllEntries();
315
319 T findLowerBound() const;
320
324 T findUpperBound() const;
325
330
337 template<class U>
338 void pasteToColumn(uint x, const U* data);
339
346 void fillColumn(uint x, const T& value);
347
353 void columnToZero(uint x);
354
358 bool containsOnlyZero() const;
359
365 template<class PredicateT>
366 bool containsOnlyZero(PredicateT& predicate) const;
367
371 void circularShiftRows(uint count);
372
376 void circularShiftRows(uint column, uint count);
378
380
381
390 bool forceReload(bool reload = false) const;
391
398 void unloadFromMemory() const;
399
405 void markAsModified() const;
406
416 bool empty() const;
417
421 void print() const;
422
424
425
426 Matrix& operator=(const Matrix& rhs);
427
428 Matrix& operator=(Matrix&& rhs) noexcept;
429
431 template<class U>
433
436 const ColumnType& operator[](uint column) const;
438
439public:
441 mutable uint width;
443 mutable uint height;
448
450 {
451 template<class U = Type>
452 inline U operator()(const U& value) const
453 {
454 return value;
455 }
456 };
457
458 void saveToBuffer(std::string& data, uint precision = 6) const;
459
460 template<class PredicateT>
461 void saveToBuffer(std::string& data,
462 uint precision,
463 bool print_dimensions,
464 PredicateT& predicate,
465 bool saveEvenIfAllZero) const;
466
467private:
471 bool internalLoadCSVFile(const AnyString& filename,
472 uint minWidth,
473 uint maxHeight,
474 uint options,
475 BufferType* buffer = NULL);
476
478 bool internalLoadJITData(const AnyString& filename,
479 uint minWidth,
480 uint maxHeight,
481 uint options);
482
486 template<class PredicateT>
487 bool internalSaveCSVFile(const AnyString& filename,
488 uint precision,
489 bool print_dimensions,
490 PredicateT& predicate,
491 bool saveEvenIfAllZero) const;
492
493 bool loadFromBuffer(const AnyString& filename,
494 BufferType& data,
495 uint minWidth,
496 uint maxHeight,
497 const int fixedSize,
498 uint options);
502 bool loadAllJITData() const;
503
507 void reverseRows(uint column, uint start, uint end);
508
509}; // class Matrix
510
511template<class T>
513{
514 // using Type = <sub column> ;
515 // using Type = const <sub column> ;
516};
517
518template<class U>
520{
521public:
522 using Type = U*;
523 using ConstType = const U*;
524};
525
526template<>
527class MatrixSubColumn<Matrix<double>::ColumnType*>
528{
529public:
531 using Type = MatrixType::ColumnType&;
532 using ConstType = const MatrixType::ColumnType&;
533};
534
535template<>
536class MatrixSubColumn<Matrix<float>::ColumnType*>
537{
538public:
540 using Type = MatrixType::ColumnType&;
541 using ConstType = const MatrixType::ColumnType&;
542};
543
550int MatrixTestForPositiveValues(const char* msg, const Matrix<>* m);
551
559int MatrixTestIfValuesAreHigherThan(const char* msg, const Matrix<>* m, const double value);
560
568int MatrixTestIfValuesAreLowerThan(const char* msg, const Matrix<>* m, const double value);
569
576int MatrixTestForNegativeValues(const char* msg, const Matrix<>* m);
577
584int MatrixTestForPositiveValues_LimitWidth(const char* msg, const Matrix<>* m, uint maxWidth);
585
592template<class T1, class T2>
593bool MatrixTestForAtLeastOnePositiveValue(const Matrix<T1, T2>& m);
594
595} // namespace Antares
596
597#include "matrix.hxx"
598
599#endif // __ANTARES_LIBS_ARRAY_MATRIX_H__
Definition matrix.h:513
A n-by-n matrix.
Definition jit.h:30
JIT::Informations * jit
Just-in-time informations.
Definition matrix.h:447
bool saveToCSVFile(const AnyString &filename, uint precision=6, bool print_dimensions=false, bool saveEvenIfAllZero=false) const
Write the content of a matrix into a single file.
Definition matrix.hxx:446
@ filesizeHardLimit
A Hard-coded maximum filesize.
Definition matrix.h:91
Options
Options when loading a file.
Definition matrix.h:71
@ optImmediate
Do not postpone the loading.
Definition matrix.h:79
@ optQuiet
Do not produce warnings/errors.
Definition matrix.h:77
@ optNeverFails
The loading never fails.
Definition matrix.h:85
@ optFixedSize
The matrix can not see its size modified.
Definition matrix.h:75
@ optNoWarnIfEmpty
Do not warn if the file is empty.
Definition matrix.h:83
@ optMarkAsModified
mark the matrix as modified after loading
Definition matrix.h:81
@ optNone
None.
Definition matrix.h:73
void swap(MatrixType &rhs) noexcept
Swap contents of Matrix with another.
Definition matrix.hxx:1569
void multiplyColumnBy(uint x, const U &c)
Multiply or divide a column by a given value.
Definition matrix.hxx:1418
void fillUnit()
Make the matrix an unit matrix (identity matrix)
Definition matrix.hxx:362
virtual ~Matrix()
Destructor.
Definition matrix.hxx:276
void reset()
Empty the matrix and mark it as modified.
Definition matrix.hxx:548
ColumnType & operator[](uint column)
operator []
Definition matrix.hxx:1746
T Type
Type.
Definition matrix.h:47
void unloadFromMemory() const
Try to remove from memory all data from the matrix.
Definition matrix.hxx:1644
void fill(const T &v)
Fill the matrix with a given value.
Definition matrix.hxx:348
Matrix & operator=(const Matrix &rhs)
Assignement.
Definition matrix.hxx:1580
bool forceReload(bool reload=false) const
Force the Load of data (if not done) for the next save and mark the matrix as modified.
Definition matrix.hxx:375
Matrix & operator=(const Matrix< U > &rhs)
Assignement.
T findLowerBound() const
Find the lower bound.
Definition matrix.hxx:1468
void pasteToColumn(uint x, const U *data)
Copy values into a given column in the matrix.
Definition matrix.hxx:468
typename Antares::Memory::Stored< T >::Type ColumnType
Column type.
Definition matrix.h:62
T * TypePtr
Pointer.
Definition matrix.h:49
ColumnType * entry
All entries of the matrix (bidimensional array)
Definition matrix.h:445
T findUpperBound() const
Find the upper bound.
Definition matrix.hxx:1486
void print() const
Print the matrix to std::cout (debug)
Definition matrix.hxx:1616
bool empty() const
Get if the matrix is empty.
Definition matrix.hxx:526
ReadWriteT ReadWriteType
Read / Write type.
Definition matrix.h:54
Matrix< T, ReadWriteT > MatrixType
Matrix type.
Definition matrix.h:51
void zero()
Make the matrix a zero matrix.
Definition matrix.hxx:293
uint width
Width of the matrix.
Definition matrix.h:441
void markAsModified() const
Mark the matrix as modified.
Definition matrix.hxx:517
ColumnType & column(uint n)
Get the Nth column.
Definition matrix.hxx:1762
uint height
Height of the matrix.
Definition matrix.h:443
void columnToZero(uint x)
Set to zero a entire column.
Definition matrix.hxx:506
virtual bool loadFromCSVFile(const AnyString &filename, uint minWidth, uint maxHeight, uint options=optNone, BufferType *buffer=NULL)
Load entries from a CSV file.
Definition matrix.hxx:425
void fillColumn(uint x, const T &value)
Set a entire column with a given value.
Definition matrix.hxx:492
void copyFrom(const Matrix< U, V > &rhs)
Copy values from another matrix.
Definition matrix.hxx:1505
void makeAllEntriesAbsolute()
Make all entries absolute.
Definition matrix.hxx:1455
bool containsOnlyZero() const
Get if the matrix only contains zero.
Definition matrix.hxx:1150
void circularShiftRows(uint count)
Shift all rows.
Definition matrix.hxx:1681
std::set< MatrixPtr > Vector
Vector.
Definition matrix.h:59
void resizeWithoutDataLost(uint x, uint y, const T &defVal=T())
Resize the matrix without destroying its content.
Definition matrix.hxx:1316
void averageTimeseries(bool roundValues=true)
Compute the average of all timeseries (derated mode)
Definition matrix.hxx:303
Matrix()
Default Constructor.
Definition matrix.hxx:202
Yuni::Clob BufferType
A buffer, for large amount of data.
Definition matrix.h:65
void resize(uint w, uint h, bool fixedSize=false)
Resize the matrix.
Definition matrix.hxx:555
void multiplyAllEntriesBy(const U &c)
Multiply all entries by a given value.
Definition matrix.hxx:1391
void clear()
Empty the matrix.
Definition matrix.hxx:532
void roundAllEntries()
Round all entries.
Definition matrix.hxx:1442
virtual bool openFile(Yuni::IO::File::Stream &file, const AnyString &filename) const
Trying to open a file.
Definition matrix.hxx:1239
Definition jit.h:118
Definition matrix.h:450