Salome HOME
Copyright update 2021
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingMatrix.hxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay
20
21 #ifndef __PARAMEDMEM_MEDCOUPLINGMATRIX_HXX__
22 #define __PARAMEDMEM_MEDCOUPLINGMATRIX_HXX__
23
24 #include "MEDCoupling.hxx"
25 #include "MEDCouplingTimeLabel.hxx"
26 #include "MEDCouplingRefCountObject.hxx"
27 #include "MEDCouplingMemArray.hxx"
28 #include "MCAuto.hxx"
29
30 #include "InterpKernelException.hxx"
31
32 namespace MEDCoupling
33 {
34   /*!
35    * The aim of this class is \b NOT to reimplement all linear algebra but only to store a dense matrix.
36    * It only provides basic set/get and basic operations and bindings to linear algebra libraries (numpy/scipy) and a compatible format to Petsc.
37    */
38   class DenseMatrix : public RefCountObject, public TimeLabel
39   {
40   public:
41     MEDCOUPLING_EXPORT static DenseMatrix *New(mcIdType nbRows, mcIdType nbCols);
42     MEDCOUPLING_EXPORT static DenseMatrix *New(DataArrayDouble *array, mcIdType nbRows, mcIdType nbCols);
43     MEDCOUPLING_EXPORT std::string getClassName() const override { return std::string("DenseMatrix"); }
44     MEDCOUPLING_EXPORT DenseMatrix *deepCopy() const;
45     MEDCOUPLING_EXPORT DenseMatrix *shallowCpy() const;
46     MEDCOUPLING_EXPORT std::size_t getHeapMemorySizeWithoutChildren() const;
47     MEDCOUPLING_EXPORT std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
48     MEDCOUPLING_EXPORT void updateTime() const;
49     //
50     MEDCOUPLING_EXPORT mcIdType getNumberOfRows() const { return _nb_rows; }
51     MEDCOUPLING_EXPORT mcIdType getNumberOfCols() const { return _nb_cols; }
52     MEDCOUPLING_EXPORT mcIdType getNbOfElems() const { return _nb_rows*_nb_cols; }
53     MEDCOUPLING_EXPORT void reBuild(DataArrayDouble *array, mcIdType nbRows=-1, mcIdType nbCols=-1);
54     MEDCOUPLING_EXPORT void reShape(mcIdType nbRows, mcIdType nbCols);
55     MEDCOUPLING_EXPORT void transpose();
56     //
57     MEDCOUPLING_EXPORT bool isEqual(const DenseMatrix& other, double eps) const;
58     MEDCOUPLING_EXPORT bool isEqualIfNotWhy(const DenseMatrix& other, double eps, std::string& reason) const;
59     MEDCOUPLING_EXPORT DataArrayDouble *matVecMult(const DataArrayDouble *vec) const;
60     MEDCOUPLING_EXPORT static DataArrayDouble *MatVecMult(const DenseMatrix *mat, const DataArrayDouble *vec);
61     MEDCOUPLING_EXPORT static DenseMatrix *Add(const DenseMatrix *a1, const DenseMatrix *a2);
62     MEDCOUPLING_EXPORT void addEqual(const DenseMatrix *other);
63     MEDCOUPLING_EXPORT static DenseMatrix *Substract(const DenseMatrix *a1, const DenseMatrix *a2);
64     MEDCOUPLING_EXPORT void substractEqual(const DenseMatrix *other);
65     MEDCOUPLING_EXPORT static DenseMatrix *Multiply(const DenseMatrix *a1, const DenseMatrix *a2);
66     MEDCOUPLING_EXPORT static DenseMatrix *Multiply(const DenseMatrix *a1, const DataArrayDouble *a2);
67     //
68     MEDCOUPLING_EXPORT const DataArrayDouble *getData() const { return _data; }
69     MEDCOUPLING_EXPORT DataArrayDouble *getData() { return _data; }
70   private:
71     ~DenseMatrix();
72     DenseMatrix(mcIdType nbRows, mcIdType nbCols);
73     DenseMatrix(DataArrayDouble *array, mcIdType nbRows, mcIdType nbCols);
74     mcIdType getNumberOfRowsExt(mcIdType nbRows) const;
75     mcIdType getNumberOfColsExt(mcIdType nbCols) const;
76     void checkValidData() const;
77     static void CheckArraySizes(DataArrayDouble *array, mcIdType nbRows, mcIdType nbCols);
78     static void CheckSameSize(const DenseMatrix *a1, const DenseMatrix *a2);
79     static void CheckCompatibleSizeForMul(const DenseMatrix *a1, const DenseMatrix *a2);
80   private:
81     mcIdType _nb_rows;
82     mcIdType _nb_cols;
83     MCAuto<DataArrayDouble> _data;
84   };
85 }
86
87 #endif