Mercurial > octave
annotate libinterp/corefcn/ls-mat4.cc @ 31231:a026fb2be108
sparse-xpow.cc: Return empty matrix for empty input (bug #63080)
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Mon, 19 Sep 2022 07:05:31 -0400 |
parents | 796f54d4ddbf |
children | aac27ad79be6 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29604
diff
changeset
|
3 // Copyright (C) 1996-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
4634 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
27 # include "config.h" |
4634 | 28 #endif |
29 | |
30 #include <iomanip> | |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25392
diff
changeset
|
31 #include <istream> |
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25392
diff
changeset
|
32 #include <ostream> |
4634 | 33 #include <string> |
34 | |
35 #include "byte-swap.h" | |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
36 #include "dMatrix.h" |
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
37 #include "dSparse.h" |
4634 | 38 #include "data-conv.h" |
39 #include "file-ops.h" | |
40 #include "glob-match.h" | |
41 #include "lo-mappers.h" | |
42 #include "mach-info.h" | |
43 #include "oct-env.h" | |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
44 #include "oct-locbuf.h" |
4634 | 45 #include "oct-time.h" |
46 #include "quit.h" | |
47 | |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
48 #include "ls-mat4.h" |
4634 | 49 #include "Cell.h" |
50 #include "defun.h" | |
51 #include "error.h" | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
52 #include "errwarn.h" |
4634 | 53 #include "load-save.h" |
54 #include "oct-map.h" | |
55 #include "ov-cell.h" | |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
56 #include "ovl.h" |
4634 | 57 #include "pager.h" |
58 #include "sysdep.h" | |
59 #include "utils.h" | |
60 #include "variables.h" | |
61 #include "version.h" | |
62 | |
63 | |
64 // Read LEN elements of data from IS in the format specified by | |
65 // PRECISION, placing the result in DATA. If SWAP is TRUE, swap | |
66 // the bytes of each element before copying to DATA. FLT_FMT | |
67 // specifies the format of the data if we are reading floating point | |
68 // numbers. | |
69 | |
70 static void | |
71 read_mat_binary_data (std::istream& is, double *data, int precision, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
72 int len, bool swap, |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
73 octave::mach_info::float_format flt_fmt) |
4634 | 74 { |
75 switch (precision) | |
76 { | |
77 case 0: | |
78 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); | |
79 break; | |
80 | |
81 case 1: | |
82 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); | |
83 break; | |
84 | |
85 case 2: | |
86 read_doubles (is, data, LS_INT, len, swap, flt_fmt); | |
87 break; | |
88 | |
89 case 3: | |
90 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); | |
91 break; | |
92 | |
93 case 4: | |
94 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); | |
95 break; | |
96 | |
97 case 5: | |
98 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); | |
99 break; | |
100 | |
101 default: | |
102 break; | |
103 } | |
104 } | |
105 | |
106 int | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
107 read_mat_file_header (std::istream& is, bool& swap, int32_t& mopt, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
108 int32_t& nr, int32_t& nc, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
109 int32_t& imag, int32_t& len, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
110 int quiet) |
4634 | 111 { |
112 swap = false; | |
113 | |
114 // We expect to fail here, at the beginning of a record, so not | |
115 // being able to read another mopt value should not result in an | |
116 // error. | |
117 | |
5760 | 118 is.read (reinterpret_cast<char *> (&mopt), 4); |
4634 | 119 if (! is) |
120 return 1; | |
121 | |
5760 | 122 if (! is.read (reinterpret_cast<char *> (&nr), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
123 return -1; |
4634 | 124 |
5760 | 125 if (! is.read (reinterpret_cast<char *> (&nc), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
126 return -1; |
4634 | 127 |
5760 | 128 if (! is.read (reinterpret_cast<char *> (&imag), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
129 return -1; |
4634 | 130 |
5760 | 131 if (! is.read (reinterpret_cast<char *> (&len), 4)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
132 return -1; |
4634 | 133 |
134 // If mopt is nonzero and the byte order is swapped, mopt will be | |
135 // bigger than we expect, so we swap bytes. | |
136 // | |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
137 // If mopt is zero, it means the file was written on a little endian machine, |
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
138 // and we only need to swap if we are running on a big endian machine. |
4634 | 139 // |
140 // Gag me. | |
141 | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
142 if (octave::mach_info::words_big_endian () && mopt == 0) |
4634 | 143 swap = true; |
144 | |
145 // mopt is signed, therefore byte swap may result in negative value. | |
146 | |
147 if (mopt > 9999 || mopt < 0) | |
148 swap = true; | |
149 | |
150 if (swap) | |
151 { | |
4944 | 152 swap_bytes<4> (&mopt); |
153 swap_bytes<4> (&nr); | |
154 swap_bytes<4> (&nc); | |
155 swap_bytes<4> (&imag); | |
156 swap_bytes<4> (&len); | |
4634 | 157 } |
158 | |
159 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) | |
160 { | |
161 if (! quiet) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
162 error ("load: can't read binary file"); |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
163 |
4634 | 164 return -1; |
165 } | |
166 | |
167 return 0; | |
168 } | |
169 | |
170 // We don't just use a cast here, because we need to be able to detect | |
171 // possible errors. | |
172 | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
173 octave::mach_info::float_format |
4634 | 174 mopt_digit_to_float_format (int mach) |
175 { | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
176 octave::mach_info::float_format flt_fmt = octave::mach_info::flt_fmt_unknown; |
4634 | 177 |
178 switch (mach) | |
179 { | |
180 case 0: | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
181 flt_fmt = octave::mach_info::flt_fmt_ieee_little_endian; |
4634 | 182 break; |
183 | |
184 case 1: | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
185 flt_fmt = octave::mach_info::flt_fmt_ieee_big_endian; |
4634 | 186 break; |
187 | |
188 case 2: | |
189 case 3: | |
190 case 4: | |
191 default: | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
192 flt_fmt = octave::mach_info::flt_fmt_unknown; |
4634 | 193 break; |
194 } | |
195 | |
196 return flt_fmt; | |
197 } | |
198 | |
199 int | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
200 float_format_to_mopt_digit (octave::mach_info::float_format flt_fmt) |
4634 | 201 { |
202 int retval = -1; | |
203 | |
204 switch (flt_fmt) | |
205 { | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
206 case octave::mach_info::flt_fmt_ieee_little_endian: |
4634 | 207 retval = 0; |
208 break; | |
209 | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
210 case octave::mach_info::flt_fmt_ieee_big_endian: |
4634 | 211 retval = 1; |
212 break; | |
213 | |
214 default: | |
215 break; | |
216 } | |
217 | |
218 return retval; | |
219 } | |
220 | |
221 // Extract one value (scalar, matrix, string, etc.) from stream IS and | |
222 // place it in TC, returning the name of the variable. | |
223 // | |
224 // The data is expected to be in Matlab version 4 .mat format, though | |
225 // not all the features of that format are supported. | |
226 // | |
227 // FILENAME is used for error messages. | |
228 // | |
229 // This format provides no way to tag the data as global. | |
230 | |
231 std::string | |
232 read_mat_binary_data (std::istream& is, const std::string& filename, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
233 octave_value& tc) |
4634 | 234 { |
235 std::string retval; | |
236 | |
237 bool swap = false; | |
5828 | 238 int32_t mopt, nr, nc, imag, len; |
4634 | 239 |
240 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); | |
241 if (err) | |
242 { | |
243 if (err < 0) | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
244 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
245 |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
246 return retval; |
4634 | 247 } |
248 | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
249 int type = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
250 int prec = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
251 int order = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
252 int mach = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
253 |
4634 | 254 type = mopt % 10; // Full, sparse, etc. |
255 mopt /= 10; // Eliminate first digit. | |
256 prec = mopt % 10; // double, float, int, etc. | |
257 mopt /= 10; // Eliminate second digit. | |
258 order = mopt % 10; // Row or column major ordering. | |
259 mopt /= 10; // Eliminate third digit. | |
260 mach = mopt % 10; // IEEE, VAX, etc. | |
261 | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
262 octave::mach_info::float_format flt_fmt; |
4634 | 263 flt_fmt = mopt_digit_to_float_format (mach); |
264 | |
21739
c4ab2e54f100
use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
265 if (flt_fmt == octave::mach_info::flt_fmt_unknown) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
266 error ("load: unrecognized binary format!"); |
4634 | 267 |
268 if (imag && type == 1) | |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
269 error ("load: encountered complex matrix with string flag set!"); |
4634 | 270 |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
271 int dlen = 0; |
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
272 |
4634 | 273 // LEN includes the terminating character, and the file is also |
274 // supposed to include it, but apparently not all files do. Either | |
275 // way, I think this should work. | |
276 | |
277 { | |
278 OCTAVE_LOCAL_BUFFER (char, name, len+1); | |
279 name[len] = '\0'; | |
5760 | 280 if (! is.read (name, len)) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
281 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 282 retval = name; |
283 | |
284 dlen = nr * nc; | |
285 if (dlen < 0) | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
286 error ("load: trouble reading binary file '%s'", filename.c_str ()); |
4634 | 287 |
288 if (order) | |
289 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
290 octave_idx_type tmp = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
291 nr = nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
292 nc = tmp; |
4634 | 293 } |
294 | |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
295 if (type == 2) |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
296 { |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
297 if (nc == 4) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
298 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
299 octave_idx_type nr_new, nc_new; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
300 Array<Complex> data (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
301 Array<octave_idx_type> c (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
302 Array<octave_idx_type> r (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
303 OCTAVE_LOCAL_BUFFER (double, dtmp, nr); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
304 OCTAVE_LOCAL_BUFFER (double, ctmp, nr); |
4634 | 305 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
306 read_mat_binary_data (is, dtmp, prec, nr, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
307 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
308 r.xelem (i) = dtmp[i] - 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
309 nr_new = dtmp[nr - 1]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
310 read_mat_binary_data (is, dtmp, prec, nr, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
311 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
312 c.xelem (i) = dtmp[i] - 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
313 nc_new = dtmp[nr - 1]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
314 read_mat_binary_data (is, dtmp, prec, nr - 1, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
315 read_mat_binary_data (is, ctmp, prec, 1, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
316 read_mat_binary_data (is, ctmp, prec, nr - 1, swap, flt_fmt); |
4634 | 317 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
318 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
319 data.xelem (i) = Complex (dtmp[i], ctmp[i]); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
320 read_mat_binary_data (is, ctmp, prec, 1, swap, flt_fmt); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
321 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
322 SparseComplexMatrix smc = SparseComplexMatrix (data, r, c, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
323 nr_new, nc_new); |
4634 | 324 |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
325 tc = (order ? smc.transpose () : smc); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
326 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
327 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
328 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
329 octave_idx_type nr_new, nc_new; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
330 Array<double> data (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
331 Array<octave_idx_type> c (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
332 Array<octave_idx_type> r (dim_vector (1, nr - 1)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
333 OCTAVE_LOCAL_BUFFER (double, dtmp, nr); |
4634 | 334 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
335 read_mat_binary_data (is, dtmp, prec, nr, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
336 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
337 r.xelem (i) = dtmp[i] - 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
338 nr_new = dtmp[nr - 1]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
339 read_mat_binary_data (is, dtmp, prec, nr, swap, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
340 for (octave_idx_type i = 0; i < nr - 1; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
341 c.xelem (i) = dtmp[i] - 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
342 nc_new = dtmp[nr - 1]; |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
343 read_mat_binary_data (is, data.fortran_vec (), prec, nr - 1, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
344 swap, flt_fmt); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
345 read_mat_binary_data (is, dtmp, prec, 1, swap, flt_fmt); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
346 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
347 SparseMatrix sm = SparseMatrix (data, r, c, nr_new, nc_new); |
4634 | 348 |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
349 tc = (order ? sm.transpose () : sm); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
350 } |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
351 } |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
352 else |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
353 { |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
354 Matrix re (nr, nc); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
355 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
356 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
357 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20228
diff
changeset
|
358 if (! is) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
359 error ("load: reading matrix data for '%s'", name); |
4634 | 360 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
361 if (imag) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
362 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
363 Matrix im (nr, nc); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
364 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
365 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
366 flt_fmt); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
367 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20228
diff
changeset
|
368 if (! is) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
369 error ("load: reading imaginary matrix data for '%s'", name); |
4634 | 370 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
371 ComplexMatrix ctmp (nr, nc); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
372 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
373 for (octave_idx_type j = 0; j < nc; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
374 for (octave_idx_type i = 0; i < nr; i++) |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
375 ctmp (i,j) = Complex (re(i,j), im(i,j)); |
4634 | 376 |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
377 tc = (order ? ctmp.transpose () : ctmp); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
378 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
379 else |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
380 tc = (order ? re.transpose () : re); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
381 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
382 if (type == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
383 tc = tc.convert_to_str (false, true, '\''); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
384 } |
4634 | 385 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
386 return retval; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
387 } |
4634 | 388 } |
389 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
390 // Save the data from TC along with the corresponding NAME on stream OS |
4634 | 391 // in the MatLab version 4 binary format. |
392 | |
393 bool | |
394 save_mat_binary_data (std::ostream& os, const octave_value& tc, | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
395 const std::string& name) |
4634 | 396 { |
5828 | 397 int32_t mopt = 0; |
4634 | 398 |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
399 mopt += tc.issparse () ? 2 : tc.is_string () ? 1 : 0; |
4634 | 400 |
27277
db687716fed6
style fixes: generally aim to break long lines before operators, not after
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
401 octave::mach_info::float_format flt_fmt |
db687716fed6
style fixes: generally aim to break long lines before operators, not after
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
402 = octave::mach_info::native_float_format ();; |
4634 | 403 |
404 mopt += 1000 * float_format_to_mopt_digit (flt_fmt); | |
405 | |
5760 | 406 os.write (reinterpret_cast<char *> (&mopt), 4); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
407 |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
408 octave_idx_type len; |
5828 | 409 int32_t nr = tc.rows (); |
4634 | 410 |
5828 | 411 int32_t nc = tc.columns (); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
412 |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
413 if (tc.issparse ()) |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
414 { |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
415 len = tc.nnz (); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
416 uint32_t nnz = len + 1; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
417 os.write (reinterpret_cast<char *> (&nnz), 4); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
418 |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23455
diff
changeset
|
419 uint32_t iscmplx = (tc.iscomplex () ? 4 : 3); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
420 os.write (reinterpret_cast<char *> (&iscmplx), 4); |
4634 | 421 |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
422 uint32_t tmp = 0; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
423 os.write (reinterpret_cast<char *> (&tmp), 4); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
424 } |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
425 else |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
426 { |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
427 os.write (reinterpret_cast<char *> (&nr), 4); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
428 os.write (reinterpret_cast<char *> (&nc), 4); |
4634 | 429 |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23455
diff
changeset
|
430 int32_t imag = (tc.iscomplex () ? 1 : 0); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
431 os.write (reinterpret_cast<char *> (&imag), 4); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
432 |
29604
44f0b8975fe5
Cast to wider resulting type before multiplication.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
433 len = static_cast<octave_idx_type> (nr) * nc; |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
434 } |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
435 |
4634 | 436 // LEN includes the terminating character, and the file is also |
437 // supposed to include it. | |
438 | |
5828 | 439 int32_t name_len = name.length () + 1; |
4634 | 440 |
5760 | 441 os.write (reinterpret_cast<char *> (&name_len), 4); |
4634 | 442 os << name << '\0'; |
443 | |
444 if (tc.is_string ()) | |
445 { | |
446 charMatrix chm = tc.char_matrix_value (); | |
447 | |
5275 | 448 octave_idx_type nrow = chm.rows (); |
449 octave_idx_type ncol = chm.cols (); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
450 |
4634 | 451 OCTAVE_LOCAL_BUFFER (double, buf, ncol*nrow); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
452 |
5275 | 453 for (octave_idx_type i = 0; i < nrow; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
454 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
455 std::string tstr = chm.row_as_string (i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
456 const char *s = tstr.data (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
457 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
458 for (octave_idx_type j = 0; j < ncol; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
459 buf[j*nrow+i] = static_cast<double> (*s++ & 0x00FF); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
460 } |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
461 std::streamsize n_bytes = static_cast<std::streamsize> (nrow) * |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
462 static_cast<std::streamsize> (ncol) * |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
463 sizeof (double); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
464 os.write (reinterpret_cast<char *> (buf), n_bytes); |
4634 | 465 } |
466 else if (tc.is_range ()) | |
467 { | |
28638
98192ec1621f
replace Range with range<double>
John W. Eaton <jwe@octave.org>
parents:
28636
diff
changeset
|
468 octave::range<double> r = tc.range_value (); |
4634 | 469 double base = r.base (); |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
27993
diff
changeset
|
470 double inc = r.increment (); |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
471 octave_idx_type nel = r.numel (); |
5275 | 472 for (octave_idx_type i = 0; i < nel; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
473 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
474 double x = base + i * inc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
475 os.write (reinterpret_cast<char *> (&x), 8); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
476 } |
4634 | 477 } |
478 else if (tc.is_real_scalar ()) | |
479 { | |
480 double tmp = tc.double_value (); | |
5760 | 481 os.write (reinterpret_cast<char *> (&tmp), 8); |
4634 | 482 } |
23583
b7747a2c88b2
maint: Deprecate is_sparse_type and replace with issparse.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
483 else if (tc.issparse ()) |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
484 { |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
485 double ds; |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
486 OCTAVE_LOCAL_BUFFER (double, dtmp, len); |
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
487 if (tc.is_complex_matrix ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
488 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
489 SparseComplexMatrix m = tc.sparse_complex_matrix_value (); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
490 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
491 for (octave_idx_type i = 0; i < len; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
492 dtmp[i] = m.ridx (i) + 1; |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
493 std::streamsize n_bytes = 8 * static_cast<std::streamsize> (len); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
494 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
495 ds = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
496 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
497 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
498 octave_idx_type ii = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
499 for (octave_idx_type j = 0; j < nc; j++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
500 for (octave_idx_type i = m.cidx (j); i < m.cidx (j+1); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
501 dtmp[ii++] = j + 1; |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
502 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
503 ds = nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
504 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
505 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
506 for (octave_idx_type i = 0; i < len; i++) |
23708
750e42a35adc
Use imag, real, arg from std library for Complex types.
Rik <rik@octave.org>
parents:
23583
diff
changeset
|
507 dtmp[i] = std::real (m.data (i)); |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
508 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
509 ds = 0.; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
510 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
511 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
512 for (octave_idx_type i = 0; i < len; i++) |
23708
750e42a35adc
Use imag, real, arg from std library for Complex types.
Rik <rik@octave.org>
parents:
23583
diff
changeset
|
513 dtmp[i] = std::imag (m.data (i)); |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
514 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
515 os.write (reinterpret_cast<const char *> (&ds), 8); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
516 } |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
517 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
518 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
519 SparseMatrix m = tc.sparse_matrix_value (); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
520 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
521 for (octave_idx_type i = 0; i < len; i++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
522 dtmp[i] = m.ridx (i) + 1; |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
523 std::streamsize n_bytes = 8 * static_cast<std::streamsize> (len); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
524 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
525 ds = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
526 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
527 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
528 octave_idx_type ii = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
529 for (octave_idx_type j = 0; j < nc; j++) |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
530 for (octave_idx_type i = m.cidx (j); i < m.cidx (j+1); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
531 dtmp[ii++] = j + 1; |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
532 os.write (reinterpret_cast<const char *> (dtmp), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
533 ds = nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
534 os.write (reinterpret_cast<const char *> (&ds), 8); |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
535 |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
536 os.write (reinterpret_cast<const char *> (m.data ()), n_bytes); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
537 ds = 0.; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
538 os.write (reinterpret_cast<const char *> (&ds), 8); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
539 } |
7918
78eef61f75d5
Add matlab V4 sparse matrixload/save
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
540 } |
4634 | 541 else if (tc.is_real_matrix ()) |
542 { | |
543 Matrix m = tc.matrix_value (); | |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
544 std::streamsize n_bytes = 8 * static_cast<std::streamsize> (len); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
545 os.write (reinterpret_cast<const char *> (m.data ()), n_bytes); |
4634 | 546 } |
547 else if (tc.is_complex_scalar ()) | |
548 { | |
549 Complex tmp = tc.complex_value (); | |
5760 | 550 os.write (reinterpret_cast<char *> (&tmp), 16); |
4634 | 551 } |
552 else if (tc.is_complex_matrix ()) | |
553 { | |
554 ComplexMatrix m_cmplx = tc.complex_matrix_value (); | |
555 Matrix m = ::real (m_cmplx); | |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
556 std::streamsize n_bytes = 8 * static_cast<std::streamsize> (len); |
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
557 os.write (reinterpret_cast<const char *> (m.data ()), n_bytes); |
4634 | 558 m = ::imag (m_cmplx); |
18691
9a6646cc7c3e
Fix load/save to handle up to 2^32-1 elements.
Rik <rik@octave.org>
parents:
18384
diff
changeset
|
559 os.write (reinterpret_cast<const char *> (m.data ()), n_bytes); |
4634 | 560 } |
561 else | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
562 // FIXME: Should this just error out rather than warn? |
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
563 warn_wrong_type_arg ("save", tc); |
4634 | 564 |
18384
bd9d34f28b0f
Use std::ostream::fail instead of unsafe implicit bool conversion (bug #41335)
Mike Miller <mtmiller@ieee.org>
parents:
17787
diff
changeset
|
565 return ! os.fail (); |
4634 | 566 } |