From 315acd7eb2866c780abe62e0224d554a9a1688b5 Mon Sep 17 00:00:00 2001 From: nmtiwari Date: Mon, 13 Feb 2017 01:15:23 -0600 Subject: [PATCH] Addinf code for matrix plus and minus. --- .../boa/functions/BoaMatrixIntrinsics.java | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/src/java/boa/functions/BoaMatrixIntrinsics.java b/src/java/boa/functions/BoaMatrixIntrinsics.java index b9ceebd1e..a3bc05643 100644 --- a/src/java/boa/functions/BoaMatrixIntrinsics.java +++ b/src/java/boa/functions/BoaMatrixIntrinsics.java @@ -111,6 +111,163 @@ public static BoaTup[][] matrix(final BoaTup[] a, final long colsize) { return result; } + /** + * Returns the matrix version of an array. Only scalar values can be sorted. + * Values will be arranged in increasing order. (An optional comparison + * function, which takes two elements and returns int {-,0,+}, is accepted + * as a second argument, but it is curently ignored.) + * + * @param a + * An array of long + * + * @return A sorted copy of a + */ + @FunctionSpec(name = "multiply", returnType = "array of array of int", formalParameters = { "array of array of int", "array of array of int" }) + public static long[][] multiply(final long[][] a, final long[][] b) { + double[][] a_double = new double[a.length][]; + double[][] b_double = new double[b.length][]; + + for(int i = 0; i< a.length; i++){ + a_double[i] = Doubles.toArray(Longs.asList(a[i])); + } + + for(int i = 0; i< b.length; i++){ + b_double[i] = Doubles.toArray(Longs.asList(b[i])); + } + Matrix a_matrix = new Matrix(a_double); + Matrix b_matrix = new Matrix(b_double); + double[][] result = a_matrix.times(b_matrix).getArray(); + long[][]result_long = new long[result.length][]; + for(int i = 0; i< result.length; i++){ + result_long[i] = Longs.toArray(Doubles.asList(result[i])); + } + return result_long; + } + + + + /** + * Returns the matrix version of an array. Only scalar values can be sorted. + * Values will be arranged in increasing order. (An optional comparison + * function, which takes two elements and returns int {-,0,+}, is accepted + * as a second argument, but it is curently ignored.) + * + * @param a + * An array of long + * + * @return A sorted copy of a + */ + @FunctionSpec(name = "matrixsum", returnType = "array of array of int", formalParameters = { "array of array of int", "array of array of int" }) + public static long[][] matrixsum(final long[][] a, final long[][] b) { + double[][] a_double = new double[a.length][]; + double[][] b_double = new double[b.length][]; + + for(int i = 0; i< a.length; i++){ + a_double[i] = Doubles.toArray(Longs.asList(a[i])); + } + + for(int i = 0; i< b.length; i++){ + b_double[i] = Doubles.toArray(Longs.asList(b[i])); + } + Matrix a_matrix = new Matrix(a_double); + Matrix b_matrix = new Matrix(b_double); + double[][] result = a_matrix.plus(b_matrix).getArray(); + long[][]result_long = new long[result.length][]; + for(int i = 0; i< result.length; i++){ + result_long[i] = Longs.toArray(Doubles.asList(result[i])); + } + return result_long; + } + + /** + * Returns the matrix version of an array. Only scalar values can be sorted. + * Values will be arranged in increasing order. (An optional comparison + * function, which takes two elements and returns int {-,0,+}, is accepted + * as a second argument, but it is curently ignored.) + * + * @param a + * An array of long + * + * @return A sorted copy of a + */ + @FunctionSpec(name = "matrixsubstract", returnType = "array of array of int", formalParameters = { "array of array of int", "array of array of int" }) + public static long[][] matrixsubstract(final long[][] a, final long[][] b) { + double[][] a_double = new double[a.length][]; + double[][] b_double = new double[b.length][]; + + for(int i = 0; i< a.length; i++){ + a_double[i] = Doubles.toArray(Longs.asList(a[i])); + } + + for(int i = 0; i< b.length; i++){ + b_double[i] = Doubles.toArray(Longs.asList(b[i])); + } + Matrix a_matrix = new Matrix(a_double); + Matrix b_matrix = new Matrix(b_double); + double[][] result = a_matrix.minus(b_matrix).getArray(); + long[][]result_long = new long[result.length][]; + for(int i = 0; i< result.length; i++){ + result_long[i] = Longs.toArray(Doubles.asList(result[i])); + } + return result_long; + } + + /** + * Returns the matrix version of an array. Only scalar values can be sorted. + * Values will be arranged in increasing order. (An optional comparison + * function, which takes two elements and returns int {-,0,+}, is accepted + * as a second argument, but it is curently ignored.) + * + * @param a + * An array of long + * + * @return A sorted copy of a + */ + @FunctionSpec(name = "multiply", returnType = "array of array of int", formalParameters = { "array of array of int", "array of array of int" }) + public static double[][] multiply(final double[][] a, final double[][] b) { + Matrix a_matrix = new Matrix(a); + Matrix b_matrix = new Matrix(b); + return a_matrix.times(b_matrix).getArray(); + } + + /** + * Returns the matrix version of an array. Only scalar values can be sorted. + * Values will be arranged in increasing order. (An optional comparison + * function, which takes two elements and returns int {-,0,+}, is accepted + * as a second argument, but it is curently ignored.) + * + * @param a + * An array of long + * + * @return A sorted copy of a + */ + @FunctionSpec(name = "matrixsum", returnType = "array of array of int", formalParameters = { "array of array of int", "array of array of int" }) + public static double[][] matrixsum(final double[][] a, final double[][] b) { + Matrix a_matrix = new Matrix(a); + Matrix b_matrix = new Matrix(b); + return a_matrix.plus(b_matrix).getArray(); + } + + /** + * Returns the matrix version of an array. Only scalar values can be sorted. + * Values will be arranged in increasing order. (An optional comparison + * function, which takes two elements and returns int {-,0,+}, is accepted + * as a second argument, but it is curently ignored.) + * + * @param a + * An array of long + * + * @return A sorted copy of a + */ + @FunctionSpec(name = "matrixsubstract", returnType = "array of array of int", formalParameters = { "array of array of int", "array of array of int" }) + public static double[][] matrixsubstract(final double[][] a, final double[][] b) { + Matrix a_matrix = new Matrix(a); + Matrix b_matrix = new Matrix(b); + return a_matrix.minus(b_matrix).getArray(); + } + + + /** * Returns the matrix version of an array. Only scalar values can be sorted. * Values will be arranged in increasing order. (An optional comparison