vector and matrix contructors from arrays of arbirary size via nested templates

This commit is contained in:
Jiri Pittner 2021-10-27 23:24:41 +02:00
parent e0b4738b17
commit b7d3a5d977
3 changed files with 81 additions and 1 deletions

40
mat.h
View File

@ -83,6 +83,9 @@ public:
//! inlined constructor creating matrix of given size filled with data located at given memory location
NRMat(const T *a, const int n, const int m);
//! inlined constructor creating matrix of given size from an array
template<int R, int C> inline NRMat(const T (&a)[R][C]);
//! inlined copy-constructor
inline NRMat(const NRMat &rhs);
@ -490,6 +493,43 @@ NRMat<T>::NRMat(const T &a, const int n, const int m, const GPUID loc) : nn(n),
#endif
}
/***************************************************************************//**
* inline constructor creating vector from an array
******************************************************************************/
template <typename T> template<int R, int C>
inline NRMat<T>::NRMat(const T (&a)[R][C]) : count(new int) {
nn = R;
mm = C;
*count = 1;
#ifdef CUDALA
if(location==cpu){
#endif
#ifdef MATPTR
v = new T*[nn];
v[0] = new T[nn*mm];
for (register int i=1; i<n; i++) v[i] = v[i-1] + m;
if(LA_traits<T>::is_plaindata()) memcpy(v[0], a, nn*mm*sizeof(T));
else for( int i=0; i<nn; i++) for(int j=0; j<mm; ++j) v[i][j]=a[i][j];
#else
v = new T[nn*mm];
if(LA_traits<T>::is_plaindata()) memcpy(v, a, nn*mm*sizeof(T));
else for( int i=0; i<nn; i++) for(int j=0; j<mm; ++j) v[i*mm+j] = a[i][j];
#endif
#ifdef CUDALA
}else{
if!LA_traits<T>::is_plaindata()) laerror("only implemented for plain data");
v = (T*) gpualloc(nn*mm*sizeof(T));
cublasSetVector(nm, sizeof(T), a, 1, v, 1);
}
#endif
}
/***************************************************************************//**
* matrix constructor
* @param[in] a value of type T intended for matrix inicialization

10
t.cc
View File

@ -2284,7 +2284,7 @@ Polynomial<double> qq=q.pow(n);
cout <<"test binom "<<(p-qq).norm()<<endl;
}
if(1)
if(0)
{
int n;
cin >>n ;
@ -2294,5 +2294,13 @@ cout <<"non-zero = "<< ((n&1)?p.odd_powers():p.even_powers())<<std::endl;
cout <<"value = "<<value(p,1.23456789)<<" "<<odd_value(p,1.23456789)+even_value(p,1.23456789)<<endl;
}
if(1)
{
NRVec<int> v({1,2,3,4});
cout <<v;
NRMat<int> m({{1,2,3},{4,5,6}});
cout<<m;
}
}

32
vec.h
View File

@ -127,6 +127,9 @@ public:
//! inlined constructor creating vector of given size filled with prescribed value
inline NRVec(const T &a, const int n);
//! inlined constructor creating vector froman array
template<int SIZE> inline NRVec(const T (&a)[SIZE]);
//! inlined constructor creating vector of given size filled with data located at given memory location
inline NRVec(const T *a, const int n);
@ -580,6 +583,35 @@ inline NRVec<T>::NRVec(const T& a, const int n): nn(n), count(new int) {
#endif
}
/***************************************************************************//**
* inline constructor creating vector from an array
******************************************************************************/
template <typename T> template<int SIZE>
inline NRVec<T>::NRVec(const T (&a)[SIZE]) : count(new int) {
nn = SIZE;
*count = 1;
#ifdef CUDALA
location = DEFAULT_LOC;
if(location == cpu){
#endif
v = new T[nn];
if(LA_traits<T>::is_plaindata()) memcpy(v, a, nn*sizeof(T));
else for( int i=0; i<nn; i++) v[i] = a[i];
#ifdef CUDALA
}else{
v = (T*) gpualloc(nn*sizeof(T));
if!LA_traits<T>::is_plaindata()) laerror("only implemented for plain data");
cublasSetVector(nn, sizeof(T), a, 1, v, 1);
TEST_CUBLAS("cublasSetVector");
}
#endif
}
/***************************************************************************//**
* inline constructor creating vector of given size filled with given data