Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
17
Task/Matrix-transposition/Go/matrix-transposition-1.go
Normal file
17
Task/Matrix-transposition/Go/matrix-transposition-1.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gonum.org/v1/gonum/mat"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := mat.NewDense(2, 3, []float64{
|
||||
1, 2, 3,
|
||||
4, 5, 6,
|
||||
})
|
||||
fmt.Println(mat.Formatted(m))
|
||||
fmt.Println()
|
||||
fmt.Println(mat.Formatted(m.T()))
|
||||
}
|
||||
19
Task/Matrix-transposition/Go/matrix-transposition-2.go
Normal file
19
Task/Matrix-transposition/Go/matrix-transposition-2.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
mat "github.com/skelterjohn/go.matrix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := mat.MakeDenseMatrixStacked([][]float64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
})
|
||||
fmt.Println("original:")
|
||||
fmt.Println(m)
|
||||
m = m.Transpose()
|
||||
fmt.Println("transpose:")
|
||||
fmt.Println(m)
|
||||
}
|
||||
35
Task/Matrix-transposition/Go/matrix-transposition-3.go
Normal file
35
Task/Matrix-transposition/Go/matrix-transposition-3.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type row []float64
|
||||
type matrix []row
|
||||
|
||||
func main() {
|
||||
m := matrix{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
}
|
||||
printMatrix(m)
|
||||
t := transpose(m)
|
||||
printMatrix(t)
|
||||
}
|
||||
|
||||
func printMatrix(m matrix) {
|
||||
for _, s := range m {
|
||||
fmt.Println(s)
|
||||
}
|
||||
}
|
||||
|
||||
func transpose(m matrix) matrix {
|
||||
r := make(matrix, len(m[0]))
|
||||
for x, _ := range r {
|
||||
r[x] = make(row, len(m))
|
||||
}
|
||||
for y, s := range m {
|
||||
for x, e := range s {
|
||||
r[x][y] = e
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
51
Task/Matrix-transposition/Go/matrix-transposition-4.go
Normal file
51
Task/Matrix-transposition/Go/matrix-transposition-4.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type matrix struct {
|
||||
ele []float64
|
||||
stride int
|
||||
}
|
||||
|
||||
// construct new matrix from slice of slices
|
||||
func matrixFromRows(rows [][]float64) *matrix {
|
||||
if len(rows) == 0 {
|
||||
return &matrix{nil, 0}
|
||||
}
|
||||
m := &matrix{make([]float64, len(rows)*len(rows[0])), len(rows[0])}
|
||||
for rx, row := range rows {
|
||||
copy(m.ele[rx*m.stride:(rx+1)*m.stride], row)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := matrixFromRows([][]float64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
})
|
||||
m.print("original:")
|
||||
m.transpose().print("transpose:")
|
||||
}
|
||||
|
||||
func (m *matrix) print(heading string) {
|
||||
if heading > "" {
|
||||
fmt.Print("\n", heading, "\n")
|
||||
}
|
||||
for e := 0; e < len(m.ele); e += m.stride {
|
||||
fmt.Println(m.ele[e : e+m.stride])
|
||||
}
|
||||
}
|
||||
|
||||
func (m *matrix) transpose() *matrix {
|
||||
r := &matrix{make([]float64, len(m.ele)), len(m.ele) / m.stride}
|
||||
rx := 0
|
||||
for _, e := range m.ele {
|
||||
r.ele[rx] = e
|
||||
rx += r.stride
|
||||
if rx >= len(r.ele) {
|
||||
rx -= len(r.ele) - 1
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
61
Task/Matrix-transposition/Go/matrix-transposition-5.go
Normal file
61
Task/Matrix-transposition/Go/matrix-transposition-5.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type matrix struct {
|
||||
stride int
|
||||
ele []float64
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := matrix{3, []float64{
|
||||
1, 2, 3,
|
||||
4, 5, 6,
|
||||
}}
|
||||
m.print("original:")
|
||||
m.transposeInPlace()
|
||||
m.print("transpose:")
|
||||
}
|
||||
|
||||
func (m *matrix) print(heading string) {
|
||||
if heading > "" {
|
||||
fmt.Print("\n", heading, "\n")
|
||||
}
|
||||
for e := 0; e < len(m.ele); e += m.stride {
|
||||
fmt.Println(m.ele[e : e+m.stride])
|
||||
}
|
||||
}
|
||||
|
||||
func (m *matrix) transposeInPlace() {
|
||||
h := len(m.ele) / m.stride
|
||||
for start := range m.ele {
|
||||
next := start
|
||||
i := 0
|
||||
for {
|
||||
i++
|
||||
next = (next%h)*m.stride + next/h
|
||||
if next <= start {
|
||||
break
|
||||
}
|
||||
}
|
||||
if next < start || i == 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
next = start
|
||||
tmp := m.ele[next]
|
||||
for {
|
||||
i = (next%h)*m.stride + next/h
|
||||
if i == start {
|
||||
m.ele[next] = tmp
|
||||
} else {
|
||||
m.ele[next] = m.ele[i]
|
||||
}
|
||||
next = i
|
||||
if next <= start {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
m.stride = h
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue