Calling functions from numerical algo group (nag) library

52 views
Skip to first unread message

webus...@gmail.com

unread,
Dec 19, 2014, 6:34:13 PM12/19/14
to gonu...@googlegroups.com
I'm looking at using some functions from the nag libary. They support multiple languages and C is one of them. I'm trying to figure out if a pointer to gonum matrix can be passed over to a NAG function. From their documentation at:

http://www.nag.com/numeric/CL/nagdoc_cl24/html/GENINT/essint.html

Different programming languages lay out two-dimensional data in memory in different ways. The C language treats a two-dimensional array as a single block of memory arranged in rows, the so called ‘row-major’ ordering. Some other languages, notably Fortran, arrange two-dimensional arrays by column (‘column-major’ ordering). Commencing at Mark 7, those functions in the NAG C Library that deal with two-dimensional arrays and where it is sensible to do so, have an extra argument, called order, which allows you to specify that your data is arranged in rows (by setting order to Nag_RowMajor) or in columns (by setting order to Nag_ColMajor). This is particularly useful if the NAG C Library is being called from a language which uses the column-major ordering or if you wish to call a function from a language, such as Visual Basic 7 onwards, which supports column-major ordering.

For efficiency, and wherever possible, the NAG C Library is designed to make use of the Basic Linear Algebra Subprograms (BLAS), a suite of low-level functions tuned by many computer manufacturers for their particular hardware. Since the BLAS are specified in Fortran, and therefore use the column-major storage order, the NAG C Library also uses this scheme, internally, for new functions wherever it is practical. Thus any two-dimensional arrays you have provided may be re-ordered on entry and/or on exit from the NAG functions, as appropriate. It is therefore slightly more efficient to use the column-major ordering; however, except for very large data sets, the effect is negligible in practice.

 
As an example I'm looking at:

nag_corr_cov (g02bxc) calculates the Pearson product-moment correlation coefficients and the variance-covariance matrix for a set of data. Weights may be used.


documentation for the function is at:


Here is the signature of function 1:

void nag_corr_cov (Integer n, Integer m, const double x[], Integer tdx, const Integer sx[], const double wt[], double *sw, double wmean[], double std[], double r[], Integer tdr, double v[], Integer tdv, NagError *fail)

 

Another example is a function which takes in the an argument order:


Here is another signature for function 2:

void nag_step_regsn (Nag_OrderType order, Integer *istep, Nag_IncludeMean mean, Integer n, Integer m, const double x[], Integer pdx, const char *var_names[], const Integer sx[], Integer maxip, const double y[], const double wt[], double fin, Nag_Boolean *addvar, const char *newvar[], double *chrss, double *f, const char *model[], Integer *nterm, double *rss, Integer *idf, Integer *ifr, const char *free_vars[], double exss[], double q[], Integer pdq, double p[], NagError *fail)



Questions: (from function 1)

- Can I pass gonum matrix as x?
- how would I convert the matrix returned r back into a gonum matrix?
- What is the stride that I have to pass?


Questions: (from function 2)

In this case what is the Nag_OrderType that I pass? row major or color major?

Thanks,

wu

Brendan Tracey

unread,
Dec 19, 2014, 6:52:30 PM12/19/14
to gonu...@googlegroups.com
I am not familiar with NAG, but I believe the following are useful pieces of advice:

1) You cannot pass a *mat64.Dense directly. Dense is a Go struct type, but you will want to just pass a pointer to the data itself.
2) You can see examples of doing this in the wrapping of BLAS using cgo. the package is github.com/gonum/blas/cblas.
3) The data is in row-major order. The stride is the number of elements between the start to the row. data[i*stride +j] is how to access the (i, j) element of the implied matrix. Naively, one might expect that stride is equal to the number of columns, but this is not, in general, true because of taking a View of the matrix.
4) You can reconstruct a Dense by constructing the RawMatrix type yourself and using SetRawMatrix.  There isn't an example of this directly, but you can look at the code for mat64.Dense.Mul to see the data from a Dense being sent to a blas routine.
5) Note that we do have a bunch of statistics functions already in github.com/gonum/stat. In particular, there is a pull request in progress to compute a covariance matrix from a set of data. It isn't through yet, but it is close to being ready. It is also backed by BLAS so it should be fast. If there are other specific functions you need and feel would be a good fit to gonum, please feel free to file issues or write code for us. If you would like to write code, that is awesome, but please discuss changes on gonum-dev first to make sure we are all on the same page before a lot of code is written unnecessarily.

webus...@gmail.com

unread,
Dec 20, 2014, 12:42:30 PM12/20/14
to gonu...@googlegroups.com
Thanks for the quick reply. 

I'll take a more detailed look at cblas and test out calling some of the nag functions...

Is there anything in the works for adding a regression package to gonum/stats?

Brendan Tracey

unread,
Dec 20, 2014, 12:46:41 PM12/20/14
to webus...@gmail.com, gonu...@googlegroups.com
Not directly. I have some stuff implemented in github.com/reggo/reggo. You’re free to use any of the code, but it’s not stable and I don’t have time to support it. There is also the golearn folks who may have what you need.

I think there’s some debate if a regression package should be in gonum or not, but I don’t think we have the manpower to develop and support it at the moment (still a lot to do in blas, mat64, stat, optimize, plot, etc.). When other packages are more stable, perhaps, though there are more fundamental libraries we should probably have first (ode, fft, etc.)


--
You received this message because you are subscribed to the Google Groups "gonum-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gonum-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathan Lawlor

unread,
Dec 21, 2014, 11:37:30 AM12/21/14
to gonu...@googlegroups.com, webus...@gmail.com
There is also a package for one pass linear regression at https://github.com/glycerine/zettalm
Reply all
Reply to author
Forward
0 new messages