Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : TSTSMAT.CPP

 
Output of file : TSTSMAT.CPP contained in archive : BLFMATH.ZIP
/////////////////////////////////////////////////////////////
// tstsmat.cpp: A test program for the SimpleMatrix class.
// Copyright(c) 1993 Azarona Software. All rights reserved.
/////////////////////////////////////////////////////////////
#include
#include
#include "simpmat.h"

template
void PrtMat(SimpleMatrix &m)
// Prints out all elements of a matrix.
// Tests subscripting operation as well.
{
for (int i = 0; i for (int j = 0; j cout << setw(3) << m[i][j] << ' ';
}
cout << '\n';
}
}

template
void SetVec(VecPtr &v, unsigned n, const TYPE &x)
// Sets all elements of a vector to x. Assumes
// TYPE as '=' defined. Uses fastest form of vector
// element access.
{
unsigned i;

for (i = 0; i *v = x;
v++;
}
}

template
void MatMult(SimpleMatrix &c, const SimpleMatrix &a, const SimpleMatrix &b)
// Does a matrix multiply.
{
unsigned i, j, k;

if (a.NCols() == b.NRows() && // A & B must conform
c.NCols() == b.NCols() && // C must be right size too
c.NRows() == a.NRows()) {
for (i = 0; i < a.NRows(); i++) {
for (j = 0; j < b.NCols(); j++) {
TYPE sum = 0;
for (k = 0; k < b.NRows(); k++) sum += a[i][k] * b[k][j];
c[i][j] = sum;
}
}
}
else {
cout << "Matrices don't conform\n";
}
}

// BC++ seems to need this prototype

void MatMult(SimpleMatrix &c, const SimpleMatrix &a, const SimpleMatrix &b);

template
SimpleMatrix operator*(const SimpleMatrix &a, const SimpleMatrix &b)
// Multiplies two matrices together and returns a resulting matrix.
// If matrices don't conform, a message is printed and the resulting
// matrix is set to all zeros.
{
SimpleMatrix r(a.NRows(), b.NCols()); // Create resulting matrix
MatMult(r, a, b);
return r; // Copy constructor called. Bad news.
}


template
SimpleMatrix Transpose(const SimpleMatrix &m)
// Returns the transpose of a matrix by interchanging
// rows and columns.
{
SimpleMatrix t(m.NCols(), m.NRows());

for (unsigned i = 0; i for (unsigned j = 0; j
return t; // Copy constructor called.
}


template
void TRYIT(const SimpleMatrix &m)
// Try updating a constant matrix.
{
unsigned i;
VecPtr v = m.ColPtr(0);

for (i = 0; i // *v = 99; Can't do this
v++;
}
}


main()
{
SimpleMatrix m(3, 3);

cout << "Setting all elements to 4's\n";
SetVec(m.PtrToAll(), m.NElems(), 4);
cout << "m:\n"; PrtMat(m); cout << '\n';

cout << "Setting row 1 to all 2's\n";
SetVec(m.RowPtr(1), m.NCols(), 2);
cout << "m:\n"; PrtMat(m); cout << '\n';

cout << "Setting column 1 to all 3's\n";
SetVec(m.ColPtr(3), m.NRows(), 3);
cout << "m:\n"; PrtMat(m); cout << '\n';

cout << "Setting diagonal to 1's\n";
SetVec(m.DiagPtr(), m.NRows(), 1);
cout << "m:\n"; PrtMat(m); cout << '\n';

// Do a matrix multiply with a 3x2 matrix

SimpleMatrix m2(3, 2), c(3,2); // Result is 3x2 as well

unsigned k = 0;
for(unsigned i = 0; i for (unsigned j = 0; j
cout << "m2:\n"; PrtMat(m2); cout << '\n';

cout << "Multiplying m by m2\n";
c = m * m2;
cout << "c = m x m2:\n"; PrtMat(c); cout << '\n';

// Transpose c
cout << "Transpose(c):\n"; PrtMat(Transpose(c)); cout << '\n';

TRYIT(m); // Try updating a constant matrix

return 0;
}


  3 Responses to “Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : TSTSMAT.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/