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.

newbie5050
  • 51
  • 6
  • 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." – experiment unit 1998X Jun 07 '23 at 03:16
  • 1
    Where did you read that NumPy uses bytes? The actual documentation says differently: https://numpy.org/devdocs/dev/internals.html. Finding this link required just a quick Google search. You should research more before asking. – aled Jun 07 '23 at 03:24
  • @experiment unit 1998X. Thanks for the clarification! I may need to use FileOutputStream! – newbie5050 Jun 07 '23 at 04: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. – newbie5050 Jun 07 '23 at 04:21
  • 1
    you could take a look at [this](https://stackoverflow.com/q/62470202/16034206). 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. – experiment unit 1998X Jun 07 '23 at 05:23

0 Answers0