Salome HOME
93c7f65025f56d14cfcc0cc9fd97d935877a5963
[modules/med.git] / src / MEDCoupling / MEDCouplingMatrix.hxx
1 // Copyright (C) 2007-2015  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 "MEDCouplingAutoRefCountObjectPtr.hxx"
29
30 #include "InterpKernelException.hxx"
31
32 namespace ParaMEDMEM
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(int nbRows, int nbCols);
42     MEDCOUPLING_EXPORT static DenseMatrix *New(DataArrayDouble *array, int nbRows, int nbCols);
43     MEDCOUPLING_EXPORT DenseMatrix *deepCpy() const;
44     MEDCOUPLING_EXPORT DenseMatrix *shallowCpy() const;
45     MEDCOUPLING_EXPORT std::size_t getHeapMemorySizeWithoutChildren() const;
46     MEDCOUPLING_EXPORT std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
47     MEDCOUPLING_EXPORT void updateTime() const;
48     //
49     MEDCOUPLING_EXPORT int getNumberOfRows() const { return _nb_rows; }
50     MEDCOUPLING_EXPORT int getNumberOfCols() const { return _nb_cols; }
51     MEDCOUPLING_EXPORT int getNbOfElems() const { return _nb_rows*_nb_cols; }
52     MEDCOUPLING_EXPORT void reBuild(DataArrayDouble *array, int nbRows=-1, int nbCols=-1);
53     MEDCOUPLING_EXPORT void reShape(int nbRows, int nbCols);
54     MEDCOUPLING_EXPORT void transpose();
55     //
56     MEDCOUPLING_EXPORT bool isEqual(const DenseMatrix& other, double eps) const;
57     MEDCOUPLING_EXPORT bool isEqualIfNotWhy(const DenseMatrix& other, double eps, std::string& reason) const;
58     MEDCOUPLING_EXPORT DataArrayDouble *matVecMult(const DataArrayDouble *vec) const;
59     MEDCOUPLING_EXPORT static DataArrayDouble *MatVecMult(const DenseMatrix *mat, const DataArrayDouble *vec);
60     MEDCOUPLING_EXPORT static DenseMatrix *Add(const DenseMatrix *a1, const DenseMatrix *a2);
61     MEDCOUPLING_EXPORT void addEqual(const DenseMatrix *other);
62     MEDCOUPLING_EXPORT static DenseMatrix *Substract(const DenseMatrix *a1, const DenseMatrix *a2);
63     MEDCOUPLING_EXPORT void substractEqual(const DenseMatrix *other);
64     MEDCOUPLING_EXPORT static DenseMatrix *Multiply(const DenseMatrix *a1, const DenseMatrix *a2);
65     MEDCOUPLING_EXPORT static DenseMatrix *Multiply(const DenseMatrix *a1, const DataArrayDouble *a2);
66     //
67     MEDCOUPLING_EXPORT const DataArrayDouble *getData() const { return _data; }
68     MEDCOUPLING_EXPORT DataArrayDouble *getData() { return _data; }
69   private:
70     ~DenseMatrix();
71     DenseMatrix(int nbRows, int nbCols);
72     DenseMatrix(DataArrayDouble *array, int nbRows, int nbCols);
73     int getNumberOfRowsExt(int nbRows) const;
74     int getNumberOfColsExt(int nbCols) const;
75     void checkValidData() const;
76     static void CheckArraySizes(DataArrayDouble *array, int nbRows, int nbCols);
77     static void CheckSameSize(const DenseMatrix *a1, const DenseMatrix *a2);
78     static void CheckCompatibleSizeForMul(const DenseMatrix *a1, const DenseMatrix *a2);
79   private:
80     int _nb_rows;
81     int _nb_cols;
82     MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> _data;
83   };
84 }
85
86 #endif