Calculating the Chi-square CDF in C - c

I'm very new to coding in C, and I'm working on calculating the CDF for a chi-square distribution given a value and the degrees of freedom. Is there a function or a library that does this or will I need to write this code myself?
Any help would be appreciated

If I am right you can use the GNU Scientific Library. Look for the function gsl_ran_chisq, which returns a random value from a Chi-square distribution provided a number of degrees of freedom.
You can check the C functions related to chi-square and many other distributions on this link. Simply install the library. Link to the lib's home page can also be found here.
I hope I have helped.

Related

Eigenvalues calculations in C-within-R codes

I am writing R code which uses compiled C code.
From "Writing R Extensions" document, I learned there are many R executable/DLL that can be called from C code. The header file ‘Rmath.h’ lists many functions that are available and its source codes are listed on the this website: http://svn.r-project.org/R/trunk/src/nmath/
I need to calculate singular value decomposition of many matrices, however I do not find subroutines which does this on the above website. (So I am assuming that Rmath.h does not contain SVD subroutines) Is there simple way to do eigenvalue calculations in C-within-R code?
Thank you very much.
If you're open to using Rcpp and its related packages, the existing examples for the fastLm() show you how to do this with
Eigen via RcppEigen
Armadillo via RcppArmadillo
the GSL via RcppGSL
where the latter two will use the same BLAS as R, and Eigen has something internal that can often be faster. All the packages implement lm() using the decompositions offered by the language (often using solve or related, but switching to SVD is straightforward once you have the toolchain working for you).
Edit: Here is an explicit example. Use the following C++ code:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::vec getEigen(arma::mat M) {
return arma::eig_sym(M);
}
save in a file "eigenEx.cpp". Then all it takes is this R code:
library(Rcpp) ## recent version for sourceCpp()
sourceCpp("eigenEx.cpp") ## converts source file into getEigen() we can call
so that we can run this:
set.seed(42)
X <- matrix(rnorm(3*3), 3, 3)
Z <- X %*% t(X)
getEigen(Z) ## calls function created above
and I get the exact same eigen vector as from R. It really doesn't get much easier.
It also lets Armadillo chose what method to use for the Eigen decomposition, and as David hinted, this is something quicker than a full-blown SVD (see the Armadillo docs).
You can use one of many available Linear Algebra (lapack) libraries. Here is a link explaining how to get lapack libraries for windows. GOTOBLAS, and ACML are free. MKL is also free for non-commercial use. Once you have the libraries installed, the function you are looking for is sgesvd (for float) or dgesvd (for double).
Here are a couple of examples from Intel on how to use gesvd.
Row Major
Col Major
In case you are using Linux, Check out GNU SL and Eigen. These libraries can usually be installed from the package manager of the distribution.

c code for logf function

I am working on a platform, which doesn't have the math library, but I need to use the logf function (natural log with floating point input). I tried to search the code for logf but in vain. Can somebody provide or give a link for logf function code.
If you don't have math library, you can go to libc to look for the code ->
http://sourceware.org/git/?p=glibc.git;a=blob;f=math/w_logf.c
And see how logf calls __ieee754_logf ->
http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/ieee754/flt-32/e_logf.c
I hope it helps you.
I've seen a few references to logf() functions which just use casting around log(), such as:
float logf(float _X)
{
return ((float)log((double)_X));
}
http://www.raspberryginger.com/jbailey/minix/html/lib_2math_2log_8c-source.html is an implementation of log() (though I have no personal experience with the function there, I merely found it while googling).

Quaternions in CLAPACK or an Alternate C Style Quaternion Library

I am porting a set of spatial computations to an embedded environment that essentially compiles and runs C code.
I have replaced a number of the linear algebra functions that previously used VNL (a templated C++ library that will not work on the new platform) with CBLAS and CLAPACK. Their API (all parameters are pointers, no templates) is perfect for what I am doing.
The catch, however, is I do not see quaternion based functions anywhere in the CLAPACK Users Guide. Am I missing a section and there is quaternion support somewhere? If so, please point me to the functions. Specifically, I'm looking for inverse, multiplication, and conversion to and from euler angles and 3x3 matrices.
If there is not this kind of support in CLAPACK, is there another library with similar design characteristics that does quaternion math?
This PDF seems to indicate that quaternion support was not planned in LAPACK. I suppose it is safe to assume CLAPACK would be the same.
http://www.netlib.org/lapack/lawnspdf/lawn106.pdf
I still have not found a replacement or supplementary library that does support quaternions.
EDIT:
Found CQRLib, an ANSI C quaternion library. It allocates variables to the stack (a problem on my architecture), so I'll have to refactor that. But otherwise it looks like it should work.
http://cqrlib.sourceforge.net/

Singular Value Decomposition simple code in c

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.

Approximate string matching in C?

I am seeking for a C library that does approximate string matching. Calculating Levenshtein distance for example. My query strings will be ~512 bytes. I know about Flamingo but it is C++.
Not a library but simple function you find here
A GPL Version
PyLevenshtein has C source and a Python wrapper: http://code.google.com/p/pylevenshtein/
Wikipedia has an article describing example implementations of Levenshtein distance: http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Levenshtein_distance#C
You might also be able to rewrite part of libraries to allow C code to call into them (via extern C calls).

Resources