#pragma once
#include "DataManager.h"
#include "DotNetUtilities.h"
#include <queue>
#include <stack>
#include <math.h>
#include <string>
typedef struct {
double ans = 0;
double x = 0;
}function;
typedef struct {
double x2 = 0;
double y2 = 0;
double xy = 0;
double x = 0;
double y = 0;
double x2y2 = 0;
double x2y = 0;
double xy2 = 0;
double normal = 0;
double x05 = 0;
double x051 = 0;
double x151 = 0;
}function_numbers;
function_numbers newton_function_number;
std::vector<std::string> equations;
namespace Optimization {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// MyForm ™∫∫K≠n
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
dataManager = new DataManager();
//0
//TODO: ¶b¶π•[§J´ÿ∫c®Á¶°µ{¶°ΩX
//
}
protected:
/// <summary>
DataManager* dataManager;
/// ≤M∞£•Ù¶Û®œ•Œ§§™∫∏Í∑Ω°C
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel1;
protected:
private: System::Windows::Forms::FlowLayoutPanel^ flowLayoutPanel1;
private: System::Windows::Forms::Label^ InputText;
private: System::Windows::Forms::TextBox^ Input;
private: System::Windows::Forms::Label^ OutputText;
private: System::Windows::Forms::TextBox^ Output;
private: System::Windows::Forms::MenuStrip^ menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ loadEquationsToolStripMenuItem;
private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
private:
/// <summary>
/// ≥]≠p§u®„©“ª›™∫≈‹º∆°C
/// </summary>
String^ userInput;
bool newton1 = false;
bool choose_function = false;
bool enter_initial=false;
int one_or_two = 0;
array <String ^> ^newton_interval;
int function_number = 0;
bool Conjugate = false;
double conjugate_interval_start,conjugate_interval_end;
array<String ^> ^conjugate_initial;
bool conjugate_enter_interval;
bool conjugate_choose_function;
bool Quasi_choose_function = false;
bool Quasi_enter_interval = false;
bool Quasi = false;
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// ¶π¨∞≥]≠p§u®„§‰¥©©“ª›™∫§Ë™k - Ω–§≈®œ•Œµ{¶°ΩXΩsøËæπ≠◊ßÔ
/// ≥o≠”§Ë™k™∫§∫Æe°C
/// </summary>
void InitializeComponent(void)
{
this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
this->flowLayoutPanel1 = (gcnew System::Windows::Forms::FlowLayoutPanel());
this->InputText = (gcnew System::Windows::Forms::Label());
this->Input = (gcnew System::Windows::Forms::TextBox());
this->OutputText = (gcnew System::Windows::Forms::Label());
this->Output = (gcnew System::Windows::Forms::TextBox());
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->loadEquationsToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
this->tableLayoutPanel1->SuspendLayout();
this->flowLayoutPanel1->SuspendLayout();
this->menuStrip1->SuspendLayout();
this->SuspendLayout();
//
// tableLayoutPanel1
//
this->tableLayoutPanel1->ColumnCount = 1;
this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
100)));
this->tableLayoutPanel1->Controls->Add(this->flowLayoutPanel1, 0, 1);
this->tableLayoutPanel1->Controls->Add(this->menuStrip1, 0, 0);
this->tableLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Fill;
this->tableLayoutPanel1->Location = System::Drawing::Point(0, 0);
this->tableLayoutPanel1->Margin = System::Windows::Forms::Padding(4);
this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
this->tableLayoutPanel1->RowCount = 2;
this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 5)));
this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 95)));
this->tableLayoutPanel1->Size = System::Drawing::Size(645, 761);
this->tableLayoutPanel1->TabIndex = 0;
//
// flowLayoutPanel1
//
this->flowLayoutPanel1->Controls->Add(this->InputText);
this->flowLayoutPanel1->Controls->Add(this->Input);
this->flowLayoutPanel1->Controls->Add(this->OutputText);
this->flowLayoutPanel1->Controls->Add(this->Output);
this->flowLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Fill;
this->flowLayoutPanel1->Location = System::Drawing::Point(4, 42);
this->flowLayoutPanel1->Margin = System::Windows::Forms::Padding(4);
this->flowLayoutPanel1->Name = L"flowLayoutPanel1";
this->flowLayoutPanel1->Size = System::Drawing::Size(637, 715);
this->flowLayoutPanel1->TabIndex = 0;
//
// InputText
//
this->InputText->AutoSize = true;
this->InputText->Location = System::Drawing::Point(4, 0);
this->InputText->Margin = System::Windows::Forms::Padding(4, 0, 4, 0);
this->InputText->Name = L"InputText";
this->InputText->Size = System::Drawing::Size(37, 15);
this->InputText->TabIndex = 0;
this->InputText->Text = L"Input";
//
// Input
//
this->Input->Location = System::Drawing::Point(4, 19);
this->Input->Margin = System::Windows::Forms::Padding(4);
this->Input->Multiline = true;
this->Input->Name = L"Input";
this->Input->Size = System::Drawing::Size(632, 164);
this->Input->TabIndex = 1;
this->Input->TextChanged += gcnew System::EventHandler(this, &MyForm::Input_TextChanged);
//
// OutputText
//
this->OutputText->AutoSize = true;
this->OutputText->Location = System::Drawing::Point(4, 187);
this->OutputText->Margin = System::Windows::Forms::Padding(4, 0, 4, 0);
this->OutputText->Name = L"OutputText";
this->OutputText->Size = System::Drawing::Size(46, 15);
this->OutputText->TabIndex = 2;
this->OutputText->Text = L"Output";
//
// Output
//
this->Output->Location = System::Drawing::Point(4, 206);
this->Output->Margin = System::Windows::Forms::Padding(4);
this->Output->Multiline = true;
this->Output->Name = L"Output";
this->Output->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
this->Output->Size = System::Drawing::Size(632, 498);
this->Output->TabIndex = 3;
//
// menuStrip1
//
this->menuStrip1->ImageScalingSize = System::Drawing::Size(20, 20);
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->fileToolStripMenuItem });
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Name = L"menuStrip1";
this->menuStrip1->Padding = System::Windows::Forms::Padding(8, 2, 0, 2);
this->menuStrip1->Size = System::Drawing::Size(645, 38);
this->menuStrip1->TabIndex = 1;
this->menuStrip1->Text = L"menuStrip1";
//
// fileToolStripMenuItem
//
this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->loadEquationsToolStripMenuItem });
this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
this->fileToolStripMenuItem->Size = System::Drawing::Size(64, 34);
this->fileToolStripMenuItem->Text = L"File";
//
// loadEquationsToolStripMenuItem
//
this->loadEquationsToolStripMenuItem->Name = L"loadEquationsToolStripMenuItem";
this->loadEquationsToolStripMenuItem->Size = System::Drawing::Size(265, 34);
this->loadEquationsToolStripMenuItem->Text = L"Load Equations";
this->loadEquationsToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::loadEquationsToolStripMenuItem_Click);
//
// openFileDialog1
//
this->openFileDialog1->FileName = L"openFileDialog1";
this->openFileDialog1->FileOk += gcnew System::ComponentModel::CancelEventHandler(this, &MyForm::openFileDialog1_FileOk);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 15);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(645, 761);
this->Controls->Add(this->tableLayoutPanel1);
this->MainMenuStrip = this->menuStrip1;
this->Margin = System::Windows::Forms::Padding(4);
this->Name = L"MyForm";
this->Text = L"Optimization";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->tableLayoutPanel1->ResumeLayout(false);
this->tableLayoutPanel1->PerformLayout();
this->flowLayoutPanel1->ResumeLayout(false);
this->flowLayoutPanel1->PerformLayout();
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: void distinguish_data(array<String^> ^data, std::queue<char> operates) {
// Output->Text += operates[0] + " " + operates[1] + " " + operates[2] + " ";
double coefficient, Denominator, molecular; //´Yº∆,§¿•¿,§¿§l
bool isX = false, isY = false, isX2 = false, isY2 = false, isX_ = false, isY_ = false;
String^ temp;
for (int i = 0; i < data->Length; i++) { //®C§@∂µ
isX = false, isY = false, isX2 = false, isY2 = false, isX_ = false, isY_ = false;
coefficient = 1;
if (data[i] == "") { //¶p™G¶≥™≈™∫∏‹ ∏ıπL
continue;
}
else { //¶p™G§£¨O™≈™∫
array<String^> ^temp_data;
temp_data = data[i]->Split('*'); //¶Aß‚®C§@∂µ®Ã∑”*®”§¿¶®¶n¥X≠”
for (int j = 0; j < temp_data->Length; j++) {
if (temp_data[j] == "x") {
isX = true;
}
else if (temp_data[j] == "x^2") {
isX2 = true;
}
else if (temp_data[j] == "x^(0.5)") {
isX_ = true;
}
else if (temp_data[j] == "y") {
isY = true;
}
else if (temp_data[j] == "y^2") {
isY2 = true;
}
else if (temp_data[j] == "y^(0.5)") {
isY_ = true;
}
else { //¨O´Yº∆
array<String^> ^number;
bool iscoefficient = false;
for (int k = 0; k < temp_data[j]->Length; k++) {
if (temp_data[j][k] == '/') {
number = temp_data[j]->Split('/');
molecular = Convert::ToDouble(number[0]);
Denominator = Convert::ToDouble(number[1]);
coefficient = molecular / Denominator;
iscoefficient = true;
break;
}
}
if (!iscoefficient) {
coefficient = Convert::ToDouble(temp_data[j]);
}
}
}
//•H§U®Ãæ⁄bool•hßP¬_¨O¨∆ªÚ∂µ,¶b®Ã∑”operates•hßP¬_•ø≠t,®√¿x¶s´Yº∆∏ÍÆ∆®ÏπÔ¿≥™∫µ≤™G
if (isX&&!(isY || isY2 || isY_)) { //x
if (operates.front() == '+')
{
operates.pop();
newton_function_number.x += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.x -= coefficient;
}
}
else if (isX2&&!(isY || isY2 || isY_)) { //x2
if (operates.front() == '+')
{
operates.pop();
newton_function_number.x2 += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.x2 -= coefficient;
}
}
else if (isX&&isY) { //xy
if (operates.front() == '+')
{
operates.pop();
newton_function_number.xy += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.xy -= coefficient;
}
}
else if (isY&&!(isX || isX2 || isX_)) { //y
if (operates.front() == '+')
{
operates.pop();
newton_function_number.y += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.y -= coefficient;
}
}
else if (isY2&&!(isX || isX2 || isX_)) { //y2
if (operates.front() == '+')
{
operates.pop();
newton_function_number.y2 += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.y2 -= coefficient;
}
}
else if (isX2&&isY2) { //x2y2
if (operates.front() == '+')
{
operates.pop();
newton_function_number.x2y2 += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.x2y2 -= coefficient;
}
}
else if (isX2&&isY) { //x2y
if (operates.front() == '+')
{
operates.pop();
newton_function_number.x2y += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.x2y -= coefficient;
}
}
else if (isX&&isY2) { //xy2
if (operates.front() == '+')
{
operates.pop();
newton_function_number.xy2 += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.xy2 -= coefficient;
}
}
else if (isX_) { //x^0.5
if (operates.front() == '+')
{
operates.pop();
newton_function_number.x05 += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.x05 -= coefficient;
}
}
else { //normal
if (operates.front() == '+')
{
operates.pop();
newton_function_number.normal += coefficient;
}
else if (operates.front() == '-') {
operates.pop();
newton_function_number.normal -= coefficient;
}
}
}
}
}
private: System::Void loadEquationsToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
openFileDialog1->ShowDialog();
}
private: System::Void openFileDialog1_FileOk(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) {
//¶bDialog´ˆ§UOK´K∑|∂i§J¶π®Á¶°
//¶r¶Í¬‡®Óstring^ to string
std::string tempFileName;
MarshalString(openFileDialog1->FileName, tempFileName);
//±N¿…Æ◊∏ÙÆ|¶W∫Ÿ∂«§JdataManager
dataManager->SetFileName(tempFileName);
//±q≈™®˙≈™®˙¶V∂q∏ÍÆ∆
if (dataManager->LoadEquationData())
{
equations = dataManager->GetEquations();
Output->Multiline = true;
for (unsigned int i = 0; i < equations.size(); i++)
{
Output->Text += gcnew String(equations[i].c_str());
Output->Text += Environment::NewLine;
}
Output->Text += "chose a method(Enter the number)" + Environment::NewLine +
"1.Newton Method" + Environment::NewLine +
"2.Quasi-Newton Method" + Environment::NewLine +
"3.Conjugate Gradient Methods" + Environment::NewLine;
}
}
private: System::Void Input_TextChanged(System::Object^ sender, System::EventArgs^ e) {
if (Input->Text->Length - 1 >= 0 && Input->Text[Input->Text->Length - 1] == '\n') {
array <String ^> ^command = userInput->Split(' ');
if (choose_function) {
function_number = Convert::ToInt32(command[0]); //®˙±o®œ•Œ≠˛§@≠”®Á¶°,•—0∂}©l
Output->Text += "Enter the is One-variable(1) or Two-variable(2) equations: " + Environment::NewLine;
choose_function = false;
newton1 = true;
}
else if (newton1) {
one_or_two = Convert::ToInt32(command[0]);
if (one_or_two == 1) {
Output->Text += "Enter the initial[x]:"+ Environment::NewLine;
enter_initial = true;
}
else if (one_or_two == 2) {
std::queue<char> operates;
//øȧJøÎßO®Á¶°
std::string temp = equations[function_number];
newton_interval = command; //newton_interval[0]¨∞X, newton_interval[1]¨∞Y
for (int i = 0; i < temp.length(); i++) {
if (i == 0 && (temp[i] != '-')) {
operates.push('+');
}
else if (temp[i] == '+' || temp[i] == '-') {
operates.push(temp[i]);
temp[i] = ' ';
}
}
String^ temp_String;
temp_String = gcnew String(temp.c_str());
//MarshalString(temp_String, temp);
array<String^> ^data = temp_String->Split(' ');
distinguish_data(data, operates);
newton(newton_function_number);
}
newton1 = false;
/*Output->Text +="x2= "+ newton_function_number.x2 + Environment::NewLine +
"x= " + newton_function_number.x + Environment::NewLine +
"y2= " + newton_function_number.y2 + Environment::NewLine +
"y= " + newton_function_number.y + Environment::NewLine +
"x2y= " + newton_function_number.x2y + Environment::NewLine +
"xy2= " + newton_function_number.xy2 + Environment::NewLine +
"x2y2= " + newton_function_number.x2y2 + Environment::NewLine +
"x0.5= " + newton_function_number.x05 + Environment::NewLine +
"normal= " + newton_function_number.normal + Environment::NewLine;*/
}
else if (enter_initial) {
std::queue<char> operates;
//øȧJøÎßO®Á¶°
std::string temp = equations[function_number];
newton_interval = command; //newton_interval[0]¨∞X, newton_interval[1]¨∞Y
for (int i = 0; i < temp.length(); i++) {
if (i == 0 && (temp[i] != '-')) {
operates.push('+');
}
else if (temp[i] == '+' || temp[i] == '-') {
operates.push(temp[i]);
temp[i] = ' ';
}
}
String^ temp_String;
temp_String = gcnew String(temp.c_str());
//MarshalString(temp_String, temp);
array<String^> ^data = temp_String->Split(' ');
distinguish_data(data, operates);
/*Output->Text += "x2= " + newton_function_number.x2 + Environment::NewLine +
"x= " + newton_function_number.x + Environment::NewLine +
"y2= " + newton_function_number.y2 + Environment::NewLine +
"y= " + newton_function_number.y + Environment::NewLine +
"x2y= " + newton_function_number.x2y + Environment::NewLine +
"xy2= " + newton_function_number.xy2 + Environment::NewLine +
"x2y2= " + newton_function_number.x2y2 + Environment::NewLine +
"x0.5= " + newton_function_number.x05 + Environment::NewLine +
"normal= " + newton_function_number.normal + Environment::NewLine;*/
newton_1(newton_function_number, Convert::ToDouble(command[0]));
enter_initial = false;
}
else if (Conjugate) {
//conjugate_initial = command; //conjugate_initial[0]¨∞X, conjugate_initial[1]¨∞Y
std::queue<char> operates;
//øȧJøÎßO®Á¶°
std::string temp = equations[function_number];
for (int i = 0; i < temp.length(); i++) {
if (i == 0 && (temp[i] != '-')) {
operates.push('+');
}
else if (temp[i] == '+' || temp[i] == '-') {
operates.push(temp[i]);
temp[i] = ' ';
}
}
String^ temp_String;
temp_String = gcnew String(temp.c_str());
//MarshalString(temp_String, temp);
array<String^> ^data = temp_String->Split(' ');
distinguish_data(data, operates);
//•·∂iπw≥]®Á¶°
conjugate_2(newton_function_number, Convert::ToDouble(command[0]), Convert::ToDouble(command[1]));
Conjugate = false;
}
else if (conjugate_enter_interval) {//øȧJ∞œ∂°
conjugate_interval_start = Convert::ToDouble(command[0]);
conjugate_interval_end = Convert::ToDouble(command[1]);//conjugate_enter_interval[0]¨∞≥çp≠»,conjugate_enter_interval[1]¨∞≥çj≠»
Output->Text += "[" + command[0] + "," + command[1] + "]" + Environment::NewLine;
Output->Text += "Enter the initial : ";
conjugate_enter_interval = false;
Conjugate = true;
}
else if (conjugate_choose_function) {//øÔfunction
function_number = Convert::ToInt32(command[0]); //®˙±o®œ•Œ≠˛§@≠”®Á¶°,•—0∂}©l
Output->Text += command[0] + Environment::NewLine;
Output->Text += "Enter the interval:" + Environment::NewLine;
conjugate_choose_function = false;
conjugate_enter_interval = true;
//∂}©l§uß@
}
else if (Quasi_choose_function) { //øÔfunction
function_number = Convert::ToInt32(command[0]); //®˙±o®œ•Œ≠˛§@≠”®Á¶°,•—0∂}©l
Output->Text += command[0] + Environment::NewLine;
Output->Text += "Enter the interval:" + Environment::NewLine;
Quasi_choose_function = false;
Quasi_enter_interval = true;
}
else if (Quasi_enter_interval) {
Output->Text += "[" + command[0] + "," + command[1] + "]" + Environment::NewLine;
Output->Text += "Enter the initial : ";
Quasi_enter_interval = false;
Quasi = true;
}
else if (Quasi) {
//conjugate_initial = command; //conjugate_initial[0]¨∞X, conjugate_initial[1]¨∞Y
std::queue<char> operates;
//øȧJøÎßO®Á¶°
std::string temp = equations[function_number];
for (int i = 0; i < temp.length(); i++) {
if (i == 0 && (temp[i] != '-')) {
operates.push('+');
}
else if (temp[i] == '+' || temp[i] == '-') {
operates.push(temp[i]);
temp[i] = ' ';
}
}
String^ temp_String;
temp_String = gcnew String(temp.c_str());
//MarshalString(temp_String, temp);
array<String^> ^data = temp_String->Split(' ');
distinguish_data(data, operates);
//•·∂iπw≥]®Á¶°
quasi_newton(newton_function_number, Convert::ToDouble(command[0]), Convert::ToDouble(command[1]));
Quasi = false;
}
else if (command[0] == "1") {
choose_function = true;
Output->Text += "Enter the function number: " + Environment::NewLine;
}
else if (command[0] == "2") {
Quasi_choose_function = true;
Output->Text += "Enter the function number: " + Environment::NewLine;
}
else if (command[0] == "3") {
conjugate_choose_function = true;
Output->Text += "Enter the function : " + Environment::NewLine;
}
else if (command[0] == "clear") {
Input->Text = "";
Output->Text = "";
}
}
else
{
array<String^> ^userCommand = Input->Text->Split('\n');
userInput = userCommand[userCommand->Length - 1];
}
}
Matrix TRANS(Matrix temp) {
Matrix ans;
std::vector<double> x;
ans.row_dim = temp.column_dim;
ans.column_dim = temp.row_dim;
for (int i = 0; i < temp.column_dim; i++)
{
for (int j = 0; j < temp.row_dim; j++)
{
x.push_back(temp.data[j][i]);
}
ans.data.push_back(x);
x.clear();
}
return ans;
}
Matrix inverse(Matrix temp1, bool* ok)
{
double temp;
Matrix temp2 = temp1;
if (temp1.row_dim != temp1.column_dim) {
*ok = false;
goto end;
}
for (int i = 0; i < temp2.row_dim; i++) {
for (int j = 0; j < temp2.column_dim; j++) {
temp2.data[i][j] = 0;
}
}
for (int i = 0; i < temp2.row_dim; i++) {
temp2.data[i][i] = 1;
}
for (int i = 0; i < temp1.row_dim; i++) {
//•H§U®˙¶C•Ê¥´
if (temp1.data[i][i] == 0) {
for (int j = i + 1; j < temp1.row_dim; j++) {
if (temp1.data[j][i] != 0) {
for (int k = 0; k < temp1.column_dim; k++) {
temp = temp1.data[j][k];
temp1.data[j][k] = temp1.data[i][k];
temp1.data[i][k] = temp;
temp = temp2.data[j][k];
temp2.data[j][k] = temp2.data[i][k];
temp2.data[i][k] = temp;
}
break;
}
}
}
/*if (temp1.Data[i][i] == 0) {
*ok = false;
break;
}*/
double t = temp1.data[i][i];
for (int k = 0; k < temp1.column_dim; k++) {
temp1.data[i][k] = temp1.data[i][k] / t;
temp2.data[i][k] = temp2.data[i][k] / t;
}
for (int j = 0; j < temp1.row_dim; j++) {
if ((i != j) && temp1.data[j][i] != 0) {
double z = temp1.data[j][i];
for (int k = 0; k < temp1.column_dim; k++) {
temp1.data[j][k] -= temp1.data[i][k] * z;
temp2.data[j][k] -= temp2.data[i][k] * z;
}
}
}
}
end:
return temp2;
}
Matrix Mul(Matrix data1, Matrix data2)
{
Matrix temp_data = TRANS(data2);
temp_data.row_dim = data2.column_dim;
temp_data.column_dim = data2.row_dim;
Matrix ANS;
std::vector<double> temp;
double tempdata = 0;
if (data1.column_dim == data2.row_dim)
{
ANS.row_dim = data1.row_dim;
ANS.column_dim = data2.column_dim;
for (int i = 0; i < data1.row_dim; i++)
{
for (int j = 0; j < temp_data.row_dim; j++)
{
for (int k = 0; k < data1.column_dim; k++)
{
tempdata += data1.data[i][k] * temp_data.data[j][k];
}
temp.push_back(tempdata);
tempdata = 0;
}
ANS.data.push_back(temp);
temp.clear();
}
}
return ANS;
}
private: Matrix hessian(function_numbers data, double x, double y)
{
std::vector<double> qq;
qq.push_back(0);
qq.push_back(0);
function_numbers temp;
function_numbers temp1;
function_numbers temp2;
Matrix h;
h.data.push_back(qq);
h.data.push_back(qq);
h.row_dim = 2;
h.column_dim = 2;
temp = differential(data, 1);
h.data[0][0] = (temp.y2 * y * y) + (temp.y * y) + temp.normal;
temp1 = differential(data, 3);
h.data[0][1] = (temp1.x * x) + (temp1.xy * x * y) + (temp1.y * y) + temp1.normal;
h.data[1][0] = h.data[0][1];
temp2 = differential(data, 2);
h.data[1][1] = (temp2.x2 * x * x) + (temp2.x * x) + temp2.normal;
return h;
}
private: Matrix gradient(function_numbers data, double x, double y)
{
std::vector<double> temp;
temp.push_back(0);
Matrix g;
g.data.push_back(temp);
g.data.push_back(temp);
function_numbers temp1;
function_numbers temp2;
g.column_dim = 1;
g.row_dim = 2;
temp1 = differential(data, 4);
g.data[0][0] = (temp1.y2 * y*y) + (temp1.y * y) + (temp1.xy * x * y) + (temp1.x * x) + (temp1.xy2 * x * y*y) + temp1.normal;
//Output->Text += "!!!" + g.data[0][0] + "!" + temp1.y2 + "!" + temp1.y + "!" + temp1.xy + "!" + temp1.x + "!" + temp1.xy2 + "!" + temp1.normal + "!!!";
temp2 = differential(data, 5);
g.data[1][0] = (temp2.x2 * x * x) + (temp2.y * y) + (temp2.x2y * x *x * y) + (temp2.x * x) + (temp2.xy * x * y) + temp2.normal;
//Output->Text += "!!!" + g.data[1][0] + "!" + temp2.x2 + "!" + temp2.y + "!" + temp2.x2y + "!" + temp2.x + "!" + temp2.xy + "!" + temp2.normal + "!!!";
return g;
}
private: double hessian_1(function_numbers data, double x)
{
function_numbers temp;
double ans;
temp = differential(data, 6);
ans = (temp.x151 * pow(x,(-1.5))) + temp.normal;
return ans;
}
private: double gradient_1(function_numbers data, double x)
{
function_numbers temp;
double ans;
temp = differential(data, 7);
ans = (temp.x * x) + (temp.x051 * pow(x,-0.5)) + temp.normal;
return ans;
}
private: function_numbers differential(function_numbers data, int k) //(x^2)^2 = 1 (y^2)^2=2 xy=3 x=4 y=5
{
if (k == 1)
{
data.y2 = data.x2y2 * 2;
data.y = data.x2y * 2;
data.normal = data.x2 * 2;
}
else if (k == 2)
{
data.x2 = data.x2y2 * 2;
data.x = data.xy2 * 2;
data.normal = data.y2 * 2;
}
else if (k == 3)
{
data.y2 = data.xy2;
data.y = data.xy;
data.xy2 = data.x2y2 * 2;
data.xy = data.x2y * 2;
data.normal = data.x;
data.x = data.x2 * 2;
data.x = data.xy * 2;
data.xy = data.xy2 * 2;
data.normal = data.y;
data.y = data.y2 * 2;
}
else if (k == 4) // πÔx∑L§¿
{
data.y2 = data.xy2;
data.y = data.xy;
data.xy2 = data.x2y2 * 2;
data.xy = data.x2y * 2;
data.normal = data.x;
data.x = data.x2 * 2;
}
else if (k == 5) //πÔy∑L§¿
{
data.normal = data.y;
data.x2 = data.x2y;
data.y = data.y2 * 2;
data.x2y = data.x2y2 * 2;
data.x = data.xy;
data.xy = data.xy2 * 2;
}
else if (k == 6) // πÔx§G¶∏∑L§¿
{
data.normal = data.x2 * 2;
data.x151 = data.x05 / -4;
}
else if (k == 7) // πÔx∑L§¿
{
data.normal = data.x;
data.x = data.x2 * 2;
data.x051 = data.x05 / 2;
}
return data;
}
private: void newton(function_numbers data)
{
function_numbers initial = data;
Matrix h, g, h_I, x_m;
std::vector<double> temp;
bool *ok = false;
double initial_x = 20, initial_y = 8;
double x = initial_x, y = initial_y;
double ans;
temp.push_back(initial_x);
x_m.data.push_back(temp);
temp.clear();
temp.push_back(initial_y);
x_m.data.push_back(temp);
temp.clear();
x_m.column_dim = 1;
x_m.row_dim = 2;
x_m.data[0][0] = initial_x;
x_m.data[1][0] = initial_y;
for (unsigned i = 0; i < 3; i++)
{
//Output->Text += "!!!" + x + "!!" + y + "!!!";
h = hessian(data, x, y);
Output->Text += "Hessian:" + Environment::NewLine + "[" + Environment::NewLine;
for (unsigned j = 0; j < 2; j++)
{
for (unsigned z = 0; z < 2; z++)
{
Output->Text += h.data[j][z] + " ";
}
Output->Text += Environment::NewLine;
}
Output->Text += "]" + Environment::NewLine;
h_I = inverse(h, ok);
Output->Text += "Hessian Inverse:" + Environment::NewLine + "[" + Environment::NewLine;
for (unsigned j = 0; j < 2; j++)
{
for (unsigned z = 0; z < 2; z++)
{
Output->Text += h_I.data[j][z]+ " ";
}
Output->Text += Environment::NewLine;
}
Output->Text += "]" + Environment::NewLine;
g = gradient(data, x, y);
//Output->Text += "!!!" + g.data[0][0] + "!!" + g.data[1][0] + "!!!";
g = Mul(h_I, g);
//Output->Text += "!!!" + g.data[0][0] + "!!" + g.data[1][0] + "!!!";
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 1; j++)
{
x_m.data[i][j] -= g.data[i][j];
}
}
Output->Text += "x:" + Environment::NewLine + "[" + Environment::NewLine;
for (unsigned j = 0; j < 2; j++)
{
for (unsigned z = 0; z < 1; z++)
{
Output->Text += x_m.data[j][z] + " ";
}
Output->Text += Environment::NewLine;
}
Output->Text += "]" + Environment::NewLine;
x = x_m.data[0][0];
y = x_m.data[1][0];
// Output->Text += "!!!" + x + "!!" + y + "!!!";
}
ans = (newton_function_number.x2 * x*x) + (newton_function_number.x * x) + (newton_function_number.x2y2 * x*x*y * y) + (newton_function_number.xy2 * x * y*y) + (newton_function_number.x2y * x *x* y) + (newton_function_number.xy * x * y) + (newton_function_number.y * y) + (newton_function_number.y2 *y* y) + newton_function_number.normal;
Output->Text += "[x,y] = " + x + " " + y +Environment::NewLine;
Output->Text += "min: " + ans + Environment::NewLine;
}
private: void newton_1(function_numbers data, double initial_x)
{
function_numbers temp;
double x = initial_x;
double k=0,h=0,x_n=0,g=0,h_I=0;
double ans = 0;
temp = differential(data, 7);
k = (temp.x * x) + (temp.x051 * pow(x, -0.5)) + temp.normal;
for (int i = 0; i < 3;i++)
{
h = hessian_1(data, x);
Output -> Text += "Hessian: " + h + Environment::NewLine;
h_I = 1 / h;
Output->Text += "Hessian Inverse: " + h_I + Environment::NewLine;
g = gradient_1(data, x);
g = g * h_I;
x_n = x - g;
Output->Text += "x: " + x + Environment::NewLine;
x = x_n;
k = (temp.x * x) + (temp.x051 * pow(x, -0.5)) + temp.normal;
}
ans = (newton_function_number.x2 *x *x) + (newton_function_number.x *x) + (newton_function_number.x05 * pow(x, 0.5)) + newton_function_number.normal;
Output->Text += "min: " + ans + Environment::NewLine;
}
private: function z(function_numbers data, double initial_x)
{
function a;
function_numbers temp;
double x = initial_x;
double k = 0, h = 0, x_n = 0, g = 0, h_I = 0;
double ans = 0;
temp = differential(data, 7);
for (int i = 0; i < 3; i++)
{
h = hessian_1(data, x);
h_I = 1 / h;
g = gradient_1(data, x);
g = g * h_I;
x_n = x - g;
x = x_n;
}
ans = (newton_function_number.x2 *x *x) + (newton_function_number.x *x) + (newton_function_number.x05 * pow(x, 0.5)) + newton_function_number.normal;
a.ans = ans;
a.x = x_n;
return a;
}
private: void conjugate_1(function_numbers data, double initial_x)
{
function_numbers temp;
function a;
double Si = 0;
double ans = 0, x_ans = 0;
double x = initial_x;
double alpha = 0;
int i = 0;
temp = differential(data, 7);
Si = (temp.x * x) + (temp.x051 * pow(x, -0.5)) + temp.normal;
Output->Text += i + Environment::NewLine;
Output->Text += "Si= " + Environment::NewLine + "[" + Environment::NewLine + Si + Environment::NewLine + "]" + Environment::NewLine;
a = z(data, x);
ans = a.ans;
x_ans = a.x;
alpha = (x_ans - initial_x) / Si;
Output->Text += "alpha = " + alpha + Environment::NewLine;
Output->Text += "Xi = " + Environment::NewLine + "[" + Environment::NewLine + x_ans + Environment::NewLine + "]";
Output->Text += "[x] = [" + x_ans + ", ]";
Output->Text += "min = " + ans;
}
private: void conjugate_2(function_numbers data, double x, double y) //º»Æ…,¡Ÿ•º±“•Œ
{
Matrix Q, b, u, ut;
Matrix temp_Ju;
std::vector<double> temp;
u.column_dim = 1;
u.row_dim = 2;
temp.push_back(x);
u.data.push_back(temp);
temp.clear();
temp.push_back(y);
u.data.push_back(temp);
temp.clear();
ut = TRANS(u);
//•H§WøȧJu,ut
Q.column_dim = 2;
Q.row_dim = 2;
temp.push_back(2 * (data.x2));
temp.push_back(data.xy);
Q.data.push_back(temp);
temp.clear();
temp.push_back(data.xy);
temp.push_back(2 * (data.y2));
Q.data.push_back(temp);
temp.clear();
//•H§WøȧJQ
b.column_dim = 1;
b.row_dim = 2;
temp.push_back((-1)*data.x);
b.data.push_back(temp);
temp.clear();
temp.push_back((-1)*data.y);
b.data.push_back(temp);
temp.clear();
//•H§WøȧJb
//™Ï©l§∆ßπ≤¶
Matrix Ju = Mul(Q, u);
Matrix d = Ju;
//≤ƒ§@¶∏JuπB∫‚
Ju.data[0][0] -= b.data[0][0];
Ju.data[1][0] -= b.data[1][0];
temp_Ju = Ju;
//≤ƒ§@¶∏dπB∫‚
d.data[0][0] = -Ju.data[0][0];
d.data[1][0] = -Ju.data[1][0];
for (int i = 0; i <2; i++) {
/*Output->Text +=Environment::NewLine+ "u " + u.data[0][0] + " , " + u.data[1][0] + Environment::NewLine;
Output->Text += "Q " + Q.data[0][0] + " , " + Q.data[0][1] + Environment::NewLine;
Output->Text += " " + Q.data[1][0] + " , " + Q.data[1][1] + Environment::NewLine;
Output->Text += "b " + b.data[0][0] + " , " + b.data[1][0] + Environment::NewLine;
Output->Text += "Ju " + Ju.data[0][0] + " , " + Ju.data[1][0] + Environment::NewLine;
Output->Text += "d " + d.data[0][0] + " , " + d.data[1][0] + Environment::NewLine+Environment::NewLine;
*/
Output->Text +=Environment::NewLine + "i="+ (i) + Environment::NewLine + "Si={ " + d.data[0][0] + "," + d.data[1][0] + " }" + Environment::NewLine;
//≤ƒ§@¶∏alphaπB∫‚
double alpha = (-1) * ((Mul(TRANS(Ju), d).data[0][0] / Mul(Mul(TRANS(d), Q), d).data[0][0]));
Output->Text += "alpha = " + alpha + Environment::NewLine;
//≤ƒ§@¶∏µ≤™G
u.data[0][0] = u.data[0][0] + alpha*d.data[0][0];
u.data[1][0] = u.data[1][0] + alpha*d.data[1][0];
ut = TRANS(u);
Output->Text += "Xi={ " + u.data[0][0] + "," + u.data[1][0] + " }" + Environment::NewLine;
//≤ƒ§G¶∏JuπB∫‚
Ju = Mul(Q, u);
Ju.data[0][0] -= b.data[0][0];
Ju.data[1][0] -= b.data[1][0];
//≤ƒ§@¶∏betaπB∫‚
double beta =Mul(TRANS(Ju), Ju).data[0][0]/ Mul(TRANS(temp_Ju), temp_Ju).data[0][0];
//-1 *( (Mul(TRANS(Ju), d).data[0][0] / Mul(Mul(TRANS(d), Q), d).data[0][0]));
temp_Ju = Ju;
//ßÛ∑sd(≤ƒ§G¶∏)
d.data[0][0] = -Ju.data[0][0] + beta*d.data[0][0];
d.data[1][0] = -Ju.data[1][0] + beta*d.data[1][0];
}
Output->Text += Environment::NewLine + "[x,y]= [ " + u.data[0][0] + " , " + u.data[1][0] + " ]" + Environment::NewLine;
Output->Text += "min= " + (data.x2*u.data[0][0] * u.data[0][0] + data.y2*u.data[1][0] * u.data[1][0] + data.xy*u.data[0][0] * u.data[1][0] + data.x*u.data[0][0] + data.y*u.data[1][0]+data.normal) + Environment::NewLine;
}
private: void quasi_newton(function_numbers data, double initial_x, double initial_y)
{
//
Matrix Q, H, d, g, x_m, a,delta_x,delta_g,temp_x,temp_g,mul_delta_x,delta_hg,new_h;
std::vector<double> p;
double alpha;
bool *ok = false;
p.push_back(initial_x); p.push_back(initial_x); H.data.push_back(p); p.clear(); p.push_back(initial_y); p.push_back(initial_y); H.data.push_back(p); p.clear();
H.column_dim = 2;
H.row_dim = 2;
H.data[0][0] = 1; H.data[0][1] = 0; H.data[1][0] = 0; H.data[1][1] = 1; // µπ™Ï≠»,≥ʶÏØx∞}
p.push_back(initial_x); x_m.data.push_back(p); p.clear(); p.push_back(initial_y); x_m.data.push_back(p); p.clear();
x_m.column_dim = 1;
x_m.row_dim = 2;
x_m.data[0][0] = initial_x; x_m.data[1][0] = initial_y;
p.push_back(initial_x); Q.data.push_back(p); p.clear(); p.push_back(initial_y); Q.data.push_back(p); p.clear();
Q = hessian(data, initial_x, initial_y);
Output->Text += Environment::NewLine + "Q= [" + Q.data[0][0] + " , " + Q.data[0][1] + "]" + Environment::NewLine + " [" + Q.data[1][0] + " , " + Q.data[1][1] + "]" + Environment::NewLine;
Output->Text += "[x,y]= [" + x_m.data[0][0] + " , " + x_m.data[1][0] + "]" + Environment::NewLine;
//p.push_back(initial_x); a.data.push_back(p); p.clear(); p.push_back(initial_y); a.data.push_back(p); p.clear();
a = gradient(data, initial_x, initial_y);
a.data[0][0] = -newton_function_number.x;
a.data[1][0] = -newton_function_number.y;
g = Mul(Q, x_m);
Output->Text += "a = [" + a.data[0][0] + " , " + a.data[1][0] + "]" + Environment::NewLine;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 1; j++)
{
g.data[i][j] -= a.data[i][j]; //g0
}
}
//Output->Text += "g = [" + g.data[0][0] + " , " + g.data[1][0] + "]" + Environment::NewLine;
d = Mul(H, g);
d.data[0][0] = -d.data[0][0];
d.data[1][0] = -d.data[1][0]; //d0
alpha = -1 * (Mul(TRANS(g), d).data[0][0] / Mul(Mul(TRANS(d), Q), d).data[0][0]); //alpha0
Output->Text += "alpha = "+alpha + Environment::NewLine;
Output->Text += "d = [" + d.data[0][0] + " , " + d.data[1][0] + "]" + Environment::NewLine;
Output->Text += "H = [" + H.data[0][0] + " , " + H.data[0][1] + "]" + Environment::NewLine+" ["+H.data[1][0]+" , "+H.data[1][1]+"]"+Environment::NewLine;
while(!(abs(d.data[0][0])<=0.0001&&abs(d.data[1][0])<=0.0001)) {
temp_x = x_m; //temp_x¨∞§@∂}©l™∫x
x_m.data[0][0] = x_m.data[0][0] + alpha* d.data[0][0];
x_m.data[1][0] = x_m.data[1][0] + alpha* d.data[1][0];
Output->Text += Environment::NewLine +"[x,y]= [" + x_m.data[0][0] + " , " + x_m.data[1][0] + "]" + Environment::NewLine;
delta_x = x_m;
delta_x.data[0][0] -= temp_x.data[0][0];
delta_x.data[1][0] -= temp_x.data[1][0];
temp_g = g; //temp_g¨∞§@∂}©l™∫g
g = Mul(Q, x_m);
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 1; j++)
{
g.data[i][j] -= a.data[i][j];
}
}
delta_g = g;
delta_g.data[0][0] = g.data[0][0] - temp_g.data[0][0];
delta_g.data[1][0] = g.data[1][0] - temp_g.data[1][0];
mul_delta_x = Mul(delta_x, TRANS(delta_x));
delta_hg = Mul(Mul(H, delta_g), TRANS(Mul(H, delta_g)));
new_h = H;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
new_h.data[i][j] = H.data[i][j] + (mul_delta_x.data[i][j] / Mul(TRANS(delta_x), delta_g).data[0][0]) - (delta_hg.data[i][j] / Mul(Mul(TRANS(delta_g), H), delta_g).data[0][0]);
}
}
H = new_h;
d = Mul(new_h, g);
d.data[0][0] = -d.data[0][0];
d.data[1][0] = -d.data[1][0];
alpha = -1 * ((Mul(TRANS(g), d).data[0][0] / Mul(Mul(TRANS(d), Q), d).data[0][0]));
Output->Text += "alpha = " + alpha + Environment::NewLine;
Output->Text += "d = [" + d.data[0][0] + " , " + d.data[1][0] + "]" + Environment::NewLine;
Output->Text += "H = [" + H.data[0][0] + " , " + H.data[0][1] + "]" + Environment::NewLine + " [" + H.data[1][0] + " , " + H.data[1][1] + "]" + Environment::NewLine;
//x_m.data[0][0] = x_m.data[0][0] + alpha* d.data[0][0];
//x_m.data[1][0] = x_m.data[1][0] + alpha* d.data[1][0];
}
Output->Text += "[x,y]= [" + x_m.data[0][0] + " , " + x_m.data[1][0] + "]" + Environment::NewLine;
double x = x_m.data[0][0], y = x_m.data[1][0];
Output->Text += "min = "+((newton_function_number.x2 * x * x) + (newton_function_number.x * x) + (newton_function_number.xy * x * y) + (newton_function_number.y * y) + (newton_function_number.y2 * y * y) + newton_function_number.normal)+Environment::NewLine;
}
};
}