LCP(linear complementary problem) implementation in Java - math

I currently have an issue with a new project, because I need a solution for a LCP. I know there are
algorithms like Lemke etc. But each of them seems to be written in C++ or Python(using numpy).
I am not really able to implement an algorithm by myself, because I'm not completely familiar with all the math behind it.
So my question is if there is any LCP solver for Java which I can just use by calling something like
SolveLCP(Matrix M, Vector q, Vector w, Vector z)
for computing w and z from M and q ?
I found an algorithm at
https://www.geometrictools.com/GTE/Mathematics/LCPSolver.h
which seems to work pretty good, but unfortunately it's C++ and I failed to convert it into java(actually I use kotlin). My version has the correct results sometimes, but not every time compared with the C++ implementation.
I implemented it in kotlin, you can find it here:
https://github.com/mihisg/LCPSolver_Kotlin/tree/main
I would really appreciate it if someone could help finding the errors. I think I did something wrong with representing all those pointers, but in fact, it only works sometimes.

Related

How to transpose a high-degree tensor in FP way?

I'm currently working on a math library.
It's now supporting several matrix operations:
- Plus
- Product
- Dot
- Get & Set
- Transpose
- Multiply
- Determinant
I always want to generalize everything I can generalize
I was thinking about a recursive way to implement the transpose of a matrix, but I just couldn't figure it out.
Anybody help?
I would advise you against trying to write a recursive method to transpose a matrix.
The idea is easy:
transpose(A) = A(j,i)
Recursion isn't hard in this case. You can see the stopping condition: a 1x1 matrix with a single value is its own transpose. Build it up for 2x2, etc.
The problem is that this will be terribly inefficient, both in terms of stack depth and memory, for any matrix beyond a trivial size. People who apply linear algebra to real problems can require tens of thousands or billions of degrees of freedom.
You don't talk about meaningful, practical cases like sparse or banded matricies, etc.
You're better off doing it using a straightforward declarative approach.
Haskell use BLAS as its backing implementation. It's a more functional language than JavaScript. Perhaps you could crib some ideas by looking at the source code.
I'd recommend that you do the simple thing first, get it all working, and then branch out from there.
Here's a question to ask yourself: Why would anyone want to do serious numerical work using JavaScript? What will your library offer that's an improvement on what's available?
If you want to learn how to reinvent wheels, by all means proceed. Just understand that you aren't the first.

Efficient and fast way of solving linear programming problems in R

I am working with a very large dataset, typically dealing with a few millions of combinations.
I want to solve the assignment problem.(maximise the sum)
I had tried solving it on a small test set using adagio::assignment, clue::solve_LSAP
I wasnt able to successfully install the "lpSolve" package on my system, threw some segmentation fault
Wanted to know which of these is faster or any other method which does it faster.
Thanks....
An LP formulation is not a good way to solve the assignment problem, whichever library you use. You have to use the Hungarian algorithm, and it looks like solve_LSAP does exactly that.
No need to try anything else IMHO.
EDIT: An efficient implementation of the Hungarian method should be O(n^3), which is extremely fast for any optimization algorithm. If solve_LSAP is not fast enough for your problem (assumed it is implemented correctly), it is very unlikely that any exact method will work.
You will have to use some sort of heuristic to approximate the solution.

using GPU in optimization in R (OpenCL)?

based on an idea from another thread, I was hoping you could help me out with this idea / push me in the right direction.
I have seen an example of OpenCL, which didn't look too complicated for basic calculations, so I hope to just rewrite the function for numerical gradient the optimization routine uses in the OpenCL language, and squeeze it in the optimizer function, so everytime I would optimize some function, it would do the independent calculations in the GPU.
Idea: Use gpu for calculation of functionals and gradients during the optimizations (e.g. nlminb()
Problems:
1, How to tap the optimization routine? (I can't seem to locate the C file of which does the optimisation)
2,Can I just replace the gradient calculation with what I prepare for GPU?
3,Does anyone got something similar to work? Any ideas, notes?
Thank you and have a nice day!
PS: If you think it wouldn't speed up the optim, it's hard to code / hard to do, etc. please let me know! I'm very inexperienced and lousy "programmer".
You could compile R linked to one of the OpenCL optimized BLAS libraries. But based on the attempts to speed up R with other BLAS libraries the results might be limited to special cases. Yours may be one of them though.

Lua alternative to optim()

I'm currently looking for a lua alternative to the R programming languages; optim() function, if anyone knows how to deal with this?
http://numlua.luaforge.net/ looks interesting but doesn't seem to have minimization. The most promising lead seems to be a Lua wrapper for GSL, which has a variety of multidimensional minimization algorithms included.
With derivatives
- BFGS (method="BFGS" in optim) and two conjugate gradient methods (Fletcher-Reeves and Polak-Ribiere) which are two of the three options available for method="CG" in optim.
Without derivatives
- the Nelder-Mead simplex (method="Nelder-Mead", the default in optim).
More specifically, see here for the Lua shell documentation covering minimization.
I agree with #Zack that you should try to use existing implementations if at all possible, and that you might need a little bit more background knowledge to know which algorithms will be useful for your particular problems ...
R's implementation of optim isn't actually written in R. If you type "optim" with no parentheses at the prompt, it'll dump out the definition of the function, and you can see that after some error checking and argument shuffling it invokes an .Internal routine (coded in C and/or Fortran) to do all the real work.
So your best bet is to find a C library for mathematical optimization -- sorry, I have no recommendations -- and wrap that into Lua. I doubt anyone has written native-Lua code for this, and I would not recommend trying to code it yourself; doing mathematical optimization efficiently is still an active domain of basic research, and the best-so-far algorithms are decidedly nontrivial to implement.

Quickly cross-check complex math results?

I am doing matrix operations on large matrices in my C++ program. I need to verify the results that I get, I used to use WolframAlpha for the task up until now. But my inputs are very large now, and the web interface does NOT accept such large values (textfield is limited).
I am looking for a better solution to quickly cross-check/do math problems.
I know there is Matlab but I have never used it and I don't know if thats what will suffice my needs and how steep the learning curve would be?
Is this the time to make the jump? or there are other solutions?
If you don't mind using python, numpy might be an option.
Apart from the license costs, MATLAB is the state of the art numerical math tool. There is octave as free open source alternative, with a similar syntax. The learning curve is for both tools absolutely smooth!
WolframAlpha is web interface to Wolfram Mathematica. The command syntax is exactly the same. If you have access to Mathematica at your university, it would be most smooth choice for you since you already have experience with WolframAlpha.
You may also try some packages to convert Mathematica commands to MATLAB:
ToMatlab
Mathematica Symbolic Toolbox for MATLAB 2.0
Let us know in more details what is your validation process. How your data look like and what commands have you used in WolframALpha? Then we can help you with MATLAB alternative.

Resources