Cholesky decomposition with jeigen? - java

I looked for fast java linear algebra library. I tried many of them(jblas, ujmp, ejml and others). In terms of performance finally I found more reliable the jeigen (java wrapper for c++ eigen library). But wrapper has no Cholesky decomposition. But original eigen it has. Is there a way to add decomposition to wrapper?

I do not know of a Java wrapper that performs a Cholesky Decomposition. Perhaps another user will post after me who is more knowledgeable specifically about Java and jeigen.
However, I can offer some help on your underlying problem, which is to perform a Cholesky Decomposition; I recently wrote a C++ program that performs a Cholesky Decomposition on a real, symmetric, positive-definite, matrix. The source code is freely available on GitHub:
https://github.com/dcb2015/dpotrf_ak1/blob/master/dpotrf_ak1.cpp
You can:
1) use the complete C++ program as-is in your own C++ compiler, or use the C++ source in your own program, if you know C++.
2) translate the C++ routine into Java for use in your own program. The sub-routine that performs the decomposition is quite small. And Java syntax is very similar to C/C++ syntax, so the translation should not be difficult. (Actually, I usually try to avoid the "++" aspects of the language and code as close to simple "C" as possible.) If writing the Java program is part of your work or assignment, this might be your best option.
3) use a ready-for-use JavaScript version of this program which is available online (see my Profile for the site.) If you do not have to write this program yourself, but only need the results of a decomposition, try it out. It may help you out of a tough spot.

Related

C++ Triangulation library in a Java Application

I'm implementing an algorithm for pathfinding that is a variant of A* (HPA*) but with a triangulated search space. I've been making a Java application and have written a good amount of code, but I recently found a C++ library that already takes care of the triangulations for the project. I have several options but I'm not sure what I should do: convert the library to Java or integrate the C++ code into my Java library. I could also rewrite my code in C++ but I'm not very familiar with the language.
This application tests the performance of an algorithm for a paper I'm writing so it doesn't need to be portable. What do you think my best option is?
If you really want to call C++ code from Java, read up on the Java Native Interface (JNI) coding conventions.
But generally, unless you have an extremely good reason to cross languages, it's more trouble than it's worth. How complicated is the triangulation code? If it's less than a few hundred lines, I'd suggest reimplementing; if it's more than a few thousand, using a library may be justified but you should probably check whether there's a Java library available or if someone has already written the appropriate JNI glue code.

Running algorithms in compiled C/C++ code within a Java/PHP/Python framework?

Occasionally, I have come across programming techniques that involve creating application frameworks or websites in Java, PHP or Python, but when complex algorithms are needed, writing those out in C or C++ and running them as API-like function calls within your Java/PHP/Python code.
I have been googling and searching around the net for this, and unless I don't know the name of the practice, I can't seem to find anything on it.
To put simply, how can I:
Create functions or classes in C or C++
Compile them into a DLL/binary/some form
Run the functions from -
Java
PHP
Python
I suspect JSON/XML like output and input must be created between the Java/PHP/Python and the C/C++ function so the data can be easily bridged, but that is okay.
I'm just not sure how to approach this technique, but it seems like a very smart way to take advantage of the great features of Java, PHP, and Python while at the same time utilizing the very fast programming languages for large, complex tasks.
The other thought going through my head is if I am creating functions using only literals in Java/PHP/Python, will it go nearly as fast as C anyway?
The specific tasks I'm looking to work with C/C++ on is massive loops, pinging a database, and analyzing maps. No work has started yet, its all theory now.
You can easily extend a python script with custom C++ code using Boost.Python, see this website for more details: http://www.boost.org/doc/libs/1_50_0/libs/python/doc/
This is how you can use it:
char const* greet()
{
return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
You need to compile this into a shared library. You will get a .dll on windows and a .so on Linux. The library will include the necessary code to make it available to python. Example using it:
>>> import hello_ext
>>> print hello_ext.greet()
hello, world
Here are some more examples: http://www.boost.org/doc/libs/1_50_0/libs/python/doc/tutorial/doc/html/index.html
When using Boost.Python remember to link your shared object to python if you are not using weak dynamic linking. There are similar things for PHP and Java.
As for other languages, I never used a custom shared library with Java but did so with PHP and it was a pain using the native Api. I found using swig way more pleasant.
Altough I agree with the comments (you might do it for fun, for business it's a bad idea) you might be interested in this similar question. The mentioned SWIG framework supports
all the languages you mentioned. I worked with it in a project with tons of legacy C code. Not really simple, but very powerful.
For Java, you can search JNI (Java Native Interface), there're a lot of guides telling how to use it.
On a slightly different take from the other proposed solutions, you could look into Gearman
Basically, it's a broker system. You have workers, which can be written in C in your case, to which you can delegate tasks from your python / php / java / w/e code.
Strong point is that you decouple both applications (if you rewrite your app in another language, you'll probably have less work as you only need to get the app to talk to Gearman).
Bad thing is that I think you'll be adding overhead which could make the performance boost irrelevant.

Binary Integer Program Solver for Java

My problem is in trying to solve a Binary Integer Program through Java. I want to run a series of experiments and an integral component of these experiments is to solve an integer program where the variables are constrained to be between 0 and 1.
In the past I have solved such problems in MatLab, with the function bintprog. In the search for such a function (or class? I'm very new to Java) to use in Java, I have come up empty handed.
Is there a Java library available to solve Integer Programs that has really good documentation?
In my search, I have seen suggestions to use a package called LP_Solve that has had a Java wrapper built around it, and a similar wrapper built for a package called GLPK (wrappers here and here) (which I have used before). The problem with these tools is that they are not strictly designed for Java, and thusly, lack the kind of documentation that I feel I need, and even worse have complicated instructions to even begin using them in my own code. As I am currently learning the Java language I am wondering if there are any really good packages available to solve Binary Integer Programs, Mixed Integer Linear Programs, or just Integer Programs from my own Java code.
As a side note, I really do not want to switch to another language because I am building off of past code and classes that perform the tasks I desire.
How about Java Integer Linear Program Solver (JILPS)?
LP_Solve with the Java wrapper is what I will be using. It is a free Mixed Integer Linear Program solver. LP_Solve for Java is very easy to install following these instructions. Included in the packages you download are files with lots of example code, which I have found useful. The only part of the installation that slowed me down was having to join the Yahoo group to find the files for download.
IBM cplex even though being c library has java wrappers and documentation
if you want values 0 or 1 try the bool datatype...if your looking at probabilities (that lie between 0 to 1)try limiting a float value such that it is >= 0 and <=1;

What parts of a Java application should be written in Scala?

I'm writing a Java application using Struts 2, but now I'd like to make it a hybrid Java & Scala project instead. I don't have much experience with Scala, but I learned Haskell years ago at college -- I really liked the functional programmed paradigm, but of course in class we were only given problems that were supremely suited to a functional solution! In the real world, I think some code is better suited to an imperative style, and I want to continue using Java for that (I know Scala supports imperative syntax, but I'm not ready to go in the direction of a pure Scala project just yet).
In a hybrid project, how does one decide what to code in Java and what to code in Scala?
Two things:
99% of Java code can be expressed in Scala
You can write projects that support mixed Java+Scala compilation. Your Scala code can call your Java code and your Java code can call your Scala code. (If you want to do the latter, I suggest defining the interface in Java and then just implementing it in Scala. Otherwise, calling Scala code from Java can get a little ugly.)
So the answer is: whatever parts you want. Your Scala code does not need to be purely functional. Your Scala code can call Java libraries. So pretty much any parts you could write in Java you could also write in Scala.
Now, some more practical considerations. When first trying Scala, some people pick relatively isolated, non-mission-critical parts of their program to write in Scala. Unit tests are a good candidate if you like that approach.
If you're familiar with Java and have learned Haskell in the past, I suggest treating Scala as a "better Java". Fundamentally, Scala compiles to JVM bytecode that is very, very similar to what Java outputs. The only difference is that Scala is more "productive": it produces more bytecode per line of code than Java does. Scala has a lot of things in common with Haskell (first-class functions, for-comprehensions are like Haskell's do-notation, limited type inference), but it's also very different (it's not lazy by default, it's not pure). So you can use some of your insights from Haskell to inspire your Scala style, but "under the hood" it's all Java bytecode.
In the spirit of your question, I recommend you write in Scala any code that involves heavy manipulation of collections, or that handle XML.
Scala's collection library is the foremost functional feature of Scala, and you'll experience great LoC reduction through its usage. Yes, there are Java alternatives, such as Google's collection library, but you asked what you should write in Scala. :-)
Scala also has native handling of XML. You might well find the transition difficult, if you try to take DOM code and make it work on Scala. But if you, instead, try to approach the problem and the Scala perspective and write it from scratch for Scala, you'll have gains.
I'd advise using Actors as well, but I'm not sure how well you can integrate that with Struts 2 code on Java. But if you have concurrent code, give Actors in Scala a thought.
It might sound silly, but why not write your entire project in Scala? It's a wonderful language that is far more expressive than Java while maintaining binary-compatible access to existing Java libraries.
Ask these questions of your project:
"What operations need side-effects?" and "What functionality is already covered well by Java libraries?" Then implement the rest in Scala.
However I would warn that hybrid projects are by their very nature more difficult than stand alone projects as you need to use multiple languages/environments. So given you claim not much experience with Scala I'd recommend playing with some toy projects first, perhaps a subset of your full goal. That will also give you a feel for where the divide should occur.

Linking languages

I asked a question earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the interface bits.
So, this leads me on to another question. How easy is it to link these languages together? And which combination works best? So, if I wanted to have a Ruby CGI-type program calling C++ or Java AI functions, is that easy to do? Any pointers for where I look for information on doing that kind of thing? Or would a different combination be better?
My main experience with writing web applications started with C++ CGI and then moved on to Java servlets (about 10 years ago) and then after a long gap away from programming I did some PHP. But I've not had experience of writing a web application in a scripting language which then calls out to a compiled language for the speed-critical bits. So any advice will be welcome!
Boost.Python provides an easy way to turn C++ code into Python modules. It's rather mature and works well in my experience.
For example, the inevitable Hello World...
char const* greet()
{
return "hello, world";
}
can be exposed to Python by writing a Boost.Python wrapper:
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python. Here's a sample Python session:
>>> import hello_ext
>>> print hello.greet()
hello, world
(example taken from boost.org)
First, a meta comment: I would highly recommend coding the entire thing in a high-level language, profiling like mad, and optimizing only where profiling shows it's necessary. First optimize the algorithm, then the code, then think about bringing in the heavy iron. Having an optimum algorithm and clean code will make things much easier when/if you need to reimplement in a lower-level language.
Speaking for Python, IronPython/C# is probably the easiest optimization path.
CPython with C++ is doable, but I find C a lot easier to handle (but not all that easy, being C). Two tools that ease this are cython/pyrex (for C) and shedskin (for C++). These compile Python into C/C++, and from there you can access C/C++ libraries without too much ado.
I've never used jython, but I hear that the jython/Java optimization path isn't all that bad.
I agree with the Idea of coding first in a high level language such as Python, Profiling and then Implementing any code that needs speeding up in C / C++ and wrapping it for use in the high level language.
As an alternative to boost I would like to suggest SWIG for creating Python callable code from C. Its reasonably painless to use, and will compile callable modules for a wide range of languages. (Python, Ruby, Java, Lua. to name a few) from C code.
The wrapping process is semi automated, so there is no need to add new functions to the base C code, making a smoother work flow.
If you choose Perl there are plenty of resources for interfacing other languages.
Inline::C
Inline::CPP
Inline::Java
From Inline::C-Cookbook:
use Inline C => <<'END_C';
void greet() {
printf("Hello, world\n");
}
END_C
greet;
With Perl 6 it gets even easier to import subroutine from native library code using NativeCall.
use v6.c;
sub c-print ( Str() $s ){
use NativeCall;
# restrict the function to inside of this subroutine because printf is
# vararg based, and we only handle '%s' based inputs here
# it should be possible to handle more but it requires generating
# a Signature object based on the format string and then do a
# nativecast with that Signature, and a pointer to printf
sub printf ( str, str --> int32 ) is native('libc:6') {}
printf '%s', $s
}
c-print 'Hello World';
This is just a simple example, you can create a class that has a representation of a Pointer, and have some of the methods be C code from the library you are using. ( only works if the first argument of the C code is the pointer, otherwise you would have to wrap it )
If you need the Perl 6 subroutine/method name to be different you can use the is symbol trait modifier.
There are also Inline modules for Perl 6 as well.
Perl has several ways to use other languages. Look at the Inline:: family of modules on CPAN. Following the advice from others in this question, I'd write the whole thing in a single dynamic language (Perl, Python, Ruby, etc) and then optimize the bits that need it. With Perl and Inline:: you can optimize in C, C++, or Java. Or you could look at AI::Prolog which allows you to embed Prolog for AI/Logic programming.
It may be a good approach to start with a script, and call a compilation-based language from that script only for more advanced needs.
For instance, calling java from ruby script works quite well.
require "java"
# The next line exposes Java's String as JString
include_class("java.lang.String") { |pkg, name| "J" + name }
s = JString.new("f")
You can build your program in one of the higher level languages for example Python or Ruby and then call modules that are compiled in the lower level language for the parts you need performance. You can choose a platform depending on the lower level language you want.
For example if you want to do C++ for the speedy stuff you can just use plain Python or Ruby and call DLLs compiled in C++. If you want to use Java you can use Jython or one of the other dynamic languages on the Java platform to call the Java code this is easier than the C++ route because you've got a common virtual machine so a Java object can be used directly in Jython or JRuby. The same can be done on the .Net platform with the Iron-languages and C# although you seem to have more experience with C++ and Java so those would be better options.
I have a different perspective, having had lots of luck with integrating C++ and Python for some real time live video image processing.
I would say you should match the language to the task for each module. If you're responding to a network, do it in Python, Python can keep up with network traffic just fine. UI: Python, People are slow, and Python is great for UIs using wxPython or PyObjC on Mac, or PyGTK. If you're doing math on lots of data, or signal processing, or image processing... code it in C or C++ with unit tests, then use SWIG to create the binding to any higher level language.
I used the image libraries in wxWidgets in my C++, which are already exposed to Python through wxPython, so it was extremely powerful and quick. SCONS is a build tool (like make) which knows what to do with swig's .i files.
The topmost level can be in C or Python, you'll have more control and fewer packaging and deployment issues if the top level is in C or C++... but it will take a really long time to duplicate what Py2EXE or Py2App gives you on Windows or Mac (or freeze on Linux.)
Enjoy the power of hybrid programming! (I call using multiple languages in a tightly coupled way 'hybrid' but it's just a quirk of mine.)
If the problem domain is hard (and AI problems can often be hard), then I'd choose a language which is expressive or suited to the domain first, and then worry about speeding it up second. For example, Ruby has meta-programming primitives (ability to easily examine and modify the running program) which can make it very easy/interesting to implement certain types of algorithms.
If you implement it in that way and then later need to speed it up, then you can use benchmarking/profiling to locate the bottleneck and either link to a compiled language for that, or optimise the algorithm. In my experience, the biggest performance gain is from tweaking the algorithm, not from using a different implementation language.

Categories

Resources