I am using Jama API for solving a problem with Linear Algebra. But it is giving me an error: java.lang.RuntimeException: Matrix is singular.
I suppose when the matrix is singular there are multiple solutions possible. Is there a way in Jama API to get one of these solutions or is there any other API that can help me here.
Below is a code snippet I am using:
Matrix A = new Matrix(input);
Matrix B = new Matrix(startState);
Matrix X = A.solve(B);
answer = X.getArray();
return answer;
check the determinant of the matrix - if zero, it means that the matrix does not have an inverse (rows making up the matrix are not independent). In that case, you can look into SVD, Gauss-Siedel, Jacobi iteration etc. Also, as an alternate library, you could look into apache commons math if it helps.
Related
I'm trying to solve M (NxN) linear systems (Ax = B, B = [b1,b2,...bM]) using Ojalgo. Thanks to apete's counsel, I successfully managed to check if A (A, B are objects of type PrimitiveMatrix) is singular but it seems that sometimes it's also unstable..
It would be really useful for me to determine if this matrix is stable or not.
Any help would be most appreciated. Thank you!
You should find the condition number of your matrix. I believe getCondition() gives you the condition number of a matrix. Bigger the number is less stable the matrices are.
I am looking for a Java library that closely mirrors matlab's Matrix functions and possibly other functions in the areas of polynomial interpolation, etc.
If such a library does not exist I was toying with the idea of building my own but using an existing Matrix or scientific computing library to do the heavy lifting - if I were to do that which libraries would be candidates to serve as backends for such an effort
Eigen, one of the most used (and fastest) library for matrix computation in C++, has a java wrapper: jeigen.
It allows one to manipulate full and sparse matrices and make operations one them. It can be also worth trying.
Check out the following resources/packages
http://math.nist.gov/javanumerics/jama/
http://www.jscience.org/
Try to look at la4j (Linear Algebra for Java). It supports dense matrices as well as sparse ones. Here is just a brief example of using functional features of la4j:
// reads the dense matrix from the CSV file
Matrix a = new Basic2DMatrix(Mattrices.asSymbolSeparatedSource("matrix.csv", ","));
// calculates the sum of all elements of the matrix 'a'
double sum = a.fold(Matrices.asSumAccumulator(0));
// creates a new matrix 'b', that contains elements of matrix 'a' multiplied by '2'.
Matrix b = a.transform(Matrices.asMulFunction(2));
The best way to get the last version of la4j - visit it's GitHub page.
I use the Colt library for matrix operations.
See in: http://acs.lbl.gov/software/colt/api/index.html
I think it's really good and easy to use and is better than Apache Commons-Math and EJML that I have already tried.
I suggest you try all of the libraries mentioned and choose the one that is closer to your needs.
This is the code my friends and I have come up with so far after fiddling around. What we are trying to do is read in the adjacency matrix (input.txt), then create a directed graph out of it so we can search it using Depth-First Search. We want the output of our program to provide the order of visited nodes.
The java code:
http://pastebin.com/bAzBadxi
The input.txt file:
http://pastebin.com/r72J34uA
My question is, what do we initialize "n" to? (line 32 in the java code)
Any help will be appreciated.
Create a vertex object before you use it.
Vertex n; // before g.addVertex(n);
I am not validating your algorithm, just removing compiler error, if your algo is correct it should work fine
What you are trying to solve is a problem of topological sorting.
Topological sorting
In this case, it doesn't matter what n you initialize to, you can simply use the first vertex in the adjacency matrix as the start.
And adjacency matrix (which should be a square matrix) is a legit representation of a directed graph, you can use the matrix to search the graph directly.
I have this problem in calculating Jaccard Similarity for finding similar books using transaction id from MySQL database of sales transactions :
t1= Java,Ruby,C
t2= Java,C#, Python
t3= C#, VB, C
....etc
Size of Java intersection = 2; (How could we find it out?)
Size of union = 3, (How could we find it out?)
Jaccard similarity = (intersection/union) = 2/3
But I don't understand how could I find out the "intersection" and "union" of the two vectors or how to implement it in Java/JSP.
Please help me and thanks a lot!
You need to use one of standard Set class. You can do an intersect, union and size calculation on sets.
Which is the best way to implement a sparse vector in Java?
Of course the good thing would be to have something that can be manipulated quite easily (normalization, scalar product and so on)
Thanks in advance
MTJ has a Sparse Vector class. It has norm functions (1-norm 2-norm and ∞-norm) and dot product functions.
JScience has a SparseVector implementation that is part of its linear algebra package.
You can also try to look at la4j's CompressedVector implementation. It uses pair of arrays: array of values and array of their indicies. And with binary search on top of that it just flies. So, this implementation guarantees O(log n) running time for get/set operations.
Just a brief example
Vector a = new CompressedVector(new double[]{ 1.0, 2.0, 3.0 }).
// calculates L_1 norm of the vector
double n = a.norm();
// calculates the sum of vectors elements
double s = a.fold(Vectors.asSumAccumulator(0.0));