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