?? ho-kashyap.cpp
字號:
// Ho-Kashyap.cpp : 定義控制臺應用程序的入口點。
//
#include "stdafx.h"
#include "Matrix.h"
#include <iostream>
#include <iomanip>
#define N 4 // 模式數目
#define n 2 // 模式維數,在算法中常見為n+1,經歷了模式增廣
inline Matrix Pinv(const Matrix A); // 求矩陣的偽逆
bool LessThan0(const Matrix A); // 判斷是否矩陣有元素均小于等于0
Matrix Abs(const Matrix A); // 將矩陣的每一元素均取絕對值
/*************************************************************
// fuction name: _tmain
// description: 控制臺主函數
該函數是 Ho-Kashyap 算法的具體實現
// 注:本算法的實現使用的下載的矩陣類 Matrix ,其中包括了矩陣的大部分操作。
*************************************************************/
int _tmain(int argc, _TCHAR* argv[])
{
// 變量聲明
Matrix X(N, n+1); // 模式的增廣矩陣 N*(n+1)維
Matrix Xplus(n+1, N); // 偽逆矩陣 (n+1)*N維
Matrix b(N, 1); // 余量矢量b N 維
double rho = 1.0; // 常熟rho 數值
int k = 0; // 步數 數值
Matrix w(n+1, 1); // 解向量w n+1維
Matrix e(N, 1); // 誤差矢量 N維
// step1:
double x[N][n+1] = {
0, 0, 1,
0, 1, 1,
-1, 0, -1,
-1, -1, -1};
X = Matrix(&x[0][0], &x[N-1][n], N, n+1);
Xplus = Pinv(X);
// 條件輸出
cout << endl << endl;
cout << "****************** Ho-Kashyap 算法演示程序 **************************" << endl << endl;
cout << "該演示程序模式增廣矩陣為:" << endl;
cout << setprecision(2);
cout << setiosflags(ios::fixed);
for(int i=0;i<N;i++)
{
cout << "\t[";
for(int j =0;j<n;j++)
cout << setw(5) << x[i][j] << ", ";
cout << setw(5) << x[i][n] << "]" << endl;
}
{
int r = Xplus.Row(), c = Xplus.Col();
cout << endl << "偽逆矩陣為:" << endl;
for(int i=0;i<r;i++)
{
cout << "\t[";
for(int j=0;j<c-1;j++)
cout << setw(5) << Xplus[i][j] << ", ";
cout << setw(5) << Xplus[i][n] << "]" << endl;
}
}
// step2:
b = Matrix(N, 1, 1.0); // 取b(0) = (1,1,1)'
rho = 1.0; // 取rho = 1.0
k = 0; // 步數
w = Xplus * b;
bool keep = true; // 循環標識
do
{
// step3:
e = X * w - b;
// step4:
if(!LessThan0(e)) // e(k) !<= 0 goto step5
{
// step5:
w = w + rho * Xplus * Abs(e);
b = b + rho * (e + Abs(e));
}
else // e(k) = 0 或負的分量停止變為正值或各分量均為負值,停止。
keep = false;
// step6:
k++;
}while(keep);
// 輸出結果
bool first = false;
cout << endl << "經過" << k << "次迭代,得判別界面為:" << endl;
cout << "\td(x) =";
for (int i=0;i<n+1;i++)
if(w[i][0] != 0)
{
if(first)
if(w[i][0] > 0)
cout << " + " << w[i][0];
else
cout << " - " << -w[i][0];
else
{
cout << " " << w[i][0];
first = true;
}
if(i < n)
cout << "x" << i+1;
}
cout << endl << endl << "*********感謝使用該 ISODATA 算法演示程序*******" << endl;
system("pause");
return 0;
}
/*************************************************************
// fuction name: Pinv
// description: 求矩陣的偽逆
// input variable: Matrix A
// output variable:Matrix 矩陣A的偽逆
*************************************************************/
Matrix Pinv(const Matrix A)
{
return ( A.Transpose() * A ).Converse() * A.Transpose();
}
/*************************************************************
// fuction name: LessThan0
// description: 判斷是否矩陣有元素均小于等于0
// input variable: Matrix A
// output variable:bool,當矩陣A中有元素小于等于0時,返回true;矩陣A中元素均大于0時,返回false.
*************************************************************/
bool LessThan0(const Matrix A)
{
if(A.Col() != 1)
throw exception("this is no 1 dimension matrix.");
int k = A.Row();
bool b = false;
for(int i=0;i<k;i++)
{
if(A[i][0] <= 0)
b = true;
}
return b;
}
/*************************************************************
// fuction name: Abs
// description: 將矩陣的每一元素均取絕對值0
// input variable: Matrix A
// output variable:Matrix,返回將矩陣A的每一元素均取絕對值后的同維數矩陣。
*************************************************************/
Matrix Abs(const Matrix A)
{
Matrix B(A.Col(), A.Row());
for (int i=0; i<A.Row(); i++)
for (int j=0; j<A.Col(); j++)
B[i][j] = abs(A[i][j]);
return B;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -