69,371
社区成员
发帖
与我相关
我的任务
分享
//FileName: lq.h
//Author: Feng Wang
//Date: Tue Mar 25 13:51:02 CST 2008
//Comment: Resolve the linear equation
//Last Modify:
//
#ifndef _LQ_H
# define _LQ_H
#include "det.h"
#include <vector>
#include <cassert>
#include <cstdlib>
#ifdef DEBUG
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
#endif
//
//for solution of the linear equation
// A X = B
//
template<class T>
void lq ( const std :: vector<T>& a, //store A[size][size]
const std :: vector<T>& b, //store B[size]
const unsigned int& order, //store size
std :: vector<T>& result //store X[size]
)
{
#ifdef DEBUG
cout << "\n>>Currently processing function lq()."
<< "\nThe order is " << order;
cout << "\nThe incoming vector a is \n";
copy( a.begin(), a.end(), ostream_iterator<T>( cout, " ") );
cout << "\nThe incoming vector b is \n";
copy( b.begin(), b.end(), ostream_iterator<T>( cout, " ") );
#endif
unsigned int Order = order;
//if order is set to default, calculate order
if ( 0 == Order )
{
Order = b.size();
}
assert ( Order * Order == a.size() );
assert ( Order == b.size() );
result.clear();
const T _D = det ( a, Order ); //store |D|
#ifdef DEBUG
cout << "\nThe base vector det is " << _D;
#endif
if ( 0 == _D )
{
if ( "Failed to solve the linear equation" )
{
#ifdef DEBUG
cout << "\n the det of vector is 0, failed to solve the equation.";
#endif
exit ( EXIT_FAILURE );
}
}
//change colum to get D[i]
//then solve it
for ( unsigned int i = 0; i < Order; ++i )
{
std :: vector<T> A = a;
for ( unsigned int j = 0; j < Order; ++j )
{
A[i+j*Order] = b[j];
}
const T D = det ( A, Order ); //store D[i]
result.push_back ( D / _D ); //get X[i]
#ifdef DEBUG
cout << "\nThe base vector det is " << _D;
cout << "\nThe current vector det is " << D;
cout << "\nresult[" << i << "] calculated, whose value is " << D / _D;
#endif
}
#ifdef DEBUG
cout << "\n<<Stride out of fucntion lq()";
#endif
}
#endif //_LQ_H
//FileName: det.h
//Author: Feng Wang
//Date: Mon Mar 24 21:25:15 CST 2008
//Comment: return the determinant of a square matrix
//Last Modify: Tue Mar 25 13:55:44 CST 2008
#ifndef _DET_H
# define _DET_H
#include <cassert>
#include <vector>
#include <cmath>
//
//USAGE:
// #include "../algorithm/ran.h"
// ......
// vector<long double> s;
// const unsigned int N = 3;
//
// for ( unsigned int i = 0; i < N; ++i )
// for ( unsigned int k = 0; k < N ;++k )
// s.push_back( _ran() );
// long double determinant = det( s, N );
// ......
//
//return the determinant of a square matrix arr whose size is order by order
template< class T >
T det ( const std :: vector<T>& arr, const unsigned int& order = 0 )
{
unsigned int Order = order;
//if order is set to default, calculate it
if ( 0 == Order )
{
Order = sqrt ( arr.size() );
}
assert ( Order * Order == arr.size() );
if ( 1 == Order )
return ( arr[0] ) ;
T sum = 0;
std ::vector<T> arr2 ;
int sign = 1;
for ( unsigned int i = 0 ; i < Order ; ++i, sign *= -1 )
{
/* copy n-1 by n-1 array into another array */
const unsigned int newsize = ( Order - 1 ) * ( Order - 1 ) ;
arr2.resize ( newsize );
for ( unsigned int j = 1 ; j < Order ; ++j )
{
for ( unsigned int k = 0, count = 0 ; k < Order ; ++k )
{
if ( k == i )
continue ;//jump out of k loop
const unsigned int pos = j * Order + k ;
const unsigned int newpos = ( j - 1 ) *
( Order - 1 ) + count ;
arr2[newpos] = arr[pos] ;
count++ ;
}//end of k loop
}//end of j loop
/* find determinant value of n-1 by n-1 array and add it to sum */
sum += arr[i] * sign * det ( arr2, Order - 1 ) ;
}
return sum;
}
#endif //_DET_H
第一式-第二式
0=a+b*y+c*z+d*w;
同除b:
0=a+y+c*z+d*w;
第三式-第四式
0=e+f*y+g*z+h*w;
同除b:
0=e+y+g*z+h*w;
一直做下去!