80 cerr <<
"Matrix addition error: bad number of columns\n";
85 cerr <<
"Matrix addition error: bad number of rows\n";
88 for (i = 0; i < num_rows(); ++i)
89 for (j = 0; j < num_columns(); ++j)
100 cerr <<
"Matrix subtraction error: bad number of columns\n";
105 cerr <<
"Matrix subtraction error: bad number of rows\n";
108 for (i = 0; i < num_rows(); ++i)
109 for (j = 0; j < num_columns(); ++j)
119 for (i = 0; i < num_rows(); ++i)
120 for (j = 0; j < num_columns(); ++j)
121 a_no_check(i, j) *=
f;
130 for (i = 0; i < num_rows(); ++i)
131 for (j = 0; j < num_columns(); ++j)
132 a_no_check(i, j) /=
f;
143 cerr <<
"Matrix addition error: bad number of columns\n";
148 cerr <<
"Matrix addition error: bad number of rows\n";
166 cerr <<
"Matrix subtraction error: bad number of columns:" <<
172 cerr <<
"Matrix subtraction error: bad number of rows\n";
201 for(i=0;i<fv1.
length();i++)
218 cerr <<
"Matrix-vector multiplication error: matrix rows != vector size" 238 cerr <<
"Vector addition error: mismatched lengths\n";
243 for (i = 0; i < a.
length(); ++i)
255 cerr <<
"Vector subtraction error: mismatched lengths\n";
260 for (i = 0; i < a.
length(); ++i)
277 cerr <<
"Matrix-vector multiplication error: matrix rows != vector size" 311 cerr <<
"Matrix multiply error: a.num_columns() != b.num_rows()\n";
323 for (j = 0; j < n; ++j)
335 for (i = 0; i < rows; ++i)
336 for (j = 0; j < cols; ++j)
337 a_no_check(i,j) = inx[i][j];
345 if ((type ==
"est_ascii") || (type ==
"est_binary"))
346 return est_save(filename,type);
354 outf =
new ofstream(filename);
358 cerr <<
"FMatrix: can't open file \"" << filename
359 <<
"\" for writing" << endl;
363 for (i = 0; i < num_rows(); ++i)
365 for (j = 0; j < num_columns(); ++j)
366 *outf << a_no_check(i,j) <<
" ";
383 std::stringstream tmpstring;
384 std::string tmpstring2;
387 else if ((fd = fopen(filename,
"wb")) ==
NULL)
389 cerr <<
"EST_FMatrix: binsave: failed to open \"" << filename <<
390 "\" for writing" << endl;
394 fprintf(fd,
"EST_File fmatrix\n");
395 fprintf(fd,
"version 1\n");
396 if (type ==
"est_binary")
398 fprintf(fd,
"DataType binary\n");
400 fprintf(fd,
"ByteOrder LittleEndian\n");
402 fprintf(fd,
"ByteOrder BigEndian\n");
405 fprintf(fd,
"DataType ascii\n");
407 tmpstring << num_rows();
408 tmpstring2 = tmpstring.str();
409 fprintf(fd,
"rows %s\n",tmpstring2.c_str());
411 tmpstring << num_columns();
412 tmpstring2 = tmpstring.str();
413 fprintf(fd,
"columns %s\n",tmpstring2.c_str());
415 fprintf(fd,
"EST_Header_End\n");
417 if (type ==
"est_binary")
419 for (i = 0; i < num_rows(); ++i)
420 for (j=0; j < num_columns(); j++)
421 if (fwrite(&a_no_check(i,j),
sizeof(
float),1,fd) != 1)
423 cerr <<
"EST_FMatrix: binsave: failed to write row " 424 << i <<
" column " << j
425 <<
" to \"" << filename <<
"\"" << endl;
426 if (fd != stdout) fclose(fd);
432 for (i = 0; i < num_rows(); ++i)
434 for (j=0; j < num_columns(); j++)
435 fprintf(fd,
"%f ",a_no_check(i,j));
450 int rows, cols, swap;
457 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
459 cerr <<
"FMatrix: can't open fmatrix input file " 467 if (hinfo.
ival(
"version") != 1)
470 " wrong version of fmatrix format expected 1 but found " <<
471 hinfo.
ival(
"version") << endl;
474 rows = hinfo.
ival(
"rows");
475 cols = hinfo.
ival(
"columns");
480 for (i = 0; i < num_rows(); ++i)
482 for (j = 0; j < num_columns(); ++j)
483 a_no_check(i,j) = atof(ts.
get().string());
487 " missing end of line at end of row " << i << endl;
501 buff =
walloc(
float,rows*cols);
503 if (ts.
fread(buff,
sizeof(
float),rows*cols) != rows*cols)
505 cerr <<
"EST_FMatrix: binload: short file in \"" 506 << filename <<
"\"" << endl;
512 for (k = i = 0; i < num_rows(); ++i)
513 for (j = 0; j < num_columns(); ++j)
514 a_no_check(i,j) = buff[k++];
527 if ((r = est_load(filename)) ==
format_ok)
533 ssize_t i, j, n_rows=0, n_cols=0;
536 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
538 cerr <<
"Can't open fmatrix file " << filename << endl;
545 for (n_rows = 0; !ts.
eof(); ++n_rows)
551 for (n_cols = 0; !tt.
eof(); ++n_cols)
556 resize(n_rows, n_cols);
558 for (p = sl.
head(), i = 0; p != 0; ++i, p = p->
next())
561 for (j = 0; !tt.
eof(); ++j)
562 a_no_check(i,j) = atof(tt.
get().string());
565 cerr <<
"Wrong number of points in row " << i << endl;
566 cerr <<
"Expected " << n_cols <<
" got " << j << endl;
581 cerr <<
"Cannot elementwise add vectors of differing lengths" 586 for (i = 0; i < n(); ++i)
597 cerr <<
"Cannot elementwise multiply vectors of differing lengths" 602 for (
ssize_t i = 0; i < n(); ++i)
610 for (
ssize_t i = 0; i < n(); ++i)
619 for (
ssize_t i = 0; i < n(); ++i)
638 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
640 cerr <<
"FVector: can't open FVector input file " 648 if (hinfo.
ival(
"version") != 1)
651 " wrong version of FVector format expected 1 but found " <<
652 hinfo.
ival(
"version") << endl;
655 l = hinfo.
ival(
"length");
660 for (i = 0; i < length(); ++i)
661 a_no_check(i) = atof(ts.
get().string());
674 if (ts.
fread(buff,
sizeof(
float),l) != l)
676 cerr <<
"EST_FVector: binload: short file in \"" 677 << filename <<
"\"" << endl;
683 for (k = i = 0; i < length(); ++i)
684 a_no_check(i) = buff[k++];
697 if ((r = est_load(filename)) ==
format_ok)
707 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
709 cerr <<
"can't open vector input file " << filename << endl;
722 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
724 cerr <<
"can't open vector input file " << filename << endl;
728 for (i = 0; !ts.
eof(); ++i)
730 s = ts.
get().string();
731 (*this)[i] = (float)(atof(s));
831 cerr <<
"Vector dot product error: differing vector size" 837 for (i = 0; i < v1.
length(); ++i)
848 if ((type ==
"est_ascii") || (type ==
"est_binary"))
849 return est_save(filename,type);
857 outf =
new ofstream(filename);
862 cerr <<
"FVector: can't open file \"" << filename
863 <<
"\" for writing" << endl;
867 for (i = 0; i < length(); ++i)
868 *outf << a_no_check(i) <<
" ";
884 std::stringstream tmpstring;
885 std::string tmpstring2;
888 else if ((fd = fopen(filename,
"wb")) ==
NULL)
890 cerr <<
"EST_FVector: binsave: failed to open \"" << filename <<
891 "\" for writing" << endl;
895 fprintf(fd,
"EST_File fvector\n");
896 fprintf(fd,
"version 1\n");
897 if (type ==
"est_binary")
899 fprintf(fd,
"DataType binary\n");
901 fprintf(fd,
"ByteOrder LittleEndian\n");
903 fprintf(fd,
"ByteOrder BigEndian\n");
906 fprintf(fd,
"DataType ascii\n");
909 tmpstring << length();
910 tmpstring2 = tmpstring.str();
911 fprintf(fd,
"length %s\n", tmpstring2.c_str());
912 fprintf(fd,
"EST_Header_End\n");
914 if (type ==
"est_binary")
916 for (i = 0; i < length(); ++i)
917 if (fwrite(&a_no_check(i),
sizeof(
float),1,fd) != 1)
919 cerr <<
"EST_FVector: binsave: failed to write item " 920 << i <<
" to \"" << filename <<
"\"" << endl;
921 if (fd != stdout) fclose(fd);
927 for (i = 0; i < length(); ++i)
928 fprintf(fd,
"%.25f ",a_no_check(i));
EST_FVector & operator/=(const float f)
elementwise divide by scalar
void copyin(float **x, ssize_t rows, ssize_t cols)
Copy 2-d array x of size rows x cols into matrix.
EST_TokenStream & get(EST_Token &t)
get next token in stream
#define walloc(TYPE, SIZE)
EST_FMatrix & operator*=(const float f)
elementwise multiply by scalar
EST_FVector & operator+=(const EST_FVector &s)
elementwise add
The file was read in successfully.
EST_FMatrix operator-(const EST_FMatrix &a, const EST_FMatrix &b)
int fread(void *buff, int size, int nitems) EST_WARN_UNUSED_RESULT
Reading binary data, (don't use peek() immediately beforehand)
EST_read_status load(const EST_String &filename)
load vector from file filename.
ssize_t num_columns() const
return number of columns
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
int ival(const EST_String &rkey, int m=1) const
EST_write_status save(const EST_String &filename, const EST_String &type)
save vector to file filename.
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
ssize_t num_rows() const
return number of rows
#define VAL_REGISTER_CLASS(NAME, CLASS)
void multiply(const EST_FMatrix &a, const EST_FMatrix &b, EST_FMatrix &ab)
EST_write_status save(const EST_String &filename, const EST_String &type=EST_FMatrix::default_file_type)
Save in file (ascii or binary)
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_FMatrix & operator-=(const EST_FMatrix &a)
Subtract elements of 2 same sized matrices.
void swap_bytes_float(float *data, int length)
EST_FMatrix & operator/=(const float f)
elementwise divide by scalar
INLINE ssize_t length() const
number of items in vector.
const EST_String & sval(const EST_String &rkey, int m=1) const
The file was written successfully.
EST_FMatrix operator*(const EST_FMatrix &a, const float x)
EST_Complex operator/(const EST_Complex &z1, const EST_Complex &z2)
EST_read_status read_est_header(EST_TokenStream &ts, EST_Option &hinfo, bool &ascii, EST_EstFileType &t)
EST_read_status load(const EST_String &filename)
Load from file (ascii or binary as defined in file)
const T & first() const
return const reference to first item in list
#define EST_LITTLE_ENDIAN
EST_FVector & operator*=(const EST_FVector &s)
elementwise multiply
void append(const T &item)
add item onto end of list
EST_write_status est_save(const EST_String &filename, const EST_String &type)
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.
INLINE const T & a_no_check(ssize_t row, ssize_t col) const
const access with no bounds check, care recommend
EST_read_status est_load(const EST_String &filename)
Load from file in est format (binary/ascii defined in file itself)
void resize(int rows, int cols, int set=1)
resize matrix
A subclass of EST_TMatrix which copies using memcopy.
int operator!=(const EST_FVector &fv1, const EST_FVector &fv2)
EST_FMatrix operator+(const EST_FMatrix &a, const EST_FMatrix &b)
INLINE ssize_t n() const
number of items in vector.
EST_read_status est_load(const EST_String &filename)
Load from file in est format (binary/ascii defined in file itself)
void resize(int n, int set=1)
resize vector
EST_FMatrix & operator+=(const EST_FMatrix &a)
Add 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.