-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve_master.js
106 lines (101 loc) · 3.38 KB
/
curve_master.js
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
let {Matrix,PBR_buster} = require('./matrix')
let matrix = new Matrix()
class curve_master{
constructor(){
this.coff = []
}
equation_model=(n)=>{
let char = []
let i;
for(i=0;i<n;i++){
char.push(String.fromCharCode(i+65))
}
let equation = 'y+'
for(i=n-1;i>=0;i--){
if(i!=0)
equation+=`${char[n-i-1]}${'x'.repeat(i)}+`
else
equation+=`${char[n-i-1]}${'x'.repeat(i)}`
}
return equation
}
least_squares=(n)=>{
let i=0,j=0,t;
let equation = this.equation_model(n)
let equations = []
for(i=0;i<n;i++){
let k = equation.split('+')
for(j=0;j<k.length;j++){
k[j]+='x'.repeat(i)
t = k[j].match(/[x]/g)!=null ? k[j].match(/[x]/g).length : 0
if(t>1){
k[j] = k[j].match('y') ? `yx^${t}` : `${k[j].replace(/[x]/g,'')}x^${t}`
}
}
equations.push(k)
}
return equations
}
curve_fit=(x,y,n)=>{
let i,j,k;
let least_array = this.least_squares(n)
let sigma_array_A = []
let sigma_array_B = []
let temp ;
let l = x.length;
for(i=0;i<least_array.length;i++){
temp = PBR_buster(least_array[i])
console.log(temp[0]+' = '+temp.slice(1,temp.length).join(' + '))
}
let flag = 0;
for(i=0;i<least_array.length;i++){
sigma_array_B[i]=0
sigma_array_A[i]=[]
flag = 0
for(j=0;j<least_array[i].length;j++){
sigma_array_A[i].push(0)
for(k=0;k<l;k++){
if(least_array[i][j].match('y') && !least_array[i][j].match('x')){
sigma_array_B[i]+=y[k]
}
else if(least_array[i][j].match('y') && least_array[i][j].match('x')){
temp = PBR_buster(least_array[i][j]).split('^')
sigma_array_B[i]+=y[k]*x[k]**(temp.length == 1 ? 1 : parseInt(temp[1]))
}
else if(least_array[i][j].match('x') && least_array[i][j].match(/[A-Z]/g)){
temp = PBR_buster(least_array[i][j]).split('^')
sigma_array_A[i][j]+=x[k]**(temp.length == 1 ? 1 : parseInt(temp[1]))
flag=1
}
}
if(flag==1){
sigma_array_A[i][j]=sigma_array_A[i][j]==0 ? l : sigma_array_A[i][j];
}
}
sigma_array_A[i]=sigma_array_A[i].slice(1,sigma_array_A[i].length)
}
console.log(sigma_array_A,sigma_array_B)
let equation = least_array[0].slice(1,least_array[0].length)
console.log(equation)
let coff = matrix.crammers(sigma_array_A,sigma_array_B)
this.coff = coff
return coff
}
predict=(x)=>{
let i;
let n = this.coff.length;
let out = 0;
for(i=0;i<n;i++){
out+=this.coff[i] * x ** (n-i-1)
}
return out
}
bulk_predict=(array)=>{
let i;
let predited_array = []
for(i=0;i<array.length;i++)
predited_array[i]=this.predict(array[i])
return predited_array
}
}
module.exports = curve_master