137 lines
3.5 KiB
Plaintext
137 lines
3.5 KiB
Plaintext
@q $Id: twod_matrix.cweb 148 2005-04-19 15:12:26Z kamenik $ @>
|
|
@q Copyright 2004, Ondra Kamenik @>
|
|
|
|
@ Start of {\tt twod\_matrix.cpp} file.
|
|
@c
|
|
#include "twod_matrix.h"
|
|
#include "tl_exception.h"
|
|
|
|
|
|
@<|ConstTwoDMatrix| constructors@>;
|
|
@<|ConstTwoDMatrix::writeMat4| code@>;
|
|
@<|TwoDMatrix| row methods code@>;
|
|
@<|TwoDMatrix| column methods code@>;
|
|
@<|TwoDMatrix::save| code@>;
|
|
@<|Mat4Header| constructor 1 code@>;
|
|
@<|Mat4Header| constructor 2 code@>;
|
|
@<|Mat4Header::write| code@>;
|
|
|
|
@
|
|
@<|ConstTwoDMatrix| constructors@>=
|
|
ConstTwoDMatrix::ConstTwoDMatrix(const TwoDMatrix& m)
|
|
: ConstGeneralMatrix(m)@+ {}
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(const TwoDMatrix& m, int first_col, int num)
|
|
: ConstGeneralMatrix(m, 0, first_col, m.nrows(), num)@+ {}
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(const ConstTwoDMatrix& m, int first_col, int num)
|
|
: ConstGeneralMatrix(m, 0, first_col, m.nrows(), num)@+ {}
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(int first_row, int num, const TwoDMatrix& m)
|
|
: ConstGeneralMatrix(m, first_row, 0, num, m.ncols())@+ {}
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(int first_row, int num, const ConstTwoDMatrix& m)
|
|
: ConstGeneralMatrix(m, first_row, 0, num, m.ncols())@+ {}
|
|
|
|
@
|
|
@<|ConstTwoDMatrix::writeMat4| code@>=
|
|
void ConstTwoDMatrix::writeMat4(FILE* fd, const char* vname) const
|
|
{
|
|
Mat4Header header(*this, vname);
|
|
header.write(fd);
|
|
for (int j = 0; j < ncols(); j++)
|
|
for (int i = 0; i < nrows(); i++)
|
|
fwrite(&(get(i,j)), sizeof(double), 1, fd);
|
|
}
|
|
|
|
@
|
|
@<|TwoDMatrix| row methods code@>=
|
|
void TwoDMatrix::copyRow(int from, int to)
|
|
{
|
|
if (from != to)
|
|
copyRow(ConstTwoDMatrix(*this), from, to);
|
|
}
|
|
|
|
void TwoDMatrix::copyRow(const ConstTwoDMatrix& m, int from, int to)
|
|
{
|
|
ConstVector fr_row(from, m);
|
|
Vector to_row(to, *this);
|
|
to_row = fr_row;
|
|
}
|
|
|
|
void TwoDMatrix::addRow(double d, const ConstTwoDMatrix& m, int from, int to)
|
|
{
|
|
ConstVector fr_row(from, m);
|
|
Vector to_row(to, *this);
|
|
to_row.add(d, fr_row);
|
|
}
|
|
|
|
|
|
@
|
|
@<|TwoDMatrix| column methods code@>=
|
|
void TwoDMatrix::copyColumn(int from, int to)
|
|
{
|
|
if (from != to)
|
|
copyColumn(ConstTwoDMatrix(*this), from, to);
|
|
}
|
|
|
|
void TwoDMatrix::copyColumn(const ConstTwoDMatrix& m, int from, int to)
|
|
{
|
|
ConstVector fr_col(m, from);
|
|
Vector to_col(*this, to);
|
|
to_col = fr_col;
|
|
}
|
|
|
|
void TwoDMatrix::addColumn(double d, const ConstTwoDMatrix& m, int from, int to)
|
|
{
|
|
ConstVector fr_col(m, from);
|
|
Vector to_col(*this, to);
|
|
to_col.add(d, fr_col);
|
|
}
|
|
|
|
@
|
|
@<|TwoDMatrix::save| code@>=
|
|
void TwoDMatrix::save(const char* fname) const
|
|
{
|
|
FILE* fd;
|
|
if (NULL==(fd = fopen(fname,"w"))) {
|
|
TL_RAISE("Cannot open file for writing in TwoDMatrix::save");
|
|
}
|
|
for (int row = 0; row < nrows(); row++) {
|
|
for (int col = 0; col < ncols(); col++)
|
|
fprintf(fd, " %20.10g", get(row, col));
|
|
fprintf(fd, "\n");
|
|
}
|
|
fclose(fd);
|
|
}
|
|
|
|
@ This constructs a MAT-4 header for Little Endian dense real double matrix.
|
|
@<|Mat4Header| constructor 1 code@>=
|
|
Mat4Header::Mat4Header(const ConstTwoDMatrix& m, const char* vn)
|
|
: type(0), rows(m.nrows()), cols(m.ncols()), imagf(0), namelen(strlen(vn)+1),
|
|
vname(vn)
|
|
{}
|
|
|
|
|
|
@ This constructs a MAT-4 header for text matrix.
|
|
@<|Mat4Header| constructor 2 code@>=
|
|
Mat4Header::Mat4Header(const ConstTwoDMatrix& m, const char* vn, const char* dummy)
|
|
: type(1), rows(m.nrows()), cols(m.ncols()), imagf(0), namelen(strlen(vn)+1),
|
|
vname(vn)
|
|
{}
|
|
|
|
|
|
@
|
|
@<|Mat4Header::write| code@>=
|
|
void Mat4Header::write(FILE* fd) const
|
|
{
|
|
fwrite(&type, sizeof(int), 1, fd);
|
|
fwrite(&rows, sizeof(int), 1, fd);
|
|
fwrite(&cols, sizeof(int), 1, fd);
|
|
fwrite(&imagf, sizeof(int), 1, fd);
|
|
fwrite(&namelen, sizeof(int), 1, fd);
|
|
fwrite(vname, 1, namelen, fd);
|
|
}
|
|
|
|
|
|
@ End of {\tt twod\_matrix.cpp} file. |