Skip to content

Commit

Permalink
Add Transpose method
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Bentley committed Feb 15, 2017
1 parent bf77fd2 commit 1c0015b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 60 deletions.
12 changes: 12 additions & 0 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ func (m Matrix) Scale(scalar float64) Matrix {
return r
}

func (m Matrix) Transpose() Matrix {
r := newZeroMatrix(m.Columns(), m.Rows())

for mRows := 0; mRows < m.Rows(); mRows++ {
for mCols := 0; mCols < m.Columns(); mCols++ {
r[mCols][mRows] = m[mRows][mCols]
}
}

return r
}

// Print writes the matrix to stdout
func (m Matrix) Print() {
// print the matrix
Expand Down
129 changes: 69 additions & 60 deletions matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,7 @@ import (
"testing"
)

var valuePositionsForMatrix1 = [][]int{
{0, 0, 1},
{0, 1, 2},
{0, 2, 3},
{1, 0, 2},
{1, 1, 4},
{1, 2, 5},
}

var valuePositionsForMatrix1ByMatrix2 = [][]int{
{0, 0, 19},
{0, 1, 21},
{1, 0, 34},
{1, 1, 37},
}

var valuePositionsForMatrix1AddMatrix3 = [][]int{
{0, 0, 4},
{0, 1, 7},
{0, 2, 4},
{1, 0, 6},
{1, 1, 8},
{1, 2, 8},
}

var valuePositionsForMatrix3SubtractMatrix1 = [][]int{
{0, 0, 2},
{0, 1, 3},
{0, 2, -2},
{1, 0, 2},
{1, 1, 0},
{1, 2, -2},
}

var valuePositionsForMatrix1ScaledBy4 = [][]int{
{0, 0, 4},
{0, 1, 8},
{0, 2, 12},
{1, 0, 8},
{1, 1, 16},
{1, 2, 20},
}

// define test matrices
var matrix1 = Matrix{
{1, 2, 3},
{2, 4, 5},
Expand Down Expand Up @@ -81,7 +39,16 @@ func TestDimensions(t *testing.T) {
}

func TestValueAtPosition(t *testing.T) {
for _, v := range valuePositionsForMatrix1 {
e := [][]int{
{0, 0, 1},
{0, 1, 2},
{0, 2, 3},
{1, 0, 2},
{1, 1, 4},
{1, 2, 5},
}

for _, v := range e {
val, err := matrix1.Value(v[0], v[1])
assert.Equal(t, float64(v[2]), val, fmt.Sprintf("Value at position (%v, %v) should be %v", v[0], v[1], v[2]))
assert.Equal(t, nil, err, "error should be nil")
Expand All @@ -104,10 +71,17 @@ func TestMultiply(t *testing.T) {
m, err := matrix1.Multiply(matrix2)
assert.Equal(t, nil, err, "error should be nil")

for _, v := range valuePositionsForMatrix1ByMatrix2 {
val, err := m.Value(v[0], v[1])
assert.Equal(t, float64(v[2]), val, fmt.Sprintf("Value at position (%v, %v) should be %v", v[0], v[1], v[2]))
assert.Equal(t, nil, err, "error should be nil")
e := Matrix{
{19, 21},
{34, 37},
}

for row := range e {
for col := range e[row] {
val, err := m.Value(row, col)
assert.Equal(t, e[row][col], val, fmt.Sprintf("Value at position (%v, %v) should be %v", row, col, e[row][col]))
assert.Equal(t, nil, err, "error should be nil")
}
}
}

Expand All @@ -118,11 +92,17 @@ func TestMultiplyIncompatible(t *testing.T) {

func TestAdd(t *testing.T) {
m, _ := matrix1.Add(matrix3)
e := Matrix{
{4, 7, 4},
{6, 8, 8},
}

for _, v := range valuePositionsForMatrix1AddMatrix3 {
val, err := m.Value(v[0], v[1])
assert.Equal(t, float64(v[2]), val, fmt.Sprintf("Value at position (%v, %v) should be %v", v[0], v[1], v[2]))
assert.Equal(t, nil, err, "error should be nil")
for row := range e {
for col := range e[row] {
val, err := m.Value(row, col)
assert.Equal(t, e[row][col], val, fmt.Sprintf("Value at position (%v, %v) should be %v", row, col, e[row][col]))
assert.Equal(t, nil, err, "error should be nil")
}
}
}

Expand All @@ -133,11 +113,17 @@ func TestAddIncompatible(t *testing.T) {

func TestSubtract(t *testing.T) {
m, _ := matrix3.Subtract(matrix1)
e := Matrix{
{2, 3, -2},
{2, 0, -2},
}

for _, v := range valuePositionsForMatrix3SubtractMatrix1 {
val, err := m.Value(v[0], v[1])
assert.Equal(t, float64(v[2]), val, fmt.Sprintf("Value at position (%v, %v) should be %v", v[0], v[1], v[2]))
assert.Equal(t, nil, err, "error should be nil")
for row := range e {
for col := range e[row] {
val, err := m.Value(row, col)
assert.Equal(t, e[row][col], val, fmt.Sprintf("Value at position (%v, %v) should be %v", row, col, e[row][col]))
assert.Equal(t, nil, err, "error should be nil")
}
}
}

Expand All @@ -148,10 +134,33 @@ func TestSubtractIncompatible(t *testing.T) {

func TestScale(t *testing.T) {
m := matrix1.Scale(4)
e := Matrix{
{4, 8, 12},
{8, 16, 20},
}

for _, v := range valuePositionsForMatrix1ScaledBy4 {
val, err := m.Value(v[0], v[1])
assert.Equal(t, float64(v[2]), val, fmt.Sprintf("Value at position (%v, %v) should be %v", v[0], v[1], v[2]))
assert.Equal(t, nil, err, "error should be nil")
for row := range e {
for col := range e[row] {
val, err := m.Value(row, col)
assert.Equal(t, e[row][col], val, fmt.Sprintf("Value at position (%v, %v) should be %v", row, col, e[row][col]))
assert.Equal(t, nil, err, "error should be nil")
}
}
}

func TestTranspose(t *testing.T) {
m := matrix1.Transpose()
e := Matrix{
{1, 2},
{2, 4},
{3, 5},
}

for row := range e {
for col := range e[row] {
val, err := m.Value(row, col)
assert.Equal(t, e[row][col], val, fmt.Sprintf("Value at position (%v, %v) should be %v", row, col, e[row][col]))
assert.Equal(t, nil, err, "error should be nil")
}
}
}

0 comments on commit 1c0015b

Please sign in to comment.