fixed dependencies

This commit is contained in:
nuknal
2024-10-24 15:46:01 +08:00
parent d16a5bd9c0
commit 1161e8d054
2005 changed files with 690883 additions and 0 deletions

12
vendor/github.com/nuknal/goNum/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,12 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

3
vendor/github.com/nuknal/goNum/AUTHOR.MD generated vendored Normal file
View File

@@ -0,0 +1,3 @@
1. NAME. chfenger、Black Ghost
2. EMAIL. chengfengcool@sina.com

43
vendor/github.com/nuknal/goNum/Anm.go generated vendored Normal file
View File

@@ -0,0 +1,43 @@
// Anm
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-6
版本 : 0.0.0
------------------------------------------------------
m
计算排列 A 的值
n
------------------------------------------------------
输入 :
n 整数
m 整数, m <= n
输出 :
------------------------------------------------------
*/
package goNum
// Anm
// m
// 计算排列 A 的值
// n
func Anm(n, m int) int {
/*
m
计算排列 A 的值
n
------------------------------------------------------
输入 :
n 整数
m 整数, m <= n
输出 :
*/
//不直接使用阶乘计算可以稍许增加速度
temp0 := 1
for i := n; i >= n-m+1; i-- {
temp0 = temp0 * i
}
return temp0
}

78
vendor/github.com/nuknal/goNum/Bisection.go generated vendored Normal file
View File

@@ -0,0 +1,78 @@
// Bisection
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-01
版本 : 0.0.0
------------------------------------------------------
此程序设计使用二分法来求解连续、单自变量、单调函数(区间
内)指定有限区间上的解
线性收敛
------------------------------------------------------
输入 :
fn 函数,定义为等式左侧部分,右侧为零
a, b 求解区间
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// Bisection 此程序设计使用二分法来求解连续、单自变量、单调函数(区间
//内)指定有限区间上的解
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
}

77
vendor/github.com/nuknal/goNum/BubbleSort.go generated vendored Normal file
View File

