📄 block_imp.cpp
字号:
#include "stdafx.h"
#include "block.h"
Block::Block(const Cluster& n)//:const_(0)//很有可能马上被修改!
:last_me_(NULL)
{
cluster_set_.insert(n); //Cluster认为所有的常数是相等的!!!
cluster_set_.insert(Cluster(double(0)));//可能失败!!
// trace("C -> B\n");
// cout<<cluster_set_.size()<<" size\n";
UpdateAll();
}
Block::Block(const Block& l):cluster_set_(l.cluster_set_),expression_(l.expression_)
{
//delete last_me_;这是构造函数!!!!!
trace("Block -> Block constructor called\n");
if (l.last_me_ == NULL)
last_me_ = NULL;
//你不含子信息时我如何保留子信息!!!
else // l.last_me_ != null
last_me_ = new Block(*l.last_me_);
UpdateAll();
}
Block& Block::operator=(const Block& l)
{
trace("Block = Block called\n");
if (&l == this)
;
else
{
cluster_set_ = l.cluster_set_;
// cout<<(cluster_set_.begin())->GetExpression()<<endl;
expression_ = l.expression_;
//expression_ = l.GetExpression();
delete last_me_; //赋值 和 copy construct 是有区别的!!!
if (l.last_me_ == NULL)
last_me_ = NULL;
else // l.last_me_ != null
last_me_ = new Block(*l.last_me_);
}
UpdateAll();
return *this;
}
void Block::UpdateAll()
{
// const head ++
string head =
ForConst(cluster_set_.begin()->GetSingleValue());
//cout<<"head"<<head<<endl;
head += " + ";
//const head--
//frame ++
string frame;
set<Cluster>::const_iterator it;
for (it = ++cluster_set_.begin() ; it != cluster_set_.end();++it)
{
string tmp = it->GetExpression();
//+++++++++++++++++++++++++++++++++++++++++++++++++
/* if (tmp[0] == '0') // bug here wasted 3hours or so
continue;*/
// ++++++++++++++++++++++++++++++++++++++++++++++
frame += tmp + " + "; // 去冗余加号!表现形式!
}
expression_ = head + frame;
ExpressionCut(expression_);
}
void Block::ExpressionCut(string& in)
{
//去冗余的0
if (in[0] == '0' && in[2] == '+' && in.size() > 4)
in = in.substr(4,in.size());
in.resize(in.size() - 3); // cut off tail
}
Block& Block::Add(const Block& b)
{
double c = GetSingleValue() + b.GetSingleValue();
cluster_set_.erase(cluster_set_.begin());
cluster_set_.insert(Cluster(c));
set<Cluster>::const_iterator it;
set<Cluster>::iterator im;
for (it = ++b.cluster_set_.begin(); it != b.cluster_set_.end() ; ++it)
{
if ((im = cluster_set_.find(*it)) != cluster_set_.end())
im->MergeWeight(*it);
else
cluster_set_.insert(*it); //能保证不是0*x之类的吗???
}
UpdateAll();
return *this;
}
void Block::ReverseAllWeight()
{
set<Cluster>::iterator it;
for (it = cluster_set_.begin(); it != cluster_set_.end();++it)
it->ReverseWeight();
UpdateAll();
}
Block& Block::Sub(const Block& b)
{
Block copy_b(b);
copy_b.ReverseAllWeight();
this->Add(copy_b);
return *this;
}
Block& Block::Mul(const Block& b)
{
Block me_copy(*this);
Block me_new(0);
set<Cluster>::const_iterator it;
for (it = b.cluster_set_.begin(); it != b.cluster_set_.end();++it)
{
Block cur_bl(me_copy);
cur_bl.MulCluster(*it);
me_new.Add(cur_bl);
}
*this = me_new;
return *this;
}
void Block::MulCluster(const Cluster& cl)
{
Block tmp(0);
set<Cluster>::const_iterator it;
for (it = cluster_set_.begin(); it != cluster_set_.end();++it)
{
Cluster cur_pro(*it);
cur_pro.Mul(cl);
tmp.Add(Block(cur_pro)); // Block cast needed!!!
}
*this = tmp;
}
void Block::ReplaceByConst(const string& name,double value)
{
Block new_block(this->GetSingleValue());
set<Cluster>::const_iterator it;
for (it = ++cluster_set_.begin(); it != cluster_set_.end();++it)
{
Cluster key_copy(*it);
// 有时候不能写得太紧密?!?!+
key_copy.ReplaceByConst(name,value);
//Block tmp(key_copy);
// 有时候不能写得太紧密?!?!-
new_block.Add(key_copy);
}
*this = new_block;
}
//===============================================
Block operator +(const Block& lb,const Block rb)
{
//cout<<"in + \n";
//cout<<lb.GetExpression()<<" lb\n";
//Block y(lb);
Block y(lb);
//cout<<y.GetExpression()<<" y\n";
y.Add(rb);
//cout<<y.GetExpression()<<"in + \n";
return y;
}
Block operator -(const Block& lb,const Block rb)
{
Block y(lb);
y.Sub(rb);
return y;
}
Block operator *(const Block& lb,const Block rb)
{
Block y(lb);
y.Mul(rb);
return y;
}
Block operator ^(const Block& lb,int n) //展开式
{
Block y(1);
for (int i = 0; i < n; ++i)
y.Mul(lb);
return y;
}
Block& Block::PowerBy(double p) //不展开!!!
{
if (IsConst()) //一项
{
Cluster cur = *cluster_set_.begin();
cur.PowerBy(p);
*this = cur;
return *this;
}
//=========================================================
if (HasSingleFactor()) //一项
{
Cluster cur = *(++cluster_set_.begin());
cur.PowerBy(p);
if (last_me_ != NULL)
{
Block back_up = Block(*last_me_);
*this = cur;
last_me_ = new Block(back_up);
}
else
*this = cur;
}
else
{ //不可运算
Block me_copy(*this);
string long_name = "<" + this->GetExpression() + ">";
Node long_node(long_name);
Cluster cur(long_node);
cur.PowerBy(p);
*this = cur;
//(this->last_me_) = me_copy;
last_me_ = new Block(me_copy);
}
//if (GetExpression()[0] == '<')
if (expression_[expression_.size() - 1] == '>')
//*this = *(this->last_me_);
{//cout<<(last_me_ == 0)<< "last_me_ == 0\n";
//cout<<last_me_->GetExpression()<<" last_me"<<endl;
/*Block tmp = *last_me_;
*this = tmp;*/
//cout<<typeid(*last_me_).name()<<endl; //多态!!!
*this = Block(*last_me_);
}
return *this;
//=========================================================
}
Block operator -(const Block& lb)
{
return Block(0) - lb;
}
Block operator /(const Block& lb,const Block rb)
{
//return lb * Power(rb,-1);//不展开
Block y(rb);
y.PowerBy(-1);
y = lb * y;
return y;
}
//===================================================
vector<Block> AssignFactor(const Block& block, const Block& block_cell)
{
vector<Block> y;
y.push_back(block.GetSingleValue());
Node node(block_cell);
set<Cluster>::const_iterator si;
for (si = ++block.cluster_set_.begin(); si != block.cluster_set_.end();++si)
{
map<Node,double>::const_iterator mi = si->node_map_.find(node);
if (mi != si->node_map_.end()) //在串中找到node 键
{
//int pow = mi->second; // get powet
double pow = mi->second;
Cluster thin(*si); // copy此串
for (int i = 0;i < pow;++i)
thin.Div(node); // clear this node;
if (pow > y.size() - 1) // y.size() - 1 means max power!!!
y.resize(pow + 1);
y[pow].Add(Block(thin)); // log in
}
//次串不含node键
else
y[0].Add(Block(*si));
}
return y;
}
Block Delta(const vector<Block>& vb)
{
if (vb.size() != 3)
{
cout<<"paramter error!\n";
return Block(0);
}
else
return vb[1] * vb[1] - vb[0] * vb[2] * 4;
}
pair<Block,Block> Root_2(const vector<Block>& vb)
{
pair<Block,Block> y;
Block delta = Delta(vb);
if (delta.IsConst() && delta.GetSingleValue() < 0)
{
Block i("i");
Block sqr_delta = (-delta).PowerBy(0.5);
y.first = ( - vb[1] - sqr_delta) * i / (vb[2] * 2);
y.second = ( - vb[1] + sqr_delta) * i / (vb[2] * 2);
}
else
{
Block sqr_delta = delta.PowerBy(0.5);
y.first = ( - vb[1] - sqr_delta) / (vb[2] * 2);
y.second = (-vb[1] + sqr_delta) / (vb[2] * 2);
}
return y;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -