I'm trying to port a Matlab code to Java and C++.
It's quite straightforward however I find a function that is more than simple operations, it numerically evaluates integral:
lungh=quad('normpp',0,1,[],[],x1,x2,x3,x4,x5,x6,x7,y1,y2,y3,y4,y5,y6,y7);
Here x1,...,x8 and y1,...,y7 are simple numbers.
First of all, how do this parameters are interpreted? How does this function work?
I think that 0 and 1 are the bounds of the integral...but what about the others?? Especially 'normpp' and []?
I read the quad help but I didn't understand how it works with such an amount of parameters.
The second problem is: Do exist a java and a C++ libraries that offer these function?
I would prefer to do it directly in Java and C++ without calling Matlab.
Thanks!
In C there is a very nice library : The Gnu Scientific Library (GSL).
Here is a link to the Numerical integration page of the GSL :
GSL
The use of this library in a C++ project is straight forward.
I think the function gsl_integration_qag is a good choice to replace the matlab quad function.
Related
In c++ we could use macro as
#define check_and_log_warning(x) if (!(x)) {log_error(#x); cout<<__LINE__;}
I want to get values of a boolean expression and also use it as a string.
Getting the line number inside macro would be added benefit.
Can something like this macro be written in Java ?
I recently moved from c/c++ to java, so I am wondering could such thing be done in c++ without macro ?
Preprocessor is what I miss from C/C++ in Java. There are ways ..., but they are Java ways.
1/ Assertions:
http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html
2/ Logs:
The one that I use is https://commons.apache.org/proper/commons-logging/
(over log4J or whatever you want)
To print line numbers in log messages, you can use something like:
int lineNo = Thread.currentThread().getStackTrace()[2].getLineNumber();
Edit:
Be warned: unlike __LINE__ in C++ getting the stack trace incurs a performance penalty. Be nice :)
There is no macro facility in Java.
You would be using a logging framework (of which there are plenty to choose from).
if (! theActualConditionNotJustX) myLogger.error("describe problem here");
The logging backend (if configured that way) makes sure to capture and display line numbers. There is some runtime cost for this, but it is usually negligable, especially where actual "errors" are involved (it can get a bit costly for trace-level logging).
Looking around I was not able to find a good way to use libsvm with Java and I still have some open questions:
1) It is possible to use only libsvm or I have to use also weka? If any, what's the difference?
2) When using String type data how can I pass the training set as Strings? I was using matlab for a similar problem for proteins classification and there I just gave the strings to the machine without problem. Is there a way to do this in Java?
Here is an incomplete example of what I did in matlab (it works):
[~,posTrain] = fastaread('dataset/1.25.1.3_d1ilk__.pos-train.seq');
[~,posTest] = fastaread('dataset/1.25.1.3_d1ilk__.pos-test.seq');
trainKernel = spectrumKernel(trainData,k);
testKernel = spectrumKernel(testData,k);
trainKf =[(1:length(trainData))', trainKernel];
testKf = [(1:length(testData))', testKernel];
disp('custom');
model = libsvmtrain(trainLabel,trainKf,'-t 4');
[~, accuracy, ~] = libsvmpredict(testLabel,testKf,model)
As you can see I read the file in fasta format and feed them to libsvm but libsvm for java look like it wants something called Node that is made of double. What I did is to take byte[] from the String and then transform them into Double. Is it correct?
3) How to use a custom kernel? I've found this line of code
KernelManager.setCustomKernel(custom_kernel);
but with my libsvm.jar I don't find. Which lib do I have to use?
Sorry for the multiple questions, I hope you will give me a brief overview of what is going on here.
Thanks.
Please note that I've used LIBSVM for MATLAB, but not for Java. I can only really answer question 1, but hopefully this still helps:
It definitely is possible to use libsvm only, and the code is located here: https://www.csie.ntu.edu.tw/~cjlin/libsvm/. Note that jlibsvm is a port of libsvm, and it seems to be easier to use and more optimized for Java. As far as I can tell, weka just has a wrapper class that runs libsvm anyways (it even requires the libsvm.jar), though I mainly based it off of this: https://weka.wikispaces.com/LibSVM.
div(1, sum(1, exp(sum(div(5, product(100, .1)), -5))))
I'm using this in a Solr query, and want to verify that it is the same as :
Where x is 5.
Is this language Java?
If it is, why am I getting this output here:
http://ideone.com/LWYWtU
If it isn't, what language is this and how do I test it?
Thanks in advance for your help.
EDIT: To add more of the surrounding code, here is the full boost value I'm sending to Solr:
if(exists(query({!frange l=0 u=60 v=product(geodist(),0.621371)})),div(1, sum(1, exp(sum(div(product(5), product(100, .1)), -5)))),0)
The reason I think it might be Java is because in the docs, it says Most Java Math functions are now supported, including: and then lists the math functions I ended up using for code.
Solr is Java, but that's not relevant since this is a set of functions that Solr parses and evaluate itself (and not related to Java, except that the backing functions are implemented in Java).
As far as I can say from what you've mapped the functions correctly, as long as the 5 in product(5) is the same as X. You shouldn't need product there, as the value can be included in div directly as far as I can see.
A way to validate it would be to use debugQuery in Solr and see what the value is evaluated as, and then compare it to your own value. Remember that floating point evaluation can introduce a few uncertanities.
I have the following part of code in C++.
cv::Mat markers(image.size(),CV_8U,cv::Scalar(-1));
markers(cv::Rect(0,0,image.cols, 5)) = cv::Scalar::all(1);
Can anybody tell me what is the equivalent syntax in Java (or android) version of this part of the code. I was looking at "setTo()" but it doesn't seem to take Rect. Or I can only modify one by one through a loop?
There are two ways that come to mind.
Use submat
markers.submat(new Rect(0,0,image.cols(),5)).setTo(new Scalar(1));
Use rowRange and colRange
markers.rowRange(0,5).colRange(0,image.cols()).setTo(new Scalar(1));
I'm looking for a Java library to solve this problem:
We know X is sparse(most of it's entries are zero), so X can be recovered by solving this:
variable X;
minimize(norm(X,1)+norm(A*X - Y,2));
It's a MATLAB code, matrix A and vector Y are known and I want the best X.
I saw JOptimizer, but I couldn't use it. (Doesn't have good documentation or examples).
What you need is a reasonably good LP Solver.
Possible Java LP Solver Options
Apache Commons (Math) Simplex Solver.
See this blog post.
If you have access to CPLEX (not-free), its Java API would work great.
Also, you can look into SuanShu, a Java numerical and statistical library
lpSolve has a Java wrapper which can do the job.
Finally, JOptimizer is indeed a good option. Not sure if you looked at this example.
Hope at least one of those help.
As far as I can tell, you're trying to solve a binary integer program for feasibility
Ax = b, x in {0,1}.
I'm not completely sure, but it seems that you might be interested in the optimization problem
min 1'*x
s.t. Ax = b, x in {0,1}
where 1 is a vector of 1's of the same dimension as x.
The feasibility problem may be in practice much easier than the optimization problem - it all depends on a particular A and b.
If you can get a license of either CPLEX or Gurobi (if you're an academic), these are excellent integer programming solvers with good Java API's. If you don't have access to these, lpsolve may be a good option.
As far as I can tell, JOptimizer will not solve your problem since your variables are integers (although I have never used JOptimizer).
To solve convex optimization problems in java you can use the following library https://github.com/erikerlandson/gibbous