Esempio n. 1
0
START_TEST (test_structs){
	hash_t structure[] = {
		{ HK(key1), DATA_HASHT(hash_end) },
		{ HK(key2), DATA_HASHT(hash_end) },
		{ HK(key3), DATA_HASHT(
			{ HK(default), DATA_UINT32T(0) }, 
			hash_end
		)},
		{ HK(key4), DATA_HASHT(hash_end) },
		hash_end
	};
	
	request_t values[] = {
		{ HK(key4), DATA_STRING("hello") },
		{ HK(key1), DATA_UINT32T(100)    },
		{ HK(key2), DATA_OFFT(10)        },
		{ HK(key3), DATA_UINT32T(0)      },
		hash_end
	};
	
	ssize_t ret;
	char    test[100] = {0};
	char    orig[] =
		"\x64\x00\x00\x00"
		"\x0A\x00\x00\x00\x00\x00\x00\x00"
		"\x00\x00\x00\x00"
		"hello\x00";
	data_t  test_data = DATA_RAW(test, 100);
	
	ret = struct_pack(structure, values, &test_data);
		fail_unless(ret > 0,                                "struct_pack failed");
Esempio n. 2
0
Type objective_function<Type>::operator() ()
{
  DATA_STRING(distr);
  DATA_INTEGER(n);
  Type ans = 0;

  if (distr == "norm") {
    PARAMETER(mu);
    PARAMETER(sd);
    vector<Type> x = rnorm(n, mu, sd);
    ans -= dnorm(x, mu, sd, true).sum();
  }
  else if (distr == "gamma") {
    PARAMETER(shape);
    PARAMETER(scale);
    vector<Type> x = rgamma(n, shape, scale);
    ans -= dgamma(x, shape, scale, true).sum();
  }
  else if (distr == "pois") {
    PARAMETER(lambda);
    vector<Type> x = rpois(n, lambda);
    ans -= dpois(x, lambda, true).sum();
  }
  else if (distr == "compois") {
    PARAMETER(mode);
    PARAMETER(nu);
    vector<Type> x = rcompois(n, mode, nu);
    ans -= dcompois(x, mode, nu, true).sum();
  }
  else if (distr == "compois2") {
    PARAMETER(mean);
    PARAMETER(nu);
    vector<Type> x = rcompois2(n, mean, nu);
    ans -= dcompois2(x, mean, nu, true).sum();
  }
  else if (distr == "nbinom") {
    PARAMETER(size);
    PARAMETER(prob);
    vector<Type> x = rnbinom(n, size, prob);
    ans -= dnbinom(x, size, prob, true).sum();
  }
  else if (distr == "nbinom2") {
    PARAMETER(mu);
    PARAMETER(var);
    vector<Type> x = rnbinom2(n, mu, var);
    ans -= dnbinom2(x, mu, var, true).sum();
  }
  else if (distr == "exp") {
    PARAMETER(rate);
    vector<Type> x = rexp(n, rate);
    ans -= dexp(x, rate, true).sum();
  }
  else if (distr == "beta") {
    PARAMETER(shape1);
    PARAMETER(shape2);
    vector<Type> x = rbeta(n, shape1, shape2);
    ans -= dbeta(x, shape1, shape2, true).sum();
  }
  else if (distr == "f") {
    PARAMETER(df1);
    PARAMETER(df2);
    vector<Type> x = rf(n, df1, df2);
    ans -= df(x, df1, df2, true).sum();
  }
  else if (distr == "logis") {
    PARAMETER(location);
    PARAMETER(scale);
    vector<Type> x = rlogis(n, location, scale);
    ans -= dlogis(x, location, scale, true).sum();
  }
  else if (distr == "t") {
    PARAMETER(df);
    vector<Type> x = rt(n, df);
    ans -= dt(x, df, true).sum();
  }
  else if (distr == "weibull") {
    PARAMETER(shape);
    PARAMETER(scale);
    vector<Type> x = rweibull(n, shape, scale);
    ans -= dweibull(x, shape, scale, true).sum();
  }
  else if (distr == "AR1") {
    PARAMETER(phi);
    vector<Type> x(n);
    density::AR1(phi).simulate(x);
    ans += density::AR1(phi)(x);
  }
  else if (distr == "ARk") {
    PARAMETER_VECTOR(phi);
    vector<Type> x(n);
    density::ARk(phi).simulate(x);
    ans += density::ARk(phi)(x);
  }
  else if (distr == "MVNORM") {
    PARAMETER(phi);
    matrix<Type> Sigma(5,5);
    for(int i=0; i<Sigma.rows(); i++)
      for(int j=0; j<Sigma.rows(); j++)
        Sigma(i,j) = exp( -phi * abs(i - j) );
    density::MVNORM_t<Type> nldens = density::MVNORM(Sigma);
    for(int i = 0; i<n; i++) {
      vector<Type> x = nldens.simulate();
      ans += nldens(x);
    }
  }
  else if (distr == "SEPARABLE") {
    PARAMETER(phi1);
    PARAMETER_VECTOR(phi2);
    array<Type> x(100, 200);
    SEPARABLE( density::ARk(phi2), density::AR1(phi1) ).simulate(x);
    ans += SEPARABLE( density::ARk(phi2), density::AR1(phi1) )(x);
  }
  else if (distr == "GMRF") {
    PARAMETER(delta);
    matrix<Type> Q0(5, 5);
    Q0 <<
      1,-1, 0, 0, 0,
     -1, 2,-1, 0, 0,
      0,-1, 2,-1, 0,
      0, 0,-1, 2,-1,
      0, 0, 0,-1, 1;
    Q0.diagonal().array() += delta;
    Eigen::SparseMatrix<Type> Q = asSparseMatrix(Q0);
    vector<Type> x(5);
    for(int i = 0; i<n; i++) {
      density::GMRF(Q).simulate(x);
      ans += density::GMRF(Q)(x);
    }
  }
  else if (distr == "SEPARABLE_NESTED") {
    PARAMETER(phi1);
    PARAMETER(phi2);
    PARAMETER(delta);
    matrix<Type> Q0(5, 5);
    Q0 <<
      1,-1, 0, 0, 0,
     -1, 2,-1, 0, 0,
      0,-1, 2,-1, 0,
      0, 0,-1, 2,-1,
      0, 0, 0,-1, 1;
    Q0.diagonal().array() += delta;
    Eigen::SparseMatrix<Type> Q = asSparseMatrix(Q0);
    array<Type> x(5, 6, 7);
    for(int i = 0; i<n; i++) {
      SEPARABLE(density::AR1(phi2),
                SEPARABLE(density::AR1(phi1),
                          density::GMRF(Q) ) ).simulate(x);
      ans += SEPARABLE(density::AR1(phi2),
                       SEPARABLE(density::AR1(phi1),
                                 density::GMRF(Q) ) )(x);
    }
  }
  else error( ("Invalid distribution '" + distr + "'").c_str() );
  return ans;
}
Esempio n. 3
0
	void 
	display::start(
		__in_opt const mirra::parameter_t &parameter
		)
	{
		std::string title;
		uint32_t height, width;
		mirra::parameter_t::const_iterator iter;
		mirra::object_parameter_t::const_iterator attribute_iter;

		if(!m_initialized) {
			THROW_MIRRA_DISPLAY_EXCEPTION(MIRRA_DISPLAY_EXCEPTION_UNINITIALIZED);
		}

		if(m_started) {
			THROW_MIRRA_DISPLAY_EXCEPTION(MIRAR_DISPLAY_EXCEPTION_STARTED);
		}

		height = DISPLAY_PARAMETER_DEFAULT_HEIGHT;
		title = DISPLAY_PARAMETER_DEFAULT_TITLE;
		width = DISPLAY_PRAAMETER_DEFAULT_WIDTH;

		iter = parameter.find(OBJECT_DISPLAY);
		if(iter != parameter.end()) {

			attribute_iter = iter->second.find(DISPLAY_PARAMETER_HEIGHT);
			if(attribute_iter != iter->second.end()) {

				if(attribute_iter->second.type != DATA_UNSIGNED) {
					THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_INVALID_PARAMETER,
						"%s: %s (expecting %s)", DISPLAY_PARAMETER_STRING(DISPLAY_PARAMETER_HEIGHT),
						DATA_STRING(attribute_iter->second.type), DATA_STRING(DATA_UNSIGNED));
				}

				height = attribute_iter->second.data.uvalue;
			}

			attribute_iter = iter->second.find(DISPLAY_PARAMETER_TITLE);
			if(attribute_iter != iter->second.end()) {

				if(attribute_iter->second.type != DATA_STRING) {
					THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_INVALID_PARAMETER,
						"%s: %s (expecting %s)", DISPLAY_PARAMETER_STRING(DISPLAY_PARAMETER_TITLE),
						DATA_STRING(attribute_iter->second.type), DATA_STRING(DATA_STRING));
				}

				title = (attribute_iter->second.data.strvalue ? attribute_iter->second.data.strvalue :
					STRING_EMPTY);
			}

			attribute_iter = iter->second.find(DISPLAY_PARAMETER_WIDTH);
			if(attribute_iter != iter->second.end()) {

				if(attribute_iter->second.type != DATA_UNSIGNED) {
					THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_INVALID_PARAMETER,
						"%s: %s (expecting %s)", DISPLAY_PARAMETER_STRING(DISPLAY_PARAMETER_WIDTH),
						DATA_STRING(attribute_iter->second.type), DATA_STRING(DATA_UNSIGNED));
				}

				width = attribute_iter->second.data.uvalue;
			}
		}

		m_window = SDL_CreateWindow(STRING_CHECK(title), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
			width, height, WINDOW_INIT_FLAGS);

		if(!m_window) {
			THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_EXTERNAL,
				"SDL_CreateWindow: %s", SDL_GetError());
		}

		m_window_renderer = SDL_CreateRenderer(m_window, SCALAR_INVALID(int), WINDOW_RENDERER_INIT_FLAGS);
		if(!m_window_renderer) {
			THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_EXTERNAL,
				"SDL_CreateRenderer: %s", SDL_GetError());
		}

		if(SDL_GetRendererOutputSize(m_window_renderer, &m_renderer_width, &m_renderer_height)) {
			THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_EXTERNAL,
				"SDL_GetRendererOutputSize: %s", SDL_GetError());
		}

		if(SDL_SetRenderDrawBlendMode(m_window_renderer, SDL_BLENDMODE_BLEND)) {
			THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_EXTERNAL,
				"SDL_SetRenderDrawBlendMode: %s", SDL_GetError());
		}

		m_window_texture = SDL_CreateTexture(m_window_renderer, SDL_PIXELFORMAT_BGRA8888, SDL_TEXTUREACCESS_STREAMING,
			width, height);

		if(!m_window_texture) {
			THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_EXTERNAL,
				"SDL_CreateTexture: %s", SDL_GetError());
		}

		if(SDL_SetTextureBlendMode(m_window_texture, SDL_BLENDMODE_BLEND)) {
			THROW_MIRRA_DISPLAY_EXCEPTION_FORMAT(MIRRA_DISPLAY_EXCEPTION_EXTERNAL,
				"SDL_SetTextureBlendMode: %s", SDL_GetError());
		}

		m_started = true;
		clear();
	}