Пример #1
0
int main(int argc, char **argv) {
	double **matrix, **inverse, **result;
	double **LU, *result2, *b;
	int *p;

	matrix = (double**)malloc(sizeof(double*) * 4);
	matrix[0] = (double*)malloc(sizeof(double) * 4);
	matrix[1] = (double*)malloc(sizeof(double) * 4);
	matrix[2] = (double*)malloc(sizeof(double) * 4);
	matrix[3] = (double*)malloc(sizeof(double) * 4);
	matrix[0][0] = 2;
	matrix[0][1] = 0;
	matrix[0][2] = 2;
	matrix[0][3] = 0.6;
	matrix[1][0] = 3;
	matrix[1][1] = 3;
	matrix[1][2] = 4;
	matrix[1][3] = -2;
	matrix[2][0] = 5;
	matrix[2][1] = 5;
	matrix[2][2] = 4;
	matrix[2][3] = 2;
	matrix[3][0] = -1;
	matrix[3][1] = -2;
	matrix[3][2] = 3.4;
	matrix[3][3] = -1;

	fwrite_matrix(stdout, "matrix", matrix, 4, 4);

	matrix_invert__alloc(matrix, 4, 4, &inverse);
	fwrite_matrix(stdout, "inverse", inverse, 4, 4);

	matrix_multiply__alloc(matrix, 4, 4, inverse, 4, 4, &result);
	fwrite_matrix(stdout, "result", result, 4, 4);

	printf("\n\n");

	matrix[0][0] = 1;
	matrix[0][1] = 3;
	matrix[0][2] = 3;
	matrix[1][0] = 1;
	matrix[1][1] = 4;
	matrix[1][2] = 3;
	matrix[2][0] = 1;
	matrix[2][1] = 3;
	matrix[2][2] = 4;
	/*
	matrix[0][0] = 1;
	matrix[0][1] = 2;
	matrix[0][2] = 0;
	matrix[1][0] = 3;
	matrix[1][1] = 4;
	matrix[1][2] = 4;
	matrix[2][0] = 5;
	matrix[2][1] = 6;
	matrix[2][2] = 3;
	*/

	b = (double*)malloc(sizeof(double) * 3);
	result2 = (double*)malloc(sizeof(double) * 3);
	b[0] = 3;
	b[1] = 7;
	b[2] = 8;

	fwrite_matrix(stdout, "matrix", matrix, 3, 3);
	matrix_invert__alloc(matrix, 3, 3, &inverse);

	LUP_decomposition__alloc(3, matrix, &LU, &p);

	fwrite_matrix(stdout, "LU", LU, 3, 3);
	printf("p: %d %d %d\n", p[0], p[1], p[2]);

	LUP_solve(3, LU, p, b, result2);

	printf("result: %lf %lf %lf\n", result2[0], result2[1], result2[2]);

	fwrite_matrix(stdout, "inverse", inverse, 3, 3);
	matrix_multiply__alloc(matrix, 3, 3, inverse, 3, 3, &result);
	fwrite_matrix(stdout, "result", result, 3, 3);
}
Пример #2
0
int
main(void)
{
    int plot;
    int i, j;
    int im;
    float x, y;
    float *rt, *ct;
    float **m;
    int xsize, ysize;
    char buf[256];
    FILE *fout;
/*  Create a few standard test interfaces */

    for (plot = 0; plot < NUM_PLOTS; plot++) {
	xsize = (TheRange[plot].xmax - TheRange[plot].xmin) * ISOSAMPLES + 1;
	ysize = (TheRange[plot].ymax - TheRange[plot].ymin) * ISOSAMPLES + 1;

	rt = calloc(xsize, sizeof(float));
	ct = calloc(ysize, sizeof(float));
	m = calloc(xsize, sizeof(m[0]));
	for (im = 0; im < xsize; im++) {
		m[im] = calloc(ysize, sizeof(m[0]));
	}

	for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
	    ct[j] = y;
	}

	for (x = TheRange[plot].xmin, i = 0; i < xsize; i++, x += 1.0 / (double) ISOSAMPLES) {
	    rt[i] = x;
	    for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
		m[i][j] = function(plot, x, y);
	    }
	}

	sprintf(buf, "binary%d", plot + 1);
	if (!(fout = fopen(buf, "wb"))) {
	    fprintf(stderr, "Could not open output file\n");
	    return -1;
	} else {
	    fwrite_matrix(fout, m, xsize, ysize, rt, ct);
	}
	free(rt);
	free(ct);
	for (im = 0; im < xsize; im++)
	    free(m[im]);
	free(m);
    }

    /* Show that it's ok to vary sampling rate, as long as x1<x2, y1<y2... */
    xsize = (TheRange[plot].xmax - TheRange[plot].xmin) * ISOSAMPLES + 1;
    ysize = (TheRange[plot].ymax - TheRange[plot].ymin) * ISOSAMPLES + 1;

    rt = calloc(xsize, sizeof(float));
    ct = calloc(ysize, sizeof(float));
    m = calloc(xsize, sizeof(m[0]));
    for (im = 0; im < xsize; im++) {
	    m[im] = calloc(ysize, sizeof(m[0]));
    }

    for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
	ct[j] = y > 0 ? 2 * y : y;
    }
    for (x = TheRange[plot].xmin, i = 0; i < xsize; i++, x += 1.0 / (double) ISOSAMPLES) {
	rt[i] = x > 0 ? 2 * x : x;
	for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
	    m[i][j] = function(plot, x, y);
	}
    }

    sprintf(buf, "binary%d", plot + 1);
    if (!(fout = fopen(buf, "wb"))) {
	fprintf(stderr, "Could not open output file\n");
	return -1;
    } else {
	fwrite_matrix(fout, m, xsize, ysize, rt, ct);
    }
    free(rt);
    free(ct);
    for (im = 0; im < xsize; im++)
	free(m[im]);
    free(m);

    return 0;
}
Пример #3
0
int main(int argc, char **argv) {
	double **a, **b, **c;
	double *v, *w;

	new_matrix(&a, 2, 3);
	new_matrix(&b, 3, 2);

	//1 0 2
	//-1 3 1
	//
	//3 1
	//2 1
	//1 0
	a[0][0] = 1;
	a[0][1] = 0;
	a[0][2] = 2;
	a[1][0] = -1;
	a[1][1] = 3;
	a[1][2] = 1;

	b[0][0] = 3;
	b[0][1] = 1;
	b[1][0] = 2;
	b[1][1] = 1;
	b[2][0] = 1;
	b[2][1] = 0;

	fwrite_matrix(stdout, "a", a, 2, 3);
	fwrite_matrix(stdout, "b", b, 3, 2);

	matrix_multiply__alloc(a, 2, 3, b, 3, 2, &c);

	fwrite_matrix(stdout, "a * b = c", c, 2, 2);
	free_matrix(&c, 2, 2);
	printf("should be:\n 5 1\n 4 2\n");

	matrix_multiply__alloc(b, 3, 2, a, 2, 3, &c);
	fwrite_matrix(stdout, "b * a = c2", c, 3, 3);
	printf("should be:\n");

	free_matrix(&a, 2, 3);
	free_matrix(&c, 2, 2);

	new_matrix(&a, 2, 3);
	a[0][0] = 1;
	a[0][1] = -1;
	a[0][2] = 2;
	a[1][0] = 0;
	a[1][1] = -3;
	a[1][2] = 1;

	v = (double*)malloc(sizeof(double) * 3);
	v[0] = 2;
	v[1] = 1;
	v[2] = 0;

	printf("\n\n");
	fwrite_matrix(stdout, "a", a, 2, 3);
	printf("v: %lf %lf %lf\n", v[0], v[1], v[2]);

	matrix_vector_multiply__alloc(a, 2, 3, v, 3, &w);
	printf("a * v = w: %lf %lf\n", w[0], w[1]);
	printf("should be: 1 -3\n");
	free(w);
	free(v);
	free_matrix(&a, 2, 3);
}
Пример #4
0
int
main(void)
{
    int plot;
    int i, j;
    float x, y;
    float *rt, *ct;
    float **m;
    int xsize, ysize;
    char buf[256];
    FILE *fout;
/*  Create a few standard test interfaces */

    for (plot = 0; plot < NUM_PLOTS; plot++) {
	xsize = (TheRange[plot].xmax - TheRange[plot].xmin) * ISOSAMPLES + 1;
	ysize = (TheRange[plot].ymax - TheRange[plot].ymin) * ISOSAMPLES + 1;

	rt = alloc_vector(0, xsize - 1);
	ct = alloc_vector(0, ysize - 1);
	m = matrix(0, xsize - 1, 0, ysize - 1);

	for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
	    ct[j] = y;
	}

	for (x = TheRange[plot].xmin, i = 0; i < xsize; i++, x += 1.0 / (double) ISOSAMPLES) {
	    rt[i] = x;
	    for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
		m[i][j] = function(plot, x, y);
	    }
	}

	sprintf(buf, "binary%d", plot + 1);
	if (!(fout = fopen(buf, "wb")))
	    int_error(0, "Could not open file");
	else {
	    fwrite_matrix(fout, m, 0, xsize - 1, 0, ysize - 1, rt, ct);
	}
	free_vector(rt, 0);
	free_vector(ct, 0);
	free_matrix(m, 0, xsize - 1, 0);
    }

    /* Show that it's ok to vary sampling rate, as long as x1<x2, y1<y2... */
    xsize = (TheRange[plot].xmax - TheRange[plot].xmin) * ISOSAMPLES + 1;
    ysize = (TheRange[plot].ymax - TheRange[plot].ymin) * ISOSAMPLES + 1;

    rt = alloc_vector(0, xsize - 1);
    ct = alloc_vector(0, ysize - 1);
    m = matrix(0, xsize - 1, 0, ysize - 1);

    for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
	ct[j] = y > 0 ? 2 * y : y;
    }
    for (x = TheRange[plot].xmin, i = 0; i < xsize; i++, x += 1.0 / (double) ISOSAMPLES) {
	rt[i] = x > 0 ? 2 * x : x;
	for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
	    m[i][j] = function(plot, x, y);
	}
    }

    sprintf(buf, "binary%d", plot + 1);
    if (!(fout = fopen(buf, "wb")))
	int_error(0, "Could not open file");
    else {
	fwrite_matrix(fout, m, 0, xsize - 1, 0, ysize - 1, rt, ct);
    }
    free_vector(rt, 0);
    free_vector(ct, 0);
    free_matrix(m, 0, xsize - 1, 0);

    return EXIT_SUCCESS;
}