简体   繁体   中英

Matrix multiplication by vector return wrong answer Java

Im having a school task in linear algebra where i have to create a encryption application. Firstly user types input string, which i convert to ASCII and put the values in array. After that i create a 2D matrix with the length of the user input and fill with random digits >= 0 and < 100. Now i have to multiply the ASCII array with the created 2D matrix in order to get coded message. I pick the vector-matrix algorithm from the site below, but it seems to return wrong answers. All suggestions highly appereciated! http://introcs.cs.princeton.edu/java/22library/Matrix.java.html

Code :

public static int[] convertToASCII(String input) {

    int[] ascii = new int[input.length()];
    System.out.println("ASCII: ");
    for(char c : input.toCharArray()) {
        for(int x = 0; x < 1;x++) { 
                //convert to ascii
                ascii[x] = (int)c;
                System.out.print(ascii[x] + " ");

        }
    }

    return ascii;

}


// generate random matrix according to the length of user input.
private static double[][] rndMatrix() {
    double[][] rndMatrix = new double[ASCII.length][ASCII.length];
    System.out.println("\n" + "Random matrix: ");
    Random rand = new Random();
    for(int i = 0; i < rndMatrix.length;i++) {
        System.out.print("|");
        for(int j=0;j < rndMatrix[i].length;j++) {
            Integer r = rand.nextInt()% 100;

            rndMatrix[i][j] = Math.abs(r);

            System.out.printf("%4d",(int)rndMatrix[i][j]);
        }
        System.out.println(" |");
    }
    return rndMatrix;
}


//crypt the message by multiplying randomly generated matrix with ascii codes
private static double[] cryptMsg(double[][] randomMatrix2, int[] ascii) {

        int m = randomMatrix2.length;
        int n = randomMatrix2[0].length;

        double[] y = new double[m];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++){
                y[i] += randomMatrix2[i][j] * ascii[j];
            System.out.println(y[i]);
            }
        }
        return y;
    }

Example :

Input:
hi
ASCII: 
104 105 
Random matrix: 
|  85  15 |
|  79  21 |
Coded message: 
8925.0 8295.0 (Has to be 10415.0 10421)

Your convertToASCII(String input) function is wrong. You always set the first index of the array. Change it to:

public static int[] convertToASCII(String input) {
    int[] ascii = new int[input.length()];
    for (int x = 0; x < ascii.length; x++) {
        ascii[x] = input.codePointAt(x);
    }
    return ascii;
}

I tested your matrix multiplication and it's fine:

public static void print(double[] arr) {
    StringBuilder sb = new StringBuilder();
    for (double x : arr) {
        sb.append(x);
        sb.append(", ");
    }
    System.out.println(sb.toString());
}

public static void main(String[] args) {
    double[][] mx1 = { { 1, 2 }, { 4, 8 } };
    int[] vec1 = { 0, 1 };
    int[] vec2 = { 1, 0 };
    int[] vec3 = { 5, 7 };

    print(cryptMsg(mx1, vec1)); // 2.0, 8.0,
    print(cryptMsg(mx1, vec2)); // 1.0, 4.0,
    print(cryptMsg(mx1, vec3)); // 19.0, 76.0,

    int[] vec4 = { 104, 105 };
    double[][] mx2 = { { 85, 15 }, { 79, 21 } };
    print(cryptMsg(mx2, vec4)); // 10415.0, 10421.0,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM