int main(int argc, char *argv[]){ char stringmagica[3],c; int height,width,rgbint,i,j,cont=0; Imagem imagem; FILE* entrada; entrada = fopen(argv[1],"r"); //entrada = fopen("entrada.ppm","r"); lerimagem (entrada,&imagem,stringmagica,&height,&width,&rgbint); fclose(entrada); //Aqui começa o programa mesmo int qtd_retirar=atoi(argv[4]); if (strcmp(argv[2],"D")==0){ if(strcmp(argv[3],"-h")==0){ imagem=transposta(imagem); ProgramacaoDinamica(&imagem,qtd_retirar); imagem=transposta(imagem); } else if (strcmp(argv[3],"-w")==0){ ProgramacaoDinamica(&imagem,qtd_retirar); } imprimirarquivo(imagem,stringmagica,rgbint); } else if(strcmp(argv[2],"G")==0){ if(strcmp(argv[3],"-h")==0){ imagem=transposta(imagem); SolucaoGrafos(&imagem,qtd_retirar); imagem=transposta(imagem); } else if (strcmp(argv[3],"-w")==0){ double inicio, final; // função para calcular o tempo de execução. Termina no final do código struct timeval tv; gettimeofday(&tv,NULL); inicio= tv.tv_sec + (tv.tv_usec/1000000.0); SolucaoGrafos(&imagem,qtd_retirar); struct rusage ru; struct timeval tim; getrusage(RUSAGE_SELF, &ru); tim=ru.ru_utime; double sutime = (double)tim.tv_sec + (double)tim.tv_usec / 1000000.0; // exibe na tela informações sobre o tempo de execução do programa printf(" - TEMPO REAL - \n"); printf(" _______________________________________________\n"); printf("| Tempo de Usuario: %.3f segundos |\n", sutime); //fprintf(arq_out,"Tempo Usuario: %.3f\t",sutime); tim=ru.ru_stime; double sstime = (double)tim.tv_sec + (double)tim.tv_usec / 1000000.0; printf("| Tempo de Sistema: %.3f segundos |\n", sstime); //fprintf(arq_out,"Tempo Sistema: %.3f\t",sstime); printf("| Tempo Total de Execucao: %.3f segundos |\n", sutime+sstime); //fprintf(arq_out,"Tempo Execucao: %.3f\t",sutime+sstime); gettimeofday(&tv, NULL); // final do marcador de tempo real final = tv.tv_sec + (tv.tv_usec)/1000000.0; // a diferença entre os marcador de tempo real final e inicial // é o tempo real de execução printf("| Tempo Real de Execucao: %.3f segundos |\n", final-inicio); }
void Inversa::potenciaRegular(matriz *m, matriz *v, double tolerance){ double lambda=0; double lambdaK; double aux; int h=0; matriz *vk; vk=(matriz*)malloc(sizeof(matriz)); vk->rows=v->rows; vk->columns=v->columns; vk->data=aloca(vk->rows,vk->columns); do{ printf("loop: k = %d.\n",h+1); igualaMatrizes(vk,normaEuclidiana(v)); // normalizando v, e vk <- v normalizado "psi"k igualaMatrizes(v,times(m,vk)); // v+1 printf("autovetor:\n"); showMatriz(v); printf("autovetor normalizado:\n"); showMatriz(normaEuclidiana(v)); lambdaK = times(transposta(vk),v)->data[0][0]; // retorna uma matriz 1x1, ou um escalar printf("autovalor:\n"); printf("%f\n",lambdaK); aux=fabs(lambdaK-lambda)/fabs(lambdaK); printf("tolerancia: %f\n",aux); lambda = lambdaK; h++; printf("==========================\n"); if(h==20) stopProgram(); }while(aux>tolerance); }
/* Aplica transformações geométrica à localização do vértice corrente e ao vetor normal associado a ele. * * Argumentos de entrada: * 'vertex_oc' : Localização do vértice representada no sistema de coordenadas do objeto (object coordinates, OC). * 'normal_oc' : Vetor normal à superfície representada no sistema de coordenadas do objeto (OC). * 'modelview_matrix' : Matriz 4x4 composta pelas transformações de modelo e visualização (view_matrix * model_matrix). * 'projection_matrix': Matriz 4x4 de projeção. * * Argumentos de saída: * 'vertex_ec' : Localização do vértice representada no sistema de coordenadas da câmera (eye coordinates, EC). * 'vertex_cc' : Localização do vértice representada no sistema coordenadas de recorte (clip coordinates, CC). * 'unit_normal_ec' : Vetor normal à superfície representado no sistema de coordenadas da câmera (EC). Assume-se que * a norma deste vetor é igual a 1.0f. */ void vertex_transformation(const location_struct &vertex_oc, const direction_struct &normal_oc, const matrix_struct &modelview_matrix, const matrix_struct &projection_matrix, location_struct &vertex_ec, location_struct &vertex_cc, direction_struct &unit_normal_ec) { //Calcular 'vertex_ec'. //Calcular 'vertex_cc'. //Calcular 'unit_normal_ec'. vertex_ec[0] = vertex_oc[0] * modelview_matrix(0,0) + vertex_oc[1] * modelview_matrix(0,1) + vertex_oc[2] * modelview_matrix(0,2) + vertex_oc[3] * modelview_matrix(0,3); vertex_ec[1] = vertex_oc[0] * modelview_matrix(1,0) + vertex_oc[1] * modelview_matrix(1,1) + vertex_oc[2] * modelview_matrix(1,2) + vertex_oc[3] * modelview_matrix(1,3); vertex_ec[2] = vertex_oc[0] * modelview_matrix(2,0) + vertex_oc[1] * modelview_matrix(2,1) + vertex_oc[2] * modelview_matrix(2,2) + vertex_oc[3] * modelview_matrix(2,3); vertex_ec[3] = vertex_oc[0] * modelview_matrix(3,0) + vertex_oc[1] * modelview_matrix(3,1) + vertex_oc[2] * modelview_matrix(3,2) + vertex_oc[3] * modelview_matrix(3,3); vertex_cc[0] = vertex_ec[0] * projection_matrix(0,0) + vertex_ec[1] * projection_matrix(0,1) + vertex_ec[2] * projection_matrix(0,2) + vertex_ec[3] * projection_matrix(0,3); vertex_cc[1] = vertex_ec[0] * projection_matrix(1,0) + vertex_ec[1] * projection_matrix(1,1) + vertex_ec[2] * projection_matrix(1,2) + vertex_ec[3] * projection_matrix(1,3); vertex_cc[2] = vertex_ec[0] * projection_matrix(2,0) + vertex_ec[1] * projection_matrix(2,1) + vertex_ec[2] * projection_matrix(2,2) + vertex_ec[3] * projection_matrix(2,3); vertex_cc[3] = vertex_ec[0] * projection_matrix(3,0) + vertex_ec[1] * projection_matrix(3,1) + vertex_ec[2] * projection_matrix(3,2) + vertex_ec[3] * projection_matrix(3,3); matrix_struct transposta = matrix_struct(); matrix_struct inverse = matrix_struct(); inverse_matrix(modelview_matrix, inverse); transpose_matrix(inverse, transposta); unit_normal_ec[0] = normal_oc[0] * transposta(0,0) + normal_oc[1] * transposta(0,1) + normal_oc[2] * transposta(0,2); unit_normal_ec[1] = normal_oc[0] * transposta(1,0) + normal_oc[1] * transposta(1,1) + normal_oc[2] * transposta(1,2); unit_normal_ec[2] = normal_oc[0] * transposta(2,0) + normal_oc[1] * transposta(2,1) + normal_oc[2] * transposta(2,2); normalize(unit_normal_ec); }
/* * Multiplicação de vetor por outro vetor que resulta em escalar * v1 >> primeiro vetor * v2 >> segundo vetor * Retorna >> O escalar */ double multiplicacao_vetor_vetor(Matriz* v1, Matriz* v2) { Matriz v2T; Matriz v3; cria_linhas(v2->numero_de_linhas, &v2T); cria_linhas(v2->numero_de_linhas, &v3); transposta(v2, &v2T); multiplicacao_matriz_vetor(v1, &v2T, &v3); if (v3.i[0].numero_de_colunas != 0) return v3.i[0].j[0].valor; else return 0; }
void ConjugateGradient(int n, Matriz* A, Matriz* b, Matriz* x) { int k; double alfa, beta, alfaNum, alfaDen, betaNum; Matriz d; Matriz d1; // proximo vetor d Matriz r; Matriz r1; // proximo vetor r Matriz x1; // proximo vetor x Matriz w; // vetor que recebe Ax Matriz w1; // vetor multiplica Adk Matriz temp; // vetor temporario Matriz temp2; // vetor temporario2 int iteracoes = 0; cria_linhas(n, &d); //Criando vetor d cria_linhas(n, &d1); //Criando vetor d1 cria_linhas(n, &r); //Criando vetor r cria_linhas(n, &r1); //Criando vetor r1 cria_linhas(n, &x1); //Criando vetor x1 cria_linhas(n, &w); //Criando vetor w cria_linhas(n, &w1); //Criando vetor w1 cria_linhas(n, &temp); //Criando vetor temp cria_linhas(n, &temp2); //Criando vetor temp multiplicacao_matriz_vetor(A, x, &w); //multipica Ax, salva em w print(&w); printf("-------------w \n"); subtrai_vetores(b, &w, &temp2); //diminui b-w --> b - Ax , salva em w print(&temp2); printf("-------------temp2 \n"); copia(&temp2, &w); printf("-------------w \n"); copia(&w, &r); // copia r = w print(&r); printf("-------------r \n"); copia(&r, &d); // copia d = r print(&d); printf("-------------d \n"); for (k = 0; k < n; k++, iteracoes++) { if (r.i[0].numero_de_colunas == 0) break; alfaNum = multiplicacao_vetor_vetor(&r, &r); // rT*r ---> numerador de alfa printf("%f \n", alfaNum); printf("-------------AlfaNum \n"); transposta(&d, &temp2); multiplicacao_matriz_vetor(A, &temp2, &w1); // Ad ---> matriz A * dk, denominador de alfa e salva no vetor w1 transposta(&w1, &temp2); copia(&temp2, &w1); filtro(&w1); print(&w1); printf("------------w1 \n"); alfaDen = multiplicacao_vetor_vetor(&d, &w1); // dkT * Adk --> multiplicao no denominador de alfa printf("%f \n", alfaDen); printf("-------------alfaDen \n"); alfa = alfaNum / alfaDen; // calculo do alfa printf("%f \n", alfa); printf("-------------alfa \n"); // inicio do calculo de Xk+1 multiplicacao_escalar_vetor(alfa, &d, &temp); //alfa * d --> multiplica alfa pelo vetor d print(&temp); printf("------------temp \n"); adicao_vetores(x, &temp, &x1); // Xk + alfaDk ---> soma de vetores print(&x1); printf("-----------x1 \n"); multiplicacao_escalar_vetor(alfa, &w1, &temp); // alfa * Adk print(&temp); printf("-----------temp \n"); subtrai_vetores(x, &temp, &r1); // Rk+1 = Rk - alfa*Adk print(&r1); printf("-----------r1 \n"); betaNum = multiplicacao_vetor_vetor(&r1, &r1); // Rk+1(transposto) * Rk+1 printf("%f \n", betaNum); printf("-----------betaNum \n"); beta = betaNum / alfaNum; // denominador de Beta eh o mesmo numerador de alfa printf("%f \n", beta); printf("-----------beta \n"); multiplicacao_escalar_vetor(beta, &d, &temp); // beta * dk print(&temp); printf("-----------temp \n"); adicao_vetores(&r1, &temp, &d1); // Rk+1 + beta * dk print(&d1); printf("-----------d1 \n"); copia(&r1, &r); copia(&x1, x); copia(&d1, &d); } }