Skip to content

Commit

Permalink
code for matrix transpose, inverse, summation and substraction suppor…
Browse files Browse the repository at this point in the history
…t operations

adding libs for supporting these operations
  • Loading branch information
nmtiwari committed Feb 13, 2017
1 parent 81af781 commit 3295b15
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 1 deletion.
Binary file added lib/guava-21.0.jar
Binary file not shown.
Binary file added lib/jama-1.0.3.jar
Binary file not shown.
220 changes: 219 additions & 1 deletion src/java/boa/functions/BoaMatrixIntrinsics.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package boa.functions;

import Jama.Matrix;
import boa.BoaTup;
import boa.compiler.ast.literals.IntegerLiteral;

import com.google.common.primitives.Doubles;
import com.google.common.primitives.Longs;


import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -107,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 <em>a</em>
*/
@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 <em>a</em>
*/
@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 <em>a</em>
*/
@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 <em>a</em>
*/
@FunctionSpec(name = "multiply", returnType = "array of array of float", formalParameters = { "array of array of float", "array of array of float" })
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 <em>a</em>
*/
@FunctionSpec(name = "matrixsum", returnType = "array of array of float", formalParameters = { "array of array of float", "array of array of float" })
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 <em>a</em>
*/
@FunctionSpec(name = "matrixsubstract", returnType = "array of array of float", formalParameters = { "array of array of float", "array of array of float" })
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
Expand Down Expand Up @@ -139,5 +300,62 @@ public static double[][] flattenedMatrix(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 <em>a</em>
*/
@FunctionSpec(name = "transpose", returnType = "array of array of float", formalParameters = { "array of array of float" })
public static double[][] transpose(final double[][] a) {
Matrix matrix = new Matrix(a);
matrix = matrix.transpose();
return 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 <em>a</em>
*/
@FunctionSpec(name = "inverse", returnType = "array of array of float", formalParameters = { "array of array of float" })
public static double[][] inverse(final double[][] a) {
Matrix matrix = new Matrix(a);
matrix = matrix.inverse();
return 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 <em>a</em>
*/
@FunctionSpec(name = "inverse", returnType = "array of array of float", formalParameters = { "array of array of int" })
public static double[][] inverse(final long[][] a) {
double[][] data = new double[a.length][];
for(int i = 0; i< data.length; i++){
data[i] = Doubles.toArray(Longs.asList(a[i]));
}
Matrix matrix = new Matrix(data);
matrix = matrix.inverse();
return matrix.getArray();
}

}

0 comments on commit 3295b15

Please sign in to comment.