forked from qri-io/matlab
-
Notifications
You must be signed in to change notification settings - Fork 3
/
matrix.go
102 lines (92 loc) · 2.24 KB
/
matrix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package matlab
import (
"unicode/utf16"
)
type Matrix struct {
Name string
Dimension []int32 // at least length 2
flags Flags
Class mxClass
value []interface{}
}
// hint to the compiler
var _ Element = &Matrix{}
func (m *Matrix) Type() DataType {
return DTmiMATRIX
}
func (m *Matrix) Value() []interface{} {
return m.value
}
func (m *Matrix) GetAtLocation(i int) interface{} {
// boundaries check
max := 1
for _, x := range m.Dimension {
max *= int(x)
}
if i >= max {
return nil
}
return m.value[i]
}
// IntArray is a convenience method to extract the matrix value as []int64. Warning: It panics if the matlab class
// is not an integer type
func (m *Matrix) IntArray() []int64 {
var res []int64
for _, e := range m.value {
switch m.Class {
case mxINT8:
res = append(res, int64(e.(int8)))
case mxINT16:
res = append(res, int64(e.(int16)))
case mxINT32:
res = append(res, int64(e.(int32)))
case mxINT64:
res = append(res, int64(e.(int64)))
case mxUINT8:
res = append(res, int64(e.(uint8)))
case mxUINT16:
res = append(res, int64(e.(uint16)))
case mxUINT32:
res = append(res, int64(e.(uint32)))
case mxUINT64:
res = append(res, int64(e.(uint64)))
default:
panic("unable to convert matrix to int64 array")
}
}
return res
}
// DoubleArray is a convenience method to extract the matrix value as []float64. Warning: It panics if the matlab class
// is not Double or Single
func (m *Matrix) DoubleArray() []float64 {
var res []float64
for _, e := range m.value {
switch m.Class {
case mxDOUBLE:
res = append(res, e.(float64))
case mxSINGLE:
res = append(res, float64(e.(float32)))
default:
panic("unable to convert matrix to double array")
}
}
return res
}
// String is a convenience method to extract the matrix value as []rune. Warning: It panics if the matlab class
// is not mxChar
func (m *Matrix) String() []rune {
var res []uint16
for _, e := range m.value {
switch m.Class {
case mxCHAR:
res = append(res, e.(uint16))
default:
panic("unable to convert matrix to double array")
}
}
return utf16.Decode(res)
}
// Convenience method to return the struct
func (m *Matrix) Struct() map[string]*Matrix {
return m.GetAtLocation(0).(map[string]*Matrix)
}