Advertisement
Krizalider

Untitled

Jul 5th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. escola
  2. import java.util.Random;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. public class Matriz {
  6. private int linhas;
  7. private int colunas;
  8. private int[][] mat;
  9.  
  10.  
  11. Matriz(int novaLinhas, int novaColunas){
  12. this.setLinhas(novaLinhas);
  13. this.setColunas(novaColunas);
  14. this.mat= new int[this.getLinhas()][this.getColunas()];
  15. }
  16. public void setLinhas(int novaLinhas){
  17. this.linhas=novaLinhas;
  18.  
  19. }
  20. public int getLinhas(){
  21. return this.linhas;
  22. }
  23. public void setColunas(int novaColunas){
  24. this.colunas=novaColunas;
  25. }
  26. public int getColunas(){
  27. return this.colunas;
  28. }
  29. public void setElemento(int i, int j, int novoElemento){
  30. this.mat[i][j] = novoElemento;
  31. }
  32. public int getElemento(int i, int j){
  33. return this.mat[i][j];
  34. }
  35. public void inicializador(int novoElemento){
  36.  
  37. for(int i = 0; i < this.getLinhas(); i++){
  38. for(int j = 0; j < this.getColunas(); j++){
  39. this.setElemento(i, j, novoElemento);
  40. }
  41. }
  42. }
  43.  
  44.  
  45. public void inicializaRandomico(){
  46. Random rand = new Random();
  47.  
  48. for(int i=0; i<getLinhas(); i++){
  49. for(int j=0; j<getColunas(); j++){
  50. this.setElemento(i,j,rand.nextInt(getLinhas()*getColunas() ));
  51.  
  52.  
  53. }
  54. }
  55. }
  56.  
  57. public void imprimidorMatriz(){
  58. if(this.retorneOrdem()!=-1){
  59. for(int i = 0; i < this.getLinhas(); i++){
  60. System.out.printf("\n|");
  61. for(int j = 0; j < this.getColunas(); j++){
  62. System.out.printf(" %d |",this.getElemento(i,j));
  63. }
  64. }
  65. }
  66. if(this.retorneOrdem()==-1){
  67. System.out.println("Nao foi possivel executar o programa, pois a matriz nao eh quadrada.");
  68. }
  69. }
  70.  
  71. public int retorneOrdem(){
  72. int numL, numC, ordem,aux;
  73.  
  74. numL = this.getLinhas();
  75. numC = this.getColunas();
  76. ordem = -1;
  77. if(numL == numC){
  78. ordem = numL;
  79.  
  80. }
  81.  
  82.  
  83. return ordem;
  84. }
  85.  
  86. public int detOrdem1(Matriz matN, int i, int j){
  87. return matN.getElemento(i,j);
  88. }
  89.  
  90. public int detOrdem2(Matriz matN, int i, int j){
  91. int diagonalP, diagonalI,detResposta;
  92.  
  93. diagonalP = matN.detOrdem1(matN,i,j) * matN.detOrdem1(matN,i+1,j+1);
  94. diagonalI = matN.detOrdem1(matN,i,j+1) * matN.detOrdem1(matN,i+1,j);
  95. detResposta=diagonalP - diagonalI;
  96. return (detResposta);
  97. }
  98.  
  99. public int determinante(){
  100. int aux = 0;
  101.  
  102. if(this.retorneOrdem()!=1){
  103. aux = this.detRecursivo(this, this.getLinhas());
  104. }
  105. return aux;
  106. }
  107.  
  108. public int determinanteComSwitch(Matriz mat, int ordem){
  109. int armazenador = 0;
  110. switch(ordem){
  111. case 1:
  112. armazenador = mat.detOrdem1(mat,0,0);
  113. break;
  114. case 2:
  115. armazenador = mat.detOrdem2(mat,0,0);
  116. break;
  117. default:
  118. armazenador = mat.detLaplace(mat,ordem);
  119. break;
  120. }
  121. return armazenador;
  122. }
  123. public void copiaMatrizOK(Matriz mat1, Matriz mat2, int isqn, int jsqn){
  124. int i_alvo = 0, j_alvo, i_original, j_original, aux;
  125.  
  126. for (i_original = 0; i_original < mat1.getLinhas(); i_original++){
  127. if (i_original != isqn){
  128. j_alvo = 0;
  129. for(j_original = 0; j_original < mat1.getLinhas(); j_original++){
  130. if (j_original != jsqn){
  131. aux = mat1.getElemento(i_original,j_original);
  132. mat2.setElemento(i_alvo,j_alvo,aux);
  133. j_alvo++;
  134. }
  135. }
  136. i_alvo++;
  137. }
  138. }
  139. }
  140.  
  141. public void copiaMatriz(Matriz mat, Matriz mat2, int isqn, int jsqn){
  142. Matriz matrizOriginal, matrizAlvo;
  143. int ordem_Max, ordem_Min;
  144. if(((mat!=null) && (mat2 != null)) && (mat.retorneOrdem()!=1 && mat2.retorneOrdem()!=1)){
  145. ordem_Max = mat.getLinhas();
  146. ordem_Min = mat2.getLinhas();
  147. matrizOriginal = mat;
  148. matrizAlvo = mat2;
  149. if(ordem_Max < ordem_Min){
  150. ordem_Max = mat2.getLinhas();
  151. ordem_Min = mat.getLinhas();
  152. matrizOriginal = mat2;
  153. matrizAlvo = mat;
  154. }
  155. if(((isqn < ordem_Max) && (jsqn < ordem_Max)) && ((ordem_Max - ordem_Min) == 1)){
  156. this.copiaMatrizOK(matrizOriginal,matrizAlvo,isqn,jsqn);
  157. }
  158. }
  159. }
  160.  
  161.  
  162. public int detRecursivo(Matriz mat, int ordem){
  163. int determinante = 0;
  164. switch(ordem){
  165. case 1:
  166. determinante = mat.detOrdem1(mat,0,0);
  167. break;
  168. case 2:
  169. determinante = mat.detOrdem2(mat,0,0);
  170. break;
  171. default:
  172. determinante = mat.detLaplace(mat,ordem);
  173. break;
  174. }
  175. return determinante;
  176. }
  177.  
  178.  
  179. public int detOtimizado(){
  180. int aux = 0;
  181.  
  182. if(this.retorneOrdem()!=1){
  183. aux = this.detRecursivoOtimizado(this, this.getLinhas());
  184.  
  185. }
  186. return aux;
  187. }
  188. public int detRecursivoOtimizado(Matriz mat, int ordem){
  189. int detResposta = 0;
  190. switch(ordem){
  191. case 1:
  192. detResposta = mat.detOrdem1(mat,0,0);
  193. break;
  194. case 2:
  195. detResposta = mat.detOrdem2(mat,0,0);
  196. break;
  197. default:
  198. detResposta = mat.detLaplaceOtimizado(mat,ordem);
  199. break;
  200. }
  201. return detResposta;
  202.  
  203. }
  204.  
  205.  
  206. public int detLaplace(Matriz mat, int ordem){
  207. Matriz matAux = new Matriz(ordem-1,ordem-1);
  208. int somaDet = 0;
  209.  
  210. for(int j = 0; j < ordem; j++){
  211. mat.copiaMatriz(mat,matAux,0,j);
  212. somaDet = somaDet + (Cofator(0,j) * mat.getElemento(0,j) * mat.determinanteComSwitch(matAux,ordem-1));
  213. }
  214. return somaDet;
  215. }
  216. public int Cofator(int indice, int indice2){
  217.  
  218. int resposta = ((indice+indice2)%2 == 0) ? 1 : -1;
  219.  
  220. return resposta;
  221. }
  222.  
  223.  
  224. public int colunaFixa_detLaplaceOtimizado(Matriz mat, int ordem, int indice){
  225. Matriz matAux = new Matriz(ordem-1,ordem-1);
  226. int somaDet = 0;
  227.  
  228. for(int i = 0; i < ordem; i++){
  229. this.copiaMatriz(mat,matAux,i, indice);
  230. if(mat.getElemento(i,indice) == 0){
  231. somaDet += 0;
  232. }else{
  233. somaDet += (Cofator(i,indice) * mat.getElemento(i,indice) * mat.detRecursivoOtimizado(matAux,ordem-1));
  234. }
  235. }
  236.  
  237. return somaDet;
  238. }
  239.  
  240.  
  241. public int linhaFixa_detLaplaceOtimizado(Matriz mat, int ordem, int indice){
  242. Matriz matAux = new Matriz(ordem-1,ordem-1);
  243. int somaDet = 0;
  244.  
  245. for(int j = 0; j < ordem; j++){
  246. this.copiaMatriz(mat,matAux,indice,j);
  247. if(mat.getElemento(indice,j) == 0){
  248. somaDet += 0;
  249. }else{
  250. somaDet += (Cofator(indice,j) * mat.getElemento(indice,j) * mat.detRecursivoOtimizado(matAux,ordem-1));
  251. }
  252.  
  253. }
  254.  
  255. return somaDet;
  256. }
  257.  
  258. public int detLaplaceOtimizado(Matriz mat, int ordem){
  259. int indice = 0;
  260. int resposta = 0;
  261.  
  262. if((mat.zerosNaLinha() != 0) || (mat.zerosNaColuna() != 0)){
  263. if(mat.zerosNaLinha() >= mat.zerosNaColuna()){
  264. indice = mat.indiceLinha_MaisZeros();
  265. resposta = mat.linhaFixa_detLaplaceOtimizado(mat,ordem,indice);
  266.  
  267. }else if(mat.zerosNaColuna() > mat.zerosNaLinha()){
  268. indice = mat.ColunaComMaisZeros();
  269. resposta = mat.colunaFixa_detLaplaceOtimizado(mat,ordem,indice);
  270.  
  271. }
  272. }else if((mat.zerosNaLinha() == 0) && (mat.zerosNaColuna() == 0)){
  273. resposta = mat.detLaplace(mat,ordem);
  274. }
  275. return resposta;
  276. }
  277. public int zerosNaLinha(){
  278. int contZ, valor, contMaior = 0;
  279.  
  280. for(int i = 0; i < this.getLinhas(); i++){
  281. contZ = 0;
  282. for(int j = 0; j < this.getColunas(); j++){
  283. valor = this.getElemento(i,j);
  284. if(valor == 0){
  285. contZ++;
  286. }
  287. }
  288. if(contMaior < contZ){
  289. contMaior = contZ;
  290. }
  291. }
  292. return contMaior;
  293. }
  294.  
  295. public int zerosNaColuna(){
  296. int contZ, valor, contMaior = 0;
  297.  
  298. for(int j = 0; j < this.getColunas(); j++){
  299. contZ = 0;
  300. for(int i = 0; i < this.getLinhas(); i++){
  301. valor = this.getElemento(i,j);
  302. if(valor == 0){
  303. contZ++;
  304. }
  305. }
  306. if(contMaior < contZ){
  307. contMaior = contZ;
  308. }
  309. }
  310. return contMaior;
  311. }
  312. public int indiceLinha_MaisZeros(){
  313. int contZ, valor, contMaior = 0, indice = 0;
  314.  
  315. for(int i = 0; i < this.getLinhas(); i++){
  316. contZ = 0;
  317. for(int j = 0; j < this.getColunas(); j++){
  318. valor = this.getElemento(i,j);
  319. if(valor == 0){
  320. contZ++;
  321. }
  322. }
  323. if(contMaior < contZ){
  324. contMaior = contZ;
  325. indice = i;
  326. }
  327. }
  328. return indice;
  329. }
  330.  
  331.  
  332. public int ColunaComMaisZeros(){
  333. int colunaZ, valor, aux, indice = 0;
  334. aux = 0;
  335. for(int j = 0; j < this.getColunas(); j++){
  336. colunaZ = 0;
  337. for(int i = 0; i < this.getLinhas(); i++){
  338. valor = this.getElemento(i,j);
  339. if(valor == 0){
  340. colunaZ++;
  341. }
  342. }
  343. if(aux < colunaZ){
  344. aux = colunaZ;
  345. indice = j;
  346. }
  347. }
  348. return indice;
  349. }
  350.  
  351.  
  352. }
  353. public class mainMatriz{
  354. public static void main(String[] args){
  355. Matriz mat = new Matriz(4,4); //ordem da matriz
  356.  
  357. mat.inicializaRandomico();
  358. mat.imprimidorMatriz();
  359.  
  360. long startTime = System.nanoTime();
  361. mat.determinante();
  362. long endTime = System.nanoTime();
  363. long TempoNaoOtimizado = endTime - startTime;
  364. double totalMilisegundosNaoOtimizado=TempoNaoOtimizado/1000000.0;
  365. long startTime2 = System.nanoTime();
  366. System.out.println();
  367. System.out.printf("Determinante = %d\n",+mat.detOtimizado());
  368.  
  369. long endTime2 = System.nanoTime();
  370. long TempoOtimizado = endTime2 - startTime2;
  371. double totalMilisegundosOtimizado=TempoOtimizado/1000000.0;
  372. System.out.println();
  373. System.out.printf("Tempo nao otimizado = %d nanossegundos\n",TempoNaoOtimizado);
  374. System.out.printf("Tempo nao otimizado = %f milissegundos\n",totalMilisegundosNaoOtimizado);
  375. System.out.println();
  376. System.out.printf("Tempo com otimizacao= %d nanossegundos\n",TempoOtimizado);
  377. System.out.printf("Tempo com otimizado = %f milissegundos\n",totalMilisegundosOtimizado);
  378.  
  379. }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement