1

Following the open question: Java equivalent for the NumPy multi-dimensional object, I ended up implementing my own numerical methods in Java. But it seems that the final output is too big (almost twice larger than NumPy). Python does not have numerical types like int, long, float, or double but I believe NumPy converts everything to the byte. Since I implemented my program in Java, I had to use those standard numerical types (double[][][], long[][], etc…), and when I save my results into a file through FileWriter, the size of the output is almost twice as larger as the NumPy functions:

FileWriter fstream = new FileWriter(outName, true);
BufferedWriter out = new BufferedWriter(fstream);

for (int i = 0; i < nFramesBack; i += 2) {
    double[][] data1 = imageObjArray[i];
    for (int j = 0; j < 128; j++) {
        for (int k = 0; k < 128; k++) {
            imageObjArray[i][j][k] = data1[j][k] * flatfB[j][k];
            out.write(data1[j][k] * flatfB[j][k] + " ");
        }
        out.newLine();
    }
    out.newLine();
}
out.flush();
out.close();

I am looking for a solution to convert my output into a byte format like NumPy.

5
  • 2
    byte format? are you referring to raw bytes? based on the javadoc: "FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream." Commented Jun 7, 2023 at 3:16
  • 1
    Where did you read that NumPy uses bytes? The actual documentation says differently: numpy.org/devdocs/dev/internals.html. Finding this link required just a quick Google search. You should research more before asking. Commented Jun 7, 2023 at 3:24
  • @experiment unit 1998X. Thanks for the clarification! I may need to use FileOutputStream! Commented Jun 7, 2023 at 4:07
  • @experiment unit 1998X: Do you have an idea what's the best way to convert a 3d array (double[][][]) into a byte array (byte[])? Because the write() method accepts byte b[]. My 3d array is too large and it's not efficient to convert the whole 3d array into the byte array at once. Commented Jun 7, 2023 at 4:21
  • 1
    you could take a look at this. Based on that, you could write the x,y,z sizes of your 3D array if it is not jagged as a header of sorts, then write the whole 3D array. Then, utilise those values when you are trying to read the file. Commented Jun 7, 2023 at 5:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.