-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
98 lines (78 loc) · 2.02 KB
/
test.js
File metadata and controls
98 lines (78 loc) · 2.02 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
const util = require('../lib/util')
const assert = require('assert')
/**
*
* @param {Array} actual
* @param {Array} excepted
* @param {String} message
* @param {Number} deviation
*/
let ArrayApproximateEqual = (function () {
let count = 0
return function ArrayApproximateEqual(actual, excepted, message, deviation=1e-7) {
for (let i = 0; i < excepted.length; i++) {
assert(actual[i] + deviation >= excepted[i], message)
assert(actual[i] - deviation <= excepted[i], message)
}
count++
console.log(`第${count}个测试通过`)
}
})()
// root = [-5, 1, 1]
let U = [
[1, 2, 3],
[0, 5, 6],
[0, 0, 1]
]
let y = [0, 11, 1]
ArrayApproximateEqual(util.backSubstitution(U, y), [-5, 1, 1])
// root = [1, 2, 3]
U = [
[1, 2, 0],
[1, 1, 2],
[0, 1, 3]
]
y = [5, 9, 11]
ArrayApproximateEqual(util.chaseMethod(U, y), [1, 2, 3])
// root = [1, 1, 8]
U = [
[2, 2, -1],
[1, -1, 0],
[4, -2, -1]
]
y = [-4, 0, -6]
ArrayApproximateEqual(util.GaussianEliminatio(U, y), [1, 1, 8])
let pai = function(x){
return 4 / (1 + x * x)
}
let PI = 3.141592653
console.log('PI', util.integral(pai, 0, 1, 100000, algo="T"))
console.log('PI', util.integral(pai, 0, 1, 100000, algo="S"))
console.log('PI', util.integral(pai, 0, 1, 100000, algo="C"))
function func(x, y){
return -2 * x * y
}
// 0.03916389509898707
console.log('精确解:', 0.03916389509898707)
console.log('EulerFormula', util.EulerFormula(func, 0, 1, 1.8, 1800000))
console.log('RungeKuttaMethod', util.RungeKuttaMethod(func, 0, 1, 1.8, 18000000, 1/2))
let x = [30, 45, 60]
y = [1/2, 0.5 ** 0.5, 0.75 ** 0.5]
console.log(util.diffQuotient(x, y, 2))
console.log(util.NewtonInterpolation(x, y, 2)(50))
let A = [
[1, 2],
[3, 4],
]
console.log(util.transport(A, 2, 2))
console.log(util.MatrixOpMtraix(A, '*', A))
A = [
[2, -1],
[8, 4],
[2, 1],
[7, -1],
[4, 0]
]
let b = [1, 0, 1, 8, 3]
// [ 0.7927199191102123, -1.4641051567239636 ]
console.log(util.leastSquares(A, b))