From eda6ac6ae3d7cfb31537e80796fec585773d2f85 Mon Sep 17 00:00:00 2001
From: hhakim <hakim.hadj-djilani@inria.fr>
Date: Tue, 6 Dec 2022 23:19:37 +0100
Subject: [PATCH] Add MatDense<FPP, Cpu>::setRand.

---
 .../src/C++/faust_matdense_conjugate.cpp.in   | 26 +++++++++----------
 .../CPU/faust_MatDense.h                      |  4 +++
 .../CPU/faust_MatDense.hpp                    |  9 +++++++
 3 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/misc/test/src/C++/faust_matdense_conjugate.cpp.in b/misc/test/src/C++/faust_matdense_conjugate.cpp.in
index b25236551..10e4e7454 100644
--- a/misc/test/src/C++/faust_matdense_conjugate.cpp.in
+++ b/misc/test/src/C++/faust_matdense_conjugate.cpp.in
@@ -15,29 +15,29 @@ int main(int argc, char* argv[])
 	faust_unsigned_int dim1 = 3;
 	faust_unsigned_int dim2 = 4;
 
-	MatDense<FPP,Cpu>* M;
+	MatDense<FPP,Cpu> M(dim1, dim2);
 	MatDense<FPP,Cpu> M_copy;
-	M = MatDense<FPP,Cpu>::randMat(dim1,dim2);
-	M_copy = *M;
+	M.setRand();
+	M_copy = M;
 	cout<<"mat value"<<endl;
-	M->Display();
+	M.Display();
 
-	M->conjugate(false);
+	M.conjugate(false);
 	cout<<"conjugate mat value (without eval)"<<endl;
-	M->Display();
+	M.Display();
 
-	M->transpose();
+	M.transpose();
 	cout<<"conjugate-transpose mat value (with eval)"<<endl;
-	M->Display();
+	M.Display();
 
-	M->conjugate();
+	M.conjugate();
 	cout << "transpose mat value from conjugate-transpose (with eval)" << endl;
-	M->Display();
+	M.Display();
 
-	M->transpose();
-	assert(M->isEqual(M_copy));
+	M.transpose();
+	assert(M.isEqual(M_copy));
 	cout << "mat value:" << endl;
-	M->Display();
+	M.Display();
 
 	return 0;
 }
diff --git a/src/faust_linear_operator/CPU/faust_MatDense.h b/src/faust_linear_operator/CPU/faust_MatDense.h
index 26cb17129..7b66fb23b 100644
--- a/src/faust_linear_operator/CPU/faust_MatDense.h
+++ b/src/faust_linear_operator/CPU/faust_MatDense.h
@@ -264,6 +264,10 @@ namespace Faust
 			// \brief Sets all nonzeros to one.
 			void setNZtoOne();
 
+			// \brief Sets the matrix to random values.
+			// \note using this function is preferable instead of using randMat functions.
+			void setRand();
+
 			//! \brief Returns the identity matrix.
 			static MatDense<FPP,Cpu> eye(faust_unsigned_int nrows, faust_unsigned_int ncols);
 
diff --git a/src/faust_linear_operator/CPU/faust_MatDense.hpp b/src/faust_linear_operator/CPU/faust_MatDense.hpp
index e86b87dea..81e294d06 100644
--- a/src/faust_linear_operator/CPU/faust_MatDense.hpp
+++ b/src/faust_linear_operator/CPU/faust_MatDense.hpp
@@ -261,6 +261,15 @@ namespace Faust
 			isZeros = false;
 		}
 
+
+	template<typename FPP>
+		void MatDense<FPP,Cpu>::setRand()
+		{
+			mat = EigDenseMat::Random(mat.rows(), mat.cols());
+			isZeros = false;
+			this->is_identity = false;
+		}
+
 	template<typename FPP>
 		void MatDense<FPP, Cpu>::setNZtoOne()
 		{
-- 
GitLab