Exemplo n.º 1
0
/**
 * Test the multiplication of two matrices of all ones
 **/
void ones_test(int m, int n, int k) {
  double *A, *B, *C, *C_ones, *C_zeros;

  printf("ones_test m=%d n=%d k=%d............", m, n, k);

  /* Allocate matrices */
  A = ones_matrix(m, k);
  B = ones_matrix(k, n);
  C = ones_matrix(m, n);

  C_ones = ones_matrix(m, n);
  C_zeros = zeros_matrix(m, n);

  /* C = (1.0/k)*(A*B) + 0.0*C */
  local_mm(m, n, k, (1.0 / k), A, m, B, k, 0.0, C, m);

  /* Verfiy the results */
  verify_matrix(m, n, C, C_ones);

  /* C = (1.0/k)*(A*B) + -1.0*C */
  local_mm(m, n, k, (1.0 / k), A, m, B, k, -1.0, C, m);

  /* Verfiy the results */
  verify_matrix(m, n, C, C_zeros);

  /* deallocate memory */
  deallocate_matrix(A);
  deallocate_matrix(B);
  deallocate_matrix(C);

  deallocate_matrix(C_ones);
  deallocate_matrix(C_zeros);

  printf("passed\n");
}
Exemplo n.º 2
0
/**
 * Verify that a matrix times the identity is itself
 **/
void identity_test(int n) {
  double *A, *B, *C;

  printf("identity_test n=%d............", n);

  /* Allocate matrices */
  A = random_matrix(n, n);
  B = identity_matrix(n, n);
  C = zeros_matrix(n, n);

  /* C = 1.0*(A*B) + 0.0*C */
  local_mm(n, n, n, 1.0, A, n, B, n, 5.0, C, n);

  /* Verfiy the results */
  verify_matrix(n, n, A, C);

  /* Backwards C = 1.0*(B*A) + 0.0*C */
  local_mm(n, n, n, 1.0, B, n, A, n, 0.0, C, n);

  /* Verfiy the results */
  verify_matrix(n, n, A, C);

  /* deallocate memory */
  deallocate_matrix(A);
  deallocate_matrix(B);
  deallocate_matrix(C);

  printf("passed\n");
}
int main()
{
	int size = SIZE;
	int *first, *result;
	monitor *m;

	first = make_square_matrix(size);
	result = make_square_matrix(size);

	initialize_matrix(first, size);

	NOTIFY(print_matrix(first, size, size));
 
	m = monitor_init(SELF);
	monitor_start(m);

	multiply_matrix_symm_transp(first, result, size);

	monitor_end(m);
	monitor_print_stats(m, VERBOSE);
	
	NOTIFY(print_matrix(result, size, size));
	NOTIFY(verify_matrix(first, result, size));
	
	monitor_free(m);

	return 0;
}
Exemplo n.º 4
0
int main(int argc, char** argv)
{
    printf("--------verify--------\n");
    verify_matrix();
    printf("----------------------\n");

    // autorelease関数をbeginとendにはさまないで使うのはNG
    // 全てのallocされた関数は
    // (1) free
    // (2) release
    // (3) begin-endの中でautorelease
    // のいずれかを行う必要がある。
    
    // 擬似逆行列演算がこれくらい簡単に記述できる。
    
    /* 3*2行列aを確保、成分ごとの代入 */
    matrix_t* a = matrix_alloc(3, 2);
    ELEMENT(a, 0, 0) = 1; ELEMENT(a, 0, 1) = 4;
    ELEMENT(a, 1, 0) = 2; ELEMENT(a, 1, 1) = 5;
    ELEMENT(a, 2, 0) = 3; ELEMENT(a, 2, 1) = 6;

    /* 擬似逆行列の演算 */
    // auto releaseモードに入る
    matrix_begin(); 
    // 擬似逆行列を求める。一行だけ!
    matrix_t* inva = matrix_product(matrix_inverse(matrix_product(matrix_transpose(a), a)), matrix_transpose(a));
    // 擬似逆行列の表示 (invaはmatrix_endで解放されるので、begin-end内で。)
    matrix_print(inva);
    // release poolを開放する
    matrix_end();

    /* ちなみに擬似逆行列を求める関数は内部に組んだので、それを使うこともできる。 */
    // auto releaseモードに入る
    matrix_begin(); 
    // 擬似逆行列を求める関数を呼ぶ。
    matrix_t* inva_simple = matrix_pseudo_inverse(a);
    // 擬似逆行列の表示 (invaはmatrix_endで解放されるので、begin-end内で。)
    matrix_print(inva_simple);
    // release poolを開放する
    matrix_end();

    
    // これはautorelease対象でないので、しっかり自分でfree。
    // リテインカウントを実装してあるので、理解できればreleaseの方が高性能。
    //matrix_free(a);
    matrix_release(a);
 
    return 0;
}