-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
74 changed files
with
8,161 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Anm | ||
/* | ||
------------------------------------------------------ | ||
作者 : Black Ghost | ||
日期 : 2018-12-6 | ||
版本 : 0.0.0 | ||
------------------------------------------------------ | ||
m | ||
计算排列 A 的值 | ||
n | ||
------------------------------------------------------ | ||
输入 : | ||
n 数据点矩阵,(n+1)x2,第一列xi;第二列yi | ||
m 插值点, xq!=xi | ||
输出 : | ||
sol 结果 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
------------------------------------------------------ | ||
*/ | ||
|
||
package goNum | ||
|
||
func Anm(n, m int) int { | ||
//不直接使用阶乘计算可以稍许增加速度 | ||
temp0 := 1 | ||
for i := n; i >= n-m+1; i-- { | ||
temp0 = temp0 * i | ||
} | ||
return temp0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Bisection | ||
/* | ||
------------------------------------------------------ | ||
作者 : Black Ghost | ||
日期 : 2018-11-01 | ||
版本 : 0.0.0 | ||
------------------------------------------------------ | ||
此程序设计使用二分法来求解连续、单自变量、单调函数(区间 | ||
内)指定有限区间上的解 | ||
线性收敛 | ||
------------------------------------------------------ | ||
输入 : | ||
fn 函数,定义为等式左侧部分,右侧为零 | ||
a, b 求解区间 | ||
N 步数上限 | ||
tol 误差上限 | ||
输出 : | ||
sol 解值,指针 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
------------------------------------------------------ | ||
*/ | ||
|
||
package goNum | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
func Bisection(fn func(float64) float64, a, b float64, N int, tol float64) (*float64, bool) { | ||
/* | ||
用二分法来求解连续、单自变量、单调函数(区间内)指定有限区间上的解 | ||
输入 : | ||
fn 函数,定义为等式左侧部分,右侧为零 | ||
a, b 求解区间 | ||
N 步数上限 | ||
tol 误差上限 | ||
输出 : | ||
sol 解值,指针 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
*/ | ||
var sol float64 | ||
var err bool = false | ||
|
||
//判断在[a,b]区间是否有解 | ||
if (fn(a) > 0 && fn(b) > 0) || (fn(a) < 0 && fn(b) < 0) { | ||
return &sol, err | ||
} | ||
|
||
//求解 | ||
for i := 0; i < N; i++ { | ||
sol = (a + b) / 2 | ||
//解出 | ||
if math.Abs(fn(sol)) < tol { | ||
err = true | ||
return &sol, err | ||
} | ||
//未解出,重置区间边界 | ||
switch { | ||
case fn(sol) < 0 && fn(a) < 0: | ||
a = sol | ||
case fn(sol) > 0 && fn(a) < 0: | ||
b = sol | ||
case fn(sol) < 0 && fn(b) < 0: | ||
b = sol | ||
case fn(sol) > 0 && fn(b) < 0: | ||
a = sol | ||
default: | ||
return &sol, err | ||
} | ||
} | ||
return &sol, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Bisection_test | ||
/* | ||
------------------------------------------------------ | ||
作者 : Black Ghost | ||
日期 : 2018-11-01 | ||
版本 : 0.0.0 | ||
------------------------------------------------------ | ||
此程序设计使用二分法来求解连续、单自变量、单调函数(区间 | ||
内)指定有限区间上的解 | ||
线性收敛 | ||
------------------------------------------------------ | ||
输入 : | ||
fn 函数,定义为等式左侧部分,右侧为零 | ||
a, b 求解区间 | ||
N 步数上限 | ||
tol 误差上限 | ||
输出 : | ||
sol 解值,指针 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
------------------------------------------------------ | ||
*/ | ||
|
||
package goNum_test | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
) | ||
|
||
func Bisection(fn func(float64) float64, a, b float64, N int, tol float64) (*float64, bool) { | ||
/* | ||
用二分法来求解连续、单自变量、单调函数(区间内)指定有限区间上的解 | ||
输入 : | ||
fn 函数,定义为等式左侧部分,右侧为零 | ||
a, b 求解区间 | ||
N 步数上限 | ||
tol 误差上限 | ||
输出 : | ||
sol 解值,指针 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
*/ | ||
var sol float64 | ||
var err bool = false | ||
|
||
//判断在[a,b]区间是否有解 | ||
if (fn(a) > 0 && fn(b) > 0) || (fn(a) < 0 && fn(b) < 0) { | ||
return &sol, err | ||
} | ||
|
||
//求解 | ||
for i := 0; i < N; i++ { | ||
sol = (a + b) / 2 | ||
//解出 | ||
if math.Abs(fn(sol)) < tol { | ||
err = true | ||
return &sol, err | ||
} | ||
//未解出,重置区间边界 | ||
switch { | ||
case fn(sol) < 0 && fn(a) < 0: | ||
a = sol | ||
case fn(sol) > 0 && fn(a) < 0: | ||
b = sol | ||
case fn(sol) < 0 && fn(b) < 0: | ||
b = sol | ||
case fn(sol) > 0 && fn(b) < 0: | ||
a = sol | ||
default: | ||
return &sol, err | ||
} | ||
} | ||
return &sol, err | ||
} | ||
|
||
func BenchmarkBisection(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Bisection(func(x float64) float64 { return math.Pow(x, 3.0) + 4.0*math.Pow(x, 2.0) - 10.0 }, 1.0, 2.0, 1000, 1e-6) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Cnm | ||
/* | ||
------------------------------------------------------ | ||
作者 : Black Ghost | ||
日期 : 2018-12-6 | ||
版本 : 0.0.0 | ||
------------------------------------------------------ | ||
m | ||
计算组合 C 的值 | ||
n | ||
------------------------------------------------------ | ||
输入 : | ||
n 数据点矩阵,(n+1)x2,第一列xi;第二列yi | ||
m 插值点, xq!=xi | ||
输出 : | ||
sol 结果 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
------------------------------------------------------ | ||
*/ | ||
|
||
package goNum | ||
|
||
func Cnm(n, m int) int { | ||
//不直接使用阶乘计算可以稍许增加速度 | ||
return Anm(n, m) / Factorial(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// DetA | ||
/* | ||
------------------------------------------------------ | ||
作者 : Black Ghost | ||
日期 : 2018-11-20 | ||
版本 : 0.0.0 | ||
------------------------------------------------------ | ||
求矩阵行列式值的列主元消去法 | ||
理论: | ||
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学 | ||
出版社, 2000, pp 52. | ||
------------------------------------------------------ | ||
输入 : | ||
a 矩阵 | ||
输出 : | ||
sol 解值,值 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
------------------------------------------------------ | ||
*/ | ||
|
||
package goNum | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
func DetA(a [][]float64) (float64, bool) { | ||
/* | ||
求矩阵行列式值的列主元消去法 | ||
输入 : | ||
a 矩阵 | ||
输出 : | ||
sol 解值,值 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
*/ | ||
|
||
var sol float64 = 1.0 | ||
var err bool = false | ||
var count0 int | ||
n := len(a) | ||
temp0 := make([]float64, n) | ||
|
||
// 判断是否方阵 | ||
if len(a) != len(a[0]) { | ||
return sol, err | ||
} | ||
|
||
//主元消去 | ||
for i := 0; i < n; i++ { | ||
//求第i列的主元素并调整行顺序 | ||
acol := make([]float64, n-i) | ||
for icol := i; icol < n; icol++ { | ||
acol[icol-i] = a[icol][i] | ||
} | ||
_, ii, _ := MaxAbs(acol) | ||
if ii+i != i { | ||
count0++ | ||
temp0 = a[ii+i] | ||
a[ii+i] = a[i] | ||
a[i] = temp0 | ||
} | ||
|
||
//列消去 | ||
for j := i + 1; j < n; j++ { | ||
mul := a[j][i] / a[i][i] | ||
for k := i; k < n; k++ { | ||
a[j][k] = a[j][k] - a[i][k]*mul | ||
} | ||
} | ||
sol = math.Pow(-1.0, float64(count0)) * sol * a[i][i] | ||
} | ||
|
||
err = true | ||
return sol, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// DetA_test | ||
/* | ||
------------------------------------------------------ | ||
作者 : Black Ghost | ||
日期 : 2018-11-20 | ||
版本 : 0.0.0 | ||
------------------------------------------------------ | ||
求矩阵行列式值的列主元消去法 | ||
理论: | ||
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学 | ||
出版社, 2000, pp 52. | ||
------------------------------------------------------ | ||
输入 : | ||
a 矩阵 | ||
输出 : | ||
sol 解值,值 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
------------------------------------------------------ | ||
*/ | ||
|
||
package goNum_test | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"github.com/chfenger/goNum" | ||
) | ||
|
||
func DetA(a [][]float64) (float64, bool) { | ||
/* | ||
求矩阵行列式值的列主元消去法 | ||
输入 : | ||
a 矩阵 | ||
输出 : | ||
sol 解值,值 | ||
err 解出标志:false-未解出或达到步数上限; | ||
true-全部解出 | ||
*/ | ||
|
||
var sol float64 = 1.0 | ||
var err bool = false | ||
var count0 int | ||
n := len(a) | ||
temp0 := make([]float64, n) | ||
|
||
// 判断是否方阵 | ||
if len(a) != len(a[0]) { | ||
return sol, err | ||
} | ||
|
||
//主元消去 | ||
for i := 0; i < n; i++ { | ||
//求第i列的主元素并调整行顺序 | ||
acol := make([]float64, n-i) | ||
for icol := i; icol < n; icol++ { | ||
acol[icol-i] = a[icol][i] | ||
} | ||
_, ii, _ := goNum.MaxAbs(acol) | ||
if ii+i != i { | ||
count0++ | ||
temp0 = a[ii+i] | ||
a[ii+i] = a[i] | ||
a[i] = temp0 | ||
} | ||
|
||
//列消去 | ||
for j := i + 1; j < n; j++ { | ||
mul := a[j][i] / a[i][i] | ||
for k := i; k < n; k++ { | ||
a[j][k] = a[j][k] - a[i][k]*mul | ||
} | ||
} | ||
sol = math.Pow(-1.0, float64(count0)) * sol * a[i][i] | ||
} | ||
|
||
err = true | ||
return sol, err | ||
} | ||
|
||
func BenchmarkDetA(b *testing.B) { | ||
a14 := [][]float64{ | ||
{11.0, -3.0, -2.0}, | ||
{-23.0, 11.0, 1.0}, | ||
{1.0, -2.0, 2.0}, | ||
} | ||
|
||
for i := 0; i < b.N; i++ { | ||
DetA(a14) | ||
} | ||
} |
Oops, something went wrong.