Edinburgh Speech Tools  2.1-release
EST_FMatrix.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Paul Taylor */
34 /* Date : April 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* Matrix Class for floats */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstdlib>
41 #include <cstdio>
42 #include <fstream>
43 #include <sstream>
44 #include <cmath>
45 #include <climits>
46 using namespace std;
47 
48 #include "EST_String.h"
49 #include "EST_types.h"
50 #include "EST_FileType.h"
51 #include "EST_Option.h"
52 #include "EST_FMatrix.h"
53 #include "EST_cutils.h" // for swap functions
54 #include "EST_Token.h"
55 
56 
57 /* EST_FVector may used as EST_Val */
59 
60 /* EST_FMatrix may used as EST_Val */
62 
63 EST_String EST_FMatrix::default_file_type = "est_ascii";
64 
66 :EST_TSimpleMatrix<float>(a.num_rows(), a.num_columns())
67 {
68  float vv = 0.0;
69  if (b < 0)
70  return;
71  if (b == 0)
72  fill(vv);
73 }
74 
76 {
77  ssize_t i, j;
78  if (a.num_columns() != num_columns())
79  {
80  cerr <<"Matrix addition error: bad number of columns\n";
81  return *this;
82  }
83  if (a.num_rows() != num_rows())
84  {
85  cerr <<"Matrix addition error: bad number of rows\n";
86  return *this;
87  }
88  for (i = 0; i < num_rows(); ++i)
89  for (j = 0; j < num_columns(); ++j)
90  a_no_check(i, j) += a.a_no_check(i,j);
91 
92  return *this;
93 }
94 
96 {
97  ssize_t i, j;
98  if (a.num_columns() != num_columns())
99  {
100  cerr <<"Matrix subtraction error: bad number of columns\n";
101  return *this;
102  }
103  if (a.num_rows() != num_rows())
104  {
105  cerr <<"Matrix subtraction error: bad number of rows\n";
106  return *this;
107  }
108  for (i = 0; i < num_rows(); ++i)
109  for (j = 0; j < num_columns(); ++j)
110  a_no_check(i, j) -= a.a_no_check(i,j);
111 
112  return *this;
113 }
114 
116 {
117 
118  ssize_t i,j;
119  for (i = 0; i < num_rows(); ++i)
120  for (j = 0; j < num_columns(); ++j)
121  a_no_check(i, j) *= f;
122 
123  return *this;
124 }
125 
127 {
128 
129  ssize_t i,j;
130  for (i = 0; i < num_rows(); ++i)
131  for (j = 0; j < num_columns(); ++j)
132  a_no_check(i, j) /= f;
133 
134  return *this;
135 }
136 
138 {
139  EST_FMatrix ab;
140  ssize_t i, j;
141  if (a.num_columns() != b.num_columns())
142  {
143  cerr <<"Matrix addition error: bad number of columns\n";
144  return ab;
145  }
146  if (a.num_rows() != b.num_rows())
147  {
148  cerr <<"Matrix addition error: bad number of rows\n";
149  return ab;
150  }
151  ab.resize(a.num_rows(), a.num_columns());
152  for (i = 0; i < a.num_rows(); ++i)
153  for (j = 0; j < a.num_columns(); ++j)
154  ab.a_no_check(i, j) = a.a_no_check(i, j) + b.a_no_check(i, j);
155 
156  return ab;
157 }
158 
160 {
161  EST_FMatrix ab;
162  ssize_t i, j;
163 
164  if (a.num_columns() != b.num_columns())
165  {
166  cerr <<"Matrix subtraction error: bad number of columns:" <<
167  a.num_columns() << " and " << b.num_columns() << endl;
168  return ab;
169  }
170  if (a.num_rows() != b.num_rows())
171  {
172  cerr <<"Matrix subtraction error: bad number of rows\n";
173  return ab;
174  }
175  ab.resize(a.num_rows(), a.num_columns());
176  for (i = 0; i < a.num_rows(); ++i)
177  for (j = 0; j < a.num_columns(); ++j)
178  ab.a_no_check(i, j) = a.a_no_check(i, j) - b.a_no_check(i, j);
179 
180  return ab;
181 }
182 
183 EST_FMatrix operator*(const EST_FMatrix &a, const float x)
184 {
185  EST_FMatrix b(a, 0);
186  ssize_t i, j;
187 
188  for (i = 0; i < a.num_rows(); ++i)
189  for (j = 0; j < a.num_columns(); ++j)
190  b.a_no_check(i,j) = a.a_no_check(i,j) * x;
191 
192  return b;
193 }
194 
195 int operator !=(const EST_FVector &fv1,
196  const EST_FVector &fv2)
197 {
198  ssize_t i;
199  if(fv1.length() != fv2.length())
200  return FALSE;
201  for(i=0;i<fv1.length();i++)
202  if(fv1.a_no_check(i) != fv2.a_no_check(i))
203  return FALSE;
204 
205  return TRUE;
206 }
207 
209 {
210  // treat the vector as a column vector
211  // multiply each row of the matrix in turn by the vector
212 
213  EST_FVector b;
214  b.resize(a.num_rows());
215 
216  if(a.num_columns() != v.n())
217  {
218  cerr <<"Matrix-vector multiplication error: matrix rows != vector size"
219  << endl;
220  return b;
221  }
222 
223  ssize_t i, j;
224  for (i = 0; i < a.num_rows(); ++i){
225  b[i] = 0.0;
226  for (j = 0; j < a.num_columns(); ++j)
227  b.a_no_check(i) += a.a_no_check(i,j) * v.a_no_check(j);
228  }
229  return b;
230 }
231 
233 {
234  EST_FVector ab;
235  ssize_t i;
236  if (a.length() != b.length())
237  {
238  cerr <<"Vector addition error: mismatched lengths\n";
239  return ab;
240  }
241 
242  ab.resize(a.length());
243  for (i = 0; i < a.length(); ++i)
244  ab.a_no_check(i) = a.a_no_check(i) + b.a_no_check(i);
245 
246  return ab;
247 }
248 
250 {
251  EST_FVector ab;
252  ssize_t i;
253  if (a.length() != b.length())
254  {
255  cerr <<"Vector subtraction error: mismatched lengths\n";
256  return ab;
257  }
258 
259  ab.resize(a.length());
260  for (i = 0; i < a.length(); ++i)
261  ab.a_no_check(i) = a.a_no_check(i) - b.a_no_check(i);
262 
263  return ab;
264 }
265 
266 
268 {
269  // treat the vector as a row vector
270  // multiply the vector by each column of the matrix in turn
271 
272  EST_FVector b;
273  b.resize(a.num_columns());
274 
275  if(a.num_columns() != v.n())
276  {
277  cerr <<"Matrix-vector multiplication error: matrix rows != vector size"
278  << endl;
279  return b;
280  }
281 
282  ssize_t i, j;
283  for (j = 0; j < a.num_columns(); ++j){
284  b[j] = 0.0;
285  for (i = 0; i < a.num_rows(); ++i)
286  b.a_no_check(i) += a.a_no_check(i,j) * v.a_no_check(j);
287  }
288  return b;
289 }
290 
291 
292 #if 0
293 EST_FMatrix operator/(const EST_FMatrix &a, float x)
294 {
295  return (a * (1/x));
296 }
297 #endif
298 
300 {
301  EST_FMatrix ab;
302  multiply(a,b,ab);
303  return ab;
304 }
305 
306 void multiply(const EST_FMatrix &a, const EST_FMatrix &b, EST_FMatrix &ab)
307 {
308 
309  if (a.num_columns() != b.num_rows())
310  {
311  cerr <<"Matrix multiply error: a.num_columns() != b.num_rows()\n";
312  return;
313  }
314 
315  ab.resize(a.num_rows(), b.num_columns());
316  ssize_t i, j, k, n;
317  n = a.num_columns(); // could also be b.num_rows()
318 
319  for (i = 0; i < a.num_rows(); ++i)
320  for (k = 0; k < b.num_columns(); ++k)
321  {
322  ab.a_no_check(i, k) = 0.0;
323  for (j = 0; j < n; ++j)
324  ab.a_no_check(i, k) +=
325  a.a_no_check(i, j) * b.a_no_check(j, k);
326  }
327 }
328 
329 void EST_FMatrix::copyin(float **inx, ssize_t rows, ssize_t cols)
330 {
331  ssize_t i, j;
332 
333  resize(rows, cols);
334 
335  for (i = 0; i < rows; ++i)
336  for (j = 0; j < cols; ++j)
337  a_no_check(i,j) = inx[i][j];
338 
339 }
340 
342  const EST_String &type)
343 {
344 
345  if ((type == "est_ascii") || (type == "est_binary"))
346  return est_save(filename,type);
347  else
348  { // the old stuff raw unheadered
349  ssize_t i, j;
350  ostream *outf;
351  if (filename == "-")
352  outf = &cout;
353  else
354  outf = new ofstream(filename);
355 
356  if (!(*outf))
357  {
358  cerr << "FMatrix: can't open file \"" << filename
359  <<"\" for writing" << endl;
360  return misc_write_error;
361  }
362 
363  for (i = 0; i < num_rows(); ++i)
364  {
365  for (j = 0; j < num_columns(); ++j)
366  *outf << a_no_check(i,j) << " ";
367  *outf << endl;
368  }
369 
370  if (outf != &cout)
371  delete outf;
372 
373  return write_ok;
374  }
375 }
376 
378  const EST_String &type)
379 {
380  // Binary save with short header for byte swap and sizes
381  ssize_t i,j;
382  FILE *fd;
383  std::stringstream tmpstring;
384  std::string tmpstring2;
385  if (filename == "-")
386  fd = stdout;
387  else if ((fd = fopen(filename, "wb")) == NULL)
388  {
389  cerr << "EST_FMatrix: binsave: failed to open \"" << filename <<
390  "\" for writing" << endl;
391  return misc_write_error;
392  }
393 
394  fprintf(fd,"EST_File fmatrix\n");
395  fprintf(fd,"version 1\n");
396  if (type == "est_binary")
397  {
398  fprintf(fd,"DataType binary\n");
399  if (EST_LITTLE_ENDIAN)
400  fprintf(fd,"ByteOrder LittleEndian\n");
401  else
402  fprintf(fd,"ByteOrder BigEndian\n");
403  }
404  else
405  fprintf(fd,"DataType ascii\n");
406  tmpstring.str("");
407  tmpstring << num_rows();
408  tmpstring2 = tmpstring.str();
409  fprintf(fd,"rows %s\n",tmpstring2.c_str());
410  tmpstring.str("");
411  tmpstring << num_columns();
412  tmpstring2 = tmpstring.str();
413  fprintf(fd,"columns %s\n",tmpstring2.c_str());
414 
415  fprintf(fd,"EST_Header_End\n");
416 
417  if (type == "est_binary")
418  {
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)
422  {
423  cerr << "EST_FMatrix: binsave: failed to write row "
424  << i << " column " << j
425  << " to \"" << filename << "\"" << endl;
426  if (fd != stdout) fclose(fd);
427  return misc_write_error;
428  }
429  }
430  else
431  { // est_ascii
432  for (i = 0; i < num_rows(); ++i)
433  {
434  for (j=0; j < num_columns(); j++)
435  fprintf(fd,"%f ",a_no_check(i,j));
436  fprintf(fd,"\n");
437  }
438  }
439 
440  if (fd != stdout)
441  fclose(fd);
442 
443  return write_ok;
444 }
445 
447 {
448  // ascii/binary load with short header for byte swap and sizes
449  ssize_t i,j,k;
450  int rows, cols, swap;
451  EST_TokenStream ts;
452  EST_read_status r;
453  EST_EstFileType t;
454  EST_Option hinfo;
455  bool ascii;
456 
457  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
458  {
459  cerr << "FMatrix: can't open fmatrix input file "
460  << filename << endl;
461  return misc_read_error;
462  }
463  if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
464  return r;
465  if (t != est_file_fmatrix)
466  return misc_read_error;
467  if (hinfo.ival("version") != 1)
468  {
469  cerr << "FMatrix load: " << ts.pos_description() <<
470  " wrong version of fmatrix format expected 1 but found " <<
471  hinfo.ival("version") << endl;
472  return misc_read_error;
473  }
474  rows = hinfo.ival("rows");
475  cols = hinfo.ival("columns");
476  resize(rows,cols);
477 
478  if (ascii)
479  { // an ascii file
480  for (i = 0; i < num_rows(); ++i)
481  {
482  for (j = 0; j < num_columns(); ++j)
483  a_no_check(i,j) = atof(ts.get().string());
484  if (!ts.eoln())
485  {
486  cerr << "FMatrix load: " << ts.pos_description() <<
487  " missing end of line at end of row " << i << endl;
488  return misc_read_error;
489  }
490  }
491  }
492  else
493  { // a binary file
494  float *buff;
495  if ((EST_BIG_ENDIAN && (hinfo.sval("ByteOrder")=="LittleEndian")) ||
496  (EST_LITTLE_ENDIAN && (hinfo.sval("ByteOrder") == "BigEndian")))
497  swap = TRUE;
498  else
499  swap = FALSE;
500 
501  buff = walloc(float,rows*cols);
502  // A single read is *much* faster than multiple reads
503  if (ts.fread(buff,sizeof(float),rows*cols) != rows*cols)
504  {
505  cerr << "EST_FMatrix: binload: short file in \""
506  << filename << "\"" << endl;
507  wfree(buff);
508  return misc_read_error;
509  }
510  if (swap)
511  swap_bytes_float(buff,rows*cols);
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++];
515  wfree(buff);
516  }
517 
518  ts.close();
519 
520  return read_ok;
521 }
522 
524 {
525  EST_read_status r;
526 
527  if ((r = est_load(filename)) == format_ok)
528  return r;
529  else if (r == wrong_format)
530  { // maybe its an ancient ascii file
531  EST_TokenStream ts, tt;
532  EST_StrList sl;
533  ssize_t i, j, n_rows=0, n_cols=0;
534  EST_String t;
535  EST_Litem *p;
536  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
537  {
538  cerr << "Can't open fmatrix file " << filename << endl;
539  return misc_read_error;
540  }
541  // set up the character constant values for this stream
542  ts.set_SingleCharSymbols(";");
543 
544  // first read in as list
545  for (n_rows = 0; !ts.eof(); ++n_rows)
546  sl.append(ts.get_upto_eoln().string());
547 
548  if (n_rows > 0)
549  {
550  tt.open_string(sl.first());
551  for (n_cols = 0; !tt.eof(); ++n_cols)
552  tt.get().string();
553  }
554 
555  // resize track and copy values in
556  resize(n_rows, n_cols);
557 
558  for (p = sl.head(), i = 0; p != 0; ++i, p = p->next())
559  {
560  tt.open_string(sl(p));
561  for (j = 0; !tt.eof(); ++j)
562  a_no_check(i,j) = atof(tt.get().string());
563  if (j != n_cols)
564  {
565  cerr << "Wrong number of points in row " << i << endl;
566  cerr << "Expected " << n_cols << " got " << j << endl;
567  return misc_read_error;
568  }
569  }
570  return format_ok;
571  }
572  else
573  return r;
574 }
575 
576 
578 {
579  ssize_t i;
580  if(n() != s.n()){
581  cerr << "Cannot elementwise add vectors of differing lengths"
582  << endl;
583  return *this;
584  }
585 
586  for (i = 0; i < n(); ++i)
587  (*this)[i] += s(i);
588 
589 
590  return *this;
591 }
592 
593 
595 {
596  if(n() != s.n()){
597  cerr << "Cannot elementwise multiply vectors of differing lengths"
598  << endl;
599  return *this;
600  }
601 
602  for (ssize_t i = 0; i < n(); ++i)
603  (*this)[i] *= s(i);
604 
605  return *this;
606 }
607 
609 {
610  for (ssize_t i = 0; i < n(); ++i)
611  (*this)[i] *= f;
612 
613  return *this;
614 }
615 
616 
618 {
619  for (ssize_t i = 0; i < n(); ++i)
620  (*this)[i] /= f;
621 
622  return *this;
623 }
624 
625 
626 
628 {
629  // ascii/binary load with short header for byte swap and sizes
630  ssize_t i,k;
631  int l, swap;
632  EST_TokenStream ts;
633  EST_read_status r;
634  EST_EstFileType t;
635  EST_Option hinfo;
636  bool ascii;
637 
638  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
639  {
640  cerr << "FVector: can't open FVector input file "
641  << filename << endl;
642  return misc_read_error;
643  }
644  if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok)
645  return r;
646  if (t != est_file_fvector)
647  return misc_read_error;
648  if (hinfo.ival("version") != 1)
649  {
650  cerr << "FVector load: " << ts.pos_description() <<
651  " wrong version of FVector format expected 1 but found " <<
652  hinfo.ival("version") << endl;
653  return misc_read_error;
654  }
655  l = hinfo.ival("length");
656  resize(l);
657 
658  if (ascii)
659  { // an ascii file
660  for (i = 0; i < length(); ++i)
661  a_no_check(i) = atof(ts.get().string());
662  }
663  else
664  { // a binary file
665  float *buff;
666  if ((EST_BIG_ENDIAN && (hinfo.sval("ByteOrder")=="LittleEndian")) ||
667  (EST_LITTLE_ENDIAN && (hinfo.sval("ByteOrder") == "BigEndian")))
668  swap = TRUE;
669  else
670  swap = FALSE;
671 
672  buff = walloc(float,l);
673  // A single read is *much* faster than multiple reads
674  if (ts.fread(buff,sizeof(float),l) != l)
675  {
676  cerr << "EST_FVector: binload: short file in \""
677  << filename << "\"" << endl;
678  wfree(buff);
679  return misc_read_error;
680  }
681  if (swap)
682  swap_bytes_float(buff,l);
683  for (k = i = 0; i < length(); ++i)
684  a_no_check(i) = buff[k++];
685  wfree(buff);
686  }
687 
688  ts.close();
689  return read_ok;
690 }
691 
693 {
694 
695  EST_read_status r;
696 
697  if ((r = est_load(filename)) == format_ok)
698  return r;
699  else if (r == wrong_format)
700  { // maybe its an ancient ascii file
701  EST_TokenStream ts;
702  EST_String s;
703  int i;
704 
705  i = 0;
706 
707  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
708  {
709  cerr << "can't open vector input file " << filename << endl;
710  return misc_read_error;
711  }
712  ts.set_SingleCharSymbols(";");
713 
714  while (!ts.eof())
715  {
716  ts.get();
717  ++i;
718  }
719  resize(i);
720 
721  ts.close();
722  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
723  {
724  cerr << "can't open vector input file " << filename << endl;
725  return misc_read_error;
726  }
727 
728  for (i = 0; !ts.eof(); ++i)
729  {
730  s = ts.get().string();
731  (*this)[i] = (float)(atof(s)); // actually returns double
732  }
733  ts.close();
734  return format_ok;
735  }
736  else
737  return r;
738 
739  return format_ok;
740 
741 }
742 
743 /*
744 
745 EST_read_status EST_FVector::load(EST_String &filename)
746 {
747  EST_TokenStream ts;
748  EST_String s;
749  int i;
750 
751  i = 0;
752 
753  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
754  {
755  cerr << "can't open vector input file " << filename << endl;
756  return misc_read_error;
757  }
758  ts.set_SingleCharSymbols(";");
759 
760  while (!ts.eof())
761  {
762  ts.get();
763  ++i;
764  }
765  resize(i);
766 
767  ts.close();
768  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
769  {
770  cerr << "can't open vector input file " << filename << endl;
771  return misc_read_error;
772  }
773 
774  for (i = 0; !ts.eof(); ++i)
775  {
776  s = ts.get().string();
777  (*this)[i] = atof(s);
778  }
779  ts.close();
780  return format_ok;
781 }
782 */
783 
784 // EST_read_status EST_DVector::load(EST_String &filename)
785 // {
786 // EST_TokenStream ts;
787 // EST_String s;
788 // int i;
789 
790 // i = 0;
791 
792 // if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
793 // {
794 // cerr << "can't open vector input file " << filename << endl;
795 // return misc_read_error;
796 // }
797 // ts.set_SingleCharSymbols(";");
798 
799 // while (!ts.eof())
800 // {
801 // ts.get();
802 // ++i;
803 // }
804 // resize(i);
805 
806 // ts.close();
807 // if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
808 // {
809 // cerr << "can't open vector input file " << filename << endl;
810 // return misc_read_error;
811 // }
812 
813 // for (i = 0; !ts.eof(); ++i)
814 // {
815 // s = ts.get().string();
816 // (*this)[i] = atof(s); // actually returns double
817 // }
818 // ts.close();
819 // return format_ok;
820 // }
821 
822 
823 float operator*(const EST_FVector &v1, const EST_FVector &v2)
824 {
825  // dot product
826 
827  float b=0;
828 
829  if(v1.length() != v2.length())
830  {
831  cerr <<"Vector dot product error: differing vector size"
832  << endl;
833  return b;
834  }
835 
836  ssize_t i;
837  for (i = 0; i < v1.length(); ++i)
838  b += v1.a_no_check(i) * v2.a_no_check(i);
839 
840  return b;
841 }
842 
843 
845  const EST_String &type)
846 {
847 
848  if ((type == "est_ascii") || (type == "est_binary"))
849  return est_save(filename,type);
850  else
851  { // the old stuff raw unheadered
852  ssize_t i;
853  ostream *outf;
854  if (filename == "-")
855  outf = &cout;
856  else
857  outf = new ofstream(filename);
858 
859  outf->precision(25);
860  if (!(*outf))
861  {
862  cerr << "FVector: can't open file \"" << filename
863  <<"\" for writing" << endl;
864  return misc_write_error;
865  }
866 
867  for (i = 0; i < length(); ++i)
868  *outf << a_no_check(i) << " ";
869  *outf << endl;
870 
871  if (outf != &cout)
872  delete outf;
873 
874  return write_ok;
875  }
876 }
877 
879  const EST_String &type)
880 {
881  // Binary save with short header for byte swap and sizes
882  ssize_t i;
883  FILE *fd;
884  std::stringstream tmpstring;
885  std::string tmpstring2;
886  if (filename == "-")
887  fd = stdout;
888  else if ((fd = fopen(filename, "wb")) == NULL)
889  {
890  cerr << "EST_FVector: binsave: failed to open \"" << filename <<
891  "\" for writing" << endl;
892  return misc_write_error;
893  }
894 
895  fprintf(fd,"EST_File fvector\n");
896  fprintf(fd,"version 1\n");
897  if (type == "est_binary")
898  {
899  fprintf(fd,"DataType binary\n");
900  if (EST_LITTLE_ENDIAN)
901  fprintf(fd,"ByteOrder LittleEndian\n");
902  else
903  fprintf(fd,"ByteOrder BigEndian\n");
904  }
905  else
906  fprintf(fd,"DataType ascii\n");
907 
908  tmpstring.str("");
909  tmpstring << length();
910  tmpstring2 = tmpstring.str();
911  fprintf(fd,"length %s\n", tmpstring2.c_str());
912  fprintf(fd,"EST_Header_End\n");
913 
914  if (type == "est_binary")
915  {
916  for (i = 0; i < length(); ++i)
917  if (fwrite(&a_no_check(i),sizeof(float),1,fd) != 1)
918  {
919  cerr << "EST_FVector: binsave: failed to write item "
920  << i << " to \"" << filename << "\"" << endl;
921  if (fd != stdout) fclose(fd);
922  return misc_write_error;
923  }
924  }
925  else
926  { // est_ascii
927  for (i = 0; i < length(); ++i)
928  fprintf(fd,"%.25f ",a_no_check(i));
929  fprintf(fd,"\n");
930  }
931 
932  if (fd != stdout)
933  fclose(fd);
934 
935  return write_ok;
936 }
937 
EST_FVector & operator/=(const float f)
elementwise divide by scalar
Definition: EST_FMatrix.cc:617
void copyin(float **x, ssize_t rows, ssize_t cols)
Copy 2-d array x of size rows x cols into matrix.
Definition: EST_FMatrix.cc:329
EST_TokenStream & get(EST_Token &t)
get next token in stream
Definition: EST_Token.cc:499
#define walloc(TYPE, SIZE)
Definition: EST_walloc.h:52
EST_FMatrix & operator*=(const float f)
elementwise multiply by scalar
Definition: EST_FMatrix.cc:115
EST_FVector & operator+=(const EST_FVector &s)
elementwise add
Definition: EST_FMatrix.cc:577
EST_write_status
The file was read in successfully.
EST_FMatrix operator-(const EST_FMatrix &a, const EST_FMatrix &b)
Definition: EST_FMatrix.cc:159
int fread(void *buff, int size, int nitems) EST_WARN_UNUSED_RESULT
Reading binary data, (don&#39;t use peek() immediately beforehand)
Definition: EST_Token.cc:368
EST_read_status load(const EST_String &filename)
load vector from file filename.
Definition: EST_FMatrix.cc:692
ssize_t num_columns() const
return number of columns
Definition: EST_TMatrix.h:179
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:119
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
EST_write_status save(const EST_String &filename, const EST_String &type)
save vector to file filename.
Definition: EST_FMatrix.cc:844
EST_EstFileType
Definition: EST_FileType.h:50
void set_SingleCharSymbols(const EST_String &sc)
set which characters are to be treated as single character symbols
Definition: EST_Token.h:344
void close(void)
Close stream.
Definition: EST_Token.cc:419
INLINE const T & a_no_check(ssize_t n) const
read-only const access operator: without bounds checking
Definition: EST_TVector.h:254
ssize_t num_rows() const
return number of rows
Definition: EST_TMatrix.h:177
int ssize_t
#define VAL_REGISTER_CLASS(NAME, CLASS)
Definition: EST_Val_defs.h:62
void multiply(const EST_FMatrix &a, const EST_FMatrix &b, EST_FMatrix &ab)
Definition: EST_FMatrix.cc:306
EST_UItem * next()
Definition: EST_UList.h:55
EST_write_status save(const EST_String &filename, const EST_String &type=EST_FMatrix::default_file_type)
Save in file (ascii or binary)
Definition: EST_FMatrix.cc:341
int open(const EST_String &filename)
open a EST_TokenStream for a file.
Definition: EST_Token.cc:213
int open_string(const EST_String &newbuffer)
open a EST_TokenStream for string rather than a file
Definition: EST_Token.cc:264
EST_FMatrix & operator-=(const EST_FMatrix &a)
Subtract elements of 2 same sized matrices.
Definition: EST_FMatrix.cc:95
void swap_bytes_float(float *data, int length)
Definition: EST_swapping.cc:59
int eof()
end of file
Definition: EST_Token.h:362
EST_FMatrix & operator/=(const float f)
elementwise divide by scalar
Definition: EST_FMatrix.cc:126
INLINE ssize_t length() const
number of items in vector.
Definition: EST_TVector.h:249
const EST_String & sval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:93
#define misc_write_error
The file was written successfully.
EST_FMatrix operator*(const EST_FMatrix &a, const float x)
Definition: EST_FMatrix.cc:183
#define wrong_format
EST_Complex operator/(const EST_Complex &z1, const EST_Complex &z2)
Definition: EST_Complex.cc:95
#define FALSE
Definition: EST_bool.h:119
EST_read_status read_est_header(EST_TokenStream &ts, EST_Option &hinfo, bool &ascii, EST_EstFileType &t)
Definition: est_file.cc:143
#define misc_read_error
NULL
Definition: EST_WFST.cc:55
EST_read_status load(const EST_String &filename)
Load from file (ascii or binary as defined in file)
Definition: EST_FMatrix.cc:523
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:152
f
Definition: EST_item_aux.cc:48
#define EST_LITTLE_ENDIAN
Definition: EST_cutils.h:71
EST_FVector & operator*=(const EST_FVector &s)
elementwise multiply
Definition: EST_FMatrix.cc:594
void append(const T &item)
add item onto end of list
Definition: EST_TList.h:196
EST_read_status
EST_write_status est_save(const EST_String &filename, const EST_String &type)
Definition: EST_FMatrix.cc:878
const EST_String & string() const
Definition: EST_Token.h:120
#define format_ok
const EST_String pos_description()
A string describing current position, suitable for error messages.
Definition: EST_Token.cc:882
#define EST_BIG_ENDIAN
Definition: EST_cutils.h:69
EST_Token get_upto_eoln(void)
get up to s in end of line as a single token.
Definition: EST_Token.cc:529
EST_UItem * head() const
Definition: EST_UList.h:97
INLINE const T & a_no_check(ssize_t row, ssize_t col) const
const access with no bounds check, care recommend
Definition: EST_TMatrix.h:182
EST_read_status est_load(const EST_String &filename)
Load from file in est format (binary/ascii defined in file itself)
Definition: EST_FMatrix.cc:627
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)
Definition: EST_FMatrix.cc:195
EST_FMatrix operator+(const EST_FMatrix &a, const EST_FMatrix &b)
Definition: EST_FMatrix.cc:137
void wfree(void *p)
Definition: walloc.c:131
#define TRUE
Definition: EST_bool.h:118
INLINE ssize_t n() const
number of items in vector.
Definition: EST_TVector.h:251
EST_read_status est_load(const EST_String &filename)
Load from file in est format (binary/ascii defined in file itself)
Definition: EST_FMatrix.cc:446
void resize(int n, int set=1)
resize vector
EST_FMatrix & operator+=(const EST_FMatrix &a)
Add elements of 2 same sized matrices.
Definition: EST_FMatrix.cc:75
EST_write_status est_save(const EST_String &filename, const EST_String &type)
Save in file in est format.
Definition: EST_FMatrix.cc:377
int eoln()
end of line
Definition: EST_Token.cc:832