-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cpp
69 lines (59 loc) · 1.18 KB
/
Vector.cpp
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
#include "Vector.h"
#include <iostream>
#include <iomanip>
using namespace std;
static const double EPS = 1e-8;
Vector * Vector::CreateVectorFromArray(int count_row, double * data)
{
VectorP v= new Vector;
v->count_column = 1;
v->count_row = count_row;
v->data = new double[count_row];
int i;
for (i = 0; i < count_row; i++)
v->data[i] = data[i];
return v;
}
Vector * Vector::CreateMatrixColumn(MatrixP m, int column)
{
return (VectorP)m->CreateSubMatrix(0,m->GetCountRow()-1,column,column);
}
Vector * Vector::CreateMatrixRow(MatrixP m, int row)
{
MatrixP temp = m->CreateSubMatrix(row, row, 0, m->GetCountColumn() - 1);
if (temp == NULL)
return NULL;
else
return (VectorP)(temp->TransformTransposition());
}
double Vector::InnerProduct(Vector *v1, Vector *v2)
{
if (v1->count_row != v2->count_row)
return 0;
double sum = 0;
int i;
for (i = 0; i < v1->count_row; i++)
sum += v1->data[i] * v2->data[i];
return sum;
}
void Vector::Show()
{
int i;
cout << '(';
for (i = 0; i < count_row; i++)
{
if (i != 0)
cout << " ";
if (fabs(data[i]) < EPS)
cout << '0';
else
cout << setprecision(4)<<data[i];
}
cout << ')';
}
Vector::Vector()
{
}
Vector::~Vector()
{
}