In a C program that gets called from within R, I need to use the 'uniroot' function of R. One way to do this is to invoke R again from C with the 'call_R' function. I am wondering if there is a better way ? Is there a function in 'Rmath.h'to do this ?
As per ?uniroot, the R function is basically a wrapper around some freely available C source code for implementing Richard Brent's root finding algorithm -- it even gives the link. So if you're already programming in C, you don't need to touch R at all for this.
The Rmath library provides a number statistical distribution functions, but no access to R itself.
What you want amounts to embedding R in your C program, which is doable but a little tedious. If you are to C++, you could look at my RInside which makes this pretty painless via C++. It comes with a fairly decent number of examples.
Related
I'm currently playing around with state space models and the book I'm using has some very useful examples.
Problem:
These examples are written in Ox, which somewhat limits its usability, particularly as I want to test out some of my models using the Interactive Brokers API, for which it's more practical to use C# / C++.
More specifically it's using examples from SsfPack which according to this article is "a library of routines for state space
modelling and inference written in C and linked to Ox".
Does this mean it can be directly implemented in C or is it a better approach to write the function in Ox and then call it in C as shown in A1.4 of this document? And if it can be directly implemented, how does one go about it?
Having some experience with C and no experience with Ox, the former of these two options would be much preferable.
Any thoughts welcome!
It seems that there is no official documentation for Ssfpack C routines. Conversely, the ssfpack Ox documentation is well detailed ( see 'SsfPack 3.0: Statistical Algorithms for Models in State Space Form' by Koopman and Doornik ).
If you are an experienced C developer you can observe the ox header file ssfpack.h, you'll find some "extern" declared functions that refers to dll located functions. Those functions can be used in C but you need to find by yourself the prototype of the function. This can be really tricky, maybe impossible, for functions where the number of arguments is not known/not constant.
So, you can call Ssfpack directly from C but, due to the lack of documentation, it is very difficult.
For this reason I would recommend that you write your code in Ox and then call it from C or C#. This require that you learn OX, a good starting point is the book Introduction to Ox by Doornik and Ooms (2006). Then, you need to read the Developer's manual for Ox 7 by Doornik (2012) to understand how to call Ox from C.
I am in the somewhat unfortunate position of interfacing C and Prolog code. We have some data collection code in C, and some analysis code in Gnu-Prolog. So what is the best method to interface C and gprolog? I am currently trying to use the C library included in the gprolog package to call Prolog from C.
Note: I am working on ubuntu machines.
One of the problems I was facing was how to iterate over a list. I finally realized that though you could make a list out of n elements, you had to iterate over it in Prolog fashion - get the head and get the tail and recurse.
There's an entire chapter called Interfacing Prolog and C in the GNU-Prolog manual. I expect that you've seen this since you mention the manual in your comment, but since you seem to be asking for more information than what's given there, perhaps you could be more specific about where you're having trouble?
I recently looked into Lua and it seems really nice. The only annoying thing is its lack of (standard) libraries. But with the JIT compiler comes along a nice FFI C interface.
Coming from a java background, i tried to avoid C as much as possible, so my question: has anyone some experience with LuaJIT, especially its FFI interface, and how difficult is it to set up a library for someone with little to no knowledge in C?
Seemed really simple to me, and Mike Pall has some nice tutorials on it here, the lua mailing list also includes some good examples, so check out the archives as well
how difficult is it to set up a library for someone with little to no
knowledge in C?
Really easy. First, you need to declare the functions that you'd like to use. Then, load the target library and assign it to a Lua variable. Use that variable to call the foreign functions.
Here's an example on using function powf from C's math library.
local ffi = require("ffi")
-- Whatever you need to use, have to be declared first
ffi.cdef([[
double powf(double x, double y);
]])
-- Name of library to load, i.e: -lm (math)
local math = ffi.load("m")
-- Call powf
local n, m = 2.5, 3.5
print(math.powf(n, m))
I am looking for Singular Value Decomposition (SVD) code in C, would you please help me?
I found many sources but I cannot run them, I am looking for a version of SVD code that provide all 3 matrix of S, V and U for me.
You can use the Numerical recipies code for that
svdcmp.c reference. Actually in my case I found more accurate the openCV one, but both work fine.
Use one of the libraries listed at the Wiki page: comparison of linear algebra libraries. Look under the "SVD" column to make sure algorithm is supported (even vast majority of the libraries do support SVD).
Don't write it yourself, don't deal with trying to build someone else's source. Use a library that provides this function for you. There's probably already one available on your target platform.
Specifically, use the industry-standard LAPACK library or use the GSL or whatever other linear algebra library you want. They all have an SVD implementation.
Hi I wanted to know how to use MATLAB as an external solver from a C program. Specifically in my code I wish
to solve several linear systems of the form Ax=b.
I have heard that to go the other way namely Calling C functions in a MATLAB routine one uses MEX files.But I am not really sure how to use Mex files either.
Thank you
Actually, MEX files allow you to include C code in Matlab programs, for example if you want to use external C libraries in Matlab.
What you want to do is use the Matlab Engine:
http://www.mathworks.com/help/techdoc/matlab_external/f29148.html
As an alternative, you could use linear algebra libraries that are written purely in C, such as LAPACK and BLAS. ( www.netlib.org )
you can use the matlab engine as Lagerbaer points out. However sometimes it can be convenient just calling the matlab process commandline style. I use this often when I don't want to mess with mxArrays etc, or when the amount of matlab code that needs executing gets really large. PseudoCode:
WriteArrayInMFormat( "in.m", myInputNumbers );
LaunchProcess( "matlab", "-nodesktop -r \"myFunction( 'in.m' )\" -logfile out.m" );
ReadArrayInMFormat( "out.m", myResult );
For me this is especially useful when testing things out: instead of having to recompile the C/C++ program each time I change something, I just apply all changes in the myFunction.m file. At that point I don't even need the C program, instead everything can be tested in matlab.