This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,54 @@
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, 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)
}

View file

@ -0,0 +1,89 @@
package main
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
}
func main() {
a := matrixFromRows([][]float64{
{1, 1, 1, 1},
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256},
})
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,
},
})
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:")
}

View file

@ -0,0 +1,50 @@
package main
import (
"fmt"
mat "github.com/skelterjohn/go.matrix"
)
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
}
fmt.Printf("Product of A and B:\n%v\n", p)
}