int main() { int m=6,n=6,k=6; int A[36] = { 5 , 3 , 4 , 5 , 1 , 4 , 5 , 5 , 3 , 1 , 2 , 2, 1 , 3 , 4 , 1 , 3 , 1, 4 , 5 , 4 , 3 , 5 , 3, 2 , 2 , 1 , 5 , 1 , 3, 3 , 4 , 1 , 4 , 5 , 1}; //B1 (3x4) int B[36] = { 3, 4, 3 , 3 , 3 , 4, 2 , 2, 1 , 1 , 1 , 1, 2 , 5, 5 , 3 , 3 , 1, 3 , 5, 5 , 2 , 3 ,2, 2 , 4 , 5 , 1 , 4 ,3, 2 , 2 , 2 , 1 , 4 , 4}; int C[36]; int Cg[36]; mul_matrix_golden(A,B,Cg,m,n,k); mul_matrix(A,B,C,m,n,k); //C[10]=0; for(int i=0;i<m*n;i++) { if(C[i]!=Cg[i]) { return 0; } } printf("Correct\n"); }
void render() { for(int j=0; j<updates.size(); j++) { updates[j]->apply(); } updates.clear(); GLfloat* scaleM = new GLfloat[16]; make_scale_matrix(1,-1,1,scaleM); //make_scale_matrix(1,1,1,scaleM); GLfloat* transM = new GLfloat[16]; make_trans_matrix(-width/2,height/2,0,transM); //make_trans_matrix(10,10,0,transM); //make_trans_matrix(0,0,0,transM); GLfloat* m4 = new GLfloat[16]; mul_matrix(m4, transM, scaleM); GLfloat* pixelM = new GLfloat[16]; // loadPixelPerfect(pixelM, width, height, 600, 100, -150); loadPixelPerfect(pixelM, width, height, eye, near, far); //printf("eye = %f\n",eye); //loadPerspectiveMatrix(pixelM, 45, 1, 10, -100); GLfloat* m5 = new GLfloat[16]; //transpose(m5,pixelM); mul_matrix(modelView,pixelM,m4); make_identity_matrix(globaltx); glViewport(0,0,width, height); glClearColor(1,1,1,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_DEPTH_TEST); for(int j=0; j<anims.size(); j++) { anims[j]->update(); } AminoNode* root = rects[rootHandle]; root->draw(); glfwSwapBuffers(); }
void esProj_perspective( esMat4f *mat, float fov, float screenratio, float near, float far, esVec3f eye, esVec3f at, esVec3f up) { float persp[16]; perspective_matrix(persp, fov, screenratio, near, far); float look[16]; lookat_matrix(look, eye, at, up); mul_matrix(mat->mat, look, persp); }
int main() { int arr[] = {1,2,3,4,5,6,7,8,9,10}; int arr2[2][2]; int arr3[2][2]; int *p = arr+1; int i, j; printf("%d %d %d \n", arr[0], arr[-5], arr[-21]); printf("%d %d %d \n", p[0], p[-1], p[-2]); printf("avg: %f\n\n", avg(arr, 10)); set_arr1(arr2, 2); printArr(arr2, 2); set_arr2(arr3, 2); printArr(arr3, 2); test_pointer_arr(); mul_matrix(arr2, arr3); return 0; }
int main(void) { srand(time(NULL)); TYPE **a = alloc_matrix(N); TYPE **b = alloc_matrix(N); TYPE **c = alloc_matrix(N); randomize_matrix(a, N); randomize_matrix(b, N); clock_t start = clock(); mul_matrix(c, a, b, N); double working_time = (double)(clock() - start) / CLOCKS_PER_SEC; printf("Working time: %g s\n", working_time); free_matrix(a, N); free_matrix(b, N); free_matrix(c, N); return 0; }
void lookat_matrix(float *mat, esVec3f eye, esVec3f at, esVec3f up) { esVec3f forw = { at.x - eye.x, at.y - eye.y, at.z - eye.z, }; normalize(&forw); esVec3f side = cross(up, forw); normalize(&side); up = cross(forw, side); float m0[16]; identity_matrix(m0); m0[ 0] = side.x; m0[ 4] = side.y; m0[ 8] = side.z; m0[ 1] = up.x; m0[ 5] = up.y; m0[ 9] = up.z; m0[ 2] = -forw.x; m0[ 6] = -forw.y; m0[10] = -forw.z; float m1[16]; identity_matrix(m1); m1[12] = -eye.x; m1[13] = -eye.y; m1[14] = -eye.z; mul_matrix(mat, m1, m0); }
void parse_world(state_t* state, FILE* f) { char line[4096]; char** tokens; int n_tokens; int bsdf_index = 0; transform_data_t t; t.has_users = false; t.index = 0; matrix_t m_identity = {{1.f,0.f,0.f,0.f}, {0.f,1.f,0.f,0.f}, {0.f,0.f,1.f,0.f}, {0.f,0.f,0.f,1.f}}; memcpy(t.t.m, m_identity, sizeof(matrix_t)); memcpy(t.t.m_inv, m_identity, sizeof(matrix_t)); state->transforms[t.index++] = t.t; state->n_transforms++; realloc_transforms(); transform_data_t tstack[1024]; int tstack_ptr = 0; while(fgets(line, 4096, f) != NULL) { if(line[0] != '#') { line[strlen(line)-1] = '\0'; tokens = tokenize(line, &n_tokens); if(n_tokens != 0) { if(strcmp(tokens[0],"EndWorld") == 0) { free(tokens); return; } else if(strcmp(tokens[0],"PushTransform") == 0) { qr_assert(n_tokens==1, "parser", "PushTransform takes only no arguments, found %d",n_tokens-1); tstack[++tstack_ptr] = t; } else if(strcmp(tokens[0],"PopTransform") == 0) { qr_assert(n_tokens==1, "parser", "PopTransform takes no arguments, found %d",n_tokens-1); transform_data_t tmp = t; t = tstack[tstack_ptr--]; t.has_users = t.has_users || tmp.has_users; t.index = state->n_transforms; } else if(strcmp(tokens[0],"LoadIdentity") == 0) { qr_assert(n_tokens==1, "parser", "LoadIdentity takes no arguments, found %d",n_tokens-1); if(t.has_users == true) { t.index = state->n_transforms; t.has_users = false; } } else if(strcmp(tokens[0],"Translate") == 0) { qr_assert(n_tokens==4, "parser", "Translate takes only 3 arguments, found %d",n_tokens-1); if(t.has_users == true) { t.index = state->n_transforms; t.has_users = false; } transform_t tmp; translate(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), &tmp); mul_matrix(&tmp.m, &t.t.m, &t.t.m); mul_matrix(&tmp.m_inv, &t.t.m_inv, &t.t.m_inv); } else if(strcmp(tokens[0],"Scale") == 0) { qr_assert(n_tokens==4, "parser", "Scale takes only 3 arguments, found %d",n_tokens-1); if(t.has_users == true) { t.index = state->n_transforms; t.has_users = false; } transform_t tmp; scale(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), &tmp); mul_matrix(&tmp.m, &t.t.m, &t.t.m); } else if(strcmp(tokens[0],"LookAt") == 0) { qr_assert(n_tokens==7, "parser", "LookAt takes only 6 arguments, found %d",n_tokens-1); if(t.has_users == true) { t.index = state->n_transforms; t.has_users = false; } lookat(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), make_vec3(to_float(tokens[4]),to_float(tokens[5]),to_float(tokens[6])), &t.t); } else if(strcmp(tokens[0],"BSDFDiffuse") == 0) { qr_assert(n_tokens==4, "parser", "BSDFDiffuse takes only 3 arguments, found %d",n_tokens-1); state->n_bsdfs++; realloc_bsdfs(); state->bsdfs[state->n_bsdfs-1] = make_bsdf_diffuse(make_vec3(to_float(tokens[1]), to_float(tokens[2]), to_float(tokens[3]))); bsdf_index = state->n_bsdfs-1; } else if(strcmp(tokens[0],"Sphere") == 0) { qr_assert(n_tokens==2, "parser", "Sphere takes only one argument, found %d",n_tokens-1); state->primitives = (primitive_t*)realloc(state->primitives, sizeof(primitive_t)*(++state->n_primitives)); primitive_t p; p.type = PRIMITIVE_SPHERE; p.data = make_primitive_sphere(to_float(tokens[1])); p.t = t.index; p.bsdf = bsdf_index; t.has_users = true; state->primitives[state->n_primitives-1] = p; state->transforms[t.index] = t.t; state->n_transforms++; realloc_transforms(); } else if(strcmp(tokens[0],"Mesh") == 0) { qr_assert(n_tokens==2, "parser", "Mesh takes only one argument, found %d",n_tokens-1); state->transforms[t.index] = t.t; t.has_users = true; state->n_transforms++; realloc_transforms(); load_mesh(tokens[1], state, t, bsdf_index, NULL); } else if(strcmp(tokens[0],"MeshMat") == 0) { qr_assert(n_tokens==3, "parser", "MeshMat takes only 2 arguments, found %d",n_tokens-1); t.has_users = true; state->transforms[t.index] = t.t; state->n_transforms++; realloc_transforms(); load_mesh(tokens[1], state, t, bsdf_index, tokens[2]); } else if(strcmp(tokens[0],"PointLight") == 0) { qr_assert(n_tokens==7, "parser", "PointLight takes only 6 arguments, found %d",n_tokens-1); realloc_lights(); state->lights[state->n_lights++] = make_light_point(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), make_vec3(to_float(tokens[4]),to_float(tokens[5]),to_float(tokens[6]))); } else if(strcmp(tokens[0],"SphereLight") == 0) { qr_assert(n_tokens==8, "parser", "SphereLight takes only 7 arguments, found %d",n_tokens-1); realloc_lights(); state->lights[state->n_lights++] = make_light_sphere(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), to_float(tokens[4]), make_vec3(to_float(tokens[5]),to_float(tokens[6]),to_float(tokens[7]))); } else if(strcmp(tokens[0],"Camera") == 0) { qr_assert(n_tokens==2, "parser", "Camera takes only one argument, found %d",n_tokens-1); state->camera_fplane = to_float(tokens[1]); state->camera_transform = t.index; t.has_users = true; state->camera_origin = transform_point(make_vec3(0.f,0.f,0.f), t.t, true); state->transforms[t.index] = t.t; state->n_transforms++; realloc_transforms(); } else { ERROR("parser", "Unknown directive: %s",tokens[0]); } } } } ERROR("parser", "File ended in the middle of a World section"); }
void BiCGComplex_VC::solve(dcomplex *&solution, dcomplex *rp, double gamma) { double eps = gamma; int max_iter = (int) 100 * sqrt(n); dcomplex dp1, dp2, alpha, beta; double rp_norm = sqrt(dot_prod(rp, rp).real()); x0 = new dcomplex [n]; mul_matrix(solution, t); for(int i = 0; i < n; i++) { x0[i] = solution[i]; r[i] = rp[i] - t[i]; p[i] = z[i] = s[i] = r[i]; } bool not_end = true; double discr; int iter; dp1 = dot_prod_nocj(p, r); for(iter = 0; iter < max_iter && not_end; iter++) { discr = sqrt(dot_prod(r,r).real()); if(iter%50 == 0) printf("BiCGVC Residual:\t%5d\t%.3e\t%.3e\r", iter, discr / rp_norm, gamma); if(discr / rp_norm > eps) { mul_matrix(z,t); alpha = dp1 / dot_prod_nocj(s, t); mul_matrix(s, t1); for(int i = 0; i < n; i++) { x0[i] += alpha * z[i]; r[i] -= alpha * t[i]; p[i] -= alpha * t1[i]; } dp2 = dot_prod_nocj(p, r); beta = dp2 / dp1; dp1 = dp2; for(int i = 0; i < n; i++) { z[i] = r[i] + beta * z[i]; s[i] = p[i] + beta * s[i]; } } else not_end = false; } printf("BiCGVC Residual:\t%5d\t%.3e\t%.3e\n", iter, discr / rp_norm, gamma); for(int i = 0; i < n; i++) solution[i] = x0[i]; delete[] x0; }