Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
23
Task/Matrix-multiplication/Go/matrix-multiplication-1.go
Normal file
23
Task/Matrix-multiplication/Go/matrix-multiplication-1.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gonum.org/v1/gonum/mat"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := mat.NewDense(2, 4, []float64{
|
||||
1, 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
})
|
||||
b := mat.NewDense(4, 3, []float64{
|
||||
1, 2, 3,
|
||||
4, 5, 6,
|
||||
7, 8, 9,
|
||||
10, 11, 12,
|
||||
})
|
||||
var m mat.Dense
|
||||
m.Mul(a, b)
|
||||
fmt.Println(mat.Formatted(&m))
|
||||
}
|
||||
28
Task/Matrix-multiplication/Go/matrix-multiplication-2.go
Normal file
28
Task/Matrix-multiplication/Go/matrix-multiplication-2.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
mat "github.com/skelterjohn/go.matrix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := mat.MakeDenseMatrixStacked([][]float64{
|
||||
{1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
})
|
||||
b := mat.MakeDenseMatrixStacked([][]float64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
{10, 11, 12},
|
||||
})
|
||||
fmt.Printf("Matrix A:\n%v\n", a)
|
||||
fmt.Printf("Matrix B:\n%v\n", b)
|
||||
p, err := a.TimesDense(b)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Product of A and B:\n%v\n", p)
|
||||
}
|
||||
60
Task/Matrix-multiplication/Go/matrix-multiplication-3.go
Normal file
60
Task/Matrix-multiplication/Go/matrix-multiplication-3.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Value float64
|
||||
type Matrix [][]Value
|
||||
|
||||
func Multiply(m1, m2 Matrix) (m3 Matrix, ok bool) {
|
||||
rows, cols, extra := len(m1), len(m2[0]), len(m2)
|
||||
if len(m1[0]) != extra {
|
||||
return nil, false
|
||||
}
|
||||
m3 = make(Matrix, rows)
|
||||
for i := 0; i < rows; i++ {
|
||||
m3[i] = make([]Value, cols)
|
||||
for j := 0; j < cols; j++ {
|
||||
for k := 0; k < extra; k++ {
|
||||
m3[i][j] += m1[i][k] * m2[k][j]
|
||||
}
|
||||
}
|
||||
}
|
||||
return m3, true
|
||||
}
|
||||
|
||||
func (m Matrix) String() string {
|
||||
rows := len(m)
|
||||
cols := len(m[0])
|
||||
out := "["
|
||||
for r := 0; r < rows; r++ {
|
||||
if r > 0 {
|
||||
out += ",\n "
|
||||
}
|
||||
out += "[ "
|
||||
for c := 0; c < cols; c++ {
|
||||
if c > 0 {
|
||||
out += ", "
|
||||
}
|
||||
out += fmt.Sprintf("%7.3f", m[r][c])
|
||||
}
|
||||
out += " ]"
|
||||
}
|
||||
out += "]"
|
||||
return out
|
||||
}
|
||||
|
||||
func main() {
|
||||
A := Matrix{[]Value{1, 2, 3, 4},
|
||||
[]Value{5, 6, 7, 8}}
|
||||
B := Matrix{[]Value{1, 2, 3},
|
||||
[]Value{4, 5, 6},
|
||||
[]Value{7, 8, 9},
|
||||
[]Value{10, 11, 12}}
|
||||
P, ok := Multiply(A, B)
|
||||
if !ok {
|
||||
panic("Invalid dimensions")
|
||||
}
|
||||
fmt.Printf("Matrix A:\n%s\n\n", A)
|
||||
fmt.Printf("Matrix B:\n%s\n\n", B)
|
||||
fmt.Printf("Product of A and B:\n%s\n\n", P)
|
||||
}
|
||||
56
Task/Matrix-multiplication/Go/matrix-multiplication-4.go
Normal file
56
Task/Matrix-multiplication/Go/matrix-multiplication-4.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type matrix struct {
|
||||
stride int
|
||||
ele []float64
|
||||
}
|
||||
|
||||
func (m *matrix) print(heading string) {
|
||||
if heading > "" {
|
||||
fmt.Print("\n", heading, "\n")
|
||||
}
|
||||
for e := 0; e < len(m.ele); e += m.stride {
|
||||
fmt.Printf("%8.3f ", m.ele[e:e+m.stride])
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
func (m1 *matrix) multiply(m2 *matrix) (m3 *matrix, ok bool) {
|
||||
if m1.stride*m2.stride != len(m2.ele) {
|
||||
return nil, false
|
||||
}
|
||||
m3 = &matrix{m2.stride, make([]float64, (len(m1.ele)/m1.stride)*m2.stride)}
|
||||
for m1c0, m3x := 0, 0; m1c0 < len(m1.ele); m1c0 += m1.stride {
|
||||
for m2r0 := 0; m2r0 < m2.stride; m2r0++ {
|
||||
for m1x, m2x := m1c0, m2r0; m2x < len(m2.ele); m2x += m2.stride {
|
||||
m3.ele[m3x] += m1.ele[m1x] * m2.ele[m2x]
|
||||
m1x++
|
||||
}
|
||||
m3x++
|
||||
}
|
||||
}
|
||||
return m3, true
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := matrix{4, []float64{
|
||||
1, 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
}}
|
||||
b := matrix{3, []float64{
|
||||
1, 2, 3,
|
||||
4, 5, 6,
|
||||
7, 8, 9,
|
||||
10, 11, 12,
|
||||
}}
|
||||
p, ok := a.multiply(&b)
|
||||
a.print("Matrix A:")
|
||||
b.print("Matrix B:")
|
||||
if !ok {
|
||||
fmt.Println("not conformable for matrix multiplication")
|
||||
return
|
||||
}
|
||||
p.print("Product of A and B:")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue