-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublevector.cpp
More file actions
126 lines (110 loc) · 3.05 KB
/
doublevector.cpp
File metadata and controls
126 lines (110 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (C) 2004 - 2005 by
* Hieu Xuan Phan & Minh Le Nguyen {hieuxuan, nguyenml}@jaist.ac.jp
* Graduate School of Information Science,
* Japan Advanced Institute of Science and Technology (JAIST)
*
* doublevector.cpp - this file is part of FlexCRFs.
*
* Begin: Dec. 15, 2004
* Last change: Sep. 09, 2005
*
* FlexCRFs is a free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* FlexCRFs is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FlexCRFs; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "doublevector.h"
#include <cstring>
using namespace std;
// constructor with length
doublevector::doublevector(int len) {
this->len = len;
vect = new double[len];
}
// constructor with length and content
doublevector::doublevector(int len, double * vect) {
this->len = len;
vect = new double[len];
for (int i = 0; i < len; i++) {
this->vect[i] = vect[i];
}
}
// copy constructor
doublevector::doublevector(doublevector & dv) {
len = dv.len;
vect = new double[len];
for (int i = 0; i < len; i++) {
vect[i] = dv.vect[i];
}
}
// the size of vector
int doublevector::size() {
return len;
}
// overloading assignment operator
void doublevector::operator=(double val) {
//memset(vect,0,sizeof(double)*len);
for (int i = 0; i < len; i++) {
vect[i] = val;
}
}
// overloading assignment operator
void doublevector::operator=(doublevector & dv) {
if (len != dv.len) {
if (vect) {
delete vect;
}
len = dv.len;
vect = new double[len];
}
//
memcpy((void*)vect,(void*)dv.vect,sizeof(double)*len);
/*for (int i = 0; i < len; i++) {
vect[i] = dv.vect[i];
}*/
}
// assign the same value to all vector elements
void doublevector::assign(double val) {
*this = val;
}
// assign values for all elements from another doublevector
void doublevector::assign(doublevector & dv) {
*this = dv;
}
// reference to an element of index "idx"
double & doublevector::operator[](int idx) {
return vect[idx];
}
// sum of all vector elements
double doublevector::sum() {
double res = 0.0;
for (int i = 0; i < len; i++) {
res += vect[i];
}
return res;
}
// component multiplication
void doublevector::comp_mult(double val) {
for (int i = 0; i < len; i++) {
vect[i] *= val;
}
}
// component multiplication
void doublevector::comp_mult(doublevector * db) {
//double sum = 0.0;
for (int i = 0; i < len; i++) {
vect[i] *= db->vect[i];
//sum += vect[i];
}
//return sum;
}