void estimate_rank_and_buildQ2(mat *M, int kblock, double TOL, mat **Y, mat **Q, int *good_rank){
    int m,n,i,j,ind,exit_loop = 0;
    double error_norm;
    mat *RN,*Y_new,*Y_big,*QtM,*QQtM;
    vec *vi,*vj,*p,*p1;
    m = M->nrows;
    n = M->ncols;

    // build random matrix
    printf("form RN..\n");
    RN = matrix_new(n,kblock);
    initialize_random_matrix(RN);

    // multiply to get matrix of random samples Y
    printf("form Y: %d x %d..\n",m,kblock);
    *Y = matrix_new(m, kblock);
    matrix_matrix_mult(M, RN, *Y);

    ind = 0;
    while(!exit_loop){
        printf("form Q..\n");
        if(ind > 0){
            matrix_delete(*Q);
        }
        *Q = matrix_new((*Y)->nrows, (*Y)->ncols);
        QR_factorization_getQ(*Y, *Q);

        // compute QtM
        QtM = matrix_new((*Q)->ncols, M->ncols);
        matrix_transpose_matrix_mult(*Q,M,QtM);

        // compute QQtM
        QQtM = matrix_new(M->nrows, M->ncols); 
        matrix_matrix_mult(*Q,QtM,QQtM);

        error_norm = 0.01*get_percent_error_between_two_mats(QQtM, M);

        printf("Y is of size %d x %d and error_norm = %f\n", (*Y)->nrows, (*Y)->ncols, error_norm);
        *good_rank = (*Y)->ncols;
       
        // add more samples if needed
        if(error_norm > TOL){
            Y_new = matrix_new(m, kblock);
            initialize_random_matrix(RN);
            matrix_matrix_mult(M, RN, Y_new);

            Y_big = matrix_new((*Y)->nrows, (*Y)->ncols + Y_new->ncols); 
            append_matrices_horizontally(*Y, Y_new, Y_big);
            matrix_delete(*Y);
            *Y = matrix_new(Y_big->nrows,Y_big->ncols);
            matrix_copy(*Y,Y_big);
            
            matrix_delete(Y_big);
            matrix_delete(Y_new);
            matrix_delete(QtM);
            matrix_delete(QQtM);
            ind++;
        }
        else{
            matrix_delete(RN);
            exit_loop = 1;
        }    
    }
}
Example #2
0
static Matrix *new_matrix(void) {
    Config *config = config_new(TEST_RAW_LEN, TEST_COL_LEN);
    return matrix_new(config);
}
void estimate_rank_and_buildQ(mat *M, double frac_of_max_rank, double TOL, mat **Q, int *good_rank){
    int m,n,i,j,ind,maxdim;
    double vec_norm;
    mat *RN,*Y,*Qbig,*Qsmall;
    vec *vi,*vj,*p,*p1;
    m = M->nrows;
    n = M->ncols;
    maxdim = round(min(m,n)*frac_of_max_rank);

    vi = vector_new(m);
    vj = vector_new(m);
    p = vector_new(m);
    p1 = vector_new(m);

    // build random matrix
    printf("form RN..\n");
    RN = matrix_new(n, maxdim);
    initialize_random_matrix(RN);

    // multiply to get matrix of random samples Y
    printf("form Y: %d x %d..\n",m,maxdim);
    Y = matrix_new(m, maxdim);
    matrix_matrix_mult(M, RN, Y);

    // estimate rank k and build Q from Y
    printf("form Qbig..\n");
    Qbig = matrix_new(m, maxdim);

    matrix_copy(Qbig, Y);

    printf("estimate rank with TOL = %f..\n", TOL);
    *good_rank = maxdim;
    int forbreak = 0;
    for(j=0; !forbreak && j<maxdim; j++){
        matrix_get_col(Qbig, j, vj);
        for(i=0; i<j; i++){
            matrix_get_col(Qbig, i, vi);
            project_vector(vj, vi, p);
            vector_sub(vj, p);
            if(vector_get2norm(p) < TOL && vector_get2norm(p1) < TOL){
                *good_rank = j;
                forbreak = 1;
                break;
            }
            vector_copy(p1,p);
        }
        vec_norm = vector_get2norm(vj);
        vector_scale(vj, 1.0/vec_norm);
        matrix_set_col(Qbig, j, vj);
    }

    printf("estimated rank = %d\n", *good_rank);
    Qsmall = matrix_new(m, *good_rank);
    *Q = matrix_new(m, *good_rank);
    matrix_copy_first_columns(Qsmall, Qbig);
    QR_factorization_getQ(Qsmall, *Q);

    matrix_delete(RN);
    matrix_delete(Y);
    matrix_delete(Qsmall);
    matrix_delete(Qbig);
}
}

void matrix_transpose(mat m)
{
	for (int i = 0; i < m->m; i++) {
		for (int j = 0; j < i; j++) {
			double t = m->v[i][j];
			m->v[i][j] = m->v[j][i];
			m->v[j][i] = t;
		}
	}
}

mat matrix_copy(int n;double a[][n], int m, int n)
{
	mat x = matrix_new(m, n);
	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++)
			x->v[i][j] = a[i][j];
	return x;
}

