Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Add solutions to a SLAE
Browse files Browse the repository at this point in the history
  • Loading branch information
kleo-53 committed Mar 21, 2024
1 parent 42b2260 commit 7068460
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
13 changes: 12 additions & 1 deletion task2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func printTable(A, L, LT mat.Dense) {

func main() {
A := mat.NewDense(4, 4, []float64{5, 2, 3, 4, 2, 4, 2, 2, 3, 2, 8, 2, 4, 2, 2, 9})
B := mat.NewDense(4, 1, []float64{4, 2, 0, 5})
L := calculateL(A)
fmt.Println("Матрица L:")
matPrint(L)
Expand All @@ -41,7 +42,17 @@ func main() {
if ok {
fmt.Println("A = L*L^T!")
} else {
fmt.Println(":(")
fmt.Println("A != L*L^T :(")
}
printTable(*A, *L, *TDense(*L))
fmt.Println("Решение системы с помощью библиотеки:")
x, xH, eps := matSolve(A, B), sqrtSolve(*L, *B), 1e-12
matPrint(&x)
fmt.Println("\nРешение системы c помощью разложения Холецкого")
matPrint(&xH)
if mat.EqualApprox(&x, &xH, eps) {
fmt.Printf("\nОтветы совпадают с точностью %s", fmt.Sprint(eps))
} else {
fmt.Println("Ответы не совпадают")
}
}
17 changes: 17 additions & 0 deletions task2/square_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func check(A, L mat.Dense) (ok bool, res *mat.Dense) {
return mat.EqualApprox(&A, res, 1e-12), res
}

// решение системы методом Холецкого
func sqrtSolve(L, B mat.Dense) (x mat.Dense) {
y := matSolve(&L, &B)
x = matSolve(L.T(), &y)
return x
}

// ____________________________________________________
// вычисление спектрального критерия по матрице
func spectralCriterion(A mat.Dense) (res float64) {
Expand Down Expand Up @@ -103,3 +110,13 @@ func TDense(A mat.Dense) (res *mat.Dense) {
}
return re
}

// нахождение решения СЛАУ вида Ax=b, возвращает вектор X
func matSolve(A mat.Matrix, b mat.Matrix) (X mat.Dense) {
err := X.Solve(A, b)
if err != nil {
fmt.Printf("Ошибка при решении СЛАУ: %v", err)
return
}
return X
}
33 changes: 23 additions & 10 deletions task2/square_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,45 @@ import (
"gonum.org/v1/gonum/mat"
)

func TestIllConditionedMatrix(t *testing.T) {
func TestDifferentConditionedMatrix(t *testing.T) {
cases := []struct {
in []float64
inA []float64
inB []float64
want bool
}{
{[]float64{1.0001, 0.9999, 0.9998, 0.9999, 1.0002, 0.9997, 0.9998, 0.9997, 1.0003}, true},
{[]float64{2.001, 0.999, 0.998, 0.999, 3.002, 0.997, 0.998, 0.997, 4.003}, true},
{[]float64{10}, true},
{[]float64{2, 0, 0, 0, 0, 9, 0, 0, 0, 0, 5, 0, 0, 0, 0, 4}, true},
{[]float64{10, 1, 2, 3, 4, 5, 1, 20, 6, 7, 8, 9, 2, 6, 30, 10, 11, 12, 3, 7, 10, 40, 13, 14, 4, 8, 11, 13, 50, 15, 5, 9, 12, 14, 15, 60}, true},
{[]float64{1.0001, 0.9999, 0.9998, 0.9999, 1.0002, 0.9997, 0.9998, 0.9997, 1.0003}, []float64{1, 2, 3}, true},
{[]float64{2.001, 0.999, 0.998, 0.999, 3.002, 0.997, 0.998, 0.997, 4.003}, []float64{4.1, 0.5, 2.6}, true},
{[]float64{10}, []float64{2}, true},
{[]float64{2, 0, 0, 0, 0, 9, 0, 0, 0, 0, 5, 0, 0, 0, 0, 4}, []float64{4, 4.5, 5, 1}, true},
{[]float64{10, 1, 2, 3, 4, 5, 1, 20, 6, 7, 8, 9, 2, 6, 30, 10, 11, 12, 3, 7, 10, 40, 13, 14, 4, 8, 11, 13, 50, 15, 5, 9, 12, 14, 15, 60}, []float64{9, 1, 3, 8, 12, 6}, true},
}
for _, c := range cases {
n := math.Sqrt(float64(len(c.in)))
A := mat.NewDense(int(n), int(n), c.in)
n := math.Sqrt(float64(len(c.inA)))
A := mat.NewDense(int(n), int(n), c.inA)
B := mat.NewDense(int(n), 1, c.inB)
fmt.Println("Матрица:")
matPrint(A)
fmt.Println("")
fmt.Printf("Спектральный критерий обусловленности матрицы: %f\n\n", spectralCriterion(*A))
L := calculateL(A)
got, LLT := check(*A, *L)
if got != c.want {
t.Errorf("get %q, want %q", fmt.Sprint(c.in), fmt.Sprint(c.want))
t.Errorf("Полученные матрицы не равны")
} else {
fmt.Println("L*L^T =")
matPrint(LLT)
}
fmt.Println("\nРешение системы с помощью библиотеки:")
x, xH, eps := matSolve(A, B), sqrtSolve(*L, *B), 1e-10
matPrint(&x)
fmt.Println("\nРешение системы c помощью разложения Холецкого")
matPrint(&xH)
if mat.EqualApprox(&x, &xH, eps) {
fmt.Printf("\nОтветы совпадают с точностью %s\n", fmt.Sprint(eps))
} else {
fmt.Println("Ответы не совпадают")
t.Errorf("Ответы не совпадают")
}
fmt.Println("_______________________________________________")
}
}

0 comments on commit 7068460

Please sign in to comment.