2013-06-20 18:47:39 +02:00
|
|
|
package matrix
|
|
|
|
|
2013-12-02 13:45:10 +01:00
|
|
|
type affine interface {
|
2014-12-07 15:20:41 +01:00
|
|
|
dim() int
|
2013-06-20 18:47:39 +02:00
|
|
|
element(i, j int) float64
|
|
|
|
setElement(i, j int, element float64)
|
|
|
|
}
|
|
|
|
|
2013-12-02 13:45:10 +01:00
|
|
|
func isIdentity(matrix affine) bool {
|
2014-12-07 15:20:41 +01:00
|
|
|
dim := matrix.dim()
|
2013-06-20 18:47:39 +02:00
|
|
|
for i := 0; i < dim-1; i++ {
|
|
|
|
for j := 0; j < dim; j++ {
|
|
|
|
element := matrix.element(i, j)
|
|
|
|
if i == j && element != 1 {
|
|
|
|
return false
|
|
|
|
} else if i != j && element != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-12-02 13:45:10 +01:00
|
|
|
func mul(lhs, rhs, result affine) {
|
2014-12-07 15:20:41 +01:00
|
|
|
dim := lhs.dim()
|
|
|
|
if dim != rhs.dim() {
|
2013-06-20 18:47:39 +02:00
|
|
|
panic("diffrent-sized matrices can't be multiplied")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < dim-1; i++ {
|
|
|
|
for j := 0; j < dim; j++ {
|
|
|
|
element := float64(0)
|
|
|
|
for k := 0; k < dim-1; k++ {
|
|
|
|
element += lhs.element(i, k) *
|
|
|
|
rhs.element(k, j)
|
|
|
|
}
|
|
|
|
if j == dim-1 {
|
|
|
|
element += lhs.element(i, j)
|
|
|
|
}
|
|
|
|
result.setElement(i, j, element)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|