Matrix Division

 

                                                           MATRIX DIVISION USING RMI

1.Remote interface – MatrixService

import java.rmi.Remote;

import java.rmi.RemoteException;


public interface MatrixService extends Remote {

    double[][] divideMatrices(double[][] A, double[][] B) throws RemoteException;

}


2.Implementation – MatrixServiceImpl

import java.rmi.server.UnicastRemoteObject;

import java.rmi.RemoteException;


public class MatrixServiceImpl extends UnicastRemoteObject implements MatrixService {


    protected MatrixServiceImpl() throws RemoteException {

        super();

    }


    public double[][] divideMatrices(double[][] A, double[][] B) throws RemoteException {

        double[][] B_inv = invertMatrix(B);

        return multiplyMatrices(A, B_inv);

    }


    private double[][] multiplyMatrices(double[][] A, double[][] B) {

        int rows = A.length;

        int cols = B[0].length;

        int common = B.length;


        double[][] result = new double[rows][cols];


        for (int i = 0; i < rows; i++) {

            for (int j = 0; j < cols; j++) {

                result[i][j] = 0;

                for (int k = 0; k < common; k++) {

                    result[i][j] += A[i][k] * B[k][j];

                }

            }

        }

        return result;

    }


    

    private double[][] invertMatrix(double[][] matrix) throws RemoteException {

        if (matrix.length != 2 || matrix[0].length != 2)

            throw new RemoteException("Only 2x2 matrix inversion supported in this demo.");


        double a = matrix[0][0];

        double b = matrix[0][1];

        double c = matrix[1][0];

        double d = matrix[1][1];


        double det = a * d - b * c;

        if (det == 0) throw new RemoteException("Matrix is singular, cannot invert.");


        double[][] inverse = {

            { d / det, -b / det },

            { -c / det, a / det }

        };


        return inverse;

    }

}


3.Starts the RMI server – MatrixServer

import java.rmi.Naming;

import java.rmi.registry.LocateRegistry;


public class MatrixServer {

    public static void main(String[] args) {

        try {

            LocateRegistry.createRegistry(1099);

            MatrixService service = new MatrixServiceImpl();

            Naming.rebind("MatrixService", service);

            System.out.println("MatrixService is running...");

        } catch (Exception e) {

            System.out.println("Server Exception: " + e.getMessage());

        }

    }

}


4.Sends matrices, gets result – MatrixClient

import java.rmi.Naming;


public class MatrixClient {

    public static void main(String[] args) {

        try {

            MatrixService service = (MatrixService) Naming.lookup("rmi://localhost/MatrixService");


            double[][] A = {

                {4, 7},

                {2, 6}

            };


            double[][] B = {

                {1, 2},

                {3, 4}

            };


            double[][] result = service.divideMatrices(A, B);


            System.out.println("Result of A / B (i.e., A * inverse(B)):");

            for (double[] row : result) {

                for (double val : row) {

                    System.out.printf("%.2f ", val);

                }

                System.out.println();

            }


        } catch (Exception e) {

            System.out.println("Client Exception: " + e.getMessage());

        }

    }

}

5.Expected Output

Server














Client



Comments

Popular posts from this blog

Real Estate Managing Website

Password Manager