Skip to content

Commit f74ade0

Browse files
committed
线性相关系数,vectorNode完善拷贝
1 parent 6176d81 commit f74ade0

File tree

4 files changed

+92
-22
lines changed

4 files changed

+92
-22
lines changed

help.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,12 @@ class help
8888
getchar();
8989
}
9090

91+
static vectorNode toVectorNode(vector<string>com)
92+
{
93+
vectorNode v(com.size()-1);
94+
for(unsigned int i=1;i<com.size();i++)
95+
v.v[i-1]=help::tofloat(com[i]);
96+
return v;
97+
}
98+
9199
};

lab.h

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,62 @@
11
#pragma once
22
#include "help.h"
33

4-
54
class lab
65
{
7-
public:
8-
static float avg(vector<string>com)
6+
private:
7+
static float avg(vectorNode v)
98
{
109
float sum=0;
11-
for(unsigned int i=1;i<com.size();i++)
12-
sum+=help::tofloat(com[i]);
13-
return sum/(com.size()-1);
10+
for(unsigned int i=0;i<v.getl();i++)
11+
sum+=v.v[i];
12+
return sum/v.getl();
1413
}
1514

16-
static float dev(vector<string>com)
15+
static float err(vectorNode v, float avg=-1)
1716
{
18-
float avg=lab::avg(com);
17+
if(avg==-1)
18+
{
19+
avg=lab::avg(v);
20+
}
1921
float sum=0;
20-
for(unsigned int i=1;i<com.size();i++)
22+
for(unsigned int i=0;i<v.getl();i++)
2123
{
22-
float num=help::tofloat(com[i])-avg;
24+
float num=v.v[i]-avg;
2325
sum+=num*num;
2426
}
25-
return sum/(com.size()-2);
27+
return sum;
28+
}
29+
30+
public:
31+
static float avg(vector<string>com)
32+
{
33+
//vectorNode v(com.size()-1);
34+
vectorNode v=help::toVectorNode(com);
35+
return avg(v);
36+
}
37+
38+
static float var(vector<string>com)
39+
{
40+
auto v=help::toVectorNode(com);
41+
return err(v)/v.getl();
42+
}
43+
44+
static float r(vectorNode &x, vectorNode &y)
45+
{
46+
float avgx=avg(x);
47+
float avgy=avg(y);
48+
float lxx=err(x,avgx);
49+
float lyy=err(y,avgy);
50+
51+
float lxy=0;
52+
for(unsigned int i=0;i<x.getl();i++)
53+
{
54+
float num1=x.v[i]-avgx;
55+
float num2=y.v[i]-avgy;
56+
lxy+=abs(num1*num2);
57+
}
58+
59+
return lxy/(sqrt(lxx*lyy));
2660
}
2761

2862
static tuple<double,double> LeastSquare(vectorNode &x, vectorNode &y)

main.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
using namespace std;
1212

13+
void getXY(vectorNode &v1,vectorNode &v2)
14+
{
15+
cout<<"vector x="<<endl;
16+
help::getvector(v1);
17+
cout<<"vector y="<<endl;
18+
help::getvector(v2);
19+
}
20+
1321
int main()
1422
{
1523
while(1)
@@ -61,26 +69,32 @@ int main()
6169
help::t();
6270
cout<<lab::avg(com)<<endl;
6371
}
64-
if(com[0]=="dev")
72+
if(com[0]=="var")
6573
{
6674
isCommand=true;
6775
help::t();
68-
cout<<lab::dev(com)<<endl;
76+
cout<<lab::var(com)<<endl;
6977
}
7078
if(com[0]=="linerFit")
7179
{
7280
isCommand=true;
7381
vectorNode v1(help::toint(com[1]));
7482
vectorNode v2(help::toint(com[1]));
75-
cout<<"vector x="<<endl;
76-
help::getvector(v1);
77-
cout<<"vector y="<<endl;
78-
help::getvector(v2);
83+
getXY(v1,v2);
7984
double k,b;
8085
tie(k,b)=lab::LeastSquare(v1,v2);
8186
help::t();
8287
cout<<"k:"<<k<<" b:"<<b<<endl;
8388
}
89+
if(com[0]=="r")
90+
{
91+
isCommand=true;
92+
vectorNode v1(help::toint(com[1]));
93+
vectorNode v2(help::toint(com[1]));
94+
getXY(v1,v2);
95+
help::t();
96+
cout<<"r:"<<lab::r(v1,v2)<<endl;
97+
}
8498
}
8599
if(isCommand==false)
86100
{

scmath/matrix.hpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ class vectorNode : public BasicNode
77
{
88
private:
99
unsigned int l;
10+
void copy(const vectorNode& m2)
11+
{
12+
this->l=m2.l;
13+
this->v = new double[l];
14+
for (unsigned int i = 0; i < l; i++)
15+
this->v[i] = m2.v[i];
16+
}
1017

1118
public:
1219
//SCMath兼容
@@ -27,10 +34,14 @@ class vectorNode : public BasicNode
2734

2835
vectorNode(const vectorNode& m2) : BasicNode(m2)
2936
{
30-
this->l = m2.l;
31-
this->v = new double[l];
32-
for (unsigned int i = 0; i < l; i++)
33-
this->v[i] = m2.v[i];
37+
copy(m2);
38+
}
39+
40+
vectorNode& operator=(const vectorNode& m2)
41+
{
42+
delete []v;
43+
copy(m2);
44+
return *this;
3445
}
3546

3647
double dot(vectorNode &v2)
@@ -76,7 +87,10 @@ class vectorNode : public BasicNode
7687
printf("\n");
7788
}
7889

79-
~vectorNode() { delete[]v; }
90+
~vectorNode()
91+
{
92+
delete[]v;
93+
}
8094
unsigned int getl() { return l; }
8195
};
8296

0 commit comments

Comments
 (0)