mat matrix_mul(mat x, mat y)
{
	if (x->n != y->m) return 0;
	mat r = matrix_new(x->m, y->n);
	for (int i = 0; i < x->m; i++)
		for (int j = 0; j < y->n; j++)
			for (int k = 0; k < x->n; k++)
				r->v[i][j] += x->v[i][k] * y->v[k][j];
	return r;
int test_sparse_vector() {
  starting_tests();
  learner_error error;
  SparseVector *v1, *v2;
  
  // create a generic matrix
  Matrix *m;
  error = matrix_new(&m);
  
  // creating
  error = sparse_vector_new(&v1, m);
  test_error(error);
  error = sparse_vector_new(&v2, m);
  test_error(error);
  
  // setting
  test_set_value(v1, 1, 2.0);
  test_set_value(v1, 0, 1.0);
  test_set_value(v2, 0, 3.0);
  test_set_value(v2, 3, 4.0);
  test_set_value(v2, 2, 5.0);
  
  // getting
  float value;
  test_get_value(v1, 0, 1.0);
  test_get_value(v1, 1, 2.0);
  test_get_value(v2, 0, 3.0);
  test_get_value(v2, 2, 5.0);
  test_get_value(v2, 3, 4.0);
  
  // freezing
  int frozen;
  error = sparse_vector_freeze(v1);
  test_error(error);
  error = sparse_vector_freeze(v2);
  test_error(error);
  error = sparse_vector_frozen(v1, &frozen);
  test_error(error);
  test(frozen);
  error = sparse_vector_frozen(v2, &frozen);
  test_error(error);
  test(frozen);
  
  // magnitude
  error = sparse_vector_magnitude(v1, &value);
  test_error(error);
  test_float(value, sqrtf(5.0));
  error = sparse_vector_magnitude(v2, &value);
  test_error(error);
  test_float(value, sqrtf(50.0));
  
  // euclidean distance
  error = sparse_vector_euclidean_distance(v1, v2, &value);
  test_error(error);
  test_float(value, sqrtf(4.0 + 4.0 + 25.0 + 16.0));
  
  // dot product
  error = sparse_vector_dot_product(v1, v2, &value);
  test_error(error);
  test_float(value, 3.0);
  
  // cosine similarity
  error = sparse_vector_cosine_similarity(v1, v2, &value);
  test_error(error);
  test_float(value, ((3.0) / (sqrtf(5.0) * sqrtf(50.0))));
  
  // setting existing values
  test_set_value(v1, 1, 12.0);
  test_set_value(v1, 0, 11.0);
  test_set_value(v2, 0, 13.0);
  test_set_value(v2, 3, 14.0);
  test_set_value(v2, 2, 15.0);
  
  // getting changed values
  test_get_value(v1, 0, 11.0);
  test_get_value(v1, 1, 12.0);
  test_get_value(v2, 0, 13.0);
  test_get_value(v2, 2, 15.0);
  test_get_value(v2, 3, 14.0);
  
  // cleanup
  error = sparse_vector_free(v1);
  test_error(error);
  error = sparse_vector_free(v2);
  test_error(error);
  finished_tests();
}
Example #6
0
struct ekf *ekf_new(int n, int m, int nw, int nv)
{
    struct ekf *ekf = malloc(sizeof(struct ekf));
    if (ekf) {
        /* X(nx1) */
        ekf->X = matrix_new(n, 1);
        /* P(nxn) */
        ekf->P = matrix_new(n, n);
        /* A(nxn) */
        ekf->A = matrix_new(n, n);
        /* H(mxn) */
        ekf->H = matrix_new(n, m);
        /* V(nxnw) */
        ekf->V = matrix_new(m, nv);
        /* R(mxm) */
        ekf->R = matrix_new(m, m);
        /* W(nxnw) */
        ekf->W = matrix_new(n, nw);
        /* Q(nxn) */
        ekf->Q = matrix_new(n, n);
        /* E(?x?) */
        /* K(nxm) */
        ekf->K = matrix_new(n, m);

        /* Tnxm(nxm) */
        ekf->Tnxm = matrix_new(n, m);
        /* Tnxn(nxn) */
        ekf->Tnxn = matrix_new(n, n);
        /* Tmxm(mxm) */
        ekf->Tmxm = matrix_new(m, m);

        ekf->n = n;
        ekf->m = m;
        ekf->nw = nw;
        ekf->nv = nv;
    }

    return ekf;
}
Example #7
0
/* main: application entry point.
 * @argc: the number of commandline arguments.
 * @argv: the commandline argument string array.
 */
int main (int argc, char **argv) {
  /* declare required variables. */
  pca_distance_metric metric;
  char *ifname, *metstr;
  struct matrix *D;
  struct pca *P;

  /* parse the command-line arguments. */
  if (!opts_init (argc, argv)) {
    /* output an error message and exit. */
    ferror ("failed to parse arguments");
    fprintf (stdout, HELP);
    return 1;
  }

  /* see if we should display the help message. */
  if (opts_geti (OPTS_S_HELP)) {
    /* print the help message and quit. */
    fprintf (stdout, HELP);
    return 0;
  }

  /* get the input filename. */
  ifname = opts_gets (OPTS_S_INPUT);

  /* ensure the input filename was provided. */
  if (!ifname) {
    /* output an error message and exit. */
    ferror ("input file required");
    fprintf (stdout, HELP);
    return 1;
  }

  /* read in the input file. */
  P = pca_new (ifname);

  /* make sure we read in the file. */
  if (!P) {
    /* output an error message and exit. */
    ferror ("failed to read '%s'", ifname);
    return 1;
  }

  /* set the default distance metric. */
  metric = pca_group_distance_euclidean;

  /* get the distance metric string. */
  metstr = opts_gets (OPTS_S_METRIC);

  /* see if the distance metric was provided. */
  if (metstr) {
    /* check the distance metric argument. */
    if (strncmp (metstr, "EUC", 3) == 0 ||
        strncmp (metstr, "euc", 3) == 0) {
      /* set the euclidean metric. */
      metric = pca_group_distance_euclidean;
    }
    else if (strncmp (metstr, "MAH", 3) == 0 ||
             strncmp (metstr, "mah", 3) == 0) {
      /* set the mahalanobis distance. */
      metric = pca_group_distance_mahalanobis;
    }
    else if (strncmp (metstr, "BHA", 3) == 0 ||
             strncmp (metstr, "bha", 3) == 0) {
      /* set the bhattacharyya distance. */
      metric = pca_group_distance_bhattacharyya;
    }
    else if (strncmp (metstr, "HEL", 3) == 0 ||
             strncmp (metstr, "hel", 3) == 0) {
      /* set the hellinger distance. */
      metric = pca_group_distance_hellinger;
    }
    else if (strncmp (metstr, "MIX", 3) == 0 ||
             strncmp (metstr, "mix", 3) == 0) {
      /* set the mixture distance. */
      metric = pca_group_distance_mixture;
    }
    else if (strncmp (metstr, "CHI", 3) == 0 ||
             strncmp (metstr, "chi", 3) == 0) {
      /* set the mixture distance. */
      metric = pca_group_distance_chisq;
    }
    else {
      /* output an error message and exit. */
      ferror ("unrecognized distance metric '%s'", metstr);
      fprintf (stdout, HELP);
      return 1;
    }
  }

  /* allocate a distance matrix. */
  D = matrix_new (P->n_groups, P->n_groups);

  /* make sure we allocated a matrix. */
  if (!D) {
    /* output an error message and exit. */
    ferror ("failed to allocate distance matrix");
    return 1;
  }

  /* calculate the distance matrix. */
  if (!pca_distances (P, D, metric)) {
    /* output an error message and exit. */
    ferror ("failed to calculate distance matrix");
    return 1;
  }

  /* write out the distance matrix. */
  pca_distances_print (P, D);

  /* free the distance matrix. */
  matrix_free (D);

  /* free the pca structure. */
  pca_free (P);

  /* return success. */
  return 0;
}
Example #8
0
/* main: application entry point.
 * @argc: the number of commandline arguments.
 * @argv: the commandline argument string array.
 */
int main (int argc, char **argv) {
  /* declare required variables. */
  struct vector *x, *y, *z, *u;
  struct matrix *V, *R, *C;
  double theta;
  char *smean, *svar, *label;
  unsigned long n;
  int i;

  /* initialize the random number generator. */
  rand_init (time (NULL));

  /* parse the command-line arguments. */
  if (!opts_init (argc, argv)) {
    /* output an error message and exit. */
    ferror ("failed to parse arguments");
    fprintf (stdout, HELP);
    return 1;
  }

  /* see if we should display the help message. */
  if (opts_geti (OPTS_S_HELP)) {
    /* print the help message and quit. */
    fprintf (stdout, HELP);
    return 0;
  }

  /* get the number of points to produce. */
  n = opts_geti (OPTS_S_COUNT);

  /* ensure the point count is valid. */
  if (!n) {
    /* output an error message and exit. */
    ferror ("invalid point count (%lu)", n);
    fprintf (stdout, HELP);
    return 1;
  }

  /* get the points label. */
  label = opts_gets (OPTS_S_LABEL);

  /* ensure the points label is valid. */
  if (!label) {
    /* output an error message and exit. */
    ferror ("invalid label '%s'", label);
    fprintf (stdout, HELP);
    return 1;
  }

  /* allocate the math structures. */
  x = vector_new (2);
  y = vector_new (2);
  z = vector_new (2);
  u = vector_new (2);
  V = matrix_new (2, 2);
  R = matrix_new (2, 2);
  C = matrix_new (2, 2);

  /* ensure we allocated the math structures. */
  if (!x || !y || !z || !u || !V || !R || !C) {
    /* output an error message and exit. */
    ferror ("failed to allocate math structures");
    return 1;
  }

  /* get the mean string. */
  smean = opts_gets (OPTS_S_MEAN);

  /* did we get a mean string? */
  if (smean) {
    /* try to parse the string. */
    if (sscanf (smean, "(%lf,%lf)", &u->d[0], &u->d[1]) != 2) {
      /* output an error message and exit. */
      ferror ("failed to parse mean string '%s'", smean);
      fprintf (stdout, HELP);
      return 1;
    }
  }
  else {
    /* initialize to the origin. */
    u->d[0] = u->d[1] = 0.0;
  }

  /* get the variance string. */
  svar = opts_gets (OPTS_S_VAR);

  /* did we get a variance string? */
  if (svar) {
    /* try to parse the string. */
    if (sscanf (svar, "(%lf,%lf)", &V->d[0][0], &V->d[1][1]) != 2) {
      /* output an error message and exit. */
      ferror ("failed to parse variance string '%s'", svar);
      fprintf (stdout, HELP);
      return 1;
    }
  }
  else {
    /* initialize to unit variances. */
    V->d[0][0] = V->d[1][1] = 1.0;
  }

  /* get the rotation value. */
  theta = opts_getf (OPTS_S_ROT) / 180.0 * M_PI;

  /* build the rotation matrix. */
  R->d[0][0] = cos (theta);
  R->d[0][1] = -sin (theta);
  R->d[1][0] = sin (theta);
  R->d[1][1] = cos (theta);

  /* calculate the covariance matrix. */
  if (!matrix_matrix_mul (1.0, R, V, C)) {
    /* output an error message and exit. */
    ferror ("failed to calculate covariance matrix");
    return 1;
  }

  /* see if we should produce a header. */
  if (opts_geti (OPTS_S_HEADER)) {
    /* yep. print one. */
    fprintf (stdout,
      "Obs ID (Primary)\tObs ID (Obs. Sec. ID:1)\tM1.t[2]\tM1.t[1]\n");
  }

  /* loop a set number of times. */
  for (i = 0; i < n; i++) {
    /* calculate the two values. */
    x->d[0] = randnormal ();
    x->d[1] = randnormal ();

    /* scale the values. */
    if (!matrix_vector_mul (1.0, C, x, y)) {
      /* output an error message and exit. */
      ferror ("failed to scale random variate %d", i + 1);
      return 1;
    }

    /* translate the values. */
    if (!vector_sum (1.0, y, 1.0, u, z)) {
      /* output an error message and exit. */
      ferror ("failed to translate random variate %d", i + 1);
      return 1;
    }

    /* print the values. */
    fprintf (stdout, "%d\t%s\t%lf\t%lf\n", i + 1, label, z->d[0], z->d[1]);
  }

  /* free the math structures. */
  vector_free (x);
  vector_free (y);
  vector_free (z);
  vector_free (u);
  matrix_free (V);
  matrix_free (R);
  matrix_free (C);

  /* return success. */
  return 0;
}
Example #9
0
int main()
{
    int i, j, m, n, k, p, q, s, vnum, offset;
    int64_t frank, numnnz;
    double val,normM,normU,normS,normV,normP,percent_error;
    mat *M, *U, *S, *V, *P;
    struct timeval start_time, end_time;

    m = 50000;
    n = 100000;
    numnnz = m*n; // dense

    M = matrix_new(m,n);
    m = M->nrows;
    n = M->ncols;
    printf("sizes of M are %d by %d\n", m, n);
    printf("m*n = %" PRId64 "\n", numnnz); // this is bigger than a 32 bit int can hold

    printf("initializing random matrix..\n");
    gettimeofday(&start_time,NULL);

    initialize_random_matrix(M);

    gettimeofday(&end_time,NULL);
    printf("done loading..\n");
    printf("elapsed time: about %4.2f seconds\n", get_seconds_frac(start_time,end_time));


    // now test low rank SVD of M..
    k = 1000; // rank we want
    p = 20; // oversampling
    q = 3; // power scheme
    s = 1; // re-rotho for power scheme    
    vnum = 1; // scheme to use

    printf("calling random SVD..\n");
    gettimeofday(&start_time,NULL);
	low_rank_svd_rand_decomp_fixed_rank(M, k, p, vnum, q, s, &frank, &U, &S, &V);
    gettimeofday(&end_time,NULL);
    printf("elapsed time: about %4.2f seconds\n", get_seconds_frac(start_time,end_time));

    // form product matrix
    P = matrix_new(m,n);
    form_svd_product_matrix(U,S,V,P);

    // get norms of each
    normM = get_matrix_frobenius_norm(M);
    normU = get_matrix_frobenius_norm(U);
    normS = get_matrix_frobenius_norm(S);
    normV = get_matrix_frobenius_norm(V);
    normP = get_matrix_frobenius_norm(P);
    printf("normM = %f ; normU = %f ; normS = %f ; normV = %f ; normP = %f\n", normM, normU, normS, normV, normP);

    // calculate percent error
    percent_error = get_percent_error_between_two_mats(M,P);
    printf("percent_error between M and U S V^T = %f\n", percent_error);

    // delete and exit
    printf("delete and exit..\n");
    matrix_delete(M);
    matrix_delete(U);
    matrix_delete(S);
    matrix_delete(V);
    matrix_delete(P);

    return 0;
}
Example #10
0
int
main (int argc, char **argv)
{
    SDL_Surface *image;
    nile_t *nl;
    char mem[500000];
    uint32_t texture_pixels[TEXTURE_WIDTH * TEXTURE_HEIGHT] = {0};
    real angle = 0;
    real scale;

    if (SDL_Init (SDL_INIT_VIDEO) == -1)
        DIE ("SDL_Init failed: %s", SDL_GetError ());
    if (!SDL_SetVideoMode (DEFAULT_WIDTH, DEFAULT_HEIGHT, 0,
                           SDL_HWSURFACE | SDL_ANYFORMAT | SDL_DOUBLEBUF))
        DIE ("SDL_SetVideoMode failed: %s", SDL_GetError ());
    image = SDL_GetVideoSurface ();

    nl = nile_new (NTHREADS, mem, sizeof (mem));

    ilInit ();
    ilBindImage (iluGenImage ());
    if (argc < 3)
        return -1;
    printf ("loading: %s\n", argv[1]);
    ilLoadImage (argv[1]);
    ilCopyPixels (0, 0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT, 1, IL_BGRA,
                  IL_UNSIGNED_BYTE, &texture_pixels);
    int filter = atoi (argv[2]);

    for (;;) {
        angle += 0.001;
        scale = fabs (angle - (int) angle - 0.5) * 10;
        SDL_Event event;
        if (SDL_PollEvent (&event) && event.type == SDL_QUIT)
            break;

        SDL_FillRect (image, NULL, 0);
        SDL_LockSurface (image);

            matrix_t M = matrix_new ();
            M = matrix_translate (M, 250, 250);
            M = matrix_rotate (M, angle);
            M = matrix_scale (M, scale, scale);
            M = matrix_translate (M, -250, -250);
            matrix_t I = matrix_inverse (M);

            nile_Kernel_t *texture =
                gezira_ReadFromImage_ARGB32 (nl, texture_pixels, TEXTURE_WIDTH,
                                             TEXTURE_HEIGHT, TEXTURE_WIDTH);
            /*
             */
            texture = nile_Pipeline (nl,
                    gezira_ImageExtendReflect (nl, TEXTURE_WIDTH, TEXTURE_HEIGHT),
                    texture, NULL);
            if (filter == 2)
                texture = gezira_BilinearFilter (nl, texture);
            if (filter == 3)
                texture = gezira_BicubicFilter (nl, texture);
            /*
             */
            texture = nile_Pipeline (nl, 
                gezira_TransformPoints (nl, I.a, I.b, I.c, I.d, I.e - 150, I.f - 125),
                texture, NULL);
            nile_Kernel_t *pipeline = nile_Pipeline (nl,
                gezira_TransformBeziers (nl, M.a, M.b, M.c, M.d, M.e, M.f),
                gezira_ClipBeziers (nl, 0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT),
                gezira_Rasterize (nl),
                gezira_ApplyTexture (nl, texture),
                gezira_WriteToImage_ARGB32 (nl, image->pixels,
                                            DEFAULT_WIDTH, DEFAULT_HEIGHT,
                                            image->pitch / 4),
                NULL);

            nile_feed (nl, pipeline, path, 6, path_n, 1);
            nile_sync (nl);

        SDL_UnlockSurface (image);
        SDL_Flip (image);
    }

    nile_free (nl);
    printf ("done\n");

    return 0;
}
Example #11
0
File: matrix.c Project: Adri96/aifh
mat matrix_solve_qr(mat xMatrix, mat yMatrix) {
	mat qr,x,result;
	double *rDiag,s;
	int i,n,m,k,j,nx;

	/* Initial validations */
	if (xMatrix->m != yMatrix->m) {
		printf("Matrix row dimensions must agree.\n");
		exit(1);
	}

	/* Perform a QR decomp. */
	n = xMatrix->n;
	m = xMatrix->m;
	rDiag = (double*)calloc(n,sizeof(double));
	qr = matrix_copy(xMatrix);

	// Main loop.
	for (k = 0; k < n; k++) {
		// Compute 2-norm of k-th column without under/overflow.
		double nrm = 0;
		for (i = k; i < m; i++) {
			nrm = calc_hypot(nrm, qr->v[i][k]);
		}

		if (nrm != 0.0) {
			// Form k-th Householder vector.
			if (qr->v[k][k] < 0) {
				nrm = -nrm;
			}
			for (i = k; i < m; i++) {
				qr->v[i][k] /= nrm;
			}
			qr->v[k][k] += 1.0;

			// Apply transformation to remaining columns.
			for (j = k + 1; j < n; j++) {
				s = 0.0;
				for (i = k; i < m; i++) {
					s += qr->v[i][k] * qr->v[i][j];
				}
				s = -s / qr->v[k][k];
				for (i = k; i < m; i++) {
					qr->v[i][j] += s * qr->v[i][k];
				}
			}
		}
		rDiag[k] = -nrm;
	}

	/* Validate */
	for(i=0;i<n;i++) {
		if( fabs(rDiag[i])==0 ) {
			printf("Matrix is rank deficient. Data fails to converge.");
			exit(1);
		}
	}

	/* Now solve */

	/* Copy right hand side */
	nx = yMatrix->n;
	x = matrix_copy(yMatrix);

	/* Compute Y = transpose(Q)*B */
	for (k = 0; k < n; k++) {
		for (j = 0; j < nx; j++) {
			s = 0.0;
			for (i = k; i < m; i++) {
				s += qr->v[i][k] * x->v[i][j];
			}
			s = -s / qr->v[k][k];
			for (i = k; i < m; i++) {
				x->v[i][j] += s * qr->v[i][k];
			}
		}
	}
	/* Solve R*X = Y; */
	for (k = n - 1; k >= 0; k--) {
		for (j = 0; j < nx; j++) {
			x->v[k][j] /= rDiag[k];
		}
		for (i = 0; i < k; i++) {
			for (j = 0; j < nx; j++) {
				x->v[i][j] -= x->v[k][j] * qr->v[i][k];
			}
		}
	}

	/* Build result matrix */
	result = matrix_new(n,nx);
	for(i=0;i<n;i++) {
		for(j=0;j<nx;j++) {
			result->v[i][j] = x->v[i][j];
		}
	}

	/* Cleanup */
	matrix_delete(qr);
	matrix_delete(x);

	/* Return the result */
	return result;
}
Example #12
0
/* read problem data from file
 * returns amount of lines
 * returns -1 when there was a reading error
 * returns -2 when the problem is infeasible
 */
int process_file(const char* filename, Matrix** matrix)
{
   assert(NULL != filename);
   assert(0 < strlen(filename));

   FILE* fp;
   char buf[MAX_LINE_LEN];
   char* s;
   char* tok;
   char delimiter[2] = " ";
   char* test;
   char relation[3];
   int lines = 0;
   int m = -1;
   int n = -1;
   TYPE* coefs = NULL;
   TYPE rhs;
   int i = 0;
   int j = 0;

   if (NULL == (fp = fopen(filename, "r")))
   {
      fprintf(stderr, "Can't open file %s\n", filename);
      return -1;
   }

   while(NULL != (s = fgets(buf, sizeof(buf), fp)))
   {
      char* t = strpbrk(s, "#\n\r");

      lines++;

      if (NULL != t) /* else line is not terminated or too long */
         *t = '\0'; /* clip comment or newline */

      /* Skip over leading space
       */
      while(isspace(*s))
         s++;

      /* Skip over empty lines
       */
      if (!*s) /* <=> (*s == '\0') */
         continue;

      /* read lines */
      i++;
      j = 0;
#ifdef debug
      fprintf(stdout, "line %i: %s\n", lines, s);
#endif
      tok = strtok(s, delimiter);
      switch(i)
      {
         /* first line contains amount of columns (variables) */
         case 1:
         {
            n = strtol(tok, &test, 10);
            if ((int)strlen(test) != 0)
            {
               fprintf(stderr, "Wrong input data for number of variables.\n");
               return -1;
            }
            if (n <= 0 || n > MAX_MATRIX_SIZE)
            {
               fprintf(stderr, "Dimension of columns is not right. (%i not in {1, ..., %i})\n", n, MAX_MATRIX_SIZE);
               return -1;
            }
            coefs = allocate(n, sizeof(*coefs));
            assert(coefs != NULL);
         break;
         }

         /* second line contains amount of rows (constraints) */
         case 2:
         {
            m = strtol(tok, &test, 10);
            if ((int)strlen(test) != 0)
            {
               fprintf(stderr, "Wrong input data for number of constraints.\n");
               return -1;
            }
            if (m <= 0 || m > MAX_MATRIX_SIZE)
            {
               fprintf(stderr, "Dimension of rows is not right. (%i not in {1, ..., %i})\n", m, MAX_MATRIX_SIZE);
               deallocate(coefs);
               return -1;
            }
            *matrix = matrix_new(m, n);
         break;
         }

         /* other lines contain rows (constraints) of matrix and right-hand sides */
         default:
         {
            if (m == -1 || n == -1 ||  coefs == NULL)
            {
               fprintf(stderr, "Dimension not set or coefficient array not allocated.\n");
               deallocate(coefs);
               return -1;
            }

            while(tok != NULL
               && strcmp(tok, "<=") != 0 && strcmp(tok, "=<") != 0
               && strcmp(tok, ">=") != 0 && strcmp(tok, "=>") != 0
               && strcmp(tok, "==") != 0 && strcmp(tok, "=") != 0)
            {
               if (j >= n)
               {
                  fprintf(stderr, "Too many coefficients or no relation sign.\n");
                  deallocate(coefs);
                  return -1;
               }
               coefs[j] = strtov(tok, &test);
               if ((int)strlen(test) != 0)
               {
                  fprintf(stderr, "Wrong input data in line %i. Wrong type of coefficient (%s) of variable %i.\n", lines, tok, j+1);
                  deallocate(coefs);
                  return -1;
               }
               tok = strtok(NULL, delimiter);
               j++;
            }

            /* when j < n, then we have not enough coefficient entries in this line */
            if (j < n )
            {
               fprintf(stderr, "Not enough entries in line %i.\n", lines);
               deallocate(coefs);
               return -1;
            }

            /* when tok is NULL, then there is no relation operator given in this line */
            if (tok == NULL)
            {
               fprintf(stderr, "No relation operator in line %i.\n", lines);
               deallocate(coefs);
               return -1;
            }

            /* copy relation operator */
            assert(strlen(tok) <= 2);
            strncpy(relation, tok, sizeof(relation));

            /* get next token, which is right-hand side */
            tok = strtok(NULL, delimiter);

            /* when tok is NULL, then there is no right-hand side given in this line */
            if (tok == NULL)
            {
               fprintf(stderr, "Wrong input data in line %i. (no right-hand side)\n", lines);
               deallocate(coefs);
               return -1;
            }

            /* convert right-hand side and check whether the format is correct */
            rhs = strtov(tok, &test);
            if ((int)strlen(test) != 0)
            {
               fprintf(stderr, "Wrong input data in line %i. (wrong type of right-hand side)\n", lines);
               deallocate(coefs);
               return -1;
            }

            /* check for additional information after right-hand side */
            tok = strtok(NULL, delimiter);
            if (tok != NULL)
            {
               fprintf(stderr, "Too many information after relation sign of line %i.\n", lines);
               deallocate(coefs);
               return -1;
            }

            /* add constraint(s) to the problem */
            if (strcmp(relation, "<=") == 0 || strcmp(relation, "=<") == 0 || strcmp(relation, "=") == 0 || strcmp(relation, "==") == 0)
            {
               if (!matrix_put(*matrix, coefs, rhs))
               {
                  fprintf(stderr, "Problem is infeasible.\n");
                  deallocate(coefs);
                  return -2;
               }
            }
            if (strcmp(relation, ">=") == 0 || strcmp(relation, "=>") == 0 || strcmp(relation, "=") == 0 || strcmp(relation, "==") == 0)
            {
               for (int k = 0; k < n; k++)
                  coefs[k] *= -1;
               rhs *= -1;
               if (!matrix_put(*matrix, coefs, rhs))
               {
                  fprintf(stderr, "Problem is infeasible.\n");
                  deallocate(coefs);
                  return -2;
               }
            }
            if (strcmp(relation, "=") == 0 || strcmp(relation, "==") == 0)
               m++;

            /* check whether we have too many rows */
            if (matrix_getM(*matrix) + matrix_getRedundant(*matrix) > m)
            {
               fprintf(stderr, "Too many rows.\n");
               deallocate(coefs);
               return -1;

            }
         }
      }
   }
   deallocate(coefs);

   if (matrix_getM(*matrix) + matrix_getRedundant(*matrix) < m)
   {
      fprintf(stderr, "Not enough rows.\n");
      return -1;
   }

   fclose(fp);

   return lines;
}
Example #13
0
matrix_p
koeffizienten_xi_1dim(int grad) {
	matrix_p  back, matrix;
	int  q, m, n;
	fepc_real_t  **x;
	fepc_real_t  *a, *b;
	fepc_real_t  rat, reell, faktor, temp;

	ASSERT(grad >= 0);
	a = (fepc_real_t*) malloc(sizeof(fepc_real_t) * (2*grad + 1) );
	ASSERT(a != NULL);
	b = (fepc_real_t*) malloc(sizeof(fepc_real_t) * (2*grad + 1) );
	ASSERT(b != NULL);

	matrix = matrix_new( 2*grad+1, 2*grad+1 );
	x = matrix->a;

	for (n=0;n<=2*grad;n++) {
		a[n] = sqrt(2*n+3)*sqrt(2*n+1)/(fepc_real_t)(n+1);
	}
	b[0] = 0.0;
	for (n=1;n<=2*grad;n++) {
		b[n] = (sqrt(2*n+3)/sqrt(2*n-1)) * ( (fepc_real_t)n/(fepc_real_t)(n+1) );
	}

	x[0][0] = 1./sqrt(2);
	for (q=1;q<=2*grad;q++) {
		for (n=0;n<=q;n++) {
			m = q-n;
			if (n<m) {
				x[n][m] = 0.0;
			}
			else {
				temp = a[n-1]/2.0*(x[n-1][m+1]/a[m]+x[n-1][m]);
				if (m>0) {
					temp = temp + a[n-1]/2.0*b[m]/a[m]*x[n-1][m-1];
				}
				if (n>=2) {
					temp = temp - b[n-1]*x[n-2][m];
				}

				/*Korrektur der Werte (siehe Dokumentation)*/
				faktor = pow(2,((fepc_real_t)n+0.5))/sqrt( (2*n+1)*(2*m+1) );
				reell = temp;
				rat = reell*faktor;
				rat = get_rational(rat,1e-14,10000);
				reell = rat/faktor;

				x[n][m] = reell;
			}
		}
	}

	back = matrix_new(grad+1,grad+1);
	for (m=0;m<=grad;m++) {
		for (n=0;n<=grad;n++) {
			back->a[m][n] = matrix->a[m][n];
		}
	}

	free(a);
	free(b);
	matrix_del(matrix);

	return back;
}
Example #14
0
/*
 * Kalman filter process
 */
static PyObject *
kf_process(PyObject *self, PyObject *args, PyObject *kws)
{
    MatrixObject *A, *B, *C, *D, *x0, *P0, *Q, *R, *x, *P;
    Float *x_est, *y_est, *P_est;
    PyObject *out, *tmp, *tmp1, *x_est_out, *y_est_out, *P_est_out, *result;
    PyObject *mupdate_callback = NULL, *arglist;
    int i, j, k, n, p, q, datalength;
    MatrixObject *y, *u;

    static char *kwlist[] = {"A", "B", "C", "D", "y", "u", "x0", "P0", "Q", "R", "mupdate_callback", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kws, "O!O!O!O!O!O!O!O!O!O!|O:set_callback", kwlist,
            &MatrixType, &A,
            &MatrixType, &B,
            &MatrixType, &C,
            &MatrixType, &D,
            &MatrixType, &y,
            &MatrixType, &u,
            &MatrixType, &x0,
            &MatrixType, &P0,
            &MatrixType, &Q,
            &MatrixType, &R,
            &mupdate_callback))
        return NULL;

    if (A->rows != A->cols) {
        PyErr_SetString(PyExc_ValueError, "A must be a square matrix");
        return NULL;
    }

    n = A->rows;
    p = B->cols;
    q = C->rows;

    if (B->rows != n) {
        PyErr_SetString(PyExc_ValueError, "B must be Nxp matrix");
        return NULL;
    }
    if (C->cols != n) {
        PyErr_SetString(PyExc_ValueError, "C must be qxN matrix");
        return NULL;
    }
    if (D->rows != p || D->cols != q) {
        PyErr_SetString(PyExc_ValueError, "D must be pxq matrix");
        return NULL;
    }
    datalength = y->cols;
    if (y->cols != u->cols) {
        PyErr_SetString(PyExc_ValueError, "y and u data lengths does not match");
        return NULL;
    }
    if (y->rows != q) {
        PyErr_SetString(PyExc_ValueError, "y must be qxlength matrix");
        return NULL;
    }
    if (u->rows != p) {
        PyErr_SetString(PyExc_ValueError, "u must be pxlength matrix");
        return NULL;
    }
    if (x0->rows != n || x0->cols != 1) {
        PyErr_SetString(PyExc_ValueError, "x0 must be Nx1 matrix");
        return NULL;
    }
    if (P0->rows != n || P0->cols != n) {
        PyErr_SetString(PyExc_ValueError, "P0 must be NxN matrix");
        return NULL;
    }
    if (Q->rows != n || Q->cols != n) {
        PyErr_SetString(PyExc_ValueError, "Q must be NxN matrix");
        return NULL;
    }
    if (R->rows != q || R->cols != q) {
        PyErr_SetString(PyExc_ValueError, "R must be qxq matrix");
        return NULL;
    }
    if (!PyCallable_Check(mupdate_callback)) {
        if (mupdate_callback != NULL) {
            PyErr_SetString(PyExc_TypeError, "parameter must be callable");
            return NULL;
        }
    }

    x_est = m_new(n, datalength);
    y_est = m_new(n, datalength);
    P_est = m_new(n, n*datalength);

    if (mupdate_callback != NULL) {
        Py_XINCREF(mupdate_callback);  // Add a reference to new callback
        x = matrix_new(n, 1);
        P = matrix_new(n, n);
        // initialize x and P
        m_copy(x->data, x0->data, n, 1);
        m_copy(P->data, P0->data, n, n);

        for (i=0; i<datalength; i++) {
            tick(A->data, B->data, C->data, D->data,
                n, p, q,
                y->data+i*q, u->data+i*p,
                x->data, P->data,
                Q->data, R->data,
                x_est+i*n, y_est+i*q, P_est+i*n*n);
            // copy x and P values into output array
            m_copy(x->data, x_est+i*n, n, 1);
            m_copy(P->data, P_est+i*n*n, n, n);
            // update the matrixes
            arglist = Py_BuildValue("(i, O, O, O, O, O)", i, A, B, C, D, x);
            result = PyEval_CallObject(mupdate_callback, arglist);
            Py_DECREF(arglist);
        }
        Py_DECREF(x);
        Py_DECREF(P);
    } else { // model update not needed
        process(A->data, B->data, C->data, D->data,
                n, p, q, y->data, u->data, x0->data, P0->data, Q->data, R->data, datalength,
                x_est, y_est, P_est);
    }

    // create lists from matrixes
    x_est_out = PyList_New(datalength);
    for (i=0; i<datalength; i++) {
        tmp = PyList_New(n);
        for (j=0; j<n; j++) {
            PyList_SetItem(tmp, j, PyFloat_FromDouble( *(x_est+i*n+j) ));
        }
        PyList_SetItem(x_est_out, i, tmp);
    }
    y_est_out = PyList_New(datalength);
    for (i=0; i<datalength; i++) {
        tmp = PyFloat_FromDouble( *(y_est+i) );
        PyList_SetItem(y_est_out, i, tmp);
    }
    P_est_out = PyList_New(datalength);
    for (i=0; i<datalength; i++) {
        tmp = PyList_New(n);
        for (j=0; j<n; j++) {
            tmp1 = PyList_New(n);
            for (k=0; k<n; k++) {
                PyList_SetItem(tmp1, k, PyFloat_FromDouble( *(x_est+i*n+j*n+k) ));
            }
            PyList_SetItem(tmp, j, tmp1);
        }
        tmp = PyFloat_FromDouble( *(P_est+i) );
        PyList_SetItem(P_est_out, i, tmp);
    }

    m_free(x_est);
    m_free(y_est);
    m_free(P_est);

    out = PyTuple_New(3);
    PyTuple_SetItem(out, 0, x_est_out);
    PyTuple_SetItem(out, 1, y_est_out);
    PyTuple_SetItem(out, 2, P_est_out);
    Py_INCREF(out); // TODO add Py_INCREF every time when returning object from fnc

    return out;
}
Example #15
0
/* initialize variables */
int init_hist_np()
{
	chamber_p  chamb;
	int        ipl;
	double     d;
	int        i, n;

	/* initialyze for Target Chamber MWDC */
	chamb = &tgc_mwdc;
	chamb->name = "TGC";
	chamb->type = CHAMB_MWDC;
	chamb->npl  = 10;
	chamb->plane = (plane_p)malloc(sizeof(plane_t)*chamb->npl);
	chamb->plane[0].name = "VT1";
	chamb->plane[1].name = "VT2";
	chamb->plane[2].name = "VT3";
	chamb->plane[3].name = "VT4";
	chamb->plane[4].name = "X1";
	chamb->plane[5].name = "X2";
	chamb->plane[6].name = "U1";
	chamb->plane[7].name = "U2";
	chamb->plane[8].name = "V1";
	chamb->plane[9].name = "V2";
	for(ipl=0; ipl<chamb->npl; ipl++){
		chamb->plane[ipl].chamb = chamb;
	}
	chamb_init_hist(chamb);
	n = 1<<chamb->npl;
	chamb->matrix = (void*)malloc(sizeof(mat_p)*n);
	for(i=0; i<n; i++) ((mat_p*)chamb->matrix)[i] = (mat_p)NULL;
	chamb->mb = matrix_new(4,1);
	chamb->mc = matrix_new(4,1);
	if(chamb->matrix==NULL||chamb->mb==NULL||chamb->mc==NULL){
		showerr("init_hist_np: No enough memory available\n");
		exit(1);
	}

	/* initialyze for Front-end Chamber MWDC */
	chamb = &fec_mwdc;
	chamb->name = "FEC";
	chamb->type = CHAMB_MWDC;
	chamb->npl  = 6;
	chamb->plane = (plane_p)malloc(sizeof(plane_t)*chamb->npl);
	chamb->plane[0].name = "Y1";
	chamb->plane[1].name = "Y2";
	chamb->plane[2].name = "V1";
	chamb->plane[3].name = "V2";
	chamb->plane[4].name = "U1";
	chamb->plane[5].name = "U2";
	for(ipl=0; ipl<chamb->npl; ipl++){
		chamb->plane[ipl].chamb = chamb;
	}
	chamb_init_hist(chamb);
	n = 1<<chamb->npl;
	chamb->matrix = (void*)malloc(sizeof(mat_p)*n);
	for(i=0; i<n; i++) ((mat_p*)chamb->matrix)[i] = (mat_p)NULL;
	chamb->mb = matrix_new(4,1);
	chamb->mc = matrix_new(4,1);
	if(chamb->matrix==NULL||chamb->mb==NULL||chamb->mc==NULL){
		showerr("init_hist_np: No enough memory available\n");
		exit(1);
	}
}
Example #16
0
File: cox.c Project: CharoL/math
void cox_test(double** covariates, size_t num_features_in_covariate, size_t num_samples, double* time, double* censor, double* coefficients, double** variance) {
    // declare variables, init values and allocate memory
    // gsl matrices
    matrix_t* coefficients_matrix_p =  NULL;
    matrix_t* information_matrix_p =  NULL;
    matrix_t* information_matrix_inverse_p =  NULL;
    matrix_t* score_matrix_p =  NULL;
    matrix_t* error_matrix_p =  NULL;
    matrix_t* variance_matrix_p =  NULL;

    // other variables
    double denominator = 0, numerator = 0;
    double error1 = 1, error2 = 1;
    double* risk_factor = (double*) calloc(num_samples, sizeof(double));
    double* score = (double*) calloc(num_features_in_covariate, sizeof(double));
    double** expected_covariate = (double**) calloc(num_features_in_covariate, sizeof(double*));
    double** information = (double**) calloc(num_features_in_covariate, sizeof(double*));
    
    for (size_t i = 0; i < num_features_in_covariate; i++) {
        coefficients[i] = 0.1;
        expected_covariate[i] = (double*) calloc(num_samples, sizeof(double));
        information[i] = (double*) calloc(num_features_in_covariate, sizeof(double));
    }

    // create gsl matrices
    coefficients_matrix_p =  matrix_new(num_features_in_covariate, 1);
    matrix_init(0.1, coefficients_matrix_p);
    information_matrix_p = matrix_new(num_features_in_covariate, num_features_in_covariate);
    score_matrix_p = matrix_new(num_features_in_covariate, 1);
    error_matrix_p = matrix_new(num_features_in_covariate, 1);
    information_matrix_inverse_p = matrix_new(num_features_in_covariate, num_features_in_covariate);

    while((error1 > COX_ERROR_LIMIT) || (error2 > COX_ERROR_LIMIT)) {
        for (size_t i = 0; i < num_samples; ++i) {
            risk_factor[i] = 1.0;
            for (size_t s = 0; s < num_features_in_covariate; ++s) {
                risk_factor[i] *= exp(coefficients[s] * covariates[s][i]);
            }
        }

        for (size_t j = 0; j < num_features_in_covariate; j++) {
            score[j] = 0.0;
            for (size_t i = 0; i < num_samples; i++) {
                for (size_t k = 0; k < num_samples; k++) {
                    if (time[k] >= time[i]) {
                        denominator += risk_factor[k];
                        numerator += covariates[j][k] * risk_factor[k];
                    }
                }
                 
                expected_covariate[j][i] = numerator / denominator;
                score[j] += censor[i] * (covariates[j][i] - expected_covariate[j][i]);

                numerator = 0.0;
                denominator = 0.0;
            }
        }

        for (size_t r = 0; r < num_features_in_covariate; r++) {
            for (size_t s = 0; s < num_features_in_covariate; s++) {
                information[r][s] = 0.0;
                for (size_t i = 0; i < num_samples; i++) {
                    for (size_t k = 0; k < num_samples; k++) {
                        if (time[k] >= time[i]) {
                            denominator += risk_factor[k];
                            numerator += (covariates[r][k] * covariates[s][k] * risk_factor[k]);
                        }
                    }
                    information[r][s] +=  censor[i] * (expected_covariate[r][i] * expected_covariate[s][i] - (numerator / denominator));

                    numerator = 0.0;
                    denominator = 0.0;
                }
            }
        }

        // fill information_matrix
        matrix_fill(information, num_features_in_covariate, num_features_in_covariate, information_matrix_p);  // fill the matrix with data

        // fill score_matrix from score array
        for (size_t i = 0; i < num_features_in_covariate; i++) {
            matrix_set(i, 0, score[i], score_matrix_p);
        }

        // calculate error matrix: inv(information_matrix) * score_matrix
        matrix_inv(information_matrix_p, information_matrix_inverse_p);
        matrix_mul(information_matrix_inverse_p, score_matrix_p, error_matrix_p);

        // calculate coefficients matrix
        coefficients_matrix_p = matrix_sub(coefficients_matrix_p, error_matrix_p);

        // fill coefficientes
        for (size_t i = 0; i < num_features_in_covariate; i++) {
            coefficients[i] = matrix_get(i, 0, coefficients_matrix_p);
        }

        error1 = sqrt(matrix_Fnorm(error_matrix_p));
        error2 = sqrt(matrix_Fnorm(score_matrix_p));
    }  // end of while
    
    // calculate variance: (-1 * inv(information_matrix))
    variance_matrix_p = matrix_scale(information_matrix_inverse_p, -1.0);
    for (size_t i = 0; i < num_features_in_covariate; i++) {
        for (size_t j = 0; j < num_features_in_covariate; j++) {
            variance[i][j] = matrix_get(i, j, variance_matrix_p);
        }
    }  

    // free gsl matrices
    matrix_free(coefficients_matrix_p);
    matrix_free(information_matrix_p);
    matrix_free(information_matrix_inverse_p);
    matrix_free(score_matrix_p);
    matrix_free(error_matrix_p);
    variance_matrix_p = NULL;  // points to information_matrix_inverse_p previously freed
    matrix_free(variance_matrix_p); 

    // free other resources
    free(risk_factor);
    free(score);
    for (size_t i = 0; i < num_features_in_covariate; i++) {
        free(expected_covariate[i]);
        free(information[i]);
    }    
    free(expected_covariate);
    free(information); 
    
    return;
}