467 |
17 Dec 05 |
peter |
// $Id$ |
467 |
17 Dec 05 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
831 |
27 Mar 07 |
peter |
Copyright (C) 2005 Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2012 Peter Johansson |
536 |
03 Mar 06 |
peter |
9 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
11 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
16 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
21 |
|
675 |
10 Oct 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
675 |
10 Oct 06 |
jari |
24 |
*/ |
675 |
10 Oct 06 |
jari |
25 |
|
2881 |
18 Nov 12 |
peter |
26 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
27 |
|
680 |
11 Oct 06 |
jari |
28 |
#include "DataLookup1D.h" |
680 |
11 Oct 06 |
jari |
29 |
#include "MatrixLookup.h" |
675 |
10 Oct 06 |
jari |
30 |
|
1121 |
22 Feb 08 |
peter |
31 |
#include "yat/utility/Matrix.h" |
1120 |
21 Feb 08 |
peter |
32 |
#include "yat/utility/Vector.h" |
766 |
22 Feb 07 |
peter |
33 |
|
536 |
03 Mar 06 |
peter |
34 |
#include <cassert> |
473 |
22 Dec 05 |
peter |
35 |
#include <iostream> |
593 |
25 Aug 06 |
markus |
36 |
#include <iomanip> |
1920 |
24 Apr 09 |
peter |
37 |
#include <limits> |
467 |
17 Dec 05 |
peter |
38 |
|
467 |
17 Dec 05 |
peter |
39 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
40 |
namespace yat { |
467 |
17 Dec 05 |
peter |
41 |
namespace classifier { |
467 |
17 Dec 05 |
peter |
42 |
|
4200 |
19 Aug 22 |
peter |
43 |
DataLookup1D::DataLookup1D(const MatrixLookup& m, const size_t i, |
536 |
03 Mar 06 |
peter |
44 |
const bool row_vector) |
537 |
05 Mar 06 |
peter |
45 |
: column_vector_(!row_vector), index_(i), matrix_(&m), owner_(false) |
467 |
17 Dec 05 |
peter |
46 |
{ |
536 |
03 Mar 06 |
peter |
47 |
assert( !column_vector_ || i<m.columns()); |
536 |
03 Mar 06 |
peter |
48 |
assert( column_vector_ || i<m.rows()); |
467 |
17 Dec 05 |
peter |
49 |
} |
720 |
26 Dec 06 |
jari |
50 |
|
4200 |
19 Aug 22 |
peter |
51 |
|
537 |
05 Mar 06 |
peter |
52 |
DataLookup1D::DataLookup1D(const size_t size, const double value) |
537 |
05 Mar 06 |
peter |
53 |
: column_vector_(false), index_(0), owner_(true) |
537 |
05 Mar 06 |
peter |
54 |
{ |
537 |
05 Mar 06 |
peter |
55 |
matrix_ = new MatrixLookup(1,size,value); |
537 |
05 Mar 06 |
peter |
56 |
} |
537 |
05 Mar 06 |
peter |
57 |
|
720 |
26 Dec 06 |
jari |
58 |
|
537 |
05 Mar 06 |
peter |
59 |
DataLookup1D::DataLookup1D(const DataLookup1D& other) |
537 |
05 Mar 06 |
peter |
60 |
: column_vector_(other.column_vector_), index_(other.index_), |
537 |
05 Mar 06 |
peter |
61 |
matrix_(other.matrix_), owner_(false) |
537 |
05 Mar 06 |
peter |
62 |
{ |
537 |
05 Mar 06 |
peter |
63 |
} |
537 |
05 Mar 06 |
peter |
64 |
|
720 |
26 Dec 06 |
jari |
65 |
|
4200 |
19 Aug 22 |
peter |
66 |
DataLookup1D::DataLookup1D(const utility::VectorBase& v, |
766 |
22 Feb 07 |
peter |
67 |
const std::vector<size_t>& index) |
766 |
22 Feb 07 |
peter |
68 |
: column_vector_(true), index_(0), owner_(true) |
766 |
22 Feb 07 |
peter |
69 |
{ |
1121 |
22 Feb 08 |
peter |
70 |
utility::Matrix* m = new utility::Matrix(1,index.size()); |
766 |
22 Feb 07 |
peter |
71 |
for (size_t i=0; i<index.size(); ++i){ |
766 |
22 Feb 07 |
peter |
72 |
assert(index[i]<v.size()); |
766 |
22 Feb 07 |
peter |
73 |
(*m)(0,i)=v(index[i]); |
766 |
22 Feb 07 |
peter |
74 |
} |
766 |
22 Feb 07 |
peter |
75 |
matrix_ = new MatrixLookup(*m, true); |
766 |
22 Feb 07 |
peter |
76 |
} |
766 |
22 Feb 07 |
peter |
77 |
|
766 |
22 Feb 07 |
peter |
78 |
|
1018 |
01 Feb 08 |
peter |
79 |
DataLookup1D::DataLookup1D(const utility::VectorBase& v) |
820 |
17 Mar 07 |
peter |
80 |
: column_vector_(false), index_(0), owner_(true) |
815 |
17 Mar 07 |
peter |
81 |
{ |
1121 |
22 Feb 08 |
peter |
82 |
utility::Matrix* m = new utility::Matrix(1,v.size()); |
815 |
17 Mar 07 |
peter |
83 |
for (size_t i=0; i<v.size(); ++i){ |
815 |
17 Mar 07 |
peter |
84 |
(*m)(0,i)=v(i); |
815 |
17 Mar 07 |
peter |
85 |
} |
815 |
17 Mar 07 |
peter |
86 |
matrix_ = new MatrixLookup(*m, true); |
815 |
17 Mar 07 |
peter |
87 |
} |
815 |
17 Mar 07 |
peter |
88 |
|
815 |
17 Mar 07 |
peter |
89 |
|
473 |
22 Dec 05 |
peter |
90 |
DataLookup1D::~DataLookup1D() |
473 |
22 Dec 05 |
peter |
91 |
{ |
537 |
05 Mar 06 |
peter |
92 |
if (owner_) |
537 |
05 Mar 06 |
peter |
93 |
delete matrix_; |
473 |
22 Dec 05 |
peter |
94 |
} |
467 |
17 Dec 05 |
peter |
95 |
|
593 |
25 Aug 06 |
markus |
96 |
|
880 |
21 Sep 07 |
peter |
97 |
DataLookup1D::const_iterator DataLookup1D::begin(void) const |
880 |
21 Sep 07 |
peter |
98 |
{ |
1062 |
10 Feb 08 |
peter |
99 |
if (column_vector_) |
1525 |
24 Sep 08 |
peter |
100 |
return matrix_->begin_column(index_); |
1525 |
24 Sep 08 |
peter |
101 |
return matrix_->begin_row(index_); |
880 |
21 Sep 07 |
peter |
102 |
} |
880 |
21 Sep 07 |
peter |
103 |
|
880 |
21 Sep 07 |
peter |
104 |
|
880 |
21 Sep 07 |
peter |
105 |
DataLookup1D::const_iterator DataLookup1D::end(void) const |
880 |
21 Sep 07 |
peter |
106 |
{ |
1062 |
10 Feb 08 |
peter |
107 |
if (column_vector_) |
1525 |
24 Sep 08 |
peter |
108 |
return matrix_->end_column(index_); |
1525 |
24 Sep 08 |
peter |
109 |
return matrix_->end_row(index_); |
880 |
21 Sep 07 |
peter |
110 |
} |
880 |
21 Sep 07 |
peter |
111 |
|
880 |
21 Sep 07 |
peter |
112 |
|
720 |
26 Dec 06 |
jari |
113 |
size_t DataLookup1D::size(void) const |
720 |
26 Dec 06 |
jari |
114 |
{ |
720 |
26 Dec 06 |
jari |
115 |
return column_vector_ ? matrix_->rows() : matrix_->columns(); |
720 |
26 Dec 06 |
jari |
116 |
} |
720 |
26 Dec 06 |
jari |
117 |
|
720 |
26 Dec 06 |
jari |
118 |
|
720 |
26 Dec 06 |
jari |
119 |
double DataLookup1D::operator()(const size_t i) const |
4200 |
19 Aug 22 |
peter |
120 |
{ |
720 |
26 Dec 06 |
jari |
121 |
assert(i<size()); |
720 |
26 Dec 06 |
jari |
122 |
return column_vector_ ? (*matrix_)(i,index_) : (*matrix_)(index_,i); |
720 |
26 Dec 06 |
jari |
123 |
} |
720 |
26 Dec 06 |
jari |
124 |
|
720 |
26 Dec 06 |
jari |
125 |
|
527 |
01 Mar 06 |
peter |
126 |
double DataLookup1D::operator*(const DataLookup1D& other) const |
527 |
01 Mar 06 |
peter |
127 |
{ |
527 |
01 Mar 06 |
peter |
128 |
assert(other.size()==size()); |
527 |
01 Mar 06 |
peter |
129 |
double res=0; |
527 |
01 Mar 06 |
peter |
130 |
for (size_t i = 0; i<size(); i++) |
527 |
01 Mar 06 |
peter |
131 |
res += (*this)(i)*other(i); |
527 |
01 Mar 06 |
peter |
132 |
return res; |
527 |
01 Mar 06 |
peter |
133 |
} |
527 |
01 Mar 06 |
peter |
134 |
|
593 |
25 Aug 06 |
markus |
135 |
|
593 |
25 Aug 06 |
markus |
136 |
std::ostream& operator<<(std::ostream& os, const DataLookup1D& x) |
527 |
01 Mar 06 |
peter |
137 |
{ |
527 |
01 Mar 06 |
peter |
138 |
os.setf(std::ios::dec); |
1919 |
24 Apr 09 |
peter |
139 |
std::streamsize precision = os.precision(); |
1920 |
24 Apr 09 |
peter |
140 |
os.precision(std::numeric_limits<double>().digits10); |
4200 |
19 Aug 22 |
peter |
141 |
|
593 |
25 Aug 06 |
markus |
142 |
for (size_t i=0; i<x.size(); ++i) { |
527 |
01 Mar 06 |
peter |
143 |
os << x(i); |
593 |
25 Aug 06 |
markus |
144 |
if ((i+1)<x.size()) |
593 |
25 Aug 06 |
markus |
145 |
os << os.fill(); |
527 |
01 Mar 06 |
peter |
146 |
} |
1919 |
24 Apr 09 |
peter |
147 |
os.precision(precision); |
527 |
01 Mar 06 |
peter |
148 |
return os; |
527 |
01 Mar 06 |
peter |
149 |
} |
593 |
25 Aug 06 |
markus |
150 |
|
680 |
11 Oct 06 |
jari |
151 |
}}} // of namespace classifier, yat, and theplu |