1 /*
2   HMat-OSS (HMatrix library, open source software)
3 
4   Copyright (C) 2014-2015 Airbus Group SAS
5 
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License
8   as published by the Free Software Foundation; either version 2
9   of the License, or (at your option) any later version.
10 
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 
20   http://github.com/jeromerobert/hmat-oss
21 */
22 
23 #include "fromdouble.hpp"
24 #include <assert.h>
25 
26 namespace hmat {
27 
28 template<>
fromDoubleScalarArray(ScalarArray<D_t> * d,bool)29 ScalarArray<D_t>* fromDoubleScalarArray(ScalarArray<D_t>* d, bool) {
30   return d;
31 }
32 template<>
fromDoubleScalarArray(ScalarArray<Z_t> * d,bool)33 ScalarArray<Z_t>* fromDoubleScalarArray(ScalarArray<Z_t>* d, bool) {
34   return d;
35 }
36 
37 template<typename T>
fromDoubleScalarArray(ScalarArray<typename Types<T>::dp> * d,bool del)38 ScalarArray<T>* fromDoubleScalarArray(ScalarArray<typename Types<T>::dp>* d, bool del) {
39   if (!d) {
40     return NULL;
41   }
42   ScalarArray<T>* result = new ScalarArray<T>(d->rows, d->cols);
43   assert(result);
44   for (int j = 0; j < d->cols; ++j)
45     for (int i = 0; i < d->rows; ++i)
46       result->get(i, j) = T(d->get(i, j));
47   result->setOrtho(d->getOrtho());
48   if (del)
49     delete d;
50   return result;
51 }
52 
53 template ScalarArray<S_t>* fromDoubleScalarArray(ScalarArray<Types<S_t>::dp>* d, bool);
54 template ScalarArray<C_t>* fromDoubleScalarArray(ScalarArray<Types<C_t>::dp>* d, bool);
55 
56 template<>
fromDoubleFull(FullMatrix<D_t> * f)57 FullMatrix<D_t>* fromDoubleFull(FullMatrix<D_t>* f) {
58   return f;
59 }
60 template<>
fromDoubleFull(FullMatrix<Z_t> * f)61 FullMatrix<Z_t>* fromDoubleFull(FullMatrix<Z_t>* f) {
62   return f;
63 }
64 
65 template<typename T>
fromDoubleFull(FullMatrix<typename Types<T>::dp> * f)66 FullMatrix<T>* fromDoubleFull(FullMatrix<typename Types<T>::dp>* f) {
67   if (!f)
68     return NULL;
69   FullMatrix<T>* result = new FullMatrix<T>(f->rows_, f->cols_);
70   for (int j = 0; j < f->cols(); ++j)
71     for (int i = 0; i < f->rows(); ++i)
72       result->get(i, j) = T(f->get(i, j));
73   result->data.setOrtho(f->data.getOrtho());
74   delete f;
75   return result;
76 }
77 
78 template FullMatrix<S_t>* fromDoubleFull(FullMatrix<Types<S_t>::dp>* f);
79 template FullMatrix<C_t>* fromDoubleFull(FullMatrix<Types<C_t>::dp>* f);
80 
fromDoubleRk(RkMatrix<D_t> * rk)81 template<> RkMatrix<D_t>* fromDoubleRk(RkMatrix<D_t>* rk) {
82   return rk;
83 }
fromDoubleRk(RkMatrix<Z_t> * rk)84 template<> RkMatrix<Z_t>* fromDoubleRk(RkMatrix<Z_t>* rk) {
85   return rk;
86 }
87 
fromDoubleRk(RkMatrix<typename Types<T>::dp> * rk)88 template<typename T> RkMatrix<T>* fromDoubleRk(RkMatrix<typename Types<T>::dp>* rk) {
89   RkMatrix<T>* result = new RkMatrix<T>(fromDoubleScalarArray<T>(rk->a),
90                                         rk->rows,
91                                         fromDoubleScalarArray<T>(rk->b),
92                                         rk->cols);
93   rk->a = NULL; // because rk->a and rk->b have allready been deleted in fromDoubleScalarArray
94   rk->b = NULL;
95   delete rk;
96   return result;
97 }
98 
99 template RkMatrix<S_t>* fromDoubleRk(RkMatrix<Types<S_t>::dp>* rk);
100 template RkMatrix<C_t>* fromDoubleRk(RkMatrix<Types<C_t>::dp>* rk);
101 
102 }  // end namespace hmat
103