Ejemplo n.º 1
0
/**
 * Test eigenvalues, eigenvectors.
 */
void test_m_eigen()
{
	precision_t data[][4] = {
		{ 1.0000, 0.5000, 0.3333, 0.2500 },
		{ 0.5000, 1.0000, 0.6667, 0.5000 },
		{ 0.3333, 0.6667, 1.0000, 0.7500 },
		{ 0.2500, 0.5000, 0.7500, 1.0000 }
	};

	matrix_t *M = m_initialize(4, 4);
	matrix_t *M_eval = m_initialize(M->rows, 1);
	matrix_t *M_evec = m_initialize(M->rows, M->cols);

	fill_matrix_data(M, data);

	m_eigen(M, M_eval, M_evec);

	printf("M = \n");
	m_fprint(stdout, M);

	printf("eigenvalues of M = \n");
	m_fprint(stdout, M_eval);

	printf("eigenvectors of M = \n");
	m_fprint(stdout, M_evec);

	m_free(M);
	m_free(M_eval);
	m_free(M_evec);
}
Ejemplo n.º 2
0
/**
 * Test matrix subtraction.
 */
void test_m_subtract()
{
	precision_t data_A[][2] = {
		{ 1, 0 },
		{ 2, 4 }
	};
	precision_t data_B[][2] = {
		{ 5, 9 },
		{ 2, 1 }
	};

	matrix_t *A = m_initialize(2, 2);
	matrix_t *B = m_initialize(2, 2);

	fill_matrix_data(A, data_A);
	fill_matrix_data(B, data_B);

	printf("A = \n");
	m_fprint(stdout, A);

	printf("B = \n");
	m_fprint(stdout, B);

	m_subtract(A, B);

	printf("A - B = \n");
	m_fprint(stdout, A);

	m_free(A);
	m_free(B);
}
/*******************************************************************************
 * void matrix_eig(data_t *out_eig_vect, data_t*out_eig_vals, data_t* matrix, int rows, int cols); 
 * Get eigenvalues and eigenvectors of symmetric matrix
 * NOTE: ONLY SYMMETRIC MATRICIES ATM
*******************************************************************************/
void m_eigenvalues_eigenvectors (matrix_t *M, matrix_t **p_eigenvalues, matrix_t **p_eigenvectors) {
	gsl_matrix * A = gsl_matrix_alloc (M->numRows, M->numCols);
	gsl_matrix * gslEigenvectors = gsl_matrix_alloc (M->numRows, M->numCols);
	gsl_vector * gslEigenvalues = gsl_vector_alloc (M->numRows);
	
	precision val;
	int i, j;
	// Copy M into A
	for (i = 0; i < M->numRows; i++) {
		for (j = 0; j < M->numCols; j++) {
			val = m_getelem(M, i, j);
			gsl_matrix_set (A, i, j, val);
		}
	}

	// Compute the Eigenvalues using the GSL library
	// Allocate workspace
	gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc (M->numRows);

	gsl_eigen_symmv (A, gslEigenvalues, gslEigenvectors, w);

	// ********************************************************
	// COMMENT
	// We might need to normalize the eigenvectors here or something
	// to match matlab eigenvectors, they don't HAVE to to match but
	// its at least something to keep in mind
	// ********************************************************
	
	matrix_t *eigenvalues = m_initialize (UNDEFINED, gslEigenvalues->size, 1);
	matrix_t *eigenvectors = m_initialize (UNDEFINED, gslEigenvectors->size1, gslEigenvectors->size2);

	// Copy the eigenvalues into a column matrix
	for (i = 0; i < gslEigenvalues->size; i++) {
		val = gsl_vector_get (gslEigenvalues, i);
		m_setelem(val, eigenvalues, i, 0);
	}
	
	// Copy the eigenvectors into a regular matrix
	for (i = 0; i < gslEigenvectors->size1; i++) {
		for (j = 0; j < gslEigenvectors->size2; j++) {
			val = gsl_matrix_get (gslEigenvectors, i, j);
			m_setelem(val, eigenvectors, i, j);
		}
	}
	gsl_eigen_symmv_free (w);
	gsl_matrix_free (gslEigenvectors);
	gsl_matrix_free (A);
	gsl_vector_free (gslEigenvalues);
	
	*p_eigenvectors = eigenvectors;
	*p_eigenvalues = eigenvalues; 
    
}
Ejemplo n.º 4
0
int main(int argc, char **argv)
{
	if ( argc != 2 ) {
		fprintf(stderr, "usage: ./test-image [image-file]\n");
		return 1;
	}

	const char *FILENAME_IN = argv[1];
	const char *FILENAME_OUT = "wahaha.ppm";

	// map an image to a column vector
	image_t *image = image_construct();
	image_read(image, FILENAME_IN);

	matrix_t *x = m_initialize(image->channels * image->width * image->height, 1);
	m_image_read(x, 0, image);

	// map a column vector to an image
	m_image_write(x, 0, image);
	image_write(image, FILENAME_OUT);

	image_destruct(image);
	m_free(x);

	return 0;
}
Ejemplo n.º 5
0
/**
 * Test matrix copying.
 */
void test_m_copy()
{
	precision_t data[][4] = {
		{ 16,  2,  3, 13 },
		{  5, 11, 10,  8 },
		{  9,  7,  6, 12 },
		{  4, 14, 15,  1 }
	};

	matrix_t *A = m_initialize(4, 4);
	fill_matrix_data(A, data);

	printf("A = \n");
	m_fprint(stdout, A);

	matrix_t *C1 = m_copy(A);

	int begin = 1;
	int end = 3;
	matrix_t *C2 = m_copy_columns(A, begin, end);

	printf("C1 = A = \n");
	m_fprint(stdout, C1);
	printf("C2 = A(:, %d:%d) = \n", begin + 1, end);
	m_fprint(stdout, C2);

	m_free(A);
	m_free(C1);
	m_free(C2);
}
Ejemplo n.º 6
0
/**
 * Test matrix square root.
 */
void test_m_sqrtm()
{
	precision_t data[][5] = {
		{  5, -4,  1,  0,  0 },
		{ -4,  6, -4,  1,  0 },
		{  1, -4,  6, -4,  1 },
		{  0,  1, -4,  6, -4 },
		{  0,  0,  1, -4,  6 }
	};

	matrix_t *A = m_initialize(5, 5);
	fill_matrix_data(A, data);

	matrix_t *X = m_sqrtm(A);
	matrix_t *X_sq = m_product(X, X);

	printf("A = \n");
	m_fprint(stdout, A);
	printf("X = sqrtm(A) = \n");
	m_fprint(stdout, X);
	printf("X * X = \n");
	m_fprint(stdout, X_sq);

	m_free(A);
	m_free(X);
	m_free(X_sq);
}
Ejemplo n.º 7
0
/**
 * Test matrix inverse.
 */
void test_m_inverse()
{
	precision_t data[][3] = {
		{  1,  0,  2 },
		{ -1,  5,  0 },
		{  0,  3, -9 }
	};

	matrix_t *X = m_initialize(3, 3);
	fill_matrix_data(X, data);

	matrix_t *Y = m_inverse(X);
	matrix_t *Z = m_product(Y, X);

	printf("X = \n");
	m_fprint(stdout, X);
	printf("Y = inv(X) = \n");
	m_fprint(stdout, Y);
	printf("Y * X = \n");
	m_fprint(stdout, Z);

	m_free(X);
	m_free(Y);
	m_free(Z);
}
Ejemplo n.º 8
0
/**
 * Implementation of the learning rule described in Bell & Sejnowski,
 * Vision Research, in press for 1997, that contained the natural
 * gradient (W' * W).
 *
 * Bell & Sejnowski hold the patent for this learning rule.
 *
 * SEP goes once through the mixed signals X in batch blocks of size B,
 * adjusting weights W at the end of each block.
 *
 * sepout is called every F counts.
 *
 * I suggest a learning rate (L) of 0.006, and a block size (B) of
 * 300, at least for 2->2 separation.  When annealing to the right
 * solution for 10->10, however, L < 0.0001 and B = 10 were most successful.
 *
 * @param X  "sphered" input matrix
 * @param W  weight matrix
 * @param B  block size
 * @param L  learning rate
 * @param F  interval to print training stats
 */
void sep96(matrix_t *X, matrix_t *W, int B, double L, int F)
{
    matrix_t *BI = m_identity(X->rows);
    m_elem_mult(BI, B);

    int t;
    for ( t = 0; t < X->cols; t += B ) {
        int end = (t + B < X->cols)
            ? t + B
            : X->cols;

        matrix_t *W0 = m_copy(W);
        matrix_t *X_batch = m_copy_columns(X, t, end);
        matrix_t *U = m_product(W0, X_batch);

        // compute Y' = 1 - 2 * f(U), f(u) = 1 / (1 + e^(-u))
        matrix_t *Y_p = m_initialize(U->rows, U->cols);

        int i, j;
        for ( i = 0; i < Y_p->rows; i++ ) {
            for ( j = 0; j < Y_p->cols; j++ ) {
                elem(Y_p, i, j) = 1 - 2 * (1 / (1 + exp(-elem(U, i, j))));
            }
        }

        // compute dW = L * (BI + Y'U') * W0
        matrix_t *U_tr = m_transpose(U);
        matrix_t *dW_temp1 = m_product(Y_p, U_tr);
        m_add(dW_temp1, BI);

        matrix_t *dW = m_product(dW_temp1, W0);
        m_elem_mult(dW, L);

        // compute W = W0 + dW
        m_add(W, dW);

        // print training stats
        if ( t % F == 0 ) {
            precision_t norm = m_norm(dW);
            precision_t angle = m_angle(W0, W);

            printf("*** norm(dW) = %.4lf, angle(W0, W) = %.1lf deg\n", norm, 180 * angle / M_PI);
        }

        // cleanup
        m_free(W0);
        m_free(X_batch);
        m_free(U);
        m_free(Y_p);
        m_free(U_tr);
        m_free(dW_temp1);
        m_free(dW);
    }
}
Ejemplo n.º 9
0
/**
 * Test generalized eigenvalues, eigenvectors for two matrices.
 */
void test_m_eigen2()
{
	precision_t data_A[][3] = {
		{ 0, 0, 0 },
		{ 0, 0, 0 },
		{ 0, 0, 0 }
	};

	precision_t data_B[][3] = {
		{ 1.0, 0.1, 0.1 },
		{ 0.1, 2.0, 0.1 },
		{ 1.0, 0.1, 3.0 }
	};

	matrix_t *A = m_initialize(3, 3);
	matrix_t *B = m_initialize(3, 3);
	matrix_t *J_eval = m_initialize(A->rows, 1);
	matrix_t *J_evec = m_initialize(A->rows, A->cols);

	fill_matrix_data(A, data_A);
	fill_matrix_data(B, data_B);

	m_eigen2(A, B, J_eval, J_evec);

	printf("A = \n");
	m_fprint(stdout, A);

	printf("B = \n");
	m_fprint(stdout, B);

	printf("eigenvalues of J = \n");
	m_fprint(stdout, J_eval);

	printf("eigenvectors of J = \n");
	m_fprint(stdout, J_evec);

	m_free(A);
	m_free(B);
	m_free(J_eval);
	m_free(J_evec);
}
Ejemplo n.º 10
0
/**
 * Test matrix column shuffling.
 */
void test_m_shuffle_columns()
{
	precision_t data[][4] = {
		{ 1, 2, 3, 4 },
		{ 5, 6, 7, 8 }
	};

	matrix_t *A = m_initialize(2, 4);
	fill_matrix_data(A, data);

	printf("A = \n");
	m_fprint(stdout, A);

	m_shuffle_columns(A);

	printf("m_shuffle_columns (A) = \n");
	m_fprint(stdout, A);

	m_free(A);
}
Ejemplo n.º 11
0
/**
 * Test the matrix convariance.
 */
void test_m_covariance()
{
	precision_t data[][3] = {
		{ 5, 1, 4 },
		{ 0, -5, 9 },
		{ 3, 7, 8 },
		{ 7, 3, 10 }
	};

	matrix_t *A = m_initialize(4, 3);
	fill_matrix_data(A, data);

	matrix_t *C = m_covariance(A);

	printf("A = \n");
	m_fprint(stdout, A);
	printf("cov(A) = \n");
	m_fprint(stdout, C);

	m_free(A);
	m_free(C);
}
Ejemplo n.º 12
0
/**
 * Test matrix multiplication by scalar.
 */
void test_m_elem_mult()
{
	precision_t data[][3] = {
		{ 1, 0, 2 },
		{ 3, 1, 4 }
	};

	matrix_t *A = m_initialize(2, 3);
	precision_t c = 3;

	fill_matrix_data(A, data);

	printf("A = \n");
	m_fprint(stdout, A);

	m_elem_mult(A, c);

	printf("%lg * A = \n", c);
	m_fprint(stdout, A);

	m_free(A);
}
Ejemplo n.º 13
0
/**
 * Test matrix mean column.
 */
void test_m_mean_column()
{
	precision_t data[][4] = {
		{ 0, 2, 1, 4 },
		{ 1, 3, 3, 2 },
		{ 1, 2, 2, 2 }
	};

	matrix_t *A = m_initialize(3, 4);
	fill_matrix_data(A, data);

	matrix_t *m = m_mean_column(A);

	printf("A = \n");
	m_fprint(stdout, A);

	printf("mean(A) = \n");
	m_fprint(stdout, m);

	m_free(A);
	m_free(m);
}
Ejemplo n.º 14
0
/**
 * Test matrix transpose.
 */
void test_m_transpose()
{
	precision_t data[][4] = {
		{ 16,  2,  3, 13 },
		{  5, 11, 10,  8 },
		{  9,  7,  6, 12 },
		{  4, 14, 15,  1 }
	};

	matrix_t *A = m_initialize(4, 4);
	fill_matrix_data(A, data);

	matrix_t *B = m_transpose(A);

	printf("A = \n");
	m_fprint(stdout, A);

	printf("B = A' = \n");
	m_fprint(stdout, B);

	m_free(A);
	m_free(B);
}
Ejemplo n.º 15
0
/**
 * Test matrix product.
 */
void test_m_product()
{
	// multiply two vectors, A * B
	precision_t data_A1[][4] = {
		{ 1, 1, 0, 0 }
	};
	precision_t data_B1[][1] = {
		{ 1 },
		{ 2 },
		{ 3 },
		{ 4 }
	};

	matrix_t *A = m_initialize(1, 4);
	matrix_t *B = m_initialize(4, 1);

	fill_matrix_data(A, data_A1);
	fill_matrix_data(B, data_B1);

	matrix_t *C = m_product(A, B);

	printf("A = \n");
	m_fprint(stdout, A);
	printf("B = \n");
	m_fprint(stdout, B);

	printf("A * B = \n");
	m_fprint(stdout, C);
	m_free(C);

	// multiply two vectors, B * A
	C = m_product(B, A);

	printf("B * A = \n");
	m_fprint(stdout, C);
	m_free(C);

	m_free(A);
	m_free(B);

	// multiply two arrays
	precision_t data_A2[][3] = {
		{ 1, 3, 5 },
		{ 2, 4, 7 }
	};
	precision_t data_B2[][3] = {
		{ -5, 8, 11 },
		{  3, 9, 21 },
		{  4, 0,  8 }
	};

	A = m_initialize(2, 3);
	B = m_initialize(3, 3);

	fill_matrix_data(A, data_A2);
	fill_matrix_data(B, data_B2);

	C = m_product(A, B);

	printf("A = \n");
	m_fprint(stdout, A);
	printf("B = \n");
	m_fprint(stdout, B);

	printf("A * B = \n");
	m_fprint(stdout, C);

	m_free(A);
	m_free(B);
	m_free(C);
}
int main (void) {
	
	FILE *output = fopen ("testResults.txt", "w");

	matrix_t *M = m_initialize (FILL, XDIM, YDIM);
    matrix_t *R; 

	fprintf (output, "M = \n");
	m_fprint (output, M);

	// Test Group 3
	fprintf (output, "\n-------------Test Group 3 -------------\n");
	M = m_initialize (FILL, XDIM, YDIM);
	
	fprintf (output, "m_norm (M, specRow) is SKIPPED IN THIS TEST\n");
	
	R = m_sqrtm (M);
	fprintf (output, "m_sqrtm(M) = \n");
	m_fprint (output, R);
	m_free (R);

	
	precision val = m_determinant (M);
	fprintf (output, "m_determinant(M) = %lf\n", val);

	R = m_cofactor (M);
	fprintf (output, "m_cofactor(M) = \n");
	m_fprint (output, R);
	m_free (R);

    matrix_t *N = m_initialize (FILL, 3, 3);
	R = m_cofactor (N);
    fprintf (output, "Three-by-Three matrix N\n");
	fprintf (output, "m_cofactor(N) = \n");
	m_fprint (output, R);
	m_free (R);
    m_free (N);

    N = m_initialize (FILL, 2, 2);
	R = m_cofactor (N);
    fprintf (output, "Two-by-Two matrix N\n");
	fprintf (output, "m_cofactor(N) = \n");
	m_fprint (output, R);
	m_free (R);
    m_free (N);

    N = m_initialize (IDENTITY, XDIM, YDIM);
	R = m_cofactor (N);
    fprintf (output, "Identity matrix 6x6 N\n");
    fprintf (output, "m_determinant(N)\n");
    fprintf (output, "%lf\n", m_determinant (N));
	fprintf (output, "m_cofactor(N) = \n");
	m_fprint (output, R);
	m_free (R);
    m_free (N);

    N = m_initialize (IDENTITY, 5, 5);
	R = m_cofactor (N);
    fprintf (output, "Identity matrix 5x5 N\n");
    fprintf (output, "m_determinant(N)\n");
    fprintf (output, "%lf\n", m_determinant (N));
    fprintf (output, "m_cofactor(N) = \n");
	m_fprint (output, R);
	m_free (R);
    m_free (N);

    N = m_initialize (IDENTITY, 4, 4);
	R = m_cofactor (N);
    fprintf (output, "Identity matrix 4x4 N\n");
    fprintf (output, "m_determinant (N)\n");
    fprintf (output, "%lf\n", m_determinant (N));
    fprintf (output, "m_cofactor (N) = \n");
	m_fprint (output, R);
	m_free (R);
    m_free (N);

    N = m_initialize (IDENTITY, 3, 3);
	R = m_cofactor (N);
    fprintf (output, "Identity matrix 3x3 N\n");
    fprintf (output, "m_determinant (N)\n");
    fprintf (output, "%lf\n", m_determinant (N));
    fprintf (output, "m_cofactor (N) = \n");
	m_fprint (output, R);
	m_free (R);
    m_free (N);

    N = m_initialize (IDENTITY, 2, 2);
	R = m_cofactor (N);
    fprintf (output, "Identity matrix 2x2 N\n");
    fprintf (output, "m_determinant (N)\n");
    fprintf (output, "%lf\n", m_determinant (N));
    fprintf (output, "m_cofactor (N) = \n");
	m_fprint (output, R);
	m_free (R);
    m_free (N);

    // Causes Segfault
    //R = m_covariance (M);
	//fprintf (output, "m_covariance(M) = \n");
	//m_fprint (output, R);

    // Causes ABORT - Double Free?
    // m_free (R);
	// m_free (M);
	// m_free (R);

	return 0;
}