示例#1
0
 R operator()(T& t, M, const V& v, R r)
 {
   typedef typename M::first_target first_target;
   typedef typename M::second_target second_target;
   
   typedef typename first_target::serializer_tag first_tag;
   typedef typename second_target::serializer_tag second_tag;
   
   if ( t.get_aspect().template get<first_tag>().check(t, first_target(), v) )
   {
     r = t.get_aspect().template get<first_tag>()(t, first_target(), v, r);
     
     if ( !try_(t) )
       return r;
     
     if ( t.get_aspect().template get<second_tag>().check(t, second_target(), v) )
     {
       if ( !try_(t) )
         return r;
       
       if ( !r )
         return throw_(t, out_of_range(), r);
       
       *(r++)=',';
     }
   }
   
   r = t.get_aspect().template get<second_tag>()(t, second_target(), v, r);
   return r;
 }
    // Check derivatives with respect to a.
    void check_derivatives_da(cl::LinearRegression& lin_regr, std::vector<cl::tvalue>& forw, double eps)
    {
        int n = lin_regr.GetN();

        // Checking alpha derivatives.
        if (std::fabs(lin_regr.GetDerivate_dAlpha_dA() - forw[0].scalar_value_) > eps)
            throw_("Adjoint alpha derivative w.r.t a differs from expectation.");

        // Checking beta derivatives.
        if (std::fabs(lin_regr.GetDerivate_dBeta_dA() - forw[1].scalar_value_) > eps)
            throw_("Adjoint beta derivative w.r.t a differs from expectation.");

        // Checking y_estimate derivatives.
        for (int j = 0; j < n; j++)
        if (std::fabs(lin_regr.GetDerivate_dYEstim_dA(j) - forw[2].element_at(j)) > eps)
            throw_("Adjoint y_estimate derivative w.r.t a differs from expectation.");
    }
示例#3
0
  R operator()(T& t, R r)
  {
    if (!r)
      return throw_( t, unexpected_end_fragment(), r );

    if (*r!='{') 
      return throw_( t, expected_of("{",  distance(r) ), r );

    r = t.get_aspect().template get<_space_>()(t, ++r);
    
    if ( !try_(t) )
      return r;
    
    for ( ; r && *r!='}'; )
    {
      r = t.get_aspect().template get<_field_>()(t, r);
      
      if ( !try_(t) )
        return r;
      
      r = t.get_aspect().template get<_space_>()(t, r);
      
      if ( !try_(t) )
        return r;
      
      if (!r)
        return throw_( t, unexpected_end_fragment(), r );
      
      if (*r == ',')
      {
        ++r;
        
        r = t.get_aspect().template get<_space_>()(t, r);
        
        if ( !try_(t) )
          return r;
      }
      else if (*r != '}')
        return throw_( t, expected_of("}",  distance(r) ), r );
    }

    if (!r)
      return throw_( t, unexpected_end_fragment(), r );

    return ++r;
  }
    // Check derivatives with respect to b.
    void check_derivatives_db(cl::LinearRegression& lin_regr, std::vector<double>& forw, double eps)
    {
        int n = lin_regr.GetN();

        // Checking alpha derivatives.
        if (std::fabs(lin_regr.GetDerivate_dAlpha_dB() - forw[0]) > eps)
            throw_("Adjoint alpha derivative w.r.t a differs from expectation.");

        // Checking beta derivatives.
        if (std::fabs(lin_regr.GetDerivate_dBeta_dB() - forw[1]) > eps)
            throw_("Adjoint beta derivative w.r.t a differs from expectation.");

        // Checking y_estimate derivatives.
        for (int j = 0; j < n; j++)
        if (std::fabs(lin_regr.GetDerivate_dYEstim_dB(j) - forw[2 + j]) > eps)
            throw_("Adjoint y_estimate derivative w.r.t a differs from expectation.");
    }
