Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,54 +1,23 @@
package main
import "fmt"
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
}
"github.com/gonum/matrix/mat64"
)
func main() {
A := Matrix{[]Value{1, 1, 1, 1},
[]Value{2, 4, 8, 16},
[]Value{3, 9, 27, 81},
[]Value{4, 16, 64, 256}}
B := Matrix{[]Value{ 4.0 , -3.0 , 4.0/3, -1.0/4 },
[]Value{-13.0/3, 19.0/4, -7.0/3, 11.0/24},
[]Value{ 3.0/2, -2.0 , 7.0/6, -1.0/4 },
[]Value{ -1.0/6, 1.0/4, -1.0/6, 1.0/24}}
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)
a := mat64.NewDense(2, 4, []float64{
1, 2, 3, 4,
5, 6, 7, 8,
})
b := mat64.NewDense(4, 3, []float64{
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12,
})
var m mat64.Dense
m.Mul(a, b)
fmt.Println(mat64.Formatted(&m))
}

View file

@ -1,89 +1,28 @@
package main
import "fmt"
import (
"fmt"
type matrix struct {
ele []float64
stride int
}
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 (m *matrix) print(heading string) {
if heading > "" {
fmt.Print("\n", heading, "\n")
}
for e := 0; e < len(m.ele); e += m.stride {
fmt.Printf("%6.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{make([]float64, (len(m1.ele)/m1.stride)*m2.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
}
mat "github.com/skelterjohn/go.matrix"
)
func main() {
a := matrixFromRows([][]float64{
{1, 1, 1, 1},
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256},
a := mat.MakeDenseMatrixStacked([][]float64{
{1, 2, 3, 4},
{5, 6, 7, 8},
})
b := matrixFromRows([][]float64{
{
4,
-3,
4. / 3,
-1. / 4,
},
{
-13. / 3,
19. / 4,
-7. / 3,
11. / 24,
},
{
3. / 2,
-2,
7. / 6,
-1. / 4,
},
{
-1. / 6,
1. / 4,
-1. / 6,
1. / 24,
},
b := mat.MakeDenseMatrixStacked([][]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")
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
}
p.print("Product of A and B:")
fmt.Printf("Product of A and B:\n%v\n", p)
}

View file

@ -1,50 +1,60 @@
package main
import (
"fmt"
import "fmt"
mat "github.com/skelterjohn/go.matrix"
)
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 := mat.MakeDenseMatrixStacked([][]float64{
{1, 1, 1, 1},
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256},
})
b := mat.MakeDenseMatrixStacked([][]float64{
{
4,
-3,
4. / 3,
-1. / 4,
},
{
-13. / 3,
19. / 4,
-7. / 3,
11. / 24,
},
{
3. / 2,
-2,
7. / 6,
-1. / 4,
},
{
-1. / 6,
1. / 4,
-1. / 6,
1. / 24,
},
})
p, err := a.TimesDense(b)
fmt.Printf("Matrix A:\n%v\n", a)
fmt.Printf("Matrix B:\n%v\n", b)
if err != nil {
fmt.Println(err)
return
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("Product of A and B:\n%v\n", p)
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)
}

View 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:")
}