📄 cluster_imp.cpp
字号:
#include "stdafx.h"
#include "cluster.h"
//#include "const.h"
#include "mynode.h"
Cluster::Cluster(const Node& n)
{
/* if (n.IsConst()) // const
node_map_.insert(make_pair(n,1)); //放置常量
else
{
node_map_.insert(make_pair(Node(1),1));//首先放置常量单元!
node_map_.insert(make_pair(n,1)); // 放置变量
}*/
//前提是Node认为所有的常数是相等的!!!
node_map_.insert(make_pair(n,1));
node_map_.insert(make_pair(Node(1),1)); // 可能失败
UpdateAll();
}
bool Cluster::operator<(const Cluster& cl)const
{
/*if (IsConst())
{
if (cl.IsConst())
return GetResult() < cl.GetResult();
else
return true;
}
else
{
if (cl.IsConst())
return false;
else
return GetFactor() < cl.GetFactor();
}*/
/*string me = GetFactor();
string you = cl.GetFactor();
if (me.empty() && you.empty()) // == is not allowed here!!!
return GetResult() < cl.GetResult();
else
return me < you;*/
return GetFactor() < cl.GetFactor(); //保证只有一个常数能够被插入!!!
}
string Cluster::GetFactor()const
{
if (IsConst())
return "";
else
{
int m = expression_.find('*');
if (m == -1) // xy
return expression_;
if (expression_[m-2] >='0' && expression_[m-2] <= '9')
return expression_.substr(m + 2,expression_.size());
return expression_;
}
}
void Cluster::UpdateAll()
{
// const head ++
string head = ForConst(node_map_.begin()->first.GetNodeValue());
head += " * ";
//const head--
//frame ++
string frame;
map<Node,double>::const_iterator it;
for (it = ++node_map_.begin() ; it != node_map_.end();++it)
{
//if (it->second != 0)
if (fabs(it->second) > c_very_small)
{
frame += ForPower(it->first.GetExpression(),it->second);
frame += " * ";
}
}
//->
expression_ = head + frame;
ExpressionCut(expression_);
/* is_const_ = (expression_.find('*') == -1 &&
(expression_[0] >= 0 || expression_[0] <= 9 ));*/
//->
}
void Cluster::SetOpposit()
{
// const++
map<Node,double>::iterator my_const = node_map_.begin();
double c = 1.0 / my_const->first.GetNodeValue();
node_map_.erase(node_map_.begin());
node_map_.insert(make_pair(Node(c),1));
// const--
//frame++
map<Node,double>::const_iterator it;
for (it = ++node_map_.begin()/* + 1*/;it != node_map_.end();++it)
{
node_map_[it->first] = -it->second;
}
//frame--
UpdateAll();
}
void Cluster::Mul(const Cluster& b)
{
// not just b.GetSingleValue() ....!!!!!
if (fabs(b.GetSingleValue() * GetSingleValue()) < c_very_small)
{
*this = Cluster(double(0));
UpdateAll();
return ;
}
// first++
map<Node,double>::iterator my_const = node_map_.begin();
map<Node,double>::const_iterator it = b.node_map_.begin();
double c = my_const->first.GetNodeValue() * it->first.GetNodeValue();
node_map_.erase(node_map_.begin());
node_map_.insert(make_pair(Node(c),1));
// first--
//frame++
for (it = ++b.node_map_.begin() ;
it != b.node_map_.end() && !it->first.IsConst(); ++it)
{
if (node_map_.find(it->first) != node_map_.end())
{
node_map_[it->first] += it->second;
//if (node_map_[it->first] == 0)
if (fabs(node_map_[it->first]) < c_very_small)
node_map_.erase(it->first); // 删除!!!!
}
else
{
//if (it->second != 0)
if (fabs(it->second) > c_very_small)
node_map_.insert(*it);
}
}
//frame--
UpdateAll();
}
void Cluster::Div(const Cluster& b)
{
Cluster opb(b);
opb.SetOpposit();
this->Mul(opb);
}
void Cluster::ExpressionCut(string& in)
{
if (in[0] == '0' && in[2] == '*')
{
in = "0";
return ;
}
//去冗余的1
if (in[0] == '1' && in[2] == '*' && in.size() > 4)
in = in.substr(4,in.size()); //[)
// ->x * ||->1 * ||->3.5 * x *
//if (in[in.size() - 2] == '*') // 不可能为空!!!
in.resize(in.size() - 3); // cut off tail
}
//protected+++
void Cluster::MergeWeight(const Cluster&b) //for Block only!!!
{ // it quite ugly
map<Node,double>::iterator my_const = node_map_.begin();
map<Node,double>::const_iterator it = b.node_map_.begin();
double c = my_const->first.GetNodeValue() + it->first.GetNodeValue();
// cout<<"sum ->0\n\n";
if (fabs(c) < c_very_small)
*this = Cluster(0);
//cout<<" ->0 "<<node_map_.size()<<" <-size\n\n\n";
else
{
node_map_.erase(node_map_.begin());
node_map_.insert(make_pair(Node(c),1));
}
UpdateAll();
}
void Cluster::ReverseWeight()
{
map<Node,double>::iterator my_const = node_map_.begin();
double c = - my_const->first.GetNodeValue();
node_map_.erase(node_map_.begin());
node_map_.insert(make_pair(Node(c),1));
UpdateAll();
}
int Cluster::ReplaceByConst(const string& name,double value)
{
Node fresh(name);
map<Node,double>::const_iterator it = node_map_.find(fresh);
if (it == node_map_.end())
return -1;
else
{
//int power = node_map_[fresh];
double power = node_map_[fresh];
node_map_.erase(fresh);
this->Mul(pow(value,power));
return 1;
}
}
//protected---
Cluster& Cluster::PowerBy(double p)
{
// cout<<"before C power"<<GetExpression()<<endl;
//p = 2;
double last_c = GetSingleValue();
double new_c = pow(last_c,p);
// cout<< "new_c"<<new_c<<endl;
node_map_.erase(node_map_.begin());
node_map_.insert(make_pair(Node(new_c),1));
map<Node,double>::iterator it;
for (it = ++node_map_.begin(); it != node_map_.end(); ++it)
node_map_[it->first] = it->second * p;
UpdateAll();
// cout<<"after C power"<<GetExpression()<<endl;
//Block tmp(*this);
//cout<<"cast->Block"<<tmp.GetExpression()<<endl;
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -