75 cerr <<
"Matrix addition error: bad number of columns\n";
80 cerr <<
"Matrix addition error: bad number of rows\n";
95 cerr <<
"Matrix subtraction error: bad number of columns\n";
100 cerr <<
"Matrix subtraction error: bad number of rows\n";
138 cerr <<
"Matrix addition error: bad number of columns\n";
143 cerr <<
"Matrix addition error: bad number of rows\n";
161 cerr <<
"Matrix subtraction error: bad number of columns:" <<
167 cerr <<
"Matrix subtraction error: bad number of rows\n";
205 cerr <<
"Matrix-vector multiplication error: matrix rows != vector size" 225 cerr <<
"Vector addition error: mismatched lengths\n";
230 for (i = 0; i < a.
length(); ++i)
242 cerr <<
"Vector subtraction error: mismatched lengths\n";
247 for (i = 0; i < a.
length(); ++i)
264 cerr <<
"Matrix-vector multiplication error: matrix rows != vector size" 298 cerr <<
"Matrix multiply error: a.num_columns() != b.num_rows()\n";
310 for (j = 0; j <
n; ++j)
322 for (i = 0; i < rows; ++i)
323 for (j = 0; j < cols; ++j)
332 if ((type ==
"est_ascii") || (type ==
"est_binary"))
341 outf =
new ofstream(filename);
346 cerr <<
"DMatrix: can't open file \"" << filename
347 <<
"\" for writing" << endl;
374 std::stringstream tmpstring;
375 std::string tmpstring2;
379 else if ((fd = fopen(filename,
"wb")) ==
NULL)
381 cerr <<
"EST_DMatrix: binsave: failed to open \"" << filename <<
382 "\" for writing" << endl;
386 fprintf(fd,
"EST_File dmatrix\n");
387 fprintf(fd,
"version 1\n");
388 if (type ==
"est_binary")
390 fprintf(fd,
"DataType binary\n");
392 fprintf(fd,
"ByteOrder LittleEndian\n");
394 fprintf(fd,
"ByteOrder BigEndian\n");
397 fprintf(fd,
"DataType ascii\n");
401 tmpstring2 = tmpstring.str();
402 fprintf(fd,
"rows %s\n", tmpstring2.c_str());
405 tmpstring2 = tmpstring.str();
406 fprintf(fd,
"columns %s\n", tmpstring2.c_str());
408 fprintf(fd,
"EST_Header_End\n");
410 if (type ==
"est_binary")
414 if (fwrite(&
a_no_check(i,j),
sizeof(
double),1,fd) != 1)
416 cerr <<
"EST_DMatrix: binsave: failed to write row " 417 << i <<
" column " << j
418 <<
" to \"" << filename <<
"\"" << endl;
419 if (fd != stdout) fclose(fd);
444 int rows, cols, swap;
451 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
453 cerr <<
"DMatrix: can't open DMatrix input file " 461 if (hinfo.
ival(
"version") != 1)
464 " wrong version of DMatrix format expected 1 but found " <<
465 hinfo.
ival(
"version") << endl;
468 rows = hinfo.
ival(
"rows");
469 cols = hinfo.
ival(
"columns");
481 " missing end of line at end of row " << i << endl;
495 buff =
walloc(
double,rows*cols);
497 if (ts.
fread(buff,
sizeof(
double),rows*cols) != rows*cols)
499 cerr <<
"EST_DMatrix: binload: short file in \"" 500 << filename <<
"\"" << endl;
506 for (k = i = 0; i <
num_rows(); ++i)
526 ssize_t i, j, n_rows=0, n_cols=0;
529 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
531 cerr <<
"Can't open DMatrix file " << filename << endl;
538 for (n_rows = 0; !ts.
eof(); ++n_rows)
544 for (n_cols = 0; !tt.
eof(); ++n_cols)
551 for (p = sl.
head(), i = 0; p != 0; ++i, p = p->
next())
554 for (j = 0; !tt.
eof(); ++j)
558 cerr <<
"Wrong number of points in row " << i << endl;
559 cerr <<
"Expected " << n_cols <<
" got " << j << endl;
582 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
584 cerr <<
"DVector: can't open DVector input file " 592 if (hinfo.
ival(
"version") != 1)
595 " wrong version of DVector format expected 1 but found " <<
596 hinfo.
ival(
"version") << endl;
599 l = hinfo.
ival(
"length");
604 for (i = 0; i <
length(); ++i)
618 if (ts.
fread(buff,
sizeof(
double),l) != l)
620 cerr <<
"EST_DVector: binload: short file in \"" 621 << filename <<
"\"" << endl;
627 for (k = i = 0; i <
length(); ++i)
651 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
653 cerr <<
"can't open vector input file " << filename << endl;
666 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
668 cerr <<
"can't open vector input file " << filename << endl;
672 for (i = 0; !ts.
eof(); ++i)
674 s = ts.
get().string();
675 (*this)[i] = atof(s);
692 cerr <<
"Cannot elementwise add vectors of differing lengths" 697 for (i = 0; i <
n(); ++i)
708 cerr <<
"Cannot elementwise multiply vectors of differing lengths" 731 cerr <<
"Can't do vector dot prod - differing vector sizes !" << endl;
755 if ((type ==
"est_ascii") || (type ==
"est_binary"))
764 outf =
new ofstream(filename);
769 cerr <<
"DVector: can't open file \"" << filename
770 <<
"\" for writing" << endl;
774 for (i = 0; i <
length(); ++i)
790 std::stringstream tmpstring;
791 std::string tmpstring2;
795 else if ((fd = fopen(filename,
"wb")) ==
NULL)
797 cerr <<
"EST_DVector: binsave: failed to open \"" << filename <<
798 "\" for writing" << endl;
802 fprintf(fd,
"EST_File dvector\n");
803 fprintf(fd,
"version 1\n");
804 if (type ==
"est_binary")
806 fprintf(fd,
"DataType binary\n");
808 fprintf(fd,
"ByteOrder LittleEndian\n");
810 fprintf(fd,
"ByteOrder BigEndian\n");
813 fprintf(fd,
"DataType ascii\n");
817 tmpstring2 = tmpstring.str();
818 fprintf(fd,
"length %s\n", tmpstring2.c_str());
819 fprintf(fd,
"EST_Header_End\n");
821 if (type ==
"est_binary")
823 for (i = 0; i <
length(); ++i)
824 if (fwrite(&
a_no_check(i),
sizeof(
double),1,fd) != 1)
826 cerr <<
"EST_DVector: binsave: failed to write item " 827 << i <<
" to \"" << filename <<
"\"" << endl;
828 if (fd != stdout) fclose(fd);
834 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
#define walloc(TYPE, SIZE)
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)
The file was read in successfully.
int fread(void *buff, int size, int nitems) EST_WARN_UNUSED_RESULT
Reading binary data, (don't use peek() immediately beforehand)
static EST_String default_file_type
ssize_t num_columns() const
return number of columns
int ival(const EST_String &rkey, int m=1) const
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(ssize_t n) const
read-only const access operator: without bounds checking
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.
void swap_bytes_double(double *data, int length)
ssize_t num_rows() const
return number of rows
const double & a(ssize_t row, ssize_t col) const
void copyin(double **x, ssize_t rows, ssize_t cols)
Copy 2-d array x of size rows x cols into matrix.
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
EST_DMatrix()
default constructor
INLINE ssize_t length() const
number of items in vector.
const EST_String & sval(const EST_String &rkey, int m=1) const
EST_DMatrix & operator+=(const EST_DMatrix &a)
Add elements of 2 same sized matrices.
The file was written successfully.
EST_Complex operator/(const EST_Complex &z1, const EST_Complex &z2)
void multiply(const EST_DMatrix &a, const EST_DMatrix &b, EST_DMatrix &ab)
EST_read_status read_est_header(EST_TokenStream &ts, EST_Option &hinfo, bool &ascii, EST_EstFileType &t)
A vector class for double precision floating point numbers. EST_DVector x should be used instead of f...
const T & first() const
return const reference to first item in list
#define EST_LITTLE_ENDIAN
EST_DMatrix & operator*=(const double f)
elementwise multiply by scalar
void append(const T &item)
add item onto end of list
EST_DMatrix operator-(const EST_DMatrix &a, const EST_DMatrix &b)
const EST_String & string() const
const EST_String pos_description()
A string describing current position, suitable for error messages.
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
INLINE const double & a_no_check(ssize_t row, ssize_t col) const
const access with no bounds check, care recommend
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
A subclass of EST_TMatrix which copies using memcopy.
EST_DMatrix & operator-=(const EST_DMatrix &a)
Subtract elements of 2 same sized matrices.
INLINE ssize_t n() const
number of items in vector.
EST_write_status est_save(const EST_String &filename, const EST_String &type)
Save in file in est format.
EST_DMatrix operator+(const EST_DMatrix &a, const EST_DMatrix &b)
void resize(int n, int set=1)
resize vector
EST_write_status est_save(const EST_String &filename, const EST_String &type)
EST_read_status est_load(const EST_String &filename)
Load from file in est format (binary/ascii defined in file itself)