10 static_assert(std::is_same<T, float>::value || std::is_same<T, double>::value,
11 "Only float and double supported");
14 using GSLVectorType =
typename GSLTemplateTypeAlias<T>::VectorType;
15 using GSLVectorViewType =
typename GSLTemplateTypeAlias<T>::VectorViewType;
16 using GSLMatrixType =
typename GSLTemplateTypeAlias<T>::MatrixType;
17 using GSLMatrixViewType =
typename GSLTemplateTypeAlias<T>::MatrixViewType;
20 typename std::conditional<IsMatrix, GSLMatrixType, GSLVectorType>::type;
22 using ViewType =
typename std::conditional<
IsMatrix, GSLMatrixViewType,
30 static_assert(!
IsMatrix,
"Vector constructor used for matrix");
38 construct_vector_from_ptr(
ptr,
size);
43 static_assert(
IsMatrix,
"Matrix constructor used for vector");
124 if (!_freeMemory || !_ptr)
return;
135 GSLType* get() {
return _ptr; }
136 const GSLType* get()
const {
return _ptr; }
138 operator GSLType*() {
return _ptr; }
139 operator const GSLType*()
const {
return _ptr; }
142 template <
typename U>
143 void construct_vector_from_ptr(
const U*
ptr,
size_t size) {
144 if constexpr (std::is_same<U, T>::value) {
146 _ptr = &_view.vector;
151 for (
size_t i = 0;
i <
size; ++
i) _ptr->data[
i] =
static_cast<T>(
ptr[
i]);
155 template <
typename U>
156 void construct_vector_from_vector(
157 const typename GSLTemplateTypeAlias<U>::VectorType*
v) {
163 if constexpr (std::is_same<U, T>::value) {
164 _ptr =
const_cast<GSLType*
>(
v);
169 for (
size_t i = 0;
i <
v->size; ++
i)
170 _ptr->data[
i] =
static_cast<T>(
v->data[
i]);
174 template <
typename U>
175 void construct_matrix_from_vector(
176 const typename GSLTemplateTypeAlias<U>::VectorType*
v,
size_t rows,
184 throw std::runtime_error(
185 "Matrix construction from vector requires rows and cols");
188 throw std::runtime_error(
"Size mismatch in reshape");
190 if constexpr (std::is_same<T, U>::value) {
194 _ptr = &_view.matrix;
199 for (
size_t i = 0;
i <
v->size; ++
i)
200 _ptr->data[
i] =
static_cast<T>(
v->data[
i]);
204 template <
typename U>
205 void construct_matrix_from_ptr(
const U*
ptr,
size_t rows,
size_t cols) {
206 if constexpr (std::is_same<U, T>::value) {
208 _ptr = &_view.matrix;
214 for (
size_t i = 0;
i <
total; ++
i) _ptr->data[
i] =
static_cast<T>(
ptr[
i]);
218 template <
typename U>
219 void construct_matrix_from_matrix(
220 const typename GSLTemplateTypeAlias<U>::MatrixType*
m) {
226 if constexpr (std::is_same<U, T>::value) {
227 _ptr =
const_cast<GSLType*
>(
m);
232 size_t total =
m->size1 *
m->size2;
234 _ptr->data[
i] =
static_cast<T>(
m->data[
i]);
238 template <
typename U>
239 void construct_vector_from_matrix(
240 const typename GSLTemplateTypeAlias<U>::MatrixType*
m) {
246 if constexpr (std::is_same<T, U>::value) {
248 _ptr = &_view.vector;
250 const size_t total =
m->size1 *
m->size2;
255 _ptr->data[
i] =
static_cast<T>(
m->data[
i]);
259 template <
typename U>
260 static constexpr bool is_float_or_double() {
261 return std::is_same<U, float>::value || std::is_same<U, double>::value;
265 GSLType* _ptr =
nullptr;
266 bool _freeMemory =
false;