15
"gonum.org/v1/gonum/mat"
19
func Sum2(a mat.Dense) []float64 {
22
for i := 0; i < r; i++ {
23
output = append(output, mat.Sum(a.RowView(i)))
29
func Max(arr []float64) float64 {
34
for i := 0; i < len(arr); i++ {
42
func ATa(matr mat.Dense) mat.Dense {
43
a := mat.Matrix(&matr)
45
ad := mat.DenseCopyOf(a)
46
aTd := mat.DenseCopyOf(aT)
49
output := mat.NewDense(n1, m2, nil)
54
func MulVecToVec(a, b mat.Vector) float64 {
55
a_matrix := mat.Matrix(a).T()
56
var varVecDense mat.VecDense
57
varVecDense.MulVec(a_matrix, b)
59
return varVecDense.AtVec(0)
63
func AbsVector(vect mat.VecDense) mat.VecDense {
64
for i := 0; i < vect.Len(); i++ {
65
vect.SetVec(i, math.Abs(vect.AtVec(i)))
85
func Averge(array []float64) float64 {
87
for _, val := range array {
90
return sum / float64(len(array))
94
func Subdiagonal(m mat.Dense, k int) ([]float64, error) {
95
var outputArray []float64
98
for i := 0; i < r_m && i < c_m; i++ {
99
outputArray = append(outputArray, m.At(i, i))
103
return nil, errors.New("k > matrix ROW")
105
for i := 0; i-k < r_m && i < c_m; i++ {
106
outputArray = append(outputArray, m.At(i-k, i))
110
return nil, errors.New("k > matrix COL")
112
for i := 0; i+k < c_m && i < r_m; i++ {
113
outputArray = append(outputArray, m.At(i, i+k))
116
return outputArray, nil
120
func ColDense(m mat.Dense, ind int) []float64 {
122
outputArray := make([]float64, row)
123
for i := 0; i < row; i++ {
124
outputArray[i] = m.At(i, ind)
130
func RowDense(m mat.Dense, ind int) []float64 {
132
outputArray := make([]float64, col)
133
for j := 0; j < col; j++ {
134
outputArray[j] = m.At(ind, j)
139
func Diag(mat mat.Dense, R int) []float64 {
140
ret := make([]float64, R)
141
for ind := range ret {
142
ret[ind] = mat.At(ind, ind)
147
func Make_diag_danse(arr []float64) mat.Dense {
148
lensOfArray := len(arr)
149
dens := mat.NewDense(lensOfArray, lensOfArray, nil)
150
for i := 0; i < len(arr); i++ {
151
dens.Set(i, i, arr[i])
156
func VecDense_in_float64(vec mat.VecDense) []float64 {
157
leng, _ := vec.Dims()
158
output := make([]float64, leng)
159
for ind := range output {
160
output[ind] = vec.AtVec(ind)
164
func Vec_in_float64(vec mat.Vector) []float64 {
165
leng, _ := vec.Dims()
166
output := make([]float64, leng)
167
for ind := range output {
168
output[ind] = vec.AtVec(ind)
174
func AsSymDense(m *mat.Dense) (*mat.SymDense, error) {
177
return nil, errors.New("matrix must be square")
180
vals := make([]float64, r*c)
182
for i := 0; i < r; i++ {
183
for j := 0; j < c; j++ {
184
if mT.At(i, j) != m.At(i, j) {
185
return nil, errors.New("matrix is not symmetric")
187
vals[idx] = m.At(i, j)
191
return mat.NewSymDense(r, vals), nil
193
func RealyPrint(matr *mat.Dense, name string) {
194
fmatr := mat.Formatted(matr, mat.Prefix(string(strings.Repeat(" ", 2+len(name)))), mat.Squeeze())
195
fmt.Printf(name+" =%.3v\n", fmatr)
198
func RealyPrint2(matr mat.Dense, name string) {
199
fmatr := mat.Formatted(&matr, mat.Prefix(string(strings.Repeat(" ", 2+len(name)))), mat.Squeeze())
200
fmt.Printf(name+" =%.3v\n", fmatr)
202
func RealyPrintMatrix(matr mat.Matrix, name string) {
203
fmatr := mat.Formatted(matr, mat.Prefix(string(strings.Repeat(" ", 2+len(name)))), mat.Squeeze())
204
fmt.Printf(name+" =%.3v\n", fmatr)
208
func Median(dataVect mat.VecDense) float64 {
209
dataVect = SortVecDense(dataVect)
215
median = (dataVect.AtVec(l/2-1) + dataVect.AtVec(l/2)) / 2
217
median = dataVect.AtVec(l / 2)
221
func Median_floatArr(dataVect []float64) float64 {
223
sort.Float64s(dataVect)
229
median = (dataVect[l/2-1] + dataVect[l/2]) / 2
231
median = dataVect[l/2]
237
func SortVecDense(dataVect mat.VecDense) mat.VecDense {
238
dataVectLength := dataVect.Len()
239
for i := 1; i < dataVectLength; i++ {
241
for j >= 0 && dataVect.AtVec(j) > dataVect.AtVec(j+1) {
242
vspom := dataVect.AtVec(j)
243
dataVect.SetVec(j, dataVect.AtVec(j+1))
244
dataVect.SetVec(j, vspom)
251
func Vec_in_ArrFloat(a mat.Vector) []float64 {
252
b := make([]float64, a.Len())
253
for i := 0; i < a.Len(); i++ {
260
func Diag_of_Dense(matr mat.Dense, k int) mat.VecDense {
264
matr2 = matr.Slice(0, r, k, c)
266
matr2 = matr.Slice(-k, r, 0, c)
268
matr2 = matr.Slice(0, r, 0, c)
271
vect := mat.NewVecDense(mat.DenseCopyOf(matr2).DiagView().Diag(), nil)
273
for i := 0; i < vect.Len(); i++ {
274
vect.SetVec(i, matr2.At(i, i))
280
func Vector_DivElemVec(a mat.Matrix, b mat.Vector) mat.VecDense {
281
var div_vectors mat.VecDense
282
var div_Dense mat.Dense
283
div_Dense.CloneFrom(a)
285
asd := div_Dense.ColView(0)
287
div_vectors.DivElemVec(asd, b)
292
func EditLastRow(matr mat.Dense) mat.Dense {
295
for i := 0; i < c; i++ {
296
matr.Set(r-1, i, matr.At(r-2, i))