@@ -0,0 +1,77 @@
// BubbleSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-05
版本 : 0.0.0
------------------------------------------------------
冒泡排序法
理论:
时间复杂度: O(n^2)
最好情况 : O(n)
最坏情况 : O(n^2)
空间复杂度: O(1)
稳定性 : 稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// BubbleSort 冒泡排序法
func BubbleSort(in Matrix) (Matrix, bool) {
/*
冒泡排序法
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if in.Rows != 1 {
panic("Error in goNum.BubbleSort: Input Matrix error")
}
if in.Columns < 1 {
panic("Error in goNum.BubbleSort: Empty input Matrix")
} else if in.Columns == 1 {
return in, true
}
n := in.Columns
sol := ZeroMatrix(1, n)
var err bool = false
//初始化sol
for i := 0; i < n; i++ {
sol.Data[i] = in.Data[i]
}
//排序开始, 方法1
for i := 0; i < n-1; i++ {
for j := 0; j < n-i-1; j++ {
if sol.Data[j] > sol.Data[j+1] {
sol.Data[j], sol.Data[j+1] = sol.Data[j+1], sol.Data[j]
}
}
}
// 方法2
// for isSort {
// isSort = false
// for j := 0; j < n-1; j++ {
// if sol.Data[j] > sol.Data[j+1] {
// sol.Data[j], sol.Data[j+1] = sol.Data[j+1], sol.Data[j]
// isSort = true
// }
// }
// }
err = true
return sol, err
}

107
vendor/github.com/nuknal/goNum/BucketSort.go generated vendored Normal file
View File

@@ -0,0 +1,107 @@
// BucketSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-06
版本 : 0.0.0
------------------------------------------------------
桶排序法
理论:
时间复杂度: O(n+k)
最好情况 : O(n)
最坏情况 : O(n^2)
空间复杂度: O(n+k)
稳定性 : 稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
bucketSize 桶中元素数
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
注意:
仅对整数排序有效
------------------------------------------------------
*/
package goNum
import (
"math"
)
// IntMin 整数切片中最小数
func IntMin(in []int) int {
min := in[0]
for i := 1; i < len(in); i++ {
if min > in[i] {
min = in[i]
}
}
return min
}
// bucketSort_sort
func bucketSort_sort(temp0 []int, bucketSize int) []int {
if (temp0 == nil) || (len(temp0) < 2) {
return temp0
}
temp2 := make([]int, 0)
min := IntMin(temp0)
max := IntMax(temp0)
bucketCount := int(math.Floor(float64((max-min)/bucketSize))) + 1
bucket := make([][]int, bucketCount) //第一维为桶数量,第二维为桶容量|| (bucketSize == 0)
//排序开始
//利用映射函数将数据分配到各个桶中
for i := 0; i < len(temp0); i++ {
indi := int(math.Floor(float64((temp0[i] - min) / bucketSize)))
bucket[indi] = append(bucket[indi], temp0[i])
}
//桶中排序
for i := 0; i < bucketCount; i++ {
if bucketCount == 1 {
bucketSize--
}
temp1 := bucketSort_sort(bucket[i], bucketSize)
for j := 0; j < len(temp1); j++ {
temp2 = append(temp2, temp1[j])
}
}
return temp2
}
// BucketSort 桶排序法
func BucketSort(in []int, bucketSize int) ([]int, bool) {
/*
桶排序法
输入 :
in 输入矩阵, 1xn
bucketSize 桶中元素数
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if len(in) < 1 {
panic("Error in goNum.BucketSort: Empty input Matrix")
} else if len(in) == 1 {
return in, true
}
n := len(in)
var err bool = false
soltemp := make([]int, n)
for i := 0; i < n; i++ {
soltemp[i] = in[i]
}
//排序开始
sol := bucketSort_sort(soltemp, bucketSize)
err = true
return sol, err
}

38
vendor/github.com/nuknal/goNum/Cnm.go generated vendored Normal file
View File

@@ -0,0 +1,38 @@
// Cnm
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-6
版本 : 0.0.0
------------------------------------------------------
m
计算组合 C 的值
n
------------------------------------------------------
输入 :
n 整数
m 整数m <= n
输出 :
------------------------------------------------------
*/
package goNum
// Cnm
// m
// 计算组合 C 的值
// n
func Cnm(n, m int) int {
/*
m
计算组合 C 的值
n
输入 :
n 整数
m 整数m <= n
输出 :
*/
//不直接使用阶乘计算可以稍许增加速度
return Anm(n, m) / Factorial(m)
}

88
vendor/github.com/nuknal/goNum/CountingSort.go generated vendored Normal file
View File

@@ -0,0 +1,88 @@
// CountingSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-06
版本 : 0.0.0
------------------------------------------------------
计数排序法
理论:
时间复杂度: O(n+k)
最好情况 : O(n+k)
最坏情况 : O(n+k)
空间复杂度: O(n+k)
稳定性 : 稳定
------------------------------------------------------
输入 :
in 输入切片, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
注意:
仅对整数排序有效
------------------------------------------------------
*/
package goNum
// IntMax 整数切片中最大数
func IntMax(in []int) int {
max := in[0]
for i := 1; i < len(in); i++ {
if in[i] > max {
max = in[i]
}
}
return max
}
// CountingSort 计数排序法
func CountingSort(in []int) ([]int, bool) {
/*
计数排序法
输入 :
in 输入切片, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if len(in) < 1 {
panic("Error in goNum.CountingSort: Empty input Matrix")
} else if len(in) == 1 {
return in, true
}
n := len(in)
sol := make([]int, n)
max := IntMax(in)
temp := make([]int, max+1)
ind := 0
var err bool = false
//初始化sol
for i := 0; i < n; i++ {
sol[i] = in[i]
}
//排序开始
for i := 0; i < n; i++ {
// if !temp[sol[i]] {
// temp[sol[i]] = 0
// }
temp[sol[i]]++
}
for i := 0; i < max+1; i++ {
for temp[i] > 0 {
sol[ind] = i
ind++
temp[i]--
}
}
err = true
return sol, err
}

65
vendor/github.com/nuknal/goNum/DerivativePoly.go generated vendored Normal file
View File

@@ -0,0 +1,65 @@
// DerivativePoly
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-25
版本 : 0.0.0
------------------------------------------------------
求单变量多项式n阶导数
理论:
------------------------------------------------------
输入 :
A 按幂次连续增加的系数向量,(Nn+1)x1,Nn为最高幂次
n 求导次数
输出 :
sol 解,(Nn+1-n)x1
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
// DerivativePoly 求单变量多项式n阶导数
func DerivativePoly(A Matrix, n int) (Matrix, bool) {
/*
求单变量多项式n阶导数
输入 :
A 按幂次连续增加的系数向量,(Nn+1)x1,Nn为最高幂次
n 求导次数
输出 :
sol 解,(Nn+1-n)x1
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//判断求导次数与最高幂次关系
Nn := A.Rows - 1
if n > Nn+1 {
panic("Error in goNum.DerivativePoly: Derivative number greater than polynomial's order")
}
//Nn+1 = n
if Nn+1 == n {
return NewMatrix(1, 1, []float64{0.0}), true
}
sol := ZeroMatrix(Nn+1, 1)
var lenSol int = Nn + 1
var err bool = false
//赋予soltemp初值
for i := 0; i < Nn+1; i++ {
sol.Data[i] = A.Data[i]
}
//求导计算
for i := 1; i < n+1; i++ {
for j := 1; j < lenSol; j++ {
sol.Data[j-1] = float64(j) * sol.Data[j]
}
lenSol--
}
err = true
return NewMatrix(lenSol, 1, sol.Data[:lenSol]), err
}

77
vendor/github.com/nuknal/goNum/DetA.go generated vendored Normal file
View File

@@ -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"
)
// DetA 求矩阵行列式值的列主元消去法
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
}

50
vendor/github.com/nuknal/goNum/E_Mat.go generated vendored Normal file
View File

@@ -0,0 +1,50 @@
// E_Mat
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-20
版本 : 0.0.0
------------------------------------------------------
返回n阶单位矩阵
------------------------------------------------------
输入 :
n 阶数
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// E_Mat 返回n阶单位矩阵
func E_Mat(n int) ([][]float64, bool) {
/*
返回n阶单位矩阵
输入 :
n 阶数
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
sol := make([][]float64, n)
for i := 0; i < n; i++ {
sol[i] = make([]float64, n)
}
var err bool = false
//判断阶数
if n < 1 {
return nil, err
}
//分配元素
for i := 0; i < n; i++ {
sol[i][i] = 1.0
}
err = true
return sol, err
}

88
vendor/github.com/nuknal/goNum/ErrorEvaluation.go generated vendored Normal file
View File

@@ -0,0 +1,88 @@
// ErrorEvaluation
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-23
版本 : 0.0.0
------------------------------------------------------
误差估计方法
理论:
0 最大误差:
E = max(Abs(f(xk)-y(xk)))
1 平均误差:
1 N
E = --- Sum (Abs(f(xk)-y(xk)))
N k=1
2 均方根误差:
1
E = Sqrt(--- Sum (f(xk)-y(xk))^2)
N
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. pp. 196
------------------------------------------------------
输入 :
FY 数据对nx2f(xk)---y(xk)
输出 :
sol 误差结果
------------------------------------------------------
*/
package goNum
import (
"math"
)
// MaxError 最大误差
func MaxError(FY Matrix) float64 {
//最大误差
//判断FY的维数
if FY.Columns < 2 {
panic("Error in goNum.MaxError: FY is at least 2 columns")
}
errs := ZeroMatrix(FY.Rows, 1)
var maxE float64
for i := 0; i < FY.Rows; i++ {
errs.Data[i] = math.Abs(FY.GetFromMatrix(i, 1) - FY.GetFromMatrix(i, 0))
}
maxE, _, _ = Max(errs.Data)
return maxE
}
// MeanError 平均误差
func MeanError(FY Matrix) float64 {
//平均误差
//判断FY的维数
if FY.Columns < 2 {
panic("Error in goNum.MaxError: FY is at least 2 columns")
}
var meanE float64
for i := 0; i < FY.Rows; i++ {
meanE += math.Abs(FY.GetFromMatrix(i, 1) - FY.GetFromMatrix(i, 0))
}
meanE = meanE / float64(FY.Rows)
return meanE
}
// RMSError 均方根误差
func RMSError(FY Matrix) float64 {
//均方根误差
//判断FY的维数
if FY.Columns < 2 {
panic("Error in goNum.MaxError: FY is at least 2 columns")
}
var rmsE float64
for i := 0; i < FY.Rows; i++ {
temp0 := FY.GetFromMatrix(i, 1) - FY.GetFromMatrix(i, 0)
rmsE += temp0 * temp0
}
rmsE = math.Sqrt(rmsE / float64(FY.Rows))
return rmsE
}

36
vendor/github.com/nuknal/goNum/Factorial.go generated vendored Normal file
View File

@@ -0,0 +1,36 @@
// Factorial
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-6
版本 : 0.0.0
------------------------------------------------------
计算自然数n的阶乘
------------------------------------------------------
输入 :
n 自然数
输出 :
sol 阶乘结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// Factorial 计算自然数n的阶乘
func Factorial(n int) int {
//判断n
if n < 0 {
panic("Error in goNum.Factorial: n < 1")
}
if n == 0 {
return 1
}
//计算
var sol int = 1
for i := n; i > 1; i-- {
sol = sol * i
}
return sol
}

47
vendor/github.com/nuknal/goNum/Fibonacci.go generated vendored Normal file
View File

@@ -0,0 +1,47 @@
// Fibonacci
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-24
版本 : 0.0.0
------------------------------------------------------
求Fibonacci数列
理论:
------------------------------------------------------
输入 :
n Fibonacci数列参数
输出 :
sol 解
------------------------------------------------------
*/
package goNum
// Fibonacci 求Fibonacci数列
func Fibonacci(n int) int {
/*
求Fibonacci数列
输入 :
n Fibonacci数列参数
输出 :
sol 解
*/
//判断n
F := make([]int, n+1)
if n == 0 {
F[0] = 0
return 0
} else if n == 1 {
F[1] = 1
return 1
}
F[0] = 0
F[1] = 1
for i := 2; i < n+1; i++ {
F[i] = F[i-1] + F[i-2]
}
return F[n]
}

97
vendor/github.com/nuknal/goNum/FittingBezier.go generated vendored Normal file
View File

@@ -0,0 +1,97 @@
// FittingBezier
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-23
版本 : 0.0.0
------------------------------------------------------
Bezier曲线拟合控制点
理论:
给定控制点集(xi, yi), i=0,1,...,N
则Bezier曲线可以表示为
| N
|x(t) = Sum xi*B_(i,N)(t)
| i=0
|
| N
|y(t) = Sum yi*B_(i,N)(t)
| i=0
其中,
B_(i,N)(t)为Bernstein多项式
N-i
B_(i,N)(t) = C *t^i*(1-t)^(N-i)
N
0 <= t <= 1
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 5.5
------------------------------------------------------
输入 :
XY 数据对nx2x-y
输出 :
sol 解,(N+1)x2x(t)-y(t)
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
//BernsteinPoly Bernstein Polynomial
func BernsteinPoly(i, N int) Matrix {
cni := Cnm(N, i)
sol := ZeroMatrix(N+1, 1)
soltemp := ZeroMatrix(N+1, 1)
soltemp.Data[0] = 1.0
soltemp.Data[1] = -1.0 //1-t
//(1-t)^(N-i)
if N-i > 1 {
for j := 2; j < N-i+1; j++ {
for k := j; k > 0; k-- {
soltemp.Data[k] = soltemp.Data[k] - soltemp.Data[k-1]
}
}
}
//(1-t)^(N-i) * t^i
for j := N; j >= i; j-- {
sol.Data[j] = float64(cni) * soltemp.Data[j-i]
}
return sol
}
// FittingBezier Bezier曲线拟合控制点
func FittingBezier(XY Matrix) (Matrix, bool) {
/*
Bezier曲线拟合控制点
输入 :
XY 数据对nx2x-y
输出 :
sol 解,(N+1)x2x(t)-y(t)
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//判断维数
if XY.Columns < 2 {
panic("Error in goNum.FittingBezier: At least 2 columns of XY needed")
}
n := XY.Rows - 1 //N-1
sol := ZeroMatrix(n+1, 2)
var err bool = false
//计算
for i := 0; i < n+1; i++ { //n+1项BernsteinPoly
soltemp := BernsteinPoly(i, n)
xi := XY.GetFromMatrix(i, 0)
yi := XY.GetFromMatrix(i, 1)
for j := 0; j < n+1; j++ { //n次BernsteinPoly
sol.SetMatrix(j, 0, sol.GetFromMatrix(j, 0)+xi*soltemp.Data[j])
sol.SetMatrix(j, 1, sol.GetFromMatrix(j, 1)+yi*soltemp.Data[j])
}
}
err = true
return sol, err
}

82
vendor/github.com/nuknal/goNum/FittingLSQ.go generated vendored Normal file
View File

@@ -0,0 +1,82 @@
// FittingLSQ
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-23
版本 : 0.0.0
------------------------------------------------------
线性最小二乘拟合
理论:
设对N个数据对的线性拟合表示为
y = Ax + B
N N N
A*Sum xi^2 + B*Sum xi = Sum xiyi
i=1 i=1 i=1
N N
A*Sum xi + NB = Sum yi
i=1 i=1
解此二元线性方程组即可得A、B
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 5.1
------------------------------------------------------
输入 :
XY 数据对nx2x-y
输出 :
sol 解2x1
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
// FittingLSQ 线性最小二乘拟合
func FittingLSQ(XY Matrix) (Matrix, bool) {
/*
线性最小二乘拟合
输入 :
XY 数据对nx2x-y
输出 :
sol 解2x1
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//判断XY的维数
if XY.Columns < 2 {
panic("Error in goNum.FittingLSQ: At least 2 columns of XY needed")
}
sol := ZeroMatrix(2, 1)
AS := ZeroMatrix(2, 2)
BS := ZeroMatrix(2, 1)
var err bool = false
var sx2, sx, sxy, sy float64
n := XY.Rows
//求累加和
for i := 0; i < n; i++ {
sx2 += XY.GetFromMatrix(i, 0) * XY.GetFromMatrix(i, 0)
sx += XY.GetFromMatrix(i, 0)
sxy += XY.GetFromMatrix(i, 0) * XY.GetFromMatrix(i, 1)
sy += XY.GetFromMatrix(i, 1)
}
AS.SetMatrix(0, 0, sx2)
AS.SetMatrix(0, 1, sx)
AS.SetMatrix(1, 0, sx)
AS.SetMatrix(1, 1, float64(n))
BS.SetMatrix(0, 0, sxy)
BS.SetMatrix(1, 0, sy)
//解二元线性方程组
soltemp, errtemp := LEs_ECPE(Matrix2ToSlices(AS), Matrix1ToSlices(BS))
if errtemp != true {
panic("Error in goNum.FittingLSQ: Solve error")
}
sol.SetMatrix(0, 0, soltemp[1])
sol.SetMatrix(1, 0, soltemp[0])
err = true
return sol, err
}

96
vendor/github.com/nuknal/goNum/FittingPolynomial.go generated vendored Normal file
View File

@@ -0,0 +1,96 @@
// FittingPolynomial
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-11
版本 : 0.0.0
------------------------------------------------------
多项式拟合
理论:
对于单自变量单因变量的N个数据对
假设其一个低于N-1次的多项式为
y(x) = a0 + a1x + a2x^2 + ... + amx^m (m < N-1)
建立矛盾方程组Ax=b
N
Sum ai*xj^i = bj (i=0, 1, 2, ..., m)
j=1
求解ai (i=0, 1, 2, ..., m)代入多项式即得拟合函数
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 136-138.
------------------------------------------------------
输入 :
xy 单自变量单因变量的N个数据对Nx2
m 多项式次数m < N-1
输出 :
sol 解向量从0到m对应a0到am
RMS 均方误差
MaxErr 最大误差
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
扩展
可以修改为适应log、exp、sin等拟合方法
------------------------------------------------------
*/
package goNum
import (
"math"
)
// FittingPolynomial 多项式拟合
func FittingPolynomial(xy Matrix, m int) (Matrix, float64, float64, bool) {
/*
多项式拟合
输入 :
xy 单自变量单因变量的N个数据对Nx2
m 多项式次数m < N-1
输出 :
sol 解向量从0到m对应a0到am
RMS 均方误差
MaxErr 最大误差
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断m是否小于N-1
if (m > xy.Rows-2) || (m < 0) {
panic("Error in goNum.FittingPolynomial: Order m is wrong number")
}
N := xy.Rows
//构建矛盾方程组系数矩阵A, b=xy.ColumnOfMatrix(1)
A := ZeroMatrix(N, m+1)
for i := 0; i < N; i++ {
A.SetMatrix(i, 0, 1.0)
for j := 1; j < m+1; j++ {
temp := xy.GetFromMatrix(i, 0)
A.SetMatrix(i, j, math.Pow(temp, float64(j)))
}
}
//求解矛盾方程组
sol, err := InconsistentLSQ(A, Slices1ToMatrix(xy.ColumnOfMatrix(1)))
//判断结果
if err != true {
panic("Error in goNum.FittingPolynomial: Solve error")
}
errSub := make([]float64, N)
var RMS float64
for i := 0; i < N; i++ {
fit := sol.Data[0]
for j := 1; j < m+1; j++ {
fit += sol.Data[j] * math.Pow(xy.GetFromMatrix(i, 0), float64(j))
}
errSub[i] = fit - xy.GetFromMatrix(i, 1)
RMS += errSub[i] * errSub[i]
}
RMS = math.Sqrt(RMS)
MaxErr, _, _ := MaxAbs(errSub)
return sol, RMS, math.Abs(MaxErr), err
}

89
vendor/github.com/nuknal/goNum/FittingTriPoly.go generated vendored Normal file
View File

@@ -0,0 +1,89 @@
// FittingTriPoly
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-23
版本 : 0.0.0
------------------------------------------------------
基于傅立叶Fourier级数的三角多项式拟合
理论:
若f(x)周期为2pi则存在M2M<N阶傅立叶Fourier级数
使得N+1个数据对xi等距分布的拟合表示为
a0 M
TM(x) = --- + Sum (aj*cos(jx)+bj*sin(jx))
2 j=1
其中
2 N
aj = ---Sum yk*cos(j*xk), j=0,1,2,...,M
N k=1
2 N
bj = ---Sum yk*sin(j*xk), j=1,2,...,M
N k=1
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 5.4.1
------------------------------------------------------
输入 :
XY 数据对nx2x-y
M 傅立叶级数,< N/2
输出 :
sol 解,(M+1)x2
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// FittingTriPoly 基于傅立叶Fourier级数的三角多项式拟合
func FittingTriPoly(XY Matrix, M int) (Matrix, bool) {
/*
基于傅立叶Fourier级数的三角多项式拟合
输入 :
XY 数据对nx2x-y
M 傅立叶级数,< N/2
输出 :
sol 解,(M+1)x2
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//判断维数
if XY.Columns < 2 {
panic("Error in goNum.FittingTriPoly: At least 2 columns of XY needed")
}
N := XY.Rows
//判断M
if float64(M) >= float64(N)/2.0 {
panic("Error in goNum.FittingTriPoly: M is wrong")
}
sol := ZeroMatrix(M+1, 2) //b0=0.0
var err bool = false
//a0
var a0 float64
for k := 1; k < N; k++ {
// a0 += XY.GetFromMatrix(k, 1) * math.Cos(0.0*XY.GetFromMatrix(k, 0))
a0 += XY.GetFromMatrix(k, 1)
}
sol.SetMatrix(0, 0, 2.0*a0/float64(N))
//aj, bj
for j := 1; j < M+1; j++ {
var aj, bj float64
for k := 1; k < N; k++ {
aj += XY.GetFromMatrix(k, 1) * math.Cos(float64(j)*XY.GetFromMatrix(k, 0))
bj += XY.GetFromMatrix(k, 1) * math.Sin(float64(j)*XY.GetFromMatrix(k, 0))
}
sol.SetMatrix(j, 0, 2.0*aj/float64(N))
sol.SetMatrix(j, 1, 2.0*bj/float64(N))
}
err = true
return sol, err
}

92
vendor/github.com/nuknal/goNum/HeapSort.go generated vendored Normal file
View File

@@ -0,0 +1,92 @@
// HeapSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-06
版本 : 0.0.0
------------------------------------------------------
堆排序法
理论:
时间复杂度: O(nlog2(n))
最好情况 : O(nlog2(n))
最坏情况 : O(nlog2(n))
空间复杂度: O(1)
稳定性 : 不稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// heapSort_maxHeap 建立最大顶堆
func heapSort_maxHeap(sol *Matrix, n *int) {
for i := *n / 2; i >= 0; i-- {
heapSort_heapify(sol, i, n)
}
}
// heapSort_heapify 堆调整
func heapSort_heapify(sol *Matrix, i int, n *int) {
i0 := 2*i + 1
i2 := 2*i + 2
max := i
if i0 < *n && (*sol).Data[i0] > (*sol).Data[i] {
max = i0
}
if i2 < *n && (*sol).Data[i2] > (*sol).Data[i] {
max = i2
}
if max != i {
(*sol).Data[i], (*sol).Data[max] = (*sol).Data[max], (*sol).Data[i]
heapSort_heapify(sol, max, n)
}
}
// HeapSort 堆排序法
func HeapSort(in Matrix) (Matrix, bool) {
/*
堆排序法
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if in.Rows != 1 {
panic("Error in goNum.HeapSort: Input Matrix error")
}
if in.Columns < 1 {
panic("Error in goNum.HeapSort: Empty input Matrix")
} else if in.Columns == 1 {
return in, true
}
n := in.Columns
sol := ZeroMatrix(1, n)
var err bool = false
//初始化sol
for i := 0; i < n; i++ {
sol.Data[i] = in.Data[i]
}
//排序开始
heapSort_maxHeap(&sol, &n)
for i := in.Columns - 1; i > 0; i-- {
sol.Data[0], sol.Data[i] = sol.Data[i], sol.Data[0]
n--
heapSort_heapify(&sol, 0, &n)
}
err = true
return sol, err
}

74
vendor/github.com/nuknal/goNum/InconsistentLSQ.go generated vendored Normal file
View File

@@ -0,0 +1,74 @@
// InconsistentLSQ
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-11
版本 : 0.0.0
------------------------------------------------------
求解矛盾方程组的最小二乘法Least Square Method
理论:
对于矛盾方程组Ax=b
n
Sum aij*xj = bi (i=1, 2, ..., N)
j=1
rank(A) = n (N > n)
则A'Ax=A'b的唯一解为原矛盾方程组的最小二乘解
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 130-135.
------------------------------------------------------
输入 :
A 原方程组系数矩阵Nxn
b 原方程组值向量Nx1
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// InconsistentLSQ 求解矛盾方程组的最小二乘法Least Square Method
func InconsistentLSQ(A, b Matrix) (Matrix, bool) {
/*
求解矛盾方程组的最小二乘法Least Square Method
输入 :
A 原方程组系数矩阵Nxn
b 原方程组值向量Nx1
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断A和b的行数是否对应
if A.Rows != b.Rows {
panic("Error in goNum.InconsistentLSQ: Rows of A and b are not equal")
}
//求解A'A和A'b
AA := DotPruduct(A.Transpose(), A)
Ab := DotPruduct(A.Transpose(), b)
//转换矩阵为切片
Atemp := Matrix2ToSlices(AA)
btemp := Matrix1ToSlices(Ab)
if (len(Atemp) != A.Columns) || (len(Atemp[0]) != A.Columns) || (len(btemp) != A.Columns) {
panic("Error in goNum.InconsistentLSQ: Matrix to slices error")
}
//求解x向量采用列主元消去法LEs_ECPE
soltemp, err := LEs_ECPE(Atemp, btemp)
if err != true {
panic("Error in goNum.InconsistentLSQ: Solve error")
}
//转换切片为矩阵
sol := Slices1ToMatrix(soltemp)
if sol.Rows != A.Columns {
panic("Error in goNum.InconsistentLSQ: Slice to matrix error")
}
return sol, true
}

69
vendor/github.com/nuknal/goNum/InsertSort.go generated vendored Normal file
View File

@@ -0,0 +1,69 @@
// InsertSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-05
版本 : 0.0.0
------------------------------------------------------
插入排序法
理论:
时间复杂度: O(n^2)
最好情况 : O(n)
最坏情况 : O(n^2)
空间复杂度: O(1)
稳定性 : 不稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// InsertSort 插入排序法
func InsertSort(in Matrix) (Matrix, bool) {
/*
插入排序法
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if in.Rows != 1 {
panic("Error in goNum.InsertSort: Input Matrix error")
}
if in.Columns < 1 {
panic("Error in goNum.InsertSort: Empty input Matrix")
} else if in.Columns == 1 {
return in, true
}
n := in.Columns
sol := ZeroMatrix(1, n)
var err bool = false
//初始化sol
for i := 0; i < n; i++ {
sol.Data[i] = in.Data[i]
}
//排序开始
for i := 1; i < n; i++ {
index := i - 1
min := sol.Data[i]
for (index >= 0) && (sol.Data[index] > min) {
sol.Data[index+1] = sol.Data[index]
index--
}
sol.Data[index+1] = min
}
err = true
return sol, err
}

View File

@@ -0,0 +1,96 @@
// IntegralCompositeNewtonCotes
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-12
版本 : 0.0.0
------------------------------------------------------
1-8级复化Newton-Cotes求积分公式
理论:
对于积分
b n
|f(x)dx ~= Sum Ak*f(xk)
a k=0
(n)
Ak = (b-a)C
k
(n) (-1)^(n-k) n
C = ------------ |t(t-1)(t-2)...(t-(k-1))(t-(k+1))...(t-n)dt
k k!(n-k)!n 0
特别的n=1为复化梯形公式
n=2为复化Simpson辛浦生公式
n=4为复化Cotes科特斯公式
将区间[a, b]等分为Nn个子区间每个子区间上使用Newton-Cotes求积分公式
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 155-156.
------------------------------------------------------
输入 :
fun 被积分函数
a, b 积分范围
n Newton-Cotes公式级数
Nn 子区间数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
注意
由于误差得不到有效控制稳定性无法保证故而并不是n值越
大越好实际应用中很少使用n值较大的Newton-Cotes公式
------------------------------------------------------
*/
package goNum
// IntegralCompositeNewtonCotes 1-8级复化Newton-Cotes求积分公式
func IntegralCompositeNewtonCotes(fun func(float64) float64, a, b float64, n, Nn int) (float64, bool) {
/*
1-8级复化Newton-Cotes求积分公式
输入 :
fun 被积分函数
a, b 积分范围
n Newton-Cotes公式级数
Nn 子区间数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if (n < 1) || (n > 8) {
panic("Error in goNum.IntegralNewtonCotes: n is not correct")
}
//判断a, b
if a == b {
return 0.0, true
}
//判断Nn
if Nn < 1 {
panic("Error in goNum.IntegralNewtonCotes: Nn is less than one")
} else if Nn == 1 {
return IntegralNewtonCotes(fun, a, b, n)
}
var sol float64
var err bool = false
//子区间长度
Hh := (b - a) / float64(Nn)
//调用IntegralNewtonCotes循环累加
for i := 1; i < Nn+1; i++ {
soltemp, errtemp := IntegralNewtonCotes(fun, a+Hh*float64(i-1), a+Hh*float64(i), n)
if errtemp != true {
panic("Error in goNum.IntegralNewtonCotes: Error in calling IntegralNewtonCotes")
}
sol += soltemp
}
err = true
return sol, err
}

View File

@@ -0,0 +1,105 @@
// IntegralCompositeNewtonCotesHalf
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-12
版本 : 0.0.0
------------------------------------------------------
1-8级逐次分半复化Newton-Cotes求积分公式
理论:
对于积分
b n
|f(x)dx ~= Sum Ak*f(xk)
a k=0
(n)
Ak = (b-a)C
k
(n) (-1)^(n-k) n
C = ------------ |t(t-1)(t-2)...(t-(k-1))(t-(k+1))...(t-n)dt
k k!(n-k)!n 0
特别的n=1为复化梯形公式
n=2为复化Simpson辛浦生公式
n=4为复化Cotes科特斯公式
将区间[a, b]等分为Nn个子区间每个子区间上使用Newton-Cotes求积分公式
子区间逐次分半以满足精度要求
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 159-160.
------------------------------------------------------
输入 :
fun 被积分函数
a, b 积分范围
tol 精度要求
n Newton-Cotes公式级数
Nmax 最大循环次数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
注意
由于误差得不到有效控制稳定性无法保证故而并不是n值越
大越好实际应用中很少使用n值较大的Newton-Cotes公式
------------------------------------------------------
*/
package goNum
import (
"math"
)
// IntegralCompositeNewtonCotesHalf 1-8级逐次分半复化Newton-Cotes求积分公式
func IntegralCompositeNewtonCotesHalf(fun func(float64) float64, a, b, tol float64, n, Nmax int) (float64, bool) {
/*
1-8级逐次分半复化Newton-Cotes求积分公式
输入 :
fun 被积分函数
a, b 积分范围
tol 精度要求
n Newton-Cotes公式级数
Nmax 最大循环次数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if (n < 1) || (n > 8) {
panic("Error in goNum.IntegralNewtonCotes: n is not correct")
}
//判断a, b
if a == b {
return 0.0, true
}
var sol0, sol1 float64
var err bool = false
var Nn int = 1
//循环
for k := 0; k < Nmax; k++ {
sol1 = 0.0
//子区间长度
Hh := (b - a) / float64(Nn)
//调用IntegralNewtonCotes循环累加
for i := 1; i < Nn+1; i++ {
soltemp, errtemp := IntegralNewtonCotes(fun, a+Hh*float64(i-1), a+Hh*float64(i), n)
if errtemp != true {
panic("Error in goNum.IntegralNewtonCotes: Error in calling IntegralNewtonCotes")
}
sol1 += soltemp
}
if math.Abs(sol1-sol0) < tol {
return sol1, true
}
Nn = 2 * Nn
sol0 = sol1
}
return 0.0, err
}

130
vendor/github.com/nuknal/goNum/IntegralGaussLagendre.go generated vendored Normal file
View File

@@ -0,0 +1,130 @@
// IntegralGaussLagendre
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-12
版本 : 0.0.0
------------------------------------------------------
不超过8次的Gauss-Lagendre求积分公式
理论:
对于积分
b
|f(x)dx
a
使用n+1次Lagendre多项式的零点作为高斯点可获得代数
精度为2n+1的高斯型求积公式
其中区间[a, b]需要预先转换为区间[-1, 1]
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 162-164.
------------------------------------------------------
输入 :
fun 被积分函数
a, b 积分区间
n 求积分公式次数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// IntegralGaussLagendre 不超过8次的Gauss-Lagendre求积分公式
func IntegralGaussLagendre(fun func(float64) float64, a, b float64, n int) (float64, bool) {
/*
不超过8次的Gauss-Lagendre求积分公式
输入 :
fun 被积分函数
a, b 积分区间
n 求积分公式次数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n范围
if (n < 1) || (n > 8) {
panic("Error in goNum.IntegralGaussLagendre: n is a not correct input")
}
xi := [][]float64{
{0.0},
{-0.5773502692, 0.5773502692},
{-0.7745966692, 0.0, 0.7745966692},
{-0.8611363116, -0.3399810436, 0.3399810436, 0.8611363116},
{-0.9061798459, -0.5384693101, 0.0, 0.5384693101, 0.9061798459},
{-0.9324695142, -0.6612093865, -0.2386191861, 0.2386191861, 0.6612093865, 0.9324695142},
{-0.9491079123, -0.7415311856, -0.4058451514, 0.0, 0.4058451514, 0.7415311856, 0.9491079123},
{-0.9602898566, -0.7966664774, -0.5255324099, -0.1834346425, 0.1834346425, 0.5255324099, 0.7966664774, 0.9602898566},
}
Ai := [][]float64{
{2.0},
{1.0, 1.0},
{0.555555555555556, 0.888888888888889, 0.555555555555556},
{0.3478548451, 0.6521451549, 0.6521451549, 0.3478548451},
{0.2369268851, 0.4786286705, 0.568888889, 0.4786286705, 0.2369268851},
{0.1713244924, 0.3607615730, 0.4679139346, 0.4679139346, 0.3607615730, 0.1713244924},
{0.1294849662, 0.2797053915, 0.3818300505, 0.4179591837, 0.3818300505, 0.2797053915, 0.1294849662},
{0.1012285363, 0.2223810345, 0.3137066459, 0.3626837834, 0.3626837834, 0.3137066459, 0.2223810345, 0.1012285363},
}
//区间转换
c := (b - a) / 2.0
d := (a + b) / 2.0
switch n {
case 1:
sol := 0.0
for i := 0; i < len(xi[0]); i++ {
sol += Ai[0][i] * fun(d+c*xi[0][i])
}
return c * sol, true
case 2:
sol := 0.0
for i := 0; i < len(xi[1]); i++ {
sol += Ai[1][i] * fun(d+c*xi[1][i])
}
return c * sol, true
case 3:
sol := 0.0
for i := 0; i < len(xi[2]); i++ {
sol += Ai[2][i] * fun(d+c*xi[2][i])
}
return c * sol, true
case 4:
sol := 0.0
for i := 0; i < len(xi[3]); i++ {
sol += Ai[3][i] * fun(d+c*xi[3][i])
}
return c * sol, true
case 5:
sol := 0.0
for i := 0; i < len(xi[4]); i++ {
sol += Ai[4][i] * fun(d+c*xi[4][i])
}
return c * sol, true
case 6:
sol := 0.0
for i := 0; i < len(xi[5]); i++ {
sol += Ai[5][i] * fun(d+c*xi[5][i])
}
return c * sol, true
case 7:
sol := 0.0
for i := 0; i < len(xi[6]); i++ {
sol += Ai[6][i] * fun(d+c*xi[6][i])
}
return c * sol, true
case 8:
sol := 0.0
for i := 0; i < len(xi[7]); i++ {
sol += Ai[7][i] * fun(d+c*xi[7][i])
}
return c * sol, true
default:
return 0.0, false
}
}

145
vendor/github.com/nuknal/goNum/IntegralNewtonCotes.go generated vendored Normal file
View File

@@ -0,0 +1,145 @@
// IntegralNewtonCotes
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-11
版本 : 0.0.0
------------------------------------------------------
1-8级Newton-Cotes求积分公式
理论:
对于积分
b n
|f(x)dx ~= Sum Ak*f(xk)
a k=0
(n)
Ak = (b-a)C
k
(n) (-1)^(n-k) n
C = ------------ |t(t-1)(t-2)...(t-(k-1))(t-(k+1))...(t-n)dt
k k!(n-k)!n 0
特别的n=1为梯形公式
n=2为Simpson辛浦生公式
n=4为Cotes科特斯公式
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 145-153.
------------------------------------------------------
输入 :
fun 被积分函数
a, b 积分范围
n Newton-Cotes公式级数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
注意
由于误差得不到有效控制稳定性无法保证故而并不是n值越
大越好实际应用中很少使用n值较大的Newton-Cotes公式
------------------------------------------------------
*/
package goNum
// IntegralNewtonCotes 1-8级Newton-Cotes求积分公式
func IntegralNewtonCotes(fun func(float64) float64, a, b float64, n int) (float64, bool) {
/*
1-8级Newton-Cotes求积分公式
输入 :
fun 被积分函数
a, b 积分范围
n Newton-Cotes公式级数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if (n < 1) || (n > 8) {
panic("Error in goNum.IntegralNewtonCotes: n is not correct")
}
//判断a, b
if a == b {
return 0.0, true
}
var sol float64
var err bool = false
//计算xi
xi := ZeroMatrix(n+1, 1)
for i := 0; i < n+1; i++ {
xi.Data[i] = a + (b-a)*float64(i)/float64(n)
}
//系数切片
coeff := [][]float64{
{1.0, 1.0},
{1.0, 4.0, 1.0},
{1.0, 3.0, 3.0, 1.0},
{7.0, 32.0, 12.0, 32.0, 7.0},
{19.0, 75.0, 50.0, 50.0, 75.0, 19.0},
{41.0, 216.0, 27.0, 272.0, 27.0, 216.0, 41.0},
{751.0, 3577.0, 1323.0, 2989.0, 2989.0, 1323.0, 3577.0, 751.0},
{989.0, 5888.0, -928.0, 10496.0, -4540.0, 10496.0, -928.0, 5888.0, 989.0},
}
//计算积分值
switch n {
case 1:
for i := 0; i < n+1; i++ {
sol += coeff[0][i] * fun(xi.Data[i])
}
sol = sol * (b - a) / 2.0
return sol, true
case 2:
for i := 0; i < n+1; i++ {
sol += coeff[1][i] * fun(xi.Data[i])
}
sol = sol * (b - a) / 6.0
return sol, true
case 3:
for i := 0; i < n+1; i++ {
sol += coeff[2][i] * fun(xi.Data[i])
}
sol = sol * (b - a) / 8.0
return sol, true
case 4:
for i := 0; i < n+1; i++ {
sol += coeff[3][i] * fun(xi.Data[i])
}
sol = sol * (b - a) / 90.0
return sol, true
case 5:
for i := 0; i < n+1; i++ {
sol += coeff[4][i] * fun(xi.Data[i])
}
sol = sol * (b - a) / 288.0
return sol, true
case 6:
for i := 0; i < n+1; i++ {
sol += coeff[5][i] * fun(xi.Data[i])
}
sol = sol * (b - a) / 840.0
return sol, true
case 7:
for i := 0; i < n+1; i++ {
sol += coeff[6][i] * fun(xi.Data[i])
}
sol = sol * (b - a) / 17280.0
return sol, true
case 8:
for i := 0; i < n+1; i++ {
sol += coeff[7][i] * fun(xi.Data[i])
}
sol = sol * (b - a) / 28350.0
return sol, true
default:
return 0.0, err
}
return sol, err
}

132
vendor/github.com/nuknal/goNum/IntegralRumberg.go generated vendored Normal file
View File

@@ -0,0 +1,132 @@
// IntegralRumberg
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-12
版本 : 0.0.0
------------------------------------------------------
Rumberg(龙贝格)求积分公式
理论:
对于积分
b
|f(x)dx
a
b-a
T1 = -----(f(a)+f(b))
2
1 b-a N b-a
T2N = ---TN + -----Sum f(a+(2j-1)------)
2 2N j=1 2N
N=2^(k-1), k=1,2,3,...
1 4T2N-TN
SN = T2N + ---(T2N-TN) = --------
3 4-1
1 4^2S2N-SN
CN = S2N + ----(S2N-SN) = -----------
15 4^2-1
1 4^3C2N-CN
RN = C2N + ----(C2N-CN) = -----------
63 4^3-1
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 162-164.
------------------------------------------------------
输入 :
fun 被积分函数
a, b 积分范围
tol 控制误差
Nn 最大循环步数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// IntegralRumberg Rumberg(龙贝格)求积分公式
func IntegralRumberg(fun func(float64) float64, a, b, tol float64, Nn int) (float64, bool) {
/*
Rumberg(龙贝格)求积分公式
输入 :
fun 被积分函数
a, b 积分范围
tol 控制误差
Nn 最大循环步数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
T := make([]float64, 0) //梯形序列
S := make([]float64, 0) //辛浦生序列
C := make([]float64, 0) //柯特斯序列
R := make([]float64, 0) //龙贝格序列
//第一步
temp0 := (b - a) * (fun(a) + fun(b)) / 2.0
T = append(T, temp0) //T[0]=T1
//第二步, k=1
temp0 = 0.0
for j := 1; j < PowIInt(2, 0)+1; j++ {
temp0 += fun(a + (2.0*float64(j)-1.0)*(b-a)/2.0)
}
temp0 = T[0]/2.0 + temp0*(b-a)/2.0
T = append(T, temp0) //T[1]=T2
temp1 := T[1] + (T[1]-T[0])/3.0
S = append(S, temp1) //S[0]=S1
//第三步, k=2
temp0 = 0.0
for j := 1; j < PowIInt(2, 1)+1; j++ {
temp0 += fun(a + (2.0*float64(j)-1.0)*(b-a)/(2.0*2.0))
}
temp0 = T[1]/2.0 + temp0*(b-a)/(2.0*2.0)
T = append(T, temp0) //T[2]=T4
temp1 = T[2] + (T[2]-T[1])/3.0
S = append(S, temp1) //S[1]=S2
temp2 := S[1] + (S[1]-S[0])/15.0
C = append(C, temp2) //C[0]=C1
//第四步, k=3
temp0 = 0.0
for j := 1; j < PowIInt(2, 2)+1; j++ {
temp0 += fun(a + (2.0*float64(j)-1.0)*(b-a)/(2.0*4.0))
}
temp0 = T[2]/2.0 + temp0*(b-a)/(2.0*4.0)
T = append(T, temp0) //T[3]=T8
temp1 = T[3] + (T[3]-T[2])/3.0
S = append(S, temp1) //S[2]=S4
temp2 = S[2] + (S[2]-S[1])/15.0
C = append(C, temp2) //C[1]=C2
temp3 := C[1] + (C[1]-C[0])/63.0
R = append(R, temp3) //R[0]=R1
//进入Rumberg循环
for i := 1; i < Nn; i++ {
temp0 = 0.0
for j := 1; j < PowIInt(2, i+2)+1; j++ {
temp0 += fun(a + (2.0*float64(j)-1.0)*(b-a)/(2.0*PowIIntF(2, i+2)))
}
temp0 = T[i+2]/2.0 + temp0*(b-a)/(2.0*PowIIntF(2, i+2))
T = append(T, temp0) //T[i+3]
temp1 = T[i+3] + (T[i+3]-T[i+2])/3.0
S = append(S, temp1) //S[i+2]
temp2 = S[i+2] + (S[i+2]-S[i+1])/15.0
C = append(C, temp2) //C[i+1]
temp3 = C[i+1] + (C[i+1]-C[i])/63.0
R = append(R, temp3) //R[i]
if math.Abs(R[i]-R[i-1]) < tol {
return R[i], true
}
}
return 0.0, false
}

99
vendor/github.com/nuknal/goNum/InterpHermite.go generated vendored Normal file
View File

@@ -0,0 +1,99 @@
// InterpHermite
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-7
版本 : 0.0.0
------------------------------------------------------
计算x点不高于2n+1次Hermite插值结果拟合n+1个函数值数据
点和对应的n+1个一阶导数点
满阶插值即阶数不高于2n+1
理论:
n
H2n+1(x) = Sum (alphaj(x)*yj+betaj(x)*mj)
j=0
yj, mj分别为函数值和一阶导数值
n 1
alphaj(x) = (1-2(x-xj)*Sum -------)lj^2(x)
k=0,k!=j xj-xk
betaj(x) = (x-xj)lj^2(x)
(x-x0)(x-x1)...(x-xn)
lj(x) = --------------------------, (被减数不含xj项)
(xj-x0)(xj-x1)...(xj-xn)
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 111-113.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi第三列y'i
xq 插值点, xq!=xi
输出 :
sol xq点插值结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
//求解lj(xq)
func ljxq_InterpHermite(A Matrix, xq float64, j int) float64 {
sol := 1.0
xj := A.GetFromMatrix(j, 0)
for i := 0; i < A.Rows; i++ {
xi := A.GetFromMatrix(i, 0)
if i != j {
sol = sol * (xq - xi) / (xj - xi)
}
}
return sol
}
//求解alplhaj(xq)
func alphajxq_InterpHermite(A Matrix, xq float64, j int) float64 {
var temp0 float64
xj := A.GetFromMatrix(j, 0)
for k := 0; k < A.Rows; k++ {
if k != j {
temp0 += 1.0 / (xj - A.GetFromMatrix(k, 0))
}
}
temp1 := ljxq_InterpHermite(A, xq, j)
return (1.0 - 2.0*(xq-xj)*temp0) * temp1 * temp1
}
//求解betaj(xq)
func betajxq_InterpHermite(A Matrix, xq float64, j int) float64 {
xj := A.GetFromMatrix(j, 0)
temp0 := ljxq_InterpHermite(A, xq, j)
return (xq - xj) * temp0 * temp0
}
// InterpHermite 计算x点不高于2n+1次Hermite插值结果拟合n+1个函数值数据点和对应的n+1个一阶导数点
func InterpHermite(A Matrix, xq float64) (float64, bool) {
/*
计算x点不高于2n+1次Hermite插值结果拟合n+1个函数值数据点和对应的n+1个一阶导数点
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi第三列y'i
xq 插值点, xq!=xi
输出 :
sol xq点插值结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断A列数是否为3
if A.Columns != 3 {
panic("Error in goNum.InterpHermite: give me xi, yi and y'i")
}
var sol float64
var err bool = false
//开始计算
for j := 0; j < A.Rows; j++ {
sol += alphajxq_InterpHermite(A, xq, j) * A.GetFromMatrix(j, 1)
sol += betajxq_InterpHermite(A, xq, j) * A.GetFromMatrix(j, 2)
}
err = true
return sol, err
}

175
vendor/github.com/nuknal/goNum/InterpHermiteFunc.go generated vendored Normal file
View File

@@ -0,0 +1,175 @@
// InterpHermiteFunc
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-7
版本 : 0.0.0
------------------------------------------------------
计算不高于2n+1次Hermite插值方程拟合n+1个函数值数据
点和对应的n+1个一阶导数点
满阶插值即阶数不高于2n+1
理论:
n
H2n+1(x) = Sum (alphaj(x)*yj+betaj(x)*mj)
j=0
yj, mj分别为函数值和一阶导数值
n 1
alphaj(x) = (1-2(x-xj)*Sum -------)lj^2(x)
k=0,k!=j xj-xk
betaj(x) = (x-xj)lj^2(x)
(x-x0)(x-x1)...(x-xn)
lj(x) = --------------------------, (被减数不含xj项)
(xj-x0)(xj-x1)...(xj-xn)
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 111-113.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi第三列y'i
输出 :
B 插值方程系数结果从前到后对应从0到2n+1阶(2n+2)x1
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
//求解lj(x)
func ljx_InterpHermiteFunc(A Matrix, j int) Matrix {
Bljx := ZeroMatrix(A.Rows, 1) //出去j加常数项总共n+1
xj := A.GetFromMatrix(j, 0)
// j!=0
temp0 := (xj - A.GetFromMatrix(0, 0)) //x0
Bljx.Data[0] = -1.0 * A.GetFromMatrix(0, 0)
Bljx.Data[1] = 1.0
// j==0
if j == 0 {
temp0 = (xj - A.GetFromMatrix(1, 0)) //x1
Bljx.Data[0] = -1.0 * A.GetFromMatrix(1, 0)
}
//其它
for i := 1; i < A.Rows; i++ {
if (i != j) && (((j == 0) && (i > 1)) || (j > 0)) {
if i < j {
CA := ZeroMatrix(i+2, 1) //实际i+2行
CB := ZeroMatrix(i+2, 1) //实际i+1行
//先用x乘以之前每一项相当于给每一项提升一阶,i+1
//再用-xi乘以B的每一有效项,i+1
CB.Data[0] = -1.0 * A.GetFromMatrix(i, 0) * Bljx.Data[0]
for ii := 1; ii < i+1; ii++ {
//单列可以这样否则只能用SetMatrix和GetFromMatrix方法
CA.Data[ii] = Bljx.Data[ii-1]
CB.Data[ii] = -1.0 * A.GetFromMatrix(i, 0) * Bljx.Data[ii]
}
CA.Data[i+1] = Bljx.Data[i]
//同阶相加赋予B
for ii := 0; ii < i+2; ii++ {
Bljx.Data[ii] = CA.Data[ii] + CB.Data[ii]
}
} else { //i>j
CA := ZeroMatrix(i+1, 1) //实际i+1行
CB := ZeroMatrix(i+1, 1) //实际i行
//先用x乘以之前每一项相当于给每一项提升一阶,i+1
//再用-xi乘以B的每一有效项,i
CB.Data[0] = -1.0 * A.GetFromMatrix(i, 0) * Bljx.Data[0]
for ii := 1; ii < i; ii++ {
//单列可以这样否则只能用SetMatrix和GetFromMatrix方法
CA.Data[ii] = Bljx.Data[ii-1]
CB.Data[ii] = -1.0 * A.GetFromMatrix(i, 0) * Bljx.Data[ii]
}
CA.Data[i] = Bljx.Data[i-1]
//同阶相加赋予B
for ii := 0; ii < i+1; ii++ {
Bljx.Data[ii] = CA.Data[ii] + CB.Data[ii]
}
}
temp0 = temp0 * (xj - A.GetFromMatrix(i, 0))
}
}
for i := 0; i < Bljx.Rows; i++ {
Bljx.Data[i] = Bljx.Data[i] / temp0
}
return Bljx
}
//求解ljx^2
func ljx2_InterpHermiteFunc(A Matrix, j int) Matrix {
ljx := ljx_InterpHermiteFunc(A, j) //n+1 rows
BA := ZeroMatrix(2*ljx.Rows-1, 1) //2n+1 rows
for i := ljx.Rows - 1; i >= 0; i-- {
for j := ljx.Rows - 1; j >= 0; j-- {
BA.Data[i+j] = BA.Data[i+j] + ljx.Data[i]*ljx.Data[j]
}
}
return BA
}
//求解alphajx和betajx合并是为了减少对ljx2_InterpHermiteFunc的调用
func alphabetajx_InterpHermiteFunc(A Matrix, j int) (Matrix, Matrix) {
var temp0 float64
ljx2 := ljx2_InterpHermiteFunc(A, j) //2n+1 rows
alphajx := ZeroMatrix(ljx2.Rows+1, 1) //2n+2 rows
betajx := ZeroMatrix(ljx2.Rows+1, 1) //2n+2 rows
xj := A.GetFromMatrix(j, 0)
//计算alphajx中的求和
for k := 0; k < A.Rows; k++ {
if k != j {
temp0 += 1.0 / (xj - A.GetFromMatrix(k, 0))
}
}
temp0 = 2.0 * temp0
//alphajx = (temp0*xj+1 - temp0*x) * ljx2
//betajx = (x-xj) * ljx2
//2n+1阶,2n+2行
alphajx.Data[alphajx.Rows-1] = -1.0 * temp0 * ljx2.Data[ljx2.Rows-1]
betajx.Data[betajx.Rows-1] = ljx2.Data[ljx2.Rows-1]
//其它非零阶, alphajx.Rows-2 == betajx.Rows-2 == ljx2.Rows-1
for i := alphajx.Rows - 2; i > 0; i-- {
alphajx.Data[i] = (temp0*xj+1.0)*ljx2.Data[i] - 1.0*temp0*ljx2.Data[i-1]
betajx.Data[i] = -1.0*xj*ljx2.Data[i] + ljx2.Data[i-1]
}
//零阶
alphajx.Data[0] = (temp0*xj + 1.0) * ljx2.Data[0]
betajx.Data[0] = -1.0 * xj * ljx2.Data[0]
return alphajx, betajx
}
// InterpHermiteFunc 计算不高于2n+1次Hermite插值方程拟合n+1个函数值数据点和对应的n+1个一阶导数点
func InterpHermiteFunc(A Matrix) (Matrix, bool) {
/*
计算不高于2n+1次Hermite插值方程拟合n+1个函数值数据点和对应的n+1个一阶导数点
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi第三列y'i
输出 :
B 插值方程系数结果从前到后对应从0到2n+1阶(2n+2)x1
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断A列数是否为3
if A.Columns != 3 {
panic("Error in goNum.InterpHermite: give me xi, yi and y'i")
}
var err bool = false
n := A.Rows - 1
BA := ZeroMatrix(2*n+2, 1)
for j := 0; j <= n; j++ {
alphajx, betajx := alphabetajx_InterpHermiteFunc(A, j)
for i := 0; i < alphajx.Rows; i++ {
BA.Data[i] = BA.Data[i] + alphajx.Data[i]*A.GetFromMatrix(j, 1)
BA.Data[i] = BA.Data[i] + betajx.Data[i]*A.GetFromMatrix(j, 2)
}
}
err = true
return BA, err
}

79
vendor/github.com/nuknal/goNum/InterpLagrange.go generated vendored Normal file
View File

@@ -0,0 +1,79 @@
// InterpLagrange
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-3
版本 : 0.0.0
------------------------------------------------------
求解n次拉格朗日Lagrange插值法拟合n+1个数据点
满阶插值,即阶数为给定点数-1
内插/外插
理论:
n omega0n+1(xq)
Ln(xq) = Sum(-----------------------)
k=0 (xq-xk)*omega1n+1(xk)
n
omega0n+1(xq) = Prod(xq-xi)
i=0
n
omega1n+1(xk) = Prod (xk-xi)
i=0,i!=k
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 94-100.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x2第一列xi第二列yi
xq 插值点
n 最大插值阶数 1 <= ... <= n
输出 :
sol 插值结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// InterpLagrange 求解n次拉格朗日Lagrange插值法拟合n+1个数据点
func InterpLagrange(A Matrix, xq float64) (float64, bool) {
/*
求解n次拉格朗日Lagrange插值法拟合n+1个数据点
输入 :
A 数据点矩阵,(n+1)x2第一列xi第二列yi
xq 插值点
n 最大插值阶数 1 <= ... <= n
输出 :
sol 插值结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var err bool = false
n := A.Rows - 1
//计算系数矩阵
for k := 0; k <= n; k++ {
//1. 计算分子
var temp0 float64 = 1.0
for i := 0; i <= n; i++ {
temp0 = temp0 * (xq - A.GetFromMatrix(i, 0))
}
temp0 = temp0 / (xq - A.GetFromMatrix(k, 0))
//2. 计算分母
var temp1 float64 = 1.0
for i := 0; i <= n; i++ {
if i != k {
temp1 = temp1 * (A.GetFromMatrix(k, 0) - A.GetFromMatrix(i, 0))
}
}
//3. 求和
sol += temp0 * A.GetFromMatrix(k, 1) / temp1
}
err = true
return sol, err
}

140
vendor/github.com/nuknal/goNum/InterpLagrangeFunc.go generated vendored Normal file
View File

@@ -0,0 +1,140 @@
// InterpLagrangeFunc
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-4
版本 : 0.0.0
------------------------------------------------------
求解n次拉格朗日Lagrange插值方程系数拟合n+1个数据点
满阶插值,即阶数为给定点数-1因不能确定非满阶时所选取的
插值点是否合理)
理论:
n omega0n+1(x)
Ln(x) = Sum(----------------------)
k=0 (x-xk)*omega1n+1(xk)
n
omega0n+1(x) = Prod(x-xi)
i=0
n
omega1n+1(xk) = Prod (xk-xi)
i=0,i!=k
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 94-100.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x2第一列xi第二列yi
输出 :
B 插值系数矩阵,(n+1)x10n
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
//计算分母
func omega1_InterpLagrangeFunc(A Matrix, k, n int) float64 {
var sol float64 = 1.0
for i := 0; i <= n; i++ {
if i != k {
sol = sol * (A.GetFromMatrix(k, 0) - A.GetFromMatrix(i, 0))
}
}
return sol
}
//计算分子并由高阶到低阶排序
func omega0_InterpLagrangeFunc(A Matrix, k, n int) Matrix {
B := ZeroMatrix(n+1, 1)
//第零阶 x-x0
if k == 0 { //如果k==0则从x1循环
B.SetMatrix(0, 0, -1.0*A.GetFromMatrix(1, 0))
B.SetMatrix(1, 0, 1.0)
}
if k > 0 { //如果k>0则从x0循环
B.SetMatrix(0, 0, -1.0*A.GetFromMatrix(0, 0))
B.SetMatrix(1, 0, 1.0)
}
//其他i!=k阶
for i := 1; i <= n; i++ {
if (i != k) && ((k > 0) || ((k == 0) && (i > 1))) {
if k < i {
CA := ZeroMatrix(i+1, 1) //实际i+1行
CB := ZeroMatrix(i+1, 1) //实际i行
//先用x乘以之前每一项相当于给每一项提升一阶,i+1
for ii := 1; ii < i+1; ii++ {
//单列可以这样否则只能用SetMatrix和GetFromMatrix方法
CA.Data[ii] = B.Data[ii-1]
}
//再用-xi乘以B的每一有效项,i
for ii := 0; ii < i; ii++ {
//单列可以这样否则只能用SetMatrix和GetFromMatrix方法
CB.Data[ii] = -1.0 * A.GetFromMatrix(i, 0) * B.Data[ii]
}
//同阶相加赋予B
for ii := 0; ii < i+1; ii++ {
B.Data[ii] = CA.Data[ii] + CB.Data[ii]
}
} else { // k > i
CA := ZeroMatrix(i+2, 1) //实际i+2行
CB := ZeroMatrix(i+2, 1) //实际i+1行
//先用x乘以之前每一项相当于给每一项提升一阶,i+1
for ii := 1; ii < i+2; ii++ {
//单列可以这样否则只能用SetMatrix和GetFromMatrix方法
CA.Data[ii] = B.Data[ii-1]
}
//再用-xi乘以B的每一有效项,i+1
for ii := 0; ii < i+1; ii++ {
//单列可以这样否则只能用SetMatrix和GetFromMatrix方法
CB.Data[ii] = -1.0 * A.GetFromMatrix(i, 0) * B.Data[ii]
}
//同阶相加赋予B
for ii := 0; ii < i+2; ii++ {
B.Data[ii] = CA.Data[ii] + CB.Data[ii]
}
}
}
}
return B
}
// InterpLagrangeFunc 求解n次拉格朗日Lagrange插值方程系数拟合n+1个数据点
func InterpLagrangeFunc(A Matrix) (Matrix, bool) {
/*
求解n次拉格朗日Lagrange插值方程系数拟合n+1个数据点
输入 :
A 数据点矩阵,(n+1)x2第一列xi第二列yi
输出 :
B 插值系数矩阵,(n+1)x1
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//阶数
n := A.Rows - 1
B := ZeroMatrix(n+1, 1) //最终系数矩阵
var err bool = false
//计算系数矩阵
for k := 0; k <= n; k++ {
//1. 计算分母和系数乘积
temp0 := A.GetFromMatrix(k, 1) / omega1_InterpLagrangeFunc(A, k, n)
//2. 计算分子并乘以上一步的结果,由高阶到低阶排序
temp1 := omega0_InterpLagrangeFunc(A, k, n)
for i := 0; i < temp1.Rows; i++ {
temp1.Data[i] = temp1.Data[i] * temp0
}
//3. 累加
for i := 0; i < B.Rows; i++ {
B.Data[i] += temp1.Data[i]
}
}
err = true
return B, err
}

95
vendor/github.com/nuknal/goNum/InterpNewton.go generated vendored Normal file
View File

@@ -0,0 +1,95 @@
// InterpNewton
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-6
版本 : 0.0.0
------------------------------------------------------
计算x点n次Newton插值结果拟合n+1个数据点
满阶插值,即阶数为给定点数-1
理论:
f(x) = f(x0) + f[x, x0](x-x0)
f[x, x0] = f[x0, x1] + f[x, x0, x1](x-x1)
...
f(x) = f(x0) + f[x0, x0](x-x0) +
f[x0, x1, x2](x-x0)(x-x1) +
... +
f[x0, x1, ..., xn](x-x0)(x-x1)...(x-x_(n-1))
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 101-105.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x2第一列xi第二列yi
xq 插值点, xq!=xi
输出 :
sol xq点插值结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
//求差商
func diffq_InterpNewton(A Matrix, k int) float64 {
var sol float64
for j := 0; j <= k; j++ {
xj := A.GetFromMatrix(j, 0)
//为保证理论可读性并不采取调用omega1_InterpLagrangeFunc函数的方式
var temp0 float64 = 1.0
for i := 0; i <= k; i++ {
if i != j {
temp0 = temp0 * (xj - A.GetFromMatrix(i, 0))
}
}
sol += A.GetFromMatrix(j, 1) / temp0
}
return sol
}
// InterpNewton 计算x点n次Newton插值结果拟合n+1个数据点
func InterpNewton(A Matrix, xq float64) (float64, bool) {
/*
计算x点n次Newton插值结果拟合n+1个数据点
输入 :
A 数据点矩阵,(n+1)x2第一列xi第二列yi
xq 插值点, xq!=xi
输出 :
sol xq点插值结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断xq是否等于xi
for i := 0; i < A.Rows; i++ {
if math.Abs(xq-A.GetFromMatrix(i, 0)) < 1e-3 {
panic("Error in goNum.InterpNewton: xq equals about xi")
}
}
var sol float64
var err bool = false
n := A.Rows - 1
BA := ZeroMatrix(n+1, 1)
//开始计算
BA.SetMatrix(0, 0, A.GetFromMatrix(0, 1)) //f(x0)
sol = BA.GetFromMatrix(0, 0)
for k := 1; k < n+1; k++ {
//求差商
BA.SetMatrix(k, 0, diffq_InterpNewton(A, k))
//求乘积
for j := 0; j < k; j++ {
BA.Data[k] = BA.Data[k] * (xq - A.GetFromMatrix(j, 0))
}
//累加
sol += BA.Data[k]
}
err = true
return sol, err
}

94
vendor/github.com/nuknal/goNum/InterpNewtonForward.go generated vendored Normal file
View File

@@ -0,0 +1,94 @@
// InterpNewtonForward
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-6
版本 : 0.0.0
------------------------------------------------------
计算x点n次Newton前向插值结果拟合n+1个等距数据点
Newton前向等距节点插值满阶插值即阶数为给定点数-1
理论:
(-1)^y0 (-1)^2y0
f(x) = f(x0) + --------(x-x0)/h + ---------(x-x0)(x-x1) +
h 2!h^2
... +
(-1)^ny0
----------(x-x0)(x-x1)...(x-x_(n-1))
n!h^n
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 107-110.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x2第一列xi等距分布第二列yi
xq 插值点, xq!=xi
输出 :
sol xq点插值结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import "math"
//k阶差分
func difff_InterpNewtonForward(A Matrix, k int) float64 {
sol := A.GetFromMatrix(k, 1) //yk
for s := 1; s <= k; s++ {
sol += math.Pow(-1.0, float64(s)) * float64(Cnm(k, s)) * A.GetFromMatrix(k-s, 1)
}
return sol
}
// InterpNewtonForward 计算x点n次Newton前向插值结果拟合n+1个等距数据点
func InterpNewtonForward(A Matrix, xq float64) (float64, bool) {
/*
计算x点n次Newton前向插值结果拟合n+1个等距数据点
输入 :
A 数据点矩阵,(n+1)x2第一列xi等距分布第二列yi
xq 插值点, xq!=xi
输出 :
sol xq点插值结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断xq是否等于xi
for i := 0; i < A.Rows; i++ {
if math.Abs(xq-A.GetFromMatrix(i, 0)) < 1e-3 {
return A.GetFromMatrix(i, 1), true
}
}
//判断xi是否等距节点
for i := 0; i < A.Rows; i++ {
x0 := A.GetFromMatrix(0, 0)
if math.Abs(A.GetFromMatrix(i, 0)-float64(i)*x0) < 1e-3 {
panic("Error in goNum.InterpNewtonForward: xi is not in equidistance")
}
}
var sol float64
var err bool = false
n := A.Rows - 1
h := A.GetFromMatrix(n, 0) - A.GetFromMatrix(n-1, 0)
BA := ZeroMatrix(n+1, 1)
//计算
BA.SetMatrix(0, 0, A.GetFromMatrix(0, 1)) //f(x0)
sol = BA.GetFromMatrix(0, 0)
for k := 1; k < n+1; k++ {
//求差分
BA.SetMatrix(k, 0, difff_InterpNewtonForward(A, k))
//乘系数1/(k!h^k)
BA.Data[k] = BA.Data[k] / (float64(Factorial(k)) * math.Pow(h, float64(k)))
//求乘积
for j := 0; j < k; j++ {
BA.Data[k] = BA.Data[k] * (xq - A.GetFromMatrix(j, 0))
}
//累加
sol += BA.Data[k]
}
err = true
return sol, err
}

217
vendor/github.com/nuknal/goNum/InterpSpline11.go generated vendored Normal file
View File

@@ -0,0 +1,217 @@
// InterpSpline11
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-8
版本 : 0.0.0
------------------------------------------------------
用节点处的一阶导数表示的三次样条插值函数,
一阶导数边界条件
n+1个点, n个区间
理论:
区间[x(i-1), xi]上的三次样条函数表达为:
(x-xi)^2 * [hi+2(x-x(i-1))]
Si(x) = -----------------------------y(i-1) +
hi^3
(x-x(i-1))^2 * [hi+2(xi-x)]
-----------------------------yi +
hi^3
(x-xi)^2 * (x-x(i-1))
-----------------------m(i-1) +
hi^2
(x-x(i-1))^2 * (x-xi)
-----------------------mi
hi^2
令 lambdai = h(i+1)/(hi+h(i+1))
Mi = 1-lambdai = hi/(hi+h(i+1))
y(i+1)-yi yi-y(i-1)
fi = 3(Mi---------- + lambdai-----------)
h(i+1) hi
(i = 1,...,n-1)
则mi可由n-1阶线性方程组求得利用LEs_Chasing
|2 M1 || m1 | | f1-l1*m0 |
| l2 2 M2 || m2 | = | f2 |
| ........ || ... | | ... |
| l(n-2) 2 M(n-2)||m(n-2)| | f(n-2) |
| l(n-1) 2 ||m(n-1)| |f(n-1)-M(n-1)mn|
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 116-123.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi
第三列y'i且y'i只需给出y'0和y'n
输出 :
B 插值方程系数结果矩阵从前到后对应从0到3阶4xn
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// InterpSpline11 用节点处的一阶导数表示的三次样条插值函数, 一阶导数边界条件
func InterpSpline11(A Matrix) (Matrix, bool) {
/*
用节点处的一阶导数表示的三次样条插值函数, 一阶导数边界条件
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi
第三列y'i且y'i只需给出y'0和y'n
输出 :
B 插值方程系数结果矩阵从前到后对应从0到3阶4xn
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var err bool = false
n := A.Rows - 1
sol := ZeroMatrix(4, n)
BA := ZeroMatrix(n-1, n-1) //对角占优的三对角矩阵
BB := ZeroMatrix(n-1, 1) //解向量
BC := ZeroMatrix(n-1, 1) //值向量
//1解插值函数的一阶导数mi
//1.0.1第一行
if true { //限制变量使用范围
h1 := A.GetFromMatrix(1, 0) - A.GetFromMatrix(0, 0)
h2 := A.GetFromMatrix(2, 0) - A.GetFromMatrix(1, 0)
y0 := A.GetFromMatrix(0, 1)
y1 := A.GetFromMatrix(1, 1)
y2 := A.GetFromMatrix(2, 1)
l1 := h2 / (h1 + h2)
M1 := 1.0 - l1
f1 := 3.0 * (M1*(y2-y1)/h2 + l1*(y1-y0)/h1)
BA.SetMatrix(0, 0, 2.0)
BA.SetMatrix(0, 1, M1)
BC.Data[0] = f1 - l1*A.GetFromMatrix(0, 2)
}
//1.0.2其它行
for i := 2; i < n-1; i++ {
yi_1 := A.GetFromMatrix(i-1, 0)
yi := A.GetFromMatrix(i, 0)
yi1 := A.GetFromMatrix(i+1, 0)
hi := A.GetFromMatrix(i, 0) - A.GetFromMatrix(i-1, 0)
hi1 := A.GetFromMatrix(i+1, 0) - A.GetFromMatrix(i, 0)
lambdai := hi1 / (hi + hi1)
Mi := 1.0 - lambdai
fi := 3.0 * (Mi*(yi1-yi)/hi1 + lambdai*(yi-yi_1)/hi)
//赋予BA
BA.SetMatrix(i-1, i-2, lambdai)
BA.SetMatrix(i-1, i-1, 2.0)
BA.SetMatrix(i-1, i, Mi)
BC.Data[i-1] = fi
}
//1.0.3最后一行
if true { //i=n-1
hn_1 := A.GetFromMatrix(n-1, 0) - A.GetFromMatrix(n-2, 0)
hn := A.GetFromMatrix(n, 0) - A.GetFromMatrix(n-1, 0)
yn_2 := A.GetFromMatrix(n-2, 1)
yn_1 := A.GetFromMatrix(n-1, 1)
yn := A.GetFromMatrix(n, 1)
lambdan_1 := hn / (hn_1 + hn)
Mn_1 := 1.0 - lambdan_1
fn_1 := 3.0 * (Mn_1*(yn-yn_1)/hn + lambdan_1*(yn_1-yn_2)/hn_1)
BA.SetMatrix(n-2, n-3, lambdan_1)
BA.SetMatrix(n-2, n-2, 2.0)
BC.Data[n-2] = fn_1 - Mn_1*A.GetFromMatrix(n, 2)
}
//1.1求解
soltemp, errtemp := LEs_Chasing(BA, BC)
if errtemp != true {
panic("Error in goNum.InterpSpline11: Solve Error with goNum.LEs_Chasing")
}
for i := 0; i < n-1; i++ {
BB.Data[i] = soltemp.Data[i]
}
//2求解Si(x)
S0 := ZeroMatrix(4, 1)
S1 := ZeroMatrix(4, 1)
S2 := ZeroMatrix(4, 1)
S3 := ZeroMatrix(4, 1)
for i := 1; i < n+1; i++ {
xi_1 := A.GetFromMatrix(i-1, 0)
xi := A.GetFromMatrix(i, 0)
yi_1 := A.GetFromMatrix(i-1, 1)
yi := A.GetFromMatrix(i, 1)
mi_1 := 0.0
mi := 0.0
if i == 1 {
mi_1 = A.GetFromMatrix(0, 2)
mi = BB.Data[i-1]
} else if i == n {
mi_1 = BB.Data[i-2]
mi = A.GetFromMatrix(n, 2)
} else {
mi_1 = BB.Data[i-2]
mi = BB.Data[i-1]
}
hi := xi - xi_1
temp0 := ZeroMatrix(4, 1)
temp1 := ZeroMatrix(4, 1)
//2.1 S0
temp0.Data[2] = 1.0
temp0.Data[1] = -2.0 * xi
temp0.Data[0] = xi * xi
for j := 3; j > 0; j-- {
temp0.Data[j] = 2.0 * temp0.Data[j-1]
temp1.Data[j-1] = (hi - 2.0*xi_1) * temp0.Data[j-1]
S0.Data[j] = (temp0.Data[j] + temp1.Data[j]) * yi_1 / math.Pow(hi, 3.0)
}
S0.Data[0] = temp1.Data[0] * yi_1 / math.Pow(hi, 3.0)
//2.1 S1
temp0 = ZeroMatrix(4, 1)
temp1 = ZeroMatrix(4, 1)
temp0.Data[2] = 1.0
temp0.Data[1] = -2.0 * xi_1
temp0.Data[0] = xi_1 * xi_1
for j := 3; j > 0; j-- {
temp0.Data[j] = -2.0 * temp0.Data[j-1]
temp1.Data[j-1] = (hi + 2.0*xi) * temp0.Data[j-1]
S1.Data[j] = (temp0.Data[j] + temp1.Data[j]) * yi / math.Pow(hi, 3.0)
}
S1.Data[0] = temp1.Data[0] * yi / math.Pow(hi, 3.0)
//2.2 S2
temp0 = ZeroMatrix(4, 1)
temp1 = ZeroMatrix(4, 1)
temp0.Data[2] = 1.0
temp0.Data[1] = -2.0 * xi
temp0.Data[0] = xi * xi
for j := 3; j > 0; j-- {
temp0.Data[j] = temp0.Data[j-1]
temp1.Data[j-1] = -1.0 * xi_1 * temp0.Data[j-1]
S2.Data[j] = (temp0.Data[j] + temp1.Data[j]) * mi_1 / math.Pow(hi, 2.0)
}
S2.Data[0] = temp1.Data[0] * mi_1 / math.Pow(hi, 2.0)
//2.3 S3
temp0 = ZeroMatrix(4, 1)
temp1 = ZeroMatrix(4, 1)
temp0.Data[2] = 1.0
temp0.Data[1] = -2.0 * xi_1
temp0.Data[0] = xi_1 * xi_1
for j := 3; j > 0; j-- {
temp0.Data[j] = temp0.Data[j-1]
temp1.Data[j-1] = -1.0 * xi * temp0.Data[j-1]
S3.Data[j] = (temp0.Data[j] + temp1.Data[j]) * mi / math.Pow(hi, 2.0)
}
S3.Data[0] = temp1.Data[0] * mi / math.Pow(hi, 2.0)
//2.4 Si(x)
for j := 0; j < 4; j++ {
sol.SetMatrix(j, i-1, S0.Data[j]+S1.Data[j]+S2.Data[j]+S3.Data[j])
}
}
err = true
return sol, err
}

199
vendor/github.com/nuknal/goNum/InterpSpline12.go generated vendored Normal file
View File

@@ -0,0 +1,199 @@
// InterpSpline12
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-8
版本 : 0.0.0
------------------------------------------------------
用节点处的一阶导数表示的三次样条插值函数,
二阶导数边界条件
n+1个点, n个区间
理论:
区间[x(i-1), xi]上的三次样条函数表达为:
(x-xi)^2 * [hi+2(x-x(i-1))]
Si(x) = -----------------------------y(i-1) +
hi^3
(x-x(i-1))^2 * [hi+2(xi-x)]
-----------------------------yi +
hi^3
(x-xi)^2 * (x-x(i-1))
-----------------------m(i-1) +
hi^2
(x-x(i-1))^2 * (x-xi)
-----------------------mi
hi^2
令 lambdai = h(i+1)/(hi+h(i+1))
Mi = 1-lambdai = hi/(hi+h(i+1))
y(i+1)-yi yi-y(i-1)
fi = 3(Mi---------- + lambdai-----------)
h(i+1) hi
(i = 1,...,n-1)
则mi可由n+1阶线性方程组求得利用LEs_Chasing
|2 1 || m0 | | f0 |
|l1 2 M1 || m1 | | f1 |
| l2 2 M2 || m2 | = | f2 |
| ........ || ... | | ... |
| l(n-1) 2 M(n-1)||m(n-1)| |f(n-1)|
| 1 2 || mn | | fn |
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 116-123.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi
第三列y''i且y''i只需给出y''0和y''n
输出 :
B 插值方程系数结果矩阵从前到后对应从0到3阶4xn
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import "math"
// InterpSpline12 用节点处的一阶导数表示的三次样条插值函数,二阶导数边界条件
func InterpSpline12(A Matrix) (Matrix, bool) {
/*
用节点处的一阶导数表示的三次样条插值函数,二阶导数边界条件
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi
第三列y'i且y'i只需给出y'0和y'n
输出 :
B 插值方程系数结果矩阵从前到后对应从0到3阶4xn
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var err bool = false
n := A.Rows - 1
sol := ZeroMatrix(4, n)
BA := ZeroMatrix(n+1, n+1) //对角占优的三对角矩阵
BB := ZeroMatrix(n+1, 1) //解向量
BC := ZeroMatrix(n+1, 1) //值向量
//1解插值函数的一阶导数mi
//1.0.1第一行
if true { //限制变量使用范围
h1 := A.GetFromMatrix(1, 0) - A.GetFromMatrix(0, 0)
y0 := A.GetFromMatrix(0, 1)
y1 := A.GetFromMatrix(1, 1)
f0 := 3.0*(y1-y0)/h1 - h1*A.GetFromMatrix(0, 2)/2.0
BA.SetMatrix(0, 0, 2.0)
BA.SetMatrix(0, 1, 1.0)
BC.Data[0] = f0
}
//1.0.2其它行
for i := 1; i < n; i++ {
yi_1 := A.GetFromMatrix(i-1, 0)
yi := A.GetFromMatrix(i, 0)
yi1 := A.GetFromMatrix(i+1, 0)
hi := A.GetFromMatrix(i, 0) - A.GetFromMatrix(i-1, 0)
hi1 := A.GetFromMatrix(i+1, 0) - A.GetFromMatrix(i, 0)
lambdai := hi1 / (hi + hi1)
Mi := 1.0 - lambdai
fi := 3.0 * (Mi*(yi1-yi)/hi1 + lambdai*(yi-yi_1)/hi)
//赋予BA
BA.SetMatrix(i, i-1, lambdai)
BA.SetMatrix(i, i, 2.0)
BA.SetMatrix(i, i+1, Mi)
BC.Data[i] = fi
}
//1.0.3最后一行
if true { //i=n
hn := A.GetFromMatrix(n, 0) - A.GetFromMatrix(n-1, 0)
yn_1 := A.GetFromMatrix(n-1, 1)
yn := A.GetFromMatrix(n, 1)
fn := 3.0*(yn-yn_1)/hn + hn*A.GetFromMatrix(n, 2)/2.0
BA.SetMatrix(n, n-1, 1.0)
BA.SetMatrix(n, n, 2.0)
BC.Data[n] = fn
}
//1.1求解
soltemp, errtemp := LEs_Chasing(BA, BC)
if errtemp != true {
panic("Error in goNum.InterpSpline12: Solve Error with goNum.LEs_Chasing")
}
for i := 0; i < n+1; i++ {
BB.Data[i] = soltemp.Data[i]
}
//2求解Si(x)
S0 := ZeroMatrix(4, 1)
S1 := ZeroMatrix(4, 1)
S2 := ZeroMatrix(4, 1)
S3 := ZeroMatrix(4, 1)
for i := 1; i < n+1; i++ {
xi_1 := A.GetFromMatrix(i-1, 0)
xi := A.GetFromMatrix(i, 0)
yi_1 := A.GetFromMatrix(i-1, 1)
yi := A.GetFromMatrix(i, 1)
mi_1 := BB.Data[i-1]
mi := BB.Data[i]
hi := xi - xi_1
temp0 := ZeroMatrix(4, 1)
temp1 := ZeroMatrix(4, 1)
//2.1 S0
temp0.Data[2] = 1.0
temp0.Data[1] = -2.0 * xi
temp0.Data[0] = xi * xi
for j := 3; j > 0; j-- {
temp0.Data[j] = 2.0 * temp0.Data[j-1]
temp1.Data[j-1] = (hi - 2.0*xi_1) * temp0.Data[j-1]
S0.Data[j] = (temp0.Data[j] + temp1.Data[j]) * yi_1 / math.Pow(hi, 3.0)
}
S0.Data[0] = temp1.Data[0] * yi_1 / math.Pow(hi, 3.0)
//2.1 S1
temp0 = ZeroMatrix(4, 1)
temp1 = ZeroMatrix(4, 1)
temp0.Data[2] = 1.0
temp0.Data[1] = -2.0 * xi_1
temp0.Data[0] = xi_1 * xi_1
for j := 3; j > 0; j-- {
temp0.Data[j] = -2.0 * temp0.Data[j-1]
temp1.Data[j-1] = (hi + 2.0*xi) * temp0.Data[j-1]
S1.Data[j] = (temp0.Data[j] + temp1.Data[j]) * yi / math.Pow(hi, 3.0)
}
S1.Data[0] = temp1.Data[0] * yi / math.Pow(hi, 3.0)
//2.2 S2
temp0 = ZeroMatrix(4, 1)
temp1 = ZeroMatrix(4, 1)
temp0.Data[2] = 1.0
temp0.Data[1] = -2.0 * xi
temp0.Data[0] = xi * xi
for j := 3; j > 0; j-- {
temp0.Data[j] = temp0.Data[j-1]
temp1.Data[j-1] = -1.0 * xi_1 * temp0.Data[j-1]
S2.Data[j] = (temp0.Data[j] + temp1.Data[j]) * mi_1 / math.Pow(hi, 2.0)
}
S2.Data[0] = temp1.Data[0] * mi_1 / math.Pow(hi, 2.0)
//2.3 S3
temp0 = ZeroMatrix(4, 1)
temp1 = ZeroMatrix(4, 1)
temp0.Data[2] = 1.0
temp0.Data[1] = -2.0 * xi_1
temp0.Data[0] = xi_1 * xi_1
for j := 3; j > 0; j-- {
temp0.Data[j] = temp0.Data[j-1]
temp1.Data[j-1] = -1.0 * xi * temp0.Data[j-1]
S3.Data[j] = (temp0.Data[j] + temp1.Data[j]) * mi / math.Pow(hi, 2.0)
}
S3.Data[0] = temp1.Data[0] * mi / math.Pow(hi, 2.0)
//2.4 Si(x)
for j := 0; j < 4; j++ {
sol.SetMatrix(j, i-1, S0.Data[j]+S1.Data[j]+S2.Data[j]+S3.Data[j])
}
}
err = true
return sol, err
}

179
vendor/github.com/nuknal/goNum/InterpSpline21.go generated vendored Normal file
View File

@@ -0,0 +1,179 @@
// InterpSpline21
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-9
版本 : 0.0.0
------------------------------------------------------
用节点处的二阶导数表示的三次样条插值函数,
一阶导数边界条件
n+1个点, n个区间
理论:
区间[x(i-1), xi]上的三次样条函数表达为:
(xi-x)^3
Si(x) = ----------M(i-1) +
6*hi
(x-x(i-1))^3
--------------Mi +
6*hi
M(i-1) xi-x
(y(i-1) - --------hi^2)-------
6 hi
Mi x-x(i-1)
(yi - ----hi^2)----------
6 hi
令 Mi = hi/(hi+h(i+1))
lambdai = 1-Mi = h(i+1)/(hi+h(i+1))
6 y(i+1)-yi yi-y(i-1)
fi = -----------(---------- - -----------)
hi+h(i+1) h(i+1) hi
(i = 1,...,n-1)
则mi可由n+1阶线性方程组求得利用LEs_Chasing
|2 1 || M0 | | f0 |
|M1 2 l1 || M1 | | f1 |
| M2 2 l2 || M2 | = | f2 |
| ........ || ... | | ... |
| M(n-1) 2 l(n-1)||M(n-1)| |f(n-1)|
| 1 2 || Mn | | fn |
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 124-127.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi
第三列y'i且y'i只需给出y'0和y'n
输出 :
B 插值方程系数结果矩阵从前到后对应从0到3阶4xn
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// InterpSpline21 用节点处的二阶导数表示的三次样条插值函数, 一阶导数边界条件
func InterpSpline21(A Matrix) (Matrix, bool) {
/*
用节点处的二阶导数表示的三次样条插值函数, 一阶导数边界条件
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi
第三列y'i且y'i只需给出y'0和y'n
输出 :
B 插值方程系数结果矩阵从前到后对应从0到3阶4xn
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var err bool = false
n := A.Rows - 1
sol := ZeroMatrix(4, n)
BA := ZeroMatrix(n+1, n+1) //对角占优的三对角矩阵
BB := ZeroMatrix(n+1, 1) //解向量
BC := ZeroMatrix(n+1, 1) //值向量
//1解插值函数的一阶导数mi
//1.0.1第一行
if true { //限制变量使用范围
h1 := A.GetFromMatrix(1, 0) - A.GetFromMatrix(0, 0)
y0 := A.GetFromMatrix(0, 1)
y1 := A.GetFromMatrix(1, 1)
f0 := 6.0 * ((y1-y0)/h1 - A.GetFromMatrix(0, 2)) / h1
BA.SetMatrix(0, 0, 2.0)
BA.SetMatrix(0, 1, 1.0)
BC.Data[0] = f0
}
//1.0.2其它行
for i := 1; i < n; i++ {
yi_1 := A.GetFromMatrix(i-1, 0)
yi := A.GetFromMatrix(i, 0)
yi1 := A.GetFromMatrix(i+1, 0)
hi := A.GetFromMatrix(i, 0) - A.GetFromMatrix(i-1, 0)
hi1 := A.GetFromMatrix(i+1, 0) - A.GetFromMatrix(i, 0)
Mi := hi / (hi + hi1)
lambdai := 1.0 - Mi
fi := 6.0 * ((yi1-yi)/hi1 - (yi-yi_1)/hi) / (hi + hi1)
//赋予BA
BA.SetMatrix(i, i-1, Mi)
BA.SetMatrix(i, i, 2.0)
BA.SetMatrix(i, i+1, lambdai)
BC.Data[i] = fi
}
//1.0.3最后一行
if true { //i=n
hn := A.GetFromMatrix(n, 0) - A.GetFromMatrix(n-1, 0)
yn_1 := A.GetFromMatrix(n-1, 1)
yn := A.GetFromMatrix(n, 1)
fn := 6.0 * (A.GetFromMatrix(n, 2) - (yn-yn_1)/hn) / hn
BA.SetMatrix(n, n-1, 1.0)
BA.SetMatrix(n, n, 2.0)
BC.Data[n] = fn
}
//1.1求解
soltemp, errtemp := LEs_Chasing(BA, BC)
if errtemp != true {
panic("Error in goNum.InterpSpline11: Solve Error with goNum.LEs_Chasing")
}
for i := 0; i < n+1; i++ {
BB.Data[i] = soltemp.Data[i]
}
//2求解Si(x)
S0 := ZeroMatrix(4, 1)
S1 := ZeroMatrix(4, 1)
S2 := ZeroMatrix(4, 1)
S3 := ZeroMatrix(4, 1)
for i := 1; i < n+1; i++ {
xi_1 := A.GetFromMatrix(i-1, 0)
xi := A.GetFromMatrix(i, 0)
yi_1 := A.GetFromMatrix(i-1, 1)
yi := A.GetFromMatrix(i, 1)
Mi_1 := BB.Data[i-1]
Mi := BB.Data[i]
hi := xi - xi_1
temp0 := ZeroMatrix(4, 1)
//2.1 S0
temp0.Data[3] = -1.0
temp0.Data[2] = 3.0 * xi
temp0.Data[1] = -3.0 * xi * xi
temp0.Data[0] = xi * xi * xi
for j := 0; j < 4; j++ {
S0.Data[j] = temp0.Data[j] * Mi_1 / (6.0 * hi)
}
//2.1 S1
temp0.Data[3] = 1.0
temp0.Data[2] = -3.0 * xi_1
temp0.Data[1] = 3.0 * xi_1 * xi_1
temp0.Data[0] = -1.0 * xi_1 * xi_1 * xi_1
for j := 0; j < 4; j++ {
S0.Data[j] = temp0.Data[j] * Mi / (6.0 * hi)
}
//2.2 S2
temp0 = ZeroMatrix(4, 1)
temp0.Data[1] = -1.0
temp0.Data[0] = xi
for j := 0; j < 4; j++ {
S2.Data[j] = temp0.Data[j] * (yi_1 - Mi_1*hi*hi/6.0) / hi
}
//2.3 S3
temp0 = ZeroMatrix(4, 1)
temp0.Data[1] = 1.0
temp0.Data[0] = -1.0 * xi_1
for j := 0; j < 4; j++ {
S3.Data[j] = temp0.Data[j] * (yi - Mi*hi*hi/6.0) / hi
}
//2.4 Si(x)
for j := 0; j < 4; j++ {
sol.SetMatrix(j, i-1, S0.Data[j]+S1.Data[j]+S2.Data[j]+S3.Data[j])
}
}
err = true
return sol, err
}

194
vendor/github.com/nuknal/goNum/InterpSpline22.go generated vendored Normal file
View File

@@ -0,0 +1,194 @@
// InterpSpline22
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-9
版本 : 0.0.0
------------------------------------------------------
用节点处的二阶导数表示的三次样条插值函数,
二阶导数边界条件
n+1个点, n个区间
理论:
区间[x(i-1), xi]上的三次样条函数表达为:
(xi-x)^3
Si(x) = ----------M(i-1) +
6*hi
(x-x(i-1))^3
--------------Mi +
6*hi
M(i-1) xi-x
(y(i-1) - --------hi^2)-------
6 hi
Mi x-x(i-1)
(yi - ----hi^2)----------
6 hi
令 Mi = hi/(hi+h(i+1))
lambdai = 1-Mi = h(i+1)/(hi+h(i+1))
6 y(i+1)-yi yi-y(i-1)
fi = -----------(---------- - -----------)
hi+h(i+1) h(i+1) hi
(i = 1,...,n-1)
则mi可由n-1阶线性方程组求得利用LEs_Chasing
|2 l1 || M1 | | f1-M1*M0 |
| M2 2 l2 || M2 | = | f2 |
| ........ || ... | | ... |
| M(n-2) 2 l(n-2)||M(n-2)| | f(n-2) |
| M(n-1) 2 ||M(n-1)| |f(n-1)-l(n-1)Mn|
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 124-127.
------------------------------------------------------
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi
第三列y''i且y''i只需给出y''0和y''n
输出 :
B 插值方程系数结果矩阵从前到后对应从0到3阶4xn
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// InterpSpline22 用节点处的二阶导数表示的三次样条插值函数, 二阶导数边界条件
func InterpSpline22(A Matrix) (Matrix, bool) {
/*
用节点处的二阶导数表示的三次样条插值函数, 二阶导数边界条件
输入 :
A 数据点矩阵,(n+1)x3第一列xi第二列yi
第三列y'i且y'i只需给出y'0和y'n
输出 :
B 插值方程系数结果矩阵从前到后对应从0到3阶4xn
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var err bool = false
n := A.Rows - 1
sol := ZeroMatrix(4, n)
BA := ZeroMatrix(n-1, n-1) //对角占优的三对角矩阵
BB := ZeroMatrix(n-1, 1) //解向量
BC := ZeroMatrix(n-1, 1) //值向量
//1解插值函数的一阶导数mi
//1.0.1第一行
if true { //限制变量使用范围
h1 := A.GetFromMatrix(1, 0) - A.GetFromMatrix(0, 0)
h2 := A.GetFromMatrix(2, 0) - A.GetFromMatrix(1, 0)
y0 := A.GetFromMatrix(0, 1)
y1 := A.GetFromMatrix(1, 1)
y2 := A.GetFromMatrix(2, 1)
M1 := h1 / (h1 + h2)
l1 := 1.0 - M1
f1 := 6.0 * ((y2-y1)/h2 - (y1-y0)/h1) / (h1 + h2)
BA.SetMatrix(0, 0, 2.0)
BA.SetMatrix(0, 1, l1)
BC.Data[0] = f1 - M1*A.GetFromMatrix(0, 2)
}
//1.0.2其它行
for i := 2; i < n-1; i++ {
yi_1 := A.GetFromMatrix(i-1, 0)
yi := A.GetFromMatrix(i, 0)
yi1 := A.GetFromMatrix(i+1, 0)
hi := A.GetFromMatrix(i, 0) - A.GetFromMatrix(i-1, 0)
hi1 := A.GetFromMatrix(i+1, 0) - A.GetFromMatrix(i, 0)
Mi := hi / (hi + hi1)
li := 1.0 - Mi
fi := 6.0 * ((yi1-yi)/hi1 - (yi-yi_1)/hi) / (hi + hi1)
//赋予BA
BA.SetMatrix(i-1, i-2, Mi)
BA.SetMatrix(i-1, i-1, 2.0)
BA.SetMatrix(i-1, i, li)
BC.Data[i-1] = fi
}
//1.0.3最后一行
if true { //i=n-1
hn_1 := A.GetFromMatrix(n-1, 0) - A.GetFromMatrix(n-2, 0)
hn := A.GetFromMatrix(n, 0) - A.GetFromMatrix(n-1, 0)
yn_2 := A.GetFromMatrix(n-2, 1)
yn_1 := A.GetFromMatrix(n-1, 1)
yn := A.GetFromMatrix(n, 1)
Mn_1 := hn_1 / (hn_1 + hn)
ln_1 := 1.0 - Mn_1
fn_1 := 6.0 * ((yn-yn_1)/hn - (yn_1-yn_2)/hn_1) / (hn_1 + hn)
BA.SetMatrix(n-2, n-3, Mn_1)
BA.SetMatrix(n-2, n-2, 2.0)
BC.Data[n-2] = fn_1 - ln_1*A.GetFromMatrix(n, 2)
}
//1.1求解
soltemp, errtemp := LEs_Chasing(BA, BC)
if errtemp != true {
panic("Error in goNum.InterpSpline11: Solve Error with goNum.LEs_Chasing")
}
for i := 0; i < n-1; i++ {
BB.Data[i] = soltemp.Data[i]
}
//2求解Si(x)
S0 := ZeroMatrix(4, 1)
S1 := ZeroMatrix(4, 1)
S2 := ZeroMatrix(4, 1)
S3 := ZeroMatrix(4, 1)
for i := 1; i < n+1; i++ {
xi_1 := A.GetFromMatrix(i-1, 0)
xi := A.GetFromMatrix(i, 0)
yi_1 := A.GetFromMatrix(i-1, 1)
yi := A.GetFromMatrix(i, 1)
Mi_1 := 0.0
Mi := 0.0
if i == 1 {
Mi_1 = A.GetFromMatrix(0, 2)
Mi = BB.Data[i-1]
} else if i == n {
Mi_1 = BB.Data[i-2]
Mi = A.GetFromMatrix(n, 2)
} else {
Mi_1 = BB.Data[i-2]
Mi = BB.Data[i-1]
}
hi := xi - xi_1
temp0 := ZeroMatrix(4, 1)
//2.1 S0
temp0.Data[3] = -1.0
temp0.Data[2] = 3.0 * xi
temp0.Data[1] = -3.0 * xi * xi
temp0.Data[0] = xi * xi * xi
for j := 0; j < 4; j++ {
S0.Data[j] = temp0.Data[j] * Mi_1 / (6.0 * hi)
}
//2.1 S1
temp0.Data[3] = 1.0
temp0.Data[2] = -3.0 * xi_1
temp0.Data[1] = 3.0 * xi_1 * xi_1
temp0.Data[0] = -1.0 * xi_1 * xi_1 * xi_1
for j := 0; j < 4; j++ {
S0.Data[j] = temp0.Data[j] * Mi / (6.0 * hi)
}
//2.2 S2
temp0 = ZeroMatrix(4, 1)
temp0.Data[1] = -1.0
temp0.Data[0] = xi
for j := 0; j < 4; j++ {
S2.Data[j] = temp0.Data[j] * (yi_1 - Mi_1*hi*hi/6.0) / hi
}
//2.3 S3
temp0 = ZeroMatrix(4, 1)
temp0.Data[1] = 1.0
temp0.Data[0] = -1.0 * xi_1
for j := 0; j < 4; j++ {
S3.Data[j] = temp0.Data[j] * (yi - Mi*hi*hi/6.0) / hi
}
//2.4 Si(x)
for j := 0; j < 4; j++ {
sol.SetMatrix(j, i-1, S0.Data[j]+S1.Data[j]+S2.Data[j]+S3.Data[j])
}
}
err = true
return sol, err
}

87
vendor/github.com/nuknal/goNum/InverseA.go generated vendored Normal file
View File

@@ -0,0 +1,87 @@
// InverseA
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-20
版本 : 0.0.0
------------------------------------------------------
求矩阵逆的列主元消去法
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 51.
------------------------------------------------------
输入 :
a 矩阵
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// InverseA 求矩阵逆的列主元消去法
func InverseA(a [][]float64) ([][]float64, bool) {
/*
求矩阵逆的列主元消去法
输入 :
a 矩阵
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var err bool = false
n := len(a)
temp0, _ := E_Mat(n)
b := temp0
sol := b
temp1 := 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 {
temp1 = a[ii+i]
a[ii+i] = a[i]
a[i] = temp1
temp1 = b[ii+i]
b[ii+i] = b[i]
b[i] = temp1
}
//列消去
//本行主元置一
mul := a[i][i]
for j := 0; j < n; j++ {
a[i][j] = a[i][j] / mul
b[i][j] = b[i][j] / mul
}
//其它列置零
for j := 0; j < n; j++ {
if j != i {
mul = a[j][i] / a[i][i]
for k := 0; k < n; k++ {
a[j][k] = a[j][k] - a[i][k]*mul
b[j][k] = b[j][k] - b[i][k]*mul
}
}
}
}
sol = b
err = true
return sol, err
}

93
vendor/github.com/nuknal/goNum/LEs_Chasing.go generated vendored Normal file
View File

@@ -0,0 +1,93 @@
// LEs_Chasing
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-8
版本 : 0.0.0
------------------------------------------------------
追赶法求解严格对角占优的三对角矩阵
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 59-61.
------------------------------------------------------
输入 :
A 系数矩阵, nxn
BA 常数值向量, nx1
输出 :
sol 解向量, nx1
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// LEs_Chasing 追赶法求解严格对角占优的三对角矩阵
func LEs_Chasing(A, BA Matrix) (Matrix, bool) {
/*
追赶法求解严格对角占优的三对角矩阵
输入 :
A 系数矩阵, nxn
BA 常数值向量, nx1
输出 :
sol 解向量, nx1
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断A是否方阵
if A.Rows != A.Columns {
panic("Error in goNum.LEs_Chasing: A is not a square matrix")
}
//判断BA是否与A行数相等
if A.Rows != BA.Rows {
panic("Error in goNum.LEs_Chasing: Rows of A and BA are not equal")
}
var err bool = false
n := A.Rows
ai := ZeroMatrix(n, 1) //第一位无效
bi := ZeroMatrix(n, 1)
ci := ZeroMatrix(n-1, 1)
gamma := ZeroMatrix(n, 1) //gammai
beta := ZeroMatrix(n, 1) //beta, 第一位无效
delta := ZeroMatrix(n-1, 1) //deltai
y := ZeroMatrix(n, 1) //yi
sol := ZeroMatrix(n, 1) //xi
//ai, bi, ci
bi.Data[0] = A.GetFromMatrix(0, 0)
ci.Data[0] = A.GetFromMatrix(0, 1)
for i := 1; i < n-1; i++ {
ai.Data[i] = A.GetFromMatrix(i, i-1)
bi.Data[i] = A.GetFromMatrix(i, i)
ci.Data[i] = A.GetFromMatrix(i, i+1)
}
ai.Data[n-1] = A.GetFromMatrix(n-1, n-2)
bi.Data[n-1] = A.GetFromMatrix(n-1, n-1)
//解gamma, beta和delta
gamma.Data[0] = bi.Data[0]
delta.Data[0] = ci.Data[0] / gamma.Data[0]
for i := 1; i < n-1; i++ {
beta.Data[i] = ai.Data[i]
gamma.Data[i] = bi.Data[i] - beta.Data[i]*delta.Data[i-1]
delta.Data[i] = ci.Data[i] / gamma.Data[i]
}
beta.Data[n-1] = ai.Data[n-1]
gamma.Data[n-1] = bi.Data[n-1] - beta.Data[n-1]*delta.Data[n-2]
//解yi
y.Data[0] = BA.Data[0] / gamma.Data[0]
for i := 1; i < BA.Rows; i++ {
y.Data[i] = (BA.Data[i] - beta.Data[i]*y.Data[i-1]) / gamma.Data[i]
}
//解xi
sol.Data[n-1] = y.Data[n-1]
for i := n - 2; i >= 0; i-- {
sol.Data[i] = y.Data[i] - delta.Data[i]*sol.Data[i+1]
}
err = true
return sol, err
}

96
vendor/github.com/nuknal/goNum/LEs_ECPE.go generated vendored Normal file
View File

@@ -0,0 +1,96 @@
// LEs_ECPE
// linear equations - elemination of column principle
// element
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-19
版本 : 0.0.0
------------------------------------------------------
线性代数方程组的列主元消去法
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 47-49.
乘除运算的次数 n^3/3+n^2-n/3
------------------------------------------------------
输入 :
a a x = b线性代数方程组的系数矩阵
b a x = b线性代数方程组的右侧常数列向量
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// LEs_ECPE 线性代数方程组的列主元消去法
func LEs_ECPE(a [][]float64, b []float64) ([]float64, bool) {
/*
线性代数方程组的列主元消去法
输入 :
a a x = b线性代数方程组的系数矩阵
b a x = b线性代数方程组的右侧常数列向量
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//方程个数为n
var err bool = false
atemp := a
btemp := b
n := len(btemp)
sol := make([]float64, n)
temp0 := make([]float64, n)
var temp1 float64
// 输入判断
if len(atemp) != n {
return sol, err
}
//求解
//消去,求得上三角矩阵
for i := 0; i < n-1; i++ {
//求第i列的主元素并调整顺序
acol := make([]float64, n-i)
for icol := i; icol < n; icol++ {
acol[icol-i] = atemp[icol][i]
}
_, ii, _ := MaxAbs(acol)
if ii+i != i {
temp0 = atemp[ii+i]
atemp[ii+i] = atemp[i]
atemp[i] = temp0
temp1 = btemp[ii+i]
btemp[ii+i] = btemp[i]
btemp[i] = temp1
}
//列消去
for j := i + 1; j < n; j++ {
mul := atemp[j][i] / atemp[i][i]
for k := i; k < n; k++ {
atemp[j][k] = atemp[j][k] - atemp[i][k]*mul
}
btemp[j] = btemp[j] - btemp[i]*mul
}
}
//回代
sol[n-1] = btemp[n-1] / atemp[n-1][n-1]
for i := n - 2; i >= 0; i-- {
temp1 = 0.0
for j := i + 1; j < n; j++ {
temp1 = temp1 + atemp[i][j]*sol[j]
}
sol[i] = (btemp[i] - temp1) / atemp[i][i]
}
//返回结果
err = true
return sol, err
}

90
vendor/github.com/nuknal/goNum/LEs_JocobiIterate.go generated vendored Normal file
View File

@@ -0,0 +1,90 @@
// LEs_JocobiIterate
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-22
版本 : 0.0.0
------------------------------------------------------
解n阶线性方程组的Jocobi迭代法简单迭代法
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 61-68.
收敛的条件B为变化后的系数矩阵
1. 矩阵B的谱半径小于1或者
2. 矩阵B的1范数小于1或者
3. 矩阵B的无穷范数小于1或者
4. 系数矩阵A严格对角占优
------------------------------------------------------
输入 :
A 系数矩阵
b 常数值向量
tol 最大容许误差
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// LEs_JocobiIterate 解n阶线性方程组的Jocobi迭代法简单迭代法
func LEs_JocobiIterate(A, b, x0 Matrix, tol float64, n int) ([]float64, bool) {
/*
解n阶线性方程组的Jocobi迭代法简单迭代法
输入 :
A 系数矩阵
b 常数值向量
tol 最大容许误差
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
B := ZeroMatrix(A.Rows, A.Columns)
g := ZeroMatrix(A.Rows, 1)
x1 := ZeroMatrix(A.Rows, 1)
sol := ZeroMatrix(A.Rows, 1)
var err bool = false
//方程组迭代化变换求得矩阵B
for i := 0; i < A.Rows; i++ {
for j := 0; j < A.Columns; j++ {
if j != i {
B.SetMatrix(i, j, -1.0*A.GetFromMatrix(i, j)/A.GetFromMatrix(i, i))
}
}
g.Data[i] = b.Data[i] / A.GetFromMatrix(i, i)
}
//判断B是否收敛
temp0, _ := Norm1(B)
temp1, _ := NormInf(B)
if (temp0 >= 1) || (temp1 >= 1) {
return sol.Data, err
}
//求解
for i := 0; i < n; i++ {
x1 = AddMatrix(DotPruduct(B, x0), g)
sol = SubMatrix(x1, x0)
max, _, _ := Max(sol.Data)
if math.Abs(max) < tol {
sol = x1
err = true
return sol.Data, err
}
for i0 := 0; i0 < x0.Rows; i0++ {
x0.Data[i0] = x1.Data[i0]
}
}
return make([]float64, A.Rows), err
}

85
vendor/github.com/nuknal/goNum/LEs_SORIterate.go generated vendored Normal file
View File

@@ -0,0 +1,85 @@
// LEs_SORIterate
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-22
版本 : 0.0.0
------------------------------------------------------
解n阶线性方程组的SOR(逐次超松弛, successive over
relaxation)迭代法
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 68-72.
收敛的条件B为变化后的系数矩阵
1. 系数矩阵A严格对角占优且0 < omega <= 1或者
2. 系数矩阵A对称正定且0 < omega < 2
------------------------------------------------------
输入 :
A 系数矩阵
b 常数值向量
tol 最大容许误差
omega 松弛因子0 < omega < 2, omega = 1: Siedel,
omega < 1: 低松弛, omega > 1: 超松弛
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import "math"
// LEs_SORIterate 解n阶线性方程组的SOR(逐次超松弛, successive over relaxation)迭代法
func LEs_SORIterate(A, b, x0 Matrix, tol, omega float64, n int) ([]float64, bool) {
/*
解n阶线性方程组的SOR(逐次超松弛, successive over relaxation)迭代法
输入 :
A 系数矩阵
b 常数值向量
tol 最大容许误差
omega 松弛因子0 < omega < 2, omega = 1: Siedel,
omega < 1: 低松弛, omega > 1: 超松弛
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
x1 := ZeroMatrix(A.Rows, 1)
sol := ZeroMatrix(A.Rows, 1)
var err bool = false
//求解
for i := 0; i < n; i++ {
for i0 := 0; i0 < A.Rows; i0++ {
sum0 := 0.0
for j := 0; j < i0; j++ {
sum0 += A.GetFromMatrix(i0, j) * x1.GetFromMatrix(j, 0)
}
sum1 := 0.0
for j := i0 + 1; j < A.Columns; j++ {
sum1 += A.GetFromMatrix(i0, j) * x0.GetFromMatrix(j, 0)
}
x1.SetMatrix(i0, 0, (1-omega)*x0.GetFromMatrix(i0, 0)+omega*(b.Data[i0]-sum0-sum1)/A.GetFromMatrix(i0, i0))
}
//判断收敛
sol = SubMatrix(x1, x0)
max, _, _ := Max(sol.Data)
if math.Abs(max) < tol {
sol = x1
err = true
return sol.Data, err
}
//准备下次迭代
for i0 := 0; i0 < x0.Rows; i0++ {
x0.Data[i0] = x1.Data[i0]
}
}
return make([]float64, A.Rows), err
}

95
vendor/github.com/nuknal/goNum/LEs_SeidelIterate.go generated vendored Normal file
View File

@@ -0,0 +1,95 @@
// LEs_SeidelIterate
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-22
版本 : 0.0.0
------------------------------------------------------
解n阶线性方程组的Seidel迭代法
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 68-72.
收敛的条件B为变化后的系数矩阵
1. 矩阵B的谱半径小于1或者
2. 矩阵B的1范数小于1或者
3. 矩阵B的无穷范数小于1或者
4. 系数矩阵A严格对角占优
------------------------------------------------------
输入 :
A 系数矩阵
b 常数值向量
tol 最大容许误差
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// LEs_SeidelIterate 解n阶线性方程组的Seidel迭代法
func LEs_SeidelIterate(A, b, x0 Matrix, tol float64, n int) ([]float64, bool) {
/*
解n阶线性方程组的Seidel迭代法
输入 :
A 系数矩阵
b 常数值向量
tol 最大容许误差
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
B := ZeroMatrix(A.Rows, A.Columns)
g := ZeroMatrix(A.Rows, 1)
x1 := ZeroMatrix(A.Rows, 1)
xtemp := ZeroMatrix(A.Rows, 1)
sol := ZeroMatrix(A.Rows, 1)
var err bool = false
//方程组迭代化变换求得矩阵B
for i := 0; i < A.Rows; i++ {
for j := 0; j < A.Columns; j++ {
if j != i {
B.SetMatrix(i, j, -1.0*A.GetFromMatrix(i, j)/A.GetFromMatrix(i, i))
}
}
g.Data[i] = b.Data[i] / A.GetFromMatrix(i, i)
}
//判断B是否收敛
temp0, _ := Norm1(B)
temp1, _ := NormInf(B)
if (temp0 >= 1) || (temp1 >= 1) {
return sol.Data, err
}
//求解
for i := 0; i < n; i++ {
for i0 := 0; i0 < B.Rows; i0++ {
dotP := DotPruduct(NewMatrix(1, B.Columns, B.RowOfMatrix(i0)), xtemp)
x1.Data[i0] = dotP.Data[0] + g.Data[i0]
xtemp.Data[i0] = x1.Data[i0]
}
sol = SubMatrix(x1, x0)
max, _, _ := Max(sol.Data)
if math.Abs(max) < tol {
sol = x1
err = true
return sol.Data, err
}
for i0 := 0; i0 < x0.Rows; i0++ {
x0.Data[i0] = x1.Data[i0]
}
}
return make([]float64, A.Rows), err
}

674
vendor/github.com/nuknal/goNum/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
{one line to give the program's name and a brief idea of what it does.}
Copyright (C) {year} {name of author}
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
{project} Copyright (C) {year} {fullname}
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

77
vendor/github.com/nuknal/goNum/LLT_Decompose.go generated vendored Normal file
View File

@@ -0,0 +1,77 @@
// LLT_Decompose
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-8
版本 : 0.0.0
------------------------------------------------------
求对称正定矩阵的平方根分解法
理论:
A = LL'
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 57-58.
------------------------------------------------------
输入 :
A 矩阵,对称正定
输出 :
L 下三角矩阵, 上三角矩阵为其转置
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// LLT_Decompose 求对称正定矩阵的平方根分解法
func LLT_Decompose(A Matrix) (Matrix, bool) {
/*
求对称正定矩阵的平方根分解法
输入 :
A 矩阵,对称正定
输出 :
L
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断对称
if A.Rows != A.Columns {
panic("Error in goNum.LLT_Decompose: A is not symmetry")
}
n := A.Rows
L := ZeroMatrix(n, n)
var err bool = false
//计算开始
//第一列
L.SetMatrix(0, 0, math.Sqrt(A.GetFromMatrix(0, 0)))
l11 := L.GetFromMatrix(0, 0)
for j := 1; j < n; j++ {
L.SetMatrix(j, 0, A.GetFromMatrix(0, j)/l11)
}
//其它列
for k := 1; k < n; k++ {
//主对角元lkk
var temp0 float64
for m := 0; m < k; m++ {
temp0 += L.GetFromMatrix(k, m) * L.GetFromMatrix(k, m)
}
temp0 = A.GetFromMatrix(k, k) - temp0
L.SetMatrix(k, k, math.Sqrt(temp0))
//k列其它元
for j := k + 1; j < n; j++ {
var temp1 float64
for m := 0; m < k; m++ {
temp1 += L.GetFromMatrix(k, m) * L.GetFromMatrix(j, m)
}
temp1 = (A.GetFromMatrix(k, j) - temp1) / L.GetFromMatrix(k, k)
L.SetMatrix(j, k, temp1)
}
}
err = true
return L, err
}

72
vendor/github.com/nuknal/goNum/LU_Doolittle.go generated vendored Normal file
View File

@@ -0,0 +1,72 @@
// LU_Doolittle
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-21
版本 : 0.0.0
------------------------------------------------------
求矩阵Doolittlede LU分解
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 53-56.
------------------------------------------------------
输入 :
A 矩阵
输出 :
L, U 下三角矩阵和上三角矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// LU_Doolittle 求矩阵Doolittlede LU分解
func LU_Doolittle(A Matrix) (Matrix, Matrix, bool) {
/*
求矩阵Doolittlede LU分解
输入 :
A 矩阵
输出 :
L, U 下三角矩阵和上三角矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var err bool = false
if A.Rows != A.Columns {
panic("goNum.LU_Doolittle: A is not a square matrix")
}
L := ZeroMatrix(A.Rows, A.Columns)
U := ZeroMatrix(A.Rows, A.Columns)
for j := 0; j < A.Rows; j++ {
U.SetMatrix(0, j, A.GetFromMatrix(0, j))
}
for i := 1; i < A.Rows; i++ {
L.SetMatrix(i, 0, A.GetFromMatrix(i, 0)/U.GetFromMatrix(0, 0))
}
for k := 1; k < A.Rows; k++ {
for j := k; j < A.Rows; j++ {
var sum float64
for m := 0; m < k; m++ {
sum += L.GetFromMatrix(k, m) * U.GetFromMatrix(m, j)
}
U.SetMatrix(k, j, A.GetFromMatrix(k, j)-sum)
}
for i := k + 1; i < A.Rows; i++ {
var sum float64
for m := 0; m < k; m++ {
sum += L.GetFromMatrix(i, m) * U.GetFromMatrix(m, k)
}
L.SetMatrix(i, k, (A.GetFromMatrix(i, k)-sum)/U.GetFromMatrix(k, k))
}
}
err = true
return L, U, err
}

285
vendor/github.com/nuknal/goNum/Matrix.go generated vendored Normal file
View File

@@ -0,0 +1,285 @@
// Matrix
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-20
版本 : 0.0.0
0.0.1 2018-12-11 增加切片与矩阵转换
0.0.2 2018-12-26 增加错误报告
0.0.3 2018-12-27 增加追加行/列
------------------------------------------------------
矩阵的创建及其操作创建及其简单操作/运算
理论:
参考 OneThin // http://outofmemery.cn/code-snippet
/16991/go-language-matrix-operation
进行了主要运算和结构的补充与修改
------------------------------------------------------
注意事项:
1. r, c 是从零开始算的
------------------------------------------------------
*/
package goNum
import (
"fmt"
"strconv"
)
//数据结构定义----------------------------------------+
// Matrix 定义Matrix数据类型
type Matrix struct {
Rows, Columns int //行数和列数
Data []float64 //将矩阵中所有元素作为一维切片
}
//矩阵操作-------------------------------------------+
//通过行列号寻找指定矩阵位置在一维切片中的编号
func findIndex(r, c int, A *Matrix) int {
//r E [0, n), c E [0, n)
return r*A.Columns + c
}
// SetMatrix 设置指定行列的值
func (A *Matrix) SetMatrix(r, c int, val float64) {
if (r >= A.Rows) || (c >= A.Columns) {
panic("Error in goNum.(*Matrix).SetMatrix: Out of range")
}
A.Data[findIndex(r, c, A)] = val
}
// GetFromMatrix 获取指定行列的值
func (A *Matrix) GetFromMatrix(r, c int) float64 {
if (r >= A.Rows) || (c >= A.Columns) {
panic("Error in goNum.(*Matrix).GetFromMatrix: Out of range")
}
return A.Data[findIndex(r, c, A)]
}
// RowOfMatrix 获取指定行的值的切片
func (A *Matrix) RowOfMatrix(i int) []float64 {
if i >= A.Rows {
panic("Error in goNum.(*Matrix).RowOfMatrix: Out of range")
}
return A.Data[findIndex(i, 0, A):findIndex(i, A.Columns, A)]
}
// ColumnOfMatrix 获取指定列的值的切片
func (A *Matrix) ColumnOfMatrix(j int) []float64 {
if j >= A.Columns {
panic("Error in goNum.(*Matrix).ColumnOfMatrix: Out of range")
}
col := make([]float64, A.Rows)
for i := 0; i < A.Rows; i++ {
col[i] = A.RowOfMatrix(i)[j]
}
return col
}
// Transpose 矩阵转置
func (A *Matrix) Transpose() Matrix {
B := ZeroMatrix(A.Columns, A.Rows)
for i := 0; i < A.Rows; i++ {
for j := 0; j < A.Columns; j++ {
B.SetMatrix(j, i, A.GetFromMatrix(i, j))
}
}
return B
}
// AppendRow 追加一行,另外一种方法是追加数据到A.Data测试显示其速度表现更差
func (A *Matrix) AppendRow(row []float64) Matrix {
//判断row长度是否等于A列数
if len(row) != A.Columns {
panic("Error in goNum.(*Matrix).AppendRow: Slice length error")
}
B := ZeroMatrix(A.Rows+1, A.Columns)
n := A.Rows * A.Columns
for i := 0; i < n; i++ {
B.Data[i] = A.Data[i]
}
for i := 0; i < len(row); i++ {
B.Data[n+i] = row[i]
}
return B
}
// AppendColumn 追加一列,对于多次调用,建议组合使用转置和追加行
func (A *Matrix) AppendColumn(col []float64) Matrix {
//判断row长度是否等于A列数
if len(col) != A.Rows {
panic("Error in goNum.(*Matrix).AppendColumn: Slice length error")
}
B := ZeroMatrix(A.Rows, A.Columns+1)
for i := 0; i < A.Rows; i++ {
for j := 0; j < A.Columns; j++ {
B.SetMatrix(i, j, A.GetFromMatrix(i, j))
}
B.SetMatrix(i, A.Columns, col[i])
}
return B
}
// PrintMatrix 格式输出
func (A *Matrix) PrintMatrix() {
//求出最长字符
colwidstr := make([]string, A.Columns)
for i := range colwidstr {
var maxLen int
thisColumn := A.ColumnOfMatrix(i)
for j := range thisColumn {
thisLen := len(strconv.FormatFloat(thisColumn[j], 'f', -1, 64))
if thisLen > maxLen {
maxLen = thisLen
}
}
}
for i := 0; i < A.Rows; i++ {
thisRow := A.RowOfMatrix(i)
fmt.Printf("[")
for j := range thisRow {
var format string
if j == 0 {
format = "%" + colwidstr[j] + "s"
} else {
format = " %" + colwidstr[j] + "s"
}
fmt.Printf(format, strconv.FormatFloat(thisRow[j], 'f', -1, 64))
}
fmt.Printf("]\n")
}
}
//矩阵初始化-----------------------------------------+
// ZeroMatrix r行c列零矩阵
func ZeroMatrix(r, c int) Matrix {
return Matrix{r, c, make([]float64, r*c)}
}
// IdentityE n阶单位矩阵
func IdentityE(n int) Matrix {
A := ZeroMatrix(n, n)
for i := 0; i < len(A.Data); i += (n + 1) {
A.Data[i] = 1.0
}
return A
}
// NewMatrix 以已有数据创建r行c列矩阵
func NewMatrix(r, c int, data []float64) Matrix {
if len(data) != r*c {
panic("goNum.Matrix.New: Length of data does not matched r rows and c columns")
}
A := ZeroMatrix(r, c)
A.Data = data
return A
}
// Slices1ToMatrix 一维切片转为矩阵(列向量)
func Slices1ToMatrix(s []float64) Matrix {
A := ZeroMatrix(len(s), 1)
for i := 0; i < A.Rows; i++ {
A.Data[i] = s[i]
}
return A
}
// Slices2ToMatrix 二维切片转为矩阵
func Slices2ToMatrix(s [][]float64) Matrix {
row := len(s)
col := len(s[0])
A := ZeroMatrix(row, col)
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
A.SetMatrix(i, j, s[i][j])
}
}
return A
}
// Matrix1ToSlices 列向量转为一维切片
func Matrix1ToSlices(A Matrix) []float64 {
s := make([]float64, A.Rows)
for i := 0; i < A.Rows; i++ {
s[i] = A.Data[i]
}
return s
}
// Matrix2ToSlices 二维矩阵转为二维切片
func Matrix2ToSlices(A Matrix) [][]float64 {
s := make([][]float64, A.Rows)
for i := 0; i < A.Rows; i++ {
s[i] = make([]float64, A.Columns)
for j := 0; j < A.Columns; j++ {
s[i][j] = A.GetFromMatrix(i, j)
}
}
return s
}
//矩阵运算------------------------------------------+
// AddMatrix 矩阵相加
func AddMatrix(A, B Matrix) Matrix {
if (A.Rows != B.Rows) || (A.Columns != B.Columns) {
panic("goNum.Matrix.Add: A and B does not matched")
}
AaddB := ZeroMatrix(A.Rows, A.Columns)
for i := 0; i < A.Rows; i++ {
for j := 0; j < A.Columns; j++ {
AaddB.SetMatrix(i, j, A.GetFromMatrix(i, j)+B.GetFromMatrix(i, j))
}
}
return AaddB
}
// SubMatrix 矩阵相减
func SubMatrix(A, B Matrix) Matrix {
if (A.Rows != B.Rows) || (A.Columns != B.Columns) {
panic("goNum.Matrix.Sub: A and B does not matched")
}
AsubB := ZeroMatrix(A.Rows, A.Columns)
for i := 0; i < A.Rows; i++ {
for j := 0; j < A.Columns; j++ {
AsubB.SetMatrix(i, j, A.GetFromMatrix(i, j)-B.GetFromMatrix(i, j))
}
}
return AsubB
}
// NumProductMatrix 矩阵数乘
func NumProductMatrix(A Matrix, c float64) Matrix {
cA := ZeroMatrix(A.Rows, A.Columns)
for i := 0; i < len(cA.Data); i++ {
cA.Data[i] = c * A.Data[i]
}
return cA
}
// DotPruduct 矩阵点乘
func DotPruduct(A, B Matrix) Matrix {
if A.Columns != B.Rows {
panic("goNum.Matrix.DotPruduct: A and B does not matched")
}
AdotB := ZeroMatrix(A.Rows, B.Columns)
for i := 0; i < A.Rows; i++ {
for j := 0; j < B.Columns; j++ {
for k := 0; k < A.Columns; k++ {
AdotB.Data[B.Columns*i+j] += A.GetFromMatrix(i, k) * B.GetFromMatrix(k, j)
}
}
}
return AdotB
}
// CrossVector 向量叉乘,得到垂直于两个向量所在平面的向量
func CrossVector(a, b []float64) []float64 {
if (len(a) != 3) || (len(b) != 3) {
panic("goNum.Matrix.CrossVector: vector a or b length is not 3")
}
acrossb := make([]float64, 3)
acrossb[0] = a[1]*b[2] - a[2]*b[1]
acrossb[1] = a[2]*b[0] - a[0]*b[2]
acrossb[2] = a[0]*b[1] - a[1]*b[0]
return acrossb
}

View File

@@ -0,0 +1,188 @@
// MatrixEigenClassicalJacobi
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-30
版本 : 0.0.0
------------------------------------------------------
求解n阶对称矩阵A的全部特征值及其特征向量经典雅可比法
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 84-89.
------------------------------------------------------
输入 :
A 系数矩阵
tol 最大容许误差
n 最大迭代步数
输出 :
Bbar 主特征值矩阵n阶对角矩阵
Rbar 主特征值所对应的特征向量n维矩阵第i列即对应于
第i个特征值的特征向量
(err) 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
//判断矩阵是否对称
func isSymMatrix_MatrixEigenClassicalJacobi(A Matrix) bool {
//是否方阵
if A.Columns != A.Rows {
return false
}
//是否对称
for i := 0; i < A.Rows; i++ {
for j := 0; j < A.Columns; j++ {
if j != i {
if A.GetFromMatrix(i, j) != A.GetFromMatrix(j, i) {
return false
}
}
}
}
return true
}
//取最大非对角元素
func maxElementElse_MatrixEigenClassicalJacobi(A Matrix) (int, int) {
var p, q int
var max float64
for i := 0; i < A.Rows-1; i++ {
for j := i + 1; j < A.Rows; j++ {
c := A.GetFromMatrix(i, j)
if math.Abs(max) < math.Abs(c) {
max = c
p = i
q = j
}
}
}
return p, q
}
//计算cos(theta)及sin(theta)
func cosSinTheta_MatrixEigenClassicalJacobi(A Matrix, p, q int) (float64, float64) {
apq := A.GetFromMatrix(p, q)
app := A.GetFromMatrix(p, p)
aqq := A.GetFromMatrix(q, q)
//情况1app-aqq=0
if app == aqq {
c := math.Sqrt(2.0) / 2.0
switch {
case apq < 0:
return c, -1.0 * c
case apq > 0:
return c, c
default:
panic("MatrixEigenClassicalJacobi/cosSinTheta: A(p, q) = 0")
}
}
//其他情况
c := (app - aqq) / (2.0 * apq)
d := 2.0 * apq / (app - aqq)
//如果|apq| << |app - aqq|用d
if 1000.0*math.Abs(apq) < math.Abs(app-aqq) {
tant := d / (1.0 + math.Sqrt(1.0+d*d))
cost := 1.0 / math.Sqrt(1.0+tant*tant)
sint := tant * cost
return cost, sint
}
// 用c
switch {
case c > 0:
tant := 1.0 / (c + math.Sqrt(1.0+c*c))
cost := 1.0 / math.Sqrt(1.0+tant*tant)
sint := tant * cost
return cost, sint
case c < 0:
tant := -1.0 / (math.Abs(c) + math.Sqrt(1.0+c*c))
cost := 1.0 / math.Sqrt(1.0+tant*tant)
sint := tant * cost
return cost, sint
default:
panic("MatrixEigenClassicalJacobi/cosSinTheta: A(p, q) = 0, c = 0")
}
return 0.0, 0.0
}
//非主对角元素平方之和
func sum2Else_MatrixEigenClassicalJacobi(A Matrix) float64 {
var sum2 float64
for i := 0; i < A.Rows-1; i++ {
for j := i + 1; j < A.Columns; j++ {
if i != j {
sum2 += A.GetFromMatrix(i, j) * A.GetFromMatrix(i, j)
}
}
}
return 2.0 * sum2
}
// MatrixEigenClassicalJacobi 求解n阶对称矩阵A的全部特征值及其特征向量经典雅可比法
func MatrixEigenClassicalJacobi(A Matrix, tol float64, n int) (Matrix, Matrix, bool) {
/*
求解n阶对称矩阵A的全部特征值及其特征向量经典雅可比法
输入 :
A 系数矩阵
tol 最大容许误差
n 最大迭代步数
输出 :
Bbar 主特征值矩阵n阶对角矩阵
Rbar 主特征值所对应的特征向量n维矩阵第i列即对应于
第i个特征值的特征向量
(err) 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断A是否对称矩阵
if !isSymMatrix_MatrixEigenClassicalJacobi(A) {
return ZeroMatrix(A.Rows, A.Columns), ZeroMatrix(A.Rows, A.Columns), false
}
//第一步
//Rbar最终为特征向量矩阵
Rbar := IdentityE(A.Rows)
//复制A矩阵为BB为迭代过程中逐渐改变的矩阵最终将成为特征值矩阵
B := ZeroMatrix(A.Rows, A.Columns)
Bbar := ZeroMatrix(A.Rows, A.Columns)
for i := 0; i < len(A.Data); i++ {
B.Data[i] = A.Data[i]
}
//迭代步
for i := 0; i < n; i++ {
//第二步,最大元素所在行列号
p, q := maxElementElse_MatrixEigenClassicalJacobi(B)
//第三步计算cos(theta)及sin(theta)
cost, sint := cosSinTheta_MatrixEigenClassicalJacobi(B, p, q)
//第四步计算R及Rbar迭代B矩阵
//R为迭代矩阵
R := IdentityE(A.Rows)
R.SetMatrix(p, p, cost)
R.SetMatrix(p, q, sint)
R.SetMatrix(q, p, -1.0*sint)
R.SetMatrix(q, q, cost)
Bbar = DotPruduct(DotPruduct(R, B), R.Transpose()) //A1 = RARt
//Rbar = Rbar*Rt
Rbar = DotPruduct(Rbar, R.Transpose())
//第五步,判断误差
if sum2Else_MatrixEigenClassicalJacobi(Bbar) <= tol {
return Bbar, Rbar, true
}
//A = A1
for i := 0; i < len(Bbar.Data); i++ {
B.Data[i] = Bbar.Data[i]
}
}
return ZeroMatrix(A.Rows, A.Columns), ZeroMatrix(A.Rows, A.Columns), false
}

View File

@@ -0,0 +1,96 @@
// MatrixEigenJacobiPass
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-30
版本 : 0.0.0
------------------------------------------------------
求解n阶对称矩阵A的全部特征值及其特征向量雅可比过关法
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 90.
------------------------------------------------------
输入 :
A 系数矩阵
tol 最大容许误差
n 最大迭代步数
输出 :
Bbar 主特征值矩阵n阶对角矩阵
Rbar 主特征值所对应的特征向量n维矩阵第i列即对应于
第i个特征值的特征向量
(err) 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// MatrixEigenJacobiPass 求解n阶对称矩阵A的全部特征值及其特征向量雅可比过关法
func MatrixEigenJacobiPass(A Matrix, tol float64, n int) (Matrix, Matrix, bool) {
/*
求解n阶对称矩阵A的全部特征值及其特征向量雅可比过关法
输入 :
A 系数矩阵
tol 最大容许误差
n 最大迭代步数
输出 :
Bbar 主特征值矩阵n阶对角矩阵
Rbar 主特征值所对应的特征向量n维矩阵第i列即对应于
第i个特征值的特征向量
(err) 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断A是否对称矩阵
if !isSymMatrix_MatrixEigenClassicalJacobi(A) {
return ZeroMatrix(A.Rows, A.Columns), ZeroMatrix(A.Rows, A.Columns), false
}
//1.
//Rbar最终为特征向量矩阵
Rbar := IdentityE(A.Rows)
//复制A矩阵为BB为迭代过程中逐渐改变的矩阵最终将成为特征值矩阵
B := ZeroMatrix(A.Rows, A.Columns)
Bbar := ZeroMatrix(A.Rows, A.Columns)
for i := 0; i < len(A.Data); i++ {
B.Data[i] = A.Data[i]
}
//2. 计算非对角元素平方和
v0 := sum2Else_MatrixEigenClassicalJacobi(B)
//3. 设置阀值v1
v1 := v0 / float64(B.Rows)
//迭代步
for i := 0; i < n; i++ {
for i0 := 0; i0 < B.Rows-1; i0++ {
for j := i + 1; j < B.Columns; j++ {
//逐个扫描,判断是否大于阀值
if B.GetFromMatrix(i, j) > v1 {
//Jocobi正交相似变换古典Jocobi法
//计算cos(theta)及sin(theta)
cost, sint := cosSinTheta_MatrixEigenClassicalJacobi(B, i, j)
//R为迭代矩阵
R := IdentityE(A.Rows)
R.SetMatrix(i, i, cost)
R.SetMatrix(i, j, sint)
R.SetMatrix(j, i, -1.0*sint)
R.SetMatrix(j, j, cost)
Bbar = DotPruduct(DotPruduct(R, B), R.Transpose()) //A1 = RARt
//Rbar = Rbar*Rt
Rbar = DotPruduct(Rbar, R.Transpose())
}
}
}
//计算并判断v1是否满足误差需求否则迭代
v0 = sum2Else_MatrixEigenClassicalJacobi(Bbar)
if v0 < tol {
return Bbar, Rbar, true
}
v1 = v0 / float64(B.Rows)
//A = A1
for i := 0; i < len(Bbar.Data); i++ {
B.Data[i] = Bbar.Data[i]
}
}
return ZeroMatrix(A.Rows, A.Columns), ZeroMatrix(A.Rows, A.Columns), false
}

100
vendor/github.com/nuknal/goNum/MatrixEigenPower.go generated vendored Normal file
View File

@@ -0,0 +1,100 @@
// MatrixEigenPower
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-23
版本 : 0.0.0
------------------------------------------------------
求解n阶矩阵A的主特征值按模最大及其特征向量
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 78-81.
------------------------------------------------------
输入 :
A 系数矩阵
u n维初始向量
tol 最大容许误差
n 最大迭代步数
输出 :
sol 主特征值
v 主特征值所对应的特征向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// MatrixEigenPower 求解n阶矩阵A的主特征值按模最大及其特征向量
func MatrixEigenPower(A, u0 Matrix, tol float64, n int) (float64, []float64, bool) {
/*
求解n阶矩阵A的主特征值按模最大及其特征向量
输入 :
A 系数矩阵
u n维初始向量
tol 最大容许误差
n 最大迭代步数
输出 :
sol 主特征值
v 主特征值所对应的特征向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断输入正确与否
if A.Rows != u0.Rows {
panic("goNum.MatrixEigenPower: A and u are not matched")
}
u1 := ZeroMatrix(u0.Rows, u0.Columns)
var l0, l1 float64
v1 := make([]float64, u0.Rows)
var err bool = false
var j int
u1 = DotPruduct(A, u0)
for i0 := 0; i0 < u0.Rows; i0++ {
if (math.Abs(u0.Data[i0]) > 1e-3) && (math.Abs(u1.Data[i0]) > 1e-3) {
j = i0
l0 = u1.Data[i0] / u0.Data[i0]
}
u0.Data[i0] = u1.Data[i0]
}
for i := 0; i < n; i++ {
u1 = DotPruduct(A, u0)
l1 = u1.Data[j] / u0.Data[j]
//计算最大值,并进行规范化处理
for i0 := 0; i0 < u0.Rows; i0++ {
v1[i0] = math.Abs(u1.Data[i0])
}
_, j0, _ := Max(v1)
max := u1.Data[j0]
if max > 1e6 {
for i0 := 0; i0 < u0.Rows; i0++ {
u1.Data[i0] = u1.Data[i0] / max
}
}
//判断算出否,并计算对应的特征向量
if math.Abs(l1-l0) < tol {
for i0 := 0; i0 < u0.Rows; i0++ {
u1.Data[i0] = u1.Data[i0] / max
}
err = true
return l1, u1.Data, err
}
//准备下次迭代
l0 = l1
for i0 := 0; i0 < u0.Rows; i0++ {
u0.Data[i0] = u1.Data[i0]
}
}
return 0.0, make([]float64, u0.Rows), err
}

50
vendor/github.com/nuknal/goNum/Max.go generated vendored Normal file
View File

@@ -0,0 +1,50 @@
// Max
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-19
版本 : 0.0.0
------------------------------------------------------
向量第一个最大值及其位置
------------------------------------------------------
输入 :
a a 被处理向量
输出 :
sol 解值
ii 第一个最大值位置
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// Max 向量第一个最大值及其位置
func Max(a []float64) (float64, int, bool) {
/*
向量第一个最大值及其位置
输入 :
a a 被处理向量
输出 :
sol 解值
ii 第一个最大值位置
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var ii int
var err bool = false
n := len(a)
ii = 0
sol = a[ii]
for i := 1; i < n; i++ {
if sol < a[i] {
ii = i
sol = a[i]
}
}
err = true
return sol, ii, err
}

54
vendor/github.com/nuknal/goNum/MaxAbs.go generated vendored Normal file
View File

@@ -0,0 +1,54 @@
// Max
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-19
版本 : 0.0.0
------------------------------------------------------
向量第一个绝对值最大值及其位置
------------------------------------------------------
输入 :
a a 被处理向量
输出 :
sol 解值
ii 第一个绝对值最大值位置
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// MaxAbs 向量第一个绝对值最大值及其位置
func MaxAbs(a []float64) (float64, int, bool) {
/*
向量第一个绝对值最大值及其位置
输入 :
a a 被处理向量
输出 :
sol 解值
ii 第一个最大值位置
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var ii int
var err bool = false
n := len(a)
ii = 0
sol = a[ii]
for i := 1; i < n; i++ {
if math.Abs(sol) < math.Abs(a[i]) {
ii = i
sol = a[i]
}
}
err = true
return sol, ii, err
}

51
vendor/github.com/nuknal/goNum/MaxMinSort.go generated vendored Normal file
View File

@@ -0,0 +1,51 @@
// MaxMinSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-19
版本 : 0.0.0
------------------------------------------------------
向量从大到小的排序
------------------------------------------------------
输入 :
a a 被排序向量
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// MaxMinSort 向量从大到小的排序
func MaxMinSort(a []float64) ([]float64, bool) {
/*
向量从大到小的排序
输入 :
a a 被排序向量
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var err bool = false
var temp float64
var n int = len(a)
sol := make([]float64, n)
for i := 0; i < n; i++ {
sol[i] = a[i]
}
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if sol[i] < sol[j] {
temp = sol[j]
sol[j] = sol[i]
sol[i] = temp
}
}
}
err = true
return sol, err
}

111
vendor/github.com/nuknal/goNum/MergeSort.go generated vendored Normal file
View File

@@ -0,0 +1,111 @@
// MergeSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-06
版本 : 0.0.0
------------------------------------------------------
归并排序法
理论:
时间复杂度: O(nlog2(n))
最好情况 : O(nlog2(n))
最坏情况 : O(nlog2(n))
空间复杂度: O(n)
稳定性 : 稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// mergeSort_merge
// i0 --- first
// i1 --- mid
// i2 --- last
func mergeSort_merge(sol *Matrix, i0, i1, i2 int) {
temp := ZeroMatrix(1, (*sol).Columns)
i := i0
j := i1 + 1
k := 0
for i <= i1 && j <= i2 {
if (*sol).Data[i] < (*sol).Data[j] {
temp.Data[k] = (*sol).Data[i]
k++
i++
} else {
temp.Data[k] = (*sol).Data[j]
k++
j++
}
}
for i <= i1 {
temp.Data[k] = (*sol).Data[i]
k++
i++
}
for j <= i2 {
temp.Data[k] = (*sol).Data[j]
k++
j++
}
for i = 0; i < k; i++ {
(*sol).Data[i0+i] = temp.Data[i]
}
}
// mergeSort_sort
// i0 --- first
// i2 --- last
func mergeSort_sort(sol *Matrix, i0, i2 int) {
if i0 < i2 {
var i1 int = (i0 + i2) / 2
mergeSort_sort(sol, i0, i1)
mergeSort_sort(sol, i1+1, i2)
mergeSort_merge(sol, i0, i1, i2)
}
}
// MergeSort 归并排序法
func MergeSort(in Matrix) (Matrix, bool) {
/*
归并排序法
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if in.Rows != 1 {
panic("Error in goNum.MergeSort: Input Matrix error")
}
if in.Columns < 1 {
panic("Error in goNum.MergeSort: Empty input Matrix")
} else if in.Columns == 1 {
return in, true
}
n := in.Columns
sol := ZeroMatrix(1, n)
var err bool = false
//初始化sol
for i := 0; i < n; i++ {
sol.Data[i] = in.Data[i]
}
//排序开始
mergeSort_sort(&sol, 0, n-1)
err = true
return sol, err
}

50
vendor/github.com/nuknal/goNum/Min.go generated vendored Normal file
View File

@@ -0,0 +1,50 @@
// Min
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-19
版本 : 0.0.0
------------------------------------------------------
向量第一个最小值及其位置
------------------------------------------------------
输入 :
a a 被处理向量
输出 :
sol 解值
ii 第一个最小值位置
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// Min 向量第一个最小值及其位置
func Min(a []float64) (float64, int, bool) {
/*
向量第一个最小值及其位置
输入 :
a a 被处理向量
输出 :
sol 解值
ii 第一个最大值位置
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var ii int
var err bool = false
n := len(a)
ii = 0
sol = a[ii]
for i := 1; i < n; i++ {
if sol > a[i] {
ii = i
sol = a[i]
}
}
err = true
return sol, ii, err
}

54
vendor/github.com/nuknal/goNum/MinAbs.go generated vendored Normal file
View File

@@ -0,0 +1,54 @@
// Min
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-19
版本 : 0.0.0
------------------------------------------------------
向量第一个绝对值最小值及其位置
------------------------------------------------------
输入 :
a a 被处理向量
输出 :
sol 解值
ii 第一个绝对值最小值位置
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// MinAbs 向量第一个绝对值最小值及其位置
func MinAbs(a []float64) (float64, int, bool) {
/*
向量第一个绝对值最小值及其位置
输入 :
a a 被处理向量
输出 :
sol 解值
ii 第一个最大值位置
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var ii int
var err bool = false
n := len(a)
ii = 0
sol = a[ii]
for i := 1; i < n; i++ {
if math.Abs(sol) > math.Abs(a[i]) {
ii = i
sol = a[i]
}
}
err = true
return sol, ii, err
}

51
vendor/github.com/nuknal/goNum/MinMaxSort.go generated vendored Normal file
View File

@@ -0,0 +1,51 @@
// MinMaxSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-19
版本 : 0.0.0
------------------------------------------------------
向量从小到大的排序
------------------------------------------------------
输入 :
a a 被排序向量
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// MinMaxSort 向量从小到大的排序
func MinMaxSort(a []float64) ([]float64, bool) {
/*
向量从小到大的排序
输入 :
a a 被排序向量
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var err bool = false
var temp float64
var n int = len(a)
sol := make([]float64, n)
for i := 0; i < n; i++ {
sol[i] = a[i]
}
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if sol[i] > sol[j] {
temp = sol[j]
sol[j] = sol[i]
sol[i] = temp
}
}
}
err = true
return sol, err
}

115
vendor/github.com/nuknal/goNum/Muller.go generated vendored Normal file
View File

@@ -0,0 +1,115 @@
// Muller
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-20
版本 : 0.0.0
------------------------------------------------------
Muller法求解非线性方程f(x)=0的解
理论:
参考 John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 2.5.2.
------------------------------------------------------
输入 :
fun 求解函数
x0 初值自变量三个不同点3x1
tol 控制误差
n 最大迭代步数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// Muller Muller法求解非线性方程f(x)=0的解
func Muller(fun func(float64) float64, x0 Matrix, tol float64, n int) (float64, bool) {
/*
Muller法求解非线性方程f(x)=0的解
输入 :
fun 求解函数
x0 初值自变量三个不同点3x1
tol 控制误差
n 最大迭代步数
输出 :
sol 解
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断tol
if tol <= 0.0 {
panic("Error in goNum.Muller: tol less than or euqals to zero")
}
var sol float64
var err bool = false
//x0赋给p0并计算对应的y0
p0 := ZeroMatrix(x0.Rows, x0.Columns+1)
x0sort, _ := MinMaxSort(x0.Data)
for i := 0; i < x0.Rows; i++ {
p0.SetMatrix(i, 0, x0sort[i])
p0.SetMatrix(i, 1, fun(x0sort[i]))
}
//迭代计算
for i := 0; i < n; i++ {
//准备系数
h0 := p0.GetFromMatrix(0, 0) - p0.GetFromMatrix(2, 0)
h1 := p0.GetFromMatrix(1, 0) - p0.GetFromMatrix(2, 0)
c := p0.GetFromMatrix(2, 1)
e0 := p0.GetFromMatrix(0, 1) - c
e1 := p0.GetFromMatrix(1, 1) - c
b := h1*h0*h0 - h0*h1*h1
a := (e0*h1 - e1*h0) / b
b = (e1*h0*h0 - e0*h1*h1) / b
//求根
z2 := b*b - 4.0*a*c
if z2 < 0 {
//panic("Error in goNum.Muller: There is complex values exist")
z2 = 0
}
var z float64
if b < 0 {
z = -2.0 * c / (b - math.Sqrt(z2))
}
z = -2.0 * c / (b + math.Sqrt(z2))
z = p0.GetFromMatrix(2, 0) + z
//判断解
if math.Abs(fun(z)) < tol {
err = true
sol = z
return sol, err
}
//删除离z最远的点
dis := []float64{
z - p0.GetFromMatrix(0, 0),
z - p0.GetFromMatrix(1, 0),
z - p0.GetFromMatrix(2, 0)}
_, deli, _ := MaxAbs(dis)
for j := 0; j < 3; j++ {
if deli == j {
p0.SetMatrix(j, 0, z)
}
}
x0sort, _ = MinMaxSort(p0.ColumnOfMatrix(0))
for j := 0; j < 3; j++ {
p0.SetMatrix(j, 0, x0sort[j])
p0.SetMatrix(j, 1, fun(x0sort[j]))
}
}
err = false
return sol, err
}

97
vendor/github.com/nuknal/goNum/NLEs_SeidelIterate.go generated vendored Normal file
View File

@@ -0,0 +1,97 @@
// NLEs_SeidelIterate
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-20
版本 : 0.0.0
------------------------------------------------------
多元非线性方程组Seidel迭代
理论:
Pk = x0
Fk = [f1, f2,..., fn]'
|df1/dx1 df1/dx2 ... df1/dxn|
|df2/dx1 df2/dx2 ... df2/dxn|
Jk = |... ... ... ... |
|dfn/dx1 dfn/dx2 ... dfn/dxn|
Jk*dPk = -Fk
P_(k+1) = Pk+dPk
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 3.7
------------------------------------------------------
输入 :
funs 方程组nx1
J Joccobi矩阵nxn
x0 初值x
tol 控制误差
n 最大迭代次数
输出 :
sol 解nx1
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// NLEs_SeidelIterate 多元非线性方程组Seidel迭代
func NLEs_SeidelIterate(funs, J func(Matrix) Matrix, x0 Matrix,
tol float64, n int) (Matrix, bool) {
/*
多元非线性方程组Seidel迭代
输入 :
funs 方程组nx1
J Joccobi矩阵nxn
x0 初值x
tol 控制误差
n 最大迭代次数
输出 :
sol 解nx1
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//判断x维数
if x0.Columns != 1 {
panic("Error in goNum.NLEs_SeidelIterate: x0 is not a vector")
}
sol := ZeroMatrix(x0.Rows, 1) //解向量
xold := ZeroMatrix(x0.Rows, 1) //Pk
var err bool = false
//将x0赋予xold
for i := 0; i < x0.Rows; i++ {
xold.Data[i] = x0.Data[i]
sol.Data[i] = x0.Data[i]
}
//循环迭代
y := NumProductMatrix(funs(xold), -1.0)
for i := 0; i < n; i++ {
ja := J(xold)
dx, dxerr := LEs_ECPE(Matrix2ToSlices(ja), y.Data)
if dxerr != true {
panic("Error in goNum.NLEs_SeidelIterate: Solve error")
}
//求解新值
for i := 0; i < x0.Rows; i++ {
sol.Data[i] = xold.Data[i] + dx[i]
xold.Data[i] = sol.Data[i]
}
y = NumProductMatrix(funs(xold), -1.0)
//判断误差
maxy, _, _ := MaxAbs(y.Data)
if math.Abs(maxy) < tol {
err = true
return sol, err
}
}
return sol, err
}

82
vendor/github.com/nuknal/goNum/NewtonIterate.go generated vendored Normal file
View File

@@ -0,0 +1,82 @@
// NewtonIterate
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-01
版本 : 0.0.0
------------------------------------------------------
牛顿迭代求解非线性方程 f(x)=0 在区间[a, b]内的根
理论:
(局部收敛定律)
1. f(x)在区间[a, b]具有二阶连续导数;
2. 当xE[a, b]f'(x) != 0
(非局部收敛定律)
1. 当xE[a, b]f'(x)、f''(x)连续且不变号
2. 选取初值x0E[a, b]使f(x0)*f''(x0) > 0
平方收敛
------------------------------------------------------
输入 :
fn f(x)函数定义为等式左侧部分右侧为0
fn1 f'(x)函数
a, b 求解区间
c 求解初值
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import "math"
// NewtonIterate 牛顿迭代求解非线性方程 f(x)=0 在区间[a, b]内的根
func NewtonIterate(fn, fn1 func(float64) float64, a, b, c float64, N int, tol float64) (float64, bool) {
/*
牛顿迭代求解非线性方程 f(x)=0 在区间[a, b]内的根
输入 :
fn f(x)函数定义为等式左侧部分右侧为0
fn1 f'(x)函数
a, b 求解区间
c 求解初值
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var err bool = false
// 判断端点和初值是否为所求之解
switch {
case math.Abs(fn(a)) < tol:
sol = a
err = true
return sol, err
case math.Abs(fn(b)) < tol:
sol = b
err = true
return sol, err
case math.Abs(fn(c)) < tol:
sol = c
err = true
return sol, err
}
//求解
sol = c - fn(c)/fn1(c)
for i := 0; i < N; i++ {
if math.Abs(sol-c) < tol {
err = true
return sol, err
}
c = sol
sol = c - fn(c)/fn1(c)
}
return sol, err
}

79
vendor/github.com/nuknal/goNum/Norm.go generated vendored Normal file
View File

@@ -0,0 +1,79 @@
// Norm
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-21
版本 : 0.0.0
------------------------------------------------------
求向量p范数
理论:
------------------------------------------------------
输入 :
A 向量,nx1
p 指定范数
输出 :
sol 范数值
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
注释 p :
1 1 modulus
2 2 modulus
p p modulus
-1 infinite modulus
------------------------------------------------------
*/
package goNum
import (
"math"
)
// Norm 求向量p范数
func Norm(A Matrix, p float64) (float64, bool) {
/*
求向量p范数
输入 :
A 向量,nx1
p 指定范数
输出 :
sol 范数值
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//A的维数
if A.Columns != 1 {
panic("Error in goNum.Norm: A is not a vector")
}
//判断p的值
if (p < (-1.0)) || ((p > (-1.0)) && (p <= 0.0)) {
panic("Error in goNum.Norm: p is wrong")
}
var sol float64
var err bool = false
switch {
case p == 1.0: //1范数
for i := 0; i < A.Rows; i++ {
sol += math.Abs(A.Data[i])
}
case p == 2.0: //2范数
for i := 0; i < A.Rows; i++ {
sol += A.Data[i] * A.Data[i]
}
sol = math.Sqrt(sol)
case p == -1.0: //无穷范数
sol, _, _ = MaxAbs(A.Data)
sol = math.Abs(sol)
default: //p范数
for i := 0; i < A.Rows; i++ {
sol += math.Pow(A.Data[i], p)
}
sol = math.Pow(sol, 1.0/p)
}
err = true
return sol, err
}

58
vendor/github.com/nuknal/goNum/Norm1.go generated vendored Normal file
View File

@@ -0,0 +1,58 @@
// Norm1
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-21
版本 : 0.0.0
------------------------------------------------------
求矩阵1范数
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 65.
||A||1 = Maxj(Sumi(|aij|))
------------------------------------------------------
输入 :
A 矩阵
输出 :
sol 范数值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// Norm1 求矩阵1范数
func Norm1(A Matrix) (float64, bool) {
/*
求矩阵1范数
输入 :
A 矩阵
输出 :
sol 范数值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var err bool = false
//求取列绝对值的和
col := make([]float64, A.Columns)
for j := 0; j < A.Columns; j++ {
thisColumn := A.ColumnOfMatrix(j)
for i := 0; i < len(thisColumn); i++ {
col[j] += math.Abs(thisColumn[i])
}
}
//求取最大值
sol, _, _ = Max(col)
err = true
return sol, err
}

56
vendor/github.com/nuknal/goNum/NormInf.go generated vendored Normal file
View File

@@ -0,0 +1,56 @@
// NormInf
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-21
版本 : 0.0.0
------------------------------------------------------
求矩阵无穷范数
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 65.
||A||Inf = Maxi(Sumj(|aij|))
------------------------------------------------------
输入 :
A 矩阵
输出 :
sol 范数值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import "math"
// NormInf 求矩阵无穷范数
func NormInf(A Matrix) (float64, bool) {
/*
求矩阵无穷范数
输入 :
A 矩阵
输出 :
sol 范数值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var err bool = false
//求取行绝对值的和
row := make([]float64, A.Rows)
for i := 0; i < A.Rows; i++ {
thisRow := A.RowOfMatrix(i)
for j := 0; j < len(thisRow); j++ {
row[i] += math.Abs(thisRow[j])
}
}
//求取最大值
sol, _, _ = Max(row)
err = true
return sol, err
}

View File

@@ -0,0 +1,96 @@
// ODEAdamsBashforthMoulton
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-26
版本 : 0.0.0
------------------------------------------------------
Adams-Bashforth-Moulton预估校正方法
理论:
预估(外插):
h
p_(k+1) = yk + ---(-9f_(k-3)+37f_(k-2)-59f_(k-1)+55fk)
24
校正(内插):
h
y_(k+1) = yn + ----(f_(k-2)-5f_(k-1)+19fk+9f_(k+1))
24
步长 h < 0.75/|fy(x,y)|
四阶精度
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 9.6.1
------------------------------------------------------
输入 :
fun 被积分函数
x0 初值,2x4
h 步长
n 积分步数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ODEAdamsBashforthMoulton Adams-Bashforth-Moulton预估校正方法
func ODEAdamsBashforthMoulton(fun func(float64, float64) float64, x0 Matrix, h float64, n int) (Matrix, bool) {
/*
Adams-Bashforth-Moulton预估校正方法
输入 :
fun 被积分函数
x0 初值,2x4
h 步长
n 积分步数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if n < 0 {
panic("Error in goNum.ODEAdamsBashforthMoulton: n is not a positive value")
}
//判断初值
if (x0.Rows != 2) || (x0.Columns < 4) {
panic("Error in goNum.ODEAdamsBashforthMoulton: Initial values error")
}
sol := ZeroMatrix(2, n+1)
p := ZeroMatrix(n+1, 1)
var err bool = false
//初值
for i := 0; i < 4; i++ {
sol.SetMatrix(0, i, x0.GetFromMatrix(0, i))
sol.SetMatrix(1, i, x0.GetFromMatrix(1, i))
}
//计算
for i := 4; i < n+1; i++ {
sol.SetMatrix(0, i, sol.GetFromMatrix(0, i-1)+h) //xi
//pi
temp0 := fun(sol.GetFromMatrix(0, i-3), sol.GetFromMatrix(1, i-3))
temp1 := fun(sol.GetFromMatrix(0, i-2), sol.GetFromMatrix(1, i-2))
temp2 := fun(sol.GetFromMatrix(0, i-1), sol.GetFromMatrix(1, i-1))
soltemp := -9.0 * fun(sol.GetFromMatrix(0, i-4), sol.GetFromMatrix(1, i-4))
soltemp += 37.0 * temp0
soltemp += -59.0 * temp1
soltemp += 55.0 * temp2
p.SetMatrix(i, 0, sol.GetFromMatrix(1, i-1)+h*soltemp/24.0)
//yi
soltemp = temp0
soltemp += -5.0 * temp1
soltemp += 19.0 * temp2
soltemp += 9.0 * fun(sol.GetFromMatrix(0, i), p.GetFromMatrix(i, 0))
sol.SetMatrix(1, i, sol.GetFromMatrix(1, i-1)+h*soltemp/24.0)
}
err = true
return sol, err
}

99
vendor/github.com/nuknal/goNum/ODEAdamsEX.go generated vendored Normal file
View File

@@ -0,0 +1,99 @@
// ODEAdamsEX
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-13
版本 : 0.0.0
------------------------------------------------------
四步Adams外推公式显式、线性
理论:
h
y_(n+1) = yn + ----(55f(xn,yn) - 59f(x_(n-1),y_(n-1)) +
24
37f(x_(n-2),y_(n-2)) - 9f(x_(n-3),y_(n-3)))
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 200-201.
------------------------------------------------------
输入 :
fun 被积分函数
x0 初值
xend 积分终止点
fn 方程个数
n 迭代次数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ODEAdamsEX 四步Adams外推公式显式、线性单个方程
func ODEAdamsEX(fun func(Matrix, int) float64, x0 Matrix, xend float64, fn, n int) (Matrix, bool) {
/*
四步Adams外推公式显式、线性单个方程
输入 :
fun 被积分函数
x0 初值
xend 积分终止点
fn 方程个数
n 迭代次数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断方程个数是否对应初值个数
if x0.Rows != fn+1 {
panic("Error in goNum.ODEAdamsEX: Quantities of x0 and fn+1 are not equal")
}
sol := ZeroMatrix(fn+1, n+1)
h := (xend - x0.GetFromMatrix(0, 0)) / float64(n)
//把初值赋给sol
for i := 0; i < fn+1; i++ {
sol.SetMatrix(i, 0, x0.Data[i])
}
//前三个使用RK44计算不包括已有的初值点
xendRK := x0.GetFromMatrix(0, 0) + 3.0*h
solRK, errRK := RK44(fun, x0, xendRK, fn, 3)
if errRK != true {
panic("Error in goNum.ODEAdamsEX: RK44 solving error")
}
//传递RK44计算的结果到sol
for k := 0; k < fn+1; k++ { //fn个方程fn+1个参数
for i := 1; i < 4; i++ { //三个结果
sol.SetMatrix(k, i, solRK.GetFromMatrix(k, i))
}
}
//Adams外推公式, 4(即n+1)需要3,2,1,0四个
for i := 4; i < n+1; i++ {
sol.SetMatrix(0, i, sol.GetFromMatrix(0, i-1)+h) //xi
//临时初值
xyn := ZeroMatrix(fn+1, 1)
xyn_1 := ZeroMatrix(fn+1, 1)
xyn_2 := ZeroMatrix(fn+1, 1)
xyn_3 := ZeroMatrix(fn+1, 1)
for j := 0; j < fn+1; j++ {
xyn.Data[j] = sol.GetFromMatrix(j, i-1)
xyn_1.Data[j] = sol.GetFromMatrix(j, i-2)
xyn_2.Data[j] = sol.GetFromMatrix(j, i-3)
xyn_3.Data[j] = sol.GetFromMatrix(j, i-4)
}
//计算
for j := 0; j < fn; j++ { //不包含xi的其他参数
temp0 := 55.0*fun(xyn, j) - 59.0*fun(xyn_1, j) + 37.0*fun(xyn_2, j) - 9.0*fun(xyn_3, j)
temp0 = xyn.Data[j+1] + temp0*h/24.0
sol.SetMatrix(j+1, i, temp0) //yi
}
}
return sol, true
}

120
vendor/github.com/nuknal/goNum/ODEAdamsIN.go generated vendored Normal file
View File

@@ -0,0 +1,120 @@
// ODEAdamsIN
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-13
版本 : 0.0.0
------------------------------------------------------
三次Adams内插公式隐式、线性
理论:
h
y_(n+1) = yn + ----(9f(x_(n+1),y_(n+1)) + 19f(xn,yn) -
24
5f(x_(n-1),y_(n-1)) + f(x_(n-2),y_(n-2)))
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 201-202.
------------------------------------------------------
输入 :
fun 被积分函数
x0 初值
xend 积分终止点
tol 内迭代控制误差
fn 方程个数
n 迭代次数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// ODEAdamsIN 四步Adams外推公式显式、线性单个方程
func ODEAdamsIN(fun func(Matrix, int) float64, x0 Matrix, xend, tol float64, fn, n int) (Matrix, bool) {
/*
四步Adams外推公式显式、线性单个方程
输入 :
fun 被积分函数
x0 初值
xend 积分终止点
tol 内迭代控制误差
fn 方程个数
n 迭代次数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断方程个数是否对应初值个数
if x0.Rows != fn+1 {
panic("Error in goNum.ODEAdamsEX: Quantities of x0 and fn+1 are not equal")
}
sol := ZeroMatrix(fn+1, n+1)
h := (xend - x0.GetFromMatrix(0, 0)) / float64(n)
//把初值赋给sol
for i := 0; i < fn+1; i++ {
sol.SetMatrix(i, 0, x0.Data[i])
}
//前三个使用RK44计算不包括已有的初值点
xendRK := x0.GetFromMatrix(0, 0) + 3.0*h
solRK, errRK := RK44(fun, x0, xendRK, fn, 3)
if errRK != true {
panic("Error in goNum.ODEAdamsEX: RK44 solving error")
}
//传递RK44计算的结果到sol
for k := 0; k < fn+1; k++ { //fn个方程fn+1个参数
for i := 1; i < 4; i++ { //三个结果
sol.SetMatrix(k, i, solRK.GetFromMatrix(k, i))
}
}
//三次Adams内插公式, 4(即n+1)需要3,2,1,0四个
for i := 4; i < n+1; i++ {
sol.SetMatrix(0, i, sol.GetFromMatrix(0, i-1)+h) //xi
//临时初值
xyn := ZeroMatrix(fn+1, 1)
xyn_1 := ZeroMatrix(fn+1, 1)
xyn_2 := ZeroMatrix(fn+1, 1)
xyn_3 := ZeroMatrix(fn+1, 1)
xyn10 := ZeroMatrix(fn+1, 1)
for j := 0; j < fn+1; j++ {
xyn.Data[j] = sol.GetFromMatrix(j, i-1)
xyn_1.Data[j] = sol.GetFromMatrix(j, i-2)
xyn_2.Data[j] = sol.GetFromMatrix(j, i-3)
xyn_3.Data[j] = sol.GetFromMatrix(j, i-4)
}
xyn10.Data[0] = sol.GetFromMatrix(0, i) //x_(n+1)
//内插公式隐式迭代初值为4步Adams外推公式结果
for j := 0; j < fn; j++ { //不包含xi的其他参数
temp0 := 55.0*fun(xyn, j) - 59.0*fun(xyn_1, j) + 37.0*fun(xyn_2, j) - 9.0*fun(xyn_3, j)
xyn10.Data[j+1] = xyn.Data[j+1] + temp0*h/24.0 //y_(n+1)0
}
//内插,对每个公式
for j := 0; j < fn; j++ {
//隐式迭代,误差控制
yn1k := xyn10.Data[j+1]
for {
temp0 := 9.0*fun(xyn10, j) + 19.0*fun(xyn, j) - 5.0*fun(xyn_1, j) + fun(xyn_2, j)
temp0 = xyn.Data[j+1] + h*temp0/24.0
if math.Abs(temp0-yn1k) < tol {
sol.SetMatrix(j+1, i, temp0)
break //跳出无条件循环
}
yn1k = temp0
}
}
}
return sol, true
}

120
vendor/github.com/nuknal/goNum/ODEDiff.go generated vendored Normal file
View File

@@ -0,0 +1,120 @@
// ODEDiff
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-26
版本 : 0.0.0
------------------------------------------------------
差分方法求解常微分方程
理论:
对于常微分方程x''(t) = p(t)x'(t)+q(t)(t)+r(t)
机器边值x(a) = x0, x(b) = xN
使用中心差分公式可得
x_(j+1)-2xj+x_(j-1) x_(j+1)-x_(j-1)
--------------------- = pj----------------- + qj*xj+rj
h^2 2h
-h h
(---pj-1)x_(j-1) + (2+h^2*qj)xj + (---pj-1)x_(j+1) = -h^2*rj
2 2
下标j表示*(tj), tj=a+j*h(区间[a, b]等分为N等份)
整理成N-1阶线性方程组:
|2+h^2*q1 h*p1/2-1 |
|-h*p2/2-1 2+h^2*q2 h*p2/2-1 |
| -h*p3/2-1 2+h^2*q3 h*p3/2-1 |*
| ...... |
| -h*p_(N-2)/2-1 2+h^2*q_(N-2) h*p_(N-2)/2-1|
| -h*p_(N-1)/2-1 2+h^2*q_(N-1)|
[x1 x2 x3 ... x_(N-1)]' =
[-h^2*r1+e0 -h^2*r2 -h^2*r3 ... -h^2*r_(N-2) -h^2*r_(N-1)+eN]'
其中:
e0 = (h*p1/2+1)x0, eN = (h*p_(N-1)/2+1)xN
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 9.9
------------------------------------------------------
输入 :
funp, funq, funr 被积分函数系数
x0 初值,2x2, 按列a, b
Nn 积分步数
输出 :
sol 解矩阵, 2x(Nn+1)
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ODEDiff 差分方法求解常微分方程
func ODEDiff(funp, funq, funr func(float64) float64, x0 Matrix, Nn int) (Matrix, bool) {
/*
差分方法求解常微分方程
输入 :
funp, funq, funr 被积分函数系数
x0 初值,2x2, 按列a, b
Nn 积分步数
输出 :
sol 解矩阵, 2x(Nn+1)
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断x0维数
if (x0.Rows != 2) || (x0.Columns != 2) {
panic("Error in goNum.ODEDiff: Initial values error")
}
//判断Nn
if Nn < 1 {
panic("Error in goNum.ODEDiff: Nn must greater than zero")
} else if Nn == 1 {
return x0, true
}
sol := ZeroMatrix(2, Nn+1)
var err bool = false
Aa := ZeroMatrix(Nn-1, Nn-1)
Bb := ZeroMatrix(Nn-1, 1)
h := (x0.GetFromMatrix(0, 1) - x0.GetFromMatrix(0, 0)) / float64(Nn)
//ti
for i := 0; i < Nn+1; i++ {
sol.SetMatrix(0, i, x0.GetFromMatrix(0, 0)+h*float64(i))
}
//x0, xN
sol.SetMatrix(1, 0, x0.GetFromMatrix(1, 0))
sol.SetMatrix(1, Nn, x0.GetFromMatrix(1, 1))
//第一行
Aa.SetMatrix(0, 0, 2.0+h*h*funq(sol.GetFromMatrix(0, 1)))
Aa.SetMatrix(0, 1, h*funp(sol.GetFromMatrix(0, 1))/2.0-1.0)
e0 := (h*funp(sol.GetFromMatrix(0, 1))/2.0 + 1.0) * sol.GetFromMatrix(1, 0)
Bb.SetMatrix(0, 0, -1.0*h*h*funr(sol.GetFromMatrix(0, 1))+e0)
for i := 1; i < Nn-2; i++ {
Aa.SetMatrix(i, i-1, -1.0*h*funp(sol.GetFromMatrix(0, i+1))/2.0-1.0)
Aa.SetMatrix(i, i, 2.0+h*h*funq(sol.GetFromMatrix(0, i+1)))
Aa.SetMatrix(i, i+1, h*funp(sol.GetFromMatrix(0, i+1))/2.0-1.0)
Bb.SetMatrix(i, 0, -1.0*h*h*funr(sol.GetFromMatrix(0, i+1)))
}
//最后行
Aa.SetMatrix(Nn-2, Nn-2-1, -1.0*h*funp(sol.GetFromMatrix(0, Nn-1))/2.0-1.0)
Aa.SetMatrix(Nn-2, Nn-2, 2.0+h*h*funq(sol.GetFromMatrix(0, Nn-1)))
eN := (-1.0*h*funp(sol.GetFromMatrix(0, Nn-1))/2.0 + 1.0) * sol.GetFromMatrix(1, Nn)
Bb.SetMatrix(Nn-2, 0, -1.0*h*h*funr(sol.GetFromMatrix(0, Nn-1))+eN)
//求解线性方程组LEs_Chasing
xTemp, errTemp := LEs_Chasing(Aa, Bb)
if errTemp != true {
panic("Error in goNum.ODEDiff: Solve error")
}
//xTemp赋予sol
for i := 1; i < Nn; i++ {
sol.SetMatrix(1, i, xTemp.GetFromMatrix(i-1, 0))
}
err = true
return sol, err
}

75
vendor/github.com/nuknal/goNum/ODEEuler.go generated vendored Normal file
View File

@@ -0,0 +1,75 @@
// ODEEuler
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-13
版本 : 0.0.0
------------------------------------------------------
常微分方程的Euler欧拉解法
理论:
对于常微分方程
dy
---- = f(x, y)
dx
y(x0) = y0, x0 <= x
Euler欧拉解法
y_(n+1) = yn + hf(xn, yn), n = 0,1,2,3,...
欧拉法是条件稳定的: 0 <= h <=-2.0/(y'/y)
欧拉法为一阶精度的方法
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 179.
------------------------------------------------------
输入 :
fun 被积分函数
x0, y0 初值
h 积分步长
n 迭代次数
输出 :
sol 解矩阵nx2
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ODEEuler 常微分方程的Euler欧拉解法
func ODEEuler(fun func(float64, float64) float64, x0, y0, h float64, n int) (Matrix, bool) {
/*
常微分方程的Euler欧拉解法
输入 :
fun 被积分函数
x0, y0 初值
h 积分步长
n 迭代次数
输出 :
sol 解矩阵nx2
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if n < 0 {
panic("Error in goNum.ODEEuler: n is not a positive value")
}
sol := ZeroMatrix(n+1, 2)
var err bool = false
//初值
sol.SetMatrix(0, 0, x0)
sol.SetMatrix(0, 1, y0)
for i := 0; i < n; i++ {
xi := sol.GetFromMatrix(i, 0)
xi1 := xi + h
yi1 := sol.GetFromMatrix(i, 1) + h*fun(xi, sol.GetFromMatrix(i, 1))
sol.SetMatrix(i+1, 0, xi1)
sol.SetMatrix(i+1, 1, yi1)
}
err = true
return sol, err
}

View File

@@ -0,0 +1,80 @@
// ODEEulerPredictorCorrector
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-13
版本 : 0.0.0
------------------------------------------------------
常微分方程的Euler欧拉预估校正解法
理论:
对于常微分方程
dy
---- = f(x, y)
dx
y(x0) = y0, x0 <= x
Euler欧拉解法
y_(n+1)0 = yn + hf(xn, yn)
y_(n+1) = yn + h(f(xn, yn)+f(x_(n+1), y_(n+1)0))/2
n = 0,1,2,3,...
欧拉法是条件稳定的: |1+(y'/y)h+((y'/y)h)^2/2| <= 1.0
欧拉法为二阶精度的方法
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 182.
------------------------------------------------------
输入 :
fun 被积分函数
x0, y0 初值
h 积分步长
n 迭代次数
输出 :
sol 解矩阵nx2
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ODEEulerPredictorCorrector 常微分方程的Euler欧拉预估校正解法
func ODEEulerPredictorCorrector(fun func(float64, float64) float64, x0, y0, h float64, n int) (Matrix, bool) {
/*
常微分方程的Euler欧拉预估校正解法
输入 :
fun 被积分函数
x0, y0 初值
h 积分步长
n 迭代次数
输出 :
sol 解矩阵nx2
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if n < 0 {
panic("Error in goNum.ODEEulerPredictorCorrector: n is not a positive value")
}
sol := ZeroMatrix(n+1, 2)
var err bool = false
//初值
sol.SetMatrix(0, 0, x0)
sol.SetMatrix(0, 1, y0)
for i := 0; i < n; i++ {
xi := sol.GetFromMatrix(i, 0)
yi := sol.GetFromMatrix(i, 1)
xi1 := xi + h
temp0 := fun(xi, yi)
yi10 := yi + h*temp0
yi1 := yi + h*(temp0+fun(xi1, yi10))/2.0
sol.SetMatrix(i+1, 0, xi1)
sol.SetMatrix(i+1, 1, yi1)
}
err = true
return sol, err
}

96
vendor/github.com/nuknal/goNum/ODEHamming.go generated vendored Normal file
View File

@@ -0,0 +1,96 @@
// ODEHamming
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-26
版本 : 0.0.0
------------------------------------------------------
Hamming预估校正方法
理论:
预估:
4h
p_(k+1) = y_(k-3) + ---(2f_(k-2)-f_(k-1)+2fk)
3
校正):
-y_(k-2)+9yk 3h
y_(k+1) = -------------- + ---(-f_(k-1)+2fk+f_(k+1))
8 8
步长 h < 0.69/|fy(x,y)|
四阶精度
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 9.6.6
------------------------------------------------------
输入 :
fun 被积分函数
x0 初值,2x4
h 步长
n 积分步数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ODEHamming Hamming预估校正方法
func ODEHamming(fun func(float64, float64) float64, x0 Matrix, h float64, n int) (Matrix, bool) {
/*
Hamming预估校正方法
输入 :
fun 被积分函数
x0 初值,2x4
h 步长
n 积分步数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if n < 0 {
panic("Error in goNum.Hamming: n is not a positive value")
}
//判断初值
if (x0.Rows != 2) || (x0.Columns < 4) {
panic("Error in goNum.Hamming: Initial values error")
}
sol := ZeroMatrix(2, n+1)
p := ZeroMatrix(n+1, 1)
var err bool = false
//初值
for i := 0; i < 4; i++ {
sol.SetMatrix(0, i, x0.GetFromMatrix(0, i))
sol.SetMatrix(1, i, x0.GetFromMatrix(1, i))
}
//计算
for i := 4; i < n+1; i++ {
sol.SetMatrix(0, i, sol.GetFromMatrix(0, i-1)+h) //xi
//pi
temp0 := fun(sol.GetFromMatrix(0, i-3), sol.GetFromMatrix(1, i-3)) //f_(i-3)
temp1 := fun(sol.GetFromMatrix(0, i-2), sol.GetFromMatrix(1, i-2)) //f_(i-2)
temp2 := fun(sol.GetFromMatrix(0, i-1), sol.GetFromMatrix(1, i-1)) //f_(i-1)
soltemp := 2.0 * temp0
soltemp += -1.0 * temp1
soltemp += 2.0 * temp2
p.SetMatrix(i, 0, sol.GetFromMatrix(1, i-4)+4.0*h*soltemp/3.0)
//yi
soltemp = -1.0 * temp1
soltemp += 2.0 * temp2
soltemp += fun(sol.GetFromMatrix(0, i), p.GetFromMatrix(i, 0)) //fi
soltemp = 3.0 * h * soltemp / 8.0
soltemp += (-1.0*sol.GetFromMatrix(1, i-3) + 9.0*sol.GetFromMatrix(1, i-1)) / 8.0
sol.SetMatrix(1, i, soltemp)
}
err = true
return sol, err
}

79
vendor/github.com/nuknal/goNum/ODEHeun.go generated vendored Normal file
View File

@@ -0,0 +1,79 @@
// ODEHeun
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-26
版本 : 0.0.0
------------------------------------------------------
常微分方程的Heun解法
理论:
对于常微分方程
dy
---- = f(x, y)
dx
y(x0) = y0, x0 <= x
Heun法为
1. p_(k+1) = yk+hf(xk,yk) //欧拉法
2. y_(k+1) = yk+h(f(xk,yk)+f(x_(k+1),p_(k+1))/2 //梯形法
k = 0,1,2,3,...
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 9.3
------------------------------------------------------
输入 :
fun 被积分函数
x0, y0 初值
h 步长
n 迭代次数
输出 :
sol 解矩阵nx2
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ODEHeun 常微分方程的Heun解法
func ODEHeun(fun func(float64, float64) float64, x0, y0, h float64, n int) (Matrix, bool) {
/*
常微分方程的Heun解法
输入 :
fun 被积分函数
x0, y0 初值
h 步长
n 迭代次数
输出 :
sol 解矩阵nx2
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if n < 0 {
panic("Error in goNum.ODEHeun: n is not a positive value")
}
sol := ZeroMatrix(n+1, 2)
p := ZeroMatrix(n+1, 2)
var err bool = false
//初值
sol.SetMatrix(0, 0, x0)
sol.SetMatrix(0, 1, y0)
for i := 1; i < n+1; i++ {
p.SetMatrix(i, 0, sol.GetFromMatrix(i-1, 0)+h) //xi=x_(i-1)+h
sol.SetMatrix(i, 0, sol.GetFromMatrix(i-1, 0)+h) //xi=x_(i-1)+h
soltemp := fun(sol.GetFromMatrix(i-1, 0), sol.GetFromMatrix(i-1, 1))
p.SetMatrix(i, 1, sol.GetFromMatrix(i-1, 1)+h*soltemp)
soltemp = h * (soltemp + fun(sol.GetFromMatrix(i, 0), p.GetFromMatrix(i, 1))) / 2.0
sol.SetMatrix(i, 1, sol.GetFromMatrix(i-1, 1)+soltemp)
}
err = true
return sol, err
}

94
vendor/github.com/nuknal/goNum/ODEMilneSimpson.go generated vendored Normal file
View File

@@ -0,0 +1,94 @@
// ODEMilneSimpson
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-26
版本 : 0.0.0
------------------------------------------------------
Milne-Simpson预估校正方法
理论:
预估:
4h
p_(k+1) = y_(k-3) + ---(2f_(k-2)-f_(k-1)+2fk)
3
校正:
h
y_(k+1) = y_(k-1) + ---(f_(k-1)+4fk+f_(k+1))
3
步长 h < 0.45/|fy(x,y)|
四阶精度
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 9.6.4
------------------------------------------------------
输入 :
fun 被积分函数
x0 初值,2x4
h 步长
n 积分步数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ODEMilneSimpson Milne-Simpson预估校正方法
func ODEMilneSimpson(fun func(float64, float64) float64, x0 Matrix, h float64, n int) (Matrix, bool) {
/*
Milne-Simpson预估校正方法
输入 :
fun 被积分函数
x0 初值,2x4
h 步长
n 积分步数
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if n < 0 {
panic("Error in goNum.ODEMilneSimpson: n is not a positive value")
}
//判断初值
if (x0.Rows != 2) || (x0.Columns < 4) {
panic("Error in goNum.ODEMilneSimpson: Initial values error")
}
sol := ZeroMatrix(2, n+1)
p := ZeroMatrix(n+1, 1)
var err bool = false
//初值
for i := 0; i < 4; i++ {
sol.SetMatrix(0, i, x0.GetFromMatrix(0, i))
sol.SetMatrix(1, i, x0.GetFromMatrix(1, i))
}
//计算
for i := 4; i < n+1; i++ {
sol.SetMatrix(0, i, sol.GetFromMatrix(0, i-1)+h) //xi
//pi
temp0 := fun(sol.GetFromMatrix(0, i-3), sol.GetFromMatrix(1, i-3)) //f_(i-3)
temp1 := fun(sol.GetFromMatrix(0, i-2), sol.GetFromMatrix(1, i-2)) //f_(i-2)
temp2 := fun(sol.GetFromMatrix(0, i-1), sol.GetFromMatrix(1, i-1)) //f_(i-1)
soltemp := 2.0 * temp0
soltemp += -1.0 * temp1
soltemp += 2.0 * temp2
p.SetMatrix(i, 0, sol.GetFromMatrix(1, i-4)+4.0*h*soltemp/3.0)
//yi
soltemp = temp1
soltemp += 4.0 * temp2
soltemp += fun(sol.GetFromMatrix(0, i), p.GetFromMatrix(i, 0)) //fi
sol.SetMatrix(1, i, sol.GetFromMatrix(1, i-2)+h*soltemp/3.0)
}
err = true
return sol, err
}

96
vendor/github.com/nuknal/goNum/ODETrapezoid.go generated vendored Normal file
View File

@@ -0,0 +1,96 @@
// ODETrapezoid
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-13
版本 : 0.0.0
------------------------------------------------------
常微分方程的梯形解法
理论:
对于常微分方程
dy
---- = f(x, y)
dx
y(x0) = y0, x0 <= x
梯形解法:
h
y_(n+1) = yn + ---(f(xn, yn)+f(x_(n+1), y_(n+1))), n = 0,1,2,3,...
2
梯形法是无条件稳定的
梯形法为二阶精度的方法
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 181.
------------------------------------------------------
输入 :
fun 被积分函数
x0, y0 初值
h 积分步长
tol 内循环控制误差
n 迭代次数
输出 :
sol 解矩阵nx2
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// ODETrapezoid 常微分方程的梯形解法
func ODETrapezoid(fun func(float64, float64) float64, x0, y0, h, tol float64, n int) (Matrix, bool) {
/*
常微分方程的梯形解法
输入 :
fun 被积分函数
x0, y0 初值
h 积分步长
tol 内循环控制误差
n 迭代次数
输出 :
sol 解矩阵nx2
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断n
if n < 0 {
panic("Error in goNum.ODETrapezoid: n is not a positive value")
}
sol := ZeroMatrix(n+1, 2)
var err bool = false
//初值
sol.SetMatrix(0, 0, x0)
sol.SetMatrix(0, 1, y0)
for i := 0; i < n; i++ {
xi := sol.GetFromMatrix(i, 0)
yi := sol.GetFromMatrix(i, 1)
xi10 := xi + h
yi10 := yi + h*fun(xi, yi)
//内循环
yik := make([]float64, 0)
yik = append(yik, yi10) //k=0
var k int = 0
for {
yik = append(yik, yi+h*(fun(xi, yi)+fun(xi10, yik[k]))/2.0)
if math.Abs(yik[k+1]-yik[k]) < tol {
break
}
k++
}
sol.SetMatrix(i+1, 0, xi10)
sol.SetMatrix(i+1, 1, yik[k+1])
}
err = true
return sol, err
}

137
vendor/github.com/nuknal/goNum/OptimizeFibonacci.go generated vendored Normal file
View File

@@ -0,0 +1,137 @@
// OptimizeFibonacci
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-24
版本 : 0.0.0
------------------------------------------------------
Fibonacci搜索法求单峰单自变量极小值
理论:
对于在区间[a, b]内有定义的凹函数f(x),取点:
ck = ak+(1-r)(bk-ak)
d = ak+rk(bk-ak)
其中r为Fibonacci数列值之比F_(n-k-1)/F_(n-k)
迭代次数n应使得Fn > (b0-a0)/tol
如果f(c) <= f(d)则将d赋予bc赋予d继续迭代
如果f(c) > f(d)则将c赋予ad赋予c继续迭代。
迭代终止条件为Abs(f(a)-f(b)) < tol取区间中值
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 8.1.1.2,并改进
------------------------------------------------------
输入 :
fun 函数
a, b 区间范围
tol 控制误差
输出 :
sol 解
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
import "math"
// OptimizeFibonacci Fibonacci搜索法求单峰单自变量极小值
func OptimizeFibonacci(fun func(float64) float64, a, b, tol float64) (float64, bool) {
/*
Fibonacci搜索法求单峰单自变量极小值
输入 :
fun 函数
a, b 区间范围
tol 控制误差
输出 :
sol 解
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//判断a和b的关系
if math.Abs(fun(a)-fun(b)) < tol {
if fun(a) < fun(b) {
return a, true
} else {
return b, true
}
}
var sol float64
var err bool = false
var n, cdFlag int = 0, 0 //cdFlag---下一步计算ccdFlag=0还是dcdFlag=1
//计算n
bat := (fun(b) - fun(a)) / tol
for i := 0; i < 1e6; i++ {
if float64(Fibonacci(i)) > bat {
n = i
break
}
}
//计算
//第一步计算两次c、d
fnn := float64(Fibonacci(n-1)) / float64(Fibonacci(n))
ba := b - a
c := a + (1.0-fnn)*ba
d := a + fnn*ba
fc := fun(c)
fd := fun(d)
if fc <= fd {
b = d
d = c
fd = fc
cdFlag = 0
} else {
a = c
c = d
fc = fd
cdFlag = 1
}
//0 < k < n-3
for k := 1; k < n-3; k++ {
fnn = float64(Fibonacci(n-k-1)) / float64(Fibonacci(n-k))
ba = b - a
if cdFlag == 0 { //计算c
c = a + (1.0-fnn)*ba
fc = fun(c)
} else { //计算d
d = a + fnn*ba
fd = fun(d)
}
//下一步
if fc <= fd {
b = d
d = c
fd = fc
cdFlag = 0
} else {
a = c
c = d
fc = fd
cdFlag = 1
}
}
//k=n-3, F2/F3 = 1/2, 不放入循环是为减少if判断的损耗
fnn = 0.5 - 0.01 //加区别常数0.01
ba = b - a
if cdFlag == 0 { //计算c
c = a + (1.0-fnn)*ba
fc = fun(c)
} else { //计算d
d = a + fnn*ba
fd = fun(d)
}
if fc <= fd {
b = d
} else {
a = c
}
sol = (b + a) / 2.0
err = true
return sol, err
}

91
vendor/github.com/nuknal/goNum/OptimizeGS.go generated vendored Normal file
View File

@@ -0,0 +1,91 @@
// OptimizeGS
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-24
版本 : 0.0.0
------------------------------------------------------
黄金分割法(Golden Section)求单峰单自变量极小值
理论:
对于在区间[a, b]内有定义的凹函数f(x),取黄金分割点:
c = a+(1-r)(b-a)
d = b-(1-r)(b-a)
其中r为黄金分割比例(Sqrt(5)-1)/2
如果f(c) <= f(d)则将d赋予b继续迭代
如果f(c) > f(d)则将c赋予a继续迭代。
迭代终止条件为Abs(f(a)-f(b)) < tol取小值c或d
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 8.1.1.1
------------------------------------------------------
输入 :
fun 函数
a, b 区间范围
tol 控制误差
N 最大迭代步数
输出 :
sol 解
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// OptimizeGS 黄金分割法(Golden Section)求单峰单自变量极小值
func OptimizeGS(fun func(float64) float64, a, b, tol float64, N int) (float64, bool) {
/*
黄金分割法(Golden Section)求单峰单自变量极小值
输入 :
fun 函数
a, b 区间范围
tol 控制误差
N 最大迭代步数
输出 :
sol 解
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//判断a和b的关系
if math.Abs(fun(a)-fun(b)) < tol {
if fun(a) < fun(b) {
return a, true
} else {
return b, true
}
}
var sol float64
var err bool = false
r1 := 1.0 - (math.Sqrt(5.0)-1.0)/2.0 //1-r
for i := 0; i < N; i++ {
ba := b - a //b-a
c := a + r1*ba
d := b - r1*ba
//区间压缩
if fun(c) > fun(d) {
a = c
} else { //fun(c)<=fun(d)
b = d
}
//误差判断
if math.Abs(fun(a)-fun(b)) < tol {
err = true
if fun(c) < fun(d) {
sol = c
} else {
sol = d
}
return sol, err
}
}
return sol, err
}

197
vendor/github.com/nuknal/goNum/OptimizeSimplex.go generated vendored Normal file
View File

@@ -0,0 +1,197 @@
// OptimizeSimplex
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-25
版本 : 0.0.0
------------------------------------------------------
Nelder-Mead单纯形法求解多自变量函数极小值
理论:
对于函数z=f(x0,x1,...,xn),取三个相异的点构成三角形,并按函数值
从小到大排序为B、G、W依下列方法进行操作
0. 取BG中点M = (B+G)/2
1. 取反射点R = M+(M-W)
2. 取延伸点E = R+(R-M)
3. 收缩点C = zMin(C1=M+(W-M)/2, C2=M+(M-W)/2)
4. 收缩点S = (B+W)/2。
1~4每一步计算函数值并置换排序BGW
n个x需要n+1个初始点
参考John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 8.2.1
------------------------------------------------------
输入 :
fun 函数表达式
x0 初始点nx(n+1)第一行x0第二行x1,...
tol 控制误差
Nn 最大迭代步数
输出 :
sol 解,(n+1)x1
|xPath 自变量变化历程二维浮点可使用Slices2ToMatrix函数转换为Matrix类型
|fxPath 函数值变化历程一维浮点可使用Slices1ToMatrix函数转换为Matrix类型
|errPath 误差绝对值历程一维浮点可使用Slices1ToMatrix函数转换为Matrix类型
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// OptimizeSimplex Nelder-Mead单纯形法求解多自变量函数极小值
func OptimizeSimplex(fun func(Matrix) float64, x0 Matrix, tol float64, Nn int) (Matrix, bool) {
/*
Nelder-Mead单纯形法求解多自变量函数极小值
输入 :
fun 函数表达式
x0 初始点nx(n+1)第一行x0第二行x1,...
tol 控制误差
Nn 最大迭代步数
输出 :
sol 解,(n+1)x1
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//判断x0大小
n := x0.Rows //xi
if x0.Columns != n+1 { //初始点个数等于自变量个数加一
panic("Error in goNum.OptimizeSimplex: Initial values error")
}
//判断N
if Nn < 1 {
panic("Error in goNum.OptimizeSimplex: Iteration number error")
}
sol := ZeroMatrix(n+1, 1)
xPath := make([][]float64, 0)
fxPath := make([]float64, 0)
errPath := make([]float64, 0)
var err bool = false
//计算f(x)
for i := 0; i < n+1; i++ {
sol.Data[i] = fun(NewMatrix(n, 1, x0.ColumnOfMatrix(i)))
}
//取最大、最小、次大和次小序号
_, l0, _ := Min(sol.Data) //最小
_, h0, _ := Max(sol.Data) //最大
l1 := h0 //次小
h1 := l0 //次大
for i := 0; i < n+1; i++ {
if (i != l0) && (i != h0) && (sol.Data[i] < sol.Data[l1]) {
l1 = i
}
if (i != l0) && (i != h0) && (sol.Data[i] > sol.Data[h1]) {
h1 = i
}
}
xPath = append(xPath, x0.ColumnOfMatrix(l0))
fxPath = append(fxPath, sol.Data[l0])
errPath = append(errPath, math.Abs(sol.Data[h0]-sol.Data[l0]))
//迭代
for i := 0; i < Nn; i++ {
//中点M = (Sum-W)/n
temp0 := ZeroMatrix(n, 1)
for j := 0; j < n+1; j++ {
temp0 = AddMatrix(temp0, NewMatrix(n, 1, x0.ColumnOfMatrix(j)))
}
mm := NumProductMatrix(SubMatrix(temp0, NewMatrix(n, 1, x0.ColumnOfMatrix(h0))), 1.0/float64(n))
//反射点R = 2M-W
rr := SubMatrix(NumProductMatrix(mm, 2.0), NewMatrix(n, 1, x0.ColumnOfMatrix(h0)))
fr := fun(rr)
//判断
if fr < sol.Data[h1] { //fr<fh1, case1
if fr > sol.Data[l1] { //R-->W
for j := 0; j < n; j++ {
x0.SetMatrix(j, h0, rr.Data[j])
}
sol.Data[h0] = fr
} else { //延伸E
ee := SubMatrix(NumProductMatrix(rr, 2.0), mm)
fe := fun(ee)
if fe < sol.Data[l1] { //E-->W
for j := 0; j < n; j++ {
x0.SetMatrix(j, h0, ee.Data[j])
}
sol.Data[h0] = fe
} else { //R-->W
for j := 0; j < n; j++ {
x0.SetMatrix(j, h0, rr.Data[j])
}
sol.Data[h0] = fr
}
}
} else { //case 2
if fr < sol.Data[h0] {
for j := 0; j < n; j++ {
x0.SetMatrix(j, h0, rr.Data[j])
}
sol.Data[h0] = fr
}
//C1 = (W+M)/2, C2 = (R+M)/2默认C=C1
cc := NumProductMatrix(AddMatrix(NewMatrix(n, 1, x0.ColumnOfMatrix(h0)), mm), 0.5)
fc := fun(cc)
c2 := NumProductMatrix(AddMatrix(rr, mm), 0.5)
fc2 := fun(c2)
//判断获得C
if fc > fc2 {
for j := 0; j < n; j++ {
cc.Data[j] = c2.Data[j]
}
fc = fc2
}
if fc < sol.Data[h0] {
for j := 0; j < n; j++ {
x0.SetMatrix(j, h0, cc.Data[j])
}
sol.Data[h0] = fc
} else { //xj = (xj+x0)/2
for j := 0; j < n+1; j++ {
if j != l0 {
temp1 := NumProductMatrix(AddMatrix(NewMatrix(n, 1, x0.ColumnOfMatrix(j)),
NewMatrix(n, 1, x0.ColumnOfMatrix(l0))), 0.5)
for k := 0; k < n; k++ {
x0.SetMatrix(k, j, temp1.Data[k])
}
sol.Data[j] = fun(temp1)
}
}
}
}
//下一步
_, l0, _ = Min(sol.Data) //最小
_, h0, _ = Max(sol.Data) //最大
l1 = h0 //次小
h1 = l0 //次大
for j := 0; j < n+1; j++ {
if (j != l0) && (j != h0) && (sol.Data[j] < sol.Data[l1]) {
l1 = j
}
if (j != l0) && (j != h0) && (sol.Data[j] > sol.Data[h1]) {
h1 = j
}
}
//记录历程
xPath = append(xPath, x0.ColumnOfMatrix(l0))
fxPath = append(fxPath, sol.Data[l0])
errPath = append(errPath, math.Abs(sol.Data[h0]-sol.Data[l0]))
//判断满足精度否
if errPath[i+1] < tol {
//将所有数据赋予sol前n项为x最后一项为f(x)
sol.Data[n] = sol.Data[l0]
for j := 0; j < n; j++ {
sol.Data[j] = x0.GetFromMatrix(j, l0)
}
err = true
return sol, err
}
}
return sol, err //,xPath,fxPath,errPath
}

201
vendor/github.com/nuknal/goNum/PDEDiffEllipticalH5.go generated vendored Normal file
View File

@@ -0,0 +1,201 @@
// PDEDiffEllipticalH5
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-01-08
版本 : 0.0.0
------------------------------------------------------
求解椭圆型偏微分方程Helmholtz的差分解法五点格式
理论:
对于椭圆型偏微分方程Helmholtz方程
d^2u d^2u
------ + ------ + f(x, y)*u= g(x, y)
dx^2 dy^2
u(x, 0) = fy0(x), u(x, b) = fyb(x)
u(0, y) = fx0(y), u(a, y) = fxa(y)
0 < x < a, 0 < y < b
x分为n等份y分为m等份
hy^2[u_(i+1,j) + u_(i-1,j) - 2u_(i,j)] +
hx^2[u_(i,j+1) + u_(i,j-1) - 2u_(i,j)] +
f_(i,j)*u_(i,j)*hx^2*hy^2 - g_(i,j)*hx^2*hy^2 = 0
解以上方程组可得解
参考 John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 10.3.
------------------------------------------------------
输入 :
funy0, funyb, funx0, funxa, funf, fung 边界函数及f(x, y)、g(x, y)
x0 求解范围2x2
n, m 网格数量, 对应x和y
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// PDEDiffEllipticalH5 求解椭圆型偏微分方程Helmholtz的差分解法五点格式
func PDEDiffEllipticalH5(funy0, funyb, funx0, funxa func(float64) float64,
funf, fung func(float64, float64) float64, x0 Matrix, n, m int) (Matrix, bool) {
/*
求解椭圆型偏微分方程Helmholtz的差分解法五点格式
输入 :
funy0, funyb, funx0, funxa, funf, fung 边界函数及f(x, y)、g(x, y)
x0 求解范围2x2
n, m 网格数量, 对应x和y
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断网格数量
if (m < 1) || (n < 1) {
panic("Error in goNum.PDEDiffEllipticalH5: Grid numbers error")
}
//判断初值维数
if (x0.Rows < 2) || (x0.Columns < 2) {
panic("Error in goNum.PDEDiffEllipticalH5: Initial values error")
}
var err bool = false
sol := ZeroMatrix(m+1, n+1) //行y变化列x变化
hx := (x0.GetFromMatrix(1, 0) - x0.GetFromMatrix(0, 0)) / float64(n) //x方向步长
hy := (x0.GetFromMatrix(1, 1) - x0.GetFromMatrix(0, 1)) / float64(m) //y方向步长
hx2 := hx * hx
hy2 := hy * hy
hxhy2 := hx2 * hy2
//边界框的解
//第一行的值和最后一行的值,不包括第一个和最后一个
for i := 1; i < n; i++ {
sol.SetMatrix(0, i, funy0(x0.GetFromMatrix(0, 0)+hx*float64(i)))
sol.SetMatrix(m, i, funyb(x0.GetFromMatrix(0, 0)+hx*float64(i)))
}
//第一列的值和最后一列的值,包括第一个和最后一个
for j := 0; j < m+1; j++ {
sol.SetMatrix(j, 0, funx0(x0.GetFromMatrix(0, 1)+hy*float64(j)))
sol.SetMatrix(j, n, funxa(x0.GetFromMatrix(0, 1)+hy*float64(j)))
}
//求解中间点,主对角占优矩阵解法,利用高斯消去方法
AA := ZeroMatrix((n-1)*(m-1), (n-1)*(m-1)) //系数矩阵A
BA := ZeroMatrix((n-1)*(m-1), 1) //值矩阵B
//赋值系数矩阵和值矩阵
//第一行, j = 1
//第一个
fij := hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*1.0)
AA.SetMatrix(0, 0, -2.0*hx2-2.0*hy2+fij)
AA.SetMatrix(0, 1, hy2)
AA.SetMatrix(0, n-1, hx2)
tempBA := hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hy2*funx0(x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hx2*funy0(x0.GetFromMatrix(0, 0)+hx*1.0)
BA.SetMatrix(0, 0, tempBA)
for i := 2; i < n-1; i++ {
fij = hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*1.0)
AA.SetMatrix(i-1, i-2, hy2)
AA.SetMatrix(i-1, i-1, -2.0*hx2-2.0*hy2+fij)
AA.SetMatrix(i-1, i, hy2)
AA.SetMatrix(i-1, (n-1)*1+i-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hx2*funy0(x0.GetFromMatrix(0, 0)+hx*float64(i))
BA.SetMatrix((n-1)*0+i-1, 0, tempBA)
}
//最后一个
fij = hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*1.0)
AA.SetMatrix(n-1-1, n-1-2, hy2)
AA.SetMatrix(n-1-1, n-1-1, -2.0*hx2-2.0*hy2+fij)
AA.SetMatrix(n-1-1, (n-1)*1+n-1-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hy2*funxa(x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hx2*funy0(x0.GetFromMatrix(0, 0)+hx*float64(n-1))
BA.SetMatrix(n-1-1, 0, tempBA)
//中间行, 2 <= j <= m-2
for j := 2; j < m-1; j++ {
//第一个
fij = hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*float64(j))
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1-1), hx2)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1), -2.0*hx2-2.0*hy2+fij)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1)+1, hy2)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1)+n-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*float64(j))
tempBA = tempBA - hy2*funx0(x0.GetFromMatrix(0, 1)+hy*float64(j))
BA.SetMatrix((n-1)*(j-1), 0, tempBA)
for i := 2; i < n-1; i++ {
fij = hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*float64(j))
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1-1)+i-1, hx2)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i-2, hy2)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i-1, -2.0*hx2-2.0*hy2+fij)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i, hy2)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1+1)+i-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*float64(j))
BA.SetMatrix((n-1)*(j-1)+i-1, 0, tempBA)
}
//最后一个
fij = hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*float64(j))
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1-1)+n-1-1, hx2)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1)+n-1-2, hy2)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1)+n-1-1, -2.0*hx2-2.0*hy2+fij)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1+1)+n-1-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*float64(j))
tempBA = tempBA - hy2*funxa(x0.GetFromMatrix(0, 1)+hy*float64(j))
BA.SetMatrix((n-1)*(j-1)+n-1-1, 0, tempBA)
}
//最后一行, j = m-1
//第一个
fij = hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*float64(m-1))
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1-1), hx2)
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1), -2.0*hx2-2.0*hy2+fij)
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1)+1, hy2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hy2*funx0(x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hx2*funyb(x0.GetFromMatrix(0, 0)+hx*1.0)
BA.SetMatrix((n-1)*(m-1-1), 0, tempBA)
for i := 2; i < n-1; i++ {
fij = hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*float64(m-1))
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1-1)+i-1, hx2)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i-2, hy2)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i-1, -2.0*hx2-2.0*hy2+fij)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i, hy2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hx2*funyb(x0.GetFromMatrix(0, 0)+hx*float64(i))
BA.SetMatrix((n-1)*(m-1-1)+i-1, 0, tempBA)
}
//最后一个
fij = hxhy2 * funf(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*float64(m-1))
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1-1)+n-1-1, hx2)
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1)+n-1-2, hy2)
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1)+n-1-1, -2.0*hx2-2.0*hy2+fij)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hy2*funxa(x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hx2*funyb(x0.GetFromMatrix(0, 0)+hx*float64(n-1))
BA.SetMatrix((n-1)*(m-1-1)+n-1-1, 0, tempBA)
//求解矩阵方程
tempp, temperr := LEs_ECPE(Matrix2ToSlices(AA), Matrix1ToSlices(BA))
if temperr != true {
panic("Error in goNum.PDEDiffEllipticalH5: Solve error")
}
//解赋予sol
ii := 1
jj := 1
for i := 0; i < len(tempp); i++ {
sol.SetMatrix(ii, jj, tempp[i])
if jj == n-1 {
ii++
jj = 0
}
jj++
}
err = true
return sol, err
}

179
vendor/github.com/nuknal/goNum/PDEDiffEllipticalLL5.go generated vendored Normal file
View File

@@ -0,0 +1,179 @@
// PDEDiffEllipticalLL5
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-01-07
版本 : 0.0.0
------------------------------------------------------
求解椭圆型偏微分方程Laplace的差分解法五点格式
理论:
对于椭圆型偏微分方程Laplace方程
d^2u d^2u
------ + ------ = 0
dx^2 dy^2
u(x, 0) = fy0(x), u(x, b) = fyb(x)
u(0, y) = fx0(y), u(a, y) = fxa(y)
0 < x < a, 0 < y < b
x分为n等份y分为m等份
hy^2[u_(i+1,j) + u_(i-1,j) - 2u_(i,j)] +
hx^2[u_(i,j+1) + u_(i,j-1) - 2u_(i,j)] = 0
解以上方程组可得解
参考 John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 10.3.1.
------------------------------------------------------
输入 :
funy0, funyb, funx0, funxa 边界函数
x0 求解范围2x2
n, m 网格数量, 对应x和y
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// PDEDiffEllipticalLL5 求解椭圆型偏微分方程Laplace的差分解法五点格式
func PDEDiffEllipticalLL5(funy0, funyb, funx0, funxa func(float64) float64,
x0 Matrix, n, m int) (Matrix, bool) {
/*
求解椭圆型偏微分方程Laplace的差分解法五点格式
输入 :
funy0, funyb, funx0, funxa 边界函数
x0 求解范围2x2
n, m 网格数量, 对应x和y
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断网格数量
if (m < 1) || (n < 1) {
panic("Error in goNum.PDEDiffEllipticalLL5: Grid numbers error")
}
//判断初值维数
if (x0.Rows < 2) || (x0.Columns < 2) {
panic("Error in goNum.PDEDiffEllipticalLL5: Initial values error")
}
var err bool = false
sol := ZeroMatrix(m+1, n+1) //行y变化列x变化
hx := (x0.GetFromMatrix(1, 0) - x0.GetFromMatrix(0, 0)) / float64(n) //x方向步长
hy := (x0.GetFromMatrix(1, 1) - x0.GetFromMatrix(0, 1)) / float64(m) //y方向步长
//边界框的解
//第一行的值和最后一行的值,不包括第一个和最后一个
for i := 1; i < n; i++ {
sol.SetMatrix(0, i, funy0(x0.GetFromMatrix(0, 0)+hx*float64(i)))
sol.SetMatrix(m, i, funyb(x0.GetFromMatrix(0, 0)+hx*float64(i)))
}
//第一列的值和最后一列的值,包括第一个和最后一个
for j := 0; j < m+1; j++ {
sol.SetMatrix(j, 0, funx0(x0.GetFromMatrix(0, 1)+hy*float64(j)))
sol.SetMatrix(j, n, funxa(x0.GetFromMatrix(0, 1)+hy*float64(j)))
}
//求解中间点,主对角占优矩阵解法,利用高斯消去方法
AA := ZeroMatrix((n-1)*(m-1), (n-1)*(m-1)) //系数矩阵A
BA := ZeroMatrix((n-1)*(m-1), 1) //值矩阵B
//赋值系数矩阵和值矩阵
//第一行, j = 1
//第一个
AA.SetMatrix(0, 0, -2.0*hx*hx-2.0*hy*hy)
AA.SetMatrix(0, 1, hy*hy)
AA.SetMatrix(0, n-1, hx*hx)
tempBA := 0.0 - hy*hy*funx0(x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hx*hx*funy0(x0.GetFromMatrix(0, 0)+hx*1)
BA.SetMatrix(0, 0, tempBA)
for i := 2; i < n-1; i++ {
AA.SetMatrix(i-1, i-2, hy*hy)
AA.SetMatrix(i-1, i-1, -2.0*hx*hx-2.0*hy*hy)
AA.SetMatrix(i-1, i, hy*hy)
AA.SetMatrix(i-1, (n-1)*1+i-1, hx*hx)
tempBA = 0.0 - hx*hx*funy0(x0.GetFromMatrix(0, 0)+hx*float64(i))
BA.SetMatrix((n-1)*0+i-1, 0, tempBA)
}
//最后一个
AA.SetMatrix(n-1-1, n-1-2, hy*hy)
AA.SetMatrix(n-1-1, n-1-1, -2.0*hx*hx-2.0*hy*hy)
AA.SetMatrix(n-1-1, (n-1)*1+n-1-1, hx*hx)
tempBA = 0.0 - hy*hy*funxa(x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hx*hx*funy0(x0.GetFromMatrix(0, 0)+hx*float64(n-1))
BA.SetMatrix(n-1-1, 0, tempBA)
//中间行
for j := 2; j < m-1; j++ {
//第一个
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1-1), hx*hx)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1), -2.0*hx*hx-2.0*hy*hy)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1)+1, hy*hy)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1)+n-1, hx*hx)
tempBA = 0.0 - hy*hy*funx0(x0.GetFromMatrix(0, 1)+hy*float64(j))
BA.SetMatrix((n-1)*(j-1), 0, tempBA)
for i := 2; i < n-1; i++ {
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1-1)+i-1, hx*hx)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i-2, hy*hy)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i-1, -2.0*hx*hx-2.0*hy*hy)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i, hy*hy)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1+1)+i-1, hx*hx)
BA.SetMatrix((n-1)*(j-1)+i-1, 0, 0.0)
}
//最后一个
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1-1)+n-1-1, hx*hx)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1)+n-1-2, hy*hy)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1)+n-1-1, -2.0*hx*hx-2.0*hy*hy)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1+1)+n-1-1, hx*hx)
tempBA = 0.0 - hy*hy*funxa(x0.GetFromMatrix(0, 1)+hy*float64(j))
BA.SetMatrix((n-1)*(j-1)+n-1-1, 0, tempBA)
}
//最后一行, j = m-1
//第一个
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1-1), hx*hx)
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1), -2.0*hx*hx-2.0*hy*hy)
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1)+1, hy*hy)
tempBA = 0.0 - hy*hy*funx0(x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hx*hx*funyb(x0.GetFromMatrix(0, 0)+hx*1)
BA.SetMatrix((n-1)*(m-1-1), 0, tempBA)
for i := 2; i < n-1; i++ {
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1-1)+i-1, hx*hx)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i-2, hy*hy)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i-1, -2.0*hx*hx-2.0*hy*hy)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i, hy*hy)
tempBA = 0.0 - hx*hx*funyb(x0.GetFromMatrix(0, 0)+hx*float64(i))
BA.SetMatrix((n-1)*(m-1-1)+i-1, 0, tempBA)
}
//最后一个
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1-1)+n-1-1, hx*hx)
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1)+n-1-2, hy*hy)
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1)+n-1-1, -2.0*hx*hx-2.0*hy*hy)
tempBA = 0.0 - hy*hy*funxa(x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hx*hx*funyb(x0.GetFromMatrix(0, 0)+hx*float64(n-1))
BA.SetMatrix((n-1)*(m-1-1)+n-1-1, 0, tempBA)
//求解矩阵方程
tempp, temperr := LEs_ECPE(Matrix2ToSlices(AA), Matrix1ToSlices(BA))
if temperr != true {
panic("Error in goNum.PDEDiffEllipticalLL5: Solve error")
}
//解赋予sol
ii := 1
jj := 1
for i := 0; i < len(tempp); i++ {
sol.SetMatrix(ii, jj, tempp[i])
if jj == n-1 {
ii++
jj = 0
}
jj++
}
err = true
return sol, err
}

191
vendor/github.com/nuknal/goNum/PDEDiffEllipticalP5.go generated vendored Normal file
View File

@@ -0,0 +1,191 @@
// PDEDiffEllipticalP5
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-01-08
版本 : 0.0.0
------------------------------------------------------
求解椭圆型偏微分方程Poisson的差分解法五点格式
理论:
对于椭圆型偏微分方程Poisson方程
d^2u d^2u
------ + ------ = g(x, y)
dx^2 dy^2
u(x, 0) = fy0(x), u(x, b) = fyb(x)
u(0, y) = fx0(y), u(a, y) = fxa(y)
0 < x < a, 0 < y < b
x分为n等份y分为m等份
hy^2[u_(i+1,j) + u_(i-1,j) - 2u_(i,j)] +
hx^2[u_(i,j+1) + u_(i,j-1) - 2u_(i,j)] - g_(i,j)*hx^2*hy^2 = 0
解以上方程组可得解
参考 John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 10.3.
------------------------------------------------------
输入 :
funy0, funyb, funx0, funxa, fung 边界函数及g(x, y)
x0 求解范围2x2
n, m 网格数量, 对应x和y
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// PDEDiffEllipticalP5 求解椭圆型偏微分方程Poisson的差分解法五点格式
func PDEDiffEllipticalP5(funy0, funyb, funx0, funxa func(float64) float64,
fung func(float64, float64) float64, x0 Matrix, n, m int) (Matrix, bool) {
/*
求解椭圆型偏微分方程Poisson的差分解法五点格式
输入 :
funy0, funyb, funx0, funxa, fung 边界函数及g(x, y)
x0 求解范围2x2
n, m 网格数量, 对应x和y
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断网格数量
if (m < 1) || (n < 1) {
panic("Error in goNum.PDEDiffEllipticalP5: Grid numbers error")
}
//判断初值维数
if (x0.Rows < 2) || (x0.Columns < 2) {
panic("Error in goNum.PDEDiffEllipticalP5: Initial values error")
}
var err bool = false
sol := ZeroMatrix(m+1, n+1) //行y变化列x变化
hx := (x0.GetFromMatrix(1, 0) - x0.GetFromMatrix(0, 0)) / float64(n) //x方向步长
hy := (x0.GetFromMatrix(1, 1) - x0.GetFromMatrix(0, 1)) / float64(m) //y方向步长
hx2 := hx * hx
hy2 := hy * hy
hxhy2 := hx2 * hy2
//边界框的解
//第一行的值和最后一行的值,不包括第一个和最后一个
for i := 1; i < n; i++ {
sol.SetMatrix(0, i, funy0(x0.GetFromMatrix(0, 0)+hx*float64(i)))
sol.SetMatrix(m, i, funyb(x0.GetFromMatrix(0, 0)+hx*float64(i)))
}
//第一列的值和最后一列的值,包括第一个和最后一个
for j := 0; j < m+1; j++ {
sol.SetMatrix(j, 0, funx0(x0.GetFromMatrix(0, 1)+hy*float64(j)))
sol.SetMatrix(j, n, funxa(x0.GetFromMatrix(0, 1)+hy*float64(j)))
}
//求解中间点,主对角占优矩阵解法,利用高斯消去方法
AA := ZeroMatrix((n-1)*(m-1), (n-1)*(m-1)) //系数矩阵A
BA := ZeroMatrix((n-1)*(m-1), 1) //值矩阵B
//赋值系数矩阵和值矩阵
//第一行, j = 1
//第一个
AA.SetMatrix(0, 0, -2.0*hx2-2.0*hy2)
AA.SetMatrix(0, 1, hy2)
AA.SetMatrix(0, n-1, hx2)
tempBA := hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hy2*funx0(x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hx2*funy0(x0.GetFromMatrix(0, 0)+hx*1.0)
BA.SetMatrix(0, 0, tempBA)
for i := 2; i < n-1; i++ {
AA.SetMatrix(i-1, i-2, hy2)
AA.SetMatrix(i-1, i-1, -2.0*hx2-2.0*hy2)
AA.SetMatrix(i-1, i, hy2)
AA.SetMatrix(i-1, (n-1)*1+i-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hx2*funy0(x0.GetFromMatrix(0, 0)+hx*float64(i))
BA.SetMatrix((n-1)*0+i-1, 0, tempBA)
}
//最后一个
AA.SetMatrix(n-1-1, n-1-2, hy2)
AA.SetMatrix(n-1-1, n-1-1, -2.0*hx2-2.0*hy2)
AA.SetMatrix(n-1-1, (n-1)*1+n-1-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hy2*funxa(x0.GetFromMatrix(0, 1)+hy*1.0)
tempBA = tempBA - hx2*funy0(x0.GetFromMatrix(0, 0)+hx*float64(n-1))
BA.SetMatrix(n-1-1, 0, tempBA)
//中间行, 2 <= j <= m-2
for j := 2; j < m-1; j++ {
//第一个
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1-1), hx2)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1), -2.0*hx2-2.0*hy2)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1)+1, hy2)
AA.SetMatrix((n-1)*(j-1), (n-1)*(j-1)+n-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*float64(j))
tempBA = tempBA - hy2*funx0(x0.GetFromMatrix(0, 1)+hy*float64(j))
BA.SetMatrix((n-1)*(j-1), 0, tempBA)
for i := 2; i < n-1; i++ {
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1-1)+i-1, hx2)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i-2, hy2)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i-1, -2.0*hx2-2.0*hy2)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1)+i, hy2)
AA.SetMatrix((n-1)*(j-1)+i-1, (n-1)*(j-1+1)+i-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*float64(j))
BA.SetMatrix((n-1)*(j-1)+i-1, 0, tempBA)
}
//最后一个
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1-1)+n-1-1, hx2)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1)+n-1-2, hy2)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1)+n-1-1, -2.0*hx2-2.0*hy2)
AA.SetMatrix((n-1)*(j-1)+n-1-1, (n-1)*(j-1+1)+n-1-1, hx2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*float64(j))
tempBA = tempBA - hy2*funxa(x0.GetFromMatrix(0, 1)+hy*float64(j))
BA.SetMatrix((n-1)*(j-1)+n-1-1, 0, tempBA)
}
//最后一行, j = m-1
//第一个
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1-1), hx2)
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1), -2.0*hx2-2.0*hy2)
AA.SetMatrix((n-1)*(m-1-1), (n-1)*(m-1-1)+1, hy2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*1.0, x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hy2*funx0(x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hx2*funyb(x0.GetFromMatrix(0, 0)+hx*1.0)
BA.SetMatrix((n-1)*(m-1-1), 0, tempBA)
for i := 2; i < n-1; i++ {
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1-1)+i-1, hx2)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i-2, hy2)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i-1, -2.0*hx2-2.0*hy2)
AA.SetMatrix((n-1)*(m-1-1)+i-1, (n-1)*(m-1-1)+i, hy2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(i), x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hx2*funyb(x0.GetFromMatrix(0, 0)+hx*float64(i))
BA.SetMatrix((n-1)*(m-1-1)+i-1, 0, tempBA)
}
//最后一个
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1-1)+n-1-1, hx2)
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1)+n-1-2, hy2)
AA.SetMatrix((n-1)*(m-1-1)+n-1-1, (n-1)*(m-1-1)+n-1-1, -2.0*hx2-2.0*hy2)
tempBA = hxhy2 * fung(x0.GetFromMatrix(0, 0)+hx*float64(n-1), x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hy2*funxa(x0.GetFromMatrix(0, 1)+hy*float64(m-1))
tempBA = tempBA - hx2*funyb(x0.GetFromMatrix(0, 0)+hx*float64(n-1))
BA.SetMatrix((n-1)*(m-1-1)+n-1-1, 0, tempBA)
//求解矩阵方程
tempp, temperr := LEs_ECPE(Matrix2ToSlices(AA), Matrix1ToSlices(BA))
if temperr != true {
panic("Error in goNum.PDEDiffEllipticalP5: Solve error")
}
//解赋予sol
ii := 1
jj := 1
for i := 0; i < len(tempp); i++ {
sol.SetMatrix(ii, jj, tempp[i])
if jj == n-1 {
ii++
jj = 0
}
jj++
}
err = true
return sol, err
}

104
vendor/github.com/nuknal/goNum/PDEDiffHyperbolic1.go generated vendored Normal file
View File

@@ -0,0 +1,104 @@
// PDEDiffHyperbolic1
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-17
版本 : 0.0.0
------------------------------------------------------
求解双曲型偏微分方程的差分解法(第一种差分格式)
理论:
对于抛物型偏微分方程:
d^2u d^2u
------ = A ------ + B
dt^2 dx^2
u(x, 0) = phi(x), (du/dt)_(t=0) = psi(x)
u(0, t) = u1(t), u(L, t) = u2(t)
0 < x < L, 0 < t < T
则差分格式为x分为m等份t分为n等份
u_(i,j+1) = lu_(i+1,j) + 2(1-l)u_(i,j) + lu_(i-1,j) -
u_(i,j-1) + B*ht^2
初值需要计算第零层和第一层、左右边界
第零层u_(i,0) = phi(i*hx), i=1,2,...,m-1
第一层u_(i,1) = u_(i,0) + ht*psi(i*hx)
左边界u_(0,j) = u1(j*ht)
右边界u_(m,j) = u2(j*ht), j=0,1,2,...,n
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 226-228.
------------------------------------------------------
输入 :
funphi, funpsi, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// PDEDiffHyperbolic1 求解双曲型偏微分方程的差分解法(第一种差分格式)
func PDEDiffHyperbolic1(funphi, funpsi, funu1, funu2 func(float64) float64,
x0 Matrix, A, B float64, m, n int) (Matrix, bool) {
/*
求解双曲型偏微分方程的差分解法(第一种差分格式)
输入 :
funphi, funpsi, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断网格数量
if (m < 1) || (n < 1) {
panic("Error in goNum.PDEDiffHyperbolic1: Grid numbers error")
}
var err bool = false
sol := ZeroMatrix(m+1, n+1)
hx := (x0.GetFromMatrix(1, 0) - x0.GetFromMatrix(0, 0)) / float64(m) //x方向步长
ht := (x0.GetFromMatrix(1, 1) - x0.GetFromMatrix(0, 1)) / float64(n) //t方向步长
//1. 计算t第零层上的值u_(i,0) i=,1,...,m-1
for i := 1; i < m; i++ {
sol.SetMatrix(i, 0, funphi(x0.GetFromMatrix(0, 0)+float64(i)*hx))
}
//2. 计算x左右边界上的节点u_(0,j)和u_(m,j) j=0,1,2,...,n
for j := 0; j < n+1; j++ {
sol.SetMatrix(0, j, funu1(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //左边界
sol.SetMatrix(m, j, funu2(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //右边界
}
//lambda及稳定性判断
l := A * ht * ht / (hx * hx)
if l > 1 {
panic("Error in goNum.PDEDiffHyperbolic1: lambda greater than one")
}
//3. 计算t第一层上的值u_(i,1) i=,1,...,m-1
for i := 1; i < m; i++ {
sol.SetMatrix(i, 1, sol.GetFromMatrix(i, 0)+ht*funpsi(x0.GetFromMatrix(0, 0)+float64(i)*hx))
}
//4. 2n层
for j := 2; j < n+1; j++ {
for i := 1; i < m; i++ {
temp0 := l * sol.GetFromMatrix(i+1, j-1)
temp0 += 2.0 * (1.0 - l) * sol.GetFromMatrix(i, j-1)
temp0 += l * sol.GetFromMatrix(i-1, j-1)
temp0 -= sol.GetFromMatrix(i, j-2)
temp0 += B * ht * ht
sol.SetMatrix(i, j, temp0)
}
}
err = true
return sol, err
}

107
vendor/github.com/nuknal/goNum/PDEDiffHyperbolic2.go generated vendored Normal file
View File

@@ -0,0 +1,107 @@
// PDEDiffHyperbolic2
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-17
版本 : 0.0.0
------------------------------------------------------
求解双曲型偏微分方程的差分解法第二种差分格式t=0时微分方程须成立
理论:
对于抛物型偏微分方程:
d^2u d^2u
------ = A ------ + B
dt^2 dx^2
u(x, 0) = phi(x), (du/dt)_(t=0) = psi(x)
u(0, t) = u1(t), u(L, t) = u2(t)
0 < x < L, 0 < t < T
则差分格式为x分为m等份t分为n等份
u_(i,j+1) = lu_(i+1,j) + 2(1-l)u_(i,j) + lu_(i-1,j) -
u_(i,j-1) + B*ht^2
初值需要计算第零层和第一层、左右边界
第零层u_(i,0) = phi(i*hx), i=1,2,...,m-1
第一层u_(i,1) = u_(i,0) + ht*psi(i*hx) + B*ht^2/2 +
l*(u_(i+1,0)-2u_(i,0)+u_(i-1,0))/2
左边界u_(0,j) = u1(j*ht)
右边界u_(m,j) = u2(j*ht), j=0,1,2,...,n
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 226-228.
------------------------------------------------------
输入 :
funphi, funpsi, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// PDEDiffHyperbolic2 求解双曲型偏微分方程的差分解法第二种差分格式t=0时微分方程须成立
func PDEDiffHyperbolic2(funphi, funpsi, funu1, funu2 func(float64) float64,
x0 Matrix, A, B float64, m, n int) (Matrix, bool) {
/*
求解双曲型偏微分方程的差分解法第二种差分格式t=0时微分方程须成立
输入 :
funphi, funpsi, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断网格数量
if (m < 1) || (n < 1) {
panic("Error in goNum.PDEDiffHyperbolic1: Grid numbers error")
}
var err bool = false
sol := ZeroMatrix(m+1, n+1)
hx := (x0.GetFromMatrix(1, 0) - x0.GetFromMatrix(0, 0)) / float64(m) //x方向步长
ht := (x0.GetFromMatrix(1, 1) - x0.GetFromMatrix(0, 1)) / float64(n) //t方向步长
//1. 计算t第零层上的值u_(i,0) i=,1,...,m-1
for i := 1; i < m; i++ {
sol.SetMatrix(i, 0, funphi(x0.GetFromMatrix(0, 0)+float64(i)*hx))
}
//2. 计算x左右边界上的节点u_(0,j)和u_(m,j) j=0,1,2,...,n
for j := 0; j < n+1; j++ {
sol.SetMatrix(0, j, funu1(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //左边界
sol.SetMatrix(m, j, funu2(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //右边界
}
//lambda及稳定性判断
l := A * ht * ht / (hx * hx)
if l > 1 {
panic("Error in goNum.PDEDiffHyperbolic1: lambda greater than one")
}
//3. 计算t第一层上的值u_(i,1) i=,1,...,m-1
for i := 1; i < m; i++ {
temp0 := sol.GetFromMatrix(i, 0) + ht*funpsi(x0.GetFromMatrix(0, 0)+float64(i)*hx)
temp0 += sol.GetFromMatrix(i+1, 0) - 2.0*sol.GetFromMatrix(i, 0) + sol.GetFromMatrix(i-1, 0)
sol.SetMatrix(i, 1, l*temp0/2.0+B*ht*ht/2.0)
}
//4. 2n层
for j := 2; j < n+1; j++ {
for i := 1; i < m; i++ {
temp0 := l * sol.GetFromMatrix(i+1, j-1)
temp0 += 2.0 * (1.0 - l) * sol.GetFromMatrix(i, j-1)
temp0 += l * sol.GetFromMatrix(i-1, j-1)
temp0 -= sol.GetFromMatrix(i, j-2)
temp0 += B * ht * ht
sol.SetMatrix(i, j, temp0)
}
}
err = true
return sol, err
}

96
vendor/github.com/nuknal/goNum/PDEDiffParabolicE.go generated vendored Normal file
View File

@@ -0,0 +1,96 @@
// PDEDiffParabolicE
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-14
版本 : 0.0.0
------------------------------------------------------
求解抛物型偏微分方程的差分解法(显式)
理论:
对于抛物型偏微分方程:
du d^2u
---- = A ------ + B
dt dx^2
u(x, 0) = p(x)
u(0, t) = u1(t), u(L, t) = u2(t)
0 < x < L, 0 < t < T
则古典显式差分格式为x分为m等份t分为n等份
u_(i,j+1) = lu_(i-1,j) + (1-2l)u_(i,j) + lu_(i+1,j) + B*tau
A*tau
l = -------
h^2
u_(i, 0) = p(ih), i=1,2,..,m-1
u_(0, j) = u1(j*tau), u_(m, j) = u2(j, tau), j=0,1,...,n
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 214-215.
------------------------------------------------------
输入 :
funp, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// PDEDiffParabolicE 求解抛物型偏微分方程的差分解法(显式)
func PDEDiffParabolicE(funp, funu1, funu2 func(float64) float64, x0 Matrix,
A, B float64, m, n int) (Matrix, bool) {
/*
求解抛物型偏微分方程的差分解法(显式)
输入 :
funp, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断网格数量
if (m < 1) || (n < 1) {
panic("Error in goNum.PDEDiffParabolicE: Grid numbers error")
}
var err bool = false
sol := ZeroMatrix(m+1, n+1)
hx := (x0.GetFromMatrix(1, 0) - x0.GetFromMatrix(0, 0)) / float64(m) //x方向步长
ht := (x0.GetFromMatrix(1, 1) - x0.GetFromMatrix(0, 1)) / float64(n) //t方向步长
//1. 计算t第零层上的值u_(i,0) i=0,1,...,m
for i := 0; i < m+1; i++ {
sol.SetMatrix(i, 0, funp(x0.GetFromMatrix(0, 0)+float64(i)*hx))
}
//2. 计算左右边界上的节点u_(0,j)和u_(m,j) j=1,2,...,n
for j := 1; j < n+1; j++ {
sol.SetMatrix(0, j, funu1(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //左边界
sol.SetMatrix(m, j, funu2(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //右边界
}
//内部节点循环求解
l := A * ht / (hx * hx)
//稳定性判断
if (l <= 0) || (l > 0.5) {
panic("Error in goNum.PDEDiffParabolicS: lambda less than or equal to zero, or greater than 0.5")
}
for j := 1; j < n+1; j++ { //层循环, ti
for i := 1; i < m; i++ { //列循环, xi
uij := l * sol.GetFromMatrix(i-1, j-1)
uij += (1 - 2.0*l) * sol.GetFromMatrix(i, j-1)
uij += l * sol.GetFromMatrix(i+1, j-1)
sol.SetMatrix(i, j, uij+B*ht)
}
}
err = true
return sol, err
}

129
vendor/github.com/nuknal/goNum/PDEDiffParabolicI.go generated vendored Normal file
View File

@@ -0,0 +1,129 @@
// PDEDiffParabolicI
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-14
版本 : 0.0.0
------------------------------------------------------
求解抛物型偏微分方程的差分解法(隐式)
理论:
对于抛物型偏微分方程:
du d^2u
---- = A ------ + B
dt dx^2
u(x, 0) = p(x)
u(0, t) = u1(t), u(L, t) = u2(t)
0 < x < L, 0 < t < T
则古典隐式差分格式为x分为m等份t分为n等份
Au_(j+1) = uj + F_(j+1)
|1+2l -l |
|-l 1+2l -l |
A = | .......... |
| -l 1+2l -l |
| -l 1+2l|
u_(j+1) = [u_(1,j+1),u_(2,j+1),...,u_(m-1,j+1)]'
F_(j+1) = [lu1((j+1)*tau)+B*tau,B*tau,B*tau,...,B*tau,lu2((j+1)*tau)+B*tau]'
V_(j+1) = uj + F_(j+1)
j = 0,1,...,n-1
u0 = [u_(1,0),u_(2,0),...,u_(m-1,0)]'
= [p(h),p(2h),...,p((m-1)h)]'
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 214-215.
------------------------------------------------------
输入 :
funp, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// PDEDiffParabolicI 求解抛物型偏微分方程的差分解法(隐式)
func PDEDiffParabolicI(funp, funu1, funu2 func(float64) float64, x0 Matrix, A, B float64, m, n int) (Matrix, bool) {
/*
求解抛物型偏微分方程的差分解法(隐式)
输入 :
funp, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断网格数量
if (m < 1) || (n < 1) {
panic("Error in goNum.PDEDiffParabolicI: Grid numbers error")
}
var err bool = false
sol := ZeroMatrix(m+1, n+1)
hx := (x0.GetFromMatrix(1, 0) - x0.GetFromMatrix(0, 0)) / float64(m) //x方向步长
ht := (x0.GetFromMatrix(1, 1) - x0.GetFromMatrix(0, 1)) / float64(n) //t方向步长
//1. 计算t第零层上的值u_(i,0) i=0,1,...,m
for i := 0; i < m+1; i++ {
sol.SetMatrix(i, 0, funp(x0.GetFromMatrix(0, 0)+float64(i)*hx))
}
//2. 计算左右边界上的节点u_(0,j)和u_(m,j) j=1,2,...,n
for j := 1; j < n+1; j++ {
sol.SetMatrix(0, j, funu1(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //左边界
sol.SetMatrix(m, j, funu2(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //右边界
}
l := A * ht / (hx * hx)
//稳定性判断
if l <= 0 {
panic("Error in goNum.PDEDiffParabolicS: lambda less than or equal to zero")
}
//A赋值
AA := ZeroMatrix(m-1, m-1)
ui := ZeroMatrix(m-1, 1)
Fi := ZeroMatrix(m-1, 1)
AA.SetMatrix(0, 0, 1.0+2.0*l) //第零行
AA.SetMatrix(0, 1, -1.0*l)
ui.Data[0] = sol.GetFromMatrix(1, 0)
for i := 1; i < m-2; i++ {
AA.SetMatrix(i, i-1, -1.0*l)
AA.SetMatrix(i, i, 1.0+2.0*l)
AA.SetMatrix(i, i+1, -1.0*l)
ui.Data[i] = sol.GetFromMatrix(i+1, 0)
Fi.Data[i] = B * ht
}
AA.SetMatrix(m-2, m-3, -1.0*l) //第零行
AA.SetMatrix(m-2, m-2, 1.0+2.0*l)
ui.Data[m-2] = sol.GetFromMatrix(m-1, 0)
//内部节点循环求解
for j := 0; j < n; j++ {
//F每一步需要重新计算第一项和最后一项
Fi.Data[0] = l*funu1(float64(j+1)*ht) + B*ht
Fi.Data[m-2] = l*funu2(float64(j+1)*ht) + B*ht
//
ui1, errtemp := LEs_Chasing(AA, AddMatrix(ui, Fi))
if errtemp != true {
panic("Error in goNum.PDEDiffParabolicI: Chasing solved error")
}
for i := 0; i < m-1; i++ {
ui.Data[i] = ui1.Data[i]
sol.SetMatrix(i+1, j+1, ui1.Data[i])
}
}
err = true
return sol, err
}

138
vendor/github.com/nuknal/goNum/PDEDiffParabolicS.go generated vendored Normal file
View File

@@ -0,0 +1,138 @@
// PDEDiffParabolicS
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-14
版本 : 0.0.0
------------------------------------------------------
求解抛物型偏微分方程的差分解法(六点对称格式)
理论:
对于抛物型偏微分方程:
du d^2u
---- = A ------ + B
dt dx^2
u(x, 0) = p(x)
u(0, t) = u1(t), u(L, t) = u2(t)
0 < x < L, 0 < t < T
则古典隐式差分格式为x分为m等份t分为n等份
Au_(j+1) = B_(j+1)
|2(1+l) -l |
|-l 2(1+l) -l |
A = | .......... |
| -l 2(1+l) -l |
| -l 2(1+l)|
u_(j+1) = [u_(1,j+1),u_(2,j+1),...,u_(m-1,j+1)]'
F_(j+1) = [lu_(0,j)+2(1-l)u_(1,j)+lu_(2,j)+lu_(0,j+1)+2B*tau,
lu_(1,j)+2(1-l)u_(2,j)+lu_(3,j)+2B*tau,
...
lu_(m-3,j)+2(1-l)u_(m-2,j)+lu_(m-1,j)+2B*tau,
lu_(m-2,j)+2(1-l)u_(m-1,j)+lu_(m,j)+lu_(m,j+1)+2B*tau]'
j = 0,1,...,n-1
u0 = [u_(1,0),u_(2,0),...,u_(m-1,0)]'
= [p(h),p(2h),...,p((m-1)h)]'
参考 John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 10.2.3.
------------------------------------------------------
输入 :
funp, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// PDEDiffParabolicS 求解抛物型偏微分方程的差分解法(隐式)
func PDEDiffParabolicS(funp, funu1, funu2 func(float64) float64, x0 Matrix, A, B float64, m, n int) (Matrix, bool) {
/*
求解抛物型偏微分方程的差分解法(隐式)
输入 :
funp, funu1, funu2 边界函数
x0 求解范围2x2
A, B 常系数
m, n 网格数量
输出 :
sol 解矩阵
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断网格数量
if (m < 1) || (n < 1) {
panic("Error in goNum.PDEDiffParabolicS: Grid numbers error")
}
var err bool = false
sol := ZeroMatrix(m+1, n+1)
hx := (x0.GetFromMatrix(1, 0) - x0.GetFromMatrix(0, 0)) / float64(m) //x方向步长
ht := (x0.GetFromMatrix(1, 1) - x0.GetFromMatrix(0, 1)) / float64(n) //t方向步长
//1. 计算t第零层上的值u_(i,0) i=0,1,...,m
for i := 0; i < m+1; i++ {
sol.SetMatrix(i, 0, funp(x0.GetFromMatrix(0, 0)+float64(i)*hx))
}
//2. 计算左右边界上的节点u_(0,j)和u_(m,j) j=1,2,...,n
for j := 1; j < n+1; j++ {
sol.SetMatrix(0, j, funu1(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //左边界
sol.SetMatrix(m, j, funu2(x0.GetFromMatrix(0, 1)+float64(j)*ht)) //右边界
}
l := A * ht / (hx * hx)
//稳定性判断
if l <= 0 {
panic("Error in goNum.PDEDiffParabolicS: lambda less than or equal to zero")
}
//A赋值
AA := ZeroMatrix(m-1, m-1)
ui := ZeroMatrix(m+1, 1)
AA.SetMatrix(0, 0, 2.0*(1.0+l)) //第零行
AA.SetMatrix(0, 1, -1.0*l)
ui.Data[0] = sol.GetFromMatrix(0, 0)
for i := 1; i < m-2; i++ {
AA.SetMatrix(i, i-1, -1.0*l)
AA.SetMatrix(i, i, 2.0*(1.0+l))
AA.SetMatrix(i, i+1, -1.0*l)
ui.Data[i] = sol.GetFromMatrix(i, 0)
}
AA.SetMatrix(m-2, m-3, -1.0*l) //第零行
AA.SetMatrix(m-2, m-2, 2.0*(1.0+l))
ui.Data[m-2] = sol.GetFromMatrix(m-2, 0)
ui.Data[m-1] = sol.GetFromMatrix(m-1, 0)
ui.Data[m] = sol.GetFromMatrix(m, 0)
//内部节点循环求解
for j := 0; j < n; j++ {
Fi := ZeroMatrix(m-1, 1)
Fi.Data[0] = l*ui.Data[0] + 2.0*(1.0-l)*ui.Data[1] + l*ui.Data[2] + l*sol.GetFromMatrix(0, j+1) + 2.0*B*ht
for i := 1; i < m-2; i++ {
Fi.Data[i] = l*ui.Data[i] + 2.0*(1.0-l)*ui.Data[i+1] + l*ui.Data[i+2] + 2.0*B*ht
}
Fi.Data[m-2] = l*ui.Data[m-2] + 2.0*(1.0-l)*ui.Data[m-1] + l*ui.Data[m] + l*sol.GetFromMatrix(m, j+1) + 2.0*B*ht
//ui1为m-1行ui为m+1行
ui1, errtemp := LEs_Chasing(AA, Fi)
if errtemp != true {
panic("Error in goNum.PDEDiffParabolicS: Chasing solved error")
}
ui.Data[0] = sol.GetFromMatrix(0, j+1)
for i := 1; i < m; i++ {
ui.Data[i] = ui1.Data[i-1]
sol.SetMatrix(i, j+1, ui1.Data[i-1])
}
ui.Data[m] = sol.GetFromMatrix(m, j+1)
}
err = true
return sol, err
}

104
vendor/github.com/nuknal/goNum/PowInt.go generated vendored Normal file
View File

@@ -0,0 +1,104 @@
// PowInt
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-12
版本 : 0.0.0
------------------------------------------------------
计算整数或浮点数的整数次幂
理论:
------------------------------------------------------
输入 :
a, n a^n
输出 :
sol 解
------------------------------------------------------
*/
package goNum
import (
"math"
)
// PowFInt 浮点数的整数次幂
func PowFInt(a float64, n int) float64 {
/*
计算浮点数的整数次幂
输入 :
a, n a^n
输出 :
sol 解
*/
if n < 0 {
panic("Error in goNum.PowFInt: n less than zero")
} else if n == 0 {
return 1.0
} else if n == 1 {
return a
}
sol := a
for i := 2; i < n+1; i++ {
sol = sol * a
}
return sol
}
// PowIF 整数的浮点数次幂
func PowIF(a int, n float64) float64 {
/*
计算整数的浮点数次幂
输入 :
a, n a^n
输出 :
sol 解
*/
return math.Pow(float64(a), n)
}
// PowIInt 整数的整数次幂,输出整数
func PowIInt(a, n int) int {
/*
计算整数的整数次幂,输出整数
输入 :
a, n a^n
输出 :
sol 解
*/
if n < 0 {
panic("Error in goNum.PowIInt: n less than zero")
} else if n == 0 {
return 1
} else if n == 1 {
return a
}
sol := a
for i := 2; i < n+1; i++ {
sol = sol * a
}
return sol
}
// PowIIntF 整数的整数次幂,输出浮点
func PowIIntF(a, n int) float64 {
/*
计算整数的整数次幂,输出浮点
输入 :
a, n a^n
输出 :
sol 解
*/
if n < 0 {
panic("Error in goNum.PowIInt: n less than zero")
} else if n == 0 {
return 1.0
} else if n == 1 {
return float64(a)
}
sol := a
for i := 2; i < n+1; i++ {
sol = sol * a
}
return float64(sol)
}

88
vendor/github.com/nuknal/goNum/QuickSort.go generated vendored Normal file
View File

@@ -0,0 +1,88 @@
// QuickSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-06
版本 : 0.0.0
------------------------------------------------------
快速排序法
理论:
时间复杂度: O(nlog2(n))
最好情况 : O(nlog2(n))
最坏情况 : O(n^2)
空间复杂度: O(nlog2(n))
稳定性 : 不稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// quickSort_sort
// i0 --- first
// i2 --- last
func quickSort_sort(sol *Matrix, i0, i2 int) {
if i0 >= i2 {
return
}
i := i0
j := i2
ref := (*sol).Data[i] //第一个元素作为分区元素
for i != j {
for i < j && (*sol).Data[j] > ref {
j -= 1
}
(*sol).Data[i] = (*sol).Data[j]
for i < j && (*sol).Data[i] < ref {
i += 1
}
(*sol).Data[j] = (*sol).Data[i]
}
(*sol).Data[i] = ref
quickSort_sort(sol, i0, i-1)
quickSort_sort(sol, i+1, i2)
}
// QuickSort 快速排序法
func QuickSort(in Matrix) (Matrix, bool) {
/*
快速排序法
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if in.Rows != 1 {
panic("Error in goNum.QuickSort: Input Matrix error")
}
if in.Columns < 1 {
panic("Error in goNum.QuickSort: Empty input Matrix")
} else if in.Columns == 1 {
return in, true
}
n := in.Columns
sol := ZeroMatrix(1, n)
var err bool = false
//初始化sol
for i := 0; i < n; i++ {
sol.Data[i] = in.Data[i]
}
//排序开始
quickSort_sort(&sol, 0, n-1)
err = true
return sol, err
}

192
vendor/github.com/nuknal/goNum/README.md generated vendored Normal file
View File

@@ -0,0 +1,192 @@
关于goNum
==========
goNum是一款完全以[Go](https://golang.org)语言为基础的开源数值算法库它可以使你像调用其它go函数一样使用其进行数值运算且不依赖于任何外部库。
限于作者业余时间有限,目前功能还在一步步完善,算法还在慢慢添加。
绝大部分算法进行了典型状态测试,但不保证所有算法在所有状态下都是安全的、可靠的。
另外需要注意的是此算法库旨在解决问题而不是实现语言的某些能力即使作者正在努力使得go语言的独特性在其中充分体现。
如果您对作者的工作满意请留心关注goNum的更新状态如果您对作者的工作有所建议请电邮chengfengcool@sina.com。
或者承蒙赏识如果您愿意捐助关于goNum工作请电邮联系作者。
欢迎有志之士加入开发。
安装环境
=========
Linux或者Windows
1. go 1.11(推荐)或更新版本;
2. [可选] LiteIDE X34或更新版本;
3. 请关注Linux和Windows换行符的区别。
安装方法
=========
## 1. 在线安装
1. 安装go;
2. 运行go get命令:
```go
go get github.com/chfenger/goNum
```
## 2. 下载源码安装
1. 下载源代码并解压到指定文件夹例如“UserDir”下的src目录或其子目录例如“UserDir/src/”或“UserDir/src/xxx/xxx/”)下;
2. 添加UserDir到GOPATH;
3. 重启IDE或终端即可。
关于命名
==========
1. 包名'goNum'为算法库包;
2. 包名'goNum_test'为测试库包Benchmark;
3. 文件名'*_test.go'为测试文件名,其内容可作为算法包使用的参考手册。
设计初衷
=========
1. 旨在为自己和他人提供一个浅显易懂而又功能强大的数值算法库;
2. 优先保证速度和精度因此诸如defer等优秀方式由于过于影响速度而并未实际采用;
3. 完全以Go语言开发独立而不依赖于任何外部库。
算法
=====
(持续更新中...
- 基本数学
- 排列
- 二分法
- 组合
- 阶乘
- 切片元素最大值
- 切片元素绝对值最大值
- 切片元素从大到小排序
- 切片元素最小值
- 切片元素绝对值最小值
- 切片元素从小到大排序
- 矩阵1范数
- 矩阵无穷范数
- 向量的范数
- 次幂扩展
- 角度的三角函数和反三角函数
- 向量在三维空间的旋转
- Fibonacci数列
- 多项式求导
- 数据结构
- 单向链表
- 双向链表
-
- 矩阵
- 矩阵定义与操作
- 求矩阵行列式的列主元消去法
- 返回n阶单位矩阵二维切片表示
- 求矩阵逆的列主元消去法
- 求对称正定矩阵的平方根分解法
- 求矩阵Doolittlede LU分解
- 求对称矩阵全部特征值及其特征向量,经典雅可比法
- 求对称矩阵全部特征值及其特征向量,雅可比过关法
- 求矩阵A的主特征值及其特征向量
- 解一般方程
- 求解非线性方程的牛顿迭代
- 搜索法求方程解
- 单点弦截法
- 双点弦截法
- 简单迭代求解类x=g(x)方程的解
- 简单迭代求解类x=g(x)方程的解Aitken加速
- Muller法求f(x)=0的解
- 插值
- Hermite插值
- Hermite插值函数
- Lagrange插值
- Lagrange插值函数
- Newton插值
- Newton前向插值
- 用节点处的一阶导数表示的三次样条插值函数(一阶导数边界条件)
- 用节点处的一阶导数表示的三次样条插值函数(二阶导数边界条件)
- 用节点处的二阶导数表示的三次样条插值函数(一阶导数边界条件)
- 用节点处的二阶导数表示的三次样条插值函数(二阶导数边界条件)
- 数值积分
- 1-8级复化Newton-Cotes求积分公式
- 1-8级逐次分半复化Newton-Cotes求积分公式
- 不超过8次的Gauss-Lagendre求积分公式
- 1-8级Newton-Cotes求积分公式
- Rumberg(龙贝格)求积分公式
- 解线性方程组
- 求解矛盾方程组的最小二乘法
- 追赶法求解严格对角占优的三对角系数矩阵方程组
- 线性代数方程组的列主元消去法
- 解n阶线性方程组的Jocobi迭代法简单迭代法
- 解n阶线性方程组的Seidel迭代法
- 解n阶线性方程组的SOR(逐次超松弛)迭代法
- 解非线性方程组
- 多元非线性方程组Seidel迭代
- 数据拟合
- 多项式拟合
- 线性最小二乘拟合
- Bezier曲线拟合控制点
- 基于傅立叶Fourier级数的三角多项式拟合
- 误差评估
- 最大误差
- 平均误差
- 均方根误差
- 优化
- 黄金分割法求单峰单自变量极小值
- Fibonacci搜索法求单峰单自变量极小值
- 单纯形法求多自变量函数极小值
- 常微分方程
- 4步Adams外推ODE
- 三步Adams内插公式ODE
- Euler法ODE
- Euler预估校正ODE
- 梯形法ODE
- 二级二阶Runge-Kutta法
- 四级四阶Runge-Kutta法
- 四阶Runge-Kutta-Fehlberg变步长
- Heun法
- Adams-Bashforth-Moulton预估校正法
- Milne-Simpson预估校正法
- Hamming预估校正法
- 差分法
- 偏微分方程
- 双曲型偏微分方程差分解法(第一种差分格式)
- 双曲型偏微分方程差分解法(第二种差分格式)
- 抛物型偏微分方程差分解法(显式)
- 抛物型偏微分方程差分解法(隐式)
- 抛物型偏微分方程差分解法(六点对称)
- 椭圆型偏微分方程(Laplace)差分解法(五点格式)
- 椭圆型偏微分方程(Poisson)的差分解法(五点格式)
- 椭圆型偏微分方程(Helmholtz)的差分解法(五点格式)
- 排序
- 冒泡排序
- 选择排序
- 插入排序
- 希尔Shell排序
- 归并排序
- 快速排序
- 堆排序
- 计数排序
- 桶排序
- 基数排序
作者
=====
详见AUTHOR.MD文件
许可证书
=========
goNum是一款开源自由算法库您可以根据自己的需求发布或者修改但这一切需要在GNU GPL(General Public License) v3.0
或者较新版本的许可下进行。关于此许可证内容详见根目录下LICENSE文件或者<http://www.gnu.org/licenses/>
程锋 版权所有 2018
致谢
=====
00. 非常感谢家人朋友们的支持和理解,为此推辞了许多业余活动.
01. 特别感谢Google提供如此美妙的编程语言希望再接再励继续改善使之丰富。
10. 感谢某实验室提供的免费服务器。

104
vendor/github.com/nuknal/goNum/RK22.go generated vendored Normal file
View File

@@ -0,0 +1,104 @@
// RK22
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-8
版本 : 0.0.0
------------------------------------------------------
二级二阶Runge-Kutta法求解常微分方程组
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 192-199.
------------------------------------------------------
输入 :
fun 第i个方程(计算变量值向量, i)
x0 初值向量,(fn+1)x1一个xfn个因变量
xend 终止x
fn 方程个数
n 最大迭代步数
输出 :
B 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// RK22 二级二阶Runge-Kutta法求解常微分方程组
func RK22(fun func(Matrix, int) float64, x0 Matrix,
xend float64, fn, n int) (Matrix, bool) {
/*
二级二阶Runge-Kutta法求解常微分方程组
输入 :
fun 第i个方程(计算变量值向量, i)
x0 初值向量,(fn+1)x1一个xfn个因变量
xend 终止x
fn 方程个数
n 最大迭代步数
输出 :
B 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断方程个数是否对应初值个数
if x0.Rows != fn+1 {
panic("Error in goNum.RK22: Quantities of x0 and fn+1 are not equal")
}
sol := ZeroMatrix(fn+1, n+1)
var err bool = false
h := (xend - x0.Data[0]) / float64(n) //步长
//稳定性条件,建议迭代时即时判断,但此举会拖慢速度
// if true {
// lambda := ZeroMatrix(fn, 1)
// for j := 0; j < fn; j++ { //微分方程迭代
// lambda.Data[j] = fun(x0, j) / x0.Data[j+1]
// }
// maxl, _, _ := Max(lambda.Data)
// stab := 1.0 + maxl*h + math.Pow(maxl*h, 2.0)/2.0
// if math.Abs(stab) > 1 {
// panic("Error in goNum.RK22: Step length too large or step number little less")
// }
// }
//把初值赋给sol
for i := 0; i < fn+1; i++ {
sol.SetMatrix(i, 0, x0.Data[i])
}
for i := 1; i < n+1; i++ { //最大迭代次数迭代
temp0 := ZeroMatrix(fn+1, 1)
//给temp0赋i-1步值每一步开始
for j := 0; j < fn+1; j++ {
temp0.Data[j] = sol.GetFromMatrix(j, i-1)
}
k1 := ZeroMatrix(fn, 1)
k2 := ZeroMatrix(fn, 1)
//1. k1
for j := 0; j < fn; j++ { //微分方程迭代
k1.Data[j] = h * fun(temp0, j)
}
//2. k2
temp0.Data[0] = sol.GetFromMatrix(0, i-1) + 2.0*h/3.0 //xn+2h/3
for j := 1; j < fn+1; j++ { //yn+2k1/3
temp0.Data[j] = sol.GetFromMatrix(j, i-1) + 2.0*k1.Data[j-1]/3.0
}
for j := 0; j < fn; j++ { //微分方程迭代
k2.Data[j] = h * fun(temp0, j)
}
//i步值
sol.SetMatrix(0, i, sol.GetFromMatrix(0, i-1)+h) //xi
for j := 1; j < fn+1; j++ {
temp1 := sol.GetFromMatrix(j, i-1) + (k1.Data[j-1]+3.0*k2.Data[j-1])/4.0
sol.SetMatrix(j, i, temp1)
}
}
err = true
return sol, err
}

123
vendor/github.com/nuknal/goNum/RK44.go generated vendored Normal file
View File

@@ -0,0 +1,123 @@
// RK44
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-8
版本 : 0.0.0
------------------------------------------------------
四级四阶Runge-Kutta法求解常微分方程组
理论:
参考 李信真, 车刚明, 欧阳洁, 等. 计算方法. 西北工业大学
出版社, 2000, pp 192-199.
------------------------------------------------------
输入 :
fun 第i个方程(计算变量值向量, i)
x0 初值向量,(fn+1)x1一个xfn个因变量
xend 终止x
fn 方程个数
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// RK44 四级四阶Runge-Kutta法求解常微分方程组
func RK44(fun func(Matrix, int) float64, x0 Matrix,
xend float64, fn, n int) (Matrix, bool) {
/*
四级四阶Runge-Kutta法求解常微分方程组
输入 :
fun 第i个方程(计算变量值向量, i)
x0 初值向量,(fn+1)x1一个xfn个因变量
xend 终止x
fn 方程个数
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断方程个数是否对应初值个数
if x0.Rows != fn+1 {
panic("Error in goNum.RK44: Quantities of x0 and fn+1 are not equal")
}
sol := ZeroMatrix(fn+1, n+1)
var err bool = false
h := (xend - x0.Data[0]) / float64(n) //步长
//稳定性条件,建议迭代时即时判断,但此举会拖慢速度
// if true {
// lambda := ZeroMatrix(fn, 1)
// for j := 0; j < fn; j++ { //微分方程迭代
// lambda.Data[j] = fun(x0, j) / x0.Data[j+1]
// }
// maxl, _, _ := Max(lambda.Data)
// stab := 1.0 + maxl*h + math.Pow(maxl*h, 2.0)/2.0
// stab += math.Pow(maxl*h, 3.0)/6.0 + math.Pow(maxl*h, 4.0)/24.0
// if math.Abs(stab) > 1 {
// panic("Error in goNum.RK44: Step length too large or step number little less")
// }
// }
//把初值赋给sol
for i := 0; i < fn+1; i++ {
sol.SetMatrix(i, 0, x0.Data[i])
}
for i := 1; i < n+1; i++ { //最大迭代次数迭代
temp0 := ZeroMatrix(fn+1, 1)
//给temp0赋i-1步值每一步开始
for j := 0; j < fn+1; j++ {
temp0.Data[j] = sol.GetFromMatrix(j, i-1)
}
k1 := ZeroMatrix(fn, 1)
k2 := ZeroMatrix(fn, 1)
k3 := ZeroMatrix(fn, 1)
k4 := ZeroMatrix(fn, 1)
//1. k1
for j := 0; j < fn; j++ { //微分方程迭代
k1.Data[j] = h * fun(temp0, j)
}
//2. k2
temp0.Data[0] = sol.GetFromMatrix(0, i-1) + h/2.0 //xn+h/2
for j := 1; j < fn+1; j++ { //yn+k1/2
temp0.Data[j] = sol.GetFromMatrix(j, i-1) + k1.Data[j-1]/2.0
}
for j := 0; j < fn; j++ { //微分方程迭代
k2.Data[j] = h * fun(temp0, j)
}
//3. k3
for j := 1; j < fn+1; j++ { //yn+k2/2
temp0.Data[j] = sol.GetFromMatrix(j, i-1) + k2.Data[j-1]/2.0
}
for j := 0; j < fn; j++ { //微分方程迭代
k3.Data[j] = h * fun(temp0, j)
}
//4. k4
temp0.Data[0] = sol.GetFromMatrix(0, i-1) + h //xn+h
for j := 1; j < fn+1; j++ { //yn+k3
temp0.Data[j] = sol.GetFromMatrix(j, i-1) + k3.Data[j-1]
}
for j := 0; j < fn; j++ { //微分方程迭代
k4.Data[j] = h * fun(temp0, j)
}
//i步值
sol.SetMatrix(0, i, sol.GetFromMatrix(0, i-1)+h) //xi
for j := 1; j < fn+1; j++ {
temp1 := k1.Data[j-1] + 2.0*k2.Data[j-1] + 2.0*k3.Data[j-1] + k4.Data[j-1]
temp1 = sol.GetFromMatrix(j, i-1) + temp1/6.0
sol.SetMatrix(j, i, temp1)
}
}
err = true
return sol, err
}

187
vendor/github.com/nuknal/goNum/RKF45.go generated vendored Normal file
View File

@@ -0,0 +1,187 @@
// RKF45
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-19
版本 : 0.0.0
------------------------------------------------------
四级五阶变步长Runge-Kutta法求解常微分方程组
理论:
参考 John H. Mathews and Kurtis D. Fink. Numerical
methods using MATLAB, 4th ed. Pearson
Education, 2004. ss 9.5.4.
------------------------------------------------------
输入 :
fun 第i个方程(计算变量值向量, i)
x0 初值向量,(fn+1)x1一个xfn个因变量
xend 终止x
tol 步长控制误差
fn 方程个数
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// RKF45 四级五阶变步长Runge-Kutta法求解常微分方程组
func RKF45(fun func(Matrix, int) float64, x0 Matrix,
xend, tol float64, fn, n int) (Matrix, bool) {
/*
四级五阶变步长Runge-Kutta法求解常微分方程组
输入 :
fun 第i个方程(计算变量值向量, i)
x0 初值向量,(fn+1)x1一个xfn个因变量
xend 终止x
tol 步长控制误差
fn 方程个数
n 最大迭代步数
输出 :
sol 解向量
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断方程个数是否对应初值个数
if x0.Rows != fn+1 {
panic("Error in goNum.RKF45: Quantities of x0 and fn+1 are not equal")
}
//判断tol值
if tol <= 0.0 {
panic("Error in goNum.RKF45: tol less than or euqals to zero")
}
//判断xend值
if xend <= x0.Data[0] {
panic("Error in goNum.RKF45: xend less than or euqals to x0")
}
sol0 := ZeroMatrix(fn+1, n+1)
var err bool = false
h := 100.0 * (xend - x0.Data[0]) / float64(n) //初始步长100倍最小步长可修改
//把初值赋给sol
for i := 0; i < fn+1; i++ {
sol0.SetMatrix(i, 0, x0.Data[i])
}
//nreal解矩阵实际长度
var i, nreal int = 1, 1
for sol0.GetFromMatrix(0, i-1) < xend { //最大迭代次数控制
temp0 := ZeroMatrix(fn+1, 1)
//给temp0赋i-1步值每一步开始
for j := 0; j < fn+1; j++ {
temp0.Data[j] = sol0.GetFromMatrix(j, i-1)
}
k1 := ZeroMatrix(fn, 1)
k2 := ZeroMatrix(fn, 1)
k3 := ZeroMatrix(fn, 1)
k4 := ZeroMatrix(fn, 1)
k5 := ZeroMatrix(fn, 1)
k6 := ZeroMatrix(fn, 1)
//1. k1
for j := 0; j < fn; j++ { //微分方程迭代
k1.Data[j] = h * fun(temp0, j)
}
//2. k2
temp0.Data[0] = sol0.GetFromMatrix(0, i-1) + h/4.0 //xn+h/4
for j := 1; j < fn+1; j++ { //yn+k1/4
temp0.Data[j] = sol0.GetFromMatrix(j, i-1) + k1.Data[j-1]/4.0
}
for j := 0; j < fn; j++ { //微分方程迭代
k2.Data[j] = h * fun(temp0, j)
}
//3. k3
temp0.Data[0] = sol0.GetFromMatrix(0, i-1) + 3.0*h/8.0 //xn+3h/8
for j := 1; j < fn+1; j++ { //yn+3k1/32+9k2/32
temp0.Data[j] = sol0.GetFromMatrix(j, i-1) + 3.0*k1.Data[j-1]/32.0 +
9.0*k2.Data[j-1]/32.0
}
for j := 0; j < fn; j++ { //微分方程迭代
k3.Data[j] = h * fun(temp0, j)
}
//4. k4
temp0.Data[0] = sol0.GetFromMatrix(0, i-1) + 12.0*h/13.0 //xn+12h/13
for j := 1; j < fn+1; j++ { //yn+1932k1/2197-7200k2/2197+7296k3/2197
temp0.Data[j] = sol0.GetFromMatrix(j, i-1) + 1932.0*k1.Data[j-1]/2197.0 -
7200.0*k2.Data[j-1]/2197.0 + 7296.0*k3.Data[j-1]/2197.0
}
for j := 0; j < fn; j++ { //微分方程迭代
k4.Data[j] = h * fun(temp0, j)
}
//5. k5
temp0.Data[0] = sol0.GetFromMatrix(0, i-1) + h //xn+h
for j := 1; j < fn+1; j++ { //yn+439k1/216-8k2+3680k3/513-845k4/4104
temp0.Data[j] = sol0.GetFromMatrix(j, i-1) + 439.0*k1.Data[j-1]/216.0 -
8.0*k2.Data[j-1] + 3680.0*k3.Data[j-1]/513.0 - 845.0*k4.Data[j-1]/4104.0
}
for j := 0; j < fn; j++ { //微分方程迭代
k5.Data[j] = h * fun(temp0, j)
}
//6. k6
temp0.Data[0] = sol0.GetFromMatrix(0, i-1) + h/2.0 //xn+h/2
for j := 1; j < fn+1; j++ { //yn-8k1/27+2k2-3544k3/2565+1859k4/4104-11k5/40
temp0.Data[j] = sol0.GetFromMatrix(j, i-1) - 8.0*k1.Data[j-1]/27.0 +
2.0*k2.Data[j-1] - 3544.0*k3.Data[j-1]/2565.0 + 1859.0*k4.Data[j-1]/4104.0 -
11.0*k5.Data[j-1]/40.0
}
for j := 0; j < fn; j++ { //微分方程迭代
k6.Data[j] = h * fun(temp0, j)
}
//误差与步长
errtemp := ZeroMatrix(fn, 1) //=ABS(z_(k+1)-y_(k+1))
for j := 1; j < fn+1; j++ {
errtemp.Data[j-1] = k1.Data[j-1]/360.0 - 128.0*k3.Data[j-1]/4275.0 -
2197.0*k4.Data[j-1]/75240.0 + k5.Data[j-1]/50.0 + 2.0*k6.Data[j-1]/55.0
}
errtemp0, _, _ := MaxAbs(errtemp.Data)
//正常推进
if math.Abs(errtemp0) < tol {
//i步值
sol0.SetMatrix(0, i, sol0.GetFromMatrix(0, i-1)+h) //xi
for j := 1; j < fn+1; j++ {
soltemp1 := 25.0*k1.Data[j-1]/216.0 + 1408.0*k3.Data[j-1]/2565.0 +
2197.0*k4.Data[j-1]/4104.0 - k5.Data[j-1]/5.0
soltemp1 = sol0.GetFromMatrix(j, i-1) + soltemp1
sol0.SetMatrix(j, i, soltemp1)
}
i++
nreal = i
continue
}
//最大步数强边界
if i >= n {
break
}
//变步长
scale := tol * h / (2.0 * math.Abs(errtemp0))
scale = math.Pow(scale, 0.25)
if scale < 0.75 {
h = h / 2.0
} else if scale > 1.5 {
h = h * 2.0
}
}
//解矩阵缩减
sol := ZeroMatrix(fn+1, nreal)
for j := 0; j < nreal; j++ {
for k := 0; k < fn+1; k++ {
sol.SetMatrix(k, j, sol0.GetFromMatrix(k, j))
}
}
err = true
return sol, err
}

86
vendor/github.com/nuknal/goNum/RadixSort.go generated vendored Normal file
View File

@@ -0,0 +1,86 @@
// RadixSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-06
版本 : 0.0.0
------------------------------------------------------
基数排序法
理论:
时间复杂度: O(n*k)
最好情况 : O(n*k)
最坏情况 : O(n*k)
空间复杂度: O(n+k)
稳定性 : 稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
注意:
仅对整数排序有效
------------------------------------------------------
*/
package goNum
// RadixSort 基数排序法
func RadixSort(in []int) ([]int, bool) {
/*
基数排序法
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if len(in) < 1 {
panic("Error in goNum.BucketSort: Empty input Matrix")
} else if len(in) == 1 {
return in, true
}
n := len(in)
var err bool = false
var maxDigit, mod, div int = 0, 10, 1
soltemp := make([]int, n)
sol := make([]int, n)
for i := 0; i < n; i++ {
soltemp[i] = in[i]
}
temp := make([][]int, 10)
//排序开始
//最大数的位数
max := IntMax(soltemp)
for max != 0 {
max = max / 10
maxDigit++
}
for i := 0; i < maxDigit; i++ {
for j := 0; j < n; j++ {
var num int = (soltemp[j] % mod) / div
temp[num] = append(temp[num], soltemp[j])
}
ind := 0
for j := 0; j < len(temp); j++ {
for k := 0; k < len(temp[j]); k++ {
sol[ind] = temp[j][k]
ind++
temp[j] = []int{} //必须置空,
}
}
mod = mod * 10
div = div * 10
}
err = true
return sol, err
}

97
vendor/github.com/nuknal/goNum/SearchByStep.go generated vendored Normal file
View File

@@ -0,0 +1,97 @@
// goNum 是一个开源的go语言数值算法库[goNum is an open
// numerical library purely based on go programming language]
package goNum
// SearchByStep
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-10-31
版本 : 0.0.0
------------------------------------------------------
此程序设计使用搜索法来求解连续、单自变量函数指定有限区间
上的解
------------------------------------------------------
输入 :
fn 函数,定义为等式左侧部分,右侧为零
a, b 求解区间一般要求a<b但不严格
N 步数,区间细分粒度
tol 误差上限
输出 :
sol 解值
err 解出标志false-未全部解出true-全部解出
------------------------------------------------------
*/
import "math"
// SearchByStep 搜索法来求解连续、单自变量函数指定有限区间上的解
func SearchByStep(fn func(float64) float64, a, b float64,
N int, tol float64) ([]float64, bool) {
/*
搜索法来求解连续、单自变量函数指定有限区间上的解
输入 :
fn 函数,定义为等式左侧部分,右侧为零
a, b 求解区间一般要求a<b但不严格
N 步数,区间细分粒度
tol 误差上限
输出 :
sol 解值
err 解出标志false-未全部解出true-全部解出
*/
//初始化
ab0 := make([]float64, 0, 1000)
ab1 := make([]float64, 0, 1000)
sol := make([]float64, 0, 1000)
err := false
j := 0 //解的数量
h := (b - a) / float64(N) //搜索步长,应小于最近两解的距离
//确定单解区间,并存入对应数组
for i := 1; i < N+1; i++ {
if (fn(a+float64(i)*h) > 0 && fn(a+float64(i-1)*h) < 0) || (fn(a+float64(i)*h) < 0 && fn(a+float64(i-1)*h) > 0) {
ab0 = append(ab0, a+float64(i-1)*h)
ab1 = append(ab1, a+float64(i)*h)
sol = append(sol, (ab0[j]+ab1[j])/2.0)
j++
}
}
//单解区间内循环细化,直至精度满足要求
for i := 0; i < j; i++ {
Nn := 0 //死循环约束
solved := 0 //解得标志
for {
Nn += 1
//循环超过一定数
if Nn > 1000 {
err = false
return sol, err
}
h = (ab1[i] - ab0[i]) / float64(N)
for ii := 1; ii < N+1; ii++ {
if (fn(ab0[i]+float64(ii)*h) > 0 && fn(ab0[i]+float64(ii-1)*h) < 0) || (fn(ab0[i]+float64(ii)*h) < 0 && fn(ab0[i]+float64(ii-1)*h) > 0) {
ab0[i] = ab0[i] + float64(ii-1)*h
ab1[i] = ab0[i] + float64(ii)*h
//是否满足精度要求
if math.Abs(fn((ab0[i]+ab1[i])/2.0)) < tol {
sol[i] = (ab0[i] + ab1[i]) / 2.0
solved = 1
}
break //退出此区间的搜索循环
}
}
//如果解除此区间的解,则退出死循环
if solved == 1 {
break
}
}
}
//返回
err = true
return sol, err
}

84
vendor/github.com/nuknal/goNum/Secant1P.go generated vendored Normal file
View File

@@ -0,0 +1,84 @@
// Secant1P
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-02
版本 : 0.0.0
------------------------------------------------------
单点弦截法求解方程 f(x)=0 在区间[a, b]内的根
理论:
1. 当xE[a, b]f'(x)、f''(x)连续且不变号
2. 选取初值x0E[a, b]使f(x0)*f''(x0) > 0x0选取其中
一个则x1选另外一个
线性收敛
------------------------------------------------------
输入 :
fn f(x)函数定义为等式左侧部分右侧为0
fn2 f''(x)函数
a, b 求解区间
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// Secant1P 单点弦截法求解方程 f(x)=0 在区间[a, b]内的根
func Secant1P(fn, fn2 func(float64) float64, a, b float64,
N int, tol float64) (float64, bool) {
/*
单点弦截法求解方程 f(x)=0 在区间[a, b]内的根
输入 :
fn f(x)函数定义为等式左侧部分右侧为0
fn2 f''(x)函数
a, b 求解区间
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var err bool = false
//判断a b的次序以及选取初值
if b < a {
return sol, err
}
switch {
// a
case fn(a)*fn2(a) > 0:
for i := 0; i < N; i++ {
sol = a - (b-a)*fn(a)/(fn(b)-fn(a))
// 求解成功
if math.Abs(sol-b) < tol {
err = true
return sol, err
}
b = sol
}
return sol, err
// b
case fn(b)*fn2(b) > 0:
for i := 0; i < N; i++ {
sol = b - (a-b)*fn(b)/(fn(a)-fn(b))
// 求解成功
if math.Abs(sol-a) < tol {
err = true
return sol, err
}
a = sol
}
return sol, err
}
return sol, err
}

79
vendor/github.com/nuknal/goNum/Secant2P.go generated vendored Normal file
View File

@@ -0,0 +1,79 @@
// Secant2P
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-02
版本 : 0.0.0
------------------------------------------------------
双点弦截法求解方程 f(x)=0 在区间[a, b]内的根
理论:
1. 当xE[a, b]f''(x)连续f'(x) != 0
xn0*f(xn1) - xn1*f(xn0)
xn2 = -------------------------
f(xn1) - f(xn0)
超线性收敛,收敛阶(1+5^0.5)/2
------------------------------------------------------
输入 :
fn f(x)函数定义为等式左侧部分右侧为0
a, b 求解区间
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// Secant2P 双点弦截法求解方程 f(x)=0 在区间[a, b]内的根
func Secant2P(fn func(float64) float64, a, b float64,
N int, tol float64) (float64, bool) {
/*
双点弦截法求解方程 f(x)=0 在区间[a, b]内的根
输入 :
fn f(x)函数定义为等式左侧部分右侧为0
a, b 求解区间
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var err bool = false
//判断a b的次序
if (b < a) || (fn(a)*fn(b) > 0) {
return sol, err
}
// 求解
sol = (a*fn(b) - b*fn(a)) / (fn(b) - fn(a))
for i := 0; i < N; i++ {
//判断是否解得
if (fn(a)*fn(sol) > 0) && (math.Abs(sol-a) < tol) {
err = true
return sol, err
} else if (fn(a)*fn(sol) < 0) && (math.Abs(sol-b) < tol) {
err = true
return sol, err
}
//下一步
switch {
case fn(a)*fn(sol) > 0:
a = sol
default:
b = sol
}
sol = (a*fn(b) - b*fn(a)) / (fn(b) - fn(a))
}
return sol, err
}

69
vendor/github.com/nuknal/goNum/SelectSort.go generated vendored Normal file
View File

@@ -0,0 +1,69 @@
// SelectSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-05
版本 : 0.0.0
------------------------------------------------------
选择排序法
理论:
时间复杂度: O(n^2)
最好情况 : O(n^2)
最坏情况 : O(n^2)
空间复杂度: O(1)
稳定性 : 不稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// SelectSort 选择排序法
func SelectSort(in Matrix) (Matrix, bool) {
/*
选择排序法
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if in.Rows != 1 {
panic("Error in goNum.SelectSort: Input Matrix error")
}
if in.Columns < 1 {
panic("Error in goNum.SelectSort: Empty input Matrix")
} else if in.Columns == 1 {
return in, true
}
n := in.Columns
sol := ZeroMatrix(1, n)
var err bool = false
//初始化sol
for i := 0; i < n; i++ {
sol.Data[i] = in.Data[i]
}
//排序开始
for i := 0; i < n-1; i++ {
mini := i
for j := i + 1; j < n; j++ {
if sol.Data[mini] > sol.Data[j] {
mini = j
}
}
sol.Data[i], sol.Data[mini] = sol.Data[mini], sol.Data[i]
}
err = true
return sol, err
}

71
vendor/github.com/nuknal/goNum/ShellSort.go generated vendored Normal file
View File

@@ -0,0 +1,71 @@
// ShellSort
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2019-03-05
版本 : 0.0.0
------------------------------------------------------
希尔Shell排序法
理论:
时间复杂度: O(n^1.3)
最好情况 : O(n)
最坏情况 : O(n^2)
空间复杂度: O(1)
稳定性 : 不稳定
------------------------------------------------------
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// ShellSort 希尔Shell排序法
func ShellSort(in Matrix) (Matrix, bool) {
/*
希尔Shell排序法
输入 :
in 输入矩阵, 1xn
输出 :
sol 排序结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
//判断初值维数
if in.Rows != 1 {
panic("Error in goNum.ShellSort: Input Matrix error")
}
if in.Columns < 1 {
panic("Error in goNum.ShellSort: Empty input Matrix")
} else if in.Columns == 1 {
return in, true
}
n := in.Columns
sol := ZeroMatrix(1, n)
subn := n / 2
var err bool = false
//初始化sol
for i := 0; i < n; i++ {
sol.Data[i] = in.Data[i]
}
//排序开始
for ; subn > 0; subn = subn / 2 { //循环到增量减小为1
for i := subn; i < n; i++ {
temp := sol.Data[i]
j := i - subn
for ; j >= 0 && sol.Data[j] > temp; j -= subn {
sol.Data[j+subn] = sol.Data[j]
}
sol.Data[j+subn] = temp
}
}
err = true
return sol, err
}

85
vendor/github.com/nuknal/goNum/SimpleIterate.go generated vendored Normal file
View File

@@ -0,0 +1,85 @@
// SimpleIterate
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-01
版本 : 0.0.0
------------------------------------------------------
简单迭代求解类x=g(x)方程的解 xn+1=g(xn)
理论:
1. g(x)在区间[a, b]可导;
2. 当xE[a, b]g(x)E[a, b]
3. 对于任意xE[a, b]|g(x)| <= L < 1
线性收敛
则求解所得的根xn与真实根xr的的误差
L^n
|xn-xr| <= ----- |x1-x0|
1-L
------------------------------------------------------
输入 :
fn g(x)函数定义为等式右侧部分左侧为x
a, b 求解区间
c 求解初值
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// SimpleIterate 简单迭代求解类x=g(x)方程的解 xn+1=g(xn)
func SimpleIterate(fn func(float64) float64, a, b, c float64,
N int, tol float64) (float64, bool) {
/*
简单迭代求解类x=g(x)方程的解 xn+1=g(xn)
输入 :
fn g(x)函数定义为等式右侧部分左侧为x
a, b 求解区间
c 求解初值
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol float64
var err bool = false
// 判断端点和初值是否为所求之解
switch {
case math.Abs(fn(a)-a) < tol:
sol = a
err = true
return sol, err
case math.Abs(fn(b)-b) < tol:
sol = b
err = true
return sol, err
case math.Abs(fn(c)-c) < tol:
sol = c
err = true
return sol, err
}
//求解
sol = fn(c)
for i := 0; i < N; i++ {
if (math.Abs(sol - c)) < tol {
err = true
return sol, err
}
c = sol
sol = fn(c)
}
return sol, err
}

95
vendor/github.com/nuknal/goNum/SimpleIterateAitken.go generated vendored Normal file
View File

@@ -0,0 +1,95 @@
// SimpleIterateAitken
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-11-01
版本 : 0.0.0
------------------------------------------------------
简单迭代求解类x=g(x)方程的解 xn+1=g(xn)
理论:
1. g(x)在区间[a, b]可导;
2. 当xE[a, b]g(x)E[a, b]
3. 对于任意xE[a, b]|g(x)| <= L < 1
线性收敛
则求解所得的根xn与真实根xr的的误差
L^n
|xn-xr| <= ----- |x1-x0|
1-L
Aitken boost method
xn1 = g(xn)
xn2 = g(xn1)
xn2*xn - xn1^2
xn1 = ------------------ (n = 0, 1, 2,...)
xn2 - 2*xn1 + xn
------------------------------------------------------
输入 :
fn g(x)函数定义为等式右侧部分左侧为x
a, b 求解区间
c 求解初值
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// SimpleIterateAitken 简单迭代求解类x=g(x)方程的解 xn+1=g(xn)
func SimpleIterateAitken(fn func(float64) float64, a, b, c float64,
N int, tol float64) (float64, bool) {
/*
简单迭代求解类x=g(x)方程的解 xn+1=g(xn)
输入 :
fn g(x)函数定义为等式右侧部分左侧为x
a, b 求解区间
c 求解初值
N 步数上限
tol 误差上限
输出 :
sol 解值
err 解出标志false-未解出或达到步数上限;
true-全部解出
*/
var sol, n2 float64
var err bool = false
// 判断端点和初值是否为所求之解
switch {
case math.Abs(fn(a)-a) < tol:
sol = a
err = true
return sol, err
case math.Abs(fn(b)-b) < tol:
sol = b
err = true
return sol, err
case math.Abs(fn(c)-c) < tol:
sol = c
err = true
return sol, err
}
//求解
sol = fn(c)
n2 = fn(sol)
sol = (n2*c - sol*sol) / (n2 - 2.0*sol + c)
for i := 0; i < N; i++ {
if (math.Abs(sol - c)) < tol {
err = true
return sol, err
}
c = sol
sol = fn(c)
n2 = fn(sol)
sol = (n2*c - sol*sol) / (n2 - 2.0*sol + c)
}
return sol, err
}

58
vendor/github.com/nuknal/goNum/TriangleDegree.go generated vendored Normal file
View File

@@ -0,0 +1,58 @@
// TriangleDegree
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-13
版本 : 0.0.0
------------------------------------------------------
以角度为输入的三角函数计算
理论:
------------------------------------------------------
输入 :
x 角度值
y 数值
输出 :
*** 数值或角度值
------------------------------------------------------
*/
package goNum
import (
"math"
)
//三角函数
// Sind 角度的正弦
func Sind(x float64) float64 {
return math.Sin(x * math.Pi / 180.0)
}
// Cosd 角度的余弦
func Cosd(x float64) float64 {
return math.Cos(x * math.Pi / 180.0)
}
// Tand 角度的正切
func Tand(x float64) float64 {
return math.Tan(x * math.Pi / 180.0)
}
//反三角函数
// Asind 反正弦的角度
func Asind(y float64) float64 {
return 180.0 * math.Asin(y) / math.Pi
}
// Acosd 反余弦的角度
func Acosd(y float64) float64 {
return 180.0 * math.Acos(y) / math.Pi
}
// Atand 反正切的角度
func Atand(y float64) float64 {
return 180.0 * math.Atan(y) / math.Pi
}

20
vendor/github.com/nuknal/goNum/UPDATES.md generated vendored Normal file
View File

@@ -0,0 +1,20 @@
- 2019-03-06 <20><><EFBFBD>ӹ鲢<D3B9><E9B2A2><EFBFBD>򡢿<EFBFBD><F2A1A2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򡢶<EFBFBD><F2A1A2B6><EFBFBD><EFBFBD>򡢼<EFBFBD><F2A1A2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͱ<EFBFBD><CDB0><EFBFBD>򡢻<EFBFBD><F2A1A2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
- 2019-03-05 <20><><EFBFBD><EFBFBD>ð<EFBFBD><C3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD>򡢲<EFBFBD><F2A1A2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD>Shell<6C><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
- 2019-03-01 <20><><EFBFBD>Ӻ<EFBFBD><D3BA><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>ʹ<EFBFBD><CAB9>godoc<6F><63><EFBFBD><EFBFBD>LiteIDE<44><EFBFBD><E0BCAD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
- 2019-01-08 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Բ<EFBFBD><D4B2>ƫ΢<C6AB>ַ<EFBFBD><D6B7><EFBFBD>(Poisson)<29>IJ<EFBFBD><C4B2>ֽⷨ<D6BD><E2B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Բ<EFBFBD><D4B2>ƫ΢<C6AB>ַ<EFBFBD><D6B7><EFBFBD>(Helmholtz)<29>IJ<EFBFBD><C4B2>ֽⷨ<D6BD><E2B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>
- 2019-01-07 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Բ<EFBFBD><D4B2>ƫ΢<C6AB>ַ<EFBFBD><D6B7><EFBFBD>(Laplace)<29><><EFBFBD>ֽⷨ<D6BD><E2B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>
- 2018-12-27 <20><><EFBFBD>Ӹ<EFBFBD><D3B8><EFBFBD>Matrix<69><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AppendRow<6F><77>AppendColumn
- 2018-12-26 <20><><EFBFBD><EFBFBD>EulerԤ<72><D4A4>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>η<EFBFBD><CEB7><EFBFBD>һЩ<D2BB><D0A9>Ӱ<EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>
<20><><EFBFBD><EFBFBD>Matrix<69><78><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<D2BB><D0A9>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<20><><EFBFBD><EFBFBD>Heun<75><6E><EFBFBD><EFBFBD>Milne-SimpsonԤ<6E><D4A4>У<EFBFBD><D0A3><EFBFBD><EFBFBD>Adams-Bashforth-MoultonԤ<6E><D4A4>У<EFBFBD><D0A3><EFBFBD><EFBFBD>HammingԤ<67><D4A4>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>΢<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
- 2018-12-25 <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>ʽ<EFBFBD>󵼣<EFBFBD><F3B5BCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>η<EFBFBD><CEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сֵ
- 2018-12-24 <20><><EFBFBD><EFBFBD>Fibonacci<63><69><EFBFBD>У<EFBFBD><D0A3>ƽ<EFBFBD><C6BD>ָ<D6B8><EEB7A8>Fibonacci<63><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Сֵ
- 2018-12-23 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Bezier<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD><CFBF>Ƶ<EFBFBD>
- 2018-12-21 <20><>ԭ<EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB7B6>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2B7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD>Seidel<65><6C><EFBFBD><EFBFBD>
- 2018-12-20 <20><><EFBFBD>ӽ<EFBFBD>һ<EFBFBD><EFBFBD>̵<EFBFBD>Muller<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>ת
- 2018-12-19 <20><><EFBFBD><EFBFBD>Runge-Kutta-Felhberg<72><EFBFBD><E4B2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ODE<44><45>
- 2018-12-17 <20><><EFBFBD><EFBFBD>ƫ΢<C6AB>ַ<EFBFBD><D6B7>̲<EFBFBD><CCB2>ֽⷨ
- 2018-12-13 <20><><EFBFBD>ӳ<EFBFBD>΢<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>
- 2018-12-13 <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>ֵ<EFBFBD><EFBFBD><E3B7A8><EFBFBD>Ƕȵ<C7B6><C8B5><EFBFBD><EFBFBD>Ǻͷ<C7BA><CDB7><EFBFBD><EFBFBD>Ǻ<EFBFBD><C7BA><EFBFBD>
- 2018-12-11 <20><><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>Ϻ<EFBFBD><CFBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>

91
vendor/github.com/nuknal/goNum/VectorRotation.go generated vendored Normal file
View File

@@ -0,0 +1,91 @@
// VectorRotation
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-20
版本 : 0.0.0
------------------------------------------------------
向量在三维空间的旋转
理论:
------------------------------------------------------
输入 :
u 初始向量3x1
angle 旋转角度3x1按绕x、y、z顺序弧度
seq 旋转顺序 []int
输出 :
sol 解,向量
err 解出标志false-未解出或达到边界;
true-全部解出
------------------------------------------------------
*/
package goNum
import (
"math"
)
// VectorRotation 向量在三维空间的旋转
func VectorRotation(u, angle Matrix, seq []int) (Matrix, bool) {
/*
向量在三维空间的旋转
输入 :
u 初始向量3x1
angle 旋转角度3x1按绕x、y、z顺序弧度
seq 旋转顺序 []int
输出 :
sol 解,向量
err 解出标志false-未解出或达到边界;
true-全部解出
*/
//向量大小
if u.Rows != 3 {
panic("Error in goNum.VectorRotation: Vector length is not right")
}
if angle.Rows != 3 {
panic("Error in goNum.VectorRotation: angles number is not right")
}
if len(seq) != 3 {
panic("Error in goNum.VectorRotation: seq length is not right")
}
//判断角度大小
for i := 0; i < 3; i++ {
if angle.Data[i] > math.Pi/2.0 {
panic("Error in goNum.VectorRotation: angle value is not right")
}
}
sol := ZeroMatrix(3, 1)
var err bool = false
//matrix around x
Rx := NewMatrix(3, 3, []float64{
1.0, 0.0, 0.0,
0.0, math.Cos(angle.Data[0]), -1.0 * math.Sin(angle.Data[0]),
0.0, math.Sin(angle.Data[0]), math.Cos(angle.Data[0])})
//matrix around y
Ry := NewMatrix(3, 3, []float64{
math.Cos(angle.Data[1]), 0.0, math.Sin(angle.Data[1]),
0.0, 1.0, 0.0,
-1.0 * math.Sin(angle.Data[1]), 0.0, math.Cos(angle.Data[1])})
//matrix around z
Rz := NewMatrix(3, 3, []float64{
math.Cos(angle.Data[2]), -1.0 * math.Sin(angle.Data[2]), 0.0,
math.Sin(angle.Data[2]), math.Cos(angle.Data[2]), 0.0,
0.0, 0.0, 1.0})
W := IdentityE(3)
for i := 0; i < 3; i++ {
switch seq[i] {
case 1:
W = DotPruduct(Rx, W)
case 2:
W = DotPruduct(Ry, W)
case 3:
W = DotPruduct(Rz, W)
default:
panic("Error in goNum.VectorRotation: sequence number is not right")
}
}
sol = DotPruduct(W, u)
err = true
return sol, err
}