示例#5
0
	static pointer malloc(size_t size) {
	    void *data = pointer();
	    if (size) {
		// std::cout << "malloc" << std::endl;
		throw_(cudaMalloc(&data, sizeof(T)*size));
		// std::cout << "malloc" << data << std::endl;
	    }
	    return static_cast<pointer>(data);
	}
示例#6
0
void TB_Castle::repair(int amount) const {
	if (health == maxHealth) {
#if _DEBUG_
		throw_("Attempt to repair not damaged castle.", "TB_Castle::repair");
#else
		return;
#endif
	}		
	health += amount;
}
示例#7
0
  R operator()(T& t, M, const V&, R r)
  {
    typename M::range_type rr = M().get_range();
    for(;rr; ++rr, ++r)
    {
      if ( !r )
        return throw_(t, out_of_range(), r);

      *r = *rr;
    }
    return r;
  }
示例#8
0
  R operator()(T& t, M, const V& v, R r)
  {
    typedef typename M::target target;
    typedef typename target::serializer_tag serializer_tag;

    if ( !r )
      return throw_(t, out_of_range(), r);

    *(r++)='{';
    
    r = t.get_aspect().template get<serializer_tag>()(t, target(), v, r);

    if ( !try_(t) )
      return r;
    
    if ( !r )
      return throw_(t, out_of_range(), r);
    
    *(r++)='}';
    
    return r;
  }
 // Calculate polynomial regression coefficients.
 std::vector<tdouble> calculate_estimation()
 {
     if (!is_coef_calculated)
         throw_("Regression coefficients are not calculated.");
     // Number of data points.
     int npoints = data_x_.size();
     std::vector<tdouble> estim_y(npoints);
     for (int i = 0; i < m_; i++)
         for (int j = 0; j < npoints; j++)
             estim_y[j] += coef_[i] * x_power_[i][j];
     is_estim_calculated = true;
     return estim_y;
 }
 // Calculate polynomial regression coefficients derivatives w.r.t data points (y).
 std::vector<std::vector<double>> calculate_coefficients_derivatives()
 {
     if (!is_coef_calculated)
         throw_("Regression coefficients are not calculated.");
     std::vector<std::vector<double>> d_coef_d_y(m_);
     // Number of data points.
     int npoints = data_x_.size();
     for (int i = 0; i < m_; i++)
     {
         d_coef_d_y[i].resize(npoints);
         for (int j = 0; j < npoints; j++)
             for (int k = 0; k < m_; k++)
                 d_coef_d_y[i][j] += (double)(mat_X_XT_inv_[i][k] * x_power_[k][j]);
     }
     return d_coef_d_y;
 }
示例#11
0
/**Extracts a number from the string
	\param s		- source
	\param value	- pointer to the target location for the number
	\param iter		- current cursor position if the source
	\param p		- service use, deals with memory leaks
	*/
void Interpreter::getValue(TInternalData *value_)
{
	float st = 10;
	//value->setValue(0);
	long double ld;
	long double* value = &ld;
	*value = 0;
	long double osn = 1;
	for (; *iter < s->size(); (*iter)++)
	{
		char temp = (*s)[*iter];
		if (temp == '.')
		{
			//If the secong comma occurs, the number is invalid
			if (st == 0.1)
				throw_(invalid_value);
			st = 0.1;
			osn = 1;
		}
		else
		{
			if (temp <= '9' && temp >= '0')
				if (st == 10)
					(*value) = (*value) * st + (temp - '0');
				else
					(*value) = (*value) + (temp - '0') * osn;
			else
			{
				value_->setValue(ld);
				return;
			}
		}
		osn *= st;
	}
	value_->setValue(ld);
}
示例#12
0
	    copy(I begin, I end, O output) {
		size_t size = sizeof(typename std:: iterator_traits<I>::value_type);
		size *= std::distance(begin, end);
		// std::cout << output << " " << begin << " " << size<<std::endl;
		throw_(cudaMemcpy(output, begin, size, K));
	    }
示例#13
0
  std::pair<R, RD> operator()(T& t, R r, RD rd)
  {
    std::pair<R, RD> rr(r, rd);
    if (!rr.first)
      return throw_( t, unexpected_end_fragment(), rr.first, rr.second );
      

    if (*rr.first!='{') 
      return throw_( t, expected_of("{",  distance(rr.first) ), rr.first, rr.second );

    if ( !rr.second )
      return throw_( t, out_of_range( distance(rr.first) ), rr.first, rr.second );
    
    *(rr.second++) = *(rr.first++);
    
    rr = t.get_aspect().template get<_space_>()(t, rr.first, rr.second);
    
    if ( !try_(t) )
      return rr;
    
    for ( ; rr.first && *rr.first!='}'; )
    {
      rr = t.get_aspect().template get<_field_>()(t, rr.first, rr.second);
      
      if ( !try_(t) )
        return rr;
      
      rr = t.get_aspect().template get<_space_>()(t, rr.first, rr.second);
      
      if ( !try_(t) )
        return rr;
      
      if (!rr.first)
        return throw_( t, unexpected_end_fragment(), rr.first, rr.second );
      
      if (*rr.first == ',')
      {
        if ( !rr.second )
          return throw_( t, out_of_range( distance(rr.first) ), rr.first, rr.second );
        
        *(rr.second++) = *(rr.first++);
        
        rr = t.get_aspect().template get<_space_>()(t, rr.first, rr.second);
        
        if ( !try_(t) )
          return rr;
      }
      else if (*rr.first != '}')
        return throw_( t, expected_of("}",  distance(rr.first) ), rr.first, rr.second );
    }

    if (!rr.first)
      return throw_( t, unexpected_end_fragment(), rr.first, rr.second );

    if ( !rr.second )
      return throw_( t, out_of_range( distance(rr.first) ), rr.first, rr.second );

    *(rr.second++) = *(rr.first++);
    
    return rr;
  }
示例#14
0
	static void free(void *data) {
	    if (data) {
		// std::cout << "free " << data << std::endl;
		throw_(cudaFree(data));
	    }
	}
示例#15
0
	void resize(size_t size,  char value) { 
	    resize(size);
	    if (size) throw_(cudaMemset(data_, value, size*sizeof(T)));
	}	    
示例#16
0
void Interpreter::makePolandList(EvalList *list)
{
	bool is_function = (v) ? true : false;

	goToNextSymbol();
	if (*iter >= s->size())
		throw_(invalid_expression);

	Interpreter::HistoryStack h_stack(this);
	Interpreter::PolStack p_stack(this);
	for (; (*iter) < s->size(); goToNextSymbol())
	{
		goToNextSymbol();
		if (!(*iter < s->size()))
			break;
		switch (h_stack.getLastType())
		{
		case val:
			switch ((*s)[*iter])
			{
			case '(':
			{
				{
					string* pr = new string("*");
					p_stack.push(Drobot::global_tree.find(pr), list);
					delete pr;
					p_stack.push(Drobot::ope_br, list);

					h_stack.pop();
					h_stack.push(bin_op);
					h_stack.incLastNumberOfArgumets();
					h_stack.push(op_br);
					++(*iter);
					continue;
				}
			}
			case ')':
				p_stack.push(')', list);

				h_stack.pop();
				if (h_stack.getLastType() != op_br)
					throw_(invalid_expression);

				h_stack.pop();

				switch (h_stack.getLastType())
				{
				case bin_op:
					h_stack.pop();
					h_stack.push(val);
					++(*iter);
					continue;
				case nothing:
					h_stack.push(val);
					++(*iter);
					continue;
				case val:
					throw(you_are_fool, p);
					continue;
				case func:
				{
					h_stack.check();
					h_stack.incLastNumberOfArgumets();
					short num = h_stack.getNumOfArguments();
					Quantum* tmp = Drobot::global_tree.find(h_stack.getName(), num);
					if (!tmp)
					{
						if (num == 1)
						{
							if (find(h_stack.getName()) && !h_stack.isUnchang())
							{
								list->add(new Variable(h_stack.getName()), h_stack.getNode());
								string* pr = new string("*");
								p_stack.push(Drobot::global_tree.find(pr), list);
								delete pr;
							}
							else
							{
								tmp = Drobot::global_tree.find(h_stack.getName(), 0);
								if (!tmp)
									throw_(unknown_name);
								if (h_stack.isUnchang())
									tmp = new Const(dynamic_cast<Const*>(tmp));
								list->add(tmp, h_stack.getNode());
								string* pr = new string("*");
								p_stack.push(Drobot::global_tree.find(pr), list);
								delete pr;
							}
						}
						else
							throw_(unknown_name);
					}
					else
					{
						if (h_stack.isUnchang())
							tmp = new Function(dynamic_cast<Function*>(tmp));
						p_stack.push(tmp, list);
					}
					h_stack.pop();
					if (h_stack.getLastType() == bin_op)
						h_stack.pop();
					h_stack.push(val);
					++(*iter);
					continue;
				}
				case op_br:
					h_stack.push(val);
					++(*iter);
					continue;
				}

				continue;
			case ',':
				p_stack.push(',', list);

				h_stack.pop();
				if (h_stack.getLastType() != op_br)
					throw_(invalid_expression);

				if (!h_stack.isFuncPre())
					throw_(invalid_expression);

				h_stack.pop();
				h_stack.incLastNumberOfArgumets();
				h_stack.push(op_br);
				++(*iter);
				continue;
			case '$':
				throw_(bad_position_of_reserved_symbol);
				continue;
			default:
			{
				char c = (*s)[*iter];
				if (c <= '9' && c >= '0')
				{
					TInternalData* tmp = new TInternalData(0.0);
					getValue(tmp);
					string* pr = new string("*");
					p_stack.push(Drobot::global_tree.find(pr), list);

					list->add(new Value(tmp));

					delete pr;
					continue;
				}

				bool found_in_ar_operators = false;
				for (int i = 0; i < strlen(ar_operators) && !found_in_ar_operators; ++i)
				{
					if (c == ar_operators[i])
					{
						found_in_ar_operators = true;
						string* st = new string;
						st->push_back(ar_operators[i]);
						p_stack.push(Drobot::global_tree.find(st), list);
						delete st;

						h_stack.pop();
						h_stack.push(bin_op);
						h_stack.incLastNumberOfArgumets();
						++(*iter);
						continue;
					}
				}

				if (!found_in_ar_operators)
				{
					string* st = new string;
					getName(st);

					bool uncheng = isConstMod();
					goToNextSymbol();

					string* pr = new string("*");
					p_stack.push(Drobot::global_tree.find(pr), list);
					delete pr;
					if (!(*iter < s->size()) || isConstName())
					{
						if (find(st))
						{
							if (uncheng)
							{
								Quantum* f = Drobot::global_tree.find(st, 0);
								if (!f)
								{
									delete st;
									throw_(invalid_expression);
								}
								else
								{
									delete st;
									list->add(new Const(dynamic_cast<Const*>(f)));
								}
							}
							else
							{
								list->add(new Variable(st));
								delete st;
							}
						}
						else
						{
							Quantum* f = Drobot::global_tree.find(st, 0);
							if (uncheng)
							{
								delete st;
								list->add(new Const(dynamic_cast<Const*>(f)));
							}
							else
							{
								delete st;
								list->add(f);
							}
						}
					}
					else
					{
						h_stack.pop();
						h_stack.push(bin_op);
						h_stack.push(func, st, list->_tail, uncheng);

						p_stack.push(Drobot::ope_br, list);
						h_stack.push(op_br);
						++(*iter);
					}
				}
				continue;
			}
			}
			continue;
		case bin_op:
			switch ((*s)[*iter])
			{
			case '(':
				p_stack.push(Drobot::ope_br, list);
				h_stack.push(op_br);
				++(*iter);
				continue;
			case ')':
				throw_(invalid_expression);
				continue;
			case ',':
				throw_(invalid_expression);
				continue;
			case '$':
				throw_(bad_position_of_reserved_symbol);
				continue;
			default:
			{
				char c = (*s)[*iter];

				if (c <= '9' && c >= '0')
				{
					TInternalData* tmp = new TInternalData(0.0);
					getValue(tmp);
					list->add(new Value(tmp));
					h_stack.pop();
					h_stack.push(val);
					continue;
				}

				for (int i = 0; i < strlen(ar_operators); ++i)
					if (c == ar_operators[i])
						throw_(invalid_expression);

				string* st = new string;
				getName(st);

				bool uncheng = isConstMod();
				goToNextSymbol();

				if (!(*iter < s->size()) || isConstName())
				{
					h_stack.pop();
					h_stack.push(val);
					if (find(st))
					{
						if (uncheng)
						{
							Quantum* f = Drobot::global_tree.find(st, 0);
							if (!f)
							{
								delete st;
								throw_(invalid_expression);
							}
							else
							{
								delete st;
								list->add(new Const(dynamic_cast<Const*>(f)));
							}
						}
						else
						{
							list->add(new Variable(st));
							delete st;
						}
					}
					else
					{
						Quantum* f = Drobot::global_tree.find(st, 0);
						if (uncheng)
						{
							delete st;
							list->add(new Const(dynamic_cast<Const*>(f)));
						}
						else
						{
							delete st;
							list->add(f);
						}
					}
				}
				else
				{
					h_stack.push(func, st, list->_tail, uncheng);

					p_stack.push(Drobot::ope_br, list);
					h_stack.push(op_br);
					++(*iter);
				}
			}
			}
			continue;
		case nothing:
			switch ((*s)[*iter])
			{
			case '(':
				p_stack.push(Drobot::ope_br, list);
				h_stack.push(op_br);
				++(*iter);
				continue;
			case ')':
				throw_(invalid_bracket_position);
				continue;
			case ',':
				throw_(invalid_expression);
				continue;
			case '$':
				throw_(bad_position_of_reserved_symbol);
				continue;
			default:
			{
				char c = (*s)[*iter];

				if (c <= '9' && c >= '0')
				{
					TInternalData* tmp = new TInternalData(0.0);
					getValue(tmp);
					list->add(new Value(tmp));

					h_stack.push(val);
					continue;
				}

				if (c == '-')
				{
					string* pr = new string("*");
					TInternalData* ch = new TInternalData(-1);
					list->add(new Value(ch));
					p_stack.push(Drobot::global_tree.find(pr), list);
					h_stack.push(bin_op);
					delete pr;
					++(*iter);
					continue;
				}

				if (c == '+')
				{
					++(*iter);
					continue;
				}

				for (int i = 0; i < strlen(ar_operators); ++i)
					if (c == ar_operators[i])
						throw_(invalid_expression);

				string* st = new string;
				getName(st);

				bool uncheng = isConstMod();
				goToNextSymbol();

				if (!(*iter < s->size()) || isConstName())
				{
					h_stack.push(val);
					if (find(st))
					{
						if (uncheng)
						{
							Quantum* f = Drobot::global_tree.find(st, 0);
							if (!f)
							{
								delete st;
								throw_(invalid_expression);
							}
							else
							{
								delete st;
								list->add(new Const(dynamic_cast<Const*>(f)));
							}
						}
						else
						{
							list->add(new Variable(st));
							delete st;
						}
					}
					else
					{
						Quantum* f = Drobot::global_tree.find(st, 0);
						if (uncheng)
						{
							delete st;
							list->add(new Const(dynamic_cast<Const*>(f)));
						}
						else
						{
							delete st;
							list->add(f);
						}
					}
				}
				else
				{
					h_stack.push(func, st, list->_tail, uncheng);

					p_stack.push(Drobot::ope_br, list);
					h_stack.push(op_br);
					++(*iter);
				}
			}
			}
			continue;
		case op_br:
			switch ((*s)[*iter])
			{
			case '(':
				p_stack.push(Drobot::ope_br, list);
				h_stack.push(op_br);
				(*iter)++;
				continue;
			case ')':
				p_stack.push(')', list);
				h_stack.pop();
				++(*iter);
				continue;
			case ',':
				throw_(invalid_expression);
				continue;
			case '$':
				throw_(bad_position_of_reserved_symbol);
				continue;
			default:
			{
				char c = (*s)[*iter];

				if (c <= '9' && c >= '0')
				{
					TInternalData* tmp = new TInternalData(0.0);
					getValue(tmp);
					list->add(new Value(tmp));

					h_stack.push(val);
					continue;
				}

				if (c == '-')
				{
					string* pr = new string("*");
					TInternalData* ch = new TInternalData(-1);
					list->add(new Value(ch));
					p_stack.push(Drobot::global_tree.find(pr), list);
					h_stack.push(bin_op);
					delete pr;
					++(*iter);
					continue;
				}

				if (c == '+')
				{
					++(*iter);
					continue;
				}

				for (int i = 0; i < strlen(ar_operators); ++i)
					if (c == ar_operators[i])
						throw_(invalid_expression);

				string* st = new string;
				getName(st);

				bool uncheng = isConstMod();
				goToNextSymbol();

				if (!(*iter < s->size()) || isConstName())
				{
					h_stack.push(val);
					if (find(st))
					{
						if (uncheng)
						{
							Quantum* f = Drobot::global_tree.find(st, 0);
							if (!f)
							{
								delete st;
								throw_(invalid_expression);
							}
							else
							{
								delete st;
								list->add(new Const(dynamic_cast<Const*>(f)));
							}
						}
						else
						{
							list->add(new Variable(st));
							delete st;
						}
					}
					else
					{
						Quantum* f = Drobot::global_tree.find(st, 0);
						if (uncheng)
						{
							delete st;
							list->add(new Const(dynamic_cast<Const*>(f)));
						}
						else
						{
							delete st;
							list->add(f);
						}
					}
				}
				else
				{
					h_stack.push(func, st, list->_tail, uncheng);

					p_stack.push(Drobot::ope_br, list);
					h_stack.push(op_br);
					++(*iter);
				}
				/*Quantum* f = Drobot::global_tree.find(st);
				if (!f)
				{
				if (!is_function)
				throw_(unknown_name, p);
				if (!find(v, st))
				throw_(unknown_name, p);
				list->add(new Variable(st));
				h_stack.push(val);

				delete st;
				continue;
				}

				if (f->GetType() == con)
				{
				if (isConstMod(s, iter))
				list->add(new Const(dynamic_cast<Const*>(f)));
				else
				list->add(f);
				delete st;
				h_stack.push(val);
				continue;
				}
				bool uncheng = isConstMod(s, iter);

				goToNextSymbol(s, iter);
				if (!(*iter < s->size()))
				throw_(wrong_number_of_argument, p);
				if ((*s)[*iter] != '(')
				throw_(invalid_operand, p);

				if (uncheng)
				p_stack.push(new Function(dynamic_cast<Function*>(f)), list);
				else
				p_stack.push(f, list);

				p_stack.push(Drobot::ope_br, list);
				h_stack.push(func, f);
				h_stack.push(op_br);
				(*iter)++;
				continue;*/
			}
			}
		}
	}

	if (!h_stack.isCorrect())
		throw_(invalid_expression);

	for (; !p_stack.isEmpty();)
		if (p_stack.isOpenedBr())
			throw_(invalid_expression);
		else
			list->add(p_stack.pop());
}
示例#17
0
    static void check_status() {
	throw_(cudaGetLastError());
    }