ssa
1package main2
3import (4"fmt"5"log"6
7"github.com/RB-PRO/ssa/pkg/oss"8"gonum.org/v1/gonum/mat"9)
10
11type SDVs struct {12V *mat.Dense13U *mat.Dense14S *mat.Dense15}
16
17func AaT(matr *mat.Dense) *mat.Dense { // Multipy matrix AT*A18a := mat.Matrix(matr)19aT := a.T()20ad := mat.DenseCopyOf(a)21aTd := mat.DenseCopyOf(aT)22n1, _ := aTd.Dims()23_, m2 := ad.Dims()24output := mat.NewDense(n1, m2, nil)25fmt.Print("X: ")26fmt.Println(ad.Dims())27fmt.Print("XT: ")28fmt.Println(aTd.Dims())29// oss.SafeToXlsxMatrix(ad, "ad")30// oss.SafeToXlsxMatrix(aTd, "aTd")31output.Mul(ad, aTd)32return output33}
34func SDV(X *mat.Dense, rank int) []SDVs {35SDVsout := make([]SDVs, rank)36for i := 0; i < rank; i++ {37/*38// Это с дроблением на матрицы
39X_y, _ := X.Dims()
40kk := X.Slice(0, X_y, i, i+X_y) // Взять часть матрицы X
41kek := mat.DenseCopyOf(kk) // Преобразовать в Dense
42SDVsout[i] = SDV_single(kek) // Сохранить значение
43*/
44
45// это без дробления на матрицы46SDVsout[i] = SDV_single(X)47}48return SDVsout49}
50func SDV_single(matT *mat.Dense) SDVs {51// oss.SafeToXlsxMatrix(matT, "matT")52
53var SDVout SDVs54var svdMat mat.SVD55ok := svdMat.Factorize(matT, mat.SVDFull)56if !ok {57log.Fatal("failed to factorize A")58}59
60SDVout.V = new(mat.Dense)61SDVout.S = new(mat.Dense)62svdMat.VTo(SDVout.V)63
64svdMat.UTo(SDVout.S)65lenX_s, lenY_s := matT.Dims()66//fmt.Println(lenY_s)67valuesMat := make([]float64, lenX_s)68//fmt.Println(len(valuesMat))69svdMat.Values(valuesMat)70
71SDVout.U = mat.NewDense(lenX_s, lenY_s, nil)72for ind, val := range valuesMat {73SDVout.U.Set(ind, ind, val)74}75
76//fmt.Println(SDVout.S.Dims())77
78SDVout.U = mat.DenseCopyOf(SDVout.U.T())79
80//fmt.Println(SDVout.S.Dims())81
82return SDVout83}
84
85func makeRank(matr *mat.Dense) int {86var svd mat.SVD87ok := svd.Factorize(matr, mat.SVDFull)88if !ok {89log.Fatal("failed to factorize A")90}91rank := svd.Rank(oss.Rcond)92if rank == 0 {93log.Fatal("zero rank system")94}95return (rank)96}
97