/******************************************************************************* * int LUP_decomposition(matrix_t A, matrix_t* L, matrix_t* U, matrix_t* P) * * LUP decomposition with partial pivoting *******************************************************************************/ int LUP_decomposition(matrix_t A, matrix_t* L, matrix_t* U, matrix_t* P){ int i, j, k, m, index; float s1, s2, temp; m = A.cols; destroy_matrix(L); destroy_matrix(U); destroy_matrix(P); matrix_t Lt, Ut, Pt; if(!A.initialized){ printf("ERROR: matrix not initialized yet\n"); return -1; } if(A.cols != A.rows){ printf("ERROR: matrix is not square\n"); return -1; } Lt = create_identity_matrix(m); Ut = create_square_matrix(m); Pt = create_identity_matrix(m); for(i=0;i<m-1;i++){ index = i; for(j=i;j<m;j++){ if(fabs(A.data[j][i]) >= fabs(A.data[index][i])){ index = j; } } if(index != i){ for(j=0;j<m;j++){ temp = A.data[index][j]; A.data[index][j] = A.data[i][j]; A.data[i][j] = temp; temp = Pt.data[index][j]; Pt.data[index][j] = Pt.data[i][j]; Pt.data[i][j] = temp; } } } for(i=0;i<m;i++){ for(j=0;j<m;j++){ s1 = 0; s2 = 0; for(k=0;k<i;k++){ s1 += Ut.data[k][j] * Lt.data[i][k]; } for(k=0;k<j;k++){ s2 += Ut.data[k][j] * Lt.data[i][k]; } if(j>=i) Ut.data[i][j] = A.data[i][j] - s1; if(i>=j) Lt.data[i][j] = (A.data[i][j] - s2)/Ut.data[j][j]; } } *L = Lt; *U = Ut; *P = Pt; return 0; }
struct matrix * scale(int x, int y, int z){ struct matrix *trans; trans = new_matrix(4,4); trans = create_identity_matrix(trans); int i,j; for(i = 0; i < trans->rows; i++){ for(j = 0; j< trans->cols; j++){ if(i == j && j == 0){ trans->m[i][j] = x; } else if(i == j && j == 1){ trans->m[i][j] = y; } else if(i == j && j == 2){ trans->m[i][j] = z; } } } print_trans_matrix(trans); return trans; }
struct matrix * rotate_z(double theta){ struct matrix *trans; trans = new_matrix(4,4); trans = create_identity_matrix(trans); trans->m[0][0] = cos(degrees_to_radians(theta)); trans->m[0][1] = -1 * sin(degrees_to_radians(theta)); trans->m[1][0] = sin(degrees_to_radians(theta)); trans->m[1][1] = cos(degrees_to_radians(theta)); print_trans_matrix(trans); return trans; }
int main(int argc, char *argv[]) { pclu_context *pclu = pclu_create_context(); printf("\n%s\n", pclu_context_info(pclu)); matrix *aa = create_random_matrix(SIZE, SIZE); matrix *bb = create_identity_matrix(SIZE, SIZE); #if PRINT_DATA printf("Matrix aa:\n"); print_matrix(aa); printf("\n"); printf("Matrix bb:\n"); print_matrix(bb); printf("\n"); #endif timer* tt1 = timer_alloc(); matrix *cc = create_matrix(SIZE, SIZE); matrix_multiply_cl(pclu, cc, aa, bb); double tm1 = timer_read(tt1); timer_free(tt1); printf("Matrix_multiply_cl took %.04f seconds.\n", tm1); #if PRINT_DATA printf("Matrix cc:\n"); print_matrix(cc); printf("\n"); #endif timer* tt = timer_alloc(); check_result(aa, cc); double ct = timer_read(tt); timer_free(tt); printf("Check result took %.04f seconds.\n", ct); destroy_matrix(aa); destroy_matrix(bb); pclu_destroy_context(pclu); return 0; }
int main() { /* Create orthographic projection matrix. */ float ortho[16]; float width = 1280.0f; float height = 720.0f; create_ortho_matrix(0.0f, width, height, 0.0f, 0.0f, 100.0f, ortho); float ident[16]; create_identity_matrix(ident); float translation[16]; create_translation_matrix(10.0f, 10.0f, 0.0f, translation); return 0; }
/******************************************************************************* * matrix_t invert_matrix(matrix_t A) * * Invert Matrix function based on LUP decomposition and then forward and * backward substitution. *******************************************************************************/ matrix_t invert_matrix(matrix_t A){ int i,j,k,m; matrix_t L,U,P,D,temp; matrix_t out = create_empty_matrix(); if(!A.initialized){ printf("ERROR: matrix not initialized yet\n"); return out; } if(A.cols != A.rows){ printf("ERROR: matrix is not square\n"); return out; } if(matrix_determinant(A) == 0){ printf("ERROR: matrix is singular, not invertible\n"); return out; } m = A.cols; LUP_decomposition(A,&L,&U,&P); D = create_identity_matrix(m); temp = create_square_matrix(m); for(j=0;j<m;j++){ for(i=0;i<m;i++){ for(k=0;k<i;k++){ D.data[i][j] -= L.data[i][k] * D.data[k][j]; } } for(i=m-1;i>=0;i--){ // backwards.. last to first temp.data[i][j] = D.data[i][j]; for(k=i+1;k<m;k++){ temp.data[i][j] -= U.data[i][k] * temp.data[k][j]; } temp.data[i][j] = temp.data[i][j] / U.data[i][i]; } } // multiply by permutation matrix out = multiply_matrices(temp, P); // free allocation destroy_matrix(&temp); destroy_matrix(&L); destroy_matrix(&U); destroy_matrix(&P); destroy_matrix(&D); return out; }