46 #include "EST_String.h" 47 #include "EST_types.h" 48 #include "EST_FileType.h" 49 #include "EST_Option.h" 50 #include "EST_DMatrix.h" 51 #include "EST_cutils.h" 52 #include "EST_Token.h" 55 EST_String EST_DMatrix::default_file_type =
"est_ascii";
72 cerr <<
"Matrix addition error: bad number of columns\n";
77 cerr <<
"Matrix addition error: bad number of rows\n";
92 cerr <<
"Matrix subtraction error: bad number of columns\n";
97 cerr <<
"Matrix subtraction error: bad number of rows\n";
135 cerr <<
"Matrix addition error: bad number of columns\n";
140 cerr <<
"Matrix addition error: bad number of rows\n";
158 cerr <<
"Matrix subtraction error: bad number of columns:" <<
164 cerr <<
"Matrix subtraction error: bad number of rows\n";
197 cerr <<
"Matrix-vector multiplication error: matrix rows != vector size" 217 cerr <<
"Vector addition error: mismatched lengths\n";
222 for (i = 0; i < a.
length(); ++i)
234 cerr <<
"Vector subtraction error: mismatched lengths\n";
239 for (i = 0; i < a.
length(); ++i)
256 cerr <<
"Matrix-vector multiplication error: matrix rows != vector size" 290 cerr <<
"Matrix multiply error: a.num_columns() != b.num_rows()\n";
302 for (j = 0; j <
n; ++j)
314 for (i = 0; i < rows; ++i)
315 for (j = 0; j < cols; ++j)
324 if ((type ==
"est_ascii") || (type ==
"est_binary"))
333 outf =
new ofstream(filename);
338 cerr <<
"DMatrix: can't open file \"" << filename
339 <<
"\" for writing" << endl;
340 return misc_write_error;
365 else if ((fd = fopen(filename,
"wb")) == NULL)
367 cerr <<
"EST_DMatrix: binsave: failed to open \"" << filename <<
368 "\" for writing" << endl;
369 return misc_write_error;
372 fprintf(fd,
"EST_File dmatrix\n");
373 fprintf(fd,
"version 1\n");
374 if (type ==
"est_binary")
376 fprintf(fd,
"DataType binary\n");
377 if (EST_LITTLE_ENDIAN)
378 fprintf(fd,
"ByteOrder LittleEndian\n");
380 fprintf(fd,
"ByteOrder BigEndian\n");
383 fprintf(fd,
"DataType ascii\n");
388 fprintf(fd,
"EST_Header_End\n");
390 if (type ==
"est_binary")
394 if (fwrite(&
a_no_check(i,j),
sizeof(
double),1,fd) != 1)
396 cerr <<
"EST_DMatrix: binsave: failed to write row " 397 << i <<
" column " << j
398 <<
" to \"" << filename <<
"\"" << endl;
399 return misc_write_error;
423 int rows, cols, swap;
430 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
432 cerr <<
"DMatrix: can't open DMatrix input file " 434 return misc_read_error;
436 if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
438 if (t != est_file_dmatrix)
439 return misc_read_error;
440 if (hinfo.
ival(
"version") != 1)
443 " wrong version of DMatrix format expected 1 but found " <<
444 hinfo.
ival(
"version") << endl;
445 return misc_read_error;
447 rows = hinfo.
ival(
"rows");
448 cols = hinfo.
ival(
"columns");
460 " missing end of line at end of row " << i << endl;
461 return misc_read_error;
468 if ((EST_BIG_ENDIAN && (hinfo.
sval(
"ByteOrder")==
"LittleEndian")) ||
469 (EST_LITTLE_ENDIAN && (hinfo.
sval(
"ByteOrder") ==
"BigEndian")))
474 buff = walloc(
double,rows*cols);
476 if (ts.
fread(buff,
sizeof(
double),rows*cols) != rows*cols)
478 cerr <<
"EST_DMatrix: binload: short file in \"" 479 << filename <<
"\"" << endl;
480 return misc_read_error;
483 swap_bytes_double(buff,rows*cols);
484 for (k = i = 0; i <
num_rows(); ++i)
498 if ((r =
est_load(filename)) == format_ok)
500 else if (r == wrong_format)
504 int i, j, n_rows=0, n_cols=0;
507 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
509 cerr <<
"Can't open DMatrix file " << filename << endl;
510 return misc_read_error;
516 for (n_rows = 0; !ts.
eof(); ++n_rows)
522 for (n_cols = 0; !tt.
eof(); ++n_cols)
529 for (p = sl.head(), i = 0; p != 0; ++i, p = p->next())
532 for (j = 0; !tt.
eof(); ++j)
536 cerr <<
"Wrong number of points in row " << i << endl;
537 cerr <<
"Expected " << n_cols <<
" got " << j << endl;
538 return misc_read_error;
560 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
562 cerr <<
"DVector: can't open DVector input file " 564 return misc_read_error;
566 if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
568 if (t != est_file_dvector)
569 return misc_read_error;
570 if (hinfo.
ival(
"version") != 1)
573 " wrong version of DVector format expected 1 but found " <<
574 hinfo.
ival(
"version") << endl;
575 return misc_read_error;
577 l = hinfo.
ival(
"length");
582 for (i = 0; i <
length(); ++i)
588 if ((EST_BIG_ENDIAN && (hinfo.
sval(
"ByteOrder")==
"LittleEndian")) ||
589 (EST_LITTLE_ENDIAN && (hinfo.
sval(
"ByteOrder") ==
"BigEndian")))
594 buff = walloc(
double,l);
596 if (ts.
fread(buff,
sizeof(
double),l) != l)
598 cerr <<
"EST_DVector: binload: short file in \"" 599 << filename <<
"\"" << endl;
600 return misc_read_error;
603 swap_bytes_double(buff,l);
604 for (k = i = 0; i <
length(); ++i)
618 if ((r =
est_load(filename)) == format_ok)
620 else if (r == wrong_format)
628 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
630 cerr <<
"can't open vector input file " << filename << endl;
631 return misc_read_error;
643 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
645 cerr <<
"can't open vector input file " << filename << endl;
646 return misc_read_error;
649 for (i = 0; !ts.
eof(); ++i)
651 s = ts.
get().string();
652 (*this)[i] = atof(s);
669 cerr <<
"Cannot elementwise add vectors of differing lengths" 674 for (i = 0; i <
n(); ++i)
685 cerr <<
"Cannot elementwise multiply vectors of differing lengths" 690 for (
int i = 0; i <
n(); ++i)
698 for (
int i = 0; i <
n(); ++i)
708 cerr <<
"Can't do vector dot prod - differing vector sizes !" << endl;
712 for (
int i = 0; i < v1.
length(); ++i)
721 for (
int i = 0; i <
n(); ++i)
732 if ((type ==
"est_ascii") || (type ==
"est_binary"))
741 outf =
new ofstream(filename);
746 cerr <<
"DVector: can't open file \"" << filename
747 <<
"\" for writing" << endl;
748 return misc_write_error;
751 for (i = 0; i <
length(); ++i)
762 EST_write_status EST_DVector::est_save(
const EST_String &filename,
770 else if ((fd = fopen(filename,
"wb")) == NULL)
772 cerr <<
"EST_DVector: binsave: failed to open \"" << filename <<
773 "\" for writing" << endl;
774 return misc_write_error;
777 fprintf(fd,
"EST_File dvector\n");
778 fprintf(fd,
"version 1\n");
779 if (type ==
"est_binary")
781 fprintf(fd,
"DataType binary\n");
782 if (EST_LITTLE_ENDIAN)
783 fprintf(fd,
"ByteOrder LittleEndian\n");
785 fprintf(fd,
"ByteOrder BigEndian\n");
788 fprintf(fd,
"DataType ascii\n");
790 fprintf(fd,
"length %d\n",
length());
791 fprintf(fd,
"EST_Header_End\n");
793 if (type ==
"est_binary")
795 for (i = 0; i <
length(); ++i)
796 if (fwrite(&
a_no_check(i),
sizeof(
double),1,fd) != 1)
798 cerr <<
"EST_DVector: binsave: failed to write item " 799 << i <<
" to \"" << filename <<
"\"" << endl;
800 return misc_write_error;
805 for (i = 0; i <
length(); ++i)
EST_write_status save(const EST_String &filename, const EST_String &type=EST_DMatrix::default_file_type)
Save in file (ascii or binary)
EST_TokenStream & get(EST_Token &t)
get next token in stream
int num_rows() const
return number of rows
EST_DVector & operator/=(const double d)
elementwise divide by scalar
EST_write_status save(const EST_String &filename, const EST_String &type)
save vector to file filename.
EST_read_status est_load(const EST_String &filename)
Load from file in est format (binary/ascii defined in file itself)
int fread(void *buff, int size, int nitems) EST_WARN_UNUSED_RESULT
Reading binary data, (don't use peek() immediately beforehand)
void set_SingleCharSymbols(const EST_String &sc)
set which characters are to be treated as single character symbols
void close(void)
Close stream.
INLINE const T & a_no_check(int n) const
read-only const access operator: without bounds checking
int num_columns() const
return number of columns
EST_DVector & operator+=(const EST_DVector &s)
elementwise add
friend EST_DMatrix operator*(const EST_DMatrix &a, const double x)
Multiply all elements of matrix by { x}.
INLINE const double & a_no_check(int row, int col) const
const access with no bounds check, care recommend
EST_read_status load(const EST_String &filename)
load vector from file filename.
EST_DVector & operator*=(const EST_DVector &s)
elementwise multiply
int open(const EST_String &filename)
open a {EST_TokenStream} for a file.
int open_string(const EST_String &newbuffer)
open a {EST_TokenStream} for string rather than a file
void fill(const double &v)
fill matrix with value v
EST_DMatrix()
default constructor
EST_DMatrix & operator+=(const EST_DMatrix &a)
Add elements of 2 same sized matrices.
EST_DMatrix & operator*=(const double f)
elementwise multiply by scalar
void append(const T &item)
add item onto end of list
INLINE int length() const
number of items in vector.
void copyin(double **x, int rows, int cols)
Copy 2-d array { x} of size { rows x cols} into matrix.
int ival(const EST_String &rkey, int m=1) const
const EST_String & sval(const EST_String &rkey, int m=1) const
const EST_String pos_description()
A string describing current position, suitable for error messages.
INLINE int n() const
number of items in vector.
EST_Token get_upto_eoln(void)
get up to { s} in end of line as a single token.
EST_DMatrix & operator/=(const double f)
elementwise divide by scalar
EST_read_status load(const EST_String &filename)
Load from file (ascii or binary as defined in file)
void resize(int rows, int cols, int set=1)
resize matrix
EST_DMatrix & operator-=(const EST_DMatrix &a)
Subtract elements of 2 same sized matrices.
EST_write_status est_save(const EST_String &filename, const EST_String &type)
Save in file in est format.
const T & first() const
return const reference to first item in list
void resize(int n, int set=1)
resize vector
EST_read_status est_load(const EST_String &filename)
Load from file in est format (binary/ascii defined in file itself)