From 054044ccf68703d9b97abc37b072d6b188f7ab71 Mon Sep 17 00:00:00 2001 From: "joseph.lizier" Date: Wed, 18 Jul 2012 05:22:12 +0000 Subject: [PATCH] Added doubleToIntArray() methods to allow Octave users to convert native double arrays to int arrays when required. --- .../infodynamics/utils/MatrixUtils.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/java/source/infodynamics/utils/MatrixUtils.java b/java/source/infodynamics/utils/MatrixUtils.java index f68b75a..8e206f2 100755 --- a/java/source/infodynamics/utils/MatrixUtils.java +++ b/java/source/infodynamics/utils/MatrixUtils.java @@ -2704,4 +2704,53 @@ public class MatrixUtils { } return result; } + + /** + * Convert a double array to an int array. + * This is designed specifically for use of the toolkit in Octave + * where all native arrays are considered as doubles for Java, + * and octave-java can't properly identify valid method signatures + * unless the arrays are converted to int arrays first. + * + * @param array + * @return + */ + public static int[] doubleToIntArray(double[] array) { + if (array == null) { + return null; + } + int[] intArray = new int[array.length]; + for (int i = 0; i < array.length; i++) { + intArray[i] = (int) array[i]; + } + return intArray; + } + + /** + * Convert a 2D double array to an int array. + * This is designed specifically for use of the toolkit in Octave + * where all native arrays are considered as doubles for Java, + * and octave-java can't properly identify valid method signatures + * unless the arrays are converted to int arrays first. + * + * @param array + * @return + */ + public static int[][] doubleToIntArray(double[][] array) { + if (array == null) { + return null; + } + int[][] intArray = new int[array.length][]; + for (int i = 0; i < array.length; i++) { + if (array[i] == null) { + intArray[i] = null; + } else { + intArray[i] = new int[array[i].length]; + for (int j = 0; j < array[i].length; j++) { + intArray[i][j] = (int) array[i][j]; + } + } + } + return intArray; + } }