void test_basic_mtx_op(void) { /******************************/ /* Test the matrix operations */ /******************************/ int dim_i = 3; int dim_j = 3; int dim_k = 4; double matrix_A[9] = {0.9572, 0.1419, 0.7922, 0.4854, 0.4218, 0.9595, 0.8003, 0.9157, 0.6557}; double matrix_B[12] = {0.8147, 0.9134, 0.2785, 0.9649, 0.9058, 0.6324, 0.5469, 0.1576, 0.1270, 0.0975, 0.9575, 0.9706}; double matrix_D[9] = {3.5712, 1.4007, 2.5214, 1.4007, 84.9129, 2.7030, 2.5214, 2.7030, 93.3993}; double matrix_C[12]; double matrix_B_prime[12]; double matrix_L[9]; double matrix_L_prime[9]; double result = 0.0; printf("Show Matrix A\n"); show_matrix(matrix_A, dim_i, dim_j); printf("Show Matrix B\n"); show_matrix(matrix_B, dim_j, dim_k); /*test for matrix multiplication*/ printf("C = A * B\n"); mtx_mult(matrix_A, matrix_B, matrix_C, dim_i, dim_j, dim_k); show_matrix(matrix_C, dim_i, dim_k); /*test for matrix transpose*/ printf("B' = B\n"); mtx_transpose(matrix_B, matrix_B_prime, dim_j, dim_k); show_matrix(matrix_B_prime, dim_k, dim_j); /*test for cholesky decomposition*/ printf("Show Symmetric Matrix D\n"); show_matrix(matrix_D, dim_i, dim_j); printf("L = chol(D)\n"); mtx_chol(matrix_D, matrix_L, dim_i); show_matrix(matrix_L, dim_i, dim_i); printf("D = L*L'\n"); mtx_transpose(matrix_L, matrix_L_prime, dim_i, dim_i); mtx_mult(matrix_L, matrix_L_prime, matrix_D, dim_i, dim_i, dim_i); show_matrix(matrix_D, dim_i, dim_i); /*test validation tag*/ printf("Matrix test results validated using Matlab: \n"); printf("All good! 05/15, FS\n"); }
static char near_enough(t_ray *r1, t_ray *r2) { t_mtx p1; t_mtx p2; p1 = mtx_add(mtx_mult(r1->dir, r1->t), r1->pos); p2 = mtx_add(mtx_mult(r2->dir, r2->t), r2->pos); return (NEAR(p1.mtx[0], p2.mtx[0]) && NEAR(p1.mtx[1], p2.mtx[1]) && NEAR(p1.mtx[2], p2.mtx[2])); }
t_color compute_light(t_scene *scene, t_ray *ray) { t_list *current; t_ray lray; t_color color[3]; t_phpa ph; current = scene->lights; ph.normal = get_normal(*ray); set_ambiant_light(&ph, scene, ray, color); while (current) { lray.pos = ((t_light *)current->content)->pos; lray.dir = norm_vect(mtx_add(mtx_sub(mtx_mult(ray->dir, ray->t), lray.pos), ray->pos)); lray.invdir = get_inv_vect(&lray.dir); if (find_closest(scene, &lray) && ray->closest == lray.closest && near_enough(ray, &lray)) { set_params(&ph, &lray, ray); ph.camera = scene->camera; ph.light = (t_light *)current->content; phong(&ph); } current = current->next; } set_color_max(&ph); return (*color); }
void QvisReflectWidget::ScaleTranslateFromOriginToOctant(int octant, float s) { matrix4 scale = m3du_create_scaling_matrix(s,s,s); matrix4 translate; // Translate from the origin to the center of the specified octant. switch(octant) { case 0: translate = m3du_create_translation_matrix(5,5,5); break; case 1: translate = m3du_create_translation_matrix(-5,5,5); break; case 2: translate = m3du_create_translation_matrix(-5,-5,5); break; case 3: translate = m3du_create_translation_matrix(5,-5,5); break; case 4: translate = m3du_create_translation_matrix(5,5,-5); break; case 5: translate = m3du_create_translation_matrix(-5,5,-5); break; case 6: translate = m3du_create_translation_matrix(-5,-5,-5); break; case 7: translate = m3du_create_translation_matrix(5,-5,-5); break; } // Set the transform renderer.set_world_matrix(mtx_mult(scale, translate)); }
void QvisReflectWidget::setupAndDraw(QPainter *p) { // Fill in the background color. p->fillRect(rect(), palette().brush(QPalette::Background)); renderer.set_light(1, M3D_LIGHT_EYE, 0.f,0.f,-35.f, 1.f, 1.f, 1.f); renderer.set_light(2, M3D_LIGHT_OFF, 0.f,0.f,-1.f, 0.f, 0.f, 0.f); renderer.begin_scene(p); renderer.set_world_matrix(m3du_create_identity_matrix()); // Draw the reference axes axes.addToRenderer(renderer); // Draw the cubes or spheres. drawOnOffActors(8, 1.f); // If we're not switching cameras, add the arrow to the scene. if(!switchingCameras) { if(activeCamera == 0) { matrix4 world = mtx_mult( m3du_create_scaling_matrix(1., 1., -1.), m3du_create_translation_matrix(axes_size + 3, -axes_size, -ARROW_LENGTH * 0.5f)); renderer.set_world_matrix(world); arrow.addToRenderer(renderer, ARROW_ID); } else { matrix4 world = mtx_mult( m3du_create_scaling_matrix(1., 1., -1.), m3du_create_translation_matrix(-axes_size - 3, -axes_size, -ARROW_LENGTH * 0.5)); renderer.set_world_matrix(world); arrow.addToRenderer(renderer, ARROW_ID); } } // Render the scene renderer.end_scene(); // Add some annotation if(!switchingCameras) { int cx = width() / 2; int cy = height() / 2; int edge = qMin(width(), height()); QRect square(cx - edge / 2, cy - edge / 2, edge, edge); if(activeCamera == 0) { const char *txt = "Front view"; p->drawText(square.x() + 5, square.y() + square.height() - 5, txt); vector3 p0 = renderer.transform_world_point(vec_create(axes_size, 0, 0)); vector3 p1 = renderer.transform_world_point(vec_create(0, axes_size, 0)); vector3 p2 = renderer.transform_world_point(vec_create(0, 0, axes_size)); p->drawText((int) (p0.x + 5), (int) (p0.y + 5), "+X"); p->drawText((int) (p1.x), (int) (p1.y - 5), "+Y"); p->drawText((int) (p2.x - 20), (int) (p2.y + 5), "+Z"); } else { const char *txt = "Back view"; p->drawText(square.x() + 5, square.y() + square.height() - 5, txt); vector3 p0 = renderer.transform_world_point(vec_create(-axes_size, 0, 0)); vector3 p1 = renderer.transform_world_point(vec_create(0, axes_size, 0)); vector3 p2 = renderer.transform_world_point(vec_create(0, 0, -axes_size)); p->drawText((int) (p0.x + 5), (int) (p0.y + 5), "-X"); p->drawText((int) (p1.x), (int) (p1.y - 5), "+Y"); p->drawText((int) (p2.x - 20), (int) (p2.y + 5), "-Z"); } } }
t_mtx vect_reflect(t_mtx i, t_mtx n) { return (mtx_sub(i, mtx_mult(n, 2 * DOTV(n, i)))); }
void test_pca(void){ int DIM_I = 6; int DIM_J=6; int i; double a[36]= { 3.5712, 1.4007, 2.5214,35.7680,12.5698,34.5678, 1.4007, 84.9129, 2.7030, 64.5638,4.5645,56.4523, 2.5214, 2.7030, 93.3993,32.4563,56.4322,24.4678, 35.7680,64.5638,32.4563,43.2345,21.3456,32.5476, 12.5698,4.5645,56.4322,21.3456,78.4356,65.4356, 34.5678,56.4523,24.4678,32.5476,65.4356,21.4567 }; double lancz_trans_mtx[36]; double *mean = (double*)calloc(sizeof(double),DIM_J); double *b = (double*)calloc(sizeof(double),(DIM_J*DIM_I)); double *b_prime = (double*)calloc(sizeof(double),(DIM_J*DIM_I)); double *cov= (double*)calloc(sizeof(double),(DIM_J*DIM_I)); double *eigenvalues=(double*)calloc(sizeof(double),(DIM_J)); double *Identity = (double*)calloc(sizeof(double),(DIM_J*DIM_I)); double *mtx_diag= (double*)calloc(sizeof(double),(DIM_J*DIM_I)); double *mtx_off_diag= (double*)calloc(sizeof(double),(DIM_J*DIM_I)); double *ident_eigen= (double*)calloc(sizeof(double),(DIM_J*DIM_I)); show_matrix(a,DIM_I,DIM_J); stat_mean(a,mean,DIM_I,DIM_J); printf("\nexpected mean matrix \n" "15.06650 35.76620 35.33000 38.31930 39.79720 39.15460"); printf("\n\nMean matrix \n"); show_matrix(mean,1,DIM_J); printf("\nexpected derivation from the mean matrix\n" "-11.49528 -34.36550 -32.80860 -2.55130 -27.22742 -4.58683\n" "-13.66578 49.14670 -32.62700 26.24450 -35.23272 17.29767\n" "-12.54508 -33.06320 58.06930 -5.86300 16.63498 -14.68683\n" "20.70152 28.79760 -2.87370 4.91520 -18.45162 -6.60703\n" "-2.49668 -31.20170 21.10220 -16.97370 38.63838 26.28097\n" "19.50132 20.68610 -10.86220 -5.77170 25.63838 -17.69793\n"); printf("\n Derivation from the mean matrix \n"); mtx_deriv_mean(b,a,mean,DIM_I,DIM_J); show_matrix(b,DIM_I,DIM_J); printf("\n"); printf("Expected transpose matrix \n""-11.49528 -13.66578 -12.54508 20.70152 -2.49668 19.50132\n" "-34.36550 49.14670 -33.06320 28.79760 -31.20170 20.68610\n" "-32.80860 -32.62700 58.06930 -2.87370 21.10220 -10.86220\n" "-2.55130 26.24450 -5.86300 4.91520 -16.97370 -5.77170\n" "-27.22742 -35.23272 16.63498 -18.45162 38.63838 25.63838\n" "-4.58683 17.29767 -14.68683 -6.60703 26.28097 -17.69793\n"); printf("\n\n Transpose matrix \n"); mtx_transpose(b,b_prime,DIM_I,DIM_J); show_matrix(b_prime,DIM_J,DIM_I); printf("\nExpected multiplication prior to covariance matrix (b*b')\n" "3158.41375 351.58407 -995.33304 -613.17658 -720.63631 -1180.85189\n" "351.58407 5896.00026 -4342.15094 1890.97606 -3540.04502 -256.36445\n" "-995.33304 -4342.15094 5149.39843 -1617.44056 2644.62062 -839.09450\n" "-613.17658 1890.97606 -1617.44056 1674.38695 -1980.86959 646.12372\n" "-720.63631 -3540.04502 2644.62062 -1980.86959 3896.80272 -299.87242\n" "-1180.85189 -256.36445 -839.09450 646.12372 -299.87242 1930.05954\n"); printf("\n\n Multiplication prior to covariance matrix (b*b')\n"); mtx_mult(b,b_prime,cov,DIM_I,DIM_J,DIM_I); show_matrix(cov,DIM_I,DIM_I); for (i=0;i<DIM_I*DIM_I;i++) cov[i] /= DIM_I; printf("\nExpected covarianc matrix\n" "526.40229 58.59735 -165.88884 -102.19610 -120.10605 -196.80865\n" "58.59735 982.66671 -723.69182 315.16268 -590.00750 -42.72741\n" "-165.88884 -723.69182 858.23307 -269.57343 440.77010 -139.84908\n" "-102.19610 315.16268 -269.57343 279.06449 -330.14493 107.68729\n" "-120.10605 -590.00750 440.77010 -330.14493 649.46712 -49.97874\n" "-196.80865 -42.72741 -139.84908 107.68729 -49.97874 321.67659\n"); printf("\nCovariance matrix\n"); show_matrix(cov,DIM_I,DIM_I); //a[0]*= 1000; //mtx_lanczos_procedure(a,mtx_diag, mtx_off_diag,DIM_I,6); //show_matrix(Identity,DIM_I,DIM_I); //printf("\n Show the eigenvalues \n"); //mtx_mrrr(mtx_diag, mtx_off_diag,eigenvalues,6); //show_matrix(eigenvalues,1,6); //mtx_lanczos_procedure(a,mtx_diag, mtx_off_diag,DIM_I,5); //show_matrix(Identity,DIM_I,DIM_I); //printf("\n Show the eigenvalues \n"); //mtx_mrrr(mtx_diag, mtx_off_diag,eigenvalues,6); //show_matrix(eigenvalues,1,6); //mtx_lanczos_procedure(a,mtx_diag, mtx_off_diag,DIM_I,3); //show_matrix(Identity,DIM_I,DIM_I); //printf("\n Show the eigenvalues \n"); //mtx_mrrr(mtx_diag, mtx_off_diag,eigenvalues,3); //show_matrix(eigenvalues,1,3); // printf("\nElements of the diagonal\n"); //show_matrix(mtx_diag,1,DIM_J); // printf("\nElements off the diagonal\n"); //show_matrix(mtx_off_diag,1,DIM_J); //printf("\nIdentity matrix \n"); //mtx_ident(Identity,DIM_I); //mtx_mult(eigenvalues,Identity,ident_eigen,1,3,3); //printf("\n Show the multiplication of eigen values with the matrix identity \n"); //show_matrix(ident_eigen,3,3); //vect_sub(a,ident_eigen,(DIM_I*DIM_J)); //printf("\n Show the substraction of a with the matrix identity* eigenvalues \n"); //show_matrix(a,3,3); }
/** * void mtx_lanczos_procedure(double *A, double *Tm, int n, int m) * * @brief Function that implements the first step of the eigenproblem solution * based on lanzos algorithm. This function generates a matrix Tm that contains * a set (<=) of eigenvalues that approximate those of matrix A. Finding the eigenvalues * in Tm is easier and serves as an optimization method for problems in which only a few * eigenpairs are required. * @param A, the matrix on which the procedure is applied. Needs to be square and Hermitian. * @param (out)a, elements of the diagonal of the matrix Tm * @param (out)b, elements off diagonal of the matrix Tm * @param n, the dimensions of the square matrix A * @param m, the number of iterations for the lanczos procedure (and the dimensions of the array returned) */ void mtx_lanczos_procedure(double *A, double *a, double *b, int n, int m) { int i,j,array_index_i; double* v_i = (double*)calloc(n, sizeof(double)); double* a_times_v_i = (double*)calloc(n, sizeof(double)); double* b_times_v_i_minus_one = (double*)calloc(n, sizeof(double)); double* v_i_minus_one = (double*)calloc(n, sizeof(double)); double* w_i = (double*)calloc(n, sizeof(double)); double* v1 = (double*)calloc(n, sizeof(double)); double* temp; /*get a n-long random vector with norm equal to 1*/ //vect_rand_unit(v1,n); for(i=0;i<n;i++){ v1[i] = 1/sqrt(n); } /*first iteration of the procedure*/ i = 1; array_index_i = i-1; /*computes w_i*/ /*w[i] <= A*v[i]*/ mtx_mult(A, v1, w_i, n, n, 1); /*computes a_i*/ /*a[i] <= w[i]*v[i]*/ a[array_index_i] = vect_dot_product(w_i, v1,n); /*update w_i*/ /*w[i] <= w[i]-a[i]*v[i]-b[i]*v[i-1]*/ /*note: b[1] = 0*/ memcpy(a_times_v_i,v1,n*sizeof(double)); vect_scalar_multiply(a_times_v_i,a[array_index_i],n); vect_sub(w_i, a_times_v_i, n); /*computes next b_i*/ /*b[i+1] = ||w[i]||*/ b[array_index_i+1] = vect_norm(w_i,n); /*copy v1 to v_i*/ memcpy(v_i,v1,n*sizeof(double)); /*save v[i] to be used asv[i-1]*/ temp = v_i_minus_one; v_i_minus_one = v_i; /*computes next v_i = w_i/b(i+1)*/ /*v[i+1] = w[i]/b[i+1]*/ v_i = w_i; vect_scalar_multiply(v_i, 1/b[array_index_i+1], n); /*reuse memory former v_i_minus_one space for w_i*/ w_i = temp; /*rest of the iterations of the procedure*/ for(i=2;i<=m-1;i++){ array_index_i = i-1; /*computes w_i*/ /*w[i] <= A*v[i]*/ mtx_mult(A, v_i, w_i, n, n, 1); /*computes a_i*/ /*a[i] <= w[i]*v[i]*/ a[array_index_i] = vect_dot_product(w_i, v_i, n); /*update w_i*/ /*prepare a[i]*v[i]*/ memcpy(a_times_v_i,v_i,n*sizeof(double)); vect_scalar_multiply(a_times_v_i,a[array_index_i],n); /*prepare b[i]*v[i-1]*/ memcpy(b_times_v_i_minus_one,v_i_minus_one,n*sizeof(double)); vect_scalar_multiply(b_times_v_i_minus_one,b[array_index_i],n); /*w[i] <= w[i]-a[i]*v[i]-b[i]*v[i-1]*/ vect_sub(w_i, a_times_v_i,n); vect_sub(w_i, b_times_v_i_minus_one,n); /*computes next b_i*/ /*b[i+1] = ||w[i]||*/ b[array_index_i+1] = vect_norm(w_i,n); /*save current v_i*/ temp = v_i_minus_one; v_i_minus_one = v_i; /*computes next v_i = w_i/b(i+1)*/ /*v[i+1] = w[i]/b[i+1]*/ v_i = w_i; vect_scalar_multiply(v_i, 1/b[array_index_i+1],n); /*reuse memory former v_i_minus_one space for w_i*/ w_i = temp; } /*compute last term a[m]*/ array_index_i = m-1; mtx_mult(A, v_i, w_i, n, n, 1); a[array_index_i] = vect_dot_product(w_i,v_i,n); /*translate data representation in the b matrix to match the CLAPACK standard*/ for(i=0;i<(n-1);i++){ b[i] = b[i+1]; } b[n-1] = 0; free(v1); free(v_i); free(a_times_v_i); free(b_times_v_i_minus_one); free(v_i_minus_one); free(w_i); }