Skip to content

Commit

Permalink
matrix/mat64: remove Order field from RawMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Mar 19, 2014
1 parent c8f51ef commit 070be6b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 71 deletions.
22 changes: 4 additions & 18 deletions mat64/dense.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ func Register(b blas.Float64) { blasEngine = b }

func Registered() blas.Float64 { return blasEngine }

const BlasOrder = blas.RowMajor

var (
matrix *Dense

Expand Down Expand Up @@ -68,7 +66,6 @@ func NewDense(r, c int, mat []float64) *Dense {
mat = make([]float64, r*c)
}
return &Dense{RawMatrix{
Order: BlasOrder,
Rows: r,
Cols: c,
Stride: c,
Expand All @@ -83,12 +80,7 @@ func DenseCopyOf(a Matrix) *Dense {
return d
}

func (m *Dense) LoadRawMatrix(b RawMatrix) {
if b.Order != BlasOrder {
panic(ErrIllegalOrder)
}
m.mat = b
}
func (m *Dense) LoadRawMatrix(b RawMatrix) { m.mat = b }

func (m *Dense) RawMatrix() RawMatrix { return m.mat }

Expand Down Expand Up @@ -207,7 +199,6 @@ func (m *Dense) Reset() {
func (m *Dense) Clone(a Matrix) {
r, c := a.Dims()
mat := RawMatrix{
Order: BlasOrder,
Rows: r,
Cols: c,
Stride: c,
Expand Down Expand Up @@ -282,7 +273,6 @@ func (m *Dense) U(a Matrix) {
return
case m.isZero():
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: ac,
Stride: ac,
Expand Down Expand Up @@ -338,7 +328,6 @@ func (m *Dense) L(a Matrix) {
return
case m.isZero():
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: ac,
Stride: ac,
Expand Down Expand Up @@ -390,10 +379,9 @@ func (m *Dense) TCopy(a Matrix) {
}
if w.isZero() {
w.mat = RawMatrix{
Order: BlasOrder,
Rows: ac,
Cols: ar,
Data: use(w.mat.Data, ar*ac),
Rows: ac,
Cols: ar,
Data: use(w.mat.Data, ar*ac),
}
w.mat.Stride = ar
} else if ar != m.mat.Cols || ac != m.mat.Rows {
Expand Down Expand Up @@ -425,7 +413,6 @@ func (m *Dense) Stack(a, b Matrix) {

if m.isZero() {
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar + br,
Cols: ac,
Stride: ac,
Expand All @@ -450,7 +437,6 @@ func (m *Dense) Augment(a, b Matrix) {

if m.isZero() {
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: ac + bc,
Stride: ac + bc,
Expand Down
11 changes: 1 addition & 10 deletions mat64/dense_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func (m *Dense) Add(a, b Matrix) {

if m.isZero() {
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: ac,
Stride: ac,
Expand Down Expand Up @@ -173,7 +172,6 @@ func (m *Dense) Sub(a, b Matrix) {

if m.isZero() {
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: ac,
Stride: ac,
Expand Down Expand Up @@ -227,7 +225,6 @@ func (m *Dense) MulElem(a, b Matrix) {

if m.isZero() {
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: ac,
Stride: ac,
Expand Down Expand Up @@ -283,9 +280,6 @@ func (m *Dense) Dot(b Matrix) float64 {

if b, ok := b.(RawMatrixer); ok {
bmat := b.RawMatrix()
if m.mat.Order != BlasOrder || bmat.Order != BlasOrder {
panic(ErrIllegalOrder)
}
for jm, jb := 0, 0; jm < mr*m.mat.Stride; jm, jb = jm+m.mat.Stride, jb+bmat.Stride {
for i, v := range m.mat.Data[jm : jm+mc] {
d += v * bmat.Data[i+jb]
Expand Down Expand Up @@ -326,7 +320,6 @@ func (m *Dense) Mul(a, b Matrix) {
}
if w.isZero() {
w.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: bc,
Stride: bc,
Expand All @@ -343,7 +336,7 @@ func (m *Dense) Mul(a, b Matrix) {
panic(ErrNoEngine)
}
blasEngine.Dgemm(
BlasOrder,
blas.RowMajor,
blas.NoTrans, blas.NoTrans,
ar, bc, ac,
1.,
Expand Down Expand Up @@ -394,7 +387,6 @@ func (m *Dense) Scale(f float64, a Matrix) {

if m.isZero() {
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: ac,
Stride: ac,
Expand Down Expand Up @@ -437,7 +429,6 @@ func (m *Dense) Apply(f ApplyFunc, a Matrix) {

if m.isZero() {
m.mat = RawMatrix{
Order: BlasOrder,
Rows: ar,
Cols: ac,
Stride: ac,
Expand Down
21 changes: 8 additions & 13 deletions mat64/dense_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package mat64
import (
"github.com/gonum/floats"

check "launchpad.net/gocheck"
"math/rand"
"testing"
check "launchpad.net/gocheck"
)

func (s *S) TestNewDense(c *check.C) {
Expand All @@ -30,8 +30,7 @@ func (s *S) TestNewDense(c *check.C) {
0, 0,
0,
&Dense{RawMatrix{
Order: BlasOrder,
Rows: 3, Cols: 3,
Rows: 3, Cols: 3,
Stride: 3,
Data: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0},
}},
Expand All @@ -46,8 +45,7 @@ func (s *S) TestNewDense(c *check.C) {
1, 1,
3,
&Dense{RawMatrix{
Order: BlasOrder,
Rows: 3, Cols: 3,
Rows: 3, Cols: 3,
Stride: 3,
Data: []float64{1, 1, 1, 1, 1, 1, 1, 1, 1},
}},
Expand All @@ -62,8 +60,7 @@ func (s *S) TestNewDense(c *check.C) {
0, 1,
1.7320508075688772,
&Dense{RawMatrix{
Order: BlasOrder,
Rows: 3, Cols: 3,
Rows: 3, Cols: 3,
Stride: 3,
Data: []float64{1, 0, 0, 0, 1, 0, 0, 0, 1},
}},
Expand All @@ -77,7 +74,7 @@ func (s *S) TestNewDense(c *check.C) {
3, 3,
-1, 0,
1.7320508075688772,
&Dense{RawMatrix{Order: BlasOrder,
&Dense{RawMatrix{
Rows: 3, Cols: 3,
Stride: 3,
Data: []float64{-1, 0, 0, 0, -1, 0, 0, 0, -1},
Expand All @@ -91,7 +88,7 @@ func (s *S) TestNewDense(c *check.C) {
2, 3,
1, 6,
9.539392014169456,
&Dense{RawMatrix{Order: BlasOrder,
&Dense{RawMatrix{
Rows: 2, Cols: 3,
Stride: 3,
Data: []float64{1, 2, 3, 4, 5, 6},
Expand All @@ -107,8 +104,7 @@ func (s *S) TestNewDense(c *check.C) {
1, 6,
9.539392014169456,
&Dense{RawMatrix{
Order: BlasOrder,
Rows: 3, Cols: 2,
Rows: 3, Cols: 2,
Stride: 2,
Data: []float64{1, 2, 3, 4, 5, 6},
}},
Expand Down Expand Up @@ -433,8 +429,7 @@ func randDense(size int, rho float64, rnd func() float64) (*Dense, error) {
return nil, ErrZeroLength
}
d := &Dense{RawMatrix{
Order: BlasOrder,
Rows: size, Cols: size, Stride: size,
Rows: size, Cols: size, Stride: size,
Data: make([]float64, size*size),
}}
for i := 0; i < size; i++ {
Expand Down
16 changes: 8 additions & 8 deletions mat64/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package mat64

import (
"fmt"
"math"
check "launchpad.net/gocheck"
"math"
)

type fm struct {
Expand Down Expand Up @@ -39,7 +39,7 @@ func (s *S) TestFormat(c *check.C) {
[]rp{
{"%v", "⎡0 0 0⎤\n⎢0 0 0⎥\n⎣0 0 0⎦"},
{"%#f", "⎡. . .⎤\n⎢. . .⎥\n⎣. . .⎦"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Order:101, Rows:3, Cols:3, Stride:3, Data:[]float64{0, 0, 0, 0, 0, 0, 0, 0, 0}}}"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Rows:3, Cols:3, Stride:3, Data:[]float64{0, 0, 0, 0, 0, 0, 0, 0, 0}}}"},
{"%s", "%!s(*mat64.Dense=Dims(3, 3))"},
},
},
Expand All @@ -48,31 +48,31 @@ func (s *S) TestFormat(c *check.C) {
[]rp{
{"%v", "⎡1 1 1⎤\n⎢1 1 1⎥\n⎣1 1 1⎦"},
{"%#f", "⎡1 1 1⎤\n⎢1 1 1⎥\n⎣1 1 1⎦"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Order:101, Rows:3, Cols:3, Stride:3, Data:[]float64{1, 1, 1, 1, 1, 1, 1, 1, 1}}}"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Rows:3, Cols:3, Stride:3, Data:[]float64{1, 1, 1, 1, 1, 1, 1, 1, 1}}}"},
},
},
{
fm{Matrix: NewDense(3, 3, []float64{1, 0, 0, 0, 1, 0, 0, 0, 1})},
[]rp{
{"%v", "⎡1 0 0⎤\n⎢0 1 0⎥\n⎣0 0 1⎦"},
{"%#f", "⎡1 . .⎤\n⎢. 1 .⎥\n⎣. . 1⎦"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Order:101, Rows:3, Cols:3, Stride:3, Data:[]float64{1, 0, 0, 0, 1, 0, 0, 0, 1}}}"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Rows:3, Cols:3, Stride:3, Data:[]float64{1, 0, 0, 0, 1, 0, 0, 0, 1}}}"},
},
},
{
fm{Matrix: NewDense(2, 3, []float64{1, 2, 3, 4, 5, 6})},
[]rp{
{"%v", "⎡1 2 3⎤\n⎣4 5 6⎦"},
{"%#f", "⎡1 2 3⎤\n⎣4 5 6⎦"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Order:101, Rows:2, Cols:3, Stride:3, Data:[]float64{1, 2, 3, 4, 5, 6}}}"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Rows:2, Cols:3, Stride:3, Data:[]float64{1, 2, 3, 4, 5, 6}}}"},
},
},
{
fm{Matrix: NewDense(3, 2, []float64{1, 2, 3, 4, 5, 6})},
[]rp{
{"%v", "⎡1 2⎤\n⎢3 4⎥\n⎣5 6⎦"},
{"%#f", "⎡1 2⎤\n⎢3 4⎥\n⎣5 6⎦"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Order:101, Rows:3, Cols:2, Stride:2, Data:[]float64{1, 2, 3, 4, 5, 6}}}"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Rows:3, Cols:2, Stride:2, Data:[]float64{1, 2, 3, 4, 5, 6}}}"},
},
},
{
Expand All @@ -85,7 +85,7 @@ func (s *S) TestFormat(c *check.C) {
{"%v", "⎡ 0 1 1.4142135623730951⎤\n⎣1.7320508075688772 2 2.23606797749979⎦"},
{"%.2f", "⎡0.00 1.00 1.41⎤\n⎣1.73 2.00 2.24⎦"},
{"%#f", "⎡ . 1 1.4142135623730951⎤\n⎣1.7320508075688772 2 2.23606797749979⎦"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Order:101, Rows:2, Cols:3, Stride:3, Data:[]float64{0, 1, 1.4142135623730951, 1.7320508075688772, 2, 2.23606797749979}}}"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Rows:2, Cols:3, Stride:3, Data:[]float64{0, 1, 1.4142135623730951, 1.7320508075688772, 2, 2.23606797749979}}}"},
},
},
{
Expand All @@ -98,7 +98,7 @@ func (s *S) TestFormat(c *check.C) {
{"%v", "⎡ 0 1⎤\n⎢1.4142135623730951 1.7320508075688772⎥\n⎣ 2 2.23606797749979⎦"},
{"%.2f", "⎡0.00 1.00⎤\n⎢1.41 1.73⎥\n⎣2.00 2.24⎦"},
{"%#f", "⎡ . 1⎤\n⎢1.4142135623730951 1.7320508075688772⎥\n⎣ 2 2.23606797749979⎦"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Order:101, Rows:3, Cols:2, Stride:2, Data:[]float64{0, 1, 1.4142135623730951, 1.7320508075688772, 2, 2.23606797749979}}}"},
{"%#v", "&mat64.Dense{mat:mat64.RawMatrix{Rows:3, Cols:2, Stride:2, Data:[]float64{0, 1, 1.4142135623730951, 1.7320508075688772, 2, 2.23606797749979}}}"},
},
},
{
Expand Down
22 changes: 3 additions & 19 deletions mat64/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
// If the matrix dimensions do not match the result, the method must panic.
package mat64

import (
"github.com/gonum/blas"
)

// Matrix is the basic matrix interface type.
type Matrix interface {
// Dims returns the dimensions of a Matrix.
Expand Down Expand Up @@ -254,7 +250,6 @@ type BandWidther interface {

// RawMatrix represents a cblas native representation of a matrix.
type RawMatrix struct {
Order blas.Order
Rows, Cols int
Stride int
Data []float64
Expand All @@ -270,20 +265,10 @@ func (b RawMatrix) Matrix(c Mutable) {
if rows, cols := c.Dims(); rows != b.Rows || cols != b.Cols {
panic(ErrShape)
}
if b.Order == blas.ColMajor {
for col := 0; col < b.Cols; col++ {
for row, v := range b.Data[col*b.Stride : col*b.Stride+b.Rows] {
c.Set(row, col, v)
}
}
} else if b.Order == blas.RowMajor {
for row := 0; row < b.Rows; row++ {
for col, v := range b.Data[row*b.Stride : row*b.Stride+b.Cols] {
c.Set(row, col, v)
}
for row := 0; row < b.Rows; row++ {
for col, v := range b.Data[row*b.Stride : row*b.Stride+b.Cols] {
c.Set(row, col, v)
}
} else {
panic("matrix: illegal order")
}
}

Expand Down Expand Up @@ -396,7 +381,6 @@ const (
ErrShape = Error("mat64: dimension mismatch")
ErrIllegalStride = Error("mat64: illegal stride")
ErrPivot = Error("mat64: malformed pivot list")
ErrIllegalOrder = Error("mat64: illegal order")
ErrNoEngine = Error("mat64: no blas engine registered: call Register()")
)

Expand Down
2 changes: 1 addition & 1 deletion mat64/mul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestMul(t *testing.T) {

// Get correct matrix multiply answer from Dgemm
blasEngine.Dgemm(
BlasOrder,
blas.RowMajor,
blas.NoTrans, blas.NoTrans,
ar, bc, ac,
1.,
Expand Down
6 changes: 4 additions & 2 deletions mat64/vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ func (m *Vec) Mul(a, b Matrix) {

if a, ok := a.(RawMatrixer); ok {
amat := a.RawMatrix()
blasEngine.Dgemv(BlasOrder,
blasEngine.Dgemv(
blas.RowMajor,
blas.NoTrans,
ar, ac,
1.,
amat.Data, amat.Stride,
bv, 1,
0.,
w, 1)
w, 1,
)
*m = w
return
}
Expand Down

0 comments on commit 070be6b

Please sign in to comment.