📄 ad_item.cc
字号:
/* HYSDEL Copyright (C) 1999-2002 Fabio D. Torrisi This file is part of HYSDEL. HYSDEL is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. HYSDEL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA CONTACT INFORMATION =================== Fabio D. Torrisi ETH Zentrum Physikstrasse. 3 ETL, CH-8032 Zurich Switzerland mailto:torrisi@aut.ee.ethz.ch (preferred)*/#include "AD_item.h"#include "MLD_representation.h"#include "Min_max_eps.h"#include "Symbol_table.h"#include "Neg_expr.h"#include "Minus_expr.h"#include "Number_expr.h"#include "Variable_expr.h"#include "Plus_expr.h"#include "Mult_expr.h"#include "Cast_log2real_expr.h"#include "Var_symbol.h"#include "Affine_func.h"#include <stdio.h>#include "Globals.h"#include "Problem.h"#include "Problem_handler.h"AD_item::AD_item(const Var_symbol * lhs, Expr * aff, Min_max_eps * mme, const Globals * glob) : Definition_item(lhs, glob) { affine_expr = aff; this->mme = mme; min_computed=false; max_computed=false; eps_computed=false;}list < const Var_symbol * > AD_item::get_required_mme() const { list < const Var_symbol * > req; return req;}list < const Var_symbol * > AD_item::get_required_simu() const { list < const Var_symbol * > req; req = get_required_mme(); add_required(req, affine_expr); return req;}AD_item::~AD_item() { delete affine_expr; delete mme; this->Definition_item::~Definition_item();}string AD_item::arg_range_check_matlab() const { string res; res += affine_expr->arg_range_check_matlab(); if (mme) res += mme->arg_range_check_matlab(); return res;}MLD_representation * AD_item::translate_MLD() const { MLD_representation * mld; Expr * tmp1, * tmp2, * tmp3, * ineq_expr; Expr * lhs_var_expr; Affine_func * aff; int subind_cnt = 0; assert(mme); //fine mld = new MLD_representation(globals); lhs_var_expr = new Cast_log2real_expr( new Variable_expr(lhs_var, globals)); // mme->eps + (mme->min - mme->eps)*lhs - aff <= 0 tmp1 = new Minus_expr(mme->get_min(), mme->get_eps()); tmp2 = new Mult_expr(tmp1, lhs_var_expr->clone()); tmp3 = new Plus_expr(mme->get_eps(), tmp2); ineq_expr = new Minus_expr(tmp3, affine_expr->clone()); aff = ineq_expr->compute_affine(); aff_leq_zero_to_mld(aff, mld, & subind_cnt); delete aff; //aff + mme->max*(lhs-1) <= 0 tmp1 = new Minus_expr(lhs_var_expr->clone(), new Number_expr(1.0, globals)); tmp2 = new Mult_expr(mme->get_max(), tmp1); ineq_expr = new Plus_expr(affine_expr->clone(), tmp2); aff = ineq_expr->compute_affine(); aff_leq_zero_to_mld(aff, mld, & subind_cnt); delete aff; delete lhs_var_expr; return mld;}void AD_item::compute_minmaxeps() { Affine_func * aff; Min_max_eps * auto_mme; aff = affine_expr->compute_affine(); try { auto_mme = compute_mme_from_aff(aff); } catch (Problem * p) { if (!mme) globals->problem_handler->process(p); else auto_mme = NULL; } if (mme && auto_mme) check_mme_tight(mme, auto_mme); if (!mme) { mme = auto_mme; min_computed=true; max_computed=true; eps_computed=true; } if (auto_mme && mme != auto_mme) delete auto_mme; delete aff;}list < Item * > AD_item::unroll() { list < Item * > unr; list < Item * >::iterator iter; unr = affine_expr->unroll(); for (iter = unr.begin(); iter != unr.end(); iter++) (* iter)->set_unrolled_from(this); return unr;}/** lhs must be bool AUX, affine_expr must be affine, check mme */void AD_item::semantic_checks() { string msg; char buf[20]; if (lhs_var->get_kind() != AUX_KIND) { sprintf(buf, "line %d: ", get_source_line()); msg = buf; msg += string("left hand side variable ") + lhs_var->get_name() + string(" is not auxiliary"); msg += string(" (") + lhs_var->get_name() + string(" declared at line "); sprintf(buf, "%d)", lhs_var->get_line_of_decl()); msg += string(buf); globals->problem_handler->process(new Problem(ERROR, msg)); } if (lhs_var->get_type() != BOOL_TYPE) { sprintf(buf, "line %d: ", get_source_line()); msg = buf; msg += string("left hand side variable ") + lhs_var->get_name() + string(" is not Boolean"); msg += string(" {") + lhs_var->get_name() + string(" declared at line "); sprintf(buf, "%d)", lhs_var->get_line_of_decl()); msg += string(buf); globals->problem_handler->process(new Problem(ERROR, msg)); } if (is_required_simu(lhs_var)) { sprintf(buf, "line %d: ", get_source_line()); msg = buf; msg += string("recursive definition of variable ") + lhs_var->get_name(); globals->problem_handler->process(new Problem(ERROR, msg)); } affine_expr->semantic_checks(); if (!affine_expr->is_affine()) { sprintf(buf, "line %d: ", get_source_line()); msg = buf; msg += string("expression must be affine"); globals->problem_handler->process(new Problem(ERROR, msg)); } if (mme) mme->semantic_checks();}string AD_item::matlab_simu() const { string res; res += string("% ") + get_source() + string("\n"); res += string("within(") + affine_expr->to_matlab() + string(", ") + mme->get_min()->to_matlab() + string(", ") + mme->get_max()->to_matlab() + string(", ") + get_source_line_str() + string(");\n"); res += string("if ") + affine_expr->to_matlab() + string(" <= 0\n") + string("\t") + lhs_var->to_matlab() + string(" = 1;\n"); res += string("else\n") + string("\t") + lhs_var->to_matlab() + string(" = 0;\n"); res += string("end\n"); res += string("\n"); return res;}bool AD_item::minmax_known() const { return mme != NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -