47 #include "EST_String.h"
48 #include "EST_types.h"
49 #include "EST_FileType.h"
50 #include "EST_Option.h"
51 #include "EST_FMatrix.h"
52 #include "EST_cutils.h"
53 #include "EST_Token.h"
79 cerr <<
"Matrix addition error: bad number of columns\n";
84 cerr <<
"Matrix addition error: bad number of rows\n";
87 for (i = 0; i < num_rows(); ++i)
88 for (j = 0; j < num_columns(); ++j)
99 cerr <<
"Matrix subtraction error: bad number of columns\n";
104 cerr <<
"Matrix subtraction error: bad number of rows\n";
107 for (i = 0; i < num_rows(); ++i)
108 for (j = 0; j < num_columns(); ++j)
118 for (i = 0; i < num_rows(); ++i)
119 for (j = 0; j < num_columns(); ++j)
120 a_no_check(i, j) *= f;
129 for (i = 0; i < num_rows(); ++i)
130 for (j = 0; j < num_columns(); ++j)
131 a_no_check(i, j) /= f;
142 cerr <<
"Matrix addition error: bad number of columns\n";
147 cerr <<
"Matrix addition error: bad number of rows\n";
165 cerr <<
"Matrix subtraction error: bad number of columns:" <<
171 cerr <<
"Matrix subtraction error: bad number of rows\n";
200 for(i=0;i<fv1.
length();i++)
217 cerr <<
"Matrix-vector multiplication error: matrix rows != vector size"
237 cerr <<
"Vector addition error: mismatched lengths\n";
242 for (i = 0; i < a.
length(); ++i)
254 cerr <<
"Vector subtraction error: mismatched lengths\n";
259 for (i = 0; i < a.
length(); ++i)
276 cerr <<
"Matrix-vector multiplication error: matrix rows != vector size"
310 cerr <<
"Matrix multiply error: a.num_columns() != b.num_rows()\n";
322 for (j = 0; j < n; ++j)
334 for (i = 0; i < rows; ++i)
335 for (j = 0; j < cols; ++j)
336 a_no_check(i,j) = inx[i][j];
344 if ((type ==
"est_ascii") || (type ==
"est_binary"))
345 return est_save(filename,type);
353 outf =
new ofstream(filename);
357 cerr <<
"FMatrix: can't open file \"" << filename
358 <<
"\" for writing" << endl;
359 return misc_write_error;
362 for (i = 0; i < num_rows(); ++i)
364 for (j = 0; j < num_columns(); ++j)
365 *outf << a_no_check(i,j) <<
" ";
384 else if ((fd = fopen(filename,
"wb")) == NULL)
386 cerr <<
"EST_FMatrix: binsave: failed to open \"" << filename <<
387 "\" for writing" << endl;
388 return misc_write_error;
391 fprintf(fd,
"EST_File fmatrix\n");
392 fprintf(fd,
"version 1\n");
393 if (type ==
"est_binary")
395 fprintf(fd,
"DataType binary\n");
396 if (EST_LITTLE_ENDIAN)
397 fprintf(fd,
"ByteOrder LittleEndian\n");
399 fprintf(fd,
"ByteOrder BigEndian\n");
402 fprintf(fd,
"DataType ascii\n");
404 fprintf(fd,
"rows %d\n",num_rows());
405 fprintf(fd,
"columns %d\n",num_columns());
407 fprintf(fd,
"EST_Header_End\n");
409 if (type ==
"est_binary")
411 for (i = 0; i < num_rows(); ++i)
412 for (j=0; j < num_columns(); j++)
413 if (fwrite(&a_no_check(i,j),
sizeof(
float),1,fd) != 1)
415 cerr <<
"EST_FMatrix: binsave: failed to write row "
416 << i <<
" column " << j
417 <<
" to \"" << filename <<
"\"" << endl;
418 return misc_write_error;
423 for (i = 0; i < num_rows(); ++i)
425 for (j=0; j < num_columns(); j++)
426 fprintf(fd,
"%f ",a_no_check(i,j));
441 int rows, cols, swap;
448 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
450 cerr <<
"FMatrix: can't open fmatrix input file "
452 return misc_read_error;
454 if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
456 if (t != est_file_fmatrix)
457 return misc_read_error;
458 if (hinfo.
ival(
"version") != 1)
461 " wrong version of fmatrix format expected 1 but found " <<
462 hinfo.
ival(
"version") << endl;
463 return misc_read_error;
465 rows = hinfo.
ival(
"rows");
466 cols = hinfo.
ival(
"columns");
471 for (i = 0; i < num_rows(); ++i)
473 for (j = 0; j < num_columns(); ++j)
474 a_no_check(i,j) = atof(ts.
get().string());
478 " missing end of line at end of row " << i << endl;
479 return misc_read_error;
486 if ((EST_BIG_ENDIAN && (hinfo.
sval(
"ByteOrder")==
"LittleEndian")) ||
487 (EST_LITTLE_ENDIAN && (hinfo.
sval(
"ByteOrder") ==
"BigEndian")))
492 buff = walloc(
float,rows*cols);
494 if (ts.
fread(buff,
sizeof(
float),rows*cols) != rows*cols)
496 cerr <<
"EST_FMatrix: binload: short file in \""
497 << filename <<
"\"" << endl;
498 return misc_read_error;
501 swap_bytes_float(buff,rows*cols);
502 for (k = i = 0; i < num_rows(); ++i)
503 for (j = 0; j < num_columns(); ++j)
504 a_no_check(i,j) = buff[k++];
517 if ((r = est_load(filename)) == format_ok)
519 else if (r == wrong_format)
523 int i, j, n_rows=0, n_cols=0;
526 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
528 cerr <<
"Can't open fmatrix file " << filename << endl;
529 return misc_read_error;
535 for (n_rows = 0; !ts.
eof(); ++n_rows)
541 for (n_cols = 0; !tt.
eof(); ++n_cols)
546 resize(n_rows, n_cols);
548 for (p = sl.head(), i = 0; p != 0; ++i, p = p->next())
551 for (j = 0; !tt.
eof(); ++j)
552 a_no_check(i,j) = atof(tt.
get().string());
555 cerr <<
"Wrong number of points in row " << i << endl;
556 cerr <<
"Expected " << n_cols <<
" got " << j << endl;
557 return misc_read_error;
571 cerr <<
"Cannot elementwise add vectors of differing lengths"
576 for (i = 0; i < n(); ++i)
587 cerr <<
"Cannot elementwise multiply vectors of differing lengths"
592 for (
int i = 0; i < n(); ++i)
600 for (
int i = 0; i < n(); ++i)
609 for (
int i = 0; i < n(); ++i)
628 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
630 cerr <<
"FVector: can't open FVector input file "
632 return misc_read_error;
634 if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
636 if (t != est_file_fvector)
637 return misc_read_error;
638 if (hinfo.
ival(
"version") != 1)
641 " wrong version of FVector format expected 1 but found " <<
642 hinfo.
ival(
"version") << endl;
643 return misc_read_error;
645 l = hinfo.
ival(
"length");
650 for (i = 0; i < length(); ++i)
651 a_no_check(i) = atof(ts.
get().string());
656 if ((EST_BIG_ENDIAN && (hinfo.
sval(
"ByteOrder")==
"LittleEndian")) ||
657 (EST_LITTLE_ENDIAN && (hinfo.
sval(
"ByteOrder") ==
"BigEndian")))
662 buff = walloc(
float,l);
664 if (ts.
fread(buff,
sizeof(
float),l) != l)
666 cerr <<
"EST_FVector: binload: short file in \""
667 << filename <<
"\"" << endl;
668 return misc_read_error;
671 swap_bytes_float(buff,l);
672 for (k = i = 0; i < length(); ++i)
673 a_no_check(i) = buff[k++];
686 if ((r = est_load(filename)) == format_ok)
688 else if (r == wrong_format)
696 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
698 cerr <<
"can't open vector input file " << filename << endl;
699 return misc_read_error;
711 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
713 cerr <<
"can't open vector input file " << filename << endl;
714 return misc_read_error;
717 for (i = 0; !ts.
eof(); ++i)
719 s = ts.
get().string();
720 (*this)[i] = (float)(atof(s));
820 cerr <<
"Vector dot product error: differing vector size"
826 for (i = 0; i < v1.
length(); ++i)
837 if ((type ==
"est_ascii") || (type ==
"est_binary"))
838 return est_save(filename,type);
846 outf =
new ofstream(filename);
851 cerr <<
"FVector: can't open file \"" << filename
852 <<
"\" for writing" << endl;
853 return misc_write_error;
856 for (i = 0; i < length(); ++i)
857 *outf << a_no_check(i) <<
" ";
867 EST_write_status EST_FVector::est_save(
const EST_String &filename,
875 else if ((fd = fopen(filename,
"wb")) == NULL)
877 cerr <<
"EST_FVector: binsave: failed to open \"" << filename <<
878 "\" for writing" << endl;
879 return misc_write_error;
882 fprintf(fd,
"EST_File fvector\n");
883 fprintf(fd,
"version 1\n");
884 if (type ==
"est_binary")
886 fprintf(fd,
"DataType binary\n");
887 if (EST_LITTLE_ENDIAN)
888 fprintf(fd,
"ByteOrder LittleEndian\n");
890 fprintf(fd,
"ByteOrder BigEndian\n");
893 fprintf(fd,
"DataType ascii\n");
895 fprintf(fd,
"length %d\n",length());
896 fprintf(fd,
"EST_Header_End\n");
898 if (type ==
"est_binary")
900 for (i = 0; i < length(); ++i)
901 if (fwrite(&a_no_check(i),
sizeof(
float),1,fd) != 1)
903 cerr <<
"EST_FVector: binsave: failed to write item "
904 << i <<
" to \"" << filename <<
"\"" << endl;
905 return misc_write_error;
910 for (i = 0; i < length(); ++i)
911 fprintf(fd,
"%.25f ",a_no_check(i));