Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    trovare un cofattore di una matrice 3x3

    sto studiando java, e sto implementando dei metodi per usare le matrici.
    questi sono i metodi che ho fatto fin'ora

    codice:
    import java.lang.Math;
    
    public class metodiMatrici3x3
    {
    
    //STAMPO UNA MATRICE
    	public static void stampaMatrici(int[][] m){
    		for (int i=0; i<3; i++){
    			for (int j=0; j<3; j++){
    				System.out.print(m[i][j]+ "  ");
    			}
    			System.out.print("\n");
    		}
    	}		
    
    
    //STAMPO UNA MATRICE 2X2
    	public static void stampaMatrici2x2(int[][] m){
    		for (int i=0; i<2; i++){
    			for (int j=0; j<2; j++){
    				System.out.print(m[i][j]+ "  ");
    			}
    			System.out.print("\n");
    		}
    	}		
    		
    //CALCOLO LA TRASPOSTA DI UNA MATRICE
    	static int[][] n = new int[3][3];
    	public static int[][] traspostaMatrice(int[][] m){
    		for (int i=0; i<3; i++){
    			for (int j=0; j<3; j++){
    				n[i][j]=m[i][j];
    			}
    		}
    		for (int i=0; i<3; i++){
    			for (int j=0; j<3; j++){
    				m[i][j]=n[j][i];
    			}
    		}
    		return m;
    	}
    	
    //CALCOLO IL PRODOTTO DI DUE MATRICI
    	public static int[][] prodottoMatrici(int[][]m, int[][]o){
    		int c[][] = new int[3][3];
    		for(int i=0; i<3; i++)
    		{
    			for(int j=0; j<3; j++)
    			{
    				int elementoProdotto=0;
    				for (int x=0; x<3; x++)
    					elementoProdotto += m[i][x]*o[x][j];
    				c[i][j]=elementoProdotto;
    			}
    		}
    		return c;
    	}
    
    //CALCOLO IL DETERMINANTE DI UNA MATRICE
    	public static int determinante(int[][] m){
    		int det=0;
    		int tmp;
    		for(int i=0;i<3;i++){
    			tmp=1;
    			for(int j=0;j<3;j++){
    				tmp*=m[(j)][((i+j)%3)];
    				}
                det+=tmp;
                }
                for(int i=0;i<3;i++){
                	tmp=1;
                	for(int j=0;j<3;j++){
                		tmp*=m[j][( ((4-i)-j) %3 )];
                       	}
                    det-=tmp;
                    }
    		return det;
            }	
    
    //MATRICE IDENTITA 3X3
    	public static int[][] matriceIdentita(){
    		int[][] I = new int [3][3];
    			for (int i=0; i<3; i++){
    				for (int j=0; j<3; j++){
    					I[i][j]=(i==j? 1 : 0);
    				}
    			}
    			return I;
    		}
    
    //SOMMA DI MATRICI 3x3
    	public static int[][] sommaMatrici(int[][]m, int[][]n){
    		int[][] s=new int [3][3];
    		for (int i=0; i<3; i++){
    			for (int j=0; j<3; j++){
    				s[i][j]=m[i][j]+n[i][j];
    			}
    		}
    		return s;
    	}
    	
    //MINORE COMPLEMENTARE DI MATRICI 3x3
    	public static int[][] minoreComplementare(int[][]m, int r, int c){
    		int[][] s= new int [2][2];
    		int tmp;
    		//qui andrebbe il codice
    		return s;
    		}		
    	
    }
    non riesco a scrivere un codice per calcolare il minore complementare di una matrice 3x3,
    e poi vorrei sapere come posso generalizzare questi metodi per matrici anche superiori a 3x3.
    grazie in anticipo

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    C'è un progetto molto bello da cui puoi trarre spunto:

    http://math.nist.gov/javanumerics/jama/
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    mmm veramente non mi è d'aiuto dato che per esercizio devo fare questo metodo usando solo i cicli base di java.

    me lo sapreste scrivere voi per favore?

    deve essere un metodo che restituisce una matrice 2x2, che riceve come argomenti una matrice 3x3 e due interi che rappresentano il numero di riga e il numero della colonna che si vuole eliminare.

    ho provato in mille modi ma non riesce.
    grazie

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Ti posto un po' del codice a cui avevo cominciato a lavorare a tempo perso

    codice:
    package math.matrix;
    
    import math.matrix.exceptions.*;
    /**
     *
     * @author Andrea
     */
    public class Matrix {
        
        private double[][] matrix;
        
        /** Creates a new instance of Matrix */
        public Matrix(double[][] matrix) {
            this.matrix = matrix;
        }
        
        public Matrix(Matrix m) {
            this.matrix = m.matrix;
        }
        
        public Matrix (int dim) {
            matrix = new double[dim][dim];
            for (int i=0; i < dim; i++) {
                for (int j = 0; j < dim; j++) {
                    matrix[i][j] = 0;
                }
            }
        }
        
        public Matrix (int rows, int cols, int elem) {
            matrix = new double[rows][cols];
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    matrix[i][j] = elem;
                }
            }
        }
        
        public Matrix suppressRow(int row) throws MatrixRowCountException {
            if (row >= matrix.length) {
                throw new MatrixRowCountException("Row index out of range");
            }
            else {
                double[][] m = new double[matrix.length -1][matrix[0].length];
                int k = 0;
                for (int i = 0; i < m.length && k < matrix.length; i++) {
                    if (row != k) {
                        for (int j = 0; j < matrix[0].length; j++) {
                            m[i][j] = matrix[k][j];                        
                        }
                        k++;
                    }
                    else {
                        k++;
                        i--;
                    }
                }
                return new Matrix(m);
            }
        }
        
        public Matrix suppressColumn(int col) throws MatrixColumnCountException {
            if (col >= matrix[0].length) {
                throw new MatrixColumnCountException("Row index out of range");
            }
            else {
                double[][] m = new double[matrix.length][matrix[0].length - 1];
                int k = 0;
                for (int j = 0; j < m[0].length && k < matrix[0].length; j++) {
                    if (col != k) {
                        for (int i = 0; i < matrix.length; i++) {
                            m[i][j] = matrix[i][k];
                        }
                        k++;
                    }
                    else {
                        k++;
                        j--;
                    }
                }
                return new Matrix(m);
            }
        }
        
        public Matrix computeMinor (int row, int col) throws MatrixRowCountException, MatrixColumnCountException {
            return this.suppressColumn(col).suppressRow(row);
        }
        
        public String toString() {
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    buf.append(matrix[i][j]+"\t");
                }
                buf.append("\n");
            }
            return buf.toString();
        }
        
        public static void main (String[] args) {
            Matrix myMatrix = new Matrix(new double[][] {
                {1,2,3,5,6},
                {4,5,6,7,8},
                {7,8,9,10,11},
                {10,11,12,13,14},
                {13,14,15,16,17,18}
            });
            System.out.println(myMatrix);        
            try {
                Matrix myMatrix3 = myMatrix.computeMinor(1,2);
                System.out.println("\n\n\nNew Matrix\n"+myMatrix3);
            }
            catch (Exception e) {
                e.printStackTrace();
            }        
        }
        
    }
    Lascia stare le exception personalizzate e l'eventuale impacchettamento e imports... la sostanza sta nei metodi per sopprimere riga e colonna. L'altro vien da sè.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    io seguendo il tuo consiglio ho scritto così per eliminare una riga:
    codice:
     package math.matrix;
    import java.lang.Math;
    
    
    public class metodiMatrici3x3
    {
    
    //STAMPO UNA MATRICE 2x3
    	public static void stampaMatrici(int[][] m){
    		for (int i=0; i<2; i++){
    			for (int j=0; j<3; j++){
    				System.out.print(m[i][j]+ "  ");
    			}
    			System.out.print("\n");
    		}
    	}		
     	
    //ELIMINA RIGA
    	public static int[][] eliminaRiga(int[][]m, int riga){
    		int[][]n = new int[2][3];
    		int k=0;
    		for (int i = 0; i<3 && k<3; i++){
    			if (riga != k){
    				for (int j = 0; j <3; j++){
    					n[i][j] = m[i][j];
    				}
    				k++;
    			}
    			else{
    				k++;
    				i--;
    			}	
    		} 
    		return n;
    	}
    }
    poi ho un altro file nella stessa cartella che ha una classe matrici(extends metodiMatrici3x3) che contiene il main e che dichiara una matrice m alla quale do valori,
    dopodiche faccio
    stampaMatrici(eliminaRiga(matrice, 0));
    ero sicuro che funzionasse...
    e invece mi fa l'arrayindexoutofbounds: 5 alla riga dove dico che n[i][j] = m[i][j]; .
    noooooooo
    che ho sbagliato?

  6. #6
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    nel mio codice, copio le righe/colonne in funzione di k. Se poi vuoi generalizzare a dimensione NxM, ti conviene scrivere fin da subito utilizzando .length altrimenti ti ritroverai a fare doppio lavoro.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  7. #7
    si è vero! avevo sbagliato a scrivere gli indici
    grazie 1000 dell'aiuto!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.