@@ -45,11 +45,146 @@ func (f formatter) Format(fs fmt.State, c rune) {
45
45
return
46
46
}
47
47
if f .format == nil {
48
- f .format = formatMATLAB
48
+ f .format = format
49
49
}
50
50
f .format (f .matrix , f .prefix , f .margin , f .dot , f .squeeze , fs , c )
51
51
}
52
52
53
+ // FormatMATLAB sets the printing behavior to output MATLAB syntax. If MATLAB syntax is
54
+ // specified, the ' ' verb flag and Excerpt option are ignored. If the alternative syntax
55
+ // verb flag, '#' is used the matrix is formatted in rows and columns.
56
+ func FormatMATLAB () FormatOption {
57
+ return func (f * formatter ) { f .format = formatMATLAB }
58
+ }
59
+
60
+ // format prints a pretty representation of m to the fs io.Writer. The format character c
61
+ // specifies the numerical representation of elements; valid values are those for float64
62
+ // specified in the fmt package, with their associated flags. In addition to this, a space
63
+ // preceding a verb indicates that zero values should be represented by the dot character.
64
+ // The printed range of the matrix can be limited by specifying a positive value for margin;
65
+ // If margin is greater than zero, only the first and last margin rows/columns of the matrix
66
+ // are output. If squeeze is true, column widths are determined on a per-column basis.
67
+ //
68
+ // format will not provide Go syntax output.
69
+ func format (m Matrix , prefix string , margin int , dot byte , squeeze bool , fs fmt.State , c rune ) {
70
+ rows , cols := m .Dims ()
71
+
72
+ var printed int
73
+ if margin <= 0 {
74
+ printed = rows
75
+ if cols > printed {
76
+ printed = cols
77
+ }
78
+ } else {
79
+ printed = margin
80
+ }
81
+
82
+ prec , pOk := fs .Precision ()
83
+ if ! pOk {
84
+ prec = - 1
85
+ }
86
+
87
+ var (
88
+ maxWidth int
89
+ widths widther
90
+ buf , pad []byte
91
+ )
92
+ if squeeze {
93
+ widths = make (columnWidth , cols )
94
+ } else {
95
+ widths = new (uniformWidth )
96
+ }
97
+ switch c {
98
+ case 'v' , 'e' , 'E' , 'f' , 'F' , 'g' , 'G' :
99
+ if c == 'v' {
100
+ buf , maxWidth = maxCellWidth (m , 'g' , printed , prec , widths )
101
+ } else {
102
+ buf , maxWidth = maxCellWidth (m , c , printed , prec , widths )
103
+ }
104
+ default :
105
+ fmt .Fprintf (fs , "%%!%c(%T=Dims(%d, %d))" , c , m , rows , cols )
106
+ return
107
+ }
108
+ width , _ := fs .Width ()
109
+ width = max (width , maxWidth )
110
+ pad = make ([]byte , max (width , 2 ))
111
+ for i := range pad {
112
+ pad [i ] = ' '
113
+ }
114
+
115
+ first := true
116
+ if rows > 2 * printed || cols > 2 * printed {
117
+ first = false
118
+ fmt .Fprintf (fs , "Dims(%d, %d)\n " , rows , cols )
119
+ }
120
+
121
+ skipZero := fs .Flag (' ' )
122
+ for i := 0 ; i < rows ; i ++ {
123
+ if ! first {
124
+ fmt .Fprint (fs , prefix )
125
+ }
126
+ first = false
127
+ var el string
128
+ switch {
129
+ case rows == 1 :
130
+ fmt .Fprint (fs , "[" )
131
+ el = "]"
132
+ case i == 0 :
133
+ fmt .Fprint (fs , "⎡" )
134
+ el = "⎤\n "
135
+ case i < rows - 1 :
136
+ fmt .Fprint (fs , "⎢" )
137
+ el = "⎥\n "
138
+ default :
139
+ fmt .Fprint (fs , "⎣" )
140
+ el = "⎦"
141
+ }
142
+
143
+ for j := 0 ; j < cols ; j ++ {
144
+ if j >= printed && j < cols - printed {
145
+ j = cols - printed - 1
146
+ if i == 0 || i == rows - 1 {
147
+ fmt .Fprint (fs , "... ... " )
148
+ } else {
149
+ fmt .Fprint (fs , " " )
150
+ }
151
+ continue
152
+ }
153
+
154
+ v := m .At (i , j )
155
+ if v == 0 && skipZero {
156
+ buf = buf [:1 ]
157
+ buf [0 ] = dot
158
+ } else {
159
+ if c == 'v' {
160
+ buf = strconv .AppendFloat (buf [:0 ], v , 'g' , prec , 64 )
161
+ } else {
162
+ buf = strconv .AppendFloat (buf [:0 ], v , byte (c ), prec , 64 )
163
+ }
164
+ }
165
+ if fs .Flag ('-' ) {
166
+ fs .Write (buf )
167
+ fs .Write (pad [:widths .width (j )- len (buf )])
168
+ } else {
169
+ fs .Write (pad [:widths .width (j )- len (buf )])
170
+ fs .Write (buf )
171
+ }
172
+
173
+ if j < cols - 1 {
174
+ fs .Write (pad [:2 ])
175
+ }
176
+ }
177
+
178
+ fmt .Fprint (fs , el )
179
+
180
+ if i >= printed - 1 && i < rows - printed && 2 * printed < rows {
181
+ i = rows - printed - 1
182
+ fmt .Fprintf (fs , "%s .\n %[1]s .\n %[1]s .\n " , prefix )
183
+ continue
184
+ }
185
+ }
186
+ }
187
+
53
188
// formatMATLAB prints a MATLAB representation of m to the fs io.Writer. The format character c
54
189
// specifies the numerical representation of elements; valid values are those for float64
55
190
// specified in the fmt package, with their associated flags.
0 commit comments