📄 example_main.cpp
字号:
/**
\file example_main.cpp
\brief An example program using the Zenautics Matrix.
\author Glenn D. MacGougan (GDM)
\date 2007-12-13
\version 1.10
\b LICENSE \b INFORMATION \n
Copyright (c) 2007, Glenn D. MacGougan, Zenautics Technologies Inc. \n
Redistribution pertains only to the following files and their contents. \n
- Matrix.h\n
- Matrix.cpp\n
- cmatrix.h\n
- cmatrix_basic.lib (for windows), cmatrix_basic_lib.a (for linux)\n
Redistribution and use in source and binary forms, with or without
modification, of the specified files is permitted provided the following
conditions are met: \n
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer. \n
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. \n
- The name(s) of the contributor(s) may not be used to endorse or promote
products derived from this software without specific prior written
permission. \n
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
\b NOTES: \n
This code was developed using rigourous unit testing for every function
and operation. Despite any rigorous development process, bugs are
inevitable. Please report bugs and suggested fixes to glenn@zenautics.com.\n
*/
#include <iostream>
#include <string>
#include <complex>
#include "Matrix.h"
using namespace Zenautics;
using namespace std;
#ifndef PI
#define PI (3.1415926535897932384626433832795)
#endif
int main( int argc, char* argv[] )
{
try
{
bool result;
char buffer[1024]; // A character buffer.
unsigned i;
double re;
double im;
Matrix A(2,2); // Create a 2x2 matrix.
// Fill A with real data.
A[0][0] = 1.0;
A[0][1] = 3.0;
A[1][0] = -2.0;
A[1][1] = 4.0;
// Output A to stdout.
A.PrintToBuffer( buffer, 1024, 4 );
cout << "A =\n" << buffer << endl;
// or use the PrintStdout function.
cout << "A =" << endl;
A.PrintStdout( 4 );
// Get the inverse of A and output to stdout.
Matrix B = A.Inverse();
cout << "inverse(A) ="<< endl;
B.PrintStdout( 4 );
result = A.GetDeterminant(re,im); // im will be zero since A is real.
cout << "determinant(A) = \n" << re << endl;
// Resize A to a 3x1 vector .
A.Resize( 3 ); // Equivalently A.Resize(1,3) is a vector and is accessed the same way as below.
A[0] = 1.0;
A[1] = 2.0;
A[2] = 3.0;
// Output A to stdout.
cout << "A =" << endl;
A.PrintStdout( 4 );
// Set A using complex values.
complex<double> c1(1.0,1.0);
complex<double> c2(-2.0,-2.0);
complex<double> c3(3.0,3.0);
A(0) = c1; // A is now a complex vector.
A(1) = c2;
A(2) = c3;
// Output A and its transpose to stdout.
cout << "A ="<< endl;
A.PrintStdout( 4 );
Matrix AT = A.Transpose();
cout << "transpose(A) =" << endl;
AT.PrintStdout(4);
// Demonstrate A as a complex matrix.
A.Resize(2,2);
A(0,0) = c1; // Set a matrix element to a complex value.
A(0,1) = c2;
A(1,0) = c2;
A(1,1) = 10.0; // Set a matrix element to a double value.
cout << "A =" << endl;
A.PrintStdout(4);
re = A(0,0).real();
im = A(1,1).imag();
A.Redim(3,3); // Change the dimensions of A to 3x3 and retain previous data.
cout << "A =" << endl;
A.PrintStdout(4);
// Set a matrix using a matrix string.
A = "[1 2; 3 4]";
cout << "A =" << endl;
A.PrintStdout(4);
// Change A back to a row vector (previous data is not retained, it's initialized to zero).
A.Resize(512);
// Set A to a complex tone sinusoid.
for( i = 0; i < A.GetNrRows(); i++ )
{
complex<double> cplx( cos(i*2*PI/512.0), sin(i*2*PI/512.0) );
A(i) = cplx;
}
// Take the fft of A inplace
A.Inplace_FFT(); // Perform an fft of the columns in A. A is now complex.
std::complex<double> cplxResult;
cplxResult = A(1); // Get a complex value from the matrix.
re = A[1];
im = A(1).imag();
// Output A to a the specified ASCII file name.
A.Print( "A.txt", 6 );
// Output A to a the specified ASCII file name.
A.PrintDelimited( "A.csv", 6, ',' );
// Output A to a compressed binary matrix file.
// This only works with the ADVANCED edition.
result = A.Save("A.mtx");
// Read real or complex data from an ascii comma delimited file.
result = A.ReadFromFile( "A.csv" );
// Read data from an ascii white space delimited file.
result = A.ReadFromFile( "A.txt" );
// Read data from a binary .mtx format file.
// This works for both the BASIC and ADVANCED editions.
result = A.ReadFromFile( "A.mtx");
// Force an exception with invalid index usage.
cout << "Causing an intentional exception to be thrown." << endl;
double q = A(2000,1).real(); // This will cause an exception.
double r = A[2000][1]; // This will cause an exception.
double s = A[2000]; // This will cause an exception.
double t = A(2000).real(); // This will cause an exception.
}
catch( MatrixException& matrix_exception )
{
cout << matrix_exception << endl;
}
catch ( ... )
{
cout << "Caught unknown exception" << endl;
}
char anykey;
cout << "You have successfully executed the Zenautics Matrix Project example progam.\n" << endl;
cout << "Type something and hit enter: " << endl;
cin >> anykey;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -