Пример #1
0
void backward(int K, int T, double * transmat, double * obslik, double * alpha, double * gamma, double * beta)
{
	int t, d;

	/* special care for eta since it has 3 dimensions */
	int eta_ndim = 3;
	int * eta_dims = (int *)malloc(eta_ndim*sizeof(int));

	/* temporary quantities in the algorithm */
	double *m = (double *)malloc(K*sizeof(double));
	double *b = (double *)malloc(K*sizeof(double));			// (K-1)*(T+1)
	double *eta = (double *)malloc(K*K*T*sizeof(double));
	double *squareSpace = (double *)malloc(K*K*sizeof(double));

	t = T - 1;
	/* I don't think we need to initialize beta to all zeros. */
	for (d = 0; d<K; ++d) {
		beta[d*T + t] = 1;
		gamma[d*T + t] = alpha[d*T + t];
	}

	///* Put the last slice of eta as zeros, to be compatible with Sohrab and Gavin's code.
	//There are no values to put there anyways. This means that you can't normalise the
	//last matrix in eta, but it shouldn't be used. Note the d<K*K range.
	//*/
	//for (d = 0; d<(K*K); ++d) {
	//	/*mexPrintf("setting *(eta + %d) = 0 \n", d+t*K*K);*/
	//	*(eta + d*T + t) = 0;/*(double)7.0f;*/
	//}

	/* We have to remember that the 1:T range in Matlab is 0:(T-1) in C. */
	for (t = (T - 2); t >= 0; --t) {

		/* setting beta */
		multiplyInPlace(b, beta + t + 1, obslik + t + 1, K, 1, T, T);
		/* Using "m" again instead of defining a new temporary variable.
		   We using a lot of lines to say
		   beta(:,t) = normalize(transmat * b);
		*/
		multiplyMatrixInPlace(m, transmat, b, K, 1);
		normalizeInPlace(m, K, 1);
		for (d = 0; d<K; ++d) { beta[d*T + t] = m[d]; }
		/* using "m" again as valueholder */

		///* setting eta, whether we want it or not in the output */
		//outerProductUVInPlace(squareSpace, alpha + t, b, K, T);
		//componentVectorMultiplyInPlace(eta + t, transmat, squareSpace, K, T);
		//normalizeInPlace(eta + t, K*K, T);

		/* setting gamma */
		multiplyInPlace(m, alpha + t, beta + t, K, 1, T, T);
		normalizeInPlace(m, K, 1);
		for (d = 0; d<K; ++d) { gamma[d*T + t] = m[d]; }
	}

	free(b); free(m); free(squareSpace);
	free(eta); free(eta_dims);

	return;
}
Пример #2
0
double forward(int K, int T, double * init_state_distrib, double * transmat, double * obslik, double * alpha)
{
	int t = 0;

	double scale = 0.0f;
	double loglik = 0.0f;
	
	double * transmatT = (double *)malloc(K*K*sizeof(double));
	double *m = (double *)malloc(K*sizeof(double)); /* temporary quantities in the algorithm */

	/* the tranposed version of transmat*/
	transposeSquareInPlace(transmatT, transmat, K, K);

	multiplyInPlace(alpha, init_state_distrib, obslik, K, T, 1, T);
	scale = normalizeInPlace(alpha, K, T);
	loglik = log(scale);

	for (t = 1; t<T; ++t){
		multiplyMatrixInPlace(m, transmatT, alpha + (t - 1), K, T);
		multiplyInPlace(alpha + t, m, obslik + t, K, T, 1, T);
		scale = normalizeInPlace(alpha + t, K, T);
		loglik += log(scale);
	}

	free(m);
	free(transmatT);

	return loglik;
}
Пример #3
0
void graphics_ParticleSystem_update(graphics_ParticleSystem *ps, float dt) {
  if(ps->pMem == 0 || dt == 0.0f) {
    return;
  }

  graphics_Particle *p = ps->pHead;

  while(p) {
    p->life -= dt;

    if(p->life <= 0.0f) {
      p = removeParticle(ps, p);
      continue;
    }

    float radialX = p->position[0] - p->origin[0];
    float radialY = p->position[1] - p->origin[1];
    normalizeInPlace(&radialX, &radialY);
    float tangentialX = -radialY * p->tangentialAcceleration;
    float tangentialY =  radialX * p->tangentialAcceleration;

    radialX *= p->radialAcceleration;
    radialY *= p->radialAcceleration;

    p->velocity[0] += (radialX + tangentialX + p->linearAcceleration[0]) * dt;
    p->velocity[1] += (radialY + tangentialY + p->linearAcceleration[1]) * dt;

    p->velocity[0] *= 1.0f / (1.0f + p->linearDamping * dt);
    p->velocity[1] *= 1.0f / (1.0f + p->linearDamping * dt);
    
    p->position[0] += p->velocity[0] * dt;
    p->position[1] += p->velocity[1] * dt;

    float const t = 1.0f - p->life / p->lifetime;
    p->rotation += lerp(p->spinStart, p->spinEnd, t) * dt;
    p->angle = p->rotation;

    if(ps->relativeRotation) {
      p->angle += atan2(p->velocity[1], p->velocity[0]);
    }

    float s = (p->sizeOffset + t * p->sizeIntervalSize) * (float)(ps->sizeCount - 1);
    size_t i = (size_t)s;
    size_t k = (i == ps->sizeCount - 1) ? i : i + 1;
    s -= (float)i;
    p->size = lerp(ps->sizes[i], ps->sizes[k], s);

    s = t * (float)(ps->colorCount - 1);
    i = (size_t)s;
    k = (i == ps->colorCount - 1) ? i : i + 1;
    s -= (float)i;

    p->color.red   = lerp(ps->colors[i].red,   ps->colors[k].red,   s);
    p->color.green = lerp(ps->colors[i].green, ps->colors[k].green, s);
    p->color.blue  = lerp(ps->colors[i].blue,  ps->colors[k].blue,  s);
    p->color.alpha = lerp(ps->colors[i].alpha, ps->colors[k].alpha, s);

    k = ps->quadCount;
    if(k > 0) {
      s = t * (float)k;
      i = (s > 0.0f) ? (size_t) s : 0;
      p->quadIndex = (i < k) ? i : k - 1;
    }

    p = p->next;
  }

  if(ps->active) {
    float rate = 1.0f / ps->emissionRate;
    ps->emitCounter += dt;
    float total = ps->emitCounter - rate;
    while(ps->emitCounter > rate) {
      addParticle(ps, 1.0f - (ps->emitCounter - rate) / total);
      ps->emitCounter -= rate;
    }

    ps->life -= dt;
    if(ps->lifetime != -1 && ps->life < 0) {
      graphics_ParticleSystem_stop(ps);
    }
  }

  memcpy(ps->prevPosition, ps->position, sizeof(ps->position));
}