diff options
author | 2011-01-08 19:05:57 +0000 | |
---|---|---|
committer | 2011-01-08 19:13:07 +0000 | |
commit | 62d213b65e148467ad41e3a073a2123c66dd1572 (patch) | |
tree | cb1ed54521ea08b8defad427118f5091ed269001 | |
parent | condor bump (diff) | |
download | sci-62d213b65e148467ad41e3a073a2123c66dd1572.tar.gz sci-62d213b65e148467ad41e3a073a2123c66dd1572.tar.bz2 sci-62d213b65e148467ad41e3a073a2123c66dd1572.zip |
mask root svn
91 files changed, 11796 insertions, 0 deletions
diff --git a/dev-python/pyds9/Manifest b/dev-python/pyds9/Manifest new file mode 100644 index 000000000..f252ebe7c --- /dev/null +++ b/dev-python/pyds9/Manifest @@ -0,0 +1,3 @@ +DIST pyds9-1.0.tar.gz 869723 RMD160 da335e04264c97b46853a0849e9717f773e01046 SHA1 3debdeba0c54a8129a2a277be98d44969a4c9db6 SHA256 4184380aa383a6cbf2d5227ea83dcc0c3dc9c6d0952b81d5c416d782ffff572f +EBUILD pyds9-1.0.ebuild 422 RMD160 d494b0419edb9dfdc398556a7ae4f33afe4a28ce SHA1 2de3873833ddd8bdfbc34b72f5eaecdc7a0fee0f SHA256 bad88d838fc35472400c4068b633d311b55bd7665f68bf36e685a9e917570206 +MISC metadata.xml 374 RMD160 751b95b8cb2d0fa8194e7388b51f5785b4f16d0f SHA1 32790eaab968b07b66c22157f42138b1f56911e6 SHA256 5fe68de0cf26109619592c99a227ee9f4b54f2a18e4c9f95206a63c8275c1b60 diff --git a/dev-python/pyds9/metadata.xml b/dev-python/pyds9/metadata.xml new file mode 100644 index 000000000..03c443f53 --- /dev/null +++ b/dev-python/pyds9/metadata.xml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd"> +<pkgmetadata> +<herd>sci</herd> +<longdescription lang="en"> + pywcs is a set of routines for handling the FITS World Coordinate + System (WCS) standard. It is a thin wrapper around the high- and + mid-level interfaces for wcslib. +</longdescription> +</pkgmetadata> diff --git a/dev-python/pyds9/pyds9-1.0-syslibs.patch b/dev-python/pyds9/pyds9-1.0-syslibs.patch new file mode 100644 index 000000000..b4e7e5b75 --- /dev/null +++ b/dev-python/pyds9/pyds9-1.0-syslibs.patch @@ -0,0 +1,74 @@ +--- setup.py.orig 2010-07-20 17:30:06.000000000 +0100 ++++ setup.py 2010-07-20 17:43:40.000000000 +0100 +@@ -1,60 +1,5 @@ + #!/usr/bin/env python + from distutils.core import setup +-from distutils.command import build_py, install_data, clean +-from os import system, path +-import os +-import struct +- +-# which shared library? +-ulist=os.uname() +-if ulist[0] == 'Darwin': +- xpalib = 'libxpa.dylib' +-else: +- xpalib = 'libxpa.so' +- +-# make command for xpa +-xpadir='xpa-2.1.10' +-def make(which): +- curdir=os.getcwd() +- srcDir=os.path.join(os.path.dirname(os.path.abspath(__file__)),xpadir) +- os.chdir(srcDir) +- if which == 'all': +- os.system('echo "building XPA shared library ..."') +- cflags='' +- if not 'CFLAGS' in os.environ and struct.calcsize("P") == 4: +- if ulist[0] == 'Darwin' or ulist[4] == 'x86_64': +- os.system('echo "adding -m32 to compiler flags ..."') +- cflags=' CFLAGS="-m32"' +- os.system('./configure --enable-shared --without-tcl'+cflags) +- os.system('make clean; make; rm -f *.o') +- elif which == 'clean': +- os.system('echo "cleaning XPA ..."') +- os.system('make clean') +- os.chdir(curdir) +- +-# rework build_py to make the xpa shared library as well +-class my_build_py(build_py.build_py): +- def run(self): +- make('all') +- build_py.build_py.run(self) +- +-# thanks to setup.py in ctypes +-class my_install_data(install_data.install_data): +- """A custom install_data command, which will install it's files +- into the standard directories (normally lib/site-packages). +- """ +- def finalize_options(self): +- if self.install_dir is None: +- installobj = self.distribution.get_command_obj('install') +- self.install_dir = installobj.install_lib +- print 'Installing data files to %s' % self.install_dir +- install_data.install_data.finalize_options(self) +- +-# clean up xpa as well +-class my_clean(clean.clean): +- def run(self): +- make('clean') +- clean.clean.run(self) + + # setup command + setup(name='pyds9', +@@ -63,9 +8,5 @@ + author='Bill Joye and Eric Mandel', + author_email='saord@cfa.harvard.edu', + url='http://hea-www.harvard.edu/saord/ds9/', +- py_modules=['ds9', 'xpa'], +- data_files=[('', [xpadir+'/'+xpalib, xpadir+'/xpans'])], +- cmdclass = {'build_py': my_build_py, \ +- 'install_data': my_install_data, \ +- 'clean': my_clean }, ++ py_modules=['ds9', 'xpa'] + ) diff --git a/dev-python/pyds9/pyds9-1.0.ebuild b/dev-python/pyds9/pyds9-1.0.ebuild new file mode 100644 index 000000000..f41f6be4c --- /dev/null +++ b/dev-python/pyds9/pyds9-1.0.ebuild @@ -0,0 +1,18 @@ +# Copyright 1999-2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +EAPI=3 +inherit distutils + +DESCRIPTION="Python wrapper around SAOImage DS9" +HOMEPAGE="http://hea-www.harvard.edu/saord/ds9/pyds9/" +SRC_URI="http://hea-www.harvard.edu/saord/download/ds9/python/${P}.tar.gz" + +LICENSE="" +SLOT="0" +KEYWORDS="~amd64 ~x86" +IUSE="" + +RDEPEND="x11-libs/xpa" +DEPEND="${RDEPEND}" diff --git a/dev-python/pymvpa/metadata.xml b/dev-python/pymvpa/metadata.xml new file mode 100644 index 000000000..6b37a236c --- /dev/null +++ b/dev-python/pymvpa/metadata.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd"> +<pkgmetadata> +<herd>sci-mathematics</herd> +<herd>python</herd> +<longdescription lang="en"> + Python module to ease pattern classification analyses of large + datasets. It provides high-level abstraction of typical processing + steps (e.g. data preparation, classification, feature selection, + generalization testing), a number of implementations of some popular + algorithms (e.g. kNN, GNB, Ridge Regressions, Sparse Multinomial Logistic + Regression), and bindings to external machine learning libraries (libsvm, + shogun). +</longdescription> +</pkgmetadata> diff --git a/dev-python/pymvpa/pymvpa-0.4.4.ebuild b/dev-python/pymvpa/pymvpa-0.4.4.ebuild new file mode 100644 index 000000000..06c6e27a7 --- /dev/null +++ b/dev-python/pymvpa/pymvpa-0.4.4.ebuild @@ -0,0 +1,37 @@ +# Copyright 1999-2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: /var/cvsroot/gentoo-x86/dev-python/pywavelets/pywavelets-0.2.0.ebuild,v 1.1 2010/04/22 20:14:30 bicatali Exp $ + +EAPI=2 +SUPPORT_PYTHON_ABIS="1" +inherit distutils + +DESCRIPTION="Multivariate pattern analysis with Python" +HOMEPAGE="http://www.pymvpa.org/" +SRC_URI="" +LICENSE="" +SLOT="0" +KEYWORDS="~amd64 ~x86" +IUSE="doc examples minimal test" + +DEPEND="dev-lang/swig + dev-python/lxml + dev-python/numpy + sys-apps/help2man + doc? ( dev-python/sphinx media-gfx/graphviz ) + test? ( dev-python/nose )" + +RDEPEND="dev-python/numpy + !minimal? ( + dev-python/hcluster + dev-python/ipython + dev-python/matplotlib + dev-python/pynifti + dev-python/rpy + sci-libs/afni + sci-libs/fsl + sci-libs/libsvm + sci-libs/scipy + sci-libs/shogun[python] ) + +RESTRICT_PYTHON_ABIS="3.*" diff --git a/dev-python/pysao/Manifest b/dev-python/pysao/Manifest new file mode 100644 index 000000000..7ea2e6bb1 --- /dev/null +++ b/dev-python/pysao/Manifest @@ -0,0 +1 @@ +EBUILD pysao-9999.ebuild 449 RMD160 a4edc0920406641d736fec434ea2d10d6f9827fe SHA1 7570ae7adf0b17a8a9d147e3bd9259ff28e2b8b3 SHA256 ec1ab487379140c32b8f3ada2bf79a067ed6a98dfca1b15b954599bb81690423 diff --git a/dev-python/pysao/pysao-9999.ebuild b/dev-python/pysao/pysao-9999.ebuild new file mode 100644 index 000000000..a63e8c03b --- /dev/null +++ b/dev-python/pysao/pysao-9999.ebuild @@ -0,0 +1,21 @@ +# Copyright 1999-2009 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +inherit subversion distutils + +DESCRIPTION="Python wrapper around some SAO tools" +HOMEPAGE="http://code.google.com/p/python-sao/" +ESVN_REPO_URI="http://python-sao.googlecode.com/svn/trunk" + +LICENSE="MIT" +SLOT="0" +KEYWORDS="~amd64 ~x86" +IUSE="" + +RDEPEND="x11-libs/xpa" +DEPEND="${RDEPEND}" + +src_unpack() { + subversion_src_unpack +} diff --git a/docs/proj/#linalg.xml# b/docs/proj/#linalg.xml# new file mode 100644 index 000000000..3105039cc --- /dev/null +++ b/docs/proj/#linalg.xml# @@ -0,0 +1,551 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE guide SYSTEM "/dtd/guide.dtd"> +<!-- $Header: $ --> + +<guide link="/proj/en/science/linalg.xml"> +<title>Linear Algebra on Gentoo</title> + +<author title="Author"> + <mail link="bicatali@gentoo.org">Sébastien Fabbro</mail> +</author> + +<abstract> + This guide explains the use of linear algebra libraries and focus on + how to use the different implementations of BLAS and LAPACK available on Gentoo. +</abstract> + +<!-- The content of this document is licensed under the CC-BY-SA license --> +<!-- See http://creativecommons.org/licenses/by-sa/2.5 --> +<license/> + +<version>1.0</version> +<date>2010-12-22</date> + +<chapter> +<title>Introduction</title> +<section> +<body> + +<p> + There are <uri link="http://en.wikipedia.org/wiki/List_of_numerical_libraries">many</uri> + performant numerical libraries available. + The Basic Linear Algebra Subprograms (BLAS) and the Linear Algebra PACKage (LAPACK) + are well designed linear algebra libraries developed by the + High Performance Computing (HPC) community. BLAS is an API of dense + matrix and vectors products, while LAPACK provides routines for + solving systems of linear equations. Both are widely used in + many scientific applications and it is, therefore, important to + have efficient implementations available. +</p> + +<p> + BLAS and LAPACK were originally written in FORTRAN 77. Since then, a + number of additional language wrappers have been developed for + languages like C, C++, FORTRAN 95, Java, Python, etc... + Netlib offers exact implementations of the APIs and they are called + "reference" libraries. There is also some parallel implementations + for +</p> + +<ul> +<li> + <uri link="http://www.netlib.org/blas/">BLAS</uri>: FORTRAN 77 and C + (CBLAS) implementations of BLAS +</li> +<li> + <uri link="http://www.netlib.org/lapack/">LAPACK</uri>: FORTRAN 77 and + C (LAPACKE) implementations of LAPACK +</li> +</ul> + +<p> + +</p> + +<ul> +<li> + <uri link="http://www.netlib.org/blacs/">BLACS</uri>: FORTRAN 77 and C + implementations of BLACS +</li> +<li> + <uri link="http://www.netlib.org/scalapack/">ScaLAPACK</uri>: FORTRAN 77 and + C implementations of PBLAS and ScaLAPACK +</li> +</ul> + +<p> + In addition, Gentoo provides a number of optimized implementations + of the above linear algebra libraries that will be described + below. You can switch between implementations with the + Gentoo's <c>eselect</c> system and the widely used <c>pkg-config</c> + tool. +</p> + +<p> + It is important to note that if you require, e.g., a well performing + BLAS implementation, simply emerging X over Y often is not enough. Rather, you will have + to carefully benchmark your applications since performance may depend + on many factors, + such as hardware or network. + If you are simply looking for a well performing and well tested + implementation, the reference ebuilds will likely be your best choice. +</p> + + +</body> +</section> +</chapter> + +<chapter> +<title>For Users</title> +<section> +<title>Installing</title> +<body> + +<p> + If best possible performance is not of paramount importance for you + and you simply need BLAS and/or LAPACK, just emerge the virtual + package: +</p> + +<pre caption="Installing"> +# <i>emerge lapack</i> +</pre> + +<p> + This will install both <><> and <><> the reference packages from + <uri>http://www.netlib.org/</uri> . They are well tested, easy to debug + implementations. They should satisfy most users; if they're all you need, you're + done reading. +</p> + +<p> +However, if: +</p> + +<ul> + <li>linear algebra libraries are critical for the speed of your applications</li> + <li>you absolutely need to build the fastest computer</li> + <li>you want to help Gentoo sci project to improve their packages</li> +</ul> + +<p> +... then read on, and be sure to file bugs both to Gentoo and upstream. +</p> + +<p> + There is a number of optimized implementations of these libraries in the Portage + tree: +</p> + +<ul> + <li> + <uri link="http://math-atlas.sourceforge.net">ATLAS</uri>: Automatically + Tuned Linear Algebra Software is an open-source package that empirically + tunes the library to the machine it is being compiled on. It provides BLAS + (FORTRAN 77 and C), and LAPACK implementations on various architectures. + </li> + <li> + <uri + link="http://www.tacc.utexas.edu/tacc-projects/gotoblas2/">GotoBLAS</uri>: + Goto BLAS provides open-source, free for academic use, hand-coded + machine language, processor optimized versions of the FORTRAN 77 + and C BLAS routines. Still claims to be the fastest BLAS. + </li> + <li> + <uri link="http://developer.amd.com/cpu/libraries/acml/Pages/default.aspx">ACML</uri>: + AMD Core Math Library is a closed-source but free package containing BLAS (FORTRAN 77 + only) and LAPACK for x86 and x86_64 architectures, but also other math tools + such as statistical libraries and FFTs. + </li> + <li> + <uri link="http://software.intel.com/en-us/articles/intel-mkl/">MKL</uri>: + Intel® Math Kernel Library is a closed-source but free package for + non-commercial use on Linux systems containing implementations of all the linear + algebra libraries mentioned here. + </li> +</ul> + +<p> + Usually performance gain is noticeable mainly with BLAS, since LAPACK routines + depend on BLAS kernels. +</p> + +</body> +</section> + + +<section> +<title>Developping with the installed linear algebra libraries</title> +<body> + +<p> + We took great care to make sure that each package provides + consistent pkg-config files generated by us. + Compiling and linking becomes straightforward: +</p> + +<pre caption="Compiling and linking linear algebra libraries"> +# <i>pkg-config --libs blas</i> <comment>(To link with FORTRAN 77 BLAS library)</comment> +# <i>pkg-config --cflags cblas</i> <comment>(To compile against C BLAS library)</comment> +# <i>pkg-config --libs cblas</i> <comment>(To link with C BLAS library)</comment> +# <i>pkg-config --libs scalapack</i> <comment>(To link with the ScaLAPACK library)</comment> +</pre> + +<p> + <c>pkg-config</c> files are available for all implementations and + various alternatives within implementations. The default names of + the implementations are: blas, cblas, lapack, lapacke, blacs and + scalapack, and they can be chosen with <c>eselect</c>. You can also always compile or link + with an library not selected for the + More information on using <c>pkg-config</c> can be obtained with <c>man pkg-config</c>. +</p> + +</body> +</section> +<section> +<title>Selecting libraries</title> +<body> + +<p> + You can switch BLAS, CBLAS and LAPACK implementations with + <c>eselect</c>. you can view which implementations of CBLAS + are available. +</p> + +<pre caption="Viewing available implementations of CBLAS"> +# <i>eselect cblas list</i> +Installed CBLAS for library directory lib64 +[1] atlas +[2] atlas-threads +[3] gsl +[4] mkl-threads * +[5] reference +</pre> + +<p> + The implementation marked with an asterisk (*) is the currently + selected implementation. To switch implementations, run: +</p> + +<pre caption="Switching to the threaded ATLAS implementation of BLAS"> +# <i>eselect blas set atlas-threads</i> +</pre> + +<p> + To learn more about the <c>eselect</c> tool, visit the + <uri link="http://www.gentoo.org/proj/en/eselect/user-guide.xml">eselect guide</uri> +</p> + +<p> + When selecting your linear algebra profiles try to avoid mixing + different implementations since we don't have any mechanism to enforce + reasonable profiles. However, here is a list of well performing + profile combinations that have been used successfully in the past: +</p> +<ul> + <li> performant on most CPUs: + <ul> + <li>blas, cblas: atlas (or atlas-threads with multi-processor)</li> + <li>lapack, lapacke: atlas</li> + </ul> + </li> + <li> performant on most CPUs: + <ul> + <li>blas, cblas: goto2 </li> + <li>lapack, lapacke: reference</li> + </ul> + </li> + <li> performant on AMD based CPUs: + <ul> + <li>blas, lapack: acml-gfortran (or acml-gfortran-openmp with + multi-processors) </li> + <li>cblas: reference</li> + </ul> + </li> + <li> performant on Intel based CPUs: + <ul> + <li>blas,cblas,lapack: mkl-threads</li> + </ul> + </li> +</ul> + +</body> +</section> + +<section> +<title>Choosing a compiler</title> +<body> + +<p> + All the above libraries have been tested with the GNU compiler + collections (gcc, gfortran). + There are many available C compilers and a few FORTRAN (ifort, + Open64) compilers on Gentoo and many other FORTRAN compilers outside + of Gentoo (). +</p> + +<pre caption="Installing BLAS with the Intel FORTRAN compiler"> +# <i>F77=ifort FFLAGS="-O2 -mp1" emerge blas-reference</i> +</pre> + +<p> + Depending on your hardware, a small performance gain can be noticed thanks to + vectorization. The <c>-mp</c> flag maintains floating-point precision, since by + default ifort is pretty aggressive on floating point arithmetic, and we are + actually compiling a math package. Try <c>man ifort</c> to see additional flags + to fit your hardware. +</p> + +<p> + Some of the implementations let you specify the Intel® C compiler as + well. Please beware that not all libraries compile with all + combinations. You should receive an error during the emerge in case you have + chosen an incompatible combination. +</p> + +<p> + As usual for Gentoo, there are many combinations of USE flags and + compilers with which you could compile a package. Unfortunately + switching compilers between BLAS and LAPACK might not be always + compatible. For example: +</p> + +<pre caption="Looking for trouble combinations"> +# <i>USE=ifort emerge acml</i> +# <i>eselect blas set acml-ifort-openmp</i> +# <i>FC=gfortran FFLAGS="-O2" emerge lapack-reference</i> +</pre> + +<p> + This will most likely break things or not even compile. +</p> + +<p> + Try to be consistent in your choice. Stay with the GCC most of the time will + avoid you some trouble, unless you want to use the MKL, in which case the Intel + compilers make a good combination. +</p> + +</body> +</section> +<section> +<title>Documentation</title> +<body> + +<p> + If you need BLAS or LAPACK to develop your own programs, the documentation + becomes pretty handy. Setting the USE="doc" flag for the corresponding BLAS or + LAPACK package will install man pages and quick reference sheets from the + <c>app-doc/blas-docs</c> and <c>app-doc/lapack-docs</c> packages. They are + standard and valid for all implementations. For optimized packages, the + USE="doc" flags will usually install extra doc in PDF or HTML format. +</p> + +</body> +</section> +</chapter> + +<chapter> +<title>For ebuild developers</title> +<section> + +<section> +<title>Packages with BLAS/LAPACK dependencies</title> +<body> + +<p> + You need two things: + set [R]DEPEND to <c>virtual/<imp></c>. To build some + packages, you m need to use the pkg-config tool. If you are lucky, the + package uses autotools together with the autoconf <>AX_BLAS and <>AX_LAPACK M4 + macros. In this case, the configuration step becomes simple. For example: +</p> + +<pre caption="Sample package configuration with autotools"> +<keyword>econf</keyword> --with-blas="<var>$(pkg-config --libs blas)</var>" +</pre> + +</body> +</section> + + + +<title>Providing new implementations</title> +<body> + +<p> + The Portage tree contains many ebuilds that depend on the + BLAS/CBLAS/LAPACK/BLACS/ScaLAPACK libraries. As there is more than + one possible implementation, the Gentoo Science Project + reorganized all the packages to provide <c>virtual</c>. All ebuilds using + should depend on this virtual package, unless it is explicitly + known to break with a specific implementation. +</p> + +<p> + To work with Gentoo's configuration tools + <c>app-admin/eselect-{blas,cblas,lapack}</c>, and the virtual, every ebuild that + installs a BLAS implementation must fulfill following requirements: +</p> + +<ol> +<li> + The ebuild must install an eselect file for each profile it provides. The + libraries should link to the ones in <path>/usr/$(get_libdir)</path> + directories and the include files in <path>/usr/include</path>: + <ul> + <li> + <path>libblas.so[.0]</path> - Shared object for FORTRAN BLAS + applications + </li> + <li> + <path>libblas.a</path> - Static library for FORTRAN BLAS applications + </li> + <li> + <path>libcblas.so[.0]</path> - Shared object for C/C++ CBLAS applications + </li> + <li> + <path>libcblas.a</path> - Static library for C/C++ CBLAS applications + </li> + <li><path>cblas.h</path> - Include header for C/C++ applications</li> + <li> + <path>liblapack.so[.0]</path> - Shared object for FORTRAN LAPACK + applications + </li> + <li> + <path>liblapack.a</path> - Static library for FORTRAN LAPACK applications + </li> + </ul> + </li> + <li> + The ebuild must install a <path>blas.pc</path>, <path>cblas.pc</path> and/or + <path>lapack.pc</path> pkg-config file and therefore RDEPEND on + <c>dev-util/pkgconfig</c>. They should also be included in the eselect + files, and link to the <path>/usr/$(get_libdir)/pkgconfig</path> directory: + <ul> + <li><path>blas.pc</path> - BLAS pkg-config file</li> + <li><path>cblas.pc</path> - CBLAS pkg-config file</li> + <li><path>lapack.pc</path> - LAPACK pkg-config file</li> + </ul> + </li> + <li>Be included in the virtual package as a possible provider: + <ul> + <li><c>virtual/blas</c> - BLAS virtual package</li> + <li><c>virtual/cblas</c> - CBLAS virtual package</li> + <li><c>virtual/lapack</c> - LAPACK virtual package</li> + </ul> + </li> +</ol> + +<p> + The easiest way of understanding all this is probably getting inspiration from + one of the available packages. Currently the Portage tree provide the following + virtual packages: +</p> + +<table> +<tr> + <th>Package name</th> + <th>virtual/blas</th> + <th>virtual/cblas</th> + <th>virtual/lapack</th> + <th>virtual/lapacke</th> + <th>virtual/blacs</th> + <th>virtual/scalapack</th> +</tr> +<tr> + <ti><c>sci-libs/acml</c></ti> + <ti>*</ti> + <ti></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/atlas</c></ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/gotoblas2</c></ti> + <ti>*</ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/blas-reference</c></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/cblas-reference</c></ti> + <ti></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/gsl</c></ti> + <ti></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/lapack-reference</c></ti> + <ti></ti> + <ti></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/mkl</c></ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> +</tr> +</table> + +</body> +</section> + +</chapter> + +<chapter> +<title>Benchmarks</title> +<section> +<body> + +<p> + If you feel inclined to write an ebuild for these, you + are more than welcomed to file it on our <uri + link="http://bugs.gentoo.org">Bugzilla</uri>. +</p> + +</body> +</section> +</chapter> + +</guide> diff --git a/docs/proj/linalg.xml b/docs/proj/linalg.xml new file mode 100644 index 000000000..9e83460f7 --- /dev/null +++ b/docs/proj/linalg.xml @@ -0,0 +1,550 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE guide SYSTEM "/dtd/guide.dtd"> +<!-- $Header: $ --> + +<guide link="/proj/en/science/linalg.xml"> +<title>Linear Algebra on Gentoo</title> + +<author title="Author"> + <mail link="bicatali@gentoo.org">Sébastien Fabbro</mail> +</author> + +<abstract> + This guide explains the use of linear algebra libraries and focus on + how to use the different implementations of BLAS and LAPACK available on Gentoo. +</abstract> + +<!-- The content of this document is licensed under the CC-BY-SA license --> +<!-- See http://creativecommons.org/licenses/by-sa/2.5 --> +<license/> + +<version>1.0</version> +<date>2010-12-22</date> + +<chapter> +<title>Introduction</title> +<section> +<body> + +<p> + There are <uri link="http://en.wikipedia.org/wiki/List_of_numerical_libraries">many</uri> + performant numerical libraries available. + The Basic Linear Algebra Subprograms (BLAS) and the Linear Algebra PACKage (LAPACK) + are well designed linear algebra libraries developed by the + High Performance Computing (HPC) community. BLAS is an API of dense + matrix and vectors products, while LAPACK provides routines for + solving systems of linear equations. Both are widely used in + many scientific applications and it is, therefore, important to + have efficient implementations available. +</p> + +<p> + BLAS and LAPACK were originally written in FORTRAN 77. Since then, a + number of additional language wrappers have been developed for + languages like C, C++, FORTRAN 95, Java, Python, etc... + Netlib offers exact implementations of the APIs and they are called + "reference" libraries. +</p> + +<ul> +<li> + <uri link="http://www.netlib.org/blas/">BLAS</uri>: FORTRAN 77 and C + (CBLAS) implementations of BLAS +</li> +<li> + <uri link="http://www.netlib.org/lapack/">LAPACK</uri>: FORTRAN 77 and + C (LAPACKE) implementations of LAPACK +</li> +</ul> + +<p> + +</p> + +<ul> +<li> + <uri link="http://www.netlib.org/blacs/">BLACS</uri>: FORTRAN 77 and C + implementations of BLACS +</li> +<li> + <uri link="http://www.netlib.org/scalapack/">ScaLAPACK</uri>: FORTRAN 77 and + C implementations of PBLAS and ScaLAPACK +</li> +</ul> + +<p> + In addition, Gentoo provides a number of optimized implementations + of the above linear algebra libraries that will be described + below. You can switch between implementations with the + Gentoo's <c>eselect</c> system and the widely used <c>pkg-config</c> + tool. +</p> + +<p> + It is important to note that if you require, e.g., a well performing + BLAS implementation, simply emerging X over Y often is not enough. Rather, you will have + to carefully benchmark your applications since performance may depend + on many factors, + such as hardware or network. + If you are simply looking for a well performing and well tested + implementation, the reference ebuilds will likely be your best choice. +</p> + + +</body> +</section> +</chapter> + +<chapter> +<title>For Users</title> +<section> +<title>Installing</title> +<body> + +<p> + If best possible performance is not of paramount importance for you + and you simply need BLAS and/or LAPACK, just emerge the virtual + package: +</p> + +<pre caption="Installing"> +# <i>emerge lapack</i> +</pre> + +<p> + This will install both <><> and <><> the reference packages from + <uri>http://www.netlib.org/</uri> . They are well tested, easy to debug + implementations. They should satisfy most users; if they're all you need, you're + done reading. +</p> + +<p> +However, if: +</p> + +<ul> + <li>linear algebra libraries are critical for the speed of your applications</li> + <li>you absolutely need to build the fastest computer</li> + <li>you want to help Gentoo sci project to improve their packages</li> +</ul> + +<p> +... then read on, and be sure to file bugs both to Gentoo and upstream. +</p> + +<p> + There is a number of optimized implementations of these libraries in the Portage + tree: +</p> + +<ul> + <li> + <uri link="http://math-atlas.sourceforge.net">ATLAS</uri>: Automatically + Tuned Linear Algebra Software is an open-source package that empirically + tunes the library to the machine it is being compiled on. It provides BLAS + (FORTRAN 77 and C), and LAPACK implementations on various architectures. + </li> + <li> + <uri + link="http://www.tacc.utexas.edu/tacc-projects/gotoblas2/">GotoBLAS</uri>: + Goto BLAS provides open-source, free for academic use, hand-coded + machine language, processor optimized versions of the FORTRAN 77 + and C BLAS routines. Still claims to be the fastest BLAS. + </li> + <li> + <uri link="http://developer.amd.com/cpu/libraries/acml/Pages/default.aspx">ACML</uri>: + AMD Core Math Library is a closed-source but free package containing BLAS (FORTRAN 77 + only) and LAPACK for x86 and x86_64 architectures, but also other math tools + such as statistical libraries and FFTs. + </li> + <li> + <uri link="http://software.intel.com/en-us/articles/intel-mkl/">MKL</uri>: + Intel® Math Kernel Library is a closed-source but free package for + non-commercial use on Linux systems containing implementations of all the linear + algebra libraries mentioned here. + </li> +</ul> + +<p> + Usually performance gain is noticeable mainly with BLAS, since LAPACK routines + depend on BLAS kernels. +</p> + +</body> +</section> + + +<section> +<title>Developping with the installed linear algebra libraries</title> +<body> + +<p> + We took great care to make sure that each package provides + consistent pkg-config files generated by us. + Compiling and linking becomes straightforward: +</p> + +<pre caption="Compiling and linking linear algebra libraries"> +# <i>pkg-config --libs blas</i> <comment>(To link with FORTRAN 77 BLAS library)</comment> +# <i>pkg-config --cflags cblas</i> <comment>(To compile against C BLAS library)</comment> +# <i>pkg-config --libs cblas</i> <comment>(To link with C BLAS library)</comment> +# <i>pkg-config --libs scalapack</i> <comment>(To link with the ScaLAPACK library)</comment> +</pre> + +<p> + <c>pkg-config</c> files are available for all implementations and + various alternatives within implementations. The default names of + the implementations are: blas, cblas, lapack, lapacke, blacs and + scalapack, and they can be chosen with <c>eselect</c>. You can also always compile or link + with an library not selected for the + More information on using <c>pkg-config</c> can be obtained with <c>man pkg-config</c>. +</p> + +</body> +</section> +<section> +<title>Selecting libraries</title> +<body> + +<p> + You can switch BLAS, CBLAS and LAPACK implementations with + <c>eselect</c>. you can view which implementations of CBLAS + are available. +</p> + +<pre caption="Viewing available implementations of CBLAS"> +# <i>eselect cblas list</i> +Installed CBLAS for library directory lib64 +[1] atlas +[2] atlas-threads +[3] gsl +[4] mkl-threads * +[5] reference +</pre> + +<p> + The implementation marked with an asterisk (*) is the currently + selected implementation. To switch implementations, run: +</p> + +<pre caption="Switching to the threaded ATLAS implementation of BLAS"> +# <i>eselect blas set atlas-threads</i> +</pre> + +<p> + To learn more about the <c>eselect</c> tool, visit the + <uri link="http://www.gentoo.org/proj/en/eselect/user-guide.xml">eselect guide</uri> +</p> + +<p> + When selecting your linear algebra profiles try to avoid mixing + different implementations since we don't have any mechanism to enforce + reasonable profiles. However, here is a list of well performing + profile combinations that have been used successfully in the past: +</p> +<ul> + <li> performant on most CPUs: + <ul> + <li>blas, cblas: atlas (or atlas-threads with multi-processor)</li> + <li>lapack, lapacke: atlas</li> + </ul> + </li> + <li> performant on most CPUs: + <ul> + <li>blas, cblas: goto2 </li> + <li>lapack, lapacke: reference</li> + </ul> + </li> + <li> performant on AMD based CPUs: + <ul> + <li>blas, lapack: acml-gfortran (or acml-gfortran-openmp with + multi-processors) </li> + <li>cblas: reference</li> + </ul> + </li> + <li> performant on Intel based CPUs: + <ul> + <li>blas,cblas,lapack: mkl-threads</li> + </ul> + </li> +</ul> + +</body> +</section> + +<section> +<title>Choosing a compiler</title> +<body> + +<p> + All the above libraries have been tested with the GNU compiler + collections (gcc, gfortran). + There are many available C compilers and a few FORTRAN (ifort, + Open64) compilers on Gentoo and many other FORTRAN compilers outside + of Gentoo (). +</p> + +<pre caption="Installing BLAS with the Intel FORTRAN compiler"> +# <i>F77=ifort FFLAGS="-O2 -mp1" emerge blas-reference</i> +</pre> + +<p> + Depending on your hardware, a small performance gain can be noticed thanks to + vectorization. The <c>-mp</c> flag maintains floating-point precision, since by + default ifort is pretty aggressive on floating point arithmetic, and we are + actually compiling a math package. Try <c>man ifort</c> to see additional flags + to fit your hardware. +</p> + +<p> + Some of the implementations let you specify the Intel® C compiler as + well. Please beware that not all libraries compile with all + combinations. You should receive an error during the emerge in case you have + chosen an incompatible combination. +</p> + +<p> + As usual for Gentoo, there are many combinations of USE flags and + compilers with which you could compile a package. Unfortunately + switching compilers between BLAS and LAPACK might not be always + compatible. For example: +</p> + +<pre caption="Looking for trouble combinations"> +# <i>USE=ifort emerge acml</i> +# <i>eselect blas set acml-ifort-openmp</i> +# <i>FC=gfortran FFLAGS="-O2" emerge lapack-reference</i> +</pre> + +<p> + This will most likely break things or not even compile. +</p> + +<p> + Try to be consistent in your choice. Stay with the GCC most of the time will + avoid you some trouble, unless you want to use the MKL, in which case the Intel + compilers make a good combination. +</p> + +</body> +</section> +<section> +<title>Documentation</title> +<body> + +<p> + If you need BLAS or LAPACK to develop your own programs, the documentation + becomes pretty handy. Setting the USE="doc" flag for the corresponding BLAS or + LAPACK package will install man pages and quick reference sheets from the + <c>app-doc/blas-docs</c> and <c>app-doc/lapack-docs</c> packages. They are + standard and valid for all implementations. For optimized packages, the + USE="doc" flags will usually install extra doc in PDF or HTML format. +</p> + +</body> +</section> +</chapter> + +<chapter> +<title>For ebuild developers</title> +<section> + +<section> +<title>Packages with BLAS/LAPACK dependencies</title> +<body> + +<p> + You need two things: + set [R]DEPEND to <c>virtual/<imp></c>. To build some + packages, you m need to use the pkg-config tool. If you are lucky, the + package uses autotools together with the autoconf <>AX_BLAS and <>AX_LAPACK M4 + macros. In this case, the configuration step becomes simple. For example: +</p> + +<pre caption="Sample package configuration with autotools"> +<keyword>econf</keyword> --with-blas="<var>$(pkg-config --libs blas)</var>" +</pre> + +</body> +</section> + + + +<title>Providing new implementations</title> +<body> + +<p> + The Portage tree contains many ebuilds that depend on the + BLAS/CBLAS/LAPACK/BLACS/ScaLAPACK libraries. As there is more than + one possible implementation, the Gentoo Science Project + reorganized all the packages to provide <c>virtual</c>. All ebuilds using + should depend on this virtual package, unless it is explicitly + known to break with a specific implementation. +</p> + +<p> + To work with Gentoo's configuration tools + <c>app-admin/eselect-{blas,cblas,lapack}</c>, and the virtual, every ebuild that + installs a BLAS implementation must fulfill following requirements: +</p> + +<ol> +<li> + The ebuild must install an eselect file for each profile it provides. The + libraries should link to the ones in <path>/usr/$(get_libdir)</path> + directories and the include files in <path>/usr/include</path>: + <ul> + <li> + <path>libblas.so[.0]</path> - Shared object for FORTRAN BLAS + applications + </li> + <li> + <path>libblas.a</path> - Static library for FORTRAN BLAS applications + </li> + <li> + <path>libcblas.so[.0]</path> - Shared object for C/C++ CBLAS applications + </li> + <li> + <path>libcblas.a</path> - Static library for C/C++ CBLAS applications + </li> + <li><path>cblas.h</path> - Include header for C/C++ applications</li> + <li> + <path>liblapack.so[.0]</path> - Shared object for FORTRAN LAPACK + applications + </li> + <li> + <path>liblapack.a</path> - Static library for FORTRAN LAPACK applications + </li> + </ul> + </li> + <li> + The ebuild must install a <path>blas.pc</path>, <path>cblas.pc</path> and/or + <path>lapack.pc</path> pkg-config file and therefore RDEPEND on + <c>dev-util/pkgconfig</c>. They should also be included in the eselect + files, and link to the <path>/usr/$(get_libdir)/pkgconfig</path> directory: + <ul> + <li><path>blas.pc</path> - BLAS pkg-config file</li> + <li><path>cblas.pc</path> - CBLAS pkg-config file</li> + <li><path>lapack.pc</path> - LAPACK pkg-config file</li> + </ul> + </li> + <li>Be included in the virtual package as a possible provider: + <ul> + <li><c>virtual/blas</c> - BLAS virtual package</li> + <li><c>virtual/cblas</c> - CBLAS virtual package</li> + <li><c>virtual/lapack</c> - LAPACK virtual package</li> + </ul> + </li> +</ol> + +<p> + The easiest way of understanding all this is probably getting inspiration from + one of the available packages. Currently the Portage tree provide the following + virtual packages: +</p> + +<table> +<tr> + <th>Package name</th> + <th>virtual/blas</th> + <th>virtual/cblas</th> + <th>virtual/lapack</th> + <th>virtual/lapacke</th> + <th>virtual/blacs</th> + <th>virtual/scalapack</th> +</tr> +<tr> + <ti><c>sci-libs/acml</c></ti> + <ti>*</ti> + <ti></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/atlas</c></ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/gotoblas2</c></ti> + <ti>*</ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/blas-reference</c></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/cblas-reference</c></ti> + <ti></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/gsl</c></ti> + <ti></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/lapack-reference</c></ti> + <ti></ti> + <ti></ti> + <ti>*</ti> + <ti></ti> + <ti></ti> + <ti></ti> +</tr> +<tr> + <ti><c>sci-libs/mkl</c></ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> + <ti>*</ti> +</tr> +</table> + +</body> +</section> + +</chapter> + +<chapter> +<title>Benchmarks</title> +<section> +<body> + +<p> + If you feel inclined to write an ebuild for these, you + are more than welcomed to file it on our <uri + link="http://bugs.gentoo.org">Bugzilla</uri>. +</p> + +</body> +</section> +</chapter> + +</guide> diff --git a/media-libs/avbin/Manifest b/media-libs/avbin/Manifest new file mode 100644 index 000000000..ad1e60821 --- /dev/null +++ b/media-libs/avbin/Manifest @@ -0,0 +1,6 @@ +AUX avbin-7-ffmpeg05.patch 2258 RMD160 60e6efe5a3f8de21fcb696e8890ac74fa93441b4 SHA1 d7d9851e390547ff58c8840661ff6aa89add90dd SHA256 3a57d76fe8bf03c2403826f055ab4fe664a4bf809206ea2c9d806661dc5aea28 +AUX avbin-7-sample24.patch 716 RMD160 cbbf7d5fb66085f2727a881154d37f59115e7e79 SHA1 97429af3d55cf718e14b5df62bc6514f4b080990 SHA256 e27bf10324381a72d1583ed40802e9abcd8cfa726538b06ef6590848751b5374 +AUX avbin-7-swscale.patch 1373 RMD160 3ff64b4ccd4caec909b737f286e492f116b039d0 SHA1 36c82bd7fa7a271c16c46006442b3fdc12fe5005 SHA256 d3de05256d312d7fd1546aafbe8bdc300a72b6ff7a2204b60ef99bad029adc75 +DIST avbin-src-7.tar.gz 63378 RMD160 4f81ead95159914e1eec16cbecd6e27d4a34f6ce SHA1 46a7e5ee68601e2053587d3248e7dfe81375aee1 SHA256 3f10835e599bed8c9831ce9a04b3ff3f65866cae67ae7010c9d8a10528c651a2 +EBUILD avbin-7.ebuild 1463 RMD160 39ac47e3a9902df3de33c6e3425e0a3089822d79 SHA1 277425186100e893159acc03fc199ff0cff69d99 SHA256 e9012741a85982f8ae718c30a10711a9c3569ef8bcd61f870244154483fa133b +MISC metadata.xml 322 RMD160 2f8aa3e8a63f8d433e70608ccb78642f787faf63 SHA1 dc9b2a3266621631cb514bfb61fe7b576f66f41e SHA256 0e951dde624ef818817a69689ab0cb6cf4d4251b3a4680a03edfa95e9997c8f8 diff --git a/media-libs/avbin/avbin-7.ebuild b/media-libs/avbin/avbin-7.ebuild new file mode 100644 index 000000000..a7f688592 --- /dev/null +++ b/media-libs/avbin/avbin-7.ebuild @@ -0,0 +1,58 @@ +# Copyright 1999-2009 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +EAPI=2 +inherit eutils toolchain-funcs + +MYP="${PN}-src-${PV}" + +DESCRIPTION="Thin wrapper around FFmpeg" +HOMEPAGE="http://code.google.com/p/avbin/" +SRC_URI="http://avbin.googlecode.com/files/${MYP}.tar.gz" + +LICENSE="|| ( GPL-3 LGPL-3 )" +SLOT="0" +KEYWORDS="~amd64 ~x86" +IUSE="doc" + +RDEPEND=">=media-video/ffmpeg-0.5" +DEPEND="${RDEPEND} + dev-util/pkgconfig + doc? ( app-doc/doxygen )" + +S="${WORKDIR}/${MYP}" + +src_prepare() { + # patch for ffmpeg-0.5 to work (suppress SAMPLE_FMT_S24) + epatch "${FILESDIR}"/${P}-ffmpeg05.patch +} + +src_compile() { + # ffmpeg_revision is taken from git repo at about ffmpeg-0.5 (harmless) + # makefile is not used + $(tc-getCC) ${CFLAGS} -fPIC \ + -DAVBIN_VERSION=${PV} -Iinclude \ + -DFFMPEG_REVISION=17762 \ + $(pkg-config --cflags libavformat libavcodec libavutil libswscale) \ + -c src/avbin.c -o avbin.o \ + || die "compiling avbin failed" + $(tc-getCC) ${LDFLAGS} -shared -Wl,-soname,libavbin.so.${PV} \ + avbin.o -o libavbin.so.${PV} \ + $(pkg-config --libs libavformat libavcodec libavutil libswscale) \ + || die "linking avbin failed" + ln -s libavbin.so.${PV} libavbin.so + if use doc; then + doxygen Doxyfile || die "doxygen failed" + fi +} + +src_install() { + dolib.so libavbin.so* || die + insinto /usr/include + doins include/avbin.h || die + dodoc CHANGELOG README + if use doc; then + dohtml doc/html/* || die + fi +} diff --git a/media-libs/avbin/files/avbin-7-ffmpeg05.patch b/media-libs/avbin/files/avbin-7-ffmpeg05.patch new file mode 100644 index 000000000..3049a49d1 --- /dev/null +++ b/media-libs/avbin/files/avbin-7-ffmpeg05.patch @@ -0,0 +1,66 @@ +--- src/avbin.c.orig 2009-04-02 12:09:58.000000000 +0100 ++++ src/avbin.c 2009-04-02 15:07:10.000000000 +0100 +@@ -28,6 +28,7 @@ + #include <libavformat/avformat.h> + #include <libavcodec/avcodec.h> + #include <libavutil/avutil.h> ++#include <libswscale/swscale.h> + + struct _AVbinFile { + AVFormatContext *context; +@@ -141,6 +142,7 @@ + free(file->packet); + } + av_close_input_file(file->context); ++ free(file); + } + + AVbinResult avbin_seek_file(AVbinFile *file, AVbinTimestamp timestamp) +@@ -215,10 +217,6 @@ + info->audio.sample_format = AVBIN_SAMPLE_FORMAT_S16; + info->audio.sample_bits = 16; + break; +- case SAMPLE_FMT_S24: +- info->audio.sample_format = AVBIN_SAMPLE_FORMAT_S24; +- info->audio.sample_bits = 24; +- break; + case SAMPLE_FMT_S32: + info->audio.sample_format = AVBIN_SAMPLE_FORMAT_S32; + info->audio.sample_bits = 32; +@@ -269,6 +267,7 @@ + if (stream->frame) + av_free(stream->frame); + avcodec_close(stream->codec_context); ++ free(stream); + } + + int avbin_read(AVbinFile *file, AVbinPacket *packet) +@@ -334,13 +333,21 @@ + + avpicture_fill(&picture_rgb, data_out, PIX_FMT_RGB24, width, height); + +- /* img_convert is marked deprecated in favour of swscale, don't +- * be surprised if this stops working the next time the ffmpeg version +- * is pushed. Example use of the new API is in ffplay.c. */ +- img_convert(&picture_rgb, PIX_FMT_RGB24, +- (AVPicture *) stream->frame, stream->codec_context->pix_fmt, +- width, height); +- ++ /* use swscale instead of deprecated img_convert */ ++ static struct SwsContext *img_convert_ctx = NULL; ++ ++ if (img_convert_ctx == NULL) { ++ img_convert_ctx = sws_getContext(width, height, ++ stream->codec_context->pix_fmt, ++ width, height, ++ PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); ++ } ++ ++ sws_scale(img_convert_ctx, stream->frame->data, stream->frame->linesize, ++ 0, height, picture_rgb.data, picture_rgb.linesize); ++ ++ /*sws_freeContext(img_convert_ctx);*/ ++ + return used; + } + diff --git a/media-libs/avbin/files/avbin-7-sample24.patch b/media-libs/avbin/files/avbin-7-sample24.patch new file mode 100644 index 000000000..ea10d709a --- /dev/null +++ b/media-libs/avbin/files/avbin-7-sample24.patch @@ -0,0 +1,15 @@ +--- src/avbin.c.orig 2009-01-15 13:49:29.000000000 -0500 ++++ src/avbin.c 2009-01-15 13:50:20.000000000 -0500 +@@ -216,10 +216,12 @@ int avbin_stream_info(AVbinFile *file, i + info->audio.sample_format = AVBIN_SAMPLE_FORMAT_S16; + info->audio.sample_bits = 16; + break; ++#if FFMPEG_REVISION < 15124 + case SAMPLE_FMT_S24: + info->audio.sample_format = AVBIN_SAMPLE_FORMAT_S24; + info->audio.sample_bits = 24; + break; ++#endif + case SAMPLE_FMT_S32: + info->audio.sample_format = AVBIN_SAMPLE_FORMAT_S32; + info->audio.sample_bits = 32; diff --git a/media-libs/avbin/files/avbin-7-swscale.patch b/media-libs/avbin/files/avbin-7-swscale.patch new file mode 100644 index 000000000..02cff3416 --- /dev/null +++ b/media-libs/avbin/files/avbin-7-swscale.patch @@ -0,0 +1,45 @@ +--- src/avbin.c.orig 2008-09-21 02:45:33.000000000 -0400 ++++ src/avbin.c 2008-11-22 20:32:48.000000000 -0500 +@@ -28,6 +28,7 @@ + #include <libavformat/avformat.h> + #include <libavcodec/avcodec.h> + #include <libavutil/avutil.h> ++#include <libswscale/swscale.h> + + struct _AVbinFile { + AVFormatContext *context; +@@ -334,9 +335,5 @@ int avbin_decode_video(AVbinStream *stre + + avpicture_fill(&picture_rgb, data_out, PIX_FMT_RGB24, width, height); + +- /* img_convert is marked deprecated in favour of swscale, don't +- * be surprised if this stops working the next time the ffmpeg version +- * is pushed. Example use of the new API is in ffplay.c. */ +- img_convert(&picture_rgb, PIX_FMT_RGB24, +- (AVPicture *) stream->frame, stream->codec_context->pix_fmt, +- width, height); ++ static int sws_flags = SWS_BICUBIC; ++ static struct SwsContext *img_convert_ctx; + ++ if (img_convert_ctx == NULL) { ++ img_convert_ctx = sws_getContext(width, ++ height, ++ stream->codec_context->pix_fmt, ++ width, ++ height, ++ PIX_FMT_RGB24, ++ sws_flags, NULL, NULL, NULL); ++ } ++ sws_scale(img_convert_ctx, ++ stream->frame->data, ++ stream->frame->linesize, ++ 0, ++ height, ++ picture_rgb.data, ++ picture_rgb.linesize); ++ ++ sws_freeContext(img_convert_ctx); ++ + return used; + } + diff --git a/media-libs/avbin/metadata.xml b/media-libs/avbin/metadata.xml new file mode 100644 index 000000000..12a08660f --- /dev/null +++ b/media-libs/avbin/metadata.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd"> +<pkgmetadata> +<herd>sci</herd> +<longdescription lang="en"> + AVbin is a thin wrapper around FFmpeg, providing binary compatibility + for applications and languages that need it. +</longdescription> +</pkgmetadata> diff --git a/sci-libs/levmar/Manifest b/sci-libs/levmar/Manifest new file mode 100644 index 000000000..aff611164 --- /dev/null +++ b/sci-libs/levmar/Manifest @@ -0,0 +1,2 @@ +DIST levmar-2.5.tgz 78817 RMD160 fc43206eef2969a143e3a9597b68d06eccbd14dd SHA1 46c11812689baba96f3b53cf89f0f3edb3582e61 SHA256 b70f6ac3eff30ec29150e217b137312cb84e85529815efea2c12e4eab74b9d75 +EBUILD levmar-2.5.ebuild 767 RMD160 fcfb1b860ba2f766a616e32124eb107be310e63d SHA1 1d30812114d6845ba057238102f91eb66526d5eb SHA256 331e2c710636a09d136cab0c312d8aac75b4f2f811cf315d1b0cd29aca451f38 diff --git a/sci-libs/levmar/levmar-2.5.ebuild b/sci-libs/levmar/levmar-2.5.ebuild new file mode 100644 index 000000000..d232eb453 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5.ebuild @@ -0,0 +1,36 @@ +# Copyright 1999-2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +EAPI=3 +inherit eutils + +DESCRIPTION="A C/C++ implementation of the Levenberg-Marquardt non-linear regression" +HOMEPAGE="http://www.ics.forth.gr/~lourakis/levmar/" +SRC_URI="${HOMEPAGE}/${P}.tgz" + +LICENSE="GPL-2" +SLOT="0" +KEYWORDS="~x86" +IUSE="lapack" +RDEPEND="lapack? ( virtual/lapack )" +DEPEND="${RDEPEND} + dev-util/pkgconfig" + +src_prepare() { + sed -e "s:-O3:${CFLAGS}:" -e 's:^LAPACKLIBS_PATH.*$:LAPACKLIBS=/usr/lib:' \ + -i Makefile + if ! use lapack; then + sed -e 's:^LAPACKFLAG:#LAPACKFLAG:' \ + -e 's:^LAPACKLIBS:#LAPACKLIBS:' \ + -i Makefile + fi + +} + +src_install() { + dolib liblevmar.a + insinto /usr/include + doins lm.h + dodoc README.txt +} diff --git a/sci-libs/levmar/levmar-2.5/Axb.c b/sci-libs/levmar/levmar-2.5/Axb.c new file mode 100644 index 000000000..7fce7c276 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/Axb.c @@ -0,0 +1,74 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Solution of linear systems involved in the Levenberg - Marquardt +// minimization algorithm +// Copyright (C) 2004 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************** + * LAPACK-based implementations for various linear system solvers. The same core + * code is used with appropriate #defines to derive single and double precision + * solver versions, see also Axb_core.c + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +#include "levmar.h" +#include "misc.h" + +#if !defined(LM_DBL_PREC) && !defined(LM_SNGL_PREC) +#error At least one of LM_DBL_PREC, LM_SNGL_PREC should be defined! +#endif + + +#ifdef LM_DBL_PREC +/* double precision definitions */ +#define LM_REAL double +#define LM_PREFIX d +#define LM_CNST(x) (x) +#ifndef HAVE_LAPACK +#include <float.h> +#define LM_REAL_EPSILON DBL_EPSILON +#endif + +#include "Axb_core.c" + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_CNST +#undef LM_REAL_EPSILON +#endif /* LM_DBL_PREC */ + +#ifdef LM_SNGL_PREC +/* single precision (float) definitions */ +#define LM_REAL float +#define LM_PREFIX s +#define __SUBCNST(x) x##F +#define LM_CNST(x) __SUBCNST(x) // force substitution +#ifndef HAVE_LAPACK +#define LM_REAL_EPSILON FLT_EPSILON +#endif + +#include "Axb_core.c" + +#undef LM_REAL +#undef LM_PREFIX +#undef __SUBCNST +#undef LM_CNST +#undef LM_REAL_EPSILON +#endif /* LM_SNGL_PREC */ diff --git a/sci-libs/levmar/levmar-2.5/Axb.o b/sci-libs/levmar/levmar-2.5/Axb.o Binary files differnew file mode 100644 index 000000000..51384256f --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/Axb.o diff --git a/sci-libs/levmar/levmar-2.5/Axb_core.c b/sci-libs/levmar/levmar-2.5/Axb_core.c new file mode 100644 index 000000000..edeeac3b6 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/Axb_core.c @@ -0,0 +1,1144 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Solution of linear systems involved in the Levenberg - Marquardt +// minimization algorithm +// Copyright (C) 2004 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + + +/* Solvers for the linear systems Ax=b. Solvers should NOT modify their A & B arguments! */ + + +#ifndef LM_REAL // not included by Axb.c +#error This file should not be compiled directly! +#endif + + +#ifdef LINSOLVERS_RETAIN_MEMORY +#define __STATIC__ static +#else +#define __STATIC__ // empty +#endif /* LINSOLVERS_RETAIN_MEMORY */ + +#ifdef HAVE_LAPACK + +/* prototypes of LAPACK routines */ + +#define GEQRF LM_MK_LAPACK_NAME(geqrf) +#define ORGQR LM_MK_LAPACK_NAME(orgqr) +#define TRTRS LM_MK_LAPACK_NAME(trtrs) +#define POTF2 LM_MK_LAPACK_NAME(potf2) +#define POTRF LM_MK_LAPACK_NAME(potrf) +#define POTRS LM_MK_LAPACK_NAME(potrs) +#define GETRF LM_MK_LAPACK_NAME(getrf) +#define GETRS LM_MK_LAPACK_NAME(getrs) +#define GESVD LM_MK_LAPACK_NAME(gesvd) +#define GESDD LM_MK_LAPACK_NAME(gesdd) +#define SYTRF LM_MK_LAPACK_NAME(sytrf) +#define SYTRS LM_MK_LAPACK_NAME(sytrs) + +/* QR decomposition */ +extern int GEQRF(int *m, int *n, LM_REAL *a, int *lda, LM_REAL *tau, LM_REAL *work, int *lwork, int *info); +extern int ORGQR(int *m, int *n, int *k, LM_REAL *a, int *lda, LM_REAL *tau, LM_REAL *work, int *lwork, int *info); + +/* solution of triangular systems */ +extern int TRTRS(char *uplo, char *trans, char *diag, int *n, int *nrhs, LM_REAL *a, int *lda, LM_REAL *b, int *ldb, int *info); + +/* Cholesky decomposition and systems solution */ +extern int POTF2(char *uplo, int *n, LM_REAL *a, int *lda, int *info); +extern int POTRF(char *uplo, int *n, LM_REAL *a, int *lda, int *info); /* block version of dpotf2 */ +extern int POTRS(char *uplo, int *n, int *nrhs, LM_REAL *a, int *lda, LM_REAL *b, int *ldb, int *info); + +/* LU decomposition and systems solution */ +extern int GETRF(int *m, int *n, LM_REAL *a, int *lda, int *ipiv, int *info); +extern int GETRS(char *trans, int *n, int *nrhs, LM_REAL *a, int *lda, int *ipiv, LM_REAL *b, int *ldb, int *info); + +/* Singular Value Decomposition (SVD) */ +extern int GESVD(char *jobu, char *jobvt, int *m, int *n, LM_REAL *a, int *lda, LM_REAL *s, LM_REAL *u, int *ldu, + LM_REAL *vt, int *ldvt, LM_REAL *work, int *lwork, int *info); + +/* lapack 3.0 new SVD routine, faster than xgesvd(). + * In case that your version of LAPACK does not include them, use the above two older routines + */ +extern int GESDD(char *jobz, int *m, int *n, LM_REAL *a, int *lda, LM_REAL *s, LM_REAL *u, int *ldu, LM_REAL *vt, int *ldvt, + LM_REAL *work, int *lwork, int *iwork, int *info); + +/* LDLt/UDUt factorization and systems solution */ +extern int SYTRF(char *uplo, int *n, LM_REAL *a, int *lda, int *ipiv, LM_REAL *work, int *lwork, int *info); +extern int SYTRS(char *uplo, int *n, int *nrhs, LM_REAL *a, int *lda, int *ipiv, LM_REAL *b, int *ldb, int *info); + +/* precision-specific definitions */ +#define AX_EQ_B_QR LM_ADD_PREFIX(Ax_eq_b_QR) +#define AX_EQ_B_QRLS LM_ADD_PREFIX(Ax_eq_b_QRLS) +#define AX_EQ_B_CHOL LM_ADD_PREFIX(Ax_eq_b_Chol) +#define AX_EQ_B_LU LM_ADD_PREFIX(Ax_eq_b_LU) +#define AX_EQ_B_SVD LM_ADD_PREFIX(Ax_eq_b_SVD) +#define AX_EQ_B_BK LM_ADD_PREFIX(Ax_eq_b_BK) + +/* + * This function returns the solution of Ax = b + * + * The function is based on QR decomposition with explicit computation of Q: + * If A=Q R with Q orthogonal and R upper triangular, the linear system becomes + * Q R x = b or R x = Q^T b. + * The last equation can be solved directly. + * + * A is mxm, b is mx1 + * + * The function returns 0 in case of error, 1 if successful + * + * This function is often called repetitively to solve problems of identical + * dimensions. To avoid repetitive malloc's and free's, allocated memory is + * retained between calls and free'd-malloc'ed when not of the appropriate size. + * A call with NULL as the first argument forces this memory to be released. + */ +int AX_EQ_B_QR(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m) +{ +__STATIC__ LM_REAL *buf=NULL; +__STATIC__ int buf_sz=0; + +static int nb=0; /* no __STATIC__ decl. here! */ + +LM_REAL *a, *tau, *r, *work; +int a_sz, tau_sz, r_sz, tot_sz; +register int i, j; +int info, worksz, nrhs=1; +register LM_REAL sum; + + if(!A) +#ifdef LINSOLVERS_RETAIN_MEMORY + { + if(buf) free(buf); + buf=NULL; + buf_sz=0; + + return 1; + } +#else + return 1; /* NOP */ +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + /* calculate required memory size */ + a_sz=m*m; + tau_sz=m; + r_sz=m*m; /* only the upper triangular part really needed */ + if(!nb){ + LM_REAL tmp; + + worksz=-1; // workspace query; optimal size is returned in tmp + GEQRF((int *)&m, (int *)&m, NULL, (int *)&m, NULL, (LM_REAL *)&tmp, (int *)&worksz, (int *)&info); + nb=((int)tmp)/m; // optimal worksize is m*nb + } + worksz=nb*m; + tot_sz=a_sz + tau_sz + r_sz + worksz; + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */ + if(buf) free(buf); /* free previously allocated memory */ + + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz*sizeof(LM_REAL)); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_QR) "() failed!\n"); + exit(1); + } + } +#else + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz*sizeof(LM_REAL)); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_QR) "() failed!\n"); + exit(1); + } +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + a=buf; + tau=a+a_sz; + r=tau+tau_sz; + work=r+r_sz; + + /* store A (column major!) into a */ + for(i=0; i<m; i++) + for(j=0; j<m; j++) + a[i+j*m]=A[i*m+j]; + + /* QR decomposition of A */ + GEQRF((int *)&m, (int *)&m, a, (int *)&m, tau, work, (int *)&worksz, (int *)&info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", GEQRF) " in ", AX_EQ_B_QR) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT(RCAT("Unknown LAPACK error %d for ", GEQRF) " in ", AX_EQ_B_QR) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + /* R is stored in the upper triangular part of a; copy it in r so that ORGQR() below won't destroy it */ + for(i=0; i<r_sz; i++) + r[i]=a[i]; + + /* compute Q using the elementary reflectors computed by the above decomposition */ + ORGQR((int *)&m, (int *)&m, (int *)&m, a, (int *)&m, tau, work, (int *)&worksz, (int *)&info); + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", ORGQR) " in ", AX_EQ_B_QR) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT("Unknown LAPACK error (%d) in ", AX_EQ_B_QR) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + /* Q is now in a; compute Q^T b in x */ + for(i=0; i<m; i++){ + for(j=0, sum=0.0; j<m; j++) + sum+=a[i*m+j]*B[j]; + x[i]=sum; + } + + /* solve the linear system R x = Q^t b */ + TRTRS("U", "N", "N", (int *)&m, (int *)&nrhs, r, (int *)&m, x, (int *)&m, &info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", TRTRS) " in ", AX_EQ_B_QR) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT("LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in ", AX_EQ_B_QR) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 1; +} + +/* + * This function returns the solution of min_x ||Ax - b|| + * + * || . || is the second order (i.e. L2) norm. This is a least squares technique that + * is based on QR decomposition: + * If A=Q R with Q orthogonal and R upper triangular, the normal equations become + * (A^T A) x = A^T b or (R^T Q^T Q R) x = A^T b or (R^T R) x = A^T b. + * This amounts to solving R^T y = A^T b for y and then R x = y for x + * Note that Q does not need to be explicitly computed + * + * A is mxn, b is mx1 + * + * The function returns 0 in case of error, 1 if successful + * + * This function is often called repetitively to solve problems of identical + * dimensions. To avoid repetitive malloc's and free's, allocated memory is + * retained between calls and free'd-malloc'ed when not of the appropriate size. + * A call with NULL as the first argument forces this memory to be released. + */ +int AX_EQ_B_QRLS(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m, int n) +{ +__STATIC__ LM_REAL *buf=NULL; +__STATIC__ int buf_sz=0; + +static int nb=0; /* no __STATIC__ decl. here! */ + +LM_REAL *a, *tau, *r, *work; +int a_sz, tau_sz, r_sz, tot_sz; +register int i, j; +int info, worksz, nrhs=1; +register LM_REAL sum; + + if(!A) +#ifdef LINSOLVERS_RETAIN_MEMORY + { + if(buf) free(buf); + buf=NULL; + buf_sz=0; + + return 1; + } +#else + return 1; /* NOP */ +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + if(m<n){ + fprintf(stderr, RCAT("Normal equations require that the number of rows is greater than number of columns in ", AX_EQ_B_QRLS) "() [%d x %d]! -- try transposing\n", m, n); + exit(1); + } + + /* calculate required memory size */ + a_sz=m*n; + tau_sz=n; + r_sz=n*n; + if(!nb){ + LM_REAL tmp; + + worksz=-1; // workspace query; optimal size is returned in tmp + GEQRF((int *)&m, (int *)&m, NULL, (int *)&m, NULL, (LM_REAL *)&tmp, (int *)&worksz, (int *)&info); + nb=((int)tmp)/m; // optimal worksize is m*nb + } + worksz=nb*m; + tot_sz=a_sz + tau_sz + r_sz + worksz; + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */ + if(buf) free(buf); /* free previously allocated memory */ + + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz*sizeof(LM_REAL)); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_QRLS) "() failed!\n"); + exit(1); + } + } +#else + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz*sizeof(LM_REAL)); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_QRLS) "() failed!\n"); + exit(1); + } +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + a=buf; + tau=a+a_sz; + r=tau+tau_sz; + work=r+r_sz; + + /* store A (column major!) into a */ + for(i=0; i<m; i++) + for(j=0; j<n; j++) + a[i+j*m]=A[i*n+j]; + + /* compute A^T b in x */ + for(i=0; i<n; i++){ + for(j=0, sum=0.0; j<m; j++) + sum+=A[j*n+i]*B[j]; + x[i]=sum; + } + + /* QR decomposition of A */ + GEQRF((int *)&m, (int *)&n, a, (int *)&m, tau, work, (int *)&worksz, (int *)&info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", GEQRF) " in ", AX_EQ_B_QRLS) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT(RCAT("Unknown LAPACK error %d for ", GEQRF) " in ", AX_EQ_B_QRLS) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + /* R is stored in the upper triangular part of a. Note that a is mxn while r nxn */ + for(j=0; j<n; j++){ + for(i=0; i<=j; i++) + r[i+j*n]=a[i+j*m]; + + /* lower part is zero */ + for(i=j+1; i<n; i++) + r[i+j*n]=0.0; + } + + /* solve the linear system R^T y = A^t b */ + TRTRS("U", "T", "N", (int *)&n, (int *)&nrhs, r, (int *)&n, x, (int *)&n, &info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", TRTRS) " in ", AX_EQ_B_QRLS) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT("LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in ", AX_EQ_B_QRLS) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + /* solve the linear system R x = y */ + TRTRS("U", "N", "N", (int *)&n, (int *)&nrhs, r, (int *)&n, x, (int *)&n, &info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", TRTRS) " in ", AX_EQ_B_QRLS) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT("LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in ", AX_EQ_B_QRLS) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 1; +} + +/* + * This function returns the solution of Ax=b + * + * The function assumes that A is symmetric & postive definite and employs + * the Cholesky decomposition: + * If A=U^T U with U upper triangular, the system to be solved becomes + * (U^T U) x = b + * This amount to solving U^T y = b for y and then U x = y for x + * + * A is mxm, b is mx1 + * + * The function returns 0 in case of error, 1 if successful + * + * This function is often called repetitively to solve problems of identical + * dimensions. To avoid repetitive malloc's and free's, allocated memory is + * retained between calls and free'd-malloc'ed when not of the appropriate size. + * A call with NULL as the first argument forces this memory to be released. + */ +int AX_EQ_B_CHOL(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m) +{ +__STATIC__ LM_REAL *buf=NULL; +__STATIC__ int buf_sz=0; + +LM_REAL *a; +int a_sz, tot_sz; +register int i; +int info, nrhs=1; + + if(!A) +#ifdef LINSOLVERS_RETAIN_MEMORY + { + if(buf) free(buf); + buf=NULL; + buf_sz=0; + + return 1; + } +#else + return 1; /* NOP */ +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + /* calculate required memory size */ + a_sz=m*m; + tot_sz=a_sz; + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */ + if(buf) free(buf); /* free previously allocated memory */ + + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz*sizeof(LM_REAL)); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_CHOL) "() failed!\n"); + exit(1); + } + } +#else + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz*sizeof(LM_REAL)); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_CHOL) "() failed!\n"); + exit(1); + } +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + a=buf; + + /* store A into a and B into x. A is assumed symmetric, + * hence no transposition is needed + */ + for(i=0; i<m; i++){ + a[i]=A[i]; + x[i]=B[i]; + } + for(i=m; i<m*m; i++) + a[i]=A[i]; + + /* Cholesky decomposition of A */ + //POTF2("U", (int *)&m, a, (int *)&m, (int *)&info); + POTRF("U", (int *)&m, a, (int *)&m, (int *)&info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT(RCAT("LAPACK error: illegal value for argument %d of ", POTF2) "/", POTRF) " in ", + AX_EQ_B_CHOL) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT(RCAT(RCAT("LAPACK error: the leading minor of order %d is not positive definite,\nthe factorization could not be completed for ", POTF2) "/", POTRF) " in ", AX_EQ_B_CHOL) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + /* solve using the computed Cholesky in one lapack call */ + POTRS("U", (int *)&m, (int *)&nrhs, a, (int *)&m, x, (int *)&m, &info); + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", POTRS) " in ", AX_EQ_B_CHOL) "()\n", -info); + exit(1); + } + +#if 0 + /* alternative: solve the linear system U^T y = b ... */ + TRTRS("U", "T", "N", (int *)&m, (int *)&nrhs, a, (int *)&m, x, (int *)&m, &info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", TRTRS) " in ", AX_EQ_B_CHOL) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT("LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in ", AX_EQ_B_CHOL) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + /* ... solve the linear system U x = y */ + TRTRS("U", "N", "N", (int *)&m, (int *)&nrhs, a, (int *)&m, x, (int *)&m, &info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", TRTRS) "in ", AX_EQ_B_CHOL) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT("LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in ", AX_EQ_B_CHOL) "()\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } +#endif /* 0 */ + +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 1; +} + +/* + * This function returns the solution of Ax = b + * + * The function employs LU decomposition: + * If A=L U with L lower and U upper triangular, then the original system + * amounts to solving + * L y = b, U x = y + * + * A is mxm, b is mx1 + * + * The function returns 0 in case of error, 1 if successful + * + * This function is often called repetitively to solve problems of identical + * dimensions. To avoid repetitive malloc's and free's, allocated memory is + * retained between calls and free'd-malloc'ed when not of the appropriate size. + * A call with NULL as the first argument forces this memory to be released. + */ +int AX_EQ_B_LU(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m) +{ +__STATIC__ LM_REAL *buf=NULL; +__STATIC__ int buf_sz=0; + +int a_sz, ipiv_sz, tot_sz; +register int i, j; +int info, *ipiv, nrhs=1; +LM_REAL *a; + + if(!A) +#ifdef LINSOLVERS_RETAIN_MEMORY + { + if(buf) free(buf); + buf=NULL; + buf_sz=0; + + return 1; + } +#else + return 1; /* NOP */ +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + /* calculate required memory size */ + ipiv_sz=m; + a_sz=m*m; + tot_sz=a_sz*sizeof(LM_REAL) + ipiv_sz*sizeof(int); /* should be arranged in that order for proper doubles alignment */ + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */ + if(buf) free(buf); /* free previously allocated memory */ + + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_LU) "() failed!\n"); + exit(1); + } + } +#else + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_LU) "() failed!\n"); + exit(1); + } +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + a=buf; + ipiv=(int *)(a+a_sz); + + /* store A (column major!) into a and B into x */ + for(i=0; i<m; i++){ + for(j=0; j<m; j++) + a[i+j*m]=A[i*m+j]; + + x[i]=B[i]; + } + + /* LU decomposition for A */ + GETRF((int *)&m, (int *)&m, a, (int *)&m, ipiv, (int *)&info); + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("argument %d of ", GETRF) " illegal in ", AX_EQ_B_LU) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT(RCAT("singular matrix A for ", GETRF) " in ", AX_EQ_B_LU) "()\n"); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + /* solve the system with the computed LU */ + GETRS("N", (int *)&m, (int *)&nrhs, a, (int *)&m, ipiv, x, (int *)&m, (int *)&info); + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("argument %d of ", GETRS) " illegal in ", AX_EQ_B_LU) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT(RCAT("unknown error for ", GETRS) " in ", AX_EQ_B_LU) "()\n"); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 1; +} + +/* + * This function returns the solution of Ax = b + * + * The function is based on SVD decomposition: + * If A=U D V^T with U, V orthogonal and D diagonal, the linear system becomes + * (U D V^T) x = b or x=V D^{-1} U^T b + * Note that V D^{-1} U^T is the pseudoinverse A^+ + * + * A is mxm, b is mx1. + * + * The function returns 0 in case of error, 1 if successful + * + * This function is often called repetitively to solve problems of identical + * dimensions. To avoid repetitive malloc's and free's, allocated memory is + * retained between calls and free'd-malloc'ed when not of the appropriate size. + * A call with NULL as the first argument forces this memory to be released. + */ +int AX_EQ_B_SVD(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m) +{ +__STATIC__ LM_REAL *buf=NULL; +__STATIC__ int buf_sz=0; +static LM_REAL eps=LM_CNST(-1.0); + +register int i, j; +LM_REAL *a, *u, *s, *vt, *work; +int a_sz, u_sz, s_sz, vt_sz, tot_sz; +LM_REAL thresh, one_over_denom; +register LM_REAL sum; +int info, rank, worksz, *iwork, iworksz; + + if(!A) +#ifdef LINSOLVERS_RETAIN_MEMORY + { + if(buf) free(buf); + buf=NULL; + buf_sz=0; + + return 1; + } +#else + return 1; /* NOP */ +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + /* calculate required memory size */ +#if 1 /* use optimal size */ + worksz=-1; // workspace query. Keep in mind that GESDD requires more memory than GESVD + /* note that optimal work size is returned in thresh */ + GESVD("A", "A", (int *)&m, (int *)&m, NULL, (int *)&m, NULL, NULL, (int *)&m, NULL, (int *)&m, (LM_REAL *)&thresh, (int *)&worksz, &info); + //GESDD("A", (int *)&m, (int *)&m, NULL, (int *)&m, NULL, NULL, (int *)&m, NULL, (int *)&m, (LM_REAL *)&thresh, (int *)&worksz, NULL, &info); + worksz=(int)thresh; +#else /* use minimum size */ + worksz=5*m; // min worksize for GESVD + //worksz=m*(7*m+4); // min worksize for GESDD +#endif + iworksz=8*m; + a_sz=m*m; + u_sz=m*m; s_sz=m; vt_sz=m*m; + + tot_sz=(a_sz + u_sz + s_sz + vt_sz + worksz)*sizeof(LM_REAL) + iworksz*sizeof(int); /* should be arranged in that order for proper doubles alignment */ + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */ + if(buf) free(buf); /* free previously allocated memory */ + + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_SVD) "() failed!\n"); + exit(1); + } + } +#else + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_SVD) "() failed!\n"); + exit(1); + } +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + a=buf; + u=a+a_sz; + s=u+u_sz; + vt=s+s_sz; + work=vt+vt_sz; + iwork=(int *)(work+worksz); + + /* store A (column major!) into a */ + for(i=0; i<m; i++) + for(j=0; j<m; j++) + a[i+j*m]=A[i*m+j]; + + /* SVD decomposition of A */ + GESVD("A", "A", (int *)&m, (int *)&m, a, (int *)&m, s, u, (int *)&m, vt, (int *)&m, work, (int *)&worksz, &info); + //GESDD("A", (int *)&m, (int *)&m, a, (int *)&m, s, u, (int *)&m, vt, (int *)&m, work, (int *)&worksz, iwork, &info); + + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT(RCAT("LAPACK error: illegal value for argument %d of ", GESVD), "/" GESDD) " in ", AX_EQ_B_SVD) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT("LAPACK error: dgesdd (dbdsdc)/dgesvd (dbdsqr) failed to converge in ", AX_EQ_B_SVD) "() [info=%d]\n", info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + if(eps<0.0){ + LM_REAL aux; + + /* compute machine epsilon */ + for(eps=LM_CNST(1.0); aux=eps+LM_CNST(1.0), aux-LM_CNST(1.0)>0.0; eps*=LM_CNST(0.5)) + ; + eps*=LM_CNST(2.0); + } + + /* compute the pseudoinverse in a */ + for(i=0; i<a_sz; i++) a[i]=0.0; /* initialize to zero */ + for(rank=0, thresh=eps*s[0]; rank<m && s[rank]>thresh; rank++){ + one_over_denom=LM_CNST(1.0)/s[rank]; + + for(j=0; j<m; j++) + for(i=0; i<m; i++) + a[i*m+j]+=vt[rank+i*m]*u[j+rank*m]*one_over_denom; + } + + /* compute A^+ b in x */ + for(i=0; i<m; i++){ + for(j=0, sum=0.0; j<m; j++) + sum+=a[i*m+j]*B[j]; + x[i]=sum; + } + +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 1; +} + +/* + * This function returns the solution of Ax = b for a real symmetric matrix A + * + * The function is based on UDUT factorization with the pivoting + * strategy of Bunch and Kaufman: + * A is factored as U*D*U^T where U is upper triangular and + * D symmetric and block diagonal (aka spectral decomposition, + * Banachiewicz factorization, modified Cholesky factorization) + * + * A is mxm, b is mx1. + * + * The function returns 0 in case of error, 1 if successfull + * + * This function is often called repetitively to solve problems of identical + * dimensions. To avoid repetitive malloc's and free's, allocated memory is + * retained between calls and free'd-malloc'ed when not of the appropriate size. + * A call with NULL as the first argument forces this memory to be released. + */ +int AX_EQ_B_BK(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m) +{ +__STATIC__ LM_REAL *buf=NULL; +__STATIC__ int buf_sz=0, nb=0; + +LM_REAL *a, *work; +int a_sz, ipiv_sz, work_sz, tot_sz; +register int i, j; +int info, *ipiv, nrhs=1; + + if(!A) +#ifdef LINSOLVERS_RETAIN_MEMORY + { + if(buf) free(buf); + buf=NULL; + buf_sz=0; + + return 1; + } +#else + return 1; /* NOP */ +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + /* calculate required memory size */ + ipiv_sz=m; + a_sz=m*m; + if(!nb){ + LM_REAL tmp; + + work_sz=-1; // workspace query; optimal size is returned in tmp + SYTRF("U", (int *)&m, NULL, (int *)&m, NULL, (LM_REAL *)&tmp, (int *)&work_sz, (int *)&info); + nb=((int)tmp)/m; // optimal worksize is m*nb + } + work_sz=(nb!=-1)? nb*m : 1; + tot_sz=(a_sz + work_sz)*sizeof(LM_REAL) + ipiv_sz*sizeof(int); /* should be arranged in that order for proper doubles alignment */ + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */ + if(buf) free(buf); /* free previously allocated memory */ + + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_BK) "() failed!\n"); + exit(1); + } + } +#else + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_BK) "() failed!\n"); + exit(1); + } +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + a=buf; + work=a+a_sz; + ipiv=(int *)(work+work_sz); + + /* store A into a and B into x; A is assumed to be symmetric, hence + * the column and row major order representations are the same + */ + for(i=0; i<m; ++i){ + a[i]=A[i]; + x[i]=B[i]; + } + for(j=m*m; i<j; ++i) // copy remaining rows; note that i is not re-initialized + a[i]=A[i]; + + /* UDUt factorization for A */ + SYTRF("U", (int *)&m, a, (int *)&m, ipiv, work, (int *)&work_sz, (int *)&info); + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", SYTRF) " in ", AX_EQ_B_BK) "()\n", -info); + exit(1); + } + else{ + fprintf(stderr, RCAT(RCAT("LAPACK error: singular block diagonal matrix D for", SYTRF) " in ", AX_EQ_B_BK)"() [D(%d, %d) is zero]\n", info, info); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + } + + /* solve the system with the computed factorization */ + SYTRS("U", (int *)&m, (int *)&nrhs, a, (int *)&m, ipiv, x, (int *)&m, (int *)&info); + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", SYTRS) " in ", AX_EQ_B_BK) "()\n", -info); + exit(1); + } + +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 1; +} + +/* undefine all. IT MUST REMAIN IN THIS POSITION IN FILE */ +#undef AX_EQ_B_QR +#undef AX_EQ_B_QRLS +#undef AX_EQ_B_CHOL +#undef AX_EQ_B_LU +#undef AX_EQ_B_SVD +#undef AX_EQ_B_BK + +#undef GEQRF +#undef ORGQR +#undef TRTRS +#undef POTF2 +#undef POTRF +#undef POTRS +#undef GETRF +#undef GETRS +#undef GESVD +#undef GESDD +#undef SYTRF +#undef SYTRS + +#else // no LAPACK + +/* precision-specific definitions */ +#define AX_EQ_B_LU LM_ADD_PREFIX(Ax_eq_b_LU_noLapack) + +/* + * This function returns the solution of Ax = b + * + * The function employs LU decomposition followed by forward/back substitution (see + * also the LAPACK-based LU solver above) + * + * A is mxm, b is mx1 + * + * The function returns 0 in case of error, 1 if successful + * + * This function is often called repetitively to solve problems of identical + * dimensions. To avoid repetitive malloc's and free's, allocated memory is + * retained between calls and free'd-malloc'ed when not of the appropriate size. + * A call with NULL as the first argument forces this memory to be released. + */ +int AX_EQ_B_LU(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m) +{ +__STATIC__ void *buf=NULL; +__STATIC__ int buf_sz=0; + +register int i, j, k; +int *idx, maxi=-1, idx_sz, a_sz, work_sz, tot_sz; +LM_REAL *a, *work, max, sum, tmp; + + if(!A) +#ifdef LINSOLVERS_RETAIN_MEMORY + { + if(buf) free(buf); + buf=NULL; + buf_sz=0; + + return 1; + } +#else + return 1; /* NOP */ +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + /* calculate required memory size */ + idx_sz=m; + a_sz=m*m; + work_sz=m; + tot_sz=(a_sz+work_sz)*sizeof(LM_REAL) + idx_sz*sizeof(int); /* should be arranged in that order for proper doubles alignment */ + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */ + if(buf) free(buf); /* free previously allocated memory */ + + buf_sz=tot_sz; + buf=(void *)malloc(tot_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_LU) "() failed!\n"); + exit(1); + } + } +#else + buf_sz=tot_sz; + buf=(void *)malloc(tot_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", AX_EQ_B_LU) "() failed!\n"); + exit(1); + } +#endif /* LINSOLVERS_RETAIN_MEMORY */ + + a=buf; + work=a+a_sz; + idx=(int *)(work+work_sz); + + /* avoid destroying A, B by copying them to a, x resp. */ + for(i=0; i<m; ++i){ // B & 1st row of A + a[i]=A[i]; + x[i]=B[i]; + } + for( ; i<a_sz; ++i) a[i]=A[i]; // copy A's remaining rows + /**** + for(i=0; i<m; ++i){ + for(j=0; j<m; ++j) + a[i*m+j]=A[i*m+j]; + x[i]=B[i]; + } + ****/ + + /* compute the LU decomposition of a row permutation of matrix a; the permutation itself is saved in idx[] */ + for(i=0; i<m; ++i){ + max=0.0; + for(j=0; j<m; ++j) + if((tmp=FABS(a[i*m+j]))>max) + max=tmp; + if(max==0.0){ + fprintf(stderr, RCAT("Singular matrix A in ", AX_EQ_B_LU) "()!\n"); +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 0; + } + work[i]=LM_CNST(1.0)/max; + } + + for(j=0; j<m; ++j){ + for(i=0; i<j; ++i){ + sum=a[i*m+j]; + for(k=0; k<i; ++k) + sum-=a[i*m+k]*a[k*m+j]; + a[i*m+j]=sum; + } + max=0.0; + for(i=j; i<m; ++i){ + sum=a[i*m+j]; + for(k=0; k<j; ++k) + sum-=a[i*m+k]*a[k*m+j]; + a[i*m+j]=sum; + if((tmp=work[i]*FABS(sum))>=max){ + max=tmp; + maxi=i; + } + } + if(j!=maxi){ + for(k=0; k<m; ++k){ + tmp=a[maxi*m+k]; + a[maxi*m+k]=a[j*m+k]; + a[j*m+k]=tmp; + } + work[maxi]=work[j]; + } + idx[j]=maxi; + if(a[j*m+j]==0.0) + a[j*m+j]=LM_REAL_EPSILON; + if(j!=m-1){ + tmp=LM_CNST(1.0)/(a[j*m+j]); + for(i=j+1; i<m; ++i) + a[i*m+j]*=tmp; + } + } + + /* The decomposition has now replaced a. Solve the linear system using + * forward and back substitution + */ + for(i=k=0; i<m; ++i){ + j=idx[i]; + sum=x[j]; + x[j]=x[i]; + if(k!=0) + for(j=k-1; j<i; ++j) + sum-=a[i*m+j]*x[j]; + else + if(sum!=0.0) + k=i+1; + x[i]=sum; + } + + for(i=m-1; i>=0; --i){ + sum=x[i]; + for(j=i+1; j<m; ++j) + sum-=a[i*m+j]*x[j]; + x[i]=sum/a[i*m+i]; + } + +#ifndef LINSOLVERS_RETAIN_MEMORY + free(buf); +#endif + + return 1; +} + +/* undefine all. IT MUST REMAIN IN THIS POSITION IN FILE */ +#undef AX_EQ_B_LU + +#endif /* HAVE_LAPACK */ diff --git a/sci-libs/levmar/levmar-2.5/CMakeLists.txt b/sci-libs/levmar/levmar-2.5/CMakeLists.txt new file mode 100644 index 000000000..0d02cb84e --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/CMakeLists.txt @@ -0,0 +1,54 @@ +# levmar CMake file; see http://www.cmake.org and
+# http://www.insightsoftwareconsortium.org/wiki/index.php/CMake_Tutorial
+
+PROJECT(LEVMAR)
+#CMAKE_MINIMUM_REQUIRED(VERSION 1.4)
+
+# compiler flags
+#ADD_DEFINITIONS(-DLINSOLVERS_RETAIN_MEMORY) # do not free memory between linear solvers calls
+#REMOVE_DEFINITIONS(-DLINSOLVERS_RETAIN_MEMORY) # free memory between calls
+
+# f2c is sometimes equivalent to libF77 & libI77; in that case, set HAVE_F2C to 0
+SET(HAVE_F2C 1 CACHE BOOL "Do we have f2c or F77/I77?" )
+
+# the directory where the lapack/blas/f2c libraries reside
+SET(LAPACKBLAS_DIR /usr/lib CACHE PATH "Path to lapack/blas libraries")
+
+# actual names for the lapack/blas/f2c libraries
+SET(LAPACK_LIB lapack CACHE STRING "The name of the lapack library")
+SET(BLAS_LIB blas CACHE STRING "The name of the blas library")
+IF(HAVE_F2C)
+ SET(F2C_LIB f2c CACHE STRING "The name of the f2c library")
+ELSE(HAVE_F2C)
+ SET(F77_LIB libF77 CACHE STRING "The name of the F77 library")
+ SET(I77_LIB libI77 CACHE STRING "The name of the I77 library")
+ENDIF(HAVE_F2C)
+
+########################## NO CHANGES BEYOND THIS POINT ##########################
+
+INCLUDE_DIRECTORIES(.)
+#INCLUDE_DIRECTORIES(/usr/include)
+
+# levmar library source files
+ADD_LIBRARY(levmar STATIC
+ lm.c Axb.c misc.c lmlec.c lmbc.c lmblec.c lmbleic.c
+ levmar.h misc.h compiler.h
+)
+
+# demo program
+LINK_DIRECTORIES(${LAPACKBLAS_DIR})
+LINK_DIRECTORIES(.)
+ADD_EXECUTABLE(lmdemo lmdemo.c levmar.h)
+# libraries the demo depends on
+IF(HAVE_F2C)
+ TARGET_LINK_LIBRARIES(lmdemo levmar ${LAPACK_LIB} ${BLAS_LIB} ${F2C_LIB})
+ELSE(HAVE_F2C)
+ TARGET_LINK_LIBRARIES(lmdemo levmar ${LAPACK_LIB} ${BLAS_LIB} ${F77_LIB} ${I77_LIB})
+ENDIF(HAVE_F2C)
+
+# make sure that the library is built before the demo
+ADD_DEPENDENCIES(lmdemo levmar)
+
+#SUBDIRS(matlab)
+
+#ADD_TEST(levmar_tst lmdemo)
diff --git a/sci-libs/levmar/levmar-2.5/LICENSE b/sci-libs/levmar/levmar-2.5/LICENSE new file mode 100644 index 000000000..d60c31a97 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/LICENSE @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/sci-libs/levmar/levmar-2.5/Makefile b/sci-libs/levmar/levmar-2.5/Makefile new file mode 100644 index 000000000..14525e223 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/Makefile @@ -0,0 +1,47 @@ +# +# Unix/Linux GCC Makefile for Levenberg - Marquardt minimization +# Under windows, use Makefile.vc for MSVC +# + +CONFIGFLAGS=#-ULINSOLVERS_RETAIN_MEMORY +#ARCHFLAGS=-march=pentium4 # YOU MIGHT WANT TO UNCOMMENT THIS FOR P4 +LIBOBJS=lm.o Axb.o misc.o lmlec.o lmbc.o lmblec.o lmbleic.o +LIBSRCS=lm.c Axb.c misc.c lmlec.c lmbc.c lmblec.c lmbleic.c +DEMOBJS=lmdemo.o +DEMOSRCS=lmdemo.c +AR=ar +RANLIB=ranlib + +LIBS=$(LAPACKLIBS) + +all: liblevmar.a lmdemo + +liblevmar.a: $(LIBOBJS) + $(AR) crv liblevmar.a $(LIBOBJS) + $(RANLIB) liblevmar.a + +lmdemo: $(DEMOBJS) liblevmar.a + $(CC) $(LDFLAGS) $(DEMOBJS) -o lmdemo liblevmar.a $(LIBS) -lm + +lm.o: lm.c lm_core.c levmar.h misc.h compiler.h +Axb.o: Axb.c Axb_core.c levmar.h misc.h +misc.o: misc.c misc_core.c levmar.h misc.h +lmlec.o: lmlec.c lmlec_core.c levmar.h misc.h +lmbc.o: lmbc.c lmbc_core.c levmar.h misc.h compiler.h +lmblec.o: lmblec.c lmblec_core.c levmar.h misc.h +lmbleic.o: lmbleic.c lmbleic_core.c levmar.h misc.h + +lmdemo.o: levmar.h + +clean: + @rm -f $(LIBOBJS) $(DEMOBJS) + +cleanall: clean + @rm -f lmdemo + @rm -f liblevmar.a + +depend: + makedepend -f Makefile $(LIBSRCS) $(DEMOSRCS) + +# DO NOT DELETE THIS LINE -- make depend depends on it. + diff --git a/sci-libs/levmar/levmar-2.5/Makefile.icc b/sci-libs/levmar/levmar-2.5/Makefile.icc new file mode 100644 index 000000000..a2ab2e72e --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/Makefile.icc @@ -0,0 +1,62 @@ +# +# Unix/Linux Intel ICC Makefile for Levenberg - Marquardt minimization +# To be used with "make -f Makefile.icc" +# Under windows, use Makefile.vc for MSVC +# + +CC=icc #-w1 # warnings on +CXX=icpc +CONFIGFLAGS=#-ULINSOLVERS_RETAIN_MEMORY +ARCHFLAGS=-march=pentium4 -mcpu=pentium4 +CFLAGS=$(CONFIGFLAGS) $(ARCHFLAGS) -O3 -tpp7 -xW -ip -ipo -unroll #-g +LAPACKLIBS_PATH=/usr/local/lib # WHEN USING LAPACK, CHANGE THIS TO WHERE YOUR COMPILED LIBS ARE! +LDFLAGS=-L$(LAPACKLIBS_PATH) -L. +LIBOBJS=lm.o Axb.o misc.o lmlec.o lmbc.o lmblec.o lmbleic.o +LIBSRCS=lm.c Axb.c misc.c lmlec.c lmbc.c lmblec.c lmbleic.c +DEMOBJS=lmdemo.o +DEMOSRCS=lmdemo.c +AR=xiar +#RANLIB=ranlib +LAPACKLIBS=-llapack -lblas -lf2c # comment this line if you are not using LAPACK. + # On systems with a FORTRAN (not f2c'ed) version of LAPACK, -lf2c is + # not necessary; on others, -lf2c is equivalent to -lF77 -lI77 + +# The following works with the ATLAS updated lapack and Linux_P4SSE2 from http://www.netlib.org/atlas/archives/linux/ +#LAPACKLIBS=-L/usr/local/atlas/lib -llapack -lcblas -lf77blas -latlas -lf2c + +#LAPACKLIBS=-llapack -lgoto2 -lpthread -lf2c # This works with GotoBLAS + # from http://www.tacc.utexas.edu/research-development/tacc-projects/ + +LIBS=$(LAPACKLIBS) + +all: liblevmar.a lmdemo + +liblevmar.a: $(LIBOBJS) + $(AR) crv liblevmar.a $(LIBOBJS) + #$(RANLIB) liblevmar.a + +lmdemo: $(DEMOBJS) liblevmar.a + $(CC) $(ARCHFLAGS) $(LDFLAGS) $(DEMOBJS) -o lmdemo -llevmar $(LIBS) -lm + +lm.o: lm.c lm_core.c levmar.h misc.h compiler.h +Axb.o: Axb.c Axb_core.c levmar.h misc.h +misc.o: misc.c misc_core.c levmar.h misc.h +lmlec.o: lmlec.c lmlec_core.c levmar.h misc.h +lmbc.o: lmbc.c lmbc_core.c levmar.h misc.h compiler.h +lmblec.o: lmblec.c lmblec_core.c levmar.h misc.h +lmbleic.o: lmbleic.c lmbleic_core.c levmar.h misc.h + +lmdemo.o: levmar.h + +clean: + @rm -f $(LIBOBJS) $(DEMOBJS) + +cleanall: clean + @rm -f lmdemo + @rm -f liblevmar.a + +depend: + makedepend -f Makefile.icc $(LIBSRCS) $(DEMOSRCS) + +# DO NOT DELETE THIS LINE -- make depend depends on it. + diff --git a/sci-libs/levmar/levmar-2.5/Makefile.so b/sci-libs/levmar/levmar-2.5/Makefile.so new file mode 100644 index 000000000..fac088c7c --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/Makefile.so @@ -0,0 +1,49 @@ +# +# Unix/Linux GCC Makefile for Levenberg - Marquardt minimization +# Under windows, use Makefile.vc for MSVC +# + +# major & minor shared lib numbers +MAJ=2 +MIN=2 +ODIR=sobj # where to place object files for shared lib +CC=gcc +CONFIGFLAGS=-ULINSOLVERS_RETAIN_MEMORY +#ARCHFLAGS=-march=pentium4 # YOU MIGHT WANT TO UNCOMMENT THIS FOR P4 +CFLAGS=-fPIC $(CONFIGFLAGS) $(ARCHFLAGS) -O3 -funroll-loops -Wall #-pg +LAPACKLIBS_PATH=/usr/local/lib # WHEN USING LAPACK, CHANGE THIS TO WHERE YOUR COMPILED LIBS ARE! +LIBOBJS=$(ODIR)/lm.o $(ODIR)/Axb.o $(ODIR)/misc.o $(ODIR)/lmlec.o $(ODIR)/lmbc.o $(ODIR)/lmblec.o $(ODIR)/lmbleic.o +LIBSRCS=lm.c Axb.c misc.c lmlec.c lmbc.c lmblec.c lmbleic.c +LAPACKLIBS=-llapack -lblas -lf2c # comment this line if you are not using LAPACK. + # On systems with a FORTRAN (not f2c'ed) version of LAPACK, -lf2c is + # not necessary; on others, -lf2c is equivalent to -lF77 -lI77 + +LIBS=$(LAPACKLIBS) + +$(ODIR)/liblevmar.so.$(MAJ).$(MIN): $(LIBOBJS) + $(CC) -shared -Wl,-soname,liblevmar.so.$(MAJ) -o $(ODIR)/liblevmar.so.$(MAJ).$(MIN) $(LIBOBJS) #-llapack -lblas -lf2c + +# implicit rule for generating *.o files in ODIR from *.c files +$(ODIR)/%.o : %.c + $(CC) $(CFLAGS) -c $< -o $@ + + +$(ODIR)/lm.o: lm.c lm_core.c levmar.h misc.h compiler.h +$(ODIR)/Axb.o: Axb.c Axb_core.c levmar.h misc.h +$(ODIR)/misc.o: misc.c misc_core.c levmar.h misc.h +$(ODIR)/lmlec.o: lmlec.c lmlec_core.c levmar.h misc.h +$(ODIR)/lmbc.o: lmbc.c lmbc_core.c levmar.h misc.h compiler.h +$(ODIR)/lmblec.o: lmblec.c lmblec_core.c levmar.h misc.h +$(ODIR)/lmbleic.o: lmbleic.c lmbleic_core.c levmar.h misc.h + +clean: + @rm -f $(LIBOBJS) + +cleanall: clean + @rm -f $(ODIR)/liblevmar.so.$(MAJ).$(MIN) + +depend: + makedepend -f Makefile $(LIBSRCS) + +# DO NOT DELETE THIS LINE -- make depend depends on it. + diff --git a/sci-libs/levmar/levmar-2.5/Makefile.vc b/sci-libs/levmar/levmar-2.5/Makefile.vc new file mode 100644 index 000000000..b8ae99aa2 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/Makefile.vc @@ -0,0 +1,59 @@ +# +# MS Visual C Makefile for Levenberg - Marquardt minimization +# Under Unix/Linux, use Makefile for GCC +# +# At the command prompt, type +# nmake /f Makefile.vc +# +# NOTE: To use this, you must have MSVC installed and properly +# configured for command line use (you might need to run VCVARS32.BAT +# included with your copy of MSVC). Another option is to use the +# free MSVC toolkit from http://msdn.microsoft.com/visualc/vctoolkit2003/ +# + +MAKE=nmake /nologo +CC=cl /nologo +CONFIGFLAGS=#/ULINSOLVERS_RETAIN_MEMORY +# YOU MIGHT WANT TO UNCOMMENT THE FOLLOWING LINE +#SPOPTFLAGS=/GL /G7 /arch:SSE2 # special optimization: resp. whole program opt., Athlon/Pentium4 opt., SSE2 extensions +# /MD COMPILES WITH MULTIPLE THREADS SUPPORT. TO DISABLE IT, SUBSTITUTE WITH /ML +# FLAG /EHsc SUPERSEDED /GX IN MSVC'05. IF YOU HAVE AN EARLIER VERSION THAT COMPLAINS ABOUT IT, CHANGE /EHsc TO /GX +CFLAGS=$(CONFIGFLAGS) /I. /MD /W3 /EHsc /O2 $(SPOPTFLAGS) # /Wall +LAPACKLIBS_PATH=C:\src\lib # WHEN USING LAPACK, CHANGE THIS TO WHERE YOUR COMPILED LIBS ARE! +LDFLAGS=/link /subsystem:console /opt:ref /libpath:$(LAPACKLIBS_PATH) /libpath:. +LIBOBJS=lm.obj Axb.obj misc.obj lmlec.obj lmbc.obj lmblec.obj lmbleic.obj +LIBSRCS=lm.c Axb.c misc.c lmlec.c lmbc.c lmblec.c lmbleic.c +DEMOBJS=lmdemo.obj +DEMOSRCS=lmdemo.c +AR=lib /nologo + +# comment the following line if you are not using LAPACK +LAPACKLIBS=clapack.lib blas.lib libF77.lib libI77.lib + +LIBS=levmar.lib $(LAPACKLIBS) + +all: levmar.lib lmdemo.exe + +levmar.lib: $(LIBOBJS) + $(AR) /out:levmar.lib $(LIBOBJS) + +lmdemo.exe: $(DEMOBJS) levmar.lib + $(CC) $(DEMOBJS) $(LDFLAGS) /out:lmdemo.exe $(LIBS) + +lm.obj: lm.c lm_core.c levmar.h misc.h compiler.h +Axb.obj: Axb.c Axb_core.c levmar.h misc.h +misc.obj: misc.c misc_core.c levmar.h misc.h +lmlec.obj: lmlec.c lmlec_core.c levmar.h misc.h +lmbc.obj: lmbc.c lmbc_core.c levmar.h misc.h compiler.h +lmblec.obj: lmblec.c lmblec_core.c levmar.h misc.h +lmbleic.obj: lmbleic.c lmbleic_core.c levmar.h misc.h + +lmdemo.obj: levmar.h + +clean: + -del $(LIBOBJS) $(DEMOBJS) + +cleanall: clean + -del lmdemo.exe + -del levmar.lib + diff --git a/sci-libs/levmar/levmar-2.5/README.txt b/sci-libs/levmar/levmar-2.5/README.txt new file mode 100644 index 000000000..44da92998 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/README.txt @@ -0,0 +1,74 @@ + **************************************************************
+ LEVMAR
+ version 2.5
+ By Manolis Lourakis
+
+ Institute of Computer Science
+ Foundation for Research and Technology - Hellas
+ Heraklion, Crete, Greece
+ **************************************************************
+
+
+GENERAL
+This is levmar, a copylefted C/C++ implementation of the Levenberg-Marquardt non-linear
+least squares algorithm. levmar includes double and single precision LM versions, both
+with analytic and finite difference approximated Jacobians. levmar also has some support
+for constrained non-linear least squares, allowing linear equation, box and linear
+inequality constraints. The following options regarding the solution of the underlying
+augmented normal equations are offered:
+
+1) Assuming that you have LAPACK (or an equivalent vendor library such as ESSL, MKL,
+ NAG, ...) installed, you can use the included LAPACK-based solvers (default).
+
+2) If you don't have LAPACK or decide not to use it, undefine HAVE_LAPACK in levmar.h
+ and a LAPACK-free, LU-based linear systems solver will by used. Also, the line
+ setting the variable LAPACKLIBS in the Makefile should be commented out.
+
+It is strongly recommended that you *do* employ LAPACK; if you don't have it already,
+I suggest getting clapack from http://www.netlib.org/clapack. However, LAPACK's
+use is not mandatory and the 2nd option makes levmar totally self-contained.
+See lmdemo.c for examples of use and http://www.ics.forth.gr/~lourakis/levmar
+for general comments. An example of using levmar for data fitting is in expfit.c
+
+The mathematical theory behind levmar is described in the lecture notes entitled
+"Methods for Non-Linear Least Squares Problems", by K. Madsen, H.B. Nielsen and O. Tingleff,
+Technical University of Denmark (http://www.imm.dtu.dk/courses/02611/nllsq.pdf).
+
+LICENSE
+levmar is released under the GNU Public License (GPL), which can be found in the included
+LICENSE file. Note that under the terms of GPL, commercial use is allowed only if a software
+employing levmar is also published in source under the GPL. However, if you are interested
+in using levmar in a proprietary commercial application, a commercial license for levmar
+can be obtained by contacting the author using the email address at the end of this file.
+
+COMPILATION
+ - You might first consider setting a few configuration options at the top of
+ levmar.h. See the accompanying comments for more details.
+
+ - On a Linux/Unix system, typing "make" will build both levmar and the demo
+ program using gcc. Alternatively, if Intel's C++ compiler is installed, it
+ can be used by typing "make -f Makefile.icc".
+
+ - Under Windows and if Visual C is installed & configured for command line
+ use, type "nmake /f Makefile.vc" in a cmd window to build levmar and the
+ demo program. In case of trouble, read the comments on top of Makefile.vc
+ Visual C++ project files (levmar.vcproj and lmdemo.vcproj) are also included,
+ however they are not supported and are only meant to serve as a starting point
+ for creating your own. Check http://www.arstdesign.com/articles/prjconverter.html
+ if you need to convert to .dsw/.dsp (i.e., Visual C++ 6.0) project files.
+
+ - levmar can also be built under various platforms using the CMake cross-platform
+ build system. The included CMakeLists.txt file can be used to generate makefiles
+ for Unix systems or project files for Windows systems. See http://www.cmake.org
+ for details.
+
+MATLAB INTERFACE
+Since version 2.2, the levmar distribution includes a matlab interface.
+See the 'matlab' subdirectory for more information and examples of use.
+
+Notice that *_core.c files are not to be compiled directly; For example,
+Axb_core.c is included by Axb.c, to provide single and double precision
+routine versions.
+
+
+Send your comments/bug reports to lourakis at ics forth gr
diff --git a/sci-libs/levmar/levmar-2.5/compiler.h b/sci-libs/levmar/levmar-2.5/compiler.h new file mode 100644 index 000000000..706924c6a --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/compiler.h @@ -0,0 +1,45 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#ifndef _COMPILER_H_ +#define _COMPILER_H_ + +/* note: intel's icc defines both __ICC & __INTEL_COMPILER. + * Also, some compilers other than gcc define __GNUC__, + * therefore gcc should be checked last + */ +#ifdef _MSC_VER +#define inline __inline // MSVC +#elif !defined(__ICC) && !defined(__INTEL_COMPILER) && !defined(__GNUC__) +#define inline // other than MSVC, ICC, GCC: define empty +#endif + +#ifdef _MSC_VER +#define LM_FINITE _finite // MSVC +#elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(__GNUC__) +#define LM_FINITE finite // ICC, GCC +#else +#define LM_FINITE finite // other than MSVC, ICC, GCC, let's hope this will work +#endif + +#ifdef _MSC_VER // avoid deprecation warnings in VS2005 +#define _CRT_SECURE_NO_WARNINGS +#endif + +#endif /* _COMPILER_H_ */ diff --git a/sci-libs/levmar/levmar-2.5/expfit.c b/sci-libs/levmar/levmar-2.5/expfit.c new file mode 100644 index 000000000..21d54f4e7 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/expfit.c @@ -0,0 +1,122 @@ +//////////////////////////////////////////////////////////////////////////////////// +// Example program that shows how to use levmar in order to fit the three- +// parameter exponential model x_i = p[0]*exp(-p[1]*i) + p[2] to a set of +// data measurements; example is based on a similar one from GSL. +// +// Copyright (C) 2008 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +//////////////////////////////////////////////////////////////////////////////////// + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +#include <levmar.h> + +#ifndef LM_DBL_PREC +#error Example program assumes that levmar has been compiled with double precision, see LM_DBL_PREC! +#endif + + +/* the following macros concern the initialization of a random number generator for adding noise */ +#undef REPEATABLE_RANDOM +#define DBL_RAND_MAX (double)(RAND_MAX) + +#ifdef _MSC_VER // MSVC +#include <process.h> +#define GETPID _getpid +#elif defined(__GNUC__) // GCC +#include <sys/types.h> +#include <unistd.h> +#define GETPID getpid +#else +#warning Do not know the name of the function returning the process id for your OS/compiler combination +#define GETPID 0 +#endif /* _MSC_VER */ + +#ifdef REPEATABLE_RANDOM +#define INIT_RANDOM(seed) srandom(seed) +#else +#define INIT_RANDOM(seed) srandom((int)GETPID()) // seed unused +#endif + +/* Gaussian noise with mean m and variance s, uses the Box-Muller transformation */ +double gNoise(double m, double s) +{ +double r1, r2, val; + + r1=((double)random())/DBL_RAND_MAX; + r2=((double)random())/DBL_RAND_MAX; + + val=sqrt(-2.0*log(r1))*cos(2.0*M_PI*r2); + + val=s*val+m; + + return val; +} + +/* model to be fitted to measurements: x_i = p[0]*exp(-p[1]*i) + p[2], i=0...n-1 */ +void expfunc(double *p, double *x, int m, int n, void *data) +{ +register int i; + + for(i=0; i<n; ++i){ + x[i]=p[0]*exp(-p[1]*i) + p[2]; + } +} + +/* Jacobian of expfunc() */ +void jacexpfunc(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; + + /* fill Jacobian row by row */ + for(i=j=0; i<n; ++i){ + jac[j++]=exp(-p[1]*i); + jac[j++]=-p[0]*i*exp(-p[1]*i); + jac[j++]=1.0; + } +} + +int main() +{ +const int n=40, m=3; // 40 measurements, 3 parameters +double p[m], x[n], opts[LM_OPTS_SZ], info[LM_INFO_SZ]; +register int i; +int ret; + + /* generate some measurement using the exponential model with + * parameters (5.0, 0.1, 1.0), corrupted with zero-mean + * Gaussian noise of s=0.1 + */ + INIT_RANDOM(0); + for(i=0; i<n; ++i) + x[i]=(5.0*exp(-0.1*i) + 1.0) + gNoise(0.0, 0.1); + + /* initial parameters estimate: (1.0, 0.0, 0.0) */ + p[0]=1.0; p[1]=0.0; p[2]=0.0; + + /* optimization control parameters; passing to levmar NULL instead of opts reverts to defaults */ + opts[0]=LM_INIT_MU; opts[1]=1E-15; opts[2]=1E-15; opts[3]=1E-20; + opts[4]=LM_DIFF_DELTA; // relevant only if the finite difference Jacobian version is used + + /* invoke the optimization function */ + ret=dlevmar_der(expfunc, jacexpfunc, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + //ret=dlevmar_dif(expfunc, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // without Jacobian + printf("Levenberg-Marquardt returned in %g iter, reason %g, sumsq %g [%g]\n", info[5], info[6], info[1], info[0]); + printf("Best fit parameters: %.7g %.7g %.7g\n", p[0], p[1], p[2]); + + exit(0); +} diff --git a/sci-libs/levmar/levmar-2.5/levmar.h b/sci-libs/levmar/levmar-2.5/levmar.h new file mode 100644 index 000000000..96b5e81cd --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/levmar.h @@ -0,0 +1,370 @@ +/* +//////////////////////////////////////////////////////////////////////////////////// +// +// Prototypes and definitions for the Levenberg - Marquardt minimization algorithm +// Copyright (C) 2004 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +//////////////////////////////////////////////////////////////////////////////////// +*/ + +#ifndef _LEVMAR_H_ +#define _LEVMAR_H_ + + +/************************************* Start of configuration options *************************************/ + +/* specify whether to use LAPACK or not. The first option is strongly recommended */ +#define HAVE_LAPACK /* use LAPACK */ +/* #undef HAVE_LAPACK */ /* uncomment this to force not using LAPACK */ + +/* to avoid the overhead of repeated mallocs(), routines in Axb.c can be instructed to + * retain working memory between calls. Such a choice, however, renders these routines + * non-reentrant and is not safe in a shared memory multiprocessing environment. + * Bellow, this option is turned on only when not compiling with OpenMP. + */ +#if !defined(_OPENMP) +#define LINSOLVERS_RETAIN_MEMORY /* comment this if you don't want routines in Axb.c retain working memory between calls */ +#endif + +/* determine the precision variants to be build. Default settings build + * both the single and double precision routines + */ +#define LM_DBL_PREC /* comment this if you don't want the double precision routines to be compiled */ +#define LM_SNGL_PREC /* comment this if you don't want the single precision routines to be compiled */ + +/****************** End of configuration options, no changes necessary beyond this point ******************/ + + +#ifdef __cplusplus +extern "C" { +#endif + + +#define FABS(x) (((x)>=0.0)? (x) : -(x)) + +/* work arrays size for ?levmar_der and ?levmar_dif functions. + * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes + */ +#define LM_DER_WORKSZ(npar, nmeas) (2*(nmeas) + 4*(npar) + (nmeas)*(npar) + (npar)*(npar)) +#define LM_DIF_WORKSZ(npar, nmeas) (4*(nmeas) + 4*(npar) + (nmeas)*(npar) + (npar)*(npar)) + +/* work arrays size for ?levmar_bc_der and ?levmar_bc_dif functions. + * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes + */ +#define LM_BC_DER_WORKSZ(npar, nmeas) (2*(nmeas) + 4*(npar) + (nmeas)*(npar) + (npar)*(npar)) +#define LM_BC_DIF_WORKSZ(npar, nmeas) LM_BC_DER_WORKSZ((npar), (nmeas)) /* LEVMAR_BC_DIF currently implemented using LEVMAR_BC_DER()! */ + +/* work arrays size for ?levmar_lec_der and ?levmar_lec_dif functions. + * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes + */ +#define LM_LEC_DER_WORKSZ(npar, nmeas, nconstr) LM_DER_WORKSZ((npar)-(nconstr), (nmeas)) +#define LM_LEC_DIF_WORKSZ(npar, nmeas, nconstr) LM_DIF_WORKSZ((npar)-(nconstr), (nmeas)) + +/* work arrays size for ?levmar_blec_der and ?levmar_blec_dif functions. + * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes + */ +#define LM_BLEC_DER_WORKSZ(npar, nmeas, nconstr) LM_LEC_DER_WORKSZ((npar), (nmeas)+(npar), (nconstr)) +#define LM_BLEC_DIF_WORKSZ(npar, nmeas, nconstr) LM_LEC_DIF_WORKSZ((npar), (nmeas)+(npar), (nconstr)) + +/* work arrays size for ?levmar_bleic_der and ?levmar_bleic_dif functions. + * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes + */ +#define LM_BLEIC_DER_WORKSZ(npar, nmeas, nconstr1, nconstr2) LM_BLEC_DER_WORKSZ((npar)+(nconstr2), (nmeas)+(nconstr2), (nconstr1)+(nconstr2)) +#define LM_BLEIC_DIF_WORKSZ(npar, nmeas, nconstr1, nconstr2) LM_BLEC_DIF_WORKSZ((npar)+(nconstr2), (nmeas)+(nconstr2), (nconstr1)+(nconstr2)) + +#define LM_OPTS_SZ 5 /* max(4, 5) */ +#define LM_INFO_SZ 10 +#define LM_ERROR -1 +#define LM_INIT_MU 1E-03 +#define LM_STOP_THRESH 1E-17 +#define LM_DIFF_DELTA 1E-06 +#define LM_VERSION "2.5 (December 2009)" + +#ifdef LM_DBL_PREC +/* double precision LM, with & without Jacobian */ +/* unconstrained minimization */ +extern int dlevmar_der( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, double *x, int m, int n, int itmax, double *opts, + double *info, double *work, double *covar, void *adata); + +extern int dlevmar_dif( + void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *x, int m, int n, int itmax, double *opts, + double *info, double *work, double *covar, void *adata); + +/* box-constrained minimization */ +extern int dlevmar_bc_der( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, double *x, int m, int n, double *lb, double *ub, + int itmax, double *opts, double *info, double *work, double *covar, void *adata); + +extern int dlevmar_bc_dif( + void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *x, int m, int n, double *lb, double *ub, + int itmax, double *opts, double *info, double *work, double *covar, void *adata); + +#ifdef HAVE_LAPACK +/* linear equation constrained minimization */ +extern int dlevmar_lec_der( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, double *x, int m, int n, double *A, double *b, int k, + int itmax, double *opts, double *info, double *work, double *covar, void *adata); + +extern int dlevmar_lec_dif( + void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *x, int m, int n, double *A, double *b, int k, + int itmax, double *opts, double *info, double *work, double *covar, void *adata); + +/* box & linear equation constrained minimization */ +extern int dlevmar_blec_der( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, double *x, int m, int n, double *lb, double *ub, double *A, double *b, int k, double *wghts, + int itmax, double *opts, double *info, double *work, double *covar, void *adata); + +extern int dlevmar_blec_dif( + void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *x, int m, int n, double *lb, double *ub, double *A, double *b, int k, double *wghts, + int itmax, double *opts, double *info, double *work, double *covar, void *adata); + +/* box, linear equations & inequalities constrained minimization */ +extern int dlevmar_bleic_der( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, double *x, int m, int n, double *lb, double *ub, + double *A, double *b, int k1, double *C, double *d, int k2, + int itmax, double *opts, double *info, double *work, double *covar, void *adata); + +extern int dlevmar_bleic_dif( + void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *x, int m, int n, double *lb, double *ub, + double *A, double *b, int k1, double *C, double *d, int k2, + int itmax, double *opts, double *info, double *work, double *covar, void *adata); + +/* box & linear inequality constraints */ +extern int dlevmar_blic_der( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, double *x, int m, int n, double *lb, double *ub, double *C, double *d, int k2, + int itmax, double opts[4], double info[LM_INFO_SZ], double *work, double *covar, void *adata); + +extern int dlevmar_blic_dif( + void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *x, int m, int n, double *lb, double *ub, double *C, double *d, int k2, + int itmax, double opts[5], double info[LM_INFO_SZ], double *work, double *covar, void *adata); + +/* linear equation & inequality constraints */ +extern int dlevmar_leic_der( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, double *x, int m, int n, double *A, double *b, int k1, double *C, double *d, int k2, + int itmax, double opts[4], double info[LM_INFO_SZ], double *work, double *covar, void *adata); + +extern int dlevmar_leic_dif( + void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *x, int m, int n, double *A, double *b, int k1, double *C, double *d, int k2, + int itmax, double opts[5], double info[LM_INFO_SZ], double *work, double *covar, void *adata); + +/* linear inequality constraints */ +extern int dlevmar_lic_der( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, double *x, int m, int n, double *C, double *d, int k2, + int itmax, double opts[4], double info[LM_INFO_SZ], double *work, double *covar, void *adata); + +extern int dlevmar_lic_dif( + void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *x, int m, int n, double *C, double *d, int k2, + int itmax, double opts[5], double info[LM_INFO_SZ], double *work, double *covar, void *adata); +#endif /* HAVE_LAPACK */ + +#endif /* LM_DBL_PREC */ + + +#ifdef LM_SNGL_PREC +/* single precision LM, with & without Jacobian */ +/* unconstrained minimization */ +extern int slevmar_der( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, float *x, int m, int n, int itmax, float *opts, + float *info, float *work, float *covar, void *adata); + +extern int slevmar_dif( + void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *x, int m, int n, int itmax, float *opts, + float *info, float *work, float *covar, void *adata); + +/* box-constrained minimization */ +extern int slevmar_bc_der( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, float *x, int m, int n, float *lb, float *ub, + int itmax, float *opts, float *info, float *work, float *covar, void *adata); + +extern int slevmar_bc_dif( + void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *x, int m, int n, float *lb, float *ub, + int itmax, float *opts, float *info, float *work, float *covar, void *adata); + +#ifdef HAVE_LAPACK +/* linear equation constrained minimization */ +extern int slevmar_lec_der( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, float *x, int m, int n, float *A, float *b, int k, + int itmax, float *opts, float *info, float *work, float *covar, void *adata); + +extern int slevmar_lec_dif( + void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *x, int m, int n, float *A, float *b, int k, + int itmax, float *opts, float *info, float *work, float *covar, void *adata); + +/* box & linear equation constrained minimization */ +extern int slevmar_blec_der( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, float *x, int m, int n, float *lb, float *ub, float *A, float *b, int k, float *wghts, + int itmax, float *opts, float *info, float *work, float *covar, void *adata); + +extern int slevmar_blec_dif( + void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *x, int m, int n, float *lb, float *ub, float *A, float *b, int k, float *wghts, + int itmax, float *opts, float *info, float *work, float *covar, void *adata); + +/* box, linear equations & inequalities constrained minimization */ +extern int slevmar_bleic_der( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, float *x, int m, int n, float *lb, float *ub, + float *A, float *b, int k1, float *C, float *d, int k2, + int itmax, float *opts, float *info, float *work, float *covar, void *adata); + +extern int slevmar_bleic_dif( + void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *x, int m, int n, float *lb, float *ub, + float *A, float *b, int k1, float *C, float *d, int k2, + int itmax, float *opts, float *info, float *work, float *covar, void *adata); + +/* box & linear inequality constraints */ +extern int slevmar_blic_der( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, float *x, int m, int n, float *lb, float *ub, float *C, float *d, int k2, + int itmax, float opts[4], float info[LM_INFO_SZ], float *work, float *covar, void *adata); + +extern int slevmar_blic_dif( + void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *x, int m, int n, float *lb, float *ub, float *C, float *d, int k2, + int itmax, float opts[5], float info[LM_INFO_SZ], float *work, float *covar, void *adata); + +/* linear equality & inequality constraints */ +extern int slevmar_leic_der( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, float *x, int m, int n, float *A, float *b, int k1, float *C, float *d, int k2, + int itmax, float opts[4], float info[LM_INFO_SZ], float *work, float *covar, void *adata); + +extern int slevmar_leic_dif( + void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *x, int m, int n, float *A, float *b, int k1, float *C, float *d, int k2, + int itmax, float opts[5], float info[LM_INFO_SZ], float *work, float *covar, void *adata); + +/* linear inequality constraints */ +extern int slevmar_lic_der( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, float *x, int m, int n, float *C, float *d, int k2, + int itmax, float opts[4], float info[LM_INFO_SZ], float *work, float *covar, void *adata); + +extern int slevmar_lic_dif( + void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *x, int m, int n, float *C, float *d, int k2, + int itmax, float opts[5], float info[LM_INFO_SZ], float *work, float *covar, void *adata); +#endif /* HAVE_LAPACK */ + +#endif /* LM_SNGL_PREC */ + +/* linear system solvers */ +#ifdef HAVE_LAPACK + +#ifdef LM_DBL_PREC +extern int dAx_eq_b_QR(double *A, double *B, double *x, int m); +extern int dAx_eq_b_QRLS(double *A, double *B, double *x, int m, int n); +extern int dAx_eq_b_Chol(double *A, double *B, double *x, int m); +extern int dAx_eq_b_LU(double *A, double *B, double *x, int m); +extern int dAx_eq_b_SVD(double *A, double *B, double *x, int m); +extern int dAx_eq_b_BK(double *A, double *B, double *x, int m); +#endif /* LM_DBL_PREC */ + +#ifdef LM_SNGL_PREC +extern int sAx_eq_b_QR(float *A, float *B, float *x, int m); +extern int sAx_eq_b_QRLS(float *A, float *B, float *x, int m, int n); +extern int sAx_eq_b_Chol(float *A, float *B, float *x, int m); +extern int sAx_eq_b_LU(float *A, float *B, float *x, int m); +extern int sAx_eq_b_SVD(float *A, float *B, float *x, int m); +extern int sAx_eq_b_BK(float *A, float *B, float *x, int m); +#endif /* LM_SNGL_PREC */ + +#else /* no LAPACK */ + +#ifdef LM_DBL_PREC +extern int dAx_eq_b_LU_noLapack(double *A, double *B, double *x, int n); +#endif /* LM_DBL_PREC */ + +#ifdef LM_SNGL_PREC +extern int sAx_eq_b_LU_noLapack(float *A, float *B, float *x, int n); +#endif /* LM_SNGL_PREC */ + +#endif /* HAVE_LAPACK */ + +/* Jacobian verification, double & single precision */ +#ifdef LM_DBL_PREC +extern void dlevmar_chkjac( + void (*func)(double *p, double *hx, int m, int n, void *adata), + void (*jacf)(double *p, double *j, int m, int n, void *adata), + double *p, int m, int n, void *adata, double *err); +#endif /* LM_DBL_PREC */ + +#ifdef LM_SNGL_PREC +extern void slevmar_chkjac( + void (*func)(float *p, float *hx, int m, int n, void *adata), + void (*jacf)(float *p, float *j, int m, int n, void *adata), + float *p, int m, int n, void *adata, float *err); +#endif /* LM_SNGL_PREC */ + +/* standard deviation, coefficient of determination (R2) & Pearson's correlation coefficient for best-fit parameters */ +#ifdef LM_DBL_PREC +extern double dlevmar_stddev( double *covar, int m, int i); +extern double dlevmar_corcoef(double *covar, int m, int i, int j); +extern double dlevmar_R2(void (*func)(double *p, double *hx, int m, int n, void *adata), double *p, double *x, int m, int n, void *adata); + +#endif /* LM_DBL_PREC */ + +#ifdef LM_SNGL_PREC +extern float slevmar_stddev( float *covar, int m, int i); +extern float slevmar_corcoef(float *covar, int m, int i, int j); +extern float slevmar_R2(void (*func)(float *p, float *hx, int m, int n, void *adata), float *p, float *x, int m, int n, void *adata); +#endif /* LM_SNGL_PREC */ + +#ifdef __cplusplus +} +#endif + +#endif /* _LEVMAR_H_ */ diff --git a/sci-libs/levmar/levmar-2.5/levmar.vcproj b/sci-libs/levmar/levmar-2.5/levmar.vcproj new file mode 100644 index 000000000..214db23c5 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/levmar.vcproj @@ -0,0 +1,200 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="8,00"
+ Name="levmar"
+ ProjectGUID="{F329E490-DB04-453A-A0BF-FEB90BD949D8}"
+ Keyword="Win32Proj"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="Debug"
+ IntermediateDirectory="Debug"
+ ConfigurationType="4"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="3"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="4"
+ CompileAs="1"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLibrarianTool"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="Release"
+ IntermediateDirectory="Release"
+ ConfigurationType="4"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
+ RuntimeLibrary="2"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="3"
+ CompileAs="1"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLibrarianTool"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <Filter
+ Name="Header Files"
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"
+ >
+ <File
+ RelativePath=".\compiler.h"
+ >
+ </File>
+ <File
+ RelativePath=".\levmar.h"
+ >
+ </File>
+ <File
+ RelativePath=".\misc.h"
+ >
+ </File>
+ </Filter>
+ <Filter
+ Name="Resource Files"
+ Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"
+ >
+ </Filter>
+ <Filter
+ Name="Source Files"
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+ >
+ <File
+ RelativePath=".\Axb.c"
+ >
+ </File>
+ <File
+ RelativePath=".\lm.c"
+ >
+ </File>
+ <File
+ RelativePath=".\lmbc.c"
+ >
+ </File>
+ <File
+ RelativePath=".\lmblec.c"
+ >
+ </File>
+ <File
+ RelativePath=".\lmlec.c"
+ >
+ </File>
+ <File
+ RelativePath=".\misc.c"
+ >
+ </File>
+ <File
+ RelativePath=".\lmbleic.c"
+ >
+ </File>
+ </Filter>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
diff --git a/sci-libs/levmar/levmar-2.5/liblevmar.a b/sci-libs/levmar/levmar-2.5/liblevmar.a Binary files differnew file mode 100644 index 000000000..8ec604258 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/liblevmar.a diff --git a/sci-libs/levmar/levmar-2.5/lm.c b/sci-libs/levmar/levmar-2.5/lm.c new file mode 100644 index 000000000..81d8acb24 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lm.c @@ -0,0 +1,83 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************** + * Levenberg-Marquardt nonlinear minimization. The same core code is used with + * appropriate #defines to derive single and double precision versions, see + * also lm_core.c + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <float.h> + +#include "levmar.h" +#include "compiler.h" +#include "misc.h" + +#define EPSILON 1E-12 +#define ONE_THIRD 0.3333333334 /* 1.0/3.0 */ + +#if !defined(LM_DBL_PREC) && !defined(LM_SNGL_PREC) +#error At least one of LM_DBL_PREC, LM_SNGL_PREC should be defined! +#endif + + +#ifdef LM_SNGL_PREC +/* single precision (float) definitions */ +#define LM_REAL float +#define LM_PREFIX s + +#define LM_REAL_MAX FLT_MAX +#define LM_REAL_MIN -FLT_MAX +#define LM_REAL_EPSILON FLT_EPSILON +#define __SUBCNST(x) x##F +#define LM_CNST(x) __SUBCNST(x) // force substitution + +#include "lm_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_MAX +#undef LM_REAL_EPSILON +#undef LM_REAL_MIN +#undef __SUBCNST +#undef LM_CNST +#endif /* LM_SNGL_PREC */ + +#ifdef LM_DBL_PREC +/* double precision definitions */ +#define LM_REAL double +#define LM_PREFIX d + +#define LM_REAL_MAX DBL_MAX +#define LM_REAL_MIN -DBL_MAX +#define LM_REAL_EPSILON DBL_EPSILON +#define LM_CNST(x) (x) + +#include "lm_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_MAX +#undef LM_REAL_EPSILON +#undef LM_REAL_MIN +#undef LM_CNST +#endif /* LM_DBL_PREC */ diff --git a/sci-libs/levmar/levmar-2.5/lm.h b/sci-libs/levmar/levmar-2.5/lm.h new file mode 120000 index 000000000..980d00cd7 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lm.h @@ -0,0 +1 @@ +levmar.h
\ No newline at end of file diff --git a/sci-libs/levmar/levmar-2.5/lm.o b/sci-libs/levmar/levmar-2.5/lm.o Binary files differnew file mode 100644 index 000000000..3075b39cb --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lm.o diff --git a/sci-libs/levmar/levmar-2.5/lm_core.c b/sci-libs/levmar/levmar-2.5/lm_core.c new file mode 100644 index 000000000..d06df2c96 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lm_core.c @@ -0,0 +1,843 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#ifndef LM_REAL // not included by lm.c +#error This file should not be compiled directly! +#endif + + +/* precision-specific definitions */ +#define LEVMAR_DER LM_ADD_PREFIX(levmar_der) +#define LEVMAR_DIF LM_ADD_PREFIX(levmar_dif) +#define LEVMAR_FDIF_FORW_JAC_APPROX LM_ADD_PREFIX(levmar_fdif_forw_jac_approx) +#define LEVMAR_FDIF_CENT_JAC_APPROX LM_ADD_PREFIX(levmar_fdif_cent_jac_approx) +#define LEVMAR_TRANS_MAT_MAT_MULT LM_ADD_PREFIX(levmar_trans_mat_mat_mult) +#define LEVMAR_L2NRMXMY LM_ADD_PREFIX(levmar_L2nrmxmy) +#define LEVMAR_COVAR LM_ADD_PREFIX(levmar_covar) + +#ifdef HAVE_LAPACK +#define AX_EQ_B_LU LM_ADD_PREFIX(Ax_eq_b_LU) +#define AX_EQ_B_CHOL LM_ADD_PREFIX(Ax_eq_b_Chol) +#define AX_EQ_B_QR LM_ADD_PREFIX(Ax_eq_b_QR) +#define AX_EQ_B_QRLS LM_ADD_PREFIX(Ax_eq_b_QRLS) +#define AX_EQ_B_SVD LM_ADD_PREFIX(Ax_eq_b_SVD) +#define AX_EQ_B_BK LM_ADD_PREFIX(Ax_eq_b_BK) +#else +#define AX_EQ_B_LU LM_ADD_PREFIX(Ax_eq_b_LU_noLapack) +#endif /* HAVE_LAPACK */ + +/* + * This function seeks the parameter vector p that best describes the measurements vector x. + * More precisely, given a vector function func : R^m --> R^n with n>=m, + * it finds p s.t. func(p) ~= x, i.e. the squared second order (i.e. L2) norm of + * e=x-func(p) is minimized. + * + * This function requires an analytic Jacobian. In case the latter is unavailable, + * use LEVMAR_DIF() bellow + * + * Returns the number of iterations (>=0) if successful, LM_ERROR if failed + * + * For more details, see K. Madsen, H.B. Nielsen and O. Tingleff's lecture notes on + * non-linear least squares at http://www.imm.dtu.dk/pubdb/views/edoc_download.php/3215/pdf/imm3215.pdf + */ + +int LEVMAR_DER( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), /* function to evaluate the Jacobian \part x / \part p */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[4], /* I: minim. options [\mu, \epsilon1, \epsilon2, \epsilon3]. Respectively the scale factor for initial \mu, + * stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2. Set to NULL for defaults to be used + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_DER_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func & jacf. + * Set to NULL if not needed + */ +{ +register int i, j, k, l; +int worksz, freework=0, issolved; +/* temp work arrays */ +LM_REAL *e, /* nx1 */ + *hx, /* \hat{x}_i, nx1 */ + *jacTe, /* J^T e_i mx1 */ + *jac, /* nxm */ + *jacTjac, /* mxm */ + *Dp, /* mx1 */ + *diag_jacTjac, /* diagonal of J^T J, mx1 */ + *pDp; /* p + Dp, mx1 */ + +register LM_REAL mu, /* damping constant */ + tmp; /* mainly used in matrix & vector multiplications */ +LM_REAL p_eL2, jacTe_inf, pDp_eL2; /* ||e(p)||_2, ||J^T e||_inf, ||e(p+Dp)||_2 */ +LM_REAL p_L2, Dp_L2=LM_REAL_MAX, dF, dL; +LM_REAL tau, eps1, eps2, eps2_sq, eps3; +LM_REAL init_p_eL2; +int nu=2, nu2, stop=0, nfev, njev=0, nlss=0; +const int nm=n*m; +int (*linsolver)(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m)=NULL; + + mu=jacTe_inf=0.0; /* -Wall */ + + if(n<m){ + fprintf(stderr, LCAT(LEVMAR_DER, "(): cannot solve a problem with fewer measurements [%d] than unknowns [%d]\n"), n, m); + return LM_ERROR; + } + + if(!jacf){ + fprintf(stderr, RCAT("No function specified for computing the Jacobian in ", LEVMAR_DER) + RCAT("().\nIf no such function is available, use ", LEVMAR_DIF) RCAT("() rather than ", LEVMAR_DER) "()\n"); + return LM_ERROR; + } + + if(opts){ + tau=opts[0]; + eps1=opts[1]; + eps2=opts[2]; + eps2_sq=opts[2]*opts[2]; + eps3=opts[3]; + } + else{ // use default values + tau=LM_CNST(LM_INIT_MU); + eps1=LM_CNST(LM_STOP_THRESH); + eps2=LM_CNST(LM_STOP_THRESH); + eps2_sq=LM_CNST(LM_STOP_THRESH)*LM_CNST(LM_STOP_THRESH); + eps3=LM_CNST(LM_STOP_THRESH); + } + + if(!work){ + worksz=LM_DER_WORKSZ(m, n); //2*n+4*m + n*m + m*m; + work=(LM_REAL *)malloc(worksz*sizeof(LM_REAL)); /* allocate a big chunk in one step */ + if(!work){ + fprintf(stderr, LCAT(LEVMAR_DER, "(): memory allocation request failed\n")); + return LM_ERROR; + } + freework=1; + } + + /* set up work arrays */ + e=work; + hx=e + n; + jacTe=hx + n; + jac=jacTe + m; + jacTjac=jac + nm; + Dp=jacTjac + m*m; + diag_jacTjac=Dp + m; + pDp=diag_jacTjac + m; + + /* compute e=x - f(p) and its L2 norm */ + (*func)(p, hx, m, n, adata); nfev=1; + /* ### e=x-hx, p_eL2=||e|| */ +#if 1 + p_eL2=LEVMAR_L2NRMXMY(e, x, hx, n); +#else + for(i=0, p_eL2=0.0; i<n; ++i){ + e[i]=tmp=x[i]-hx[i]; + p_eL2+=tmp*tmp; + } +#endif + init_p_eL2=p_eL2; + if(!LM_FINITE(p_eL2)) stop=7; + + for(k=0; k<itmax && !stop; ++k){ + /* Note that p and e have been updated at a previous iteration */ + + if(p_eL2<=eps3){ /* error is small */ + stop=6; + break; + } + + /* Compute the Jacobian J at p, J^T J, J^T e, ||J^T e||_inf and ||p||^2. + * Since J^T J is symmetric, its computation can be sped up by computing + * only its upper triangular part and copying it to the lower part + */ + + (*jacf)(p, jac, m, n, adata); ++njev; + + /* J^T J, J^T e */ + if(nm<__BLOCKSZ__SQ){ // this is a small problem + /* J^T*J_ij = \sum_l J^T_il * J_lj = \sum_l J_li * J_lj. + * Thus, the product J^T J can be computed using an outer loop for + * l that adds J_li*J_lj to each element ij of the result. Note that + * with this scheme, the accesses to J and JtJ are always along rows, + * therefore induces less cache misses compared to the straightforward + * algorithm for computing the product (i.e., l loop is innermost one). + * A similar scheme applies to the computation of J^T e. + * However, for large minimization problems (i.e., involving a large number + * of unknowns and measurements) for which J/J^T J rows are too large to + * fit in the L1 cache, even this scheme incures many cache misses. In + * such cases, a cache-efficient blocking scheme is preferable. + * + * Thanks to John Nitao of Lawrence Livermore Lab for pointing out this + * performance problem. + * + * Note that the non-blocking algorithm is faster on small + * problems since in this case it avoids the overheads of blocking. + */ + + /* looping downwards saves a few computations */ + register int l, im; + register LM_REAL alpha, *jaclm; + + for(i=m*m; i-->0; ) + jacTjac[i]=0.0; + for(i=m; i-->0; ) + jacTe[i]=0.0; + + for(l=n; l-->0; ){ + jaclm=jac+l*m; + for(i=m; i-->0; ){ + im=i*m; + alpha=jaclm[i]; //jac[l*m+i]; + for(j=i+1; j-->0; ) /* j<=i computes lower triangular part only */ + jacTjac[im+j]+=jaclm[j]*alpha; //jac[l*m+j] + + /* J^T e */ + jacTe[i]+=alpha*e[l]; + } + } + + for(i=m; i-->0; ) /* copy to upper part */ + for(j=i+1; j<m; ++j) + jacTjac[i*m+j]=jacTjac[j*m+i]; + + } + else{ // this is a large problem + /* Cache efficient computation of J^T J based on blocking + */ + LEVMAR_TRANS_MAT_MAT_MULT(jac, jacTjac, n, m); + + /* cache efficient computation of J^T e */ + for(i=0; i<m; ++i) + jacTe[i]=0.0; + + for(i=0; i<n; ++i){ + register LM_REAL *jacrow; + + for(l=0, jacrow=jac+i*m, tmp=e[i]; l<m; ++l) + jacTe[l]+=jacrow[l]*tmp; + } + } + + /* Compute ||J^T e||_inf and ||p||^2 */ + for(i=0, p_L2=jacTe_inf=0.0; i<m; ++i){ + if(jacTe_inf < (tmp=FABS(jacTe[i]))) jacTe_inf=tmp; + + diag_jacTjac[i]=jacTjac[i*m+i]; /* save diagonal entries so that augmentation can be later canceled */ + p_L2+=p[i]*p[i]; + } + //p_L2=sqrt(p_L2); + +#if 0 +if(!(k%100)){ + printf("Current estimate: "); + for(i=0; i<m; ++i) + printf("%.9g ", p[i]); + printf("-- errors %.9g %0.9g\n", jacTe_inf, p_eL2); +} +#endif + + /* check for convergence */ + if((jacTe_inf <= eps1)){ + Dp_L2=0.0; /* no increment for p in this case */ + stop=1; + break; + } + + /* compute initial damping factor */ + if(k==0){ + for(i=0, tmp=LM_REAL_MIN; i<m; ++i) + if(diag_jacTjac[i]>tmp) tmp=diag_jacTjac[i]; /* find max diagonal element */ + mu=tau*tmp; + } + + /* determine increment using adaptive damping */ + while(1){ + /* augment normal equations */ + for(i=0; i<m; ++i) + jacTjac[i*m+i]+=mu; + + /* solve augmented equations */ +#ifdef HAVE_LAPACK + /* 6 alternatives are available: LU, Cholesky, 2 variants of QR decomposition, SVD and LDLt. + * Cholesky is the fastest but might be inaccurate; QR is slower but more accurate; + * SVD is the slowest but most accurate; LU offers a tradeoff between accuracy and speed + */ + + issolved=AX_EQ_B_BK(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_BK; + //issolved=AX_EQ_B_LU(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_LU; + //issolved=AX_EQ_B_CHOL(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_CHOL; + //issolved=AX_EQ_B_QR(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_QR; + //issolved=AX_EQ_B_QRLS(jacTjac, jacTe, Dp, m, m); ++nlss; linsolver=(int (*)(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m))AX_EQ_B_QRLS; + //issolved=AX_EQ_B_SVD(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_SVD; + +#else + /* use the LU included with levmar */ + issolved=AX_EQ_B_LU(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_LU; +#endif /* HAVE_LAPACK */ + + if(issolved){ + /* compute p's new estimate and ||Dp||^2 */ + for(i=0, Dp_L2=0.0; i<m; ++i){ + pDp[i]=p[i] + (tmp=Dp[i]); + Dp_L2+=tmp*tmp; + } + //Dp_L2=sqrt(Dp_L2); + + if(Dp_L2<=eps2_sq*p_L2){ /* relative change in p is small, stop */ + //if(Dp_L2<=eps2*(p_L2 + eps2)){ /* relative change in p is small, stop */ + stop=2; + break; + } + + if(Dp_L2>=(p_L2+eps2)/(LM_CNST(EPSILON)*LM_CNST(EPSILON))){ /* almost singular */ + //if(Dp_L2>=(p_L2+eps2)/LM_CNST(EPSILON)){ /* almost singular */ + stop=4; + break; + } + + (*func)(pDp, hx, m, n, adata); ++nfev; /* evaluate function at p + Dp */ + /* compute ||e(pDp)||_2 */ + /* ### hx=x-hx, pDp_eL2=||hx|| */ +#if 1 + pDp_eL2=LEVMAR_L2NRMXMY(hx, x, hx, n); +#else + for(i=0, pDp_eL2=0.0; i<n; ++i){ + hx[i]=tmp=x[i]-hx[i]; + pDp_eL2+=tmp*tmp; + } +#endif + if(!LM_FINITE(pDp_eL2)){ /* sum of squares is not finite, most probably due to a user error. + * This check makes sure that the inner loop does not run indefinitely. + * Thanks to Steve Danauskas for reporting such cases + */ + stop=7; + break; + } + + for(i=0, dL=0.0; i<m; ++i) + dL+=Dp[i]*(mu*Dp[i]+jacTe[i]); + + dF=p_eL2-pDp_eL2; + + if(dL>0.0 && dF>0.0){ /* reduction in error, increment is accepted */ + tmp=(LM_CNST(2.0)*dF/dL-LM_CNST(1.0)); + tmp=LM_CNST(1.0)-tmp*tmp*tmp; + mu=mu*( (tmp>=LM_CNST(ONE_THIRD))? tmp : LM_CNST(ONE_THIRD) ); + nu=2; + + for(i=0 ; i<m; ++i) /* update p's estimate */ + p[i]=pDp[i]; + + for(i=0; i<n; ++i) /* update e and ||e||_2 */ + e[i]=hx[i]; + p_eL2=pDp_eL2; + break; + } + } + + /* if this point is reached, either the linear system could not be solved or + * the error did not reduce; in any case, the increment must be rejected + */ + + mu*=nu; + nu2=nu<<1; // 2*nu; + if(nu2<=nu){ /* nu has wrapped around (overflown). Thanks to Frank Jordan for spotting this case */ + stop=5; + break; + } + nu=nu2; + + for(i=0; i<m; ++i) /* restore diagonal J^T J entries */ + jacTjac[i*m+i]=diag_jacTjac[i]; + } /* inner loop */ + } + + if(k>=itmax) stop=3; + + for(i=0; i<m; ++i) /* restore diagonal J^T J entries */ + jacTjac[i*m+i]=diag_jacTjac[i]; + + if(info){ + info[0]=init_p_eL2; + info[1]=p_eL2; + info[2]=jacTe_inf; + info[3]=Dp_L2; + for(i=0, tmp=LM_REAL_MIN; i<m; ++i) + if(tmp<jacTjac[i*m+i]) tmp=jacTjac[i*m+i]; + info[4]=mu/tmp; + info[5]=(LM_REAL)k; + info[6]=(LM_REAL)stop; + info[7]=(LM_REAL)nfev; + info[8]=(LM_REAL)njev; + info[9]=(LM_REAL)nlss; + } + + /* covariance matrix */ + if(covar){ + LEVMAR_COVAR(jacTjac, covar, p_eL2, m, n); + } + + if(freework) free(work); + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(linsolver) (*linsolver)(NULL, NULL, NULL, 0); +#endif + + return (stop!=4 && stop!=7)? k : LM_ERROR; +} + + +/* Secant version of the LEVMAR_DER() function above: the Jacobian is approximated with + * the aid of finite differences (forward or central, see the comment for the opts argument) + */ +int LEVMAR_DIF( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[5], /* I: opts[0-4] = minim. options [\mu, \epsilon1, \epsilon2, \epsilon3, \delta]. Respectively the + * scale factor for initial \mu, stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2 and + * the step used in difference approximation to the Jacobian. Set to NULL for defaults to be used. + * If \delta<0, the Jacobian is approximated with central differences which are more accurate + * (but slower!) compared to the forward differences employed by default. + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_DIF_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func. + * Set to NULL if not needed + */ +{ +register int i, j, k, l; +int worksz, freework=0, issolved; +/* temp work arrays */ +LM_REAL *e, /* nx1 */ + *hx, /* \hat{x}_i, nx1 */ + *jacTe, /* J^T e_i mx1 */ + *jac, /* nxm */ + *jacTjac, /* mxm */ + *Dp, /* mx1 */ + *diag_jacTjac, /* diagonal of J^T J, mx1 */ + *pDp, /* p + Dp, mx1 */ + *wrk, /* nx1 */ + *wrk2; /* nx1, used only for holding a temporary e vector and when differentiating with central differences */ + +int using_ffdif=1; + +register LM_REAL mu, /* damping constant */ + tmp; /* mainly used in matrix & vector multiplications */ +LM_REAL p_eL2, jacTe_inf, pDp_eL2; /* ||e(p)||_2, ||J^T e||_inf, ||e(p+Dp)||_2 */ +LM_REAL p_L2, Dp_L2=LM_REAL_MAX, dF, dL; +LM_REAL tau, eps1, eps2, eps2_sq, eps3, delta; +LM_REAL init_p_eL2; +int nu, nu2, stop=0, nfev, njap=0, nlss=0, K=(m>=10)? m: 10, updjac, updp=1, newjac; +const int nm=n*m; +int (*linsolver)(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m)=NULL; + + mu=jacTe_inf=p_L2=0.0; /* -Wall */ + updjac=newjac=0; /* -Wall */ + + if(n<m){ + fprintf(stderr, LCAT(LEVMAR_DIF, "(): cannot solve a problem with fewer measurements [%d] than unknowns [%d]\n"), n, m); + return LM_ERROR; + } + + if(opts){ + tau=opts[0]; + eps1=opts[1]; + eps2=opts[2]; + eps2_sq=opts[2]*opts[2]; + eps3=opts[3]; + delta=opts[4]; + if(delta<0.0){ + delta=-delta; /* make positive */ + using_ffdif=0; /* use central differencing */ + } + } + else{ // use default values + tau=LM_CNST(LM_INIT_MU); + eps1=LM_CNST(LM_STOP_THRESH); + eps2=LM_CNST(LM_STOP_THRESH); + eps2_sq=LM_CNST(LM_STOP_THRESH)*LM_CNST(LM_STOP_THRESH); + eps3=LM_CNST(LM_STOP_THRESH); + delta=LM_CNST(LM_DIFF_DELTA); + } + + if(!work){ + worksz=LM_DIF_WORKSZ(m, n); //4*n+4*m + n*m + m*m; + work=(LM_REAL *)malloc(worksz*sizeof(LM_REAL)); /* allocate a big chunk in one step */ + if(!work){ + fprintf(stderr, LCAT(LEVMAR_DIF, "(): memory allocation request failed\n")); + return LM_ERROR; + } + freework=1; + } + + /* set up work arrays */ + e=work; + hx=e + n; + jacTe=hx + n; + jac=jacTe + m; + jacTjac=jac + nm; + Dp=jacTjac + m*m; + diag_jacTjac=Dp + m; + pDp=diag_jacTjac + m; + wrk=pDp + m; + wrk2=wrk + n; + + /* compute e=x - f(p) and its L2 norm */ + (*func)(p, hx, m, n, adata); nfev=1; + /* ### e=x-hx, p_eL2=||e|| */ +#if 1 + p_eL2=LEVMAR_L2NRMXMY(e, x, hx, n); +#else + for(i=0, p_eL2=0.0; i<n; ++i){ + e[i]=tmp=x[i]-hx[i]; + p_eL2+=tmp*tmp; + } +#endif + init_p_eL2=p_eL2; + if(!LM_FINITE(p_eL2)) stop=7; + + nu=20; /* force computation of J */ + + for(k=0; k<itmax && !stop; ++k){ + /* Note that p and e have been updated at a previous iteration */ + + if(p_eL2<=eps3){ /* error is small */ + stop=6; + break; + } + + /* Compute the Jacobian J at p, J^T J, J^T e, ||J^T e||_inf and ||p||^2. + * The symmetry of J^T J is again exploited for speed + */ + + if((updp && nu>16) || updjac==K){ /* compute difference approximation to J */ + if(using_ffdif){ /* use forward differences */ + LEVMAR_FDIF_FORW_JAC_APPROX(func, p, hx, wrk, delta, jac, m, n, adata); + ++njap; nfev+=m; + } + else{ /* use central differences */ + LEVMAR_FDIF_CENT_JAC_APPROX(func, p, wrk, wrk2, delta, jac, m, n, adata); + ++njap; nfev+=2*m; + } + nu=2; updjac=0; updp=0; newjac=1; + } + + if(newjac){ /* Jacobian has changed, recompute J^T J, J^t e, etc */ + newjac=0; + + /* J^T J, J^T e */ + if(nm<=__BLOCKSZ__SQ){ // this is a small problem + /* J^T*J_ij = \sum_l J^T_il * J_lj = \sum_l J_li * J_lj. + * Thus, the product J^T J can be computed using an outer loop for + * l that adds J_li*J_lj to each element ij of the result. Note that + * with this scheme, the accesses to J and JtJ are always along rows, + * therefore induces less cache misses compared to the straightforward + * algorithm for computing the product (i.e., l loop is innermost one). + * A similar scheme applies to the computation of J^T e. + * However, for large minimization problems (i.e., involving a large number + * of unknowns and measurements) for which J/J^T J rows are too large to + * fit in the L1 cache, even this scheme incures many cache misses. In + * such cases, a cache-efficient blocking scheme is preferable. + * + * Thanks to John Nitao of Lawrence Livermore Lab for pointing out this + * performance problem. + * + * Note that the non-blocking algorithm is faster on small + * problems since in this case it avoids the overheads of blocking. + */ + register int l, im; + register LM_REAL alpha, *jaclm; + + /* looping downwards saves a few computations */ + for(i=m*m; i-->0; ) + jacTjac[i]=0.0; + for(i=m; i-->0; ) + jacTe[i]=0.0; + + for(l=n; l-->0; ){ + jaclm=jac+l*m; + for(i=m; i-->0; ){ + im=i*m; + alpha=jaclm[i]; //jac[l*m+i]; + for(j=i+1; j-->0; ) /* j<=i computes lower triangular part only */ + jacTjac[im+j]+=jaclm[j]*alpha; //jac[l*m+j] + + /* J^T e */ + jacTe[i]+=alpha*e[l]; + } + } + + for(i=m; i-->0; ) /* copy to upper part */ + for(j=i+1; j<m; ++j) + jacTjac[i*m+j]=jacTjac[j*m+i]; + } + else{ // this is a large problem + /* Cache efficient computation of J^T J based on blocking + */ + LEVMAR_TRANS_MAT_MAT_MULT(jac, jacTjac, n, m); + + /* cache efficient computation of J^T e */ + for(i=0; i<m; ++i) + jacTe[i]=0.0; + + for(i=0; i<n; ++i){ + register LM_REAL *jacrow; + + for(l=0, jacrow=jac+i*m, tmp=e[i]; l<m; ++l) + jacTe[l]+=jacrow[l]*tmp; + } + } + + /* Compute ||J^T e||_inf and ||p||^2 */ + for(i=0, p_L2=jacTe_inf=0.0; i<m; ++i){ + if(jacTe_inf < (tmp=FABS(jacTe[i]))) jacTe_inf=tmp; + + diag_jacTjac[i]=jacTjac[i*m+i]; /* save diagonal entries so that augmentation can be later canceled */ + p_L2+=p[i]*p[i]; + } + //p_L2=sqrt(p_L2); + } + +#if 0 +if(!(k%100)){ + printf("Current estimate: "); + for(i=0; i<m; ++i) + printf("%.9g ", p[i]); + printf("-- errors %.9g %0.9g\n", jacTe_inf, p_eL2); +} +#endif + + /* check for convergence */ + if((jacTe_inf <= eps1)){ + Dp_L2=0.0; /* no increment for p in this case */ + stop=1; + break; + } + + /* compute initial damping factor */ + if(k==0){ + for(i=0, tmp=LM_REAL_MIN; i<m; ++i) + if(diag_jacTjac[i]>tmp) tmp=diag_jacTjac[i]; /* find max diagonal element */ + mu=tau*tmp; + } + + /* determine increment using adaptive damping */ + + /* augment normal equations */ + for(i=0; i<m; ++i) + jacTjac[i*m+i]+=mu; + + /* solve augmented equations */ +#ifdef HAVE_LAPACK + /* 6 alternatives are available: LU, Cholesky, 2 variants of QR decomposition, SVD and LDLt. + * Cholesky is the fastest but might be inaccurate; QR is slower but more accurate; + * SVD is the slowest but most accurate; LU offers a tradeoff between accuracy and speed + */ + + issolved=AX_EQ_B_BK(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_BK; + //issolved=AX_EQ_B_LU(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_LU; + //issolved=AX_EQ_B_CHOL(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_CHOL; + //issolved=AX_EQ_B_QR(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_QR; + //issolved=AX_EQ_B_QRLS(jacTjac, jacTe, Dp, m, m); ++nlss; linsolver=(int (*)(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m))AX_EQ_B_QRLS; + //issolved=AX_EQ_B_SVD(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_SVD; +#else + /* use the LU included with levmar */ + issolved=AX_EQ_B_LU(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_LU; +#endif /* HAVE_LAPACK */ + + if(issolved){ + /* compute p's new estimate and ||Dp||^2 */ + for(i=0, Dp_L2=0.0; i<m; ++i){ + pDp[i]=p[i] + (tmp=Dp[i]); + Dp_L2+=tmp*tmp; + } + //Dp_L2=sqrt(Dp_L2); + + if(Dp_L2<=eps2_sq*p_L2){ /* relative change in p is small, stop */ + //if(Dp_L2<=eps2*(p_L2 + eps2)){ /* relative change in p is small, stop */ + stop=2; + break; + } + + if(Dp_L2>=(p_L2+eps2)/(LM_CNST(EPSILON)*LM_CNST(EPSILON))){ /* almost singular */ + //if(Dp_L2>=(p_L2+eps2)/LM_CNST(EPSILON)){ /* almost singular */ + stop=4; + break; + } + + (*func)(pDp, wrk, m, n, adata); ++nfev; /* evaluate function at p + Dp */ + /* compute ||e(pDp)||_2 */ + /* ### wrk2=x-wrk, pDp_eL2=||wrk2|| */ +#if 1 + pDp_eL2=LEVMAR_L2NRMXMY(wrk2, x, wrk, n); +#else + for(i=0, pDp_eL2=0.0; i<n; ++i){ + wrk2[i]=tmp=x[i]-wrk[i]; + pDp_eL2+=tmp*tmp; + } +#endif + if(!LM_FINITE(pDp_eL2)){ /* sum of squares is not finite, most probably due to a user error. + * This check makes sure that the loop terminates early in the case + * of invalid input. Thanks to Steve Danauskas for suggesting it + */ + + stop=7; + break; + } + + dF=p_eL2-pDp_eL2; + if(updp || dF>0){ /* update jac */ + for(i=0; i<n; ++i){ + for(l=0, tmp=0.0; l<m; ++l) + tmp+=jac[i*m+l]*Dp[l]; /* (J * Dp)[i] */ + tmp=(wrk[i] - hx[i] - tmp)/Dp_L2; /* (f(p+dp)[i] - f(p)[i] - (J * Dp)[i])/(dp^T*dp) */ + for(j=0; j<m; ++j) + jac[i*m+j]+=tmp*Dp[j]; + } + ++updjac; + newjac=1; + } + + for(i=0, dL=0.0; i<m; ++i) + dL+=Dp[i]*(mu*Dp[i]+jacTe[i]); + + if(dL>0.0 && dF>0.0){ /* reduction in error, increment is accepted */ + tmp=(LM_CNST(2.0)*dF/dL-LM_CNST(1.0)); + tmp=LM_CNST(1.0)-tmp*tmp*tmp; + mu=mu*( (tmp>=LM_CNST(ONE_THIRD))? tmp : LM_CNST(ONE_THIRD) ); + nu=2; + + for(i=0 ; i<m; ++i) /* update p's estimate */ + p[i]=pDp[i]; + + for(i=0; i<n; ++i){ /* update e, hx and ||e||_2 */ + e[i]=wrk2[i]; //x[i]-wrk[i]; + hx[i]=wrk[i]; + } + p_eL2=pDp_eL2; + updp=1; + continue; + } + } + + /* if this point is reached, either the linear system could not be solved or + * the error did not reduce; in any case, the increment must be rejected + */ + + mu*=nu; + nu2=nu<<1; // 2*nu; + if(nu2<=nu){ /* nu has wrapped around (overflown). Thanks to Frank Jordan for spotting this case */ + stop=5; + break; + } + nu=nu2; + + for(i=0; i<m; ++i) /* restore diagonal J^T J entries */ + jacTjac[i*m+i]=diag_jacTjac[i]; + } + + if(k>=itmax) stop=3; + + for(i=0; i<m; ++i) /* restore diagonal J^T J entries */ + jacTjac[i*m+i]=diag_jacTjac[i]; + + if(info){ + info[0]=init_p_eL2; + info[1]=p_eL2; + info[2]=jacTe_inf; + info[3]=Dp_L2; + for(i=0, tmp=LM_REAL_MIN; i<m; ++i) + if(tmp<jacTjac[i*m+i]) tmp=jacTjac[i*m+i]; + info[4]=mu/tmp; + info[5]=(LM_REAL)k; + info[6]=(LM_REAL)stop; + info[7]=(LM_REAL)nfev; + info[8]=(LM_REAL)njap; + info[9]=(LM_REAL)nlss; + } + + /* covariance matrix */ + if(covar){ + LEVMAR_COVAR(jacTjac, covar, p_eL2, m, n); + } + + + if(freework) free(work); + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(linsolver) (*linsolver)(NULL, NULL, NULL, 0); +#endif + + return (stop!=4 && stop!=7)? k : LM_ERROR; +} + +/* undefine everything. THIS MUST REMAIN AT THE END OF THE FILE */ +#undef LEVMAR_DER +#undef LEVMAR_DIF +#undef LEVMAR_FDIF_FORW_JAC_APPROX +#undef LEVMAR_FDIF_CENT_JAC_APPROX +#undef LEVMAR_COVAR +#undef LEVMAR_TRANS_MAT_MAT_MULT +#undef LEVMAR_L2NRMXMY +#undef AX_EQ_B_LU +#undef AX_EQ_B_CHOL +#undef AX_EQ_B_QR +#undef AX_EQ_B_QRLS +#undef AX_EQ_B_SVD +#undef AX_EQ_B_BK diff --git a/sci-libs/levmar/levmar-2.5/lmbc.c b/sci-libs/levmar/levmar-2.5/lmbc.c new file mode 100644 index 000000000..17c4a2465 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmbc.c @@ -0,0 +1,85 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004-05 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************** + * Box-constrained Levenberg-Marquardt nonlinear minimization. The same core code + * is used with appropriate #defines to derive single and double precision versions, + * see also lmbc_core.c + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <float.h> + +#include "levmar.h" +#include "compiler.h" +#include "misc.h" + +#define EPSILON 1E-12 +#define ONE_THIRD 0.3333333334 /* 1.0/3.0 */ + +#if !defined(LM_DBL_PREC) && !defined(LM_SNGL_PREC) +#error At least one of LM_DBL_PREC, LM_SNGL_PREC should be defined! +#endif + + +#ifdef LM_SNGL_PREC +/* single precision (float) definitions */ +#define LM_REAL float +#define LM_PREFIX s + +#define LM_REAL_MAX FLT_MAX +#define LM_REAL_MIN -FLT_MAX + +#define LM_REAL_EPSILON FLT_EPSILON +#define __SUBCNST(x) x##F +#define LM_CNST(x) __SUBCNST(x) // force substitution + +#include "lmbc_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_MAX +#undef LM_REAL_MIN +#undef LM_REAL_EPSILON +#undef __SUBCNST +#undef LM_CNST +#endif /* LM_SNGL_PREC */ + +#ifdef LM_DBL_PREC +/* double precision definitions */ +#define LM_REAL double +#define LM_PREFIX d + +#define LM_REAL_MAX DBL_MAX +#define LM_REAL_MIN -DBL_MAX + +#define LM_REAL_EPSILON DBL_EPSILON +#define LM_CNST(x) (x) + +#include "lmbc_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_MAX +#undef LM_REAL_MIN +#undef LM_REAL_EPSILON +#undef LM_CNST +#endif /* LM_DBL_PREC */ diff --git a/sci-libs/levmar/levmar-2.5/lmbc.o b/sci-libs/levmar/levmar-2.5/lmbc.o Binary files differnew file mode 100644 index 000000000..53b0e6599 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmbc.o diff --git a/sci-libs/levmar/levmar-2.5/lmbc_core.c b/sci-libs/levmar/levmar-2.5/lmbc_core.c new file mode 100644 index 000000000..ca0fdb054 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmbc_core.c @@ -0,0 +1,948 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004-05 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#ifndef LM_REAL // not included by lmbc.c +#error This file should not be compiled directly! +#endif + + +/* precision-specific definitions */ +#define FUNC_STATE LM_ADD_PREFIX(func_state) +#define LNSRCH LM_ADD_PREFIX(lnsrch) +#define BOXPROJECT LM_ADD_PREFIX(boxProject) +#define LEVMAR_BOX_CHECK LM_ADD_PREFIX(levmar_box_check) +#define LEVMAR_BC_DER LM_ADD_PREFIX(levmar_bc_der) +#define LEVMAR_BC_DIF LM_ADD_PREFIX(levmar_bc_dif) +#define LEVMAR_FDIF_FORW_JAC_APPROX LM_ADD_PREFIX(levmar_fdif_forw_jac_approx) +#define LEVMAR_FDIF_CENT_JAC_APPROX LM_ADD_PREFIX(levmar_fdif_cent_jac_approx) +#define LEVMAR_TRANS_MAT_MAT_MULT LM_ADD_PREFIX(levmar_trans_mat_mat_mult) +#define LEVMAR_L2NRMXMY LM_ADD_PREFIX(levmar_L2nrmxmy) +#define LEVMAR_COVAR LM_ADD_PREFIX(levmar_covar) +#define LMBC_DIF_DATA LM_ADD_PREFIX(lmbc_dif_data) +#define LMBC_DIF_FUNC LM_ADD_PREFIX(lmbc_dif_func) +#define LMBC_DIF_JACF LM_ADD_PREFIX(lmbc_dif_jacf) + +#ifdef HAVE_LAPACK +#define AX_EQ_B_LU LM_ADD_PREFIX(Ax_eq_b_LU) +#define AX_EQ_B_CHOL LM_ADD_PREFIX(Ax_eq_b_Chol) +#define AX_EQ_B_QR LM_ADD_PREFIX(Ax_eq_b_QR) +#define AX_EQ_B_QRLS LM_ADD_PREFIX(Ax_eq_b_QRLS) +#define AX_EQ_B_SVD LM_ADD_PREFIX(Ax_eq_b_SVD) +#define AX_EQ_B_BK LM_ADD_PREFIX(Ax_eq_b_BK) +#else +#define AX_EQ_B_LU LM_ADD_PREFIX(Ax_eq_b_LU_noLapack) +#endif /* HAVE_LAPACK */ + +/* find the median of 3 numbers */ +#define __MEDIAN3(a, b, c) ( ((a) >= (b))?\ + ( ((c) >= (a))? (a) : ( ((c) <= (b))? (b) : (c) ) ) : \ + ( ((c) >= (b))? (b) : ( ((c) <= (a))? (a) : (c) ) ) ) + +#define _POW_ LM_CNST(2.1) + +#define __LSITMAX 150 // max #iterations for line search + +struct FUNC_STATE{ + int n, *nfev; + LM_REAL *hx, *x; + void *adata; +}; + +static void +LNSRCH(int m, LM_REAL *x, LM_REAL f, LM_REAL *g, LM_REAL *p, LM_REAL alpha, LM_REAL *xpls, + LM_REAL *ffpls, void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), struct FUNC_STATE state, + int *mxtake, int *iretcd, LM_REAL stepmx, LM_REAL steptl, LM_REAL *sx) +{ +/* Find a next newton iterate by backtracking line search. + * Specifically, finds a \lambda such that for a fixed alpha<0.5 (usually 1e-4), + * f(x + \lambda*p) <= f(x) + alpha * \lambda * g^T*p + * + * Translated (with minor changes) from Schnabel, Koontz & Weiss uncmin.f, v1.3 + + * PARAMETERS : + + * m --> dimension of problem (i.e. number of variables) + * x(m) --> old iterate: x[k-1] + * f --> function value at old iterate, f(x) + * g(m) --> gradient at old iterate, g(x), or approximate + * p(m) --> non-zero newton step + * alpha --> fixed constant < 0.5 for line search (see above) + * xpls(m) <-- new iterate x[k] + * ffpls <-- function value at new iterate, f(xpls) + * func --> name of subroutine to evaluate function + * state <--> information other than x and m that func requires. + * state is not modified in xlnsrch (but can be modified by func). + * iretcd <-- return code + * mxtake <-- boolean flag indicating step of maximum length used + * stepmx --> maximum allowable step size + * steptl --> relative step size at which successive iterates + * considered close enough to terminate algorithm + * sx(m) --> diagonal scaling matrix for x, can be NULL + + * internal variables + + * sln newton length + * rln relative length of newton step +*/ + + register int i, j; + int firstback = 1; + LM_REAL disc; + LM_REAL a3, b; + LM_REAL t1, t2, t3, lambda, tlmbda, rmnlmb; + LM_REAL scl, rln, sln, slp; + LM_REAL tmp1, tmp2; + LM_REAL fpls, pfpls = 0., plmbda = 0.; /* -Wall */ + + f*=LM_CNST(0.5); + *mxtake = 0; + *iretcd = 2; + tmp1 = 0.; + if(!sx) /* no scaling */ + for (i = 0; i < m; ++i) + tmp1 += p[i] * p[i]; + else + for (i = 0; i < m; ++i) + tmp1 += sx[i] * sx[i] * p[i] * p[i]; + sln = (LM_REAL)sqrt(tmp1); + if (sln > stepmx) { + /* newton step longer than maximum allowed */ + scl = stepmx / sln; + for(i=0; i<m; ++i) /* p * scl */ + p[i]*=scl; + sln = stepmx; + } + for(i=0, slp=0.; i<m; ++i) /* g^T * p */ + slp+=g[i]*p[i]; + rln = 0.; + if(!sx) /* no scaling */ + for (i = 0; i < m; ++i) { + tmp1 = (FABS(x[i])>=LM_CNST(1.))? FABS(x[i]) : LM_CNST(1.); + tmp2 = FABS(p[i])/tmp1; + if(rln < tmp2) rln = tmp2; + } + else + for (i = 0; i < m; ++i) { + tmp1 = (FABS(x[i])>=LM_CNST(1.)/sx[i])? FABS(x[i]) : LM_CNST(1.)/sx[i]; + tmp2 = FABS(p[i])/tmp1; + if(rln < tmp2) rln = tmp2; + } + rmnlmb = steptl / rln; + lambda = LM_CNST(1.0); + + /* check if new iterate satisfactory. generate new lambda if necessary. */ + + for(j=__LSITMAX; j>=0; --j) { + for (i = 0; i < m; ++i) + xpls[i] = x[i] + lambda * p[i]; + + /* evaluate function at new point */ + (*func)(xpls, state.hx, m, state.n, state.adata); ++(*(state.nfev)); + /* ### state.hx=state.x-state.hx, tmp1=||state.hx|| */ +#if 1 + tmp1=LEVMAR_L2NRMXMY(state.hx, state.x, state.hx, state.n); +#else + for(i=0, tmp1=0.0; i<state.n; ++i){ + state.hx[i]=tmp2=state.x[i]-state.hx[i]; + tmp1+=tmp2*tmp2; + } +#endif + fpls=LM_CNST(0.5)*tmp1; *ffpls=tmp1; + + if (fpls <= f + slp * alpha * lambda) { /* solution found */ + *iretcd = 0; + if (lambda == LM_CNST(1.) && sln > stepmx * LM_CNST(.99)) *mxtake = 1; + return; + } + + /* else : solution not (yet) found */ + + /* First find a point with a finite value */ + + if (lambda < rmnlmb) { + /* no satisfactory xpls found sufficiently distinct from x */ + + *iretcd = 1; + return; + } + else { /* calculate new lambda */ + + /* modifications to cover non-finite values */ + if (!LM_FINITE(fpls)) { + lambda *= LM_CNST(0.1); + firstback = 1; + } + else { + if (firstback) { /* first backtrack: quadratic fit */ + tlmbda = -lambda * slp / ((fpls - f - slp) * LM_CNST(2.)); + firstback = 0; + } + else { /* all subsequent backtracks: cubic fit */ + t1 = fpls - f - lambda * slp; + t2 = pfpls - f - plmbda * slp; + t3 = LM_CNST(1.) / (lambda - plmbda); + a3 = LM_CNST(3.) * t3 * (t1 / (lambda * lambda) + - t2 / (plmbda * plmbda)); + b = t3 * (t2 * lambda / (plmbda * plmbda) + - t1 * plmbda / (lambda * lambda)); + disc = b * b - a3 * slp; + if (disc > b * b) + /* only one positive critical point, must be minimum */ + tlmbda = (-b + ((a3 < 0)? -(LM_REAL)sqrt(disc): (LM_REAL)sqrt(disc))) /a3; + else + /* both critical points positive, first is minimum */ + tlmbda = (-b + ((a3 < 0)? (LM_REAL)sqrt(disc): -(LM_REAL)sqrt(disc))) /a3; + + if (tlmbda > lambda * LM_CNST(.5)) + tlmbda = lambda * LM_CNST(.5); + } + plmbda = lambda; + pfpls = fpls; + if (tlmbda < lambda * LM_CNST(.1)) + lambda *= LM_CNST(.1); + else + lambda = tlmbda; + } + } + } + /* this point is reached when the iterations limit is exceeded */ + *iretcd = 1; /* failed */ + return; +} /* LNSRCH */ + +/* Projections to feasible set \Omega: P_{\Omega}(y) := arg min { ||x - y|| : x \in \Omega}, y \in R^m */ + +/* project vector p to a box shaped feasible set. p is a mx1 vector. + * Either lb, ub can be NULL. If not NULL, they are mx1 vectors + */ +static void BOXPROJECT(LM_REAL *p, LM_REAL *lb, LM_REAL *ub, int m) +{ +register int i; + + if(!lb){ /* no lower bounds */ + if(!ub) /* no upper bounds */ + return; + else{ /* upper bounds only */ + for(i=0; i<m; ++i) + if(p[i]>ub[i]) p[i]=ub[i]; + } + } + else + if(!ub){ /* lower bounds only */ + for(i=0; i<m; ++i) + if(p[i]<lb[i]) p[i]=lb[i]; + } + else /* box bounds */ + for(i=0; i<m; ++i) + p[i]=__MEDIAN3(lb[i], p[i], ub[i]); +} + +/* + * This function seeks the parameter vector p that best describes the measurements + * vector x under box constraints. + * More precisely, given a vector function func : R^m --> R^n with n>=m, + * it finds p s.t. func(p) ~= x, i.e. the squared second order (i.e. L2) norm of + * e=x-func(p) is minimized under the constraints lb[i]<=p[i]<=ub[i]. + * If no lower bound constraint applies for p[i], use -DBL_MAX/-FLT_MAX for lb[i]; + * If no upper bound constraint applies for p[i], use DBL_MAX/FLT_MAX for ub[i]. + * + * This function requires an analytic Jacobian. In case the latter is unavailable, + * use LEVMAR_BC_DIF() bellow + * + * Returns the number of iterations (>=0) if successful, LM_ERROR if failed + * + * For details, see C. Kanzow, N. Yamashita and M. Fukushima: "Levenberg-Marquardt + * methods for constrained nonlinear equations with strong local convergence properties", + * Journal of Computational and Applied Mathematics 172, 2004, pp. 375-397. + * Also, see K. Madsen, H.B. Nielsen and O. Tingleff's lecture notes on + * unconstrained Levenberg-Marquardt at http://www.imm.dtu.dk/pubdb/views/edoc_download.php/3215/pdf/imm3215.pdf + */ + +int LEVMAR_BC_DER( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), /* function to evaluate the Jacobian \part x / \part p */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + LM_REAL *lb, /* I: vector of lower bounds. If NULL, no lower bounds apply */ + LM_REAL *ub, /* I: vector of upper bounds. If NULL, no upper bounds apply */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[4], /* I: minim. options [\mu, \epsilon1, \epsilon2, \epsilon3]. Respectively the scale factor for initial \mu, + * stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2. Set to NULL for defaults to be used. + * Note that ||J^T e||_inf is computed on free (not equal to lb[i] or ub[i]) variables only. + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_BC_DER_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func & jacf. + * Set to NULL if not needed + */ +{ +register int i, j, k, l; +int worksz, freework=0, issolved; +/* temp work arrays */ +LM_REAL *e, /* nx1 */ + *hx, /* \hat{x}_i, nx1 */ + *jacTe, /* J^T e_i mx1 */ + *jac, /* nxm */ + *jacTjac, /* mxm */ + *Dp, /* mx1 */ + *diag_jacTjac, /* diagonal of J^T J, mx1 */ + *pDp; /* p + Dp, mx1 */ + +register LM_REAL mu, /* damping constant */ + tmp; /* mainly used in matrix & vector multiplications */ +LM_REAL p_eL2, jacTe_inf, pDp_eL2; /* ||e(p)||_2, ||J^T e||_inf, ||e(p+Dp)||_2 */ +LM_REAL p_L2, Dp_L2=LM_REAL_MAX, dF, dL; +LM_REAL tau, eps1, eps2, eps2_sq, eps3; +LM_REAL init_p_eL2; +int nu=2, nu2, stop=0, nfev, njev=0, nlss=0; +const int nm=n*m; + +/* variables for constrained LM */ +struct FUNC_STATE fstate; +LM_REAL alpha=LM_CNST(1e-4), beta=LM_CNST(0.9), gamma=LM_CNST(0.99995), gamma_sq=gamma*gamma, rho=LM_CNST(1e-8); +LM_REAL t, t0; +LM_REAL steptl=LM_CNST(1e3)*(LM_REAL)sqrt(LM_REAL_EPSILON), jacTeDp; +LM_REAL tmin=LM_CNST(1e-12), tming=LM_CNST(1e-18); /* minimum step length for LS and PG steps */ +const LM_REAL tini=LM_CNST(1.0); /* initial step length for LS and PG steps */ +int nLMsteps=0, nLSsteps=0, nPGsteps=0, gprevtaken=0; +int numactive; +int (*linsolver)(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m)=NULL; + + mu=jacTe_inf=t=0.0; tmin=tmin; /* -Wall */ + + if(n<m){ + fprintf(stderr, LCAT(LEVMAR_BC_DER, "(): cannot solve a problem with fewer measurements [%d] than unknowns [%d]\n"), n, m); + return LM_ERROR; + } + + if(!jacf){ + fprintf(stderr, RCAT("No function specified for computing the Jacobian in ", LEVMAR_BC_DER) + RCAT("().\nIf no such function is available, use ", LEVMAR_BC_DIF) RCAT("() rather than ", LEVMAR_BC_DER) "()\n"); + return LM_ERROR; + } + + if(!LEVMAR_BOX_CHECK(lb, ub, m)){ + fprintf(stderr, LCAT(LEVMAR_BC_DER, "(): at least one lower bound exceeds the upper one\n")); + return LM_ERROR; + } + + if(opts){ + tau=opts[0]; + eps1=opts[1]; + eps2=opts[2]; + eps2_sq=opts[2]*opts[2]; + eps3=opts[3]; + } + else{ // use default values + tau=LM_CNST(LM_INIT_MU); + eps1=LM_CNST(LM_STOP_THRESH); + eps2=LM_CNST(LM_STOP_THRESH); + eps2_sq=LM_CNST(LM_STOP_THRESH)*LM_CNST(LM_STOP_THRESH); + eps3=LM_CNST(LM_STOP_THRESH); + } + + if(!work){ + worksz=LM_BC_DER_WORKSZ(m, n); //2*n+4*m + n*m + m*m; + work=(LM_REAL *)malloc(worksz*sizeof(LM_REAL)); /* allocate a big chunk in one step */ + if(!work){ + fprintf(stderr, LCAT(LEVMAR_BC_DER, "(): memory allocation request failed\n")); + return LM_ERROR; + } + freework=1; + } + + /* set up work arrays */ + e=work; + hx=e + n; + jacTe=hx + n; + jac=jacTe + m; + jacTjac=jac + nm; + Dp=jacTjac + m*m; + diag_jacTjac=Dp + m; + pDp=diag_jacTjac + m; + + fstate.n=n; + fstate.hx=hx; + fstate.x=x; + fstate.adata=adata; + fstate.nfev=&nfev; + + /* see if starting point is within the feasile set */ + for(i=0; i<m; ++i) + pDp[i]=p[i]; + BOXPROJECT(p, lb, ub, m); /* project to feasible set */ + for(i=0; i<m; ++i) + if(pDp[i]!=p[i]) + fprintf(stderr, RCAT("Warning: component %d of starting point not feasible in ", LEVMAR_BC_DER) "()! [%g projected to %g]\n", + i, pDp[i], p[i]); + + /* compute e=x - f(p) and its L2 norm */ + (*func)(p, hx, m, n, adata); nfev=1; + /* ### e=x-hx, p_eL2=||e|| */ +#if 1 + p_eL2=LEVMAR_L2NRMXMY(e, x, hx, n); +#else + for(i=0, p_eL2=0.0; i<n; ++i){ + e[i]=tmp=x[i]-hx[i]; + p_eL2+=tmp*tmp; + } +#endif + init_p_eL2=p_eL2; + if(!LM_FINITE(p_eL2)) stop=7; + + for(k=0; k<itmax && !stop; ++k){ + /* Note that p and e have been updated at a previous iteration */ + + if(p_eL2<=eps3){ /* error is small */ + stop=6; + break; + } + + /* Compute the Jacobian J at p, J^T J, J^T e, ||J^T e||_inf and ||p||^2. + * Since J^T J is symmetric, its computation can be sped up by computing + * only its upper triangular part and copying it to the lower part + */ + + (*jacf)(p, jac, m, n, adata); ++njev; + + /* J^T J, J^T e */ + if(nm<__BLOCKSZ__SQ){ // this is a small problem + /* J^T*J_ij = \sum_l J^T_il * J_lj = \sum_l J_li * J_lj. + * Thus, the product J^T J can be computed using an outer loop for + * l that adds J_li*J_lj to each element ij of the result. Note that + * with this scheme, the accesses to J and JtJ are always along rows, + * therefore induces less cache misses compared to the straightforward + * algorithm for computing the product (i.e., l loop is innermost one). + * A similar scheme applies to the computation of J^T e. + * However, for large minimization problems (i.e., involving a large number + * of unknowns and measurements) for which J/J^T J rows are too large to + * fit in the L1 cache, even this scheme incures many cache misses. In + * such cases, a cache-efficient blocking scheme is preferable. + * + * Thanks to John Nitao of Lawrence Livermore Lab for pointing out this + * performance problem. + * + * Note that the non-blocking algorithm is faster on small + * problems since in this case it avoids the overheads of blocking. + */ + register int l, im; + register LM_REAL alpha, *jaclm; + + /* looping downwards saves a few computations */ + for(i=m*m; i-->0; ) + jacTjac[i]=0.0; + for(i=m; i-->0; ) + jacTe[i]=0.0; + + for(l=n; l-->0; ){ + jaclm=jac+l*m; + for(i=m; i-->0; ){ + im=i*m; + alpha=jaclm[i]; //jac[l*m+i]; + for(j=i+1; j-->0; ) /* j<=i computes lower triangular part only */ + jacTjac[im+j]+=jaclm[j]*alpha; //jac[l*m+j] + + /* J^T e */ + jacTe[i]+=alpha*e[l]; + } + } + + for(i=m; i-->0; ) /* copy to upper part */ + for(j=i+1; j<m; ++j) + jacTjac[i*m+j]=jacTjac[j*m+i]; + } + else{ // this is a large problem + /* Cache efficient computation of J^T J based on blocking + */ + LEVMAR_TRANS_MAT_MAT_MULT(jac, jacTjac, n, m); + + /* cache efficient computation of J^T e */ + for(i=0; i<m; ++i) + jacTe[i]=0.0; + + for(i=0; i<n; ++i){ + register LM_REAL *jacrow; + + for(l=0, jacrow=jac+i*m, tmp=e[i]; l<m; ++l) + jacTe[l]+=jacrow[l]*tmp; + } + } + + /* Compute ||J^T e||_inf and ||p||^2. Note that ||J^T e||_inf + * is computed for free (i.e. inactive) variables only. + * At a local minimum, if p[i]==ub[i] then g[i]>0; + * if p[i]==lb[i] g[i]<0; otherwise g[i]=0 + */ + for(i=j=numactive=0, p_L2=jacTe_inf=0.0; i<m; ++i){ + if(ub && p[i]==ub[i]){ ++numactive; if(jacTe[i]>0.0) ++j; } + else if(lb && p[i]==lb[i]){ ++numactive; if(jacTe[i]<0.0) ++j; } + else if(jacTe_inf < (tmp=FABS(jacTe[i]))) jacTe_inf=tmp; + + diag_jacTjac[i]=jacTjac[i*m+i]; /* save diagonal entries so that augmentation can be later canceled */ + p_L2+=p[i]*p[i]; + } + //p_L2=sqrt(p_L2); + +#if 0 +if(!(k%100)){ + printf("Current estimate: "); + for(i=0; i<m; ++i) + printf("%.9g ", p[i]); + printf("-- errors %.9g %0.9g, #active %d [%d]\n", jacTe_inf, p_eL2, numactive, j); +} +#endif + + /* check for convergence */ + if(j==numactive && (jacTe_inf <= eps1)){ + Dp_L2=0.0; /* no increment for p in this case */ + stop=1; + break; + } + + /* compute initial damping factor */ + if(k==0){ + if(!lb && !ub){ /* no bounds */ + for(i=0, tmp=LM_REAL_MIN; i<m; ++i) + if(diag_jacTjac[i]>tmp) tmp=diag_jacTjac[i]; /* find max diagonal element */ + mu=tau*tmp; + } + else + mu=LM_CNST(0.5)*tau*p_eL2; /* use Kanzow's starting mu */ + } + + /* determine increment using a combination of adaptive damping, line search and projected gradient search */ + while(1){ + /* augment normal equations */ + for(i=0; i<m; ++i) + jacTjac[i*m+i]+=mu; + + /* solve augmented equations */ +#ifdef HAVE_LAPACK + /* 6 alternatives are available: LU, Cholesky, 2 variants of QR decomposition, SVD and LDLt. + * Cholesky is the fastest but might be inaccurate; QR is slower but more accurate; + * SVD is the slowest but most accurate; LU offers a tradeoff between accuracy and speed + */ + + issolved=AX_EQ_B_BK(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_BK; + //issolved=AX_EQ_B_LU(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_LU; + //issolved=AX_EQ_B_CHOL(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_CHOL; + //issolved=AX_EQ_B_QR(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_QR; + //issolved=AX_EQ_B_QRLS(jacTjac, jacTe, Dp, m, m); ++nlss; linsolver=(int (*)(LM_REAL *A, LM_REAL *B, LM_REAL *x, int m))AX_EQ_B_QRLS; + //issolved=AX_EQ_B_SVD(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_SVD; + +#else + /* use the LU included with levmar */ + issolved=AX_EQ_B_LU(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_LU; +#endif /* HAVE_LAPACK */ + + if(issolved){ + for(i=0; i<m; ++i) + pDp[i]=p[i] + Dp[i]; + + /* compute p's new estimate and ||Dp||^2 */ + BOXPROJECT(pDp, lb, ub, m); /* project to feasible set */ + for(i=0, Dp_L2=0.0; i<m; ++i){ + Dp[i]=tmp=pDp[i]-p[i]; + Dp_L2+=tmp*tmp; + } + //Dp_L2=sqrt(Dp_L2); + + if(Dp_L2<=eps2_sq*p_L2){ /* relative change in p is small, stop */ + stop=2; + break; + } + + if(Dp_L2>=(p_L2+eps2)/(LM_CNST(EPSILON)*LM_CNST(EPSILON))){ /* almost singular */ + stop=4; + break; + } + + (*func)(pDp, hx, m, n, adata); ++nfev; /* evaluate function at p + Dp */ + /* ### hx=x-hx, pDp_eL2=||hx|| */ +#if 1 + pDp_eL2=LEVMAR_L2NRMXMY(hx, x, hx, n); +#else + for(i=0, pDp_eL2=0.0; i<n; ++i){ /* compute ||e(pDp)||_2 */ + hx[i]=tmp=x[i]-hx[i]; + pDp_eL2+=tmp*tmp; + } +#endif + if(!LM_FINITE(pDp_eL2)){ + stop=7; + break; + } + + if(pDp_eL2<=gamma_sq*p_eL2){ + for(i=0, dL=0.0; i<m; ++i) + dL+=Dp[i]*(mu*Dp[i]+jacTe[i]); + +#if 1 + if(dL>0.0){ + dF=p_eL2-pDp_eL2; + tmp=(LM_CNST(2.0)*dF/dL-LM_CNST(1.0)); + tmp=LM_CNST(1.0)-tmp*tmp*tmp; + mu=mu*( (tmp>=LM_CNST(ONE_THIRD))? tmp : LM_CNST(ONE_THIRD) ); + } + else + mu=(mu>=pDp_eL2)? pDp_eL2 : mu; /* pDp_eL2 is the new pDp_eL2 */ +#else + + mu=(mu>=pDp_eL2)? pDp_eL2 : mu; /* pDp_eL2 is the new pDp_eL2 */ +#endif + + nu=2; + + for(i=0 ; i<m; ++i) /* update p's estimate */ + p[i]=pDp[i]; + + for(i=0; i<n; ++i) /* update e and ||e||_2 */ + e[i]=hx[i]; + p_eL2=pDp_eL2; + ++nLMsteps; + gprevtaken=0; + break; + } + } + else{ + + /* the augmented linear system could not be solved, increase mu */ + + mu*=nu; + nu2=nu<<1; // 2*nu; + if(nu2<=nu){ /* nu has wrapped around (overflown). Thanks to Frank Jordan for spotting this case */ + stop=5; + break; + } + nu=nu2; + + for(i=0; i<m; ++i) /* restore diagonal J^T J entries */ + jacTjac[i*m+i]=diag_jacTjac[i]; + + continue; /* solve again with increased nu */ + } + + /* if this point is reached, the LM step did not reduce the error; + * see if it is a descent direction + */ + + /* negate jacTe (i.e. g) & compute g^T * Dp */ + for(i=0, jacTeDp=0.0; i<m; ++i){ + jacTe[i]=-jacTe[i]; + jacTeDp+=jacTe[i]*Dp[i]; + } + + if(jacTeDp<=-rho*pow(Dp_L2, _POW_/LM_CNST(2.0))){ + /* Dp is a descent direction; do a line search along it */ + int mxtake, iretcd; + LM_REAL stepmx; + + tmp=(LM_REAL)sqrt(p_L2); stepmx=LM_CNST(1e3)*( (tmp>=LM_CNST(1.0))? tmp : LM_CNST(1.0) ); + +#if 1 + /* use Schnabel's backtracking line search; it requires fewer "func" evaluations */ + LNSRCH(m, p, p_eL2, jacTe, Dp, alpha, pDp, &pDp_eL2, func, fstate, + &mxtake, &iretcd, stepmx, steptl, NULL); /* NOTE: LNSRCH() updates hx */ + if(iretcd!=0) goto gradproj; /* rather inelegant but effective way to handle LNSRCH() failures... */ +#else + /* use the simpler (but slower!) line search described by Kanzow et al */ + for(t=tini; t>tmin; t*=beta){ + for(i=0; i<m; ++i){ + pDp[i]=p[i] + t*Dp[i]; + //pDp[i]=__MEDIAN3(lb[i], pDp[i], ub[i]); /* project to feasible set */ + } + + (*func)(pDp, hx, m, n, adata); ++nfev; /* evaluate function at p + t*Dp */ + for(i=0, pDp_eL2=0.0; i<n; ++i){ /* compute ||e(pDp)||_2 */ + hx[i]=tmp=x[i]-hx[i]; + pDp_eL2+=tmp*tmp; + } + if(!LM_FINITE(pDp_eL2)) goto gradproj; /* treat as line search failure */ + + //if(LM_CNST(0.5)*pDp_eL2<=LM_CNST(0.5)*p_eL2 + t*alpha*jacTeDp) break; + if(pDp_eL2<=p_eL2 + LM_CNST(2.0)*t*alpha*jacTeDp) break; + } +#endif + ++nLSsteps; + gprevtaken=0; + + /* NOTE: new estimate for p is in pDp, associated error in hx and its norm in pDp_eL2. + * These values are used below to update their corresponding variables + */ + } + else{ +gradproj: /* Note that this point can also be reached via a goto when LNSRCH() fails */ + + /* jacTe is a descent direction; make a projected gradient step */ + + /* if the previous step was along the gradient descent, try to use the t employed in that step */ + /* compute ||g|| */ + for(i=0, tmp=0.0; i<m; ++i) + tmp+=jacTe[i]*jacTe[i]; + tmp=(LM_REAL)sqrt(tmp); + tmp=LM_CNST(100.0)/(LM_CNST(1.0)+tmp); + t0=(tmp<=tini)? tmp : tini; /* guard against poor scaling & large steps; see (3.50) in C.T. Kelley's book */ + + for(t=(gprevtaken)? t : t0; t>tming; t*=beta){ + for(i=0; i<m; ++i) + pDp[i]=p[i] - t*jacTe[i]; + BOXPROJECT(pDp, lb, ub, m); /* project to feasible set */ + for(i=0; i<m; ++i) + Dp[i]=pDp[i]-p[i]; + + (*func)(pDp, hx, m, n, adata); ++nfev; /* evaluate function at p - t*g */ + /* compute ||e(pDp)||_2 */ + /* ### hx=x-hx, pDp_eL2=||hx|| */ +#if 1 + pDp_eL2=LEVMAR_L2NRMXMY(hx, x, hx, n); +#else + for(i=0, pDp_eL2=0.0; i<n; ++i){ + hx[i]=tmp=x[i]-hx[i]; + pDp_eL2+=tmp*tmp; + } +#endif + if(!LM_FINITE(pDp_eL2)){ + stop=7; + goto breaknested; + } + + for(i=0, tmp=0.0; i<m; ++i) /* compute ||g^T * Dp|| */ + tmp+=jacTe[i]*Dp[i]; + + if(gprevtaken && pDp_eL2<=p_eL2 + LM_CNST(2.0)*LM_CNST(0.99999)*tmp){ /* starting t too small */ + t=t0; + gprevtaken=0; + continue; + } + //if(LM_CNST(0.5)*pDp_eL2<=LM_CNST(0.5)*p_eL2 + alpha*tmp) break; + if(pDp_eL2<=p_eL2 + LM_CNST(2.0)*alpha*tmp) break; + } + + ++nPGsteps; + gprevtaken=1; + /* NOTE: new estimate for p is in pDp, associated error in hx and its norm in pDp_eL2 */ + } + + /* update using computed values */ + + for(i=0, Dp_L2=0.0; i<m; ++i){ + tmp=pDp[i]-p[i]; + Dp_L2+=tmp*tmp; + } + //Dp_L2=sqrt(Dp_L2); + + if(Dp_L2<=eps2_sq*p_L2){ /* relative change in p is small, stop */ + stop=2; + break; + } + + for(i=0 ; i<m; ++i) /* update p's estimate */ + p[i]=pDp[i]; + + for(i=0; i<n; ++i) /* update e and ||e||_2 */ + e[i]=hx[i]; + p_eL2=pDp_eL2; + break; + } /* inner loop */ + } + +breaknested: /* NOTE: this point is also reached via an explicit goto! */ + + if(k>=itmax) stop=3; + + for(i=0; i<m; ++i) /* restore diagonal J^T J entries */ + jacTjac[i*m+i]=diag_jacTjac[i]; + + if(info){ + info[0]=init_p_eL2; + info[1]=p_eL2; + info[2]=jacTe_inf; + info[3]=Dp_L2; + for(i=0, tmp=LM_REAL_MIN; i<m; ++i) + if(tmp<jacTjac[i*m+i]) tmp=jacTjac[i*m+i]; + info[4]=mu/tmp; + info[5]=(LM_REAL)k; + info[6]=(LM_REAL)stop; + info[7]=(LM_REAL)nfev; + info[8]=(LM_REAL)njev; + info[9]=(LM_REAL)nlss; + } + + /* covariance matrix */ + if(covar){ + LEVMAR_COVAR(jacTjac, covar, p_eL2, m, n); + } + + if(freework) free(work); + +#ifdef LINSOLVERS_RETAIN_MEMORY + if(linsolver) (*linsolver)(NULL, NULL, NULL, 0); +#endif + +#if 0 +printf("%d LM steps, %d line search, %d projected gradient\n", nLMsteps, nLSsteps, nPGsteps); +#endif + + return (stop!=4 && stop!=7)? k : LM_ERROR; +} + +/* following struct & LMBC_DIF_XXX functions won't be necessary if a true secant + * version of LEVMAR_BC_DIF() is implemented... + */ +struct LMBC_DIF_DATA{ + int ffdif; // nonzero if forward differencing is used + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata); + LM_REAL *hx, *hxx; + void *adata; + LM_REAL delta; +}; + +static void LMBC_DIF_FUNC(LM_REAL *p, LM_REAL *hx, int m, int n, void *data) +{ +struct LMBC_DIF_DATA *dta=(struct LMBC_DIF_DATA *)data; + + /* call user-supplied function passing it the user-supplied data */ + (*(dta->func))(p, hx, m, n, dta->adata); +} + +static void LMBC_DIF_JACF(LM_REAL *p, LM_REAL *jac, int m, int n, void *data) +{ +struct LMBC_DIF_DATA *dta=(struct LMBC_DIF_DATA *)data; + + if(dta->ffdif){ + /* evaluate user-supplied function at p */ + (*(dta->func))(p, dta->hx, m, n, dta->adata); + LEVMAR_FDIF_FORW_JAC_APPROX(dta->func, p, dta->hx, dta->hxx, dta->delta, jac, m, n, dta->adata); + } + else + LEVMAR_FDIF_CENT_JAC_APPROX(dta->func, p, dta->hx, dta->hxx, dta->delta, jac, m, n, dta->adata); +} + + +/* No Jacobian version of the LEVMAR_BC_DER() function above: the Jacobian is approximated with + * the aid of finite differences (forward or central, see the comment for the opts argument) + * Ideally, this function should be implemented with a secant approach. Currently, it just calls + * LEVMAR_BC_DER() + */ +int LEVMAR_BC_DIF( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + LM_REAL *lb, /* I: vector of lower bounds. If NULL, no lower bounds apply */ + LM_REAL *ub, /* I: vector of upper bounds. If NULL, no upper bounds apply */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[5], /* I: opts[0-4] = minim. options [\mu, \epsilon1, \epsilon2, \epsilon3, \delta]. Respectively the + * scale factor for initial \mu, stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2 and + * the step used in difference approximation to the Jacobian. Set to NULL for defaults to be used. + * If \delta<0, the Jacobian is approximated with central differences which are more accurate + * (but slower!) compared to the forward differences employed by default. + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_BC_DIF_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func. + * Set to NULL if not needed + */ +{ +struct LMBC_DIF_DATA data; +int ret; + + //fprintf(stderr, RCAT("\nWarning: current implementation of ", LEVMAR_BC_DIF) "() does not use a secant approach!\n\n"); + + data.ffdif=!opts || opts[4]>=0.0; + + data.func=func; + data.hx=(LM_REAL *)malloc(2*n*sizeof(LM_REAL)); /* allocate a big chunk in one step */ + if(!data.hx){ + fprintf(stderr, LCAT(LEVMAR_BC_DIF, "(): memory allocation request failed\n")); + return LM_ERROR; + } + data.hxx=data.hx+n; + data.adata=adata; + data.delta=(opts)? FABS(opts[4]) : (LM_REAL)LM_DIFF_DELTA; + + ret=LEVMAR_BC_DER(LMBC_DIF_FUNC, LMBC_DIF_JACF, p, x, m, n, lb, ub, itmax, opts, info, work, covar, (void *)&data); + + if(info){ /* correct the number of function calls */ + if(data.ffdif) + info[7]+=info[8]*(m+1); /* each Jacobian evaluation costs m+1 function calls */ + else + info[7]+=info[8]*(2*m); /* each Jacobian evaluation costs 2*m function calls */ + } + + free(data.hx); + + return ret; +} + +/* undefine everything. THIS MUST REMAIN AT THE END OF THE FILE */ +#undef FUNC_STATE +#undef LNSRCH +#undef BOXPROJECT +#undef LEVMAR_BOX_CHECK +#undef LEVMAR_BC_DER +#undef LMBC_DIF_DATA +#undef LMBC_DIF_FUNC +#undef LMBC_DIF_JACF +#undef LEVMAR_BC_DIF +#undef LEVMAR_FDIF_FORW_JAC_APPROX +#undef LEVMAR_FDIF_CENT_JAC_APPROX +#undef LEVMAR_COVAR +#undef LEVMAR_TRANS_MAT_MAT_MULT +#undef LEVMAR_L2NRMXMY +#undef AX_EQ_B_LU +#undef AX_EQ_B_CHOL +#undef AX_EQ_B_QR +#undef AX_EQ_B_QRLS +#undef AX_EQ_B_SVD +#undef AX_EQ_B_BK diff --git a/sci-libs/levmar/levmar-2.5/lmblec.c b/sci-libs/levmar/levmar-2.5/lmblec.c new file mode 100644 index 000000000..f64c8f2ea --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmblec.c @@ -0,0 +1,87 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004-06 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************** + * combined box and linear equation constraints Levenberg-Marquardt nonlinear + * minimization. The same core code is used with appropriate #defines to derive + * single and double precision versions, see also lmblec_core.c + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <float.h> + +#include "levmar.h" +#include "misc.h" + +#ifndef HAVE_LAPACK + +#ifdef _MSC_VER +#pragma message("Combined box and linearly constrained optimization requires LAPACK and was not compiled!") +#else +#warning Combined box and linearly constrained optimization requires LAPACK and was not compiled! +#endif // _MSC_VER + +#else // LAPACK present + +#if !defined(LM_DBL_PREC) && !defined(LM_SNGL_PREC) +#error At least one of LM_DBL_PREC, LM_SNGL_PREC should be defined! +#endif + + +#ifdef LM_SNGL_PREC +/* single precision (float) definitions */ +#define LM_REAL float +#define LM_PREFIX s + +#define LM_REAL_MAX FLT_MAX +#define LM_REAL_MIN -FLT_MAX +#define __SUBCNST(x) x##F +#define LM_CNST(x) __SUBCNST(x) // force substitution + +#include "lmblec_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_MAX +#undef LM_REAL_MIN +#undef __SUBCNST +#undef LM_CNST +#endif /* LM_SNGL_PREC */ + +#ifdef LM_DBL_PREC +/* double precision definitions */ +#define LM_REAL double +#define LM_PREFIX d + +#define LM_REAL_MAX DBL_MAX +#define LM_REAL_MIN -DBL_MAX +#define LM_CNST(x) (x) + +#include "lmblec_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_MAX +#undef LM_REAL_MIN +#undef LM_CNST +#endif /* LM_DBL_PREC */ + +#endif /* HAVE_LAPACK */ diff --git a/sci-libs/levmar/levmar-2.5/lmblec.o b/sci-libs/levmar/levmar-2.5/lmblec.o Binary files differnew file mode 100644 index 000000000..ffc0fc149 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmblec.o diff --git a/sci-libs/levmar/levmar-2.5/lmblec_core.c b/sci-libs/levmar/levmar-2.5/lmblec_core.c new file mode 100644 index 000000000..3d3476c3a --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmblec_core.c @@ -0,0 +1,413 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004-06 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************* + * This file implements combined box and linear equation constraints. + * + * Note that the algorithm implementing linearly constrained minimization does + * so by a change in parameters that transforms the original program into an + * unconstrained one. To employ the same idea for implementing box & linear + * constraints would require the transformation of box constraints on the + * original parameters to box constraints for the new parameter set. This + * being impossible, a different approach is used here for finding the minimum. + * The trick is to remove the box constraints by augmenting the function to + * be fitted with penalty terms and then solve the resulting problem (which + * involves linear constrains only) with the functions in lmlec.c + * + * More specifically, for the constraint a<=x[i]<=b to hold, the term C[i]= + * (2*x[i]-(a+b))/(b-a) should be within [-1, 1]. This is enforced by adding + * the penalty term w[i]*max((C[i])^2-1, 0) to the objective function, where + * w[i] is a large weight. In the case of constraints of the form a<=x[i], + * the term C[i]=a-x[i] has to be non positive, thus the penalty term is + * w[i]*max(C[i], 0). If x[i]<=b, C[i]=x[i]-b has to be non negative and + * the penalty is w[i]*max(C[i], 0). The derivatives needed for the Jacobian + * are as follows: + * For the constraint a<=x[i]<=b: 4*(2*x[i]-(a+b))/(b-a)^2 if x[i] not in [a, b], + * 0 otherwise + * For the constraint a<=x[i]: -1 if x[i]<=a, 0 otherwise + * For the constraint x[i]<=b: 1 if b<=x[i], 0 otherwise + * + * Note that for the above to work, the weights w[i] should be large enough; + * depending on your minimization problem, the default values might need some + * tweaking (see arg "wghts" below). + *******************************************************************************/ + +#ifndef LM_REAL // not included by lmblec.c +#error This file should not be compiled directly! +#endif + + +#define __MAX__(x, y) (((x)>=(y))? (x) : (y)) +#define __BC_WEIGHT__ LM_CNST(1E+04) + +#define __BC_INTERVAL__ 0 +#define __BC_LOW__ 1 +#define __BC_HIGH__ 2 + +/* precision-specific definitions */ +#define LEVMAR_BOX_CHECK LM_ADD_PREFIX(levmar_box_check) +#define LMBLEC_DATA LM_ADD_PREFIX(lmblec_data) +#define LMBLEC_FUNC LM_ADD_PREFIX(lmblec_func) +#define LMBLEC_JACF LM_ADD_PREFIX(lmblec_jacf) +#define LEVMAR_LEC_DER LM_ADD_PREFIX(levmar_lec_der) +#define LEVMAR_LEC_DIF LM_ADD_PREFIX(levmar_lec_dif) +#define LEVMAR_BLEC_DER LM_ADD_PREFIX(levmar_blec_der) +#define LEVMAR_BLEC_DIF LM_ADD_PREFIX(levmar_blec_dif) +#define LEVMAR_COVAR LM_ADD_PREFIX(levmar_covar) + +struct LMBLEC_DATA{ + LM_REAL *x, *lb, *ub, *w; + int *bctype; + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata); + void (*jacf)(LM_REAL *p, LM_REAL *jac, int m, int n, void *adata); + void *adata; +}; + +/* augmented measurements */ +static void LMBLEC_FUNC(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata) +{ +struct LMBLEC_DATA *data=(struct LMBLEC_DATA *)adata; +int nn; +register int i, j, *typ; +register LM_REAL *lb, *ub, *w, tmp; + + nn=n-m; + lb=data->lb; + ub=data->ub; + w=data->w; + typ=data->bctype; + (*(data->func))(p, hx, m, nn, data->adata); + + for(i=nn, j=0; i<n; ++i, ++j){ + switch(typ[j]){ + case __BC_INTERVAL__: + tmp=(LM_CNST(2.0)*p[j]-(lb[j]+ub[j]))/(ub[j]-lb[j]); + hx[i]=w[j]*__MAX__(tmp*tmp-LM_CNST(1.0), LM_CNST(0.0)); + break; + + case __BC_LOW__: + hx[i]=w[j]*__MAX__(lb[j]-p[j], LM_CNST(0.0)); + break; + + case __BC_HIGH__: + hx[i]=w[j]*__MAX__(p[j]-ub[j], LM_CNST(0.0)); + break; + } + } +} + +/* augmented Jacobian */ +static void LMBLEC_JACF(LM_REAL *p, LM_REAL *jac, int m, int n, void *adata) +{ +struct LMBLEC_DATA *data=(struct LMBLEC_DATA *)adata; +int nn, *typ; +register int i, j; +register LM_REAL *lb, *ub, *w, tmp; + + nn=n-m; + lb=data->lb; + ub=data->ub; + w=data->w; + typ=data->bctype; + (*(data->jacf))(p, jac, m, nn, data->adata); + + /* clear all extra rows */ + for(i=nn*m; i<n*m; ++i) + jac[i]=0.0; + + for(i=nn, j=0; i<n; ++i, ++j){ + switch(typ[j]){ + case __BC_INTERVAL__: + if(lb[j]<=p[j] && p[j]<=ub[j]) + continue; // corresp. jac element already 0 + + /* out of interval */ + tmp=ub[j]-lb[j]; + tmp=LM_CNST(4.0)*(LM_CNST(2.0)*p[j]-(lb[j]+ub[j]))/(tmp*tmp); + jac[i*m+j]=w[j]*tmp; + break; + + case __BC_LOW__: // (lb[j]<=p[j])? 0.0 : -1.0; + if(lb[j]<=p[j]) + continue; // corresp. jac element already 0 + + /* smaller than lower bound */ + jac[i*m+j]=-w[j]; + break; + + case __BC_HIGH__: // (p[j]<=ub[j])? 0.0 : 1.0; + if(p[j]<=ub[j]) + continue; // corresp. jac element already 0 + + /* greater than upper bound */ + jac[i*m+j]=w[j]; + break; + } + } +} + +/* + * This function seeks the parameter vector p that best describes the measurements + * vector x under box & linear constraints. + * More precisely, given a vector function func : R^m --> R^n with n>=m, + * it finds p s.t. func(p) ~= x, i.e. the squared second order (i.e. L2) norm of + * e=x-func(p) is minimized under the constraints lb[i]<=p[i]<=ub[i] and A p=b; + * A is kxm, b kx1. Note that this function DOES NOT check the satisfiability of + * the specified box and linear equation constraints. + * If no lower bound constraint applies for p[i], use -DBL_MAX/-FLT_MAX for lb[i]; + * If no upper bound constraint applies for p[i], use DBL_MAX/FLT_MAX for ub[i]. + * + * This function requires an analytic Jacobian. In case the latter is unavailable, + * use LEVMAR_BLEC_DIF() bellow + * + * Returns the number of iterations (>=0) if successful, LM_ERROR if failed + * + * For more details on the algorithm implemented by this function, please refer to + * the comments in the top of this file. + * + */ +int LEVMAR_BLEC_DER( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), /* function to evaluate the Jacobian \part x / \part p */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + LM_REAL *lb, /* I: vector of lower bounds. If NULL, no lower bounds apply */ + LM_REAL *ub, /* I: vector of upper bounds. If NULL, no upper bounds apply */ + LM_REAL *A, /* I: constraints matrix, kxm */ + LM_REAL *b, /* I: right hand constraints vector, kx1 */ + int k, /* I: number of constraints (i.e. A's #rows) */ + LM_REAL *wghts, /* mx1 weights for penalty terms, defaults used if NULL */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[4], /* I: minim. options [\mu, \epsilon1, \epsilon2, \epsilon3]. Respectively the scale factor for initial \mu, + * stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2. Set to NULL for defaults to be used + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_BLEC_DER_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func & jacf. + * Set to NULL if not needed + */ +{ + struct LMBLEC_DATA data; + int ret; + LM_REAL locinfo[LM_INFO_SZ]; + register int i; + + if(!jacf){ + fprintf(stderr, RCAT("No function specified for computing the Jacobian in ", LEVMAR_BLEC_DER) + RCAT("().\nIf no such function is available, use ", LEVMAR_BLEC_DIF) RCAT("() rather than ", LEVMAR_BLEC_DER) "()\n"); + return LM_ERROR; + } + + if(!lb && !ub){ + fprintf(stderr, RCAT(LCAT(LEVMAR_BLEC_DER, "(): lower and upper bounds for box constraints cannot be both NULL, use "), + LEVMAR_LEC_DER) "() in this case!\n"); + return LM_ERROR; + } + + if(!LEVMAR_BOX_CHECK(lb, ub, m)){ + fprintf(stderr, LCAT(LEVMAR_BLEC_DER, "(): at least one lower bound exceeds the upper one\n")); + return LM_ERROR; + } + + /* measurement vector needs to be extended by m */ + if(x){ /* nonzero x */ + data.x=(LM_REAL *)malloc((n+m)*sizeof(LM_REAL)); + if(!data.x){ + fprintf(stderr, LCAT(LEVMAR_BLEC_DER, "(): memory allocation request #1 failed\n")); + return LM_ERROR; + } + + for(i=0; i<n; ++i) + data.x[i]=x[i]; + for(i=n; i<n+m; ++i) + data.x[i]=0.0; + } + else + data.x=NULL; + + data.w=(LM_REAL *)malloc(m*sizeof(LM_REAL) + m*sizeof(int)); /* should be arranged in that order for proper doubles alignment */ + if(!data.w){ + fprintf(stderr, LCAT(LEVMAR_BLEC_DER, "(): memory allocation request #2 failed\n")); + if(data.x) free(data.x); + return LM_ERROR; + } + data.bctype=(int *)(data.w+m); + + /* note: at this point, one of lb, ub are not NULL */ + for(i=0; i<m; ++i){ + data.w[i]=(!wghts)? __BC_WEIGHT__ : wghts[i]; + if(!lb) data.bctype[i]=__BC_HIGH__; + else if(!ub) data.bctype[i]=__BC_LOW__; + else if(ub[i]!=LM_REAL_MAX && lb[i]!=LM_REAL_MIN) data.bctype[i]=__BC_INTERVAL__; + else if(lb[i]!=LM_REAL_MIN) data.bctype[i]=__BC_LOW__; + else data.bctype[i]=__BC_HIGH__; + } + + data.lb=lb; + data.ub=ub; + data.func=func; + data.jacf=jacf; + data.adata=adata; + + if(!info) info=locinfo; /* make sure that LEVMAR_LEC_DER() is called with non-null info */ + ret=LEVMAR_LEC_DER(LMBLEC_FUNC, LMBLEC_JACF, p, data.x, m, n+m, A, b, k, itmax, opts, info, work, covar, (void *)&data); + + if(data.x) free(data.x); + free(data.w); + + return ret; +} + +/* Similar to the LEVMAR_BLEC_DER() function above, except that the Jacobian is approximated + * with the aid of finite differences (forward or central, see the comment for the opts argument) + */ +int LEVMAR_BLEC_DIF( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + LM_REAL *lb, /* I: vector of lower bounds. If NULL, no lower bounds apply */ + LM_REAL *ub, /* I: vector of upper bounds. If NULL, no upper bounds apply */ + LM_REAL *A, /* I: constraints matrix, kxm */ + LM_REAL *b, /* I: right hand constraints vector, kx1 */ + int k, /* I: number of constraints (i.e. A's #rows) */ + LM_REAL *wghts, /* mx1 weights for penalty terms, defaults used if NULL */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[5], /* I: opts[0-3] = minim. options [\mu, \epsilon1, \epsilon2, \epsilon3, \delta]. Respectively the + * scale factor for initial \mu, stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2 and + * the step used in difference approximation to the Jacobian. Set to NULL for defaults to be used. + * If \delta<0, the Jacobian is approximated with central differences which are more accurate + * (but slower!) compared to the forward differences employed by default. + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_BLEC_DIF_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func. + * Set to NULL if not needed + */ +{ + struct LMBLEC_DATA data; + int ret; + register int i; + LM_REAL locinfo[LM_INFO_SZ]; + + if(!lb && !ub){ + fprintf(stderr, RCAT(LCAT(LEVMAR_BLEC_DIF, "(): lower and upper bounds for box constraints cannot be both NULL, use "), + LEVMAR_LEC_DIF) "() in this case!\n"); + return LM_ERROR; + } + + if(!LEVMAR_BOX_CHECK(lb, ub, m)){ + fprintf(stderr, LCAT(LEVMAR_BLEC_DER, "(): at least one lower bound exceeds the upper one\n")); + return LM_ERROR; + } + + /* measurement vector needs to be extended by m */ + if(x){ /* nonzero x */ + data.x=(LM_REAL *)malloc((n+m)*sizeof(LM_REAL)); + if(!data.x){ + fprintf(stderr, LCAT(LEVMAR_BLEC_DER, "(): memory allocation request #1 failed\n")); + return LM_ERROR; + } + + for(i=0; i<n; ++i) + data.x[i]=x[i]; + for(i=n; i<n+m; ++i) + data.x[i]=0.0; + } + else + data.x=NULL; + + data.w=(LM_REAL *)malloc(m*sizeof(LM_REAL) + m*sizeof(int)); /* should be arranged in that order for proper doubles alignment */ + if(!data.w){ + fprintf(stderr, LCAT(LEVMAR_BLEC_DER, "(): memory allocation request #2 failed\n")); + if(data.x) free(data.x); + return LM_ERROR; + } + data.bctype=(int *)(data.w+m); + + /* note: at this point, one of lb, ub are not NULL */ + for(i=0; i<m; ++i){ + data.w[i]=(!wghts)? __BC_WEIGHT__ : wghts[i]; + if(!lb) data.bctype[i]=__BC_HIGH__; + else if(!ub) data.bctype[i]=__BC_LOW__; + else if(ub[i]!=LM_REAL_MAX && lb[i]!=LM_REAL_MIN) data.bctype[i]=__BC_INTERVAL__; + else if(lb[i]!=LM_REAL_MIN) data.bctype[i]=__BC_LOW__; + else data.bctype[i]=__BC_HIGH__; + } + + data.lb=lb; + data.ub=ub; + data.func=func; + data.jacf=NULL; + data.adata=adata; + + if(!info) info=locinfo; /* make sure that LEVMAR_LEC_DIF() is called with non-null info */ + ret=LEVMAR_LEC_DIF(LMBLEC_FUNC, p, data.x, m, n+m, A, b, k, itmax, opts, info, work, covar, (void *)&data); + + if(data.x) free(data.x); + free(data.w); + + return ret; +} + +/* undefine all. THIS MUST REMAIN AT THE END OF THE FILE */ +#undef LEVMAR_BOX_CHECK +#undef LMBLEC_DATA +#undef LMBLEC_FUNC +#undef LMBLEC_JACF +#undef LEVMAR_COVAR +#undef LEVMAR_LEC_DER +#undef LEVMAR_LEC_DIF +#undef LEVMAR_BLEC_DER +#undef LEVMAR_BLEC_DIF diff --git a/sci-libs/levmar/levmar-2.5/lmbleic.c b/sci-libs/levmar/levmar-2.5/lmbleic.c new file mode 100644 index 000000000..ca1d825eb --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmbleic.c @@ -0,0 +1,89 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2009 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************* + * Wrappers for linear inequality constrained Levenberg-Marquardt minimization. + * The same core code is used with appropriate #defines to derive single and + * double precision versions, see also lmbleic_core.c + *******************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <float.h> + +#include "levmar.h" +#include "misc.h" + + +#ifndef HAVE_LAPACK + +#ifdef _MSC_VER +#pragma message("Linear inequalities constrained optimization requires LAPACK and was not compiled!") +#else +#warning Linear inequalities constrained optimization requires LAPACK and was not compiled! +#endif // _MSC_VER + +#else // LAPACK present + +#if !defined(LM_DBL_PREC) && !defined(LM_SNGL_PREC) +#error At least one of LM_DBL_PREC, LM_SNGL_PREC should be defined! +#endif + + +#ifdef LM_SNGL_PREC +/* single precision (float) definitions */ +#define LM_REAL float +#define LM_PREFIX s + +#define LM_REAL_MAX FLT_MAX +#define LM_REAL_MIN -FLT_MAX +#define __SUBCNST(x) x##F +#define LM_CNST(x) __SUBCNST(x) // force substitution + +#include "lmbleic_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_MAX +#undef LM_REAL_MIN +#undef __SUBCNST +#undef LM_CNST +#endif /* LM_SNGL_PREC */ + +#ifdef LM_DBL_PREC +/* double precision definitions */ +#define LM_REAL double +#define LM_PREFIX d + +#define LM_REAL_MAX DBL_MAX +#define LM_REAL_MIN -DBL_MAX +#define LM_CNST(x) (x) + +#include "lmbleic_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_MAX +#undef LM_REAL_MIN +#undef LM_CNST +#endif /* LM_DBL_PREC */ + +#endif /* HAVE_LAPACK */ + diff --git a/sci-libs/levmar/levmar-2.5/lmbleic.o b/sci-libs/levmar/levmar-2.5/lmbleic.o Binary files differnew file mode 100644 index 000000000..37e47be50 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmbleic.o diff --git a/sci-libs/levmar/levmar-2.5/lmbleic_core.c b/sci-libs/levmar/levmar-2.5/lmbleic_core.c new file mode 100644 index 000000000..7ff1d34d0 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmbleic_core.c @@ -0,0 +1,506 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2009 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#ifndef LM_REAL // not included by lmbleic.c +#error This file should not be compiled directly! +#endif + + +/* precision-specific definitions */ +#define LMBLEIC_DATA LM_ADD_PREFIX(lmbleic_data) +#define LMBLEIC_ELIM LM_ADD_PREFIX(lmbleic_elim) +#define LMBLEIC_FUNC LM_ADD_PREFIX(lmbleic_func) +#define LMBLEIC_JACF LM_ADD_PREFIX(lmbleic_jacf) +#define LEVMAR_BLEIC_DER LM_ADD_PREFIX(levmar_bleic_der) +#define LEVMAR_BLEIC_DIF LM_ADD_PREFIX(levmar_bleic_dif) +#define LEVMAR_BLIC_DER LM_ADD_PREFIX(levmar_blic_der) +#define LEVMAR_BLIC_DIF LM_ADD_PREFIX(levmar_blic_dif) +#define LEVMAR_LEIC_DER LM_ADD_PREFIX(levmar_leic_der) +#define LEVMAR_LEIC_DIF LM_ADD_PREFIX(levmar_leic_dif) +#define LEVMAR_LIC_DER LM_ADD_PREFIX(levmar_lic_der) +#define LEVMAR_LIC_DIF LM_ADD_PREFIX(levmar_lic_dif) +#define LEVMAR_BLEC_DER LM_ADD_PREFIX(levmar_blec_der) +#define LEVMAR_BLEC_DIF LM_ADD_PREFIX(levmar_blec_dif) +#define LEVMAR_TRANS_MAT_MAT_MULT LM_ADD_PREFIX(levmar_trans_mat_mat_mult) +#define LEVMAR_COVAR LM_ADD_PREFIX(levmar_covar) +#define LEVMAR_FDIF_FORW_JAC_APPROX LM_ADD_PREFIX(levmar_fdif_forw_jac_approx) + +struct LMBLEIC_DATA{ + LM_REAL *jac; + int nineqcnstr; // #inequality constraints + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata); + void (*jacf)(LM_REAL *p, LM_REAL *jac, int m, int n, void *adata); + void *adata; +}; + + +/* wrapper ensuring that the user-supplied function is called with the right number of variables (i.e. m) */ +static void LMBLEIC_FUNC(LM_REAL *pext, LM_REAL *hx, int mm, int n, void *adata) +{ +struct LMBLEIC_DATA *data=(struct LMBLEIC_DATA *)adata; +int m; + + m=mm-data->nineqcnstr; + (*(data->func))(pext, hx, m, n, data->adata); +} + + +/* wrapper for computing the Jacobian at pext. The Jacobian is nxmm */ +static void LMBLEIC_JACF(LM_REAL *pext, LM_REAL *jacext, int mm, int n, void *adata) +{ +struct LMBLEIC_DATA *data=(struct LMBLEIC_DATA *)adata; +int m; +register int i, j; +LM_REAL *jac, *jacim, *jacextimm; + + m=mm-data->nineqcnstr; + jac=data->jac; + + (*(data->jacf))(pext, jac, m, n, data->adata); + + for(i=0; i<n; ++i){ + jacextimm=jacext+i*mm; + jacim=jac+i*m; + for(j=0; j<m; ++j) + jacextimm[j]=jacim[j]; //jacext[i*mm+j]=jac[i*m+j]; + + for(j=m; j<mm; ++j) + jacextimm[j]=0.0; //jacext[i*mm+j]=0.0; + } +} + + +/* + * This function is similar to LEVMAR_DER except that the minimization is + * performed subject to the box constraints lb[i]<=p[i]<=ub[i], the linear + * equation constraints A*p=b, A being k1xm, b k1x1, and the linear inequality + * constraints C*p>=d, C being k2xm, d k2x1. + * + * The inequalities are converted to equations by introducing surplus variables, + * i.e. c^T*p >= d becomes c^T*p - y = d, with y>=0. To transform all inequalities + * to equations, a total of k2 surplus variables are introduced; a problem with only + * box and linear constraints results then and is solved with LEVMAR_BLEC_DER() + * Note that opposite direction inequalities should be converted to the desired + * direction by negating, i.e. c^T*p <= d becomes -c^T*p >= -d + * + * This function requires an analytic Jacobian. In case the latter is unavailable, + * use LEVMAR_BLEIC_DIF() bellow + * + */ +int LEVMAR_BLEIC_DER( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), /* function to evaluate the Jacobian \part x / \part p */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + LM_REAL *lb, /* I: vector of lower bounds. If NULL, no lower bounds apply */ + LM_REAL *ub, /* I: vector of upper bounds. If NULL, no upper bounds apply */ + LM_REAL *A, /* I: equality constraints matrix, k1xm. If NULL, no linear equation constraints apply */ + LM_REAL *b, /* I: right hand constraints vector, k1x1 */ + int k1, /* I: number of constraints (i.e. A's #rows) */ + LM_REAL *C, /* I: inequality constraints matrix, k2xm */ + LM_REAL *d, /* I: right hand constraints vector, k2x1 */ + int k2, /* I: number of inequality constraints (i.e. C's #rows) */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[4], /* I: minim. options [\mu, \epsilon1, \epsilon2, \epsilon3]. Respectively the scale factor for initial \mu, + * stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2. Set to NULL for defaults to be used + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_BLEIC_DER_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func & jacf. + * Set to NULL if not needed + */ +{ + struct LMBLEIC_DATA data; + LM_REAL *ptr, *pext, *Aext, *bext, *covext; /* corresponding to p, A, b, covar for the full set of variables; + pext=[p, surplus], pext is mm, Aext is (k1+k2)xmm, bext (k1+k2), covext is mmxmm + */ + LM_REAL *lbext, *ubext; // corresponding to lb, ub for the full set of variables + int mm, ret, k12; + register int i, j, ii; + register LM_REAL tmp; + LM_REAL locinfo[LM_INFO_SZ]; + + if(!jacf){ + fprintf(stderr, RCAT("No function specified for computing the Jacobian in ", LEVMAR_BLEIC_DER) + RCAT("().\nIf no such function is available, use ", LEVMAR_BLEIC_DIF) RCAT("() rather than ", LEVMAR_BLEIC_DER) "()\n"); + return LM_ERROR; + } + + if(!C || !d){ + fprintf(stderr, RCAT(LCAT(LEVMAR_BLEIC_DER, "(): missing inequality constraints, use "), LEVMAR_BLEC_DER) "() in this case!\n"); + return LM_ERROR; + } + + if(!A || !b) k1=0; // sanity check + + mm=m+k2; + + if(n<m-k1){ + fprintf(stderr, LCAT(LEVMAR_BLEIC_DER, "(): cannot solve a problem with fewer measurements + equality constraints [%d + %d] than unknowns [%d]\n"), n, k1, m); + return LM_ERROR; + } + + k12=k1+k2; + ptr=(LM_REAL *)malloc((3*mm + k12*mm + k12 + n*m + (covar? mm*mm : 0))*sizeof(LM_REAL)); + if(!ptr){ + fprintf(stderr, LCAT(LEVMAR_BLEIC_DER, "(): memory allocation request failed\n")); + return LM_ERROR; + } + pext=ptr; + lbext=pext+mm; + ubext=lbext+mm; + Aext=ubext+mm; + bext=Aext+k12*mm; + data.jac=bext+k12; + covext=covar? data.jac+n*m : NULL; + data.nineqcnstr=k2; + data.func=func; + data.jacf=jacf; + data.adata=adata; + + /* compute y s.t. C*p - y=d, i.e. y=C*p-d. + * y is stored in the last k2 elements of pext + */ + for(i=0; i<k2; ++i){ + for(j=0, tmp=0.0; j<m; ++j) + tmp+=C[i*m+j]*p[j]; + pext[j=i+m]=tmp-d[i]; + + /* surplus variables must be >=0 */ + lbext[j]=0.0; + ubext[j]=LM_REAL_MAX; + } + /* set the first m elements of pext equal to p */ + for(i=0; i<m; ++i){ + pext[i]=p[i]; + lbext[i]=lb? lb[i] : LM_REAL_MIN; + ubext[i]=ub? ub[i] : LM_REAL_MAX; + } + + /* setup the constraints matrix */ + /* original linear equation constraints */ + for(i=0; i<k1; ++i){ + for(j=0; j<m; ++j) + Aext[i*mm+j]=A[i*m+j]; + + for(j=m; j<mm; ++j) + Aext[i*mm+j]=0.0; + + bext[i]=b[i]; + } + /* linear equation constraints resulting from surplus variables */ + for(i=0, ii=k1; i<k2; ++i, ++ii){ + for(j=0; j<m; ++j) + Aext[ii*mm+j]=C[i*m+j]; + + for(j=m; j<mm; ++j) + Aext[ii*mm+j]=0.0; + + Aext[ii*mm+m+i]=-1.0; + + bext[ii]=d[i]; + } + + if(!info) info=locinfo; /* make sure that LEVMAR_BLEC_DER() is called with non-null info */ + /* note that the default weights for the penalty terms are being used below */ + ret=LEVMAR_BLEC_DER(LMBLEIC_FUNC, LMBLEIC_JACF, pext, x, mm, n, lbext, ubext, Aext, bext, k12, NULL, itmax, opts, info, work, covext, (void *)&data); + + /* copy back the minimizer */ + for(i=0; i<m; ++i) + p[i]=pext[i]; + +#if 0 +printf("Surplus variables for the minimizer:\n"); +for(i=m; i<mm; ++i) + printf("%g ", pext[i]); +printf("\n\n"); +#endif + + if(covar){ + for(i=0; i<m; ++i){ + for(j=0; j<m; ++j) + covar[i*m+j]=covext[i*mm+j]; + } + } + + free(ptr); + + return ret; +} + +/* Similar to the LEVMAR_BLEIC_DER() function above, except that the Jacobian is approximated + * with the aid of finite differences (forward or central, see the comment for the opts argument) + */ +int LEVMAR_BLEIC_DIF( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + LM_REAL *lb, /* I: vector of lower bounds. If NULL, no lower bounds apply */ + LM_REAL *ub, /* I: vector of upper bounds. If NULL, no upper bounds apply */ + LM_REAL *A, /* I: equality constraints matrix, k1xm. If NULL, no linear equation constraints apply */ + LM_REAL *b, /* I: right hand constraints vector, k1x1 */ + int k1, /* I: number of constraints (i.e. A's #rows) */ + LM_REAL *C, /* I: inequality constraints matrix, k2xm */ + LM_REAL *d, /* I: right hand constraints vector, k2x1 */ + int k2, /* I: number of inequality constraints (i.e. C's #rows) */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[5], /* I: opts[0-3] = minim. options [\mu, \epsilon1, \epsilon2, \epsilon3, \delta]. Respectively the + * scale factor for initial \mu, stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2 and + * the step used in difference approximation to the Jacobian. Set to NULL for defaults to be used. + * If \delta<0, the Jacobian is approximated with central differences which are more accurate + * (but slower!) compared to the forward differences employed by default. + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_BLEIC_DIF_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func. + * Set to NULL if not needed + */ +{ + struct LMBLEIC_DATA data; + LM_REAL *ptr, *pext, *Aext, *bext, *covext; /* corresponding to p, A, b, covar for the full set of variables; + pext=[p, surplus], pext is mm, Aext is (k1+k2)xmm, bext (k1+k2), covext is mmxmm + */ + LM_REAL *lbext, *ubext; // corresponding to lb, ub for the full set of variables + int mm, ret, k12; + register int i, j, ii; + register LM_REAL tmp; + LM_REAL locinfo[LM_INFO_SZ]; + + if(!C || !d){ + fprintf(stderr, RCAT(LCAT(LEVMAR_BLEIC_DIF, "(): missing inequality constraints, use "), LEVMAR_BLEC_DIF) "() in this case!\n"); + return LM_ERROR; + } + if(!A || !b) k1=0; // sanity check + + mm=m+k2; + + if(n<m-k1){ + fprintf(stderr, LCAT(LEVMAR_BLEIC_DIF, "(): cannot solve a problem with fewer measurements + equality constraints [%d + %d] than unknowns [%d]\n"), n, k1, m); + return LM_ERROR; + } + + k12=k1+k2; + ptr=(LM_REAL *)malloc((3*mm + k12*mm + k12 + (covar? mm*mm : 0))*sizeof(LM_REAL)); + if(!ptr){ + fprintf(stderr, LCAT(LEVMAR_BLEIC_DIF, "(): memory allocation request failed\n")); + return LM_ERROR; + } + pext=ptr; + lbext=pext+mm; + ubext=lbext+mm; + Aext=ubext+mm; + bext=Aext+k12*mm; + data.jac=NULL; + covext=covar? bext+k12 : NULL; + data.nineqcnstr=k2; + data.func=func; + data.jacf=NULL; + data.adata=adata; + + /* compute y s.t. C*p - y=d, i.e. y=C*p-d. + * y is stored in the last k2 elements of pext + */ + for(i=0; i<k2; ++i){ + for(j=0, tmp=0.0; j<m; ++j) + tmp+=C[i*m+j]*p[j]; + pext[j=i+m]=tmp-d[i]; + + /* surplus variables must be >=0 */ + lbext[j]=0.0; + ubext[j]=LM_REAL_MAX; + } + /* set the first m elements of pext equal to p */ + for(i=0; i<m; ++i){ + pext[i]=p[i]; + lbext[i]=lb? lb[i] : LM_REAL_MIN; + ubext[i]=ub? ub[i] : LM_REAL_MAX; + } + + /* setup the constraints matrix */ + /* original linear equation constraints */ + for(i=0; i<k1; ++i){ + for(j=0; j<m; ++j) + Aext[i*mm+j]=A[i*m+j]; + + for(j=m; j<mm; ++j) + Aext[i*mm+j]=0.0; + + bext[i]=b[i]; + } + /* linear equation constraints resulting from surplus variables */ + for(i=0, ii=k1; i<k2; ++i, ++ii){ + for(j=0; j<m; ++j) + Aext[ii*mm+j]=C[i*m+j]; + + for(j=m; j<mm; ++j) + Aext[ii*mm+j]=0.0; + + Aext[ii*mm+m+i]=-1.0; + + bext[ii]=d[i]; + } + + if(!info) info=locinfo; /* make sure that LEVMAR_BLEC_DIF() is called with non-null info */ + /* note that the default weights for the penalty terms are being used below */ + ret=LEVMAR_BLEC_DIF(LMBLEIC_FUNC, pext, x, mm, n, lbext, ubext, Aext, bext, k12, NULL, itmax, opts, info, work, covext, (void *)&data); + + /* copy back the minimizer */ + for(i=0; i<m; ++i) + p[i]=pext[i]; + +#if 0 +printf("Surplus variables for the minimizer:\n"); +for(i=m; i<mm; ++i) + printf("%g ", pext[i]); +printf("\n\n"); +#endif + + if(covar){ + for(i=0; i<m; ++i){ + for(j=0; j<m; ++j) + covar[i*m+j]=covext[i*mm+j]; + } + } + + free(ptr); + + return ret; +} + + +/* convenience wrappers to LEVMAR_BLEIC_DER/LEVMAR_BLEIC_DIF */ + +/* box & linear inequality constraints */ +int LEVMAR_BLIC_DER( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), + LM_REAL *p, LM_REAL *x, int m, int n, + LM_REAL *lb, LM_REAL *ub, + LM_REAL *C, LM_REAL *d, int k2, + int itmax, LM_REAL opts[4], LM_REAL info[LM_INFO_SZ], LM_REAL *work, LM_REAL *covar, void *adata) +{ + return LEVMAR_BLEIC_DER(func, jacf, p, x, m, n, lb, ub, NULL, NULL, 0, C, d, k2, itmax, opts, info, work, covar, adata); +} + +int LEVMAR_BLIC_DIF( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + LM_REAL *p, LM_REAL *x, int m, int n, + LM_REAL *lb, LM_REAL *ub, + LM_REAL *C, LM_REAL *d, int k2, + int itmax, LM_REAL opts[5], LM_REAL info[LM_INFO_SZ], LM_REAL *work, LM_REAL *covar, void *adata) +{ + return LEVMAR_BLEIC_DIF(func, p, x, m, n, lb, ub, NULL, NULL, 0, C, d, k2, itmax, opts, info, work, covar, adata); +} + +/* linear equation & inequality constraints */ +int LEVMAR_LEIC_DER( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), + LM_REAL *p, LM_REAL *x, int m, int n, + LM_REAL *A, LM_REAL *b, int k1, + LM_REAL *C, LM_REAL *d, int k2, + int itmax, LM_REAL opts[4], LM_REAL info[LM_INFO_SZ], LM_REAL *work, LM_REAL *covar, void *adata) +{ + return LEVMAR_BLEIC_DER(func, jacf, p, x, m, n, NULL, NULL, A, b, k1, C, d, k2, itmax, opts, info, work, covar, adata); +} + +int LEVMAR_LEIC_DIF( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + LM_REAL *p, LM_REAL *x, int m, int n, + LM_REAL *A, LM_REAL *b, int k1, + LM_REAL *C, LM_REAL *d, int k2, + int itmax, LM_REAL opts[5], LM_REAL info[LM_INFO_SZ], LM_REAL *work, LM_REAL *covar, void *adata) +{ + return LEVMAR_BLEIC_DIF(func, p, x, m, n, NULL, NULL, A, b, k1, C, d, k2, itmax, opts, info, work, covar, adata); +} + +/* linear inequality constraints */ +int LEVMAR_LIC_DER( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), + LM_REAL *p, LM_REAL *x, int m, int n, + LM_REAL *C, LM_REAL *d, int k2, + int itmax, LM_REAL opts[4], LM_REAL info[LM_INFO_SZ], LM_REAL *work, LM_REAL *covar, void *adata) +{ + return LEVMAR_BLEIC_DER(func, jacf, p, x, m, n, NULL, NULL, NULL, NULL, 0, C, d, k2, itmax, opts, info, work, covar, adata); +} + +int LEVMAR_LIC_DIF( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + LM_REAL *p, LM_REAL *x, int m, int n, + LM_REAL *C, LM_REAL *d, int k2, + int itmax, LM_REAL opts[5], LM_REAL info[LM_INFO_SZ], LM_REAL *work, LM_REAL *covar, void *adata) +{ + return LEVMAR_BLEIC_DIF(func, p, x, m, n, NULL, NULL, NULL, NULL, 0, C, d, k2, itmax, opts, info, work, covar, adata); +} + +/* undefine all. THIS MUST REMAIN AT THE END OF THE FILE */ +#undef LMBLEIC_DATA +#undef LMBLEIC_ELIM +#undef LMBLEIC_FUNC +#undef LMBLEIC_JACF +#undef LEVMAR_FDIF_FORW_JAC_APPROX +#undef LEVMAR_COVAR +#undef LEVMAR_TRANS_MAT_MAT_MULT +#undef LEVMAR_BLEIC_DER +#undef LEVMAR_BLEIC_DIF +#undef LEVMAR_BLIC_DER +#undef LEVMAR_BLIC_DIF +#undef LEVMAR_LEIC_DER +#undef LEVMAR_LEIC_DIF +#undef LEVMAR_LIC_DER +#undef LEVMAR_LIC_DIF +#undef LEVMAR_BLEC_DER +#undef LEVMAR_BLEC_DIF diff --git a/sci-libs/levmar/levmar-2.5/lmdemo.c b/sci-libs/levmar/levmar-2.5/lmdemo.c new file mode 100644 index 000000000..5ba6a9f43 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmdemo.c @@ -0,0 +1,1230 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Demonstration driver program for the Levenberg - Marquardt minimization +// algorithm +// Copyright (C) 2004-05 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************** + * Levenberg-Marquardt minimization demo driver. Only the double precision versions + * are tested here. See the Meyer case for an example of verifying the Jacobian + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <float.h> + +#include "levmar.h" + +#ifndef LM_DBL_PREC +#error Demo program assumes that levmar has been compiled with double precision, see LM_DBL_PREC! +#endif + + +/* Sample functions to be minimized with LM and their Jacobians. + * More test functions at http://www.csit.fsu.edu/~burkardt/f_src/test_nls/test_nls.html + * Check also the CUTE problems collection at ftp://ftp.numerical.rl.ac.uk/pub/cute/; + * CUTE is searchable through http://numawww.mathematik.tu-darmstadt.de:8081/opti/select.html + * CUTE problems can also be solved through the AMPL web interface at http://www.ampl.com/TRYAMPL/startup.html + * + * Nonlinear optimization models in AMPL can be found at http://www.princeton.edu/~rvdb/ampl/nlmodels/ + */ + +#define ROSD 105.0 + +/* Rosenbrock function, global minimum at (1, 1) */ +void ros(double *p, double *x, int m, int n, void *data) +{ +register int i; + + for(i=0; i<n; ++i) + x[i]=((1.0-p[0])*(1.0-p[0]) + ROSD*(p[1]-p[0]*p[0])*(p[1]-p[0]*p[0])); +} + +void jacros(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; + + for(i=j=0; i<n; ++i){ + jac[j++]=(-2 + 2*p[0]-4*ROSD*(p[1]-p[0]*p[0])*p[0]); + jac[j++]=(2*ROSD*(p[1]-p[0]*p[0])); + } +} + + +#define MODROSLAM 1E02 +/* Modified Rosenbrock problem, global minimum at (1, 1) */ +void modros(double *p, double *x, int m, int n, void *data) +{ +register int i; + + for(i=0; i<n; i+=3){ + x[i]=10*(p[1]-p[0]*p[0]); + x[i+1]=1.0-p[0]; + x[i+2]=MODROSLAM; + } +} + +void jacmodros(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; + + for(i=j=0; i<n; i+=3){ + jac[j++]=-20.0*p[0]; + jac[j++]=10.0; + + jac[j++]=-1.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + } +} + + +/* Powell's function, minimum at (0, 0) */ +void powell(double *p, double *x, int m, int n, void *data) +{ +register int i; + + for(i=0; i<n; i+=2){ + x[i]=p[0]; + x[i+1]=10.0*p[0]/(p[0]+0.1) + 2*p[1]*p[1]; + } +} + +void jacpowell(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; + + for(i=j=0; i<n; i+=2){ + jac[j++]=1.0; + jac[j++]=0.0; + + jac[j++]=1.0/((p[0]+0.1)*(p[0]+0.1)); + jac[j++]=4.0*p[1]; + } +} + +/* Wood's function, minimum at (1, 1, 1, 1) */ +void wood(double *p, double *x, int m, int n, void *data) +{ +register int i; + + for(i=0; i<n; i+=6){ + x[i]=10.0*(p[1] - p[0]*p[0]); + x[i+1]=1.0 - p[0]; + x[i+2]=sqrt(90.0)*(p[3] - p[2]*p[2]); + x[i+3]=1.0 - p[2]; + x[i+4]=sqrt(10.0)*(p[1]+p[3] - 2.0); + x[i+5]=(p[1] - p[3])/sqrt(10.0); + } +} + +/* Meyer's (reformulated) problem, minimum at (2.48, 6.18, 3.45) */ +void meyer(double *p, double *x, int m, int n, void *data) +{ +register int i; +double ui; + + for(i=0; i<n; ++i){ + ui=0.45+0.05*i; + x[i]=p[0]*exp(10.0*p[1]/(ui+p[2]) - 13.0); + } +} + +void jacmeyer(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; +double ui, tmp; + + for(i=j=0; i<n; ++i){ + ui=0.45+0.05*i; + tmp=exp(10.0*p[1]/(ui+p[2]) - 13.0); + + jac[j++]=tmp; + jac[j++]=10.0*p[0]*tmp/(ui+p[2]); + jac[j++]=-10.0*p[0]*p[1]*tmp/((ui+p[2])*(ui+p[2])); + } +} + +/* Osborne's problem, minimum at (0.3754, 1.9358, -1.4647, 0.0129, 0.0221) */ +void osborne(double *p, double *x, int m, int n, void *data) +{ +register int i; +double t; + + for(i=0; i<n; ++i){ + t=10*i; + x[i]=p[0] + p[1]*exp(-p[3]*t) + p[2]*exp(-p[4]*t); + } +} + +void jacosborne(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; +double t, tmp1, tmp2; + + for(i=j=0; i<n; ++i){ + t=10*i; + tmp1=exp(-p[3]*t); + tmp2=exp(-p[4]*t); + + jac[j++]=1.0; + jac[j++]=tmp1; + jac[j++]=tmp2; + jac[j++]=-p[1]*t*tmp1; + jac[j++]=-p[2]*t*tmp2; + } +} + +/* helical valley function, minimum at (1.0, 0.0, 0.0) */ +#ifndef M_PI +#define M_PI 3.14159265358979323846 /* pi */ +#endif + +void helval(double *p, double *x, int m, int n, void *data) +{ +double theta; + + if(p[0]<0.0) + theta=atan(p[1]/p[0])/(2.0*M_PI) + 0.5; + else if(0.0<p[0]) + theta=atan(p[1]/p[0])/(2.0*M_PI); + else + theta=(p[1]>=0)? 0.25 : -0.25; + + x[0]=10.0*(p[2] - 10.0*theta); + x[1]=10.0*(sqrt(p[0]*p[0] + p[1]*p[1]) - 1.0); + x[2]=p[2]; +} + +void jachelval(double *p, double *jac, int m, int n, void *data) +{ +register int i=0; +double tmp; + + tmp=p[0]*p[0] + p[1]*p[1]; + + jac[i++]=50.0*p[1]/(M_PI*tmp); + jac[i++]=-50.0*p[0]/(M_PI*tmp); + jac[i++]=10.0; + + jac[i++]=10.0*p[0]/sqrt(tmp); + jac[i++]=10.0*p[1]/sqrt(tmp); + jac[i++]=0.0; + + jac[i++]=0.0; + jac[i++]=0.0; + jac[i++]=1.0; +} + +/* Boggs - Tolle problem 3 (linearly constrained), minimum at (-0.76744, 0.25581, 0.62791, -0.11628, 0.25581) + * constr1: p[0] + 3*p[1] = 0; + * constr2: p[2] + p[3] - 2*p[4] = 0; + * constr3: p[1] - p[4] = 0; + */ +void bt3(double *p, double *x, int m, int n, void *data) +{ +register int i; +double t1, t2, t3, t4; + + t1=p[0]-p[1]; + t2=p[1]+p[2]-2.0; + t3=p[3]-1.0; + t4=p[4]-1.0; + + for(i=0; i<n; ++i) + x[i]=t1*t1 + t2*t2 + t3*t3 + t4*t4; +} + +void jacbt3(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; +double t1, t2, t3, t4; + + t1=p[0]-p[1]; + t2=p[1]+p[2]-2.0; + t3=p[3]-1.0; + t4=p[4]-1.0; + + for(i=j=0; i<n; ++i){ + jac[j++]=2.0*t1; + jac[j++]=2.0*(t2-t1); + jac[j++]=2.0*t2; + jac[j++]=2.0*t3; + jac[j++]=2.0*t4; + } +} + +/* Hock - Schittkowski problem 28 (linearly constrained), minimum at (0.5, -0.5, 0.5) + * constr1: p[0] + 2*p[1] + 3*p[2] = 1; + */ +void hs28(double *p, double *x, int m, int n, void *data) +{ +register int i; +double t1, t2; + + t1=p[0]+p[1]; + t2=p[1]+p[2]; + + for(i=0; i<n; ++i) + x[i]=t1*t1 + t2*t2; +} + +void jachs28(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; +double t1, t2; + + t1=p[0]+p[1]; + t2=p[1]+p[2]; + + for(i=j=0; i<n; ++i){ + jac[j++]=2.0*t1; + jac[j++]=2.0*(t1+t2); + jac[j++]=2.0*t2; + } +} + +/* Hock - Schittkowski problem 48 (linearly constrained), minimum at (1.0, 1.0, 1.0, 1.0, 1.0) + * constr1: sum {i in 0..4} p[i] = 5; + * constr2: p[2] - 2*(p[3]+p[4]) = -3; + */ +void hs48(double *p, double *x, int m, int n, void *data) +{ +register int i; +double t1, t2, t3; + + t1=p[0]-1.0; + t2=p[1]-p[2]; + t3=p[3]-p[4]; + + for(i=0; i<n; ++i) + x[i]=t1*t1 + t2*t2 + t3*t3; +} + +void jachs48(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; +double t1, t2, t3; + + t1=p[0]-1.0; + t2=p[1]-p[2]; + t3=p[3]-p[4]; + + for(i=j=0; i<n; ++i){ + jac[j++]=2.0*t1; + jac[j++]=2.0*t2; + jac[j++]=-2.0*t2; + jac[j++]=2.0*t3; + jac[j++]=-2.0*t3; + } +} + +/* Hock - Schittkowski problem 51 (linearly constrained), minimum at (1.0, 1.0, 1.0, 1.0, 1.0) + * constr1: p[0] + 3*p[1] = 4; + * constr2: p[2] + p[3] - 2*p[4] = 0; + * constr3: p[1] - p[4] = 0; + */ +void hs51(double *p, double *x, int m, int n, void *data) +{ +register int i; +double t1, t2, t3, t4; + + t1=p[0]-p[1]; + t2=p[1]+p[2]-2.0; + t3=p[3]-1.0; + t4=p[4]-1.0; + + for(i=0; i<n; ++i) + x[i]=t1*t1 + t2*t2 + t3*t3 + t4*t4; +} + +void jachs51(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; +double t1, t2, t3, t4; + + t1=p[0]-p[1]; + t2=p[1]+p[2]-2.0; + t3=p[3]-1.0; + t4=p[4]-1.0; + + for(i=j=0; i<n; ++i){ + jac[j++]=2.0*t1; + jac[j++]=2.0*(t2-t1); + jac[j++]=2.0*t2; + jac[j++]=2.0*t3; + jac[j++]=2.0*t4; + } +} + +/* Hock - Schittkowski problem 01 (box constrained), minimum at (1.0, 1.0) + * constr1: p[1]>=-1.5; + */ +void hs01(double *p, double *x, int m, int n, void *data) +{ +double t; + + t=p[0]*p[0]; + x[0]=10.0*(p[1]-t); + x[1]=1.0-p[0]; +} + +void jachs01(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + + jac[j++]=-20.0*p[0]; + jac[j++]=10.0; + + jac[j++]=-1.0; + jac[j++]=0.0; +} + +/* Hock - Schittkowski MODIFIED problem 21 (box constrained), minimum at (2.0, 0.0) + * constr1: 2 <= p[0] <=50; + * constr2: -50 <= p[1] <=50; + * + * Original HS21 has the additional constraint 10*p[0] - p[1] >= 10; which is inactive + * at the solution, so it is dropped here. + */ +void hs21(double *p, double *x, int m, int n, void *data) +{ + x[0]=p[0]/10.0; + x[1]=p[1]; +} + +void jachs21(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + + jac[j++]=0.1; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=1.0; +} + +/* Problem hatfldb (box constrained), minimum at (0.947214, 0.8, 0.64, 0.4096) + * constri: p[i]>=0.0; (i=1..4) + * constr5: p[1]<=0.8; + */ +void hatfldb(double *p, double *x, int m, int n, void *data) +{ +register int i; + + x[0]=p[0]-1.0; + + for(i=1; i<m; ++i) + x[i]=p[i-1]-sqrt(p[i]); +} + +void jachatfldb(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + + jac[j++]=1.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=1.0; + jac[j++]=-0.5/sqrt(p[1]); + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=1.0; + jac[j++]=-0.5/sqrt(p[2]); + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=1.0; + jac[j++]=-0.5/sqrt(p[3]); +} + +/* Problem hatfldc (box constrained), minimum at (1.0, 1.0, 1.0, 1.0) + * constri: p[i]>=0.0; (i=1..4) + * constri+4: p[i]<=10.0; (i=1..4) + */ +void hatfldc(double *p, double *x, int m, int n, void *data) +{ +register int i; + + x[0]=p[0]-1.0; + + for(i=1; i<m-1; ++i) + x[i]=p[i-1]-sqrt(p[i]); + + x[m-1]=p[m-1]-1.0; +} + +void jachatfldc(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + + jac[j++]=1.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=1.0; + jac[j++]=-0.5/sqrt(p[1]); + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=1.0; + jac[j++]=-0.5/sqrt(p[2]); + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=1.0; +} + +/* Hock - Schittkowski (modified #1) problem 52 (box/linearly constrained), minimum at (-0.09, 0.03, 0.25, -0.19, 0.03) + * constr1: p[0] + 3*p[1] = 0; + * constr2: p[2] + p[3] - 2*p[4] = 0; + * constr3: p[1] - p[4] = 0; + * + * To the above 3 constraints, we add the following 5: + * constr4: -0.09 <= p[0]; + * constr5: 0.0 <= p[1] <= 0.3; + * constr6: p[2] <= 0.25; + * constr7: -0.2 <= p[3] <= 0.3; + * constr8: 0.0 <= p[4] <= 0.3; + * + */ +void mod1hs52(double *p, double *x, int m, int n, void *data) +{ + x[0]=4.0*p[0]-p[1]; + x[1]=p[1]+p[2]-2.0; + x[2]=p[3]-1.0; + x[3]=p[4]-1.0; +} + +void jacmod1hs52(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + + jac[j++]=4.0; + jac[j++]=-1.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=1.0; + jac[j++]=1.0; + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=1.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=1.0; +} + + +/* Hock - Schittkowski (modified #2) problem 52 (linear inequality constrained), minimum at (0.5, 2.0, 0.0, 1.0, 1.0) + * A fifth term [(p[0]-0.5)^2] is added to the objective function and + * the equality contraints are replaced by the following inequalities: + * constr1: p[0] + 3*p[1] >= -1.0; + * constr2: p[2] + p[3] - 2*p[4] >= -2.0; + * constr3: p[1] - p[4] <= 7.0; + * + * + */ +void mod2hs52(double *p, double *x, int m, int n, void *data) +{ + x[0]=4.0*p[0]-p[1]; + x[1]=p[1]+p[2]-2.0; + x[2]=p[3]-1.0; + x[3]=p[4]-1.0; + x[4]=p[0]-0.5; +} + +void jacmod2hs52(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + + jac[j++]=4.0; + jac[j++]=-1.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=1.0; + jac[j++]=1.0; + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=1.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=1.0; + + jac[j++]=1.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; +} + +/* Schittkowski (modified) problem 235 (box/linearly constrained), minimum at (-1.725, 2.9, 0.725) + * constr1: p[0] + p[2] = -1.0; + * + * To the above constraint, we add the following 2: + * constr2: p[1] - 4*p[2] = 0; + * constr3: 0.1 <= p[1] <= 2.9; + * constr4: 0.7 <= p[2]; + * + */ +void mods235(double *p, double *x, int m, int n, void *data) +{ + x[0]=0.1*(p[0]-1.0); + x[1]=p[1]-p[0]*p[0]; +} + +void jacmods235(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + + jac[j++]=0.1; + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=-2.0*p[0]; + jac[j++]=1.0; + jac[j++]=0.0; +} + +/* Boggs and Tolle modified problem 7 (box/linearly constrained), minimum at (0.7, 0.49, 0.19, 1.19, -0.2) + * We keep the original objective function & starting point and use the following constraints: + * + * subject to cons1: + * x[1]+x[2] - x[3] = 1.0; + * subject to cons2: + * x[2] - x[4] + x[1] = 0.0; + * subject to cons3: + * x[5] + x[1] = 0.5; + * subject to cons4: + * x[5]>=-0.3; + * subject to cons5: + * x[1]<=0.7; + * + */ +void modbt7(double *p, double *x, int m, int n, void *data) +{ +register int i; + + for(i=0; i<n; ++i) + x[i]=100.0*(p[1]-p[0]*p[0])*(p[1]-p[0]*p[0]) + (p[0]-1.0)*(p[0]-1.0); +} + +void jacmodbt7(double *p, double *jac, int m, int n, void *data) +{ +register int i, j; + + for(i=j=0; i<m; ++i){ + jac[j++]=-400.0*(p[1]-p[0]*p[0])*p[0] + 2.0*p[0] - 2.0; + jac[j++]=200.0*(p[1]-p[0]*p[0]); + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + } +} + +/* Equilibrium combustion problem, constrained nonlinear equation from the book by Floudas et al. + * Minimum at (0.0034, 31.3265, 0.0684, 0.8595, 0.0370) + * constri: p[i]>=0.0001; (i=1..5) + * constri+5: p[i]<=100.0; (i=1..5) + */ +void combust(double *p, double *x, int m, int n, void *data) +{ + double R, R5, R6, R7, R8, R9, R10; + + R=10; + R5=0.193; + R6=4.10622*1e-4; + R7=5.45177*1e-4; + R8=4.4975*1e-7; + R9=3.40735*1e-5; + R10=9.615*1e-7; + + x[0]=p[0]*p[1]+p[0]-3*p[4]; + x[1]=2*p[0]*p[1]+p[0]+3*R10*p[1]*p[1]+p[1]*p[2]*p[2]+R7*p[1]*p[2]+R9*p[1]*p[3]+R8*p[1]-R*p[4]; + x[2]=2*p[1]*p[2]*p[2]+R7*p[1]*p[2]+2*R5*p[2]*p[2]+R6*p[2]-8*p[4]; + x[3]=R9*p[1]*p[3]+2*p[3]*p[3]-4*R*p[4]; + x[4]=p[0]*p[1]+p[0]+R10*p[1]*p[1]+p[1]*p[2]*p[2]+R7*p[1]*p[2]+R9*p[1]*p[3]+R8*p[1]+R5*p[2]*p[2]+R6*p[2]+p[3]*p[3]-1.0; +} + +void jaccombust(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + double R, R5, R6, R7, R8, R9, R10; + + R=10; + R5=0.193; + R6=4.10622*1e-4; + R7=5.45177*1e-4; + R8=4.4975*1e-7; + R9=3.40735*1e-5; + R10=9.615*1e-7; + + for(j=0; j<m*n; ++j) jac[j]=0.0; + + j=0; + jac[j]=p[1]+1; + jac[j+1]=p[0]; + jac[j+4]=-3; + + j+=m; + jac[j]=2*p[1]+1; + jac[j+1]=2*p[0]+6*R10*p[1]+p[2]*p[2]+R7*p[2]+R9*p[3]+R8; + jac[j+2]=2*p[1]*p[2]+R7*p[1]; + jac[j+3]=R9*p[1]; + jac[j+4]=-R; + + j+=m; + jac[j+1]=2*p[2]*p[2]+R7*p[2]; + jac[j+2]=4*p[1]*p[2]+R7*p[1]+4*R5*p[2]+R6; + jac[j+4]=-8; + + j+=m; + jac[j+1]=R9*p[3]; + jac[j+3]=R9*p[1]+4*p[3]; + jac[j+4]=-4*R; + + j+=m; + jac[j]=p[1]+1; + jac[j+1]=p[0]+2*R10*p[1]+p[2]*p[2]+R7*p[2]+R9*p[3]+R8; + jac[j+2]=2*p[1]*p[2]+R7*p[1]+2*R5*p[2]+R6; + jac[j+3]=R9*p[1]+2*p[3]; +} + +/* Hock - Schittkowski (modified) problem 76 (linear inequalities & equations constrained), minimum at (0.0, 0.00909091, 0.372727, 0.354545) + * The non-squared terms in the objective function have been removed, the rhs of constr2 has been changed to 0.4 (from 4) + * and constr3 has been changed to an equation. + * + * constr1: p[0] + 2*p[1] + p[2] + p[3] <= 5; + * constr2: 3*p[0] + p[1] + 2*p[2] - p[3] <= 0.4; + * constr3: p[1] + 4*p[2] = 1.5; + * + */ +void modhs76(double *p, double *x, int m, int n, void *data) +{ + x[0]=p[0]; + x[1]=sqrt(0.5)*p[1]; + x[2]=p[2]; + x[3]=sqrt(0.5)*p[3]; +} + +void jacmodhs76(double *p, double *jac, int m, int n, void *data) +{ +register int j=0; + + jac[j++]=1.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=sqrt(0.5); + jac[j++]=0.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=1.0; + jac[j++]=0.0; + + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=0.0; + jac[j++]=sqrt(0.5); +} + + + +int main() +{ +register int i, j; +int problem, ret; +double p[5], // 5 is max(2, 3, 5) + x[16]; // 16 is max(2, 3, 5, 6, 16) +int m, n; +double opts[LM_OPTS_SZ], info[LM_INFO_SZ]; +char *probname[]={ + "Rosenbrock function", + "modified Rosenbrock problem", + "Powell's function", + "Wood's function", + "Meyer's (reformulated) problem", + "Osborne's problem", + "helical valley function", + "Boggs & Tolle's problem #3", + "Hock - Schittkowski problem #28", + "Hock - Schittkowski problem #48", + "Hock - Schittkowski problem #51", + "Hock - Schittkowski problem #01", + "Hock - Schittkowski modified problem #21", + "hatfldb problem", + "hatfldc problem", + "equilibrium combustion problem", + "Hock - Schittkowski modified #1 problem #52", + "Schittkowski modified problem #235", + "Boggs & Tolle modified problem #7", + "Hock - Schittkowski modified #2 problem #52", + "Hock - Schittkowski modified problem #76", +}; + + opts[0]=LM_INIT_MU; opts[1]=1E-15; opts[2]=1E-15; opts[3]=1E-20; + opts[4]= LM_DIFF_DELTA; // relevant only if the Jacobian is approximated using finite differences; specifies forward differencing + //opts[4]=-LM_DIFF_DELTA; // specifies central differencing to approximate Jacobian; more accurate but more expensive to compute! + + /* uncomment the appropriate line below to select a minimization problem */ + problem= + //0; // Rosenbrock function + //1; // modified Rosenbrock problem + //2; // Powell's function + //3; // Wood's function + 4; // Meyer's (reformulated) problem + //5; // Osborne's problem + //6; // helical valley function +#ifdef HAVE_LAPACK + //7; // Boggs & Tolle's problem 3 + //8; // Hock - Schittkowski problem 28 + //9; // Hock - Schittkowski problem 48 + //10; // Hock - Schittkowski problem 51 +#else // no LAPACK +#ifdef _MSC_VER +#pragma message("LAPACK not available, some test problems cannot be used") +#else +#warning LAPACK not available, some test problems cannot be used +#endif // _MSC_VER + +#endif /* HAVE_LAPACK */ + //11; // Hock - Schittkowski problem 01 + //12; // Hock - Schittkowski modified problem 21 + //13; // hatfldb problem + //14; // hatfldc problem + //15; // equilibrium combustion problem +#ifdef HAVE_LAPACK + //16; // Hock - Schittkowski modified #1 problem 52 + //17; // Schittkowski modified problem 235 + //18; // Boggs & Tolle modified problem #7 + //19; // Hock - Schittkowski modified #2 problem 52 + //20; // Hock - Schittkowski modified problem #76" +#endif /* HAVE_LAPACK */ + + switch(problem){ + default: fprintf(stderr, "unknown problem specified (#%d)! Note that some minimization problems require LAPACK.\n", problem); + exit(1); + break; + + case 0: + /* Rosenbrock function */ + m=2; n=2; + p[0]=-1.2; p[1]=1.0; + for(i=0; i<n; i++) x[i]=0.0; + ret=dlevmar_der(ros, jacros, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + //ret=dlevmar_dif(ros, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // no Jacobian + break; + + case 1: + /* modified Rosenbrock problem */ + m=2; n=3; + p[0]=-1.2; p[1]=1.0; + for(i=0; i<n; i++) x[i]=0.0; + ret=dlevmar_der(modros, jacmodros, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + //ret=dlevmar_dif(modros, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // no Jacobian + break; + + case 2: + /* Powell's function */ + m=2; n=2; + p[0]=3.0; p[1]=1.0; + for(i=0; i<n; i++) x[i]=0.0; + ret=dlevmar_der(powell, jacpowell, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + //ret=dlevmar_dif(powell, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // no Jacobian + break; + + case 3: + /* Wood's function */ + m=4; n=6; + p[0]=-3.0; p[1]=-1.0; p[2]=-3.0; p[3]=-1.0; + for(i=0; i<n; i++) x[i]=0.0; + ret=dlevmar_dif(wood, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // no Jacobian + break; + + case 4: + /* Meyer's data fitting problem */ + m=3; n=16; + p[0]=8.85; p[1]=4.0; p[2]=2.5; + x[0]=34.780; x[1]=28.610; x[2]=23.650; x[3]=19.630; + x[4]=16.370; x[5]=13.720; x[6]=11.540; x[7]=9.744; + x[8]=8.261; x[9]=7.030; x[10]=6.005; x[11]=5.147; + x[12]=4.427; x[13]=3.820; x[14]=3.307; x[15]=2.872; + //ret=dlevmar_der(meyer, jacmeyer, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + + { double *work, *covar; + work=malloc((LM_DIF_WORKSZ(m, n)+m*m)*sizeof(double)); + if(!work){ + fprintf(stderr, "memory allocation request failed in main()\n"); + exit(1); + } + covar=work+LM_DIF_WORKSZ(m, n); + + ret=dlevmar_dif(meyer, p, x, m, n, 1000, opts, info, work, covar, NULL); // no Jacobian, caller allocates work memory, covariance estimated + + printf("Covariance of the fit:\n"); + for(i=0; i<m; ++i){ + for(j=0; j<m; ++j) + printf("%g ", covar[i*m+j]); + printf("\n"); + } + printf("\n"); + + free(work); + } + +/* uncomment the following block to verify Jacobian */ +/* + { + double err[16]; + dlevmar_chkjac(meyer, jacmeyer, p, m, n, NULL, err); + for(i=0; i<n; ++i) printf("gradient %d, err %g\n", i, err[i]); + } +*/ + break; + + case 5: + /* Osborne's data fitting problem */ + { + double x33[]={ + 8.44E-1, 9.08E-1, 9.32E-1, 9.36E-1, 9.25E-1, 9.08E-1, 8.81E-1, + 8.5E-1, 8.18E-1, 7.84E-1, 7.51E-1, 7.18E-1, 6.85E-1, 6.58E-1, + 6.28E-1, 6.03E-1, 5.8E-1, 5.58E-1, 5.38E-1, 5.22E-1, 5.06E-1, + 4.9E-1, 4.78E-1, 4.67E-1, 4.57E-1, 4.48E-1, 4.38E-1, 4.31E-1, + 4.24E-1, 4.2E-1, 4.14E-1, 4.11E-1, 4.06E-1}; + + m=5; n=33; + p[0]=0.5; p[1]=1.5; p[2]=-1.0; p[3]=1.0E-2; p[4]=2.0E-2; + + ret=dlevmar_der(osborne, jacosborne, p, x33, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + //ret=dlevmar_dif(osborne, p, x33, m, n, 1000, opts, info, NULL, NULL, NULL); // no Jacobian + } + break; + + case 6: + /* helical valley function */ + m=3; n=3; + p[0]=-1.0; p[1]=0.0; p[2]=0.0; + for(i=0; i<n; i++) x[i]=0.0; + ret=dlevmar_der(helval, jachelval, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + //ret=dlevmar_dif(helval, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // no Jacobian + break; + +#ifdef HAVE_LAPACK + case 7: + /* Boggs-Tolle problem 3 */ + m=5; n=5; + p[0]=2.0; p[1]=2.0; p[2]=2.0; + p[3]=2.0; p[4]=2.0; + for(i=0; i<n; i++) x[i]=0.0; + + { + double A[3*5]={1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, -2.0, 0.0, 1.0, 0.0, 0.0, -1.0}, + b[3]={0.0, 0.0, 0.0}; + + ret=dlevmar_lec_der(bt3, jacbt3, p, x, m, n, A, b, 3, 1000, opts, info, NULL, NULL, NULL); // lin. constraints, analytic Jacobian + //ret=dlevmar_lec_dif(bt3, p, x, m, n, A, b, 3, 1000, opts, info, NULL, NULL, NULL); // lin. constraints, no Jacobian + } + break; + + case 8: + /* Hock - Schittkowski problem 28 */ + m=3; n=3; + p[0]=-4.0; p[1]=1.0; p[2]=1.0; + for(i=0; i<n; i++) x[i]=0.0; + + { + double A[1*3]={1.0, 2.0, 3.0}, + b[1]={1.0}; + + ret=dlevmar_lec_der(hs28, jachs28, p, x, m, n, A, b, 1, 1000, opts, info, NULL, NULL, NULL); // lin. constraints, analytic Jacobian + //ret=dlevmar_lec_dif(hs28, p, x, m, n, A, b, 1, 1000, opts, info, NULL, NULL, NULL); // lin. constraints, no Jacobian + } + break; + + case 9: + /* Hock - Schittkowski problem 48 */ + m=5; n=5; + p[0]=3.0; p[1]=5.0; p[2]=-3.0; + p[3]=2.0; p[4]=-2.0; + for(i=0; i<n; i++) x[i]=0.0; + + { + double A[2*5]={1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, -2.0, -2.0}, + b[2]={5.0, -3.0}; + + ret=dlevmar_lec_der(hs48, jachs48, p, x, m, n, A, b, 2, 1000, opts, info, NULL, NULL, NULL); // lin. constraints, analytic Jacobian + //ret=dlevmar_lec_dif(hs48, p, x, m, n, A, b, 2, 1000, opts, info, NULL, NULL, NULL); // lin. constraints, no Jacobian + } + break; + + case 10: + /* Hock - Schittkowski problem 51 */ + m=5; n=5; + p[0]=2.5; p[1]=0.5; p[2]=2.0; + p[3]=-1.0; p[4]=0.5; + for(i=0; i<n; i++) x[i]=0.0; + + { + double A[3*5]={1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, -2.0, 0.0, 1.0, 0.0, 0.0, -1.0}, + b[3]={4.0, 0.0, 0.0}; + + ret=dlevmar_lec_der(hs51, jachs51, p, x, m, n, A, b, 3, 1000, opts, info, NULL, NULL, NULL); // lin. constraints, analytic Jacobian + //ret=dlevmar_lec_dif(hs51, p, x, m, n, A, b, 3, 1000, opts, info, NULL, NULL, NULL); // lin. constraints, no Jacobian + } + break; + +#endif /* HAVE_LAPACK */ + + case 11: + /* Hock - Schittkowski problem 01 */ + m=2; n=2; + p[0]=-2.0; p[1]=1.0; + for(i=0; i<n; i++) x[i]=0.0; + //ret=dlevmar_der(hs01, jachs01, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + { + double lb[2], ub[2]; + + lb[0]=-DBL_MAX; lb[1]=-1.5; + ub[0]=ub[1]=DBL_MAX; + + ret=dlevmar_bc_der(hs01, jachs01, p, x, m, n, lb, ub, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + } + break; + + case 12: + /* Hock - Schittkowski (modified) problem 21 */ + m=2; n=2; + p[0]=-1.0; p[1]=-1.0; + for(i=0; i<n; i++) x[i]=0.0; + //ret=dlevmar_der(hs21, jachs21, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + { + double lb[2], ub[2]; + + lb[0]=2.0; lb[1]=-50.0; + ub[0]=50.0; ub[1]=50.0; + + ret=dlevmar_bc_der(hs21, jachs21, p, x, m, n, lb, ub, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + } + break; + + case 13: + /* hatfldb problem */ + m=4; n=4; + p[0]=p[1]=p[2]=p[3]=0.1; + for(i=0; i<n; i++) x[i]=0.0; + //ret=dlevmar_der(hatfldb, jachatfldb, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + { + double lb[4], ub[4]; + + lb[0]=lb[1]=lb[2]=lb[3]=0.0; + + ub[0]=ub[2]=ub[3]=DBL_MAX; + ub[1]=0.8; + + ret=dlevmar_bc_der(hatfldb, jachatfldb, p, x, m, n, lb, ub, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + } + break; + + case 14: + /* hatfldc problem */ + m=4; n=4; + p[0]=p[1]=p[2]=p[3]=0.9; + for(i=0; i<n; i++) x[i]=0.0; + //ret=dlevmar_der(hatfldc, jachatfldc, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + { + double lb[4], ub[4]; + + lb[0]=lb[1]=lb[2]=lb[3]=0.0; + + ub[0]=ub[1]=ub[2]=ub[3]=10.0; + + ret=dlevmar_bc_der(hatfldc, jachatfldc, p, x, m, n, lb, ub, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + } + break; + + case 15: + /* equilibrium combustion problem */ + m=5; n=5; + p[0]=p[1]=p[2]=p[3]=p[4]=0.0001; + for(i=0; i<n; i++) x[i]=0.0; + //ret=dlevmar_der(combust, jaccombust, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + { + double lb[5], ub[5]; + + lb[0]=lb[1]=lb[2]=lb[3]=lb[4]=0.0001; + + ub[0]=ub[1]=ub[2]=ub[3]=ub[4]=100.0; + + ret=dlevmar_bc_der(combust, jaccombust, p, x, m, n, lb, ub, 5000, opts, info, NULL, NULL, NULL); // with analytic Jacobian + } + break; + +#ifdef HAVE_LAPACK + case 16: + /* Hock - Schittkowski modified #1 problem 52 */ + m=5; n=4; + p[0]=2.0; p[1]=2.0; p[2]=2.0; + p[3]=2.0; p[4]=2.0; + for(i=0; i<n; i++) x[i]=0.0; + + { + double A[3*5]={1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, -2.0, 0.0, 1.0, 0.0, 0.0, -1.0}, + b[3]={0.0, 0.0, 0.0}; + + double lb[5], ub[5]; + + double weights[5]={2000.0, 2000.0, 2000.0, 2000.0, 2000.0}; // penalty terms weights + + lb[0]=-0.09; lb[1]=0.0; lb[2]=-DBL_MAX; lb[3]=-0.2; lb[4]=0.0; + ub[0]=DBL_MAX; ub[1]=0.3; ub[2]=0.25; ub[3]=0.3; ub[4]=0.3; + + ret=dlevmar_blec_der(mod1hs52, jacmod1hs52, p, x, m, n, lb, ub, A, b, 3, weights, 1000, opts, info, NULL, NULL, NULL); // box & lin. constraints, analytic Jacobian + //ret=dlevmar_blec_dif(mod1hs52, p, x, m, n, lb, ub, A, b, 3, weights, 1000, opts, info, NULL, NULL, NULL); // box & lin. constraints, no Jacobian + } + break; + + case 17: + /* Schittkowski modified problem 235 */ + m=3; n=2; + p[0]=-2.0; p[1]=3.0; p[2]=1.0; + for(i=0; i<n; i++) x[i]=0.0; + + { + double A[2*3]={1.0, 0.0, 1.0, 0.0, 1.0, -4.0}, + b[2]={-1.0, 0.0}; + + double lb[3], ub[3]; + + lb[0]=-DBL_MAX; lb[1]=0.1; lb[2]=0.7; + ub[0]=DBL_MAX; ub[1]=2.9; ub[2]=DBL_MAX; + + ret=dlevmar_blec_der(mods235, jacmods235, p, x, m, n, lb, ub, A, b, 2, NULL, 1000, opts, info, NULL, NULL, NULL); // box & lin. constraints, analytic Jacobian + //ret=dlevmar_blec_dif(mods235, p, x, m, n, lb, ub, A, b, 2, NULL, 1000, opts, info, NULL, NULL, NULL); // box & lin. constraints, no Jacobian + } + break; + + case 18: + /* Boggs & Tolle modified problem 7 */ + m=5; n=5; + p[0]=-2.0; p[1]=1.0; p[2]=1.0; p[3]=1.0; p[4]=1.0; + for(i=0; i<n; i++) x[i]=0.0; + + { + double A[3*5]={1.0, 1.0, -1.0, 0.0, 0.0, 1.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}, + b[3]={1.0, 0.0, 0.5}; + + double lb[5], ub[5]; + + lb[0]=-DBL_MAX; lb[1]=-DBL_MAX; lb[2]=-DBL_MAX; lb[3]=-DBL_MAX; lb[4]=-0.3; + ub[0]=0.7; ub[1]= DBL_MAX; ub[2]= DBL_MAX; ub[3]= DBL_MAX; ub[4]=DBL_MAX; + + ret=dlevmar_blec_der(modbt7, jacmodbt7, p, x, m, n, lb, ub, A, b, 3, NULL, 1000, opts, info, NULL, NULL, NULL); // box & lin. constraints, analytic Jacobian + //ret=dlevmar_blec_dif(modbt7, p, x, m, n, lb, ub, A, b, 3, NULL, 10000, opts, info, NULL, NULL, NULL); // box & lin. constraints, no Jacobian + } + break; + + case 19: + /* Hock - Schittkowski modified #2 problem 52 */ + m=5; n=5; + p[0]=2.0; p[1]=2.0; p[2]=2.0; + p[3]=2.0; p[4]=2.0; + for(i=0; i<n; i++) x[i]=0.0; + + { + double C[3*5]={1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, -2.0, 0.0, -1.0, 0.0, 0.0, 1.0}, + d[3]={-1.0, -2.0, -7.0}; + + ret=dlevmar_bleic_der(mod2hs52, jacmod2hs52, p, x, m, n, NULL, NULL, NULL, NULL, 0, C, d, 3, 1000, opts, info, NULL, NULL, NULL); // lin. ineq. constraints, analytic Jacobian + //ret=dlevmar_bleic_dif(mod2hs52, p, x, m, n, NULL, NULL, NULL, NULL, 0, C, d, 3, 1000, opts, info, NULL, NULL, NULL); // lin. ineq. constraints, no Jacobian + } + break; + + case 20: + /* Hock - Schittkowski modified problem 76 */ + m=4; n=4; + p[0]=0.5; p[1]=0.5; p[2]=0.5; p[3]=0.5; + for(i=0; i<n; i++) x[i]=0.0; + + { + double A[1*4]={0.0, 1.0, 4.0, 0.0}, + b[1]={1.5}; + + double C[2*4]={-1.0, -2.0, -1.0, -1.0, -3.0, -1.0, -2.0, 1.0}, + d[2]={-5.0, -0.4}; + + double lb[4]={0.0, 0.0, 0.0, 0.0}; + + ret=dlevmar_bleic_der(modhs76, jacmodhs76, p, x, m, n, lb, NULL, A, b, 1, C, d, 2, 1000, opts, info, NULL, NULL, NULL); // lin. ineq. constraints, analytic Jacobian + //ret=dlevmar_bleic_dif(modhs76, p, x, m, n, lb, NULL, A, b, 1, C, d, 2, 1000, opts, info, NULL, NULL, NULL); // lin. ineq. constraints, no Jacobian + /* variations: + * if no lb is used, the minimizer is (-0.1135922 0.1330097 0.3417476 0.07572816) + * if the rhs of constr2 is 4.0, the minimizer is (0.0, 0.166667, 0.333333, 0.0) + */ + } + break; + +#endif /* HAVE_LAPACK */ + } /* switch */ + + printf("Results for %s:\n", probname[problem]); + printf("Levenberg-Marquardt returned %d in %g iter, reason %g\nSolution: ", ret, info[5], info[6]); + for(i=0; i<m; ++i) + printf("%.7g ", p[i]); + printf("\n\nMinimization info:\n"); + for(i=0; i<LM_INFO_SZ; ++i) + printf("%g ", info[i]); + printf("\n"); + + return 0; +} diff --git a/sci-libs/levmar/levmar-2.5/lmdemo.o b/sci-libs/levmar/levmar-2.5/lmdemo.o Binary files differnew file mode 100644 index 000000000..817e5b31b --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmdemo.o diff --git a/sci-libs/levmar/levmar-2.5/lmdemo.vcproj b/sci-libs/levmar/levmar-2.5/lmdemo.vcproj new file mode 100644 index 000000000..ce2b9b584 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmdemo.vcproj @@ -0,0 +1,194 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="8,00"
+ Name="lmdemo"
+ ProjectGUID="{4A085982-23EF-4B63-8D72-B901D0521326}"
+ Keyword="Win32Proj"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="Debug"
+ IntermediateDirectory="Debug"
+ ConfigurationType="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="."
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="3"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="4"
+ CompileAs="1"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="levmar.lib clapack.lib blas.lib libF77.lib libI77.lib"
+ AdditionalLibraryDirectories=".\Debug;c:\RECOVER\src\lib"
+ GenerateManifest="true"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="Release"
+ IntermediateDirectory="Release"
+ ConfigurationType="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ AdditionalIncludeDirectories="."
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
+ RuntimeLibrary="2"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="3"
+ CompileAs="1"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="levmar.lib clapack.lib blas.lib libF77.lib libI77.lib"
+ AdditionalLibraryDirectories=".\Release;c:\RECOVER\src\lib"
+ GenerateManifest="true"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <Filter
+ Name="Header Files"
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"
+ >
+ <File
+ RelativePath=".\levmar.h"
+ >
+ </File>
+ </Filter>
+ <Filter
+ Name="Resource Files"
+ Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"
+ >
+ </Filter>
+ <Filter
+ Name="Source Files"
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+ >
+ <File
+ RelativePath=".\lmdemo.c"
+ >
+ </File>
+ </Filter>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
diff --git a/sci-libs/levmar/levmar-2.5/lmlec.c b/sci-libs/levmar/levmar-2.5/lmlec.c new file mode 100644 index 000000000..d5cea7acb --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmlec.c @@ -0,0 +1,80 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004-05 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************* + * Wrappers for linearly constrained Levenberg-Marquardt minimization. The same + * core code is used with appropriate #defines to derive single and double + * precision versions, see also lmlec_core.c + *******************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +#include "levmar.h" +#include "misc.h" + + +#ifndef HAVE_LAPACK + +#ifdef _MSC_VER +#pragma message("Linearly constrained optimization requires LAPACK and was not compiled!") +#else +#warning Linearly constrained optimization requires LAPACK and was not compiled! +#endif // _MSC_VER + +#else // LAPACK present + +#if !defined(LM_DBL_PREC) && !defined(LM_SNGL_PREC) +#error At least one of LM_DBL_PREC, LM_SNGL_PREC should be defined! +#endif + + +#ifdef LM_SNGL_PREC +/* single precision (float) definitions */ +#define LM_REAL float +#define LM_PREFIX s + +#define __SUBCNST(x) x##F +#define LM_CNST(x) __SUBCNST(x) // force substitution + +#include "lmlec_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef __SUBCNST +#undef LM_CNST +#endif /* LM_SNGL_PREC */ + +#ifdef LM_DBL_PREC +/* double precision definitions */ +#define LM_REAL double +#define LM_PREFIX d + +#define LM_CNST(x) (x) + +#include "lmlec_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_CNST +#endif /* LM_DBL_PREC */ + +#endif /* HAVE_LAPACK */ + diff --git a/sci-libs/levmar/levmar-2.5/lmlec.o b/sci-libs/levmar/levmar-2.5/lmlec.o Binary files differnew file mode 100644 index 000000000..fc73c0f39 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmlec.o diff --git a/sci-libs/levmar/levmar-2.5/lmlec_core.c b/sci-libs/levmar/levmar-2.5/lmlec_core.c new file mode 100644 index 000000000..083408895 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/lmlec_core.c @@ -0,0 +1,657 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004-05 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#ifndef LM_REAL // not included by lmlec.c +#error This file should not be compiled directly! +#endif + + +/* precision-specific definitions */ +#define LMLEC_DATA LM_ADD_PREFIX(lmlec_data) +#define LMLEC_ELIM LM_ADD_PREFIX(lmlec_elim) +#define LMLEC_FUNC LM_ADD_PREFIX(lmlec_func) +#define LMLEC_JACF LM_ADD_PREFIX(lmlec_jacf) +#define LEVMAR_LEC_DER LM_ADD_PREFIX(levmar_lec_der) +#define LEVMAR_LEC_DIF LM_ADD_PREFIX(levmar_lec_dif) +#define LEVMAR_DER LM_ADD_PREFIX(levmar_der) +#define LEVMAR_DIF LM_ADD_PREFIX(levmar_dif) +#define LEVMAR_TRANS_MAT_MAT_MULT LM_ADD_PREFIX(levmar_trans_mat_mat_mult) +#define LEVMAR_COVAR LM_ADD_PREFIX(levmar_covar) +#define LEVMAR_FDIF_FORW_JAC_APPROX LM_ADD_PREFIX(levmar_fdif_forw_jac_approx) + +#define GEQP3 LM_MK_LAPACK_NAME(geqp3) +#define ORGQR LM_MK_LAPACK_NAME(orgqr) +#define TRTRI LM_MK_LAPACK_NAME(trtri) + +struct LMLEC_DATA{ + LM_REAL *c, *Z, *p, *jac; + int ncnstr; + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata); + void (*jacf)(LM_REAL *p, LM_REAL *jac, int m, int n, void *adata); + void *adata; +}; + +/* prototypes for LAPACK routines */ +extern int GEQP3(int *m, int *n, LM_REAL *a, int *lda, int *jpvt, + LM_REAL *tau, LM_REAL *work, int *lwork, int *info); + +extern int ORGQR(int *m, int *n, int *k, LM_REAL *a, int *lda, LM_REAL *tau, + LM_REAL *work, int *lwork, int *info); + +extern int TRTRI(char *uplo, char *diag, int *n, LM_REAL *a, int *lda, int *info); + +/* + * This function implements an elimination strategy for linearly constrained + * optimization problems. The strategy relies on QR decomposition to transform + * an optimization problem constrained by Ax=b to an equivalent, unconstrained + * one. Also referred to as "null space" or "reduced Hessian" method. + * See pp. 430-433 (chap. 15) of "Numerical Optimization" by Nocedal-Wright + * for details. + * + * A is mxn with m<=n and rank(A)=m + * Two matrices Y and Z of dimensions nxm and nx(n-m) are computed from A^T so that + * their columns are orthonormal and every x can be written as x=Y*b + Z*x_z= + * c + Z*x_z, where c=Y*b is a fixed vector of dimension n and x_z is an + * arbitrary vector of dimension n-m. Then, the problem of minimizing f(x) + * subject to Ax=b is equivalent to minimizing f(c + Z*x_z) with no constraints. + * The computed Y and Z are such that any solution of Ax=b can be written as + * x=Y*x_y + Z*x_z for some x_y, x_z. Furthermore, A*Y is nonsingular, A*Z=0 + * and Z spans the null space of A. + * + * The function accepts A, b and computes c, Y, Z. If b or c is NULL, c is not + * computed. Also, Y can be NULL in which case it is not referenced. + * The function returns LM_ERROR in case of error, A's computed rank if successful + * + */ +static int LMLEC_ELIM(LM_REAL *A, LM_REAL *b, LM_REAL *c, LM_REAL *Y, LM_REAL *Z, int m, int n) +{ +static LM_REAL eps=LM_CNST(-1.0); + +LM_REAL *buf=NULL; +LM_REAL *a, *tau, *work, *r, aux; +register LM_REAL tmp; +int a_sz, jpvt_sz, tau_sz, r_sz, Y_sz, worksz; +int info, rank, *jpvt, tot_sz, mintmn, tm, tn; +register int i, j, k; + + if(m>n){ + fprintf(stderr, RCAT("matrix of constraints cannot have more rows than columns in", LMLEC_ELIM) "()!\n"); + return LM_ERROR; + } + + tm=n; tn=m; // transpose dimensions + mintmn=m; + + /* calculate required memory size */ + worksz=-1; // workspace query. Optimal work size is returned in aux + //ORGQR((int *)&tm, (int *)&tm, (int *)&mintmn, NULL, (int *)&tm, NULL, (LM_REAL *)&aux, &worksz, &info); + GEQP3((int *)&tm, (int *)&tn, NULL, (int *)&tm, NULL, NULL, (LM_REAL *)&aux, (int *)&worksz, &info); + worksz=(int)aux; + a_sz=tm*tm; // tm*tn is enough for xgeqp3() + jpvt_sz=tn; + tau_sz=mintmn; + r_sz=mintmn*mintmn; // actually smaller if a is not of full row rank + Y_sz=(Y)? 0 : tm*tn; + + tot_sz=(a_sz + tau_sz + r_sz + worksz + Y_sz)*sizeof(LM_REAL) + jpvt_sz*sizeof(int); /* should be arranged in that order for proper doubles alignment */ + buf=(LM_REAL *)malloc(tot_sz); /* allocate a "big" memory chunk at once */ + if(!buf){ + fprintf(stderr, RCAT("Memory allocation request failed in ", LMLEC_ELIM) "()\n"); + return LM_ERROR; + } + + a=buf; + tau=a+a_sz; + r=tau+tau_sz; + work=r+r_sz; + if(!Y){ + Y=work+worksz; + jpvt=(int *)(Y+Y_sz); + } + else + jpvt=(int *)(work+worksz); + + /* copy input array so that LAPACK won't destroy it. Note that copying is + * done in row-major order, which equals A^T in column-major + */ + for(i=0; i<tm*tn; ++i) + a[i]=A[i]; + + /* clear jpvt */ + for(i=0; i<jpvt_sz; ++i) jpvt[i]=0; + + /* rank revealing QR decomposition of A^T*/ + GEQP3((int *)&tm, (int *)&tn, a, (int *)&tm, jpvt, tau, work, (int *)&worksz, &info); + //dgeqpf_((int *)&tm, (int *)&tn, a, (int *)&tm, jpvt, tau, work, &info); + /* error checking */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", GEQP3) " in ", LMLEC_ELIM) "()\n", -info); + } + else if(info>0){ + fprintf(stderr, RCAT(RCAT("unknown LAPACK error (%d) for ", GEQP3) " in ", LMLEC_ELIM) "()\n", info); + } + free(buf); + return LM_ERROR; + } + /* the upper triangular part of a now contains the upper triangle of the unpermuted R */ + + if(eps<0.0){ + LM_REAL aux; + + /* compute machine epsilon. DBL_EPSILON should do also */ + for(eps=LM_CNST(1.0); aux=eps+LM_CNST(1.0), aux-LM_CNST(1.0)>0.0; eps*=LM_CNST(0.5)) + ; + eps*=LM_CNST(2.0); + } + + tmp=tm*LM_CNST(10.0)*eps*FABS(a[0]); // threshold. tm is max(tm, tn) + tmp=(tmp>LM_CNST(1E-12))? tmp : LM_CNST(1E-12); // ensure that threshold is not too small + /* compute A^T's numerical rank by counting the non-zeros in R's diagonal */ + for(i=rank=0; i<mintmn; ++i) + if(a[i*(tm+1)]>tmp || a[i*(tm+1)]<-tmp) ++rank; /* loop across R's diagonal elements */ + else break; /* diagonal is arranged in absolute decreasing order */ + + if(rank<tn){ + fprintf(stderr, RCAT("\nConstraints matrix in ", LMLEC_ELIM) "() is not of full row rank (i.e. %d < %d)!\n" + "Make sure that you do not specify redundant or inconsistent constraints.\n\n", rank, tn); + free(buf); + return LM_ERROR; + } + + /* compute the permuted inverse transpose of R */ + /* first, copy R from the upper triangular part of a to r. R is rank x rank */ + for(j=0; j<rank; ++j){ + for(i=0; i<=j; ++i) + r[i+j*rank]=a[i+j*tm]; + for(i=j+1; i<rank; ++i) + r[i+j*rank]=0.0; // lower part is zero + } + + /* compute the inverse */ + TRTRI("U", "N", (int *)&rank, r, (int *)&rank, &info); + /* error checking */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", TRTRI) " in ", LMLEC_ELIM) "()\n", -info); + } + else if(info>0){ + fprintf(stderr, RCAT(RCAT("A(%d, %d) is exactly zero for ", TRTRI) " (singular matrix) in ", LMLEC_ELIM) "()\n", info, info); + } + free(buf); + return LM_ERROR; + } + /* then, transpose r in place */ + for(i=0; i<rank; ++i) + for(j=i+1; j<rank; ++j){ + tmp=r[i+j*rank]; + k=j+i*rank; + r[i+j*rank]=r[k]; + r[k]=tmp; + } + + /* finally, permute R^-T using Y as intermediate storage */ + for(j=0; j<rank; ++j) + for(i=0, k=jpvt[j]-1; i<rank; ++i) + Y[i+k*rank]=r[i+j*rank]; + + for(i=0; i<rank*rank; ++i) // copy back to r + r[i]=Y[i]; + + /* resize a to be tm x tm, filling with zeroes */ + for(i=tm*tn; i<tm*tm; ++i) + a[i]=0.0; + + /* compute Q in a as the product of elementary reflectors. Q is tm x tm */ + ORGQR((int *)&tm, (int *)&tm, (int *)&mintmn, a, (int *)&tm, tau, work, &worksz, &info); + /* error checking */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT("LAPACK error: illegal value for argument %d of ", ORGQR) " in ", LMLEC_ELIM) "()\n", -info); + } + else if(info>0){ + fprintf(stderr, RCAT(RCAT("unknown LAPACK error (%d) for ", ORGQR) " in ", LMLEC_ELIM) "()\n", info); + } + free(buf); + return LM_ERROR; + } + + /* compute Y=Q_1*R^-T*P^T. Y is tm x rank */ + for(i=0; i<tm; ++i) + for(j=0; j<rank; ++j){ + for(k=0, tmp=0.0; k<rank; ++k) + tmp+=a[i+k*tm]*r[k+j*rank]; + Y[i*rank+j]=tmp; + } + + if(b && c){ + /* compute c=Y*b */ + for(i=0; i<tm; ++i){ + for(j=0, tmp=0.0; j<rank; ++j) + tmp+=Y[i*rank+j]*b[j]; + + c[i]=tmp; + } + } + + /* copy Q_2 into Z. Z is tm x (tm-rank) */ + for(j=0; j<tm-rank; ++j) + for(i=0, k=j+rank; i<tm; ++i) + Z[i*(tm-rank)+j]=a[i+k*tm]; + + free(buf); + + return rank; +} + +/* constrained measurements: given pp, compute the measurements at c + Z*pp */ +static void LMLEC_FUNC(LM_REAL *pp, LM_REAL *hx, int mm, int n, void *adata) +{ +struct LMLEC_DATA *data=(struct LMLEC_DATA *)adata; +int m; +register int i, j; +register LM_REAL sum; +LM_REAL *c, *Z, *p, *Zimm; + + m=mm+data->ncnstr; + c=data->c; + Z=data->Z; + p=data->p; + /* p=c + Z*pp */ + for(i=0; i<m; ++i){ + Zimm=Z+i*mm; + for(j=0, sum=c[i]; j<mm; ++j) + sum+=Zimm[j]*pp[j]; // sum+=Z[i*mm+j]*pp[j]; + p[i]=sum; + } + + (*(data->func))(p, hx, m, n, data->adata); +} + +/* constrained Jacobian: given pp, compute the Jacobian at c + Z*pp + * Using the chain rule, the Jacobian with respect to pp equals the + * product of the Jacobian with respect to p (at c + Z*pp) times Z + */ +static void LMLEC_JACF(LM_REAL *pp, LM_REAL *jacjac, int mm, int n, void *adata) +{ +struct LMLEC_DATA *data=(struct LMLEC_DATA *)adata; +int m; +register int i, j, l; +register LM_REAL sum, *aux1, *aux2; +LM_REAL *c, *Z, *p, *jac; + + m=mm+data->ncnstr; + c=data->c; + Z=data->Z; + p=data->p; + jac=data->jac; + /* p=c + Z*pp */ + for(i=0; i<m; ++i){ + aux1=Z+i*mm; + for(j=0, sum=c[i]; j<mm; ++j) + sum+=aux1[j]*pp[j]; // sum+=Z[i*mm+j]*pp[j]; + p[i]=sum; + } + + (*(data->jacf))(p, jac, m, n, data->adata); + + /* compute jac*Z in jacjac */ + if(n*m<=__BLOCKSZ__SQ){ // this is a small problem + /* This is the straightforward way to compute jac*Z. However, due to + * its noncontinuous memory access pattern, it incures many cache misses when + * applied to large minimization problems (i.e. problems involving a large + * number of free variables and measurements), in which jac is too large to + * fit in the L1 cache. For such problems, a cache-efficient blocking scheme + * is preferable. On the other hand, the straightforward algorithm is faster + * on small problems since in this case it avoids the overheads of blocking. + */ + + for(i=0; i<n; ++i){ + aux1=jac+i*m; + aux2=jacjac+i*mm; + for(j=0; j<mm; ++j){ + for(l=0, sum=0.0; l<m; ++l) + sum+=aux1[l]*Z[l*mm+j]; // sum+=jac[i*m+l]*Z[l*mm+j]; + + aux2[j]=sum; // jacjac[i*mm+j]=sum; + } + } + } + else{ // this is a large problem + /* Cache efficient computation of jac*Z based on blocking + */ +#define __MIN__(x, y) (((x)<=(y))? (x) : (y)) + register int jj, ll; + + for(jj=0; jj<mm; jj+=__BLOCKSZ__){ + for(i=0; i<n; ++i){ + aux1=jacjac+i*mm; + for(j=jj; j<__MIN__(jj+__BLOCKSZ__, mm); ++j) + aux1[j]=0.0; //jacjac[i*mm+j]=0.0; + } + + for(ll=0; ll<m; ll+=__BLOCKSZ__){ + for(i=0; i<n; ++i){ + aux1=jacjac+i*mm; aux2=jac+i*m; + for(j=jj; j<__MIN__(jj+__BLOCKSZ__, mm); ++j){ + sum=0.0; + for(l=ll; l<__MIN__(ll+__BLOCKSZ__, m); ++l) + sum+=aux2[l]*Z[l*mm+j]; //jac[i*m+l]*Z[l*mm+j]; + aux1[j]+=sum; //jacjac[i*mm+j]+=sum; + } + } + } + } + } +} +#undef __MIN__ + + +/* + * This function is similar to LEVMAR_DER except that the minimization + * is performed subject to the linear constraints A p=b, A is kxm, b kx1 + * + * This function requires an analytic Jacobian. In case the latter is unavailable, + * use LEVMAR_LEC_DIF() bellow + * + */ +int LEVMAR_LEC_DER( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), /* function to evaluate the Jacobian \part x / \part p */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + LM_REAL *A, /* I: constraints matrix, kxm */ + LM_REAL *b, /* I: right hand constraints vector, kx1 */ + int k, /* I: number of constraints (i.e. A's #rows) */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[4], /* I: minim. options [\mu, \epsilon1, \epsilon2, \epsilon3]. Respectively the scale factor for initial \mu, + * stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2. Set to NULL for defaults to be used + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_LEC_DER_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func & jacf. + * Set to NULL if not needed + */ +{ + struct LMLEC_DATA data; + LM_REAL *ptr, *Z, *pp, *p0, *Zimm; /* Z is mxmm */ + int mm, ret; + register int i, j; + register LM_REAL tmp; + LM_REAL locinfo[LM_INFO_SZ]; + + if(!jacf){ + fprintf(stderr, RCAT("No function specified for computing the Jacobian in ", LEVMAR_LEC_DER) + RCAT("().\nIf no such function is available, use ", LEVMAR_LEC_DIF) RCAT("() rather than ", LEVMAR_LEC_DER) "()\n"); + return LM_ERROR; + } + + mm=m-k; + + if(n<mm){ + fprintf(stderr, LCAT(LEVMAR_LEC_DER, "(): cannot solve a problem with fewer measurements + equality constraints [%d + %d] than unknowns [%d]\n"), n, k, m); + return LM_ERROR; + } + + ptr=(LM_REAL *)malloc((2*m + m*mm + n*m + mm)*sizeof(LM_REAL)); + if(!ptr){ + fprintf(stderr, LCAT(LEVMAR_LEC_DER, "(): memory allocation request failed\n")); + return LM_ERROR; + } + data.p=p; + p0=ptr; + data.c=p0+m; + data.Z=Z=data.c+m; + data.jac=data.Z+m*mm; + pp=data.jac+n*m; + data.ncnstr=k; + data.func=func; + data.jacf=jacf; + data.adata=adata; + + ret=LMLEC_ELIM(A, b, data.c, NULL, Z, k, m); // compute c, Z + if(ret==LM_ERROR){ + free(ptr); + return LM_ERROR; + } + + /* compute pp s.t. p = c + Z*pp or (Z^T Z)*pp=Z^T*(p-c) + * Due to orthogonality, Z^T Z = I and the last equation + * becomes pp=Z^T*(p-c). Also, save the starting p in p0 + */ + for(i=0; i<m; ++i){ + p0[i]=p[i]; + p[i]-=data.c[i]; + } + + /* Z^T*(p-c) */ + for(i=0; i<mm; ++i){ + for(j=0, tmp=0.0; j<m; ++j) + tmp+=Z[j*mm+i]*p[j]; + pp[i]=tmp; + } + + /* compute the p corresponding to pp (i.e. c + Z*pp) and compare with p0 */ + for(i=0; i<m; ++i){ + Zimm=Z+i*mm; + for(j=0, tmp=data.c[i]; j<mm; ++j) + tmp+=Zimm[j]*pp[j]; // tmp+=Z[i*mm+j]*pp[j]; + if(FABS(tmp-p0[i])>LM_CNST(1E-03)) + fprintf(stderr, RCAT("Warning: component %d of starting point not feasible in ", LEVMAR_LEC_DER) "()! [%.10g reset to %.10g]\n", + i, p0[i], tmp); + } + + if(!info) info=locinfo; /* make sure that LEVMAR_DER() is called with non-null info */ + /* note that covariance computation is not requested from LEVMAR_DER() */ + ret=LEVMAR_DER(LMLEC_FUNC, LMLEC_JACF, pp, x, mm, n, itmax, opts, info, work, NULL, (void *)&data); + + /* p=c + Z*pp */ + for(i=0; i<m; ++i){ + Zimm=Z+i*mm; + for(j=0, tmp=data.c[i]; j<mm; ++j) + tmp+=Zimm[j]*pp[j]; // tmp+=Z[i*mm+j]*pp[j]; + p[i]=tmp; + } + + /* compute the covariance from the Jacobian in data.jac */ + if(covar){ + LEVMAR_TRANS_MAT_MAT_MULT(data.jac, covar, n, m); /* covar = J^T J */ + LEVMAR_COVAR(covar, covar, info[1], m, n); + } + + free(ptr); + + return ret; +} + +/* Similar to the LEVMAR_LEC_DER() function above, except that the Jacobian is approximated + * with the aid of finite differences (forward or central, see the comment for the opts argument) + */ +int LEVMAR_LEC_DIF( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), /* functional relation describing measurements. A p \in R^m yields a \hat{x} \in R^n */ + LM_REAL *p, /* I/O: initial parameter estimates. On output has the estimated solution */ + LM_REAL *x, /* I: measurement vector. NULL implies a zero vector */ + int m, /* I: parameter vector dimension (i.e. #unknowns) */ + int n, /* I: measurement vector dimension */ + LM_REAL *A, /* I: constraints matrix, kxm */ + LM_REAL *b, /* I: right hand constraints vector, kx1 */ + int k, /* I: number of constraints (i.e. A's #rows) */ + int itmax, /* I: maximum number of iterations */ + LM_REAL opts[5], /* I: opts[0-3] = minim. options [\mu, \epsilon1, \epsilon2, \epsilon3, \delta]. Respectively the + * scale factor for initial \mu, stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2 and + * the step used in difference approximation to the Jacobian. Set to NULL for defaults to be used. + * If \delta<0, the Jacobian is approximated with central differences which are more accurate + * (but slower!) compared to the forward differences employed by default. + */ + LM_REAL info[LM_INFO_SZ], + /* O: information regarding the minimization. Set to NULL if don't care + * info[0]= ||e||_2 at initial p. + * info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, mu/max[J^T J]_ii ], all computed at estimated p. + * info[5]= # iterations, + * info[6]=reason for terminating: 1 - stopped by small gradient J^T e + * 2 - stopped by small Dp + * 3 - stopped by itmax + * 4 - singular matrix. Restart from current p with increased mu + * 5 - no further error reduction is possible. Restart with increased mu + * 6 - stopped by small ||e||_2 + * 7 - stopped by invalid (i.e. NaN or Inf) "func" values. This is a user error + * info[7]= # function evaluations + * info[8]= # Jacobian evaluations + * info[9]= # linear systems solved, i.e. # attempts for reducing error + */ + LM_REAL *work, /* working memory at least LM_LEC_DIF_WORKSZ() reals large, allocated if NULL */ + LM_REAL *covar, /* O: Covariance matrix corresponding to LS solution; mxm. Set to NULL if not needed. */ + void *adata) /* pointer to possibly additional data, passed uninterpreted to func. + * Set to NULL if not needed + */ +{ + struct LMLEC_DATA data; + LM_REAL *ptr, *Z, *pp, *p0, *Zimm; /* Z is mxmm */ + int mm, ret; + register int i, j; + register LM_REAL tmp; + LM_REAL locinfo[LM_INFO_SZ]; + + mm=m-k; + + if(n<mm){ + fprintf(stderr, LCAT(LEVMAR_LEC_DIF, "(): cannot solve a problem with fewer measurements + equality constraints [%d + %d] than unknowns [%d]\n"), n, k, m); + return LM_ERROR; + } + + ptr=(LM_REAL *)malloc((2*m + m*mm + mm)*sizeof(LM_REAL)); + if(!ptr){ + fprintf(stderr, LCAT(LEVMAR_LEC_DIF, "(): memory allocation request failed\n")); + return LM_ERROR; + } + data.p=p; + p0=ptr; + data.c=p0+m; + data.Z=Z=data.c+m; + data.jac=NULL; + pp=data.Z+m*mm; + data.ncnstr=k; + data.func=func; + data.jacf=NULL; + data.adata=adata; + + ret=LMLEC_ELIM(A, b, data.c, NULL, Z, k, m); // compute c, Z + if(ret==LM_ERROR){ + free(ptr); + return LM_ERROR; + } + + /* compute pp s.t. p = c + Z*pp or (Z^T Z)*pp=Z^T*(p-c) + * Due to orthogonality, Z^T Z = I and the last equation + * becomes pp=Z^T*(p-c). Also, save the starting p in p0 + */ + for(i=0; i<m; ++i){ + p0[i]=p[i]; + p[i]-=data.c[i]; + } + + /* Z^T*(p-c) */ + for(i=0; i<mm; ++i){ + for(j=0, tmp=0.0; j<m; ++j) + tmp+=Z[j*mm+i]*p[j]; + pp[i]=tmp; + } + + /* compute the p corresponding to pp (i.e. c + Z*pp) and compare with p0 */ + for(i=0; i<m; ++i){ + Zimm=Z+i*mm; + for(j=0, tmp=data.c[i]; j<mm; ++j) + tmp+=Zimm[j]*pp[j]; // tmp+=Z[i*mm+j]*pp[j]; + if(FABS(tmp-p0[i])>LM_CNST(1E-03)) + fprintf(stderr, RCAT("Warning: component %d of starting point not feasible in ", LEVMAR_LEC_DIF) "()! [%.10g reset to %.10g]\n", + i, p0[i], tmp); + } + + if(!info) info=locinfo; /* make sure that LEVMAR_DIF() is called with non-null info */ + /* note that covariance computation is not requested from LEVMAR_DIF() */ + ret=LEVMAR_DIF(LMLEC_FUNC, pp, x, mm, n, itmax, opts, info, work, NULL, (void *)&data); + + /* p=c + Z*pp */ + for(i=0; i<m; ++i){ + Zimm=Z+i*mm; + for(j=0, tmp=data.c[i]; j<mm; ++j) + tmp+=Zimm[j]*pp[j]; // tmp+=Z[i*mm+j]*pp[j]; + p[i]=tmp; + } + + /* compute the Jacobian with finite differences and use it to estimate the covariance */ + if(covar){ + LM_REAL *hx, *wrk, *jac; + + hx=(LM_REAL *)malloc((2*n+n*m)*sizeof(LM_REAL)); + if(!hx){ + fprintf(stderr, LCAT(LEVMAR_LEC_DIF, "(): memory allocation request failed\n")); + free(ptr); + return LM_ERROR; + } + + wrk=hx+n; + jac=wrk+n; + + (*func)(p, hx, m, n, adata); /* evaluate function at p */ + LEVMAR_FDIF_FORW_JAC_APPROX(func, p, hx, wrk, (LM_REAL)LM_DIFF_DELTA, jac, m, n, adata); /* compute the Jacobian at p */ + LEVMAR_TRANS_MAT_MAT_MULT(jac, covar, n, m); /* covar = J^T J */ + LEVMAR_COVAR(covar, covar, info[1], m, n); + free(hx); + } + + free(ptr); + + return ret; +} + +/* undefine all. THIS MUST REMAIN AT THE END OF THE FILE */ +#undef LMLEC_DATA +#undef LMLEC_ELIM +#undef LMLEC_FUNC +#undef LMLEC_JACF +#undef LEVMAR_FDIF_FORW_JAC_APPROX +#undef LEVMAR_COVAR +#undef LEVMAR_TRANS_MAT_MAT_MULT +#undef LEVMAR_LEC_DER +#undef LEVMAR_LEC_DIF +#undef LEVMAR_DER +#undef LEVMAR_DIF + +#undef GEQP3 +#undef ORGQR +#undef TRTRI diff --git a/sci-libs/levmar/levmar-2.5/matlab/CMakeLists.txt b/sci-libs/levmar/levmar-2.5/matlab/CMakeLists.txt new file mode 100644 index 000000000..3f529f641 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/CMakeLists.txt @@ -0,0 +1,58 @@ +# CMake file for levmar's MEX-file; see http://www.cmake.org
+# Requires FindMatlab.cmake included with cmake
+
+PROJECT(LEVMARMEX)
+#CMAKE_MINIMUM_REQUIRED(VERSION 1.4)
+
+INCLUDE("C:/Program Files/CMake 2.4/share/cmake-2.4/Modules/FindMatlab.cmake")
+
+# f2c is sometimes equivalent to libF77 & libI77; in that case, set HAVE_F2C to 0
+SET(HAVE_F2C 1 CACHE BOOL "Do we have f2c or F77/I77?" )
+
+# the directory where the lapack/blas/f2c libraries reside
+SET(LAPACKBLAS_DIR /usr/lib CACHE PATH "Path to lapack/blas libraries")
+
+# the directory where levmar.h resides
+SET(LM_H_DIR .. CACHE PATH "Path to levmar.h")
+# the directory where the levmar library resides
+SET(LEVMAR_DIR .. CACHE PATH "Path to levmar library")
+
+# actual names for the lapack/blas/f2c libraries
+SET(LAPACK_LIB lapack CACHE STRING "The name of the lapack library")
+SET(BLAS_LIB blas CACHE STRING "The name of the blas library")
+IF(HAVE_F2C)
+ SET(F2C_LIB f2c CACHE STRING "The name of the f2c library")
+ELSE(HAVE_F2C)
+ SET(F77_LIB libF77 CACHE STRING "The name of the F77 library")
+ SET(I77_LIB libI77 CACHE STRING "The name of the I77 library")
+ENDIF(HAVE_F2C)
+
+########################## NO CHANGES BEYOND THIS POINT ##########################
+
+INCLUDE_DIRECTORIES(${LM_H_DIR})
+LINK_DIRECTORIES(${LAPACKBLAS_DIR} ${LEVMAR_DIR})
+
+SET(SRC levmar.c)
+
+# naming conventions for the generated file's suffix
+IF(WIN32)
+ SET(SUFFIX ".mexw32")
+ELSE(WIN32)
+ SET(SUFFIX ".mexglx")
+ENDIF(WIN32)
+
+SET(OUTNAME "levmar${SUFFIX}")
+
+ADD_LIBRARY(${OUTNAME} MODULE ${SRC})
+
+IF(HAVE_F2C)
+ ADD_CUSTOM_COMMAND(OUTPUT ${OUTNAME}
+ DEPENDS ${SRC}
+ COMMAND mex
+ ARGS -O -I${LM_H_DIR} ${SRC} -I${MATLAB_INCLUDE_DIR} -L${LAPACKBLAS_DIR} -L${LEVMAR_DIR} -L${MATLAB_MEX_LIBRARY} -llevmar -l${LAPACK_LIB} -l${BLAS_LIB} -l${F2C_LIB} -output ${MATLAB_LIBRARIES} ${OUTNAME})
+ELSE(HAVE_F2C)
+ ADD_CUSTOM_COMMAND(OUTPUT ${OUTNAME}
+ DEPENDS ${SRC}
+ COMMAND mex
+ ARGS -O -I${LM_H_DIR} ${SRC} -I${MATLAB_INCLUDE_DIR} -L${LAPACKBLAS_DIR} -L${LEVMAR_DIR} -L${MATLAB_MEX_LIBRARY} -llevmar -l${LAPACK_LIB} -l${BLAS_LIB} -l${F77_LIB} -l${I77_LIB} ${MATLAB_LIBRARIES} -output ${OUTNAME})
+ENDIF(HAVE_F2C)
diff --git a/sci-libs/levmar/levmar-2.5/matlab/Makefile b/sci-libs/levmar/levmar-2.5/matlab/Makefile new file mode 100644 index 000000000..024dff3aa --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/Makefile @@ -0,0 +1,30 @@ +# +# Unix/Linux Makefile for MATLAB interface to levmar +# + +MEX=mex +MEXCFLAGS=-I.. -O #-g +# WHEN USING LAPACK, CHANGE THE NEXT TWO LINES TO WHERE YOUR COMPILED LAPACK/BLAS & F2C LIBS ARE! +LAPACKBLASLIBS_PATH=/usr/lib +F2CLIBS_PATH=/usr/local/lib + + +# I had to specify the absolute path to the libs, otherwise mex linked against their dynamic versions... +INTFACESRCS=levmar.c +LAPACKLIBS=$(LAPACKBLASLIBS_PATH)/liblapack.a $(LAPACKBLASLIBS_PATH)/libblas.a $(F2CLIBS_PATH)/libf2c.a + # On systems with a FORTRAN (not f2c'ed) version of LAPACK, libf2c.a is + # not necessary; on others, libf2c.a comes in two parts: libF77.a and libI77.a + +LIBS=$(LAPACKLIBS) + +dummy: $(INTFACESRCS) + $(MEX) $(MEXCFLAGS) $(INTFACESRCS) ../liblevmar.a $(LIBS) + +clean: + @rm -f levmar.mexglx + +depend: + makedepend -f Makefile $(INTFACESRCS) + +# DO NOT DELETE THIS LINE -- make depend depends on it. + diff --git a/sci-libs/levmar/levmar-2.5/matlab/Makefile.w32 b/sci-libs/levmar/levmar-2.5/matlab/Makefile.w32 new file mode 100644 index 000000000..758d8f90e --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/Makefile.w32 @@ -0,0 +1,26 @@ +# +# Windows Makefile for MATLAB interface to levmar +# + +MEX=mex +MEXCFLAGS=-I.. -O #-g +# WHEN USING LAPACK, CHANGE THE NEXT TWO LINES TO WHERE YOUR COMPILED LAPACK/BLAS & F2C LIBS ARE! +LAPACKBLASLIBS_PATH=C:\src\lib +F2CLIBS_PATH=$(LAPACKBLASLIBS_PATH) # define appropriately if not identical to LAPACKBLASLIBS_PATH + + +INTFACESRCS=levmar.c +LAPACKLIBS=$(LAPACKBLASLIBS_PATH)/clapack.lib $(LAPACKBLASLIBS_PATH)/blas.lib $(F2CLIBS_PATH)/libF77.lib $(F2CLIBS_PATH)/libI77.lib +LIBS=$(LAPACKLIBS) + +dummy: $(INTFACESRCS) + $(MEX) $(MEXCFLAGS) $(INTFACESRCS) ../levmar.lib $(LIBS) + +clean: + -del levmar.mexw32 + +depend: + makedepend -f Makefile $(INTFACESRCS) + +# DO NOT DELETE THIS LINE -- make depend depends on it. + diff --git a/sci-libs/levmar/levmar-2.5/matlab/README.txt b/sci-libs/levmar/levmar-2.5/matlab/README.txt new file mode 100644 index 000000000..cdc84c9a4 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/README.txt @@ -0,0 +1,35 @@ +This directory contains a matlab MEX interface to levmar. This interface
+has been tested with Matlab v. 6.5 R13 under linux and v. 7.4 R2007 under Windows.
+Users have also reported success with GNU Octave.
+
+FILES
+The following files are included:
+levmar.c: C MEX-file for levmar
+Makefile: UNIX makefile for compiling levmar.c using mex
+Makefile.w32: Windows makefile for compiling levmar.c using mex
+levmar.m: Documentation for the MEX interface
+lmdemo.m: Demonstration of using the MEX interface; run as matlab < lmdemo.m
+
+*.m: Matlab functions implementing various objective functions and their Jacobians.
+ For instance, meyer.m implements the objective function for Meyer's (reformulated)
+ problem and jacmeyer.m implements its Jacobian.
+
+
+
+COMPILING
+Use the provided Makefile or Makefile.w32, depending on your platform.
+Alternatively, levmar.c can be compiled from matlab's prompt with a
+command like
+
+mex -DHAVE_LAPACK -I.. -O -L<levmar library dir> -L<blas/lapack libraries dir> levmar.c -llevmar -lclapack -lblas -lf2c
+
+Make sure that you substitute the angle brackets with the correct paths to
+the levmar and the blas/lapack directories. Also, on certain systems,
+-lf2c should be changed to -llibF77 -llibI77
+If your mex compiler has not been configured, the following command should be run first:
+
+mex -setup
+
+
+TESTING
+After compiling, execute lmdemo.m with matlab < lmdemo.m
diff --git a/sci-libs/levmar/levmar-2.5/matlab/bt3.m b/sci-libs/levmar/levmar-2.5/matlab/bt3.m new file mode 100644 index 000000000..a8e4773e1 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/bt3.m @@ -0,0 +1,11 @@ +function x = bt3(p, adata) + n=5; + + t1=p(1)-p(2); + t2=p(2)+p(3)-2.0; + t3=p(4)-1.0; + t4=p(5)-1.0; + + for i=1:n + x(i)=t1*t1 + t2*t2 + t3*t3 + t4*t4; + end diff --git a/sci-libs/levmar/levmar-2.5/matlab/expfit.m b/sci-libs/levmar/levmar-2.5/matlab/expfit.m new file mode 100644 index 000000000..eeb37fcce --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/expfit.m @@ -0,0 +1,8 @@ +function x = expfit(p, data) + n=data; + +% data1, data2 are actually unused + + for i=1:n + x(i)=p(1)*exp(-p(2)*i) + p(3); + end diff --git a/sci-libs/levmar/levmar-2.5/matlab/hs01.m b/sci-libs/levmar/levmar-2.5/matlab/hs01.m new file mode 100644 index 000000000..c6912f205 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/hs01.m @@ -0,0 +1,6 @@ +function x = hs01(p) + n=2; + + t=p(1)*p(1); + x(1)=10.0*(p(2)-t); + x(2)=1.0-p(1); diff --git a/sci-libs/levmar/levmar-2.5/matlab/jacbt3.m b/sci-libs/levmar/levmar-2.5/matlab/jacbt3.m new file mode 100644 index 000000000..64383f272 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/jacbt3.m @@ -0,0 +1,13 @@ +function jac = jacbt3(p, adata) + n=5; + m=5; + + t1=p(1)-p(2); + t2=p(2)+p(3)-2.0; + t3=p(4)-1.0; + t4=p(5)-1.0; + + for i=1:n + jac(i, 1:m)=[2.0*t1, 2.0*(t2-t1), 2.0*t2, 2.0*t3, 2.0*t4]; + end + diff --git a/sci-libs/levmar/levmar-2.5/matlab/jacexpfit.m b/sci-libs/levmar/levmar-2.5/matlab/jacexpfit.m new file mode 100644 index 000000000..978c7b7ce --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/jacexpfit.m @@ -0,0 +1,7 @@ +function jac = jacexpfit(p, data) + n=data; + m=max(size(p)); + + for i=1:n + jac(i, 1:m)=[exp(-p(2)*i), -p(1)*i*exp(-p(2)*i), 1.0]; + end diff --git a/sci-libs/levmar/levmar-2.5/matlab/jachs01.m b/sci-libs/levmar/levmar-2.5/matlab/jachs01.m new file mode 100644 index 000000000..e08e5e33e --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/jachs01.m @@ -0,0 +1,5 @@ +function jac = jachs01(p) + m=2; + + jac(1, 1:m)=[-20.0*p(1), 10.0]; + jac(2, 1:m)=[-1.0, 0.0]; diff --git a/sci-libs/levmar/levmar-2.5/matlab/jacmeyer.m b/sci-libs/levmar/levmar-2.5/matlab/jacmeyer.m new file mode 100644 index 000000000..475d361a1 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/jacmeyer.m @@ -0,0 +1,10 @@ +function jac = jacmeyer(p, data1, data2) + n=16; + m=3; + + for i=1:n + ui=0.45+0.05*i; + tmp=exp(10.0*p(2)/(ui+p(3)) - 13.0); + + jac(i, 1:m)=[tmp, 10.0*p(1)*tmp/(ui+p(3)), -10.0*p(1)*p(2)*tmp/((ui+p(3))*(ui+p(3)))]; + end diff --git a/sci-libs/levmar/levmar-2.5/matlab/jacmodhs52.m b/sci-libs/levmar/levmar-2.5/matlab/jacmodhs52.m new file mode 100644 index 000000000..2be492490 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/jacmodhs52.m @@ -0,0 +1,7 @@ +function jac = jacmodhs52(p) + m=5; + + jac(1, 1:m)=[4.0, -1.0, 0.0, 0.0, 0.0]; + jac(2, 1:m)=[0.0, 1.0, 1.0, 0.0, 0.0]; + jac(3, 1:m)=[0.0, 0.0, 0.0, 1.0, 0.0]; + jac(4, 1:m)=[0.0, 0.0, 0.0, 0.0, 1.0]; diff --git a/sci-libs/levmar/levmar-2.5/matlab/jacmodhs76.m b/sci-libs/levmar/levmar-2.5/matlab/jacmodhs76.m new file mode 100644 index 000000000..5ffb3dc98 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/jacmodhs76.m @@ -0,0 +1,7 @@ +function jac = jacmodhs76(p) + m=4; + + jac(1, 1:m)=[1.0, 0.0, 0.0, 0.0]; + jac(2, 1:m)=[0.0, sqrt(0.5), 0.0, 0.0]; + jac(3, 1:m)=[0.0, 0.0, 1.0, 0.0]; + jac(4, 1:m)=[0.0, 0.0, 0.0, sqrt(0.5)]; diff --git a/sci-libs/levmar/levmar-2.5/matlab/jacosborne.m b/sci-libs/levmar/levmar-2.5/matlab/jacosborne.m new file mode 100644 index 000000000..22082c6ef --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/jacosborne.m @@ -0,0 +1,11 @@ +function jac = jacosborne(p) + n=33; + m=5; + + for i=1:n + t=10*(i-1); + tmp1=exp(-p(4)*t); + tmp2=exp(-p(5)*t); + + jac(i, 1:m)=[1.0, tmp1, tmp2, -p(2)*t*tmp1, -p(3)*t*tmp2]; + end diff --git a/sci-libs/levmar/levmar-2.5/matlab/levmar.c b/sci-libs/levmar/levmar-2.5/matlab/levmar.c new file mode 100644 index 000000000..b2fb6a216 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/levmar.c @@ -0,0 +1,677 @@ +/* //////////////////////////////////////////////////////////////////////////////// +// +// Matlab MEX file for the Levenberg - Marquardt minimization algorithm +// Copyright (C) 2007 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +//////////////////////////////////////////////////////////////////////////////// */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <math.h> +#include <string.h> +#include <ctype.h> + +#include <levmar.h> + +#include <mex.h> + +/** +#define DEBUG +**/ + +#ifndef HAVE_LAPACK +#ifdef _MSC_VER +#pragma message("LAPACK not available, certain functionalities cannot be compiled!") +#else +#warning LAPACK not available, certain functionalities cannot be compiled +#endif /* _MSC_VER */ +#endif /* HAVE_LAPACK */ + +#define __MAX__(A, B) ((A)>=(B)? (A) : (B)) + +#define MIN_UNCONSTRAINED 0 +#define MIN_CONSTRAINED_BC 1 +#define MIN_CONSTRAINED_LEC 2 +#define MIN_CONSTRAINED_BLEC 3 +#define MIN_CONSTRAINED_BLEIC 4 +#define MIN_CONSTRAINED_BLIC 5 +#define MIN_CONSTRAINED_LEIC 6 +#define MIN_CONSTRAINED_LIC 7 + +struct mexdata { + /* matlab names of the fitting function & its Jacobian */ + char *fname, *jacname; + + /* binary flags specifying if input p0 is a row or column vector */ + int isrow_p0; + + /* rhs args to be passed to matlab. rhs[0] is reserved for + * passing the parameter vector. If present, problem-specific + * data are passed in rhs[1], rhs[2], etc + */ + mxArray **rhs; + int nrhs; /* >= 1 */ +}; + +/* display printf-style error messages in matlab */ +static void matlabFmtdErrMsgTxt(char *fmt, ...) +{ +char buf[256]; +va_list args; + + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + + mexErrMsgTxt(buf); +} + +/* display printf-style warning messages in matlab */ +static void matlabFmtdWarnMsgTxt(char *fmt, ...) +{ +char buf[256]; +va_list args; + + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + + mexWarnMsgTxt(buf); +} + +static void func(double *p, double *hx, int m, int n, void *adata) +{ +mxArray *lhs[1]; +double *mp, *mx; +register int i; +struct mexdata *dat=(struct mexdata *)adata; + + /* prepare to call matlab */ + mp=mxGetPr(dat->rhs[0]); + for(i=0; i<m; ++i) + mp[i]=p[i]; + + /* invoke matlab */ + mexCallMATLAB(1, lhs, dat->nrhs, dat->rhs, dat->fname); + + /* copy back results & cleanup */ + mx=mxGetPr(lhs[0]); + for(i=0; i<n; ++i) + hx[i]=mx[i]; + + /* delete the matrix created by matlab */ + mxDestroyArray(lhs[0]); +} + +static void jacfunc(double *p, double *j, int m, int n, void *adata) +{ +mxArray *lhs[1]; +double *mp; +double *mj; +register int i, k; +struct mexdata *dat=(struct mexdata *)adata; + + /* prepare to call matlab */ + mp=mxGetPr(dat->rhs[0]); + for(i=0; i<m; ++i) + mp[i]=p[i]; + + /* invoke matlab */ + mexCallMATLAB(1, lhs, dat->nrhs, dat->rhs, dat->jacname); + + /* copy back results & cleanup. Note that the nxm Jacobian + * computed by matlab should be transposed so that + * levmar gets it in row major, as expected + */ + mj=mxGetPr(lhs[0]); + for(i=0; i<n; ++i) + for(k=0; k<m; ++k) + j[i*m+k]=mj[i+k*n]; + + /* delete the matrix created by matlab */ + mxDestroyArray(lhs[0]); +} + +/* matlab matrices are in column-major, this routine converts them to row major for levmar */ +static double *getTranspose(mxArray *Am) +{ +int m, n; +double *At, *A; +register int i, j; + + m=mxGetM(Am); + n=mxGetN(Am); + A=mxGetPr(Am); + At=mxMalloc(m*n*sizeof(double)); + + for(i=0; i<m; i++) + for(j=0; j<n; j++) + At[i*n+j]=A[i+j*m]; + + return At; +} + +/* check the supplied matlab function and its Jacobian. Returns 1 on error, 0 otherwise */ +static int checkFuncAndJacobian(double *p, int m, int n, int havejac, struct mexdata *dat) +{ +mxArray *lhs[1]; +register int i; +int ret=0; +double *mp; + + mexSetTrapFlag(1); /* handle errors in the MEX-file */ + + mp=mxGetPr(dat->rhs[0]); + for(i=0; i<m; ++i) + mp[i]=p[i]; + + /* attempt to call the supplied func */ + i=mexCallMATLAB(1, lhs, dat->nrhs, dat->rhs, dat->fname); + if(i){ + fprintf(stderr, "levmar: error calling '%s'.\n", dat->fname); + ret=1; + } + else if(!mxIsDouble(lhs[0]) || mxIsComplex(lhs[0]) || !(mxGetM(lhs[0])==1 || mxGetN(lhs[0])==1) || + __MAX__(mxGetM(lhs[0]), mxGetN(lhs[0]))!=n){ + fprintf(stderr, "levmar: '%s' should produce a real vector with %d elements (got %d).\n", + dat->fname, n, __MAX__(mxGetM(lhs[0]), mxGetN(lhs[0]))); + ret=1; + } + /* delete the matrix created by matlab */ + mxDestroyArray(lhs[0]); + + if(havejac){ + /* attempt to call the supplied jac */ + i=mexCallMATLAB(1, lhs, dat->nrhs, dat->rhs, dat->jacname); + if(i){ + fprintf(stderr, "levmar: error calling '%s'.\n", dat->jacname); + ret=1; + } + else if(!mxIsDouble(lhs[0]) || mxIsComplex(lhs[0]) || mxGetM(lhs[0])!=n || mxGetN(lhs[0])!=m){ + fprintf(stderr, "levmar: '%s' should produce a real %dx%d matrix (got %dx%d).\n", + dat->jacname, n, m, mxGetM(lhs[0]), mxGetN(lhs[0])); + ret=1; + } + else if(mxIsSparse(lhs[0])){ + fprintf(stderr, "levmar: '%s' should produce a real dense matrix (got a sparse one).\n", dat->jacname); + ret=1; + } + /* delete the matrix created by matlab */ + mxDestroyArray(lhs[0]); + } + + mexSetTrapFlag(0); /* on error terminate the MEX-file and return control to the MATLAB prompt */ + + return ret; +} + + +/* +[ret, p, info, covar]=levmar_der (f, j, p0, x, itmax, opts, 'unc' ...) +[ret, p, info, covar]=levmar_bc (f, j, p0, x, itmax, opts, 'bc', lb, ub, ...) +[ret, p, info, covar]=levmar_lec (f, j, p0, x, itmax, opts, 'lec', A, b, ...) +[ret, p, info, covar]=levmar_blec(f, j, p0, x, itmax, opts, 'blec', lb, ub, A, b, wghts, ...) + +[ret, p, info, covar]=levmar_bleic(f, j, p0, x, itmax, opts, 'bleic', lb, ub, A, b, C, d, ...) +[ret, p, info, covar]=levmar_blic (f, j, p0, x, itmax, opts, 'blic', lb, ub, C, d, ...) +[ret, p, info, covar]=levmar_leic (f, j, p0, x, itmax, opts, 'leic', A, b, C, d, ...) +[ret, p, info, covar]=levmar_lic (f, j, p0, x, itmax, opts, 'lic', C, d, ...) + +*/ + +void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *Prhs[]) +{ +register int i; +register double *pdbl; +mxArray **prhs=(mxArray **)&Prhs[0], *At, *Ct; +struct mexdata mdata; +int len, status; +double *p, *p0, *ret, *x; +int m, n, havejac, Arows, Crows, itmax, nopts, mintype, nextra; +double opts[LM_OPTS_SZ]={LM_INIT_MU, LM_STOP_THRESH, LM_STOP_THRESH, LM_STOP_THRESH, LM_DIFF_DELTA}; +double info[LM_INFO_SZ]; +double *lb=NULL, *ub=NULL, *A=NULL, *b=NULL, *wghts=NULL, *C=NULL, *d=NULL, *covar=NULL; + + /* parse input args; start by checking their number */ + if((nrhs<5)) + matlabFmtdErrMsgTxt("levmar: at least 5 input arguments required (got %d).", nrhs); + if(nlhs>4) + matlabFmtdErrMsgTxt("levmar: too many output arguments (max. 4, got %d).", nlhs); + else if(nlhs<2) + matlabFmtdErrMsgTxt("levmar: too few output arguments (min. 2, got %d).", nlhs); + + /* note that in order to accommodate optional args, prhs & nrhs are adjusted accordingly below */ + + /** func **/ + /* first argument must be a string , i.e. a char row vector */ + if(mxIsChar(prhs[0])!=1) + mexErrMsgTxt("levmar: first argument must be a string."); + if(mxGetM(prhs[0])!=1) + mexErrMsgTxt("levmar: first argument must be a string (i.e. char row vector)."); + /* store supplied name */ + len=mxGetN(prhs[0])+1; + mdata.fname=mxCalloc(len, sizeof(char)); + status=mxGetString(prhs[0], mdata.fname, len); + if(status!=0) + mexErrMsgTxt("levmar: not enough space. String is truncated."); + + /** jac (optional) **/ + /* check whether second argument is a string */ + if(mxIsChar(prhs[1])==1){ + if(mxGetM(prhs[1])!=1) + mexErrMsgTxt("levmar: second argument must be a string (i.e. row vector)."); + /* store supplied name */ + len=mxGetN(prhs[1])+1; + mdata.jacname=mxCalloc(len, sizeof(char)); + status=mxGetString(prhs[1], mdata.jacname, len); + if(status!=0) + mexErrMsgTxt("levmar: not enough space. String is truncated."); + havejac=1; + + ++prhs; + --nrhs; + } + else{ + mdata.jacname=NULL; + havejac=0; + } + +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: %s analytic Jacobian\n", havejac? "with" : "no"); +#endif /* DEBUG */ + +/* CHECK +if(!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || !(mxGetM(prhs[1])==1 && mxGetN(prhs[1])==1)) +*/ + + /** p0 **/ + /* the second required argument must be a real row or column vector */ + if(!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || !(mxGetM(prhs[1])==1 || mxGetN(prhs[1])==1)) + mexErrMsgTxt("levmar: p0 must be a real vector."); + p0=mxGetPr(prhs[1]); + /* determine if we have a row or column vector and retrieve its + * size, i.e. the number of parameters + */ + if(mxGetM(prhs[1])==1){ + m=mxGetN(prhs[1]); + mdata.isrow_p0=1; + } + else{ + m=mxGetM(prhs[1]); + mdata.isrow_p0=0; + } + /* copy input parameter vector to avoid destroying it */ + p=mxMalloc(m*sizeof(double)); + for(i=0; i<m; ++i) + p[i]=p0[i]; + + /** x **/ + /* the third required argument must be a real row or column vector */ + if(!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || !(mxGetM(prhs[2])==1 || mxGetN(prhs[2])==1)) + mexErrMsgTxt("levmar: x must be a real vector."); + x=mxGetPr(prhs[2]); + n=__MAX__(mxGetM(prhs[2]), mxGetN(prhs[2])); + + /** itmax **/ + /* the fourth required argument must be a scalar */ + if(!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxGetM(prhs[3])!=1 || mxGetN(prhs[3])!=1) + mexErrMsgTxt("levmar: itmax must be a scalar."); + itmax=(int)mxGetScalar(prhs[3]); + + /** opts **/ + /* the fifth required argument must be a real row or column vector */ + if(!mxIsDouble(prhs[4]) || mxIsComplex(prhs[4]) || (!(mxGetM(prhs[4])==1 || mxGetN(prhs[4])==1) && + !(mxGetM(prhs[4])==0 && mxGetN(prhs[4])==0))) + mexErrMsgTxt("levmar: opts must be a real vector."); + pdbl=mxGetPr(prhs[4]); + nopts=__MAX__(mxGetM(prhs[4]), mxGetN(prhs[4])); + if(nopts!=0){ /* if opts==[], nothing needs to be done and the defaults are used */ + if(nopts>LM_OPTS_SZ) + matlabFmtdErrMsgTxt("levmar: opts must have at most %d elements, got %d.", LM_OPTS_SZ, nopts); + else if(nopts<((havejac)? LM_OPTS_SZ-1 : LM_OPTS_SZ)) + matlabFmtdWarnMsgTxt("levmar: only the %d first elements of opts specified, remaining set to defaults.", nopts); + for(i=0; i<nopts; ++i) + opts[i]=pdbl[i]; + } +#ifdef DEBUG + else{ + fflush(stderr); + fprintf(stderr, "LEVMAR: empty options vector, using defaults\n"); + } +#endif /* DEBUG */ + + /** mintype (optional) **/ + /* check whether sixth argument is a string */ + if(nrhs>=6 && mxIsChar(prhs[5])==1 && mxGetM(prhs[5])==1){ + char *minhowto; + + /* examine supplied name */ + len=mxGetN(prhs[5])+1; + minhowto=mxCalloc(len, sizeof(char)); + status=mxGetString(prhs[5], minhowto, len); + if(status!=0) + mexErrMsgTxt("levmar: not enough space. String is truncated."); + + for(i=0; minhowto[i]; ++i) + minhowto[i]=tolower(minhowto[i]); + if(!strncmp(minhowto, "unc", 3)) mintype=MIN_UNCONSTRAINED; + else if(!strncmp(minhowto, "bc", 2)) mintype=MIN_CONSTRAINED_BC; + else if(!strncmp(minhowto, "lec", 3)) mintype=MIN_CONSTRAINED_LEC; + else if(!strncmp(minhowto, "blec", 4)) mintype=MIN_CONSTRAINED_BLEC; + else if(!strncmp(minhowto, "bleic", 5)) mintype=MIN_CONSTRAINED_BLEIC; + else if(!strncmp(minhowto, "blic", 4)) mintype=MIN_CONSTRAINED_BLIC; + else if(!strncmp(minhowto, "leic", 4)) mintype=MIN_CONSTRAINED_LEIC; + else if(!strncmp(minhowto, "lic", 3)) mintype=MIN_CONSTRAINED_BLIC; + else matlabFmtdErrMsgTxt("levmar: unknown minimization type '%s'.", minhowto); + + mxFree(minhowto); + + ++prhs; + --nrhs; + } + else + mintype=MIN_UNCONSTRAINED; + + if(mintype==MIN_UNCONSTRAINED) goto extraargs; + + /* arguments below this point are optional and their presence depends + * upon the minimization type determined above + */ + /** lb, ub **/ + if(nrhs>=7 && (mintype==MIN_CONSTRAINED_BC || mintype==MIN_CONSTRAINED_BLEC || mintype==MIN_CONSTRAINED_BLIC || mintype==MIN_CONSTRAINED_BLEIC)){ + /* check if the next two arguments are real row or column vectors */ + if(mxIsDouble(prhs[5]) && !mxIsComplex(prhs[5]) && (mxGetM(prhs[5])==1 || mxGetN(prhs[5])==1)){ + if(mxIsDouble(prhs[6]) && !mxIsComplex(prhs[6]) && (mxGetM(prhs[6])==1 || mxGetN(prhs[6])==1)){ + if((i=__MAX__(mxGetM(prhs[5]), mxGetN(prhs[5])))!=m) + matlabFmtdErrMsgTxt("levmar: lb must have %d elements, got %d.", m, i); + if((i=__MAX__(mxGetM(prhs[6]), mxGetN(prhs[6])))!=m) + matlabFmtdErrMsgTxt("levmar: ub must have %d elements, got %d.", m, i); + + lb=mxGetPr(prhs[5]); + ub=mxGetPr(prhs[6]); + + prhs+=2; + nrhs-=2; + } + } + } + + /** A, b **/ + if(nrhs>=7 && (mintype==MIN_CONSTRAINED_LEC || mintype==MIN_CONSTRAINED_BLEC || mintype==MIN_CONSTRAINED_LEIC || mintype==MIN_CONSTRAINED_BLEIC)){ + /* check if the next two arguments are a real matrix and a real row or column vector */ + if(mxIsDouble(prhs[5]) && !mxIsComplex(prhs[5]) && mxGetM(prhs[5])>=1 && mxGetN(prhs[5])>=1){ + if(mxIsDouble(prhs[6]) && !mxIsComplex(prhs[6]) && (mxGetM(prhs[6])==1 || mxGetN(prhs[6])==1)){ + if((i=mxGetN(prhs[5]))!=m) + matlabFmtdErrMsgTxt("levmar: A must have %d columns, got %d.", m, i); + if((i=__MAX__(mxGetM(prhs[6]), mxGetN(prhs[6])))!=(Arows=mxGetM(prhs[5]))) + matlabFmtdErrMsgTxt("levmar: b must have %d elements, got %d.", Arows, i); + + At=prhs[5]; + b=mxGetPr(prhs[6]); + A=getTranspose(At); + + prhs+=2; + nrhs-=2; + } + } + } + + /* wghts */ + /* check if we have a weights vector */ + if(nrhs>=6 && mintype==MIN_CONSTRAINED_BLEC){ /* only check if we have seen both box & linear constraints */ + if(mxIsDouble(prhs[5]) && !mxIsComplex(prhs[5]) && (mxGetM(prhs[5])==1 || mxGetN(prhs[5])==1)){ + if(__MAX__(mxGetM(prhs[5]), mxGetN(prhs[5]))==m){ + wghts=mxGetPr(prhs[5]); + + ++prhs; + --nrhs; + } + } + } + + /** C, d **/ + if(nrhs>=7 && (mintype==MIN_CONSTRAINED_BLEIC || mintype==MIN_CONSTRAINED_BLIC || mintype==MIN_CONSTRAINED_LEIC || mintype==MIN_CONSTRAINED_LIC)){ + /* check if the next two arguments are a real matrix and a real row or column vector */ + if(mxIsDouble(prhs[5]) && !mxIsComplex(prhs[5]) && mxGetM(prhs[5])>=1 && mxGetN(prhs[5])>=1){ + if(mxIsDouble(prhs[6]) && !mxIsComplex(prhs[6]) && (mxGetM(prhs[6])==1 || mxGetN(prhs[6])==1)){ + if((i=mxGetN(prhs[5]))!=m) + matlabFmtdErrMsgTxt("levmar: C must have %d columns, got %d.", m, i); + if((i=__MAX__(mxGetM(prhs[6]), mxGetN(prhs[6])))!=(Crows=mxGetM(prhs[5]))) + matlabFmtdErrMsgTxt("levmar: d must have %d elements, got %d.", Crows, i); + + Ct=prhs[5]; + d=mxGetPr(prhs[6]); + C=getTranspose(Ct); + + prhs+=2; + nrhs-=2; + } + } + } + + /* arguments below this point are assumed to be extra arguments passed + * to every invocation of the fitting function and its Jacobian + */ + +extraargs: + /* handle any extra args and allocate memory for + * passing the current parameter estimate to matlab + */ + nextra=nrhs-5; + mdata.nrhs=nextra+1; + mdata.rhs=(mxArray **)mxMalloc(mdata.nrhs*sizeof(mxArray *)); + for(i=0; i<nextra; ++i) + mdata.rhs[i+1]=(mxArray *)prhs[nrhs-nextra+i]; /* discard 'const' modifier */ +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: %d extra args\n", nextra); +#endif /* DEBUG */ + + if(mdata.isrow_p0){ /* row vector */ + mdata.rhs[0]=mxCreateDoubleMatrix(1, m, mxREAL); + /* + mxSetM(mdata.rhs[0], 1); + mxSetN(mdata.rhs[0], m); + */ + } + else{ /* column vector */ + mdata.rhs[0]=mxCreateDoubleMatrix(m, 1, mxREAL); + /* + mxSetM(mdata.rhs[0], m); + mxSetN(mdata.rhs[0], 1); + */ + } + + /* ensure that the supplied function & Jacobian are as expected */ + if(checkFuncAndJacobian(p, m, n, havejac, &mdata)){ + status=LM_ERROR; + goto cleanup; + } + + if(nlhs>3) /* covariance output required */ + covar=mxMalloc(m*m*sizeof(double)); + + /* invoke levmar */ + switch(mintype){ + case MIN_UNCONSTRAINED: /* no constraints */ + if(havejac) + status=dlevmar_der(func, jacfunc, p, x, m, n, itmax, opts, info, NULL, covar, (void *)&mdata); + else + status=dlevmar_dif(func, p, x, m, n, itmax, opts, info, NULL, covar, (void *)&mdata); +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: calling dlevmar_der()/dlevmar_dif()\n"); +#endif /* DEBUG */ + break; + case MIN_CONSTRAINED_BC: /* box constraints */ + if(havejac) + status=dlevmar_bc_der(func, jacfunc, p, x, m, n, lb, ub, itmax, opts, info, NULL, covar, (void *)&mdata); + else + status=dlevmar_bc_dif(func, p, x, m, n, lb, ub, itmax, opts, info, NULL, covar, (void *)&mdata); +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: calling dlevmar_bc_der()/dlevmar_bc_dif()\n"); +#endif /* DEBUG */ + break; + case MIN_CONSTRAINED_LEC: /* linear equation constraints */ +#ifdef HAVE_LAPACK + if(havejac) + status=dlevmar_lec_der(func, jacfunc, p, x, m, n, A, b, Arows, itmax, opts, info, NULL, covar, (void *)&mdata); + else + status=dlevmar_lec_dif(func, p, x, m, n, A, b, Arows, itmax, opts, info, NULL, covar, (void *)&mdata); +#else + mexErrMsgTxt("levmar: no linear constraints support, HAVE_LAPACK was not defined during MEX-file compilation."); +#endif /* HAVE_LAPACK */ + +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: calling dlevmar_lec_der()/dlevmar_lec_dif()\n"); +#endif /* DEBUG */ + break; + case MIN_CONSTRAINED_BLEC: /* box & linear equation constraints */ +#ifdef HAVE_LAPACK + if(havejac) + status=dlevmar_blec_der(func, jacfunc, p, x, m, n, lb, ub, A, b, Arows, wghts, itmax, opts, info, NULL, covar, (void *)&mdata); + else + status=dlevmar_blec_dif(func, p, x, m, n, lb, ub, A, b, Arows, wghts, itmax, opts, info, NULL, covar, (void *)&mdata); +#else + mexErrMsgTxt("levmar: no box & linear constraints support, HAVE_LAPACK was not defined during MEX-file compilation."); +#endif /* HAVE_LAPACK */ + +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: calling dlevmar_blec_der()/dlevmar_blec_dif()\n"); +#endif /* DEBUG */ + break; + case MIN_CONSTRAINED_BLEIC: /* box, linear equation & inequalities constraints */ +#ifdef HAVE_LAPACK + if(havejac) + status=dlevmar_bleic_der(func, jacfunc, p, x, m, n, lb, ub, A, b, Arows, C, d, Crows, itmax, opts, info, NULL, covar, (void *)&mdata); + else + status=dlevmar_bleic_dif(func, p, x, m, n, lb, ub, A, b, Arows, C, d, Crows, itmax, opts, info, NULL, covar, (void *)&mdata); +#else + mexErrMsgTxt("levmar: no box, linear equation & inequality constraints support, HAVE_LAPACK was not defined during MEX-file compilation."); +#endif /* HAVE_LAPACK */ + +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: calling dlevmar_bleic_der()/dlevmar_bleic_dif()\n"); +#endif /* DEBUG */ + break; + case MIN_CONSTRAINED_BLIC: /* box, linear inequalities constraints */ +#ifdef HAVE_LAPACK + if(havejac) + status=dlevmar_bleic_der(func, jacfunc, p, x, m, n, lb, ub, NULL, NULL, 0, C, d, Crows, itmax, opts, info, NULL, covar, (void *)&mdata); + else + status=dlevmar_bleic_dif(func, p, x, m, n, lb, ub, NULL, NULL, 0, C, d, Crows, itmax, opts, info, NULL, covar, (void *)&mdata); +#else + mexErrMsgTxt("levmar: no box & linear inequality constraints support, HAVE_LAPACK was not defined during MEX-file compilation."); +#endif /* HAVE_LAPACK */ + +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: calling dlevmar_blic_der()/dlevmar_blic_dif()\n"); +#endif /* DEBUG */ + break; + case MIN_CONSTRAINED_LEIC: /* linear equation & inequalities constraints */ +#ifdef HAVE_LAPACK + if(havejac) + status=dlevmar_bleic_der(func, jacfunc, p, x, m, n, NULL, NULL, A, b, Arows, C, d, Crows, itmax, opts, info, NULL, covar, (void *)&mdata); + else + status=dlevmar_bleic_dif(func, p, x, m, n, NULL, NULL, A, b, Arows, C, d, Crows, itmax, opts, info, NULL, covar, (void *)&mdata); +#else + mexErrMsgTxt("levmar: no linear equation & inequality constraints support, HAVE_LAPACK was not defined during MEX-file compilation."); +#endif /* HAVE_LAPACK */ + +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: calling dlevmar_leic_der()/dlevmar_leic_dif()\n"); +#endif /* DEBUG */ + break; + case MIN_CONSTRAINED_LIC: /* linear inequalities constraints */ +#ifdef HAVE_LAPACK + if(havejac) + status=dlevmar_bleic_der(func, jacfunc, p, x, m, n, NULL, NULL, NULL, NULL, 0, C, d, Crows, itmax, opts, info, NULL, covar, (void *)&mdata); + else + status=dlevmar_bleic_dif(func, p, x, m, n, NULL, NULL, NULL, NULL, 0, C, d, Crows, itmax, opts, info, NULL, covar, (void *)&mdata); +#else + mexErrMsgTxt("levmar: no linear equation & inequality constraints support, HAVE_LAPACK was not defined during MEX-file compilation."); +#endif /* HAVE_LAPACK */ + +#ifdef DEBUG + fflush(stderr); + fprintf(stderr, "LEVMAR: calling dlevmar_lic_der()/dlevmar_lic_dif()\n"); +#endif /* DEBUG */ + break; + default: + mexErrMsgTxt("levmar: unexpected internal error."); + } + +#ifdef DEBUG + fflush(stderr); + printf("LEVMAR: minimization returned %d in %g iter, reason %g\n\tSolution: ", status, info[5], info[6]); + for(i=0; i<m; ++i) + printf("%.7g ", p[i]); + printf("\n\n\tMinimization info:\n\t"); + for(i=0; i<LM_INFO_SZ; ++i) + printf("%g ", info[i]); + printf("\n"); +#endif /* DEBUG */ + + /* copy back return results */ + /** ret **/ + plhs[0]=mxCreateDoubleMatrix(1, 1, mxREAL); + ret=mxGetPr(plhs[0]); + ret[0]=(double)status; + + /** popt **/ + plhs[1]=(mdata.isrow_p0==1)? mxCreateDoubleMatrix(1, m, mxREAL) : mxCreateDoubleMatrix(m, 1, mxREAL); + pdbl=mxGetPr(plhs[1]); + for(i=0; i<m; ++i) + pdbl[i]=p[i]; + + /** info **/ + if(nlhs>2){ + plhs[2]=mxCreateDoubleMatrix(1, LM_INFO_SZ, mxREAL); + pdbl=mxGetPr(plhs[2]); + for(i=0; i<LM_INFO_SZ; ++i) + pdbl[i]=info[i]; + } + + /** covar **/ + if(nlhs>3){ + plhs[3]=mxCreateDoubleMatrix(m, m, mxREAL); + pdbl=mxGetPr(plhs[3]); + for(i=0; i<m*m; ++i) /* covariance matrices are symmetric, thus no need to transpose! */ + pdbl[i]=covar[i]; + } + +cleanup: + /* cleanup */ + mxDestroyArray(mdata.rhs[0]); + if(A) mxFree(A); + if(C) mxFree(C); + + mxFree(mdata.fname); + if(havejac) mxFree(mdata.jacname); + mxFree(p); + mxFree(mdata.rhs); + if(covar) mxFree(covar); + + if(status==LM_ERROR) + mexWarnMsgTxt("levmar: optimization returned with an error!"); +} diff --git a/sci-libs/levmar/levmar-2.5/matlab/levmar.m b/sci-libs/levmar/levmar-2.5/matlab/levmar.m new file mode 100644 index 000000000..3dfaad0c5 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/levmar.m @@ -0,0 +1,84 @@ +function [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, type) +% LEVMAR matlab MEX interface to the levmar non-linear least squares minimization +% library available from http://www.ics.forth.gr/~lourakis/levmar/ +% +% levmar can be used in any of the 8 following ways: +% [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, 'unc', ...) +% [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, 'bc', lb, ub, ...) +% [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, 'lec', A, b, ...) +% [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, 'blec', lb, ub, A, b, wghts, ...) +% +% [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, 'bleic', lb, ub, A, b, C, d, ...) +% [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, 'blic', lb, ub, C, d, ...) +% [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, 'leic', A, b, C, d, ...) +% [ret, popt, info, covar]=levmar(fname, jacname, p0, x, itmax, opts, 'lic', C, d, ...) +% +% +% The dots at the end denote any additional, problem specific data that are passed uniterpreted to +% all invocations of fname and jacname, see below for details. +% +% In the following, the word "vector" is meant to imply either a row or a column vector. +% +% required input arguments: +% - fname: String defining the name of a matlab function implementing the function to be minimized. +% fname will be called as fname(p, ...), where p denotes the parameter vector and the dots any +% additional data passed as extra arguments during the invocation of levmar (refer to Meyer's +% problem in lmdemo.m for an example). +% +% - p0: vector of doubles holding the initial parameters estimates. +% +% - x: vector of doubles holding the measurements vector. +% +% - itmax: maximum number of iterations. +% +% - opts: vector of doubles specifying the minimization parameters, as follows: +% opts(1) scale factor for the initial damping factor +% opts(2) stopping threshold for ||J^T e||_inf +% opts(3) stopping threshold for ||Dp||_2 +% opts(4) stopping threshold for ||e||_2 +% opts(5) step used in finite difference approximation to the Jacobian. +% If an empty vector (i.e. []) is specified, defaults are used. +% +% optional input arguments: +% - jacname: String defining the name of matlab function implementing the Jacobian of function fname. +% jacname will be called as jacname(p, ...) where p is again the parameter vector and the dots +% denote any additional data passed as extra arguments to the invocation of levmar. If omitted, +% the Jacobian is approximated with finite differences through repeated invocations of fname. +% +% - type: String defining the minimization type. It should be one of the following: +% 'unc' specifies unconstrained minimization. +% 'bc' specifies minimization subject to box constraints. +% 'lec' specifies minimization subject to linear equation constraints. +% 'blec' specifies minimization subject to box and linear equation constraints. +% 'bleic' specifies minimization subject to box, linear equation and inequality constraints. +% 'blic' specifies minimization subject to box and linear inequality constraints. +% 'leic' specifies minimization subject to linear equation and inequality constraints. +% 'lic' specifies minimization subject to linear inequality constraints. +% If omitted, a default of 'unc' is assumed. Depending on the minimization type, the MEX +% interface will invoke one of dlevmar_XXX, dlevmar_bc_XXX, dlevmar_lec_XXX, dlevmar_blec_XXX or dlevmar_bleic_XXX +% +% - lb, ub: vectors of doubles specifying lower and upper bounds for p, respectively +% +% - A, b: k x m matrix and k vector specifying linear equation constraints for p, i.e. A*p=b +% A should have full rank. +% +% - C, d: k x m matrix and k vector specifying linear inequality constraints for p, i.e. C*p>=d +% A should have full rank. +% +% - wghts: vector of doubles specifying the weights for the penalty terms corresponding to +% the box constraints, see lmblec_core.c for more details. If omitted and a 'blec' type +% minimization is to be carried out, default weights are used. +% +% +% output arguments +% - ret: return value of levmar, corresponding to the number of iterations if successful, -1 otherwise. +% +% - popt: estimated minimizer, i.e. minimized parameters vector. +% +% - info: optional array of doubles, which upon return provides information regarding the minimization. +% See lm_core.c for more details. +% +% - covar: optional covariance matrix corresponding to the estimated minimizer. +% + +error('levmar.m is used only for providing documentation to levmar; make sure that levmar.c has been compiled using mex'); diff --git a/sci-libs/levmar/levmar-2.5/matlab/lmdemo.m b/sci-libs/levmar/levmar-2.5/matlab/lmdemo.m new file mode 100644 index 000000000..d4cb96acc --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/lmdemo.m @@ -0,0 +1,140 @@ +% Demo program for levmar's MEX-file interface +% Performs minimization of several test problems + +format long; + +% Unconstrained minimization + +% fitting the exponential model x_i=p(1)*exp(-p(2)*i)+p(3) of expfit.c to noisy measurements obtained with (5.0 0.1 1.0) +p0=[1.0, 0.0, 0.0]; +x=[5.8728, 5.4948, 5.0081, 4.5929, 4.3574, 4.1198, 3.6843, 3.3642, 2.9742, 3.0237, 2.7002, 2.8781,... + 2.5144, 2.4432, 2.2894, 2.0938, 1.9265, 2.1271, 1.8387, 1.7791, 1.6686, 1.6232, 1.571, 1.6057,... + 1.3825, 1.5087, 1.3624, 1.4206, 1.2097, 1.3129, 1.131, 1.306, 1.2008, 1.3469, 1.1837, 1.2102,... + 0.96518, 1.2129, 1.2003, 1.0743]; + +options=[1E-03, 1E-15, 1E-15, 1E-20, 1E-06]; +% arg demonstrates additional data passing to expfit/jacexpfit +arg=[40]; + +[ret, popt, info]=levmar('expfit', 'jacexpfit', p0, x, 200, options, arg); +disp('Exponential model fitting (see also ../expfit.c)'); +popt + + +% Meyer's (reformulated) problem +p0=[8.85, 4.0, 2.5]; + +x=[]; +x(1:4)=[34.780, 28.610, 23.650, 19.630]; +x(5:8)=[16.370, 13.720, 11.540, 9.744]; +x(9:12)=[8.261, 7.030, 6.005, 5.147]; +x(13:16)=[4.427, 3.820, 3.307, 2.872]; + +options=[1E-03, 1E-15, 1E-15, 1E-20, 1E-06]; +% arg1, arg2 demonstrate additional dummy data passing to meyer/jacmeyer +arg1=[17]; +arg2=[27]; + +%[ret, popt, info]=levmar('meyer', 'jacmeyer', p0, x, 200, options, arg1, arg2); + +%[ret, popt, info, covar]=levmar('meyer', 'jacmeyer', p0, x, 200, options, arg1, arg2); +[ret, popt, info, covar]=levmar('meyer', p0, x, 200, options, 'unc', arg1, arg2); +disp('Meyer''s (reformulated) problem'); +popt + + +% Osborne's problem +p0=[0.5, 1.5, -1.0, 1.0E-2, 2.0E-2]; + +x=[8.44E-1, 9.08E-1, 9.32E-1, 9.36E-1, 9.25E-1, 9.08E-1, 8.81E-1,... +8.5E-1, 8.18E-1, 7.84E-1, 7.51E-1, 7.18E-1, 6.85E-1, 6.58E-1,... +6.28E-1, 6.03E-1, 5.8E-1, 5.58E-1, 5.38E-1, 5.22E-1, 5.06E-1,... +4.9E-1, 4.78E-1, 4.67E-1, 4.57E-1, 4.48E-1, 4.38E-1, 4.31E-1,... +4.24E-1, 4.2E-1, 4.14E-1, 4.11E-1, 4.06E-1]; + + +options=[1E-03, 1E-15, 1E-15, 1E-20, 1E-06]; + +[ret, popt, info, covar]=levmar('osborne', 'jacosborne', p0, x, 200, options); +%[ret, popt, info, covar]=levmar('osborne', p0, x, 200, options, 'unc'); +disp('Osborne''s problem'); +popt + + +% Linear constraints + +% Boggs-Tolle problem 3 +p0=[2.0, 2.0, 2.0, 2.0, 2.0]; +x=[0.0, 0.0, 0.0, 0.0, 0.0]; +options=[1E-03, 1E-15, 1E-15, 1E-20]; +adata=[]; + +A=[1.0, 3.0, 0.0, 0.0, 0.0; + 0.0, 0.0, 1.0, 1.0, -2.0; + 0.0, 1.0, 0.0, 0.0, -1.0]; +b=[0.0, 0.0, 0.0]'; + +[ret, popt, info, covar]=levmar('bt3', 'jacbt3', p0, x, 200, options, 'lec', A, b, adata); +disp('Boggs-Tolle problem 3'); +popt + + +% Box constraints + +% Hock-Schittkowski problem 01 +p0=[-2.0, 1.0]; +x=[0.0, 0.0]; +lb=[-realmax, -1.5]; +ub=[realmax, realmax]; +options=[1E-03, 1E-15, 1E-15, 1E-20]; + +[ret, popt, info, covar]=levmar('hs01', 'jachs01', p0, x, 200, options, 'bc', lb, ub); +disp('Hock-Schittkowski problem 01'); +popt + + +% Box and linear constraints + +% Hock-Schittkowski modified problem 52 (#1) +p0=[2.0, 2.0, 2.0, 2.0, 2.0]; +x=[0.0, 0.0, 0.0, 0.0]; +lb=[-0.09, 0.0, -realmax, -0.2, 0.0]; +ub=[realmax, 0.3, 0.25, 0.3, 0.3]; +A=[1.0, 3.0, 0.0, 0.0, 0.0; + 0.0, 0.0, 1.0, 1.0, -2.0; + 0.0, 1.0, 0.0, 0.0, -1.0]; +b=[0.0, 0.0, 0.0]'; +options=[1E-03, 1E-15, 1E-15, 1E-20]; + +[ret, popt, info, covar]=levmar('modhs52', 'jacmodhs52', p0, x, 200, options, 'blec', lb, ub, A, b); +disp('Hock-Schittkowski modified problem 52 (#1)'); +popt + +% Schittkowski modified problem 235 +p0=[-2.0, 3.0, 1.0]; +x=[0.0, 0.0]; +lb=[-realmax, 0.1, 0.7]; +ub=[realmax, 2.9, realmax]; +A=[1.0, 0.0, 1.0; + 0.0, 1.0, -4.0]; +b=[-1.0, 0.0]'; +options=[1E-03, 1E-15, 1E-15, 1E-20]; + +[ret, popt, info, covar]=levmar('mods235', p0, x, 200, options, 'blec', lb, ub, A, b); +disp('Hock-Schittkowski modified problem 235'); +popt + +% Box, linear equation & inequality constraints +p0=[0.5, 0.5, 0.5, 0.5]; +x=[0.0, 0.0, 0.0, 0.0]; +lb=[0.0, 0.0, 0.0, 0.0]; +ub=[realmax, realmax, realmax, realmax]; +A=[0.0, 1.0, 4.0, 0.0]; +b=[1.5]'; +C=[-1.0, -2.0, -1.0, -1.0; + -3.0, -1.0, -2.0, 1.0]; +d=[-5.0, -0.4]'; + +[ret, popt, info, covar]=levmar('modhs76', 'jacmodhs76', p0, x, 200, options, 'bleic', lb, ub, A, b, C, d); +disp('Hock-Schittkowski modified problem 76'); +popt diff --git a/sci-libs/levmar/levmar-2.5/matlab/meyer.m b/sci-libs/levmar/levmar-2.5/matlab/meyer.m new file mode 100644 index 000000000..3d6b7c989 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/meyer.m @@ -0,0 +1,9 @@ +function x = meyer(p, data1, data2) + n=16; + +% data1, data2 are actually unused + + for i=1:n + ui=0.45+0.05*i; + x(i)=p(1)*exp(10.0*p(2)/(ui+p(3)) - 13.0); + end diff --git a/sci-libs/levmar/levmar-2.5/matlab/modhs52.m b/sci-libs/levmar/levmar-2.5/matlab/modhs52.m new file mode 100644 index 000000000..db23006ee --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/modhs52.m @@ -0,0 +1,7 @@ +function x = modhs52(p) + n=4; + + x(1)=4.0*p(1)-p(2); + x(2)=p(2)+p(3)-2.0; + x(3)=p(4)-1.0; + x(4)=p(5)-1.0; diff --git a/sci-libs/levmar/levmar-2.5/matlab/modhs76.m b/sci-libs/levmar/levmar-2.5/matlab/modhs76.m new file mode 100644 index 000000000..1e4958326 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/modhs76.m @@ -0,0 +1,7 @@ +function x = modhs76(p) + + x(1)=p(1); + x(2)=sqrt(0.5)*p(2); + x(3)=p(3); + x(4)=sqrt(0.5)*p(4); + diff --git a/sci-libs/levmar/levmar-2.5/matlab/mods235.m b/sci-libs/levmar/levmar-2.5/matlab/mods235.m new file mode 100644 index 000000000..abef5eb82 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/mods235.m @@ -0,0 +1,5 @@ +function x = mods235(p) + n=2; + + x(1)=0.1*(p(1)-1.0); + x(2)=p(2)-p(1)*p(1); diff --git a/sci-libs/levmar/levmar-2.5/matlab/osborne.m b/sci-libs/levmar/levmar-2.5/matlab/osborne.m new file mode 100644 index 000000000..2d15373ee --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/matlab/osborne.m @@ -0,0 +1,7 @@ +function x = osborne(p) + n=33; + + for i=1:n + t=10*(i-1); + x(i)=p(1) + p(2)*exp(-p(4)*t) + p(3)*exp(-p(5)*t); + end diff --git a/sci-libs/levmar/levmar-2.5/misc.c b/sci-libs/levmar/levmar-2.5/misc.c new file mode 100644 index 000000000..1c6e99649 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/misc.c @@ -0,0 +1,70 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004-05 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +/******************************************************************************** + * Miscelaneous functions for Levenberg-Marquardt nonlinear minimization. The + * same core code is used with appropriate #defines to derive single and double + * precision versions, see also misc_core.c + ********************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <float.h> + +#include "levmar.h" +#include "misc.h" + +#if !defined(LM_DBL_PREC) && !defined(LM_SNGL_PREC) +#error At least one of LM_DBL_PREC, LM_SNGL_PREC should be defined! +#endif + +#ifdef LM_SNGL_PREC +/* single precision (float) definitions */ +#define LM_REAL float +#define LM_PREFIX s + +#define LM_REAL_EPSILON FLT_EPSILON +#define __SUBCNST(x) x##F +#define LM_CNST(x) __SUBCNST(x) // force substitution + +#include "misc_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_EPSILON +#undef __SUBCNST +#undef LM_CNST +#endif /* LM_SNGL_PREC */ + +#ifdef LM_DBL_PREC +/* double precision definitions */ +#define LM_REAL double +#define LM_PREFIX d + +#define LM_REAL_EPSILON DBL_EPSILON +#define LM_CNST(x) (x) + +#include "misc_core.c" // read in core code + +#undef LM_REAL +#undef LM_PREFIX +#undef LM_REAL_EPSILON +#undef LM_CNST +#endif /* LM_DBL_PREC */ diff --git a/sci-libs/levmar/levmar-2.5/misc.h b/sci-libs/levmar/levmar-2.5/misc.h new file mode 100644 index 000000000..e32f18de2 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/misc.h @@ -0,0 +1,106 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#ifndef _MISC_H_ +#define _MISC_H_ + +/* common suffix for LAPACK subroutines. Define empty in case of no prefix. */ +#define LM_LAPACK_SUFFIX _ +//#define LM_LAPACK_SUFFIX // define empty + +/* common prefix for BLAS subroutines. Leave undefined in case of no prefix. + * You might also need to modify LM_BLAS_PREFIX below + */ +/* f2c'd BLAS */ +//#define LM_BLAS_PREFIX f2c_ +/* C BLAS */ +//#define LM_BLAS_PREFIX cblas_ + +/* common suffix for BLAS subroutines */ +//#define LM_BLAS_SUFFIX // define empty if a f2c_ or cblas_ prefix was defined for LM_BLAS_PREFIX above +#define LM_BLAS_SUFFIX _ // use this in case of no BLAS prefix + + +#define LCAT_(a, b) #a b +#define LCAT(a, b) LCAT_(a, b) // force substitution +#define RCAT_(a, b) a #b +#define RCAT(a, b) RCAT_(a, b) // force substitution + +#define LM_MK_LAPACK_NAME(s) LM_ADD_PREFIX(LM_CAT_(s, LM_LAPACK_SUFFIX)) + + +#define __BLOCKSZ__ 32 /* block size for cache-friendly matrix-matrix multiply. It should be + * such that __BLOCKSZ__^2*sizeof(LM_REAL) is smaller than the CPU (L1) + * data cache size. Notice that a value of 32 when LM_REAL=double assumes + * an 8Kb L1 data cache (32*32*8=8K). This is a concervative choice since + * newer Pentium 4s have a L1 data cache of size 16K, capable of holding + * up to 45x45 double blocks. + */ +#define __BLOCKSZ__SQ (__BLOCKSZ__)*(__BLOCKSZ__) + +/* add a prefix in front of a token */ +#define LM_CAT__(a, b) a ## b +#define LM_CAT_(a, b) LM_CAT__(a, b) // force substitution +#define LM_ADD_PREFIX(s) LM_CAT_(LM_PREFIX, s) + +#ifdef __cplusplus +extern "C" { +#endif + +/* blocking-based matrix multiply */ +extern void slevmar_trans_mat_mat_mult(float *a, float *b, int n, int m); +extern void dlevmar_trans_mat_mat_mult(double *a, double *b, int n, int m); + +/* forward finite differences */ +extern void slevmar_fdif_forw_jac_approx(void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *hx, float *hxx, float delta, + float *jac, int m, int n, void *adata); +extern void dlevmar_fdif_forw_jac_approx(void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *hx, double *hxx, double delta, + double *jac, int m, int n, void *adata); + +/* central finite differences */ +extern void slevmar_fdif_cent_jac_approx(void (*func)(float *p, float *hx, int m, int n, void *adata), + float *p, float *hxm, float *hxp, float delta, + float *jac, int m, int n, void *adata); +extern void dlevmar_fdif_cent_jac_approx(void (*func)(double *p, double *hx, int m, int n, void *adata), + double *p, double *hxm, double *hxp, double delta, + double *jac, int m, int n, void *adata); + +/* e=x-y and ||e|| */ +extern float slevmar_L2nrmxmy(float *e, float *x, float *y, int n); +extern double dlevmar_L2nrmxmy(double *e, double *x, double *y, int n); + +/* covariance of LS fit */ +extern int slevmar_covar(float *JtJ, float *C, float sumsq, int m, int n); +extern int dlevmar_covar(double *JtJ, double *C, double sumsq, int m, int n); + +/* box constraints consistency check */ +extern int slevmar_box_check(float *lb, float *ub, int m); +extern int dlevmar_box_check(double *lb, double *ub, int m); + +/* Cholesky */ +extern int slevmar_chol(float *C, float *W, int m); +extern int dlevmar_chol(double *C, double *W, int m); + +#ifdef __cplusplus +} +#endif + +#endif /* _MISC_H_ */ diff --git a/sci-libs/levmar/levmar-2.5/misc.o b/sci-libs/levmar/levmar-2.5/misc.o Binary files differnew file mode 100644 index 000000000..776f03e96 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/misc.o diff --git a/sci-libs/levmar/levmar-2.5/misc_core.c b/sci-libs/levmar/levmar-2.5/misc_core.c new file mode 100644 index 000000000..f76c00e41 --- /dev/null +++ b/sci-libs/levmar/levmar-2.5/misc_core.c @@ -0,0 +1,813 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Levenberg - Marquardt non-linear minimization algorithm +// Copyright (C) 2004-05 Manolis Lourakis (lourakis at ics forth gr) +// Institute of Computer Science, Foundation for Research & Technology - Hellas +// Heraklion, Crete, Greece. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#ifndef LM_REAL // not included by misc.c +#error This file should not be compiled directly! +#endif + + +/* precision-specific definitions */ +#define LEVMAR_CHKJAC LM_ADD_PREFIX(levmar_chkjac) +#define LEVMAR_FDIF_FORW_JAC_APPROX LM_ADD_PREFIX(levmar_fdif_forw_jac_approx) +#define LEVMAR_FDIF_CENT_JAC_APPROX LM_ADD_PREFIX(levmar_fdif_cent_jac_approx) +#define LEVMAR_TRANS_MAT_MAT_MULT LM_ADD_PREFIX(levmar_trans_mat_mat_mult) +#define LEVMAR_COVAR LM_ADD_PREFIX(levmar_covar) +#define LEVMAR_STDDEV LM_ADD_PREFIX(levmar_stddev) +#define LEVMAR_CORCOEF LM_ADD_PREFIX(levmar_corcoef) +#define LEVMAR_R2 LM_ADD_PREFIX(levmar_R2) +#define LEVMAR_BOX_CHECK LM_ADD_PREFIX(levmar_box_check) +#define LEVMAR_L2NRMXMY LM_ADD_PREFIX(levmar_L2nrmxmy) + +#ifdef HAVE_LAPACK +#define LEVMAR_PSEUDOINVERSE LM_ADD_PREFIX(levmar_pseudoinverse) +static int LEVMAR_PSEUDOINVERSE(LM_REAL *A, LM_REAL *B, int m); + +/* BLAS matrix multiplication & LAPACK SVD routines */ +#ifdef LM_BLAS_PREFIX +#define GEMM LM_CAT_(LM_BLAS_PREFIX, LM_ADD_PREFIX(LM_CAT_(gemm, LM_BLAS_SUFFIX))) +#else +#define GEMM LM_ADD_PREFIX(LM_CAT_(gemm, LM_BLAS_SUFFIX)) +#endif +/* C := alpha*op( A )*op( B ) + beta*C */ +extern void GEMM(char *transa, char *transb, int *m, int *n, int *k, + LM_REAL *alpha, LM_REAL *a, int *lda, LM_REAL *b, int *ldb, LM_REAL *beta, LM_REAL *c, int *ldc); + +#define GESVD LM_MK_LAPACK_NAME(gesvd) +#define GESDD LM_MK_LAPACK_NAME(gesdd) +extern int GESVD(char *jobu, char *jobvt, int *m, int *n, LM_REAL *a, int *lda, LM_REAL *s, LM_REAL *u, int *ldu, + LM_REAL *vt, int *ldvt, LM_REAL *work, int *lwork, int *info); + +/* lapack 3.0 new SVD routine, faster than xgesvd() */ +extern int GESDD(char *jobz, int *m, int *n, LM_REAL *a, int *lda, LM_REAL *s, LM_REAL *u, int *ldu, LM_REAL *vt, int *ldvt, + LM_REAL *work, int *lwork, int *iwork, int *info); + +/* Cholesky decomposition */ +#define POTF2 LM_MK_LAPACK_NAME(potf2) +extern int POTF2(char *uplo, int *n, LM_REAL *a, int *lda, int *info); + +#define LEVMAR_CHOLESKY LM_ADD_PREFIX(levmar_chol) + +#else +#define LEVMAR_LUINVERSE LM_ADD_PREFIX(levmar_LUinverse_noLapack) + +static int LEVMAR_LUINVERSE(LM_REAL *A, LM_REAL *B, int m); +#endif /* HAVE_LAPACK */ + +/* blocked multiplication of the transpose of the nxm matrix a with itself (i.e. a^T a) + * using a block size of bsize. The product is returned in b. + * Since a^T a is symmetric, its computation can be sped up by computing only its + * upper triangular part and copying it to the lower part. + * + * More details on blocking can be found at + * http://www-2.cs.cmu.edu/afs/cs/academic/class/15213-f02/www/R07/section_a/Recitation07-SectionA.pdf + */ +void LEVMAR_TRANS_MAT_MAT_MULT(LM_REAL *a, LM_REAL *b, int n, int m) +{ +#ifdef HAVE_LAPACK /* use BLAS matrix multiply */ + +LM_REAL alpha=LM_CNST(1.0), beta=LM_CNST(0.0); + /* Fool BLAS to compute a^T*a avoiding transposing a: a is equivalent to a^T in column major, + * therefore BLAS computes a*a^T with a and a*a^T in column major, which is equivalent to + * computing a^T*a in row major! + */ + GEMM("N", "T", &m, &m, &n, &alpha, a, &m, a, &m, &beta, b, &m); + +#else /* no LAPACK, use blocking-based multiply */ + +register int i, j, k, jj, kk; +register LM_REAL sum, *bim, *akm; +const int bsize=__BLOCKSZ__; + +#define __MIN__(x, y) (((x)<=(y))? (x) : (y)) +#define __MAX__(x, y) (((x)>=(y))? (x) : (y)) + + /* compute upper triangular part using blocking */ + for(jj=0; jj<m; jj+=bsize){ + for(i=0; i<m; ++i){ + bim=b+i*m; + for(j=__MAX__(jj, i); j<__MIN__(jj+bsize, m); ++j) + bim[j]=0.0; //b[i*m+j]=0.0; + } + + for(kk=0; kk<n; kk+=bsize){ + for(i=0; i<m; ++i){ + bim=b+i*m; + for(j=__MAX__(jj, i); j<__MIN__(jj+bsize, m); ++j){ + sum=0.0; + for(k=kk; k<__MIN__(kk+bsize, n); ++k){ + akm=a+k*m; + sum+=akm[i]*akm[j]; //a[k*m+i]*a[k*m+j]; + } + bim[j]+=sum; //b[i*m+j]+=sum; + } + } + } + } + + /* copy upper triangular part to the lower one */ + for(i=0; i<m; ++i) + for(j=0; j<i; ++j) + b[i*m+j]=b[j*m+i]; + +#undef __MIN__ +#undef __MAX__ + +#endif /* HAVE_LAPACK */ +} + +/* forward finite difference approximation to the Jacobian of func */ +void LEVMAR_FDIF_FORW_JAC_APPROX( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + /* function to differentiate */ + LM_REAL *p, /* I: current parameter estimate, mx1 */ + LM_REAL *hx, /* I: func evaluated at p, i.e. hx=func(p), nx1 */ + LM_REAL *hxx, /* W/O: work array for evaluating func(p+delta), nx1 */ + LM_REAL delta, /* increment for computing the Jacobian */ + LM_REAL *jac, /* O: array for storing approximated Jacobian, nxm */ + int m, + int n, + void *adata) +{ +register int i, j; +LM_REAL tmp; +register LM_REAL d; + + for(j=0; j<m; ++j){ + /* determine d=max(1E-04*|p[j]|, delta), see HZ */ + d=LM_CNST(1E-04)*p[j]; // force evaluation + d=FABS(d); + if(d<delta) + d=delta; + + tmp=p[j]; + p[j]+=d; + + (*func)(p, hxx, m, n, adata); + + p[j]=tmp; /* restore */ + + d=LM_CNST(1.0)/d; /* invert so that divisions can be carried out faster as multiplications */ + for(i=0; i<n; ++i){ + jac[i*m+j]=(hxx[i]-hx[i])*d; + } + } +} + +/* central finite difference approximation to the Jacobian of func */ +void LEVMAR_FDIF_CENT_JAC_APPROX( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + /* function to differentiate */ + LM_REAL *p, /* I: current parameter estimate, mx1 */ + LM_REAL *hxm, /* W/O: work array for evaluating func(p-delta), nx1 */ + LM_REAL *hxp, /* W/O: work array for evaluating func(p+delta), nx1 */ + LM_REAL delta, /* increment for computing the Jacobian */ + LM_REAL *jac, /* O: array for storing approximated Jacobian, nxm */ + int m, + int n, + void *adata) +{ +register int i, j; +LM_REAL tmp; +register LM_REAL d; + + for(j=0; j<m; ++j){ + /* determine d=max(1E-04*|p[j]|, delta), see HZ */ + d=LM_CNST(1E-04)*p[j]; // force evaluation + d=FABS(d); + if(d<delta) + d=delta; + + tmp=p[j]; + p[j]-=d; + (*func)(p, hxm, m, n, adata); + + p[j]=tmp+d; + (*func)(p, hxp, m, n, adata); + p[j]=tmp; /* restore */ + + d=LM_CNST(0.5)/d; /* invert so that divisions can be carried out faster as multiplications */ + for(i=0; i<n; ++i){ + jac[i*m+j]=(hxp[i]-hxm[i])*d; + } + } +} + +/* + * Check the Jacobian of a n-valued nonlinear function in m variables + * evaluated at a point p, for consistency with the function itself. + * + * Based on fortran77 subroutine CHKDER by + * Burton S. Garbow, Kenneth E. Hillstrom, Jorge J. More + * Argonne National Laboratory. MINPACK project. March 1980. + * + * + * func points to a function from R^m --> R^n: Given a p in R^m it yields hx in R^n + * jacf points to a function implementing the Jacobian of func, whose correctness + * is to be tested. Given a p in R^m, jacf computes into the nxm matrix j the + * Jacobian of func at p. Note that row i of j corresponds to the gradient of + * the i-th component of func, evaluated at p. + * p is an input array of length m containing the point of evaluation. + * m is the number of variables + * n is the number of functions + * adata points to possible additional data and is passed uninterpreted + * to func, jacf. + * err is an array of length n. On output, err contains measures + * of correctness of the respective gradients. if there is + * no severe loss of significance, then if err[i] is 1.0 the + * i-th gradient is correct, while if err[i] is 0.0 the i-th + * gradient is incorrect. For values of err between 0.0 and 1.0, + * the categorization is less certain. In general, a value of + * err[i] greater than 0.5 indicates that the i-th gradient is + * probably correct, while a value of err[i] less than 0.5 + * indicates that the i-th gradient is probably incorrect. + * + * + * The function does not perform reliably if cancellation or + * rounding errors cause a severe loss of significance in the + * evaluation of a function. therefore, none of the components + * of p should be unusually small (in particular, zero) or any + * other value which may cause loss of significance. + */ + +void LEVMAR_CHKJAC( + void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + void (*jacf)(LM_REAL *p, LM_REAL *j, int m, int n, void *adata), + LM_REAL *p, int m, int n, void *adata, LM_REAL *err) +{ +LM_REAL factor=LM_CNST(100.0); +LM_REAL one=LM_CNST(1.0); +LM_REAL zero=LM_CNST(0.0); +LM_REAL *fvec, *fjac, *pp, *fvecp, *buf; + +register int i, j; +LM_REAL eps, epsf, temp, epsmch; +LM_REAL epslog; +int fvec_sz=n, fjac_sz=n*m, pp_sz=m, fvecp_sz=n; + + epsmch=LM_REAL_EPSILON; + eps=(LM_REAL)sqrt(epsmch); + + buf=(LM_REAL *)malloc((fvec_sz + fjac_sz + pp_sz + fvecp_sz)*sizeof(LM_REAL)); + if(!buf){ + fprintf(stderr, LCAT(LEVMAR_CHKJAC, "(): memory allocation request failed\n")); + exit(1); + } + fvec=buf; + fjac=fvec+fvec_sz; + pp=fjac+fjac_sz; + fvecp=pp+pp_sz; + + /* compute fvec=func(p) */ + (*func)(p, fvec, m, n, adata); + + /* compute the Jacobian at p */ + (*jacf)(p, fjac, m, n, adata); + + /* compute pp */ + for(j=0; j<m; ++j){ + temp=eps*FABS(p[j]); + if(temp==zero) temp=eps; + pp[j]=p[j]+temp; + } + + /* compute fvecp=func(pp) */ + (*func)(pp, fvecp, m, n, adata); + + epsf=factor*epsmch; + epslog=(LM_REAL)log10(eps); + + for(i=0; i<n; ++i) + err[i]=zero; + + for(j=0; j<m; ++j){ + temp=FABS(p[j]); + if(temp==zero) temp=one; + + for(i=0; i<n; ++i) + err[i]+=temp*fjac[i*m+j]; + } + + for(i=0; i<n; ++i){ + temp=one; + if(fvec[i]!=zero && fvecp[i]!=zero && FABS(fvecp[i]-fvec[i])>=epsf*FABS(fvec[i])) + temp=eps*FABS((fvecp[i]-fvec[i])/eps - err[i])/(FABS(fvec[i])+FABS(fvecp[i])); + err[i]=one; + if(temp>epsmch && temp<eps) + err[i]=((LM_REAL)log10(temp) - epslog)/epslog; + if(temp>=eps) err[i]=zero; + } + + free(buf); + + return; +} + +#ifdef HAVE_LAPACK +/* + * This function computes the pseudoinverse of a square matrix A + * into B using SVD. A and B can coincide + * + * The function returns 0 in case of error (e.g. A is singular), + * the rank of A if successful + * + * A, B are mxm + * + */ +static int LEVMAR_PSEUDOINVERSE(LM_REAL *A, LM_REAL *B, int m) +{ +LM_REAL *buf=NULL; +int buf_sz=0; +static LM_REAL eps=LM_CNST(-1.0); + +register int i, j; +LM_REAL *a, *u, *s, *vt, *work; +int a_sz, u_sz, s_sz, vt_sz, tot_sz; +LM_REAL thresh, one_over_denom; +int info, rank, worksz, *iwork, iworksz; + + /* calculate required memory size */ + worksz=5*m; // min worksize for GESVD + //worksz=m*(7*m+4); // min worksize for GESDD + iworksz=8*m; + a_sz=m*m; + u_sz=m*m; s_sz=m; vt_sz=m*m; + + tot_sz=(a_sz + u_sz + s_sz + vt_sz + worksz)*sizeof(LM_REAL) + iworksz*sizeof(int); /* should be arranged in that order for proper doubles alignment */ + + buf_sz=tot_sz; + buf=(LM_REAL *)malloc(buf_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", LEVMAR_PSEUDOINVERSE) "() failed!\n"); + return 0; /* error */ + } + + a=buf; + u=a+a_sz; + s=u+u_sz; + vt=s+s_sz; + work=vt+vt_sz; + iwork=(int *)(work+worksz); + + /* store A (column major!) into a */ + for(i=0; i<m; i++) + for(j=0; j<m; j++) + a[i+j*m]=A[i*m+j]; + + /* SVD decomposition of A */ + GESVD("A", "A", (int *)&m, (int *)&m, a, (int *)&m, s, u, (int *)&m, vt, (int *)&m, work, (int *)&worksz, &info); + //GESDD("A", (int *)&m, (int *)&m, a, (int *)&m, s, u, (int *)&m, vt, (int *)&m, work, (int *)&worksz, iwork, &info); + + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, RCAT(RCAT(RCAT("LAPACK error: illegal value for argument %d of ", GESVD), "/" GESDD) " in ", LEVMAR_PSEUDOINVERSE) "()\n", -info); + } + else{ + fprintf(stderr, RCAT("LAPACK error: dgesdd (dbdsdc)/dgesvd (dbdsqr) failed to converge in ", LEVMAR_PSEUDOINVERSE) "() [info=%d]\n", info); + } + free(buf); + return 0; + } + + if(eps<0.0){ + LM_REAL aux; + + /* compute machine epsilon */ + for(eps=LM_CNST(1.0); aux=eps+LM_CNST(1.0), aux-LM_CNST(1.0)>0.0; eps*=LM_CNST(0.5)) + ; + eps*=LM_CNST(2.0); + } + + /* compute the pseudoinverse in B */ + for(i=0; i<a_sz; i++) B[i]=0.0; /* initialize to zero */ + for(rank=0, thresh=eps*s[0]; rank<m && s[rank]>thresh; rank++){ + one_over_denom=LM_CNST(1.0)/s[rank]; + + for(j=0; j<m; j++) + for(i=0; i<m; i++) + B[i*m+j]+=vt[rank+i*m]*u[j+rank*m]*one_over_denom; + } + + free(buf); + + return rank; +} +#else // no LAPACK + +/* + * This function computes the inverse of A in B. A and B can coincide + * + * The function employs LAPACK-free LU decomposition of A to solve m linear + * systems A*B_i=I_i, where B_i and I_i are the i-th columns of B and I. + * + * A and B are mxm + * + * The function returns 0 in case of error, 1 if successful + * + */ +static int LEVMAR_LUINVERSE(LM_REAL *A, LM_REAL *B, int m) +{ +void *buf=NULL; +int buf_sz=0; + +register int i, j, k, l; +int *idx, maxi=-1, idx_sz, a_sz, x_sz, work_sz, tot_sz; +LM_REAL *a, *x, *work, max, sum, tmp; + + /* calculate required memory size */ + idx_sz=m; + a_sz=m*m; + x_sz=m; + work_sz=m; + tot_sz=(a_sz + x_sz + work_sz)*sizeof(LM_REAL) + idx_sz*sizeof(int); /* should be arranged in that order for proper doubles alignment */ + + buf_sz=tot_sz; + buf=(void *)malloc(tot_sz); + if(!buf){ + fprintf(stderr, RCAT("memory allocation in ", LEVMAR_LUINVERSE) "() failed!\n"); + return 0; /* error */ + } + + a=buf; + x=a+a_sz; + work=x+x_sz; + idx=(int *)(work+work_sz); + + /* avoid destroying A by copying it to a */ + for(i=0; i<a_sz; ++i) a[i]=A[i]; + + /* compute the LU decomposition of a row permutation of matrix a; the permutation itself is saved in idx[] */ + for(i=0; i<m; ++i){ + max=0.0; + for(j=0; j<m; ++j) + if((tmp=FABS(a[i*m+j]))>max) + max=tmp; + if(max==0.0){ + fprintf(stderr, RCAT("Singular matrix A in ", LEVMAR_LUINVERSE) "()!\n"); + free(buf); + + return 0; + } + work[i]=LM_CNST(1.0)/max; + } + + for(j=0; j<m; ++j){ + for(i=0; i<j; ++i){ + sum=a[i*m+j]; + for(k=0; k<i; ++k) + sum-=a[i*m+k]*a[k*m+j]; + a[i*m+j]=sum; + } + max=0.0; + for(i=j; i<m; ++i){ + sum=a[i*m+j]; + for(k=0; k<j; ++k) + sum-=a[i*m+k]*a[k*m+j]; + a[i*m+j]=sum; + if((tmp=work[i]*FABS(sum))>=max){ + max=tmp; + maxi=i; + } + } + if(j!=maxi){ + for(k=0; k<m; ++k){ + tmp=a[maxi*m+k]; + a[maxi*m+k]=a[j*m+k]; + a[j*m+k]=tmp; + } + work[maxi]=work[j]; + } + idx[j]=maxi; + if(a[j*m+j]==0.0) + a[j*m+j]=LM_REAL_EPSILON; + if(j!=m-1){ + tmp=LM_CNST(1.0)/(a[j*m+j]); + for(i=j+1; i<m; ++i) + a[i*m+j]*=tmp; + } + } + + /* The decomposition has now replaced a. Solve the m linear systems using + * forward and back substitution + */ + for(l=0; l<m; ++l){ + for(i=0; i<m; ++i) x[i]=0.0; + x[l]=LM_CNST(1.0); + + for(i=k=0; i<m; ++i){ + j=idx[i]; + sum=x[j]; + x[j]=x[i]; + if(k!=0) + for(j=k-1; j<i; ++j) + sum-=a[i*m+j]*x[j]; + else + if(sum!=0.0) + k=i+1; + x[i]=sum; + } + + for(i=m-1; i>=0; --i){ + sum=x[i]; + for(j=i+1; j<m; ++j) + sum-=a[i*m+j]*x[j]; + x[i]=sum/a[i*m+i]; + } + + for(i=0; i<m; ++i) + B[i*m+l]=x[i]; + } + + free(buf); + + return 1; +} +#endif /* HAVE_LAPACK */ + +/* + * This function computes in C the covariance matrix corresponding to a least + * squares fit. JtJ is the approximate Hessian at the solution (i.e. J^T*J, where + * J is the Jacobian at the solution), sumsq is the sum of squared residuals + * (i.e. goodnes of fit) at the solution, m is the number of parameters (variables) + * and n the number of observations. JtJ can coincide with C. + * + * if JtJ is of full rank, C is computed as sumsq/(n-m)*(JtJ)^-1 + * otherwise and if LAPACK is available, C=sumsq/(n-r)*(JtJ)^+ + * where r is JtJ's rank and ^+ denotes the pseudoinverse + * The diagonal of C is made up from the estimates of the variances + * of the estimated regression coefficients. + * See the documentation of routine E04YCF from the NAG fortran lib + * + * The function returns the rank of JtJ if successful, 0 on error + * + * A and C are mxm + * + */ +int LEVMAR_COVAR(LM_REAL *JtJ, LM_REAL *C, LM_REAL sumsq, int m, int n) +{ +register int i; +int rnk; +LM_REAL fact; + +#ifdef HAVE_LAPACK + rnk=LEVMAR_PSEUDOINVERSE(JtJ, C, m); + if(!rnk) return 0; +#else +#ifdef _MSC_VER +#pragma message("LAPACK not available, LU will be used for matrix inversion when computing the covariance; this might be unstable at times") +#else +#warning LAPACK not available, LU will be used for matrix inversion when computing the covariance; this might be unstable at times +#endif // _MSC_VER + + rnk=LEVMAR_LUINVERSE(JtJ, C, m); + if(!rnk) return 0; + + rnk=m; /* assume full rank */ +#endif /* HAVE_LAPACK */ + + fact=sumsq/(LM_REAL)(n-rnk); + for(i=0; i<m*m; ++i) + C[i]*=fact; + + return rnk; +} + +/* standard deviation of the best-fit parameter i. + * covar is the mxm covariance matrix of the best-fit parameters (see also LEVMAR_COVAR()). + * + * The standard deviation is computed as \sigma_{i} = \sqrt{C_{ii}} + */ +LM_REAL LEVMAR_STDDEV(LM_REAL *covar, int m, int i) +{ + return (LM_REAL)sqrt(covar[i*m+i]); +} + +/* Pearson's correlation coefficient of the best-fit parameters i and j. + * covar is the mxm covariance matrix of the best-fit parameters (see also LEVMAR_COVAR()). + * + * The coefficient is computed as \rho_{ij} = C_{ij} / sqrt(C_{ii} C_{jj}) + */ +LM_REAL LEVMAR_CORCOEF(LM_REAL *covar, int m, int i, int j) +{ + return (LM_REAL)(covar[i*m+j]/sqrt(covar[i*m+i]*covar[j*m+j])); +} + +/* coefficient of determination. + * see http://en.wikipedia.org/wiki/Coefficient_of_determination + */ +LM_REAL LEVMAR_R2(void (*func)(LM_REAL *p, LM_REAL *hx, int m, int n, void *adata), + LM_REAL *p, LM_REAL *x, int m, int n, void *adata) +{ +register int i; +register LM_REAL tmp; +LM_REAL SSerr, // sum of squared errors, i.e. residual sum of squares \sum_i (x_i-hx_i)^2 + SStot, // \sum_i (x_i-xavg)^2 + *hx, xavg; + + + if((hx=(LM_REAL *)malloc(n*sizeof(LM_REAL)))==NULL){ + fprintf(stderr, RCAT("memory allocation request failed in ", LEVMAR_R2) "()\n"); + exit(1); + } + + /* hx=f(p) */ + (*func)(p, hx, m, n, adata); + + for(i=0, tmp=0.0; i<n; ++i) + tmp+=x[i]; + xavg=tmp/(LM_REAL)n; + + for(i=0, SSerr=SStot=0.0; i<n; ++i){ + tmp=x[i]-hx[i]; + SSerr+=tmp*tmp; + + tmp=x[i]-xavg; + SStot+=tmp*tmp; + } + + free(hx); + + return LM_CNST(1.0) - SSerr/SStot; +} + +/* check box constraints for consistency */ +int LEVMAR_BOX_CHECK(LM_REAL *lb, LM_REAL *ub, int m) +{ +register int i; + + if(!lb || !ub) return 1; + + for(i=0; i<m; ++i) + if(lb[i]>ub[i]) return 0; + + return 1; +} + +#ifdef HAVE_LAPACK + +/* compute the Cholesky decomposition of C in W, s.t. C=W^t W and W is upper triangular */ +int LEVMAR_CHOLESKY(LM_REAL *C, LM_REAL *W, int m) +{ +register int i, j; +int info; + + /* copy weights array C to W so that LAPACK won't destroy it; + * C is assumed symmetric, hence no transposition is needed + */ + for(i=0, j=m*m; i<j; ++i) + W[i]=C[i]; + + /* Cholesky decomposition */ + POTF2("U", (int *)&m, W, (int *)&m, (int *)&info); + /* error treatment */ + if(info!=0){ + if(info<0){ + fprintf(stderr, "LAPACK error: illegal value for argument %d of dpotf2 in %s\n", -info, LCAT(LEVMAR_CHOLESKY, "()")); + } + else{ + fprintf(stderr, "LAPACK error: the leading minor of order %d is not positive definite,\n%s()\n", info, + RCAT("and the Cholesky factorization could not be completed in ", LEVMAR_CHOLESKY)); + } + return LM_ERROR; + } + + /* the decomposition is in the upper part of W (in column-major order!). + * copying it to the lower part and zeroing the upper transposes + * W in row-major order + */ + for(i=0; i<m; i++) + for(j=0; j<i; j++){ + W[i+j*m]=W[j+i*m]; + W[j+i*m]=0.0; + } + + return 0; +} +#endif /* HAVE_LAPACK */ + + +/* Compute e=x-y for two n-vectors x and y and return the squared L2 norm of e. + * e can coincide with either x or y; x can be NULL, in which case it is assumed + * to be equal to the zero vector. + * Uses loop unrolling and blocking to reduce bookkeeping overhead & pipeline + * stalls and increase instruction-level parallelism; see http://www.abarnett.demon.co.uk/tutorial.html + */ + +LM_REAL LEVMAR_L2NRMXMY(LM_REAL *e, LM_REAL *x, LM_REAL *y, int n) +{ +const int blocksize=8, bpwr=3; /* 8=2^3 */ +register int i; +int j1, j2, j3, j4, j5, j6, j7; +int blockn; +register LM_REAL sum0=0.0, sum1=0.0, sum2=0.0, sum3=0.0; + + /* n may not be divisible by blocksize, + * go as near as we can first, then tidy up. + */ + blockn = (n>>bpwr)<<bpwr; /* (n / blocksize) * blocksize; */ + + /* unroll the loop in blocks of `blocksize'; looping downwards gains some more speed */ + if(x){ + for(i=blockn-1; i>0; i-=blocksize){ + e[i ]=x[i ]-y[i ]; sum0+=e[i ]*e[i ]; + j1=i-1; e[j1]=x[j1]-y[j1]; sum1+=e[j1]*e[j1]; + j2=i-2; e[j2]=x[j2]-y[j2]; sum2+=e[j2]*e[j2]; + j3=i-3; e[j3]=x[j3]-y[j3]; sum3+=e[j3]*e[j3]; + j4=i-4; e[j4]=x[j4]-y[j4]; sum0+=e[j4]*e[j4]; + j5=i-5; e[j5]=x[j5]-y[j5]; sum1+=e[j5]*e[j5]; + j6=i-6; e[j6]=x[j6]-y[j6]; sum2+=e[j6]*e[j6]; + j7=i-7; e[j7]=x[j7]-y[j7]; sum3+=e[j7]*e[j7]; + } + + /* + * There may be some left to do. + * This could be done as a simple for() loop, + * but a switch is faster (and more interesting) + */ + + i=blockn; + if(i<n){ + /* Jump into the case at the place that will allow + * us to finish off the appropriate number of items. + */ + + switch(n - i){ + case 7 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i; + case 6 : e[i]=x[i]-y[i]; sum1+=e[i]*e[i]; ++i; + case 5 : e[i]=x[i]-y[i]; sum2+=e[i]*e[i]; ++i; + case 4 : e[i]=x[i]-y[i]; sum3+=e[i]*e[i]; ++i; + case 3 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i; + case 2 : e[i]=x[i]-y[i]; sum1+=e[i]*e[i]; ++i; + case 1 : e[i]=x[i]-y[i]; sum2+=e[i]*e[i]; //++i; + } + } + } + else{ /* x==0 */ + for(i=blockn-1; i>0; i-=blocksize){ + e[i ]=-y[i ]; sum0+=e[i ]*e[i ]; + j1=i-1; e[j1]=-y[j1]; sum1+=e[j1]*e[j1]; + j2=i-2; e[j2]=-y[j2]; sum2+=e[j2]*e[j2]; + j3=i-3; e[j3]=-y[j3]; sum3+=e[j3]*e[j3]; + j4=i-4; e[j4]=-y[j4]; sum0+=e[j4]*e[j4]; + j5=i-5; e[j5]=-y[j5]; sum1+=e[j5]*e[j5]; + j6=i-6; e[j6]=-y[j6]; sum2+=e[j6]*e[j6]; + j7=i-7; e[j7]=-y[j7]; sum3+=e[j7]*e[j7]; + } + + /* + * There may be some left to do. + * This could be done as a simple for() loop, + * but a switch is faster (and more interesting) + */ + + i=blockn; + if(i<n){ + /* Jump into the case at the place that will allow + * us to finish off the appropriate number of items. + */ + + switch(n - i){ + case 7 : e[i]=-y[i]; sum0+=e[i]*e[i]; ++i; + case 6 : e[i]=-y[i]; sum1+=e[i]*e[i]; ++i; + case 5 : e[i]=-y[i]; sum2+=e[i]*e[i]; ++i; + case 4 : e[i]=-y[i]; sum3+=e[i]*e[i]; ++i; + case 3 : e[i]=-y[i]; sum0+=e[i]*e[i]; ++i; + case 2 : e[i]=-y[i]; sum1+=e[i]*e[i]; ++i; + case 1 : e[i]=-y[i]; sum2+=e[i]*e[i]; //++i; + } + } + } + + return sum0+sum1+sum2+sum3; +} + +/* undefine everything. THIS MUST REMAIN AT THE END OF THE FILE */ +#undef LEVMAR_PSEUDOINVERSE +#undef LEVMAR_LUINVERSE +#undef LEVMAR_BOX_CHECK +#undef LEVMAR_CHOLESKY +#undef LEVMAR_COVAR +#undef LEVMAR_STDDEV +#undef LEVMAR_CORCOEF +#undef LEVMAR_R2 +#undef LEVMAR_CHKJAC +#undef LEVMAR_FDIF_FORW_JAC_APPROX +#undef LEVMAR_FDIF_CENT_JAC_APPROX +#undef LEVMAR_TRANS_MAT_MAT_MULT +#undef LEVMAR_L2NRMXMY diff --git a/sci-libs/lpsolve/Manifest b/sci-libs/lpsolve/Manifest new file mode 100644 index 000000000..aae2cd6e8 --- /dev/null +++ b/sci-libs/lpsolve/Manifest @@ -0,0 +1,2 @@ +DIST lp_solve_5.5.0.15_source.tar.gz 802881 RMD160 2a7d43c3c0627a49e03f9e1924932f0f0441e011 SHA1 e4b80684e8c1fe996b6bc90c01ce5cf022d9ccfe SHA256 ea1243e8aa2f0d52172dc0a90d1c2a8d2a4f696a39fc9cf07321810363d18985 +EBUILD lpsolve-5.5.0.15.ebuild 585 RMD160 3459ae31248a518a9c4caf022d17bc20abec7130 SHA1 dc6a3c0bd49f7d5bc6dd1bce534c3ec332eeed43 SHA256 6615a64fc57813d1a6141a21247318997df9b53e38a20c9f001579328805b62b diff --git a/sci-libs/lpsolve/lpsolve-5.5.0.15.ebuild b/sci-libs/lpsolve/lpsolve-5.5.0.15.ebuild new file mode 100644 index 000000000..0d0ad5f61 --- /dev/null +++ b/sci-libs/lpsolve/lpsolve-5.5.0.15.ebuild @@ -0,0 +1,53 @@ +# Copyright 1999-2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +EAPI=3 + +inherit eutils toolchain-funcs + +MYPN="lp_solve" +DESCRIPTION="Library for solving (mixed integer) linear programming problems" +HOMEPAGE="http://lpsolve.sourceforge.net/" +SRC_URI="mirror://sourceforge/${PN}/${PN}/${PV}/${MYPN}_${PV}_source.tar.gz" + +LICENSE="LPGL-2.1" +SLOT="0" +KEYWORDS="~amd64 ~x86" + +IUSE="static-libs examples" +DEPEND="" +RDEPEND="${DEPEND}" + +S="${WORKDIR}/${MYPN}_${PV}" + +PLIB=lpsolve55 + +src_prepare() { + LPSOLVE_ARCH=ux32 + LPSOLVE_ARCH=ux64 + sed -i \ + "s|^c=.*$$|c=$(tc-getCC)|g" \ + 's|^opts=.*$$|opts="${CFLAGS}"|g' \ + "s|-fpic|-fPIC|g" \ + "s|-ldl||g" \ + lp_solve/ccc ${PLIB}/ccc || die "sed failed" + +src_compile() { + for d in lp_solve ${PLIB}; do + pushd $d &> /dev/null + bash -x ccc + popd &> /dev/null + done +} + +src_install() { + dobin lp_solve/bin/${LPSOLVE_ARCH}/lp_solve || die + insinto /usr/include/lpsolve + doins declare.h fortify.h ini.h lp_*.h lpkit.h lpsolve.h ufortify.h yacc_read.h + dolib.so lib${PLIB}.so + if use static-libs; then + dolib.a lib${PLIB}.a || die + fi + dosym lib${PLIB}.so /usr/$(get_libdir)/lib${PLIB}.so.1 +} diff --git a/sci-libs/pagmo/Manifest b/sci-libs/pagmo/Manifest new file mode 100644 index 000000000..0a62ed106 --- /dev/null +++ b/sci-libs/pagmo/Manifest @@ -0,0 +1 @@ +EBUILD pagmo-9999.ebuild 887 RMD160 4c9f301299d663468dfc64b13fa76c1db1c559c3 SHA1 2c608caa9a20559de1c53fb7d5bd015707e3f443 SHA256 f836edbbdaaf3072b8a0b3e5247b2f02c1be4564425a379ead3804ff9643e841 diff --git a/sci-libs/pagmo/pagmo-9999.ebuild b/sci-libs/pagmo/pagmo-9999.ebuild new file mode 100644 index 000000000..8a898fc1e --- /dev/null +++ b/sci-libs/pagmo/pagmo-9999.ebuild @@ -0,0 +1,35 @@ +# Copyright 1999-2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +EAPI="2" +inherit cmake-utils git + +DESCRIPTION="Parallelization engine for optimization problems" +HOMEPAGE="http://pagmo.sourceforge.net/" +EGIT_REPO_URI="git://pagmo.git.sourceforge.net/gitroot/pagmo/pagmo" +SRC_URI="" + +LICENSE="GPL-3" +SLOT="0" +IUSE="gsl kepler mpi nlopt python test" +KEYWORDS="~amd64 ~x86" + +RDEPEND="dev-libs/boost[mpi?,python?] + gsl? ( sci-libs/gsl ) + nlopt? ( sci-libs/nlopt )" +DEPEND="${RDEPEND}" + +src_configure() { + mycmakeargs=( + -DENABLE_SNOPT=OFF + -DBUILD_MAIN=OFF + $(cmake-utils_use_build python PYGMO) + $(cmake-utils_use_enable gsl GSL) + $(cmake-utils_use_enable kepler KEPLERIAN_TOOLBOX) + $(cmake-utils_use_enable mpi MPI) + $(cmake-utils_use_enable nlopt NLOPT) + $(cmake-utils_use_enable test TESTS) + ) + cmake-utils_src_configure +} diff --git a/sci-libs/shogun/Manifest b/sci-libs/shogun/Manifest new file mode 100644 index 000000000..a8c260cf2 --- /dev/null +++ b/sci-libs/shogun/Manifest @@ -0,0 +1,4 @@ +AUX shogun-0.9.3-lapack.patch 583 RMD160 e1e70f045fa448601e8f43b6a7603030981dbf97 SHA1 abef54b23c9041ef65faa7e0324e89fed52749c4 SHA256 b86cbcb4f0003754393c6e55f501b0721d3479ee6bed48c876f1ee564b9a2825 +DIST shogun-0.9.3.tar.bz2 2853271 RMD160 9638a6b747a1177b048720b8999c60f33c7df5ef SHA1 d559dff3e11f777a23f00278d78d259ad896b829 SHA256 597d08155c7eff894dfae64dff8d4b37a5aba1da4a87b335881bddcb1a587526 +EBUILD shogun-0.9.3.ebuild 1625 RMD160 79dfc02d159c84b6cfcd749150779979e8273cdc SHA1 17df8f64dbe4270ead4158f54a6928fa05095a76 SHA256 42e0e74ebf89cff12b1c461264067ccf32350abd63899404add950aa8d290968 +MISC metadata.xml 1297 RMD160 dcc89a39c7d7a60ae5c128b8778cd314646e1417 SHA1 9ffe75693797b1eeb466223d6332cd589b32c624 SHA256 84677e9d55cab8bed4c8d99fdb917a6a31d21904526f20b95137716e6fed63e0 diff --git a/sci-libs/shogun/files/shogun-0.9.3-lapack.patch b/sci-libs/shogun/files/shogun-0.9.3-lapack.patch new file mode 100644 index 000000000..32e41aa1d --- /dev/null +++ b/sci-libs/shogun/files/shogun-0.9.3-lapack.patch @@ -0,0 +1,18 @@ +--- src/configure.orig 2010-06-01 19:49:30.000000000 +0100 ++++ src/configure 2010-06-01 19:52:05.000000000 +0100 +@@ -2421,13 +2421,13 @@ + } + EOF + echocheck "AMD ACML support" +- if cc_check -lacml -lcblas -lgfortran ++ if cc_check $(pkg-config --libs cblas lapack) + then + echores "yes" + HAVE_ACML='#define HAVE_ACML 1' + HAVE_LAPACK='#define HAVE_LAPACK 1' + DEFINES="$DEFINES -DHAVE_ACML -DHAVE_LAPACK" +- LINKFLAGS="$LINKFLAGS -lacml -lcblas -lgfortran" ++ LINKFLAGS="$LINKFLAGS $(pkg-config --libs cblas lapack)" + else + echores "no" + diff --git a/sci-libs/shogun/metadata.xml b/sci-libs/shogun/metadata.xml new file mode 100644 index 000000000..b5cdff054 --- /dev/null +++ b/sci-libs/shogun/metadata.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd"> +<pkgmetadata> +<herd>sci</herd> +<longdescription> + SHOGUN is a new machine learning toolbox with focus on large scale kernel + methods and especially on Support Vector Machines (SVM) with focus to + bioinformatics. It provides a generic SVM object interfacing to several + different SVM implementations. Each of the SVMs can be combined with a variety + of the many kernels implemented. It can deal with weighted linear combination + of a number of sub-kernels, each of which not necessarily working on the same + domain, where an optimal sub-kernel weighting can be learned using Multiple + Kernel Learning. Apart from SVM 2-class classification and regression + problems, a number of linear methods like Linear Discriminant Analysis (LDA), + Linear Programming Machine (LPM), (Kernel) Perceptrons and also algorithms to + train hidden markov models are implemented. The input feature-objects can be + dense, sparse or strings and of type int/short/double/char and can be + converted into different feature types. Chains of preprocessors (e.g. + substracting the mean) can be attached to each feature object allowing for + on-the-fly pre-processing. +</longdescription> +</pkgmetadata> diff --git a/sci-libs/shogun/shogun-0.9.3.ebuild b/sci-libs/shogun/shogun-0.9.3.ebuild new file mode 100644 index 000000000..321959d93 --- /dev/null +++ b/sci-libs/shogun/shogun-0.9.3.ebuild @@ -0,0 +1,64 @@ +# Copyright 1999-2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +EAPI=2 + +inherit eutils toolchain-funcs + +DESCRIPTION="Large Scale Machine Learning Toolbox" +HOMEPAGE="http://www.shogun-toolbox.org/" +SRC_URI="http://shogun-toolbox.org/archives/shogun/releases/${PV:0:3}/sources/${P}.tar.bz2" + +LICENSE="GPL-3" +SLOT="0" +KEYWORDS="~amd64 ~x86" + +IUSE="boost bz2 cplex doc glpk gzip hdf5 lapack lpsolve lzma lzo octave python R readline threads" + +RDEPEND="virtual/lapack + bzip2? ( app-arch/bzip2 ) + cplex? ( sci-mathematics/cplex-bin ) + glpk? ( sci-mathematics/glpk ) + gzip? ( app-arch/gzip ) + hdf5? ( sci-libs/hdf5 ) + glpk? ( sci-mathematics/lpsolve ) + lzma? ( app-arch/xz-utils ) + lzo? ( dev-libs/lzo ) + octave? ( sci-mathematics/octave ) + python? ( dev-python/numpy ) + R? ( dev-lang/R ) + readline? ( sys-libs/readline )" + +DEPEND="${RDEPEND} + dev-libs/boost + dev-util/pkgconfig + doc? ( app-doc/doxygen )" + +S="${WORKDIR}/${P}/src" + +src_configure() { + # not an autotools configure (based on mplayer one) + # disable svmlight based on debian comment + ./configure \ + --cc=$(tc-getCC) \ + --cxx=$(tc-getCXX) \ + --prefix=/usr \ + --datadir=/usr/share/${PN} \ + --mandir=/usr/share/man \ + --confdir=/etc \ + --libdir=/usr/$(get_libdir) \ + --disable-cpudetection \ + --disable-svm-light \ + $(use_enable doc doxygen) \ + $(use_enable boost boost-serialization) \ + $(use_enable glpk) \ + $(use_enable hdf5) \ + $(use_enable lapack) \ + $(use_enable readline) \ + $(use_enable threads hmm-parallel) +} + +src_install() { + emake DESTDIR="${D}" install || die "emake install failed" +} diff --git a/sci-physics/fedora b/sci-physics/fedora new file mode 160000 +Subproject c60383e16ea43ddc971ddd92317094f6f984979 diff --git a/sci-physics/healpix/Manifest b/sci-physics/healpix/Manifest new file mode 100644 index 000000000..f0584cc9f --- /dev/null +++ b/sci-physics/healpix/Manifest @@ -0,0 +1,3 @@ +DIST Healpix_2.15a_2010Jun18.tar.gz 28853992 RMD160 2961593e2f6f3a20e2120a52952db5f8698005ae SHA1 9ca4b56775b20350cdf9067a4a6b885caa643724 SHA256 9123239171c70ca0f021367ebb0ba2a6581de4251729d6d3e9b0edc3f4e500c9 +EBUILD healpix-2.15a.ebuild 715 RMD160 c69125bdd304a208b82341506527d7bd1b6cf02f SHA1 e8feefb1db1f28585fd9db72be820757229ad63d SHA256 36fe133f3af3789cb471b018cf2bdd1420b4512f9dcaa41495f60add614f971f +MISC metadata.xml 1238 RMD160 c82938e77f387291d6a1c3bf9b0d802f2878dd12 SHA1 b5738f14922c69e78cfb867acdc65eeea25977bf SHA256 2c13f98be5fa8bdaf21ac86bcd00bbeb8944df5615de0cfce35ae4c00ef4b4d5 diff --git a/sci-physics/healpix/healpix-2.15a.ebuild b/sci-physics/healpix/healpix-2.15a.ebuild new file mode 100644 index 000000000..8e043ce29 --- /dev/null +++ b/sci-physics/healpix/healpix-2.15a.ebuild @@ -0,0 +1,33 @@ +# Copyright 1999-2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +EAPI=2 +MYP="Healpix_${PV}" +MYPP="2010Jun18" + +DESCRIPTION="Hierarchical Equal Area isoLatitude Pixelization of a sphere." +HOMEPAGE="http://healpix.jpl.nasa.gov/index.shtml" +SRC_URI="mirror://sourceforge/${PN}/${MYP}/${MYP}_${MYPP}.tar.gz" + +LICENSE="GPL-2" +SLOT="0" +KEYWORDS="~amd86 ~x86" + +IUSE="fortran idl java" +DEPEND=">=sci-libs/cfitsio-3 + fortran? ( media-libs/gd )" +RDEPEND="${DEPEND}" + +S="${WORKDIR}/${MYP}" + +src_configure() { + econf \ + $(use_enable fortran) \ + $(use_enable idl) \ + $(use_enable java) +} + +src_install() { + emake DESTDIR="${D}" install || die "emake install failed" +} diff --git a/sci-physics/healpix/metadata.xml b/sci-physics/healpix/metadata.xml new file mode 100644 index 000000000..741a15b6a --- /dev/null +++ b/sci-physics/healpix/metadata.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd"> +<pkgmetadata> +<herd>sci-physics</herd> +<longdescription lang='en'> +XXXXX +</longdescription> +<use> + <flag name='blah'>Installs blah</flag> +</use> +</pkgmetadata> diff --git a/sci-physics/root-9999.ebuild b/sci-physics/root-9999.ebuild new file mode 100644 index 000000000..719702352 --- /dev/null +++ b/sci-physics/root-9999.ebuild @@ -0,0 +1,18 @@ +# Copyright 1999-2010 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: /var/cvsroot/gentoo-x86/sci-physics/root/root-5.26.00-r4.ebuild,v 1.4 2010/07/06 15:59:37 bicatali Exp $ + +EAPI=3 + +PYTHON_DEPEND="python? 2" +ESVN_REPO_URI="https://root.cern.ch/svn/root/trunk/" + +inherit versionator eutils qt4 subversion elisp-common fdo-mime python toolchain-funcs + +DOC_PV=$(get_major_version)_$(get_version_component_range 2) +ROOFIT_DOC_PV=2.91-33 +TMVA_DOC_PV=4 + +DESCRIPTION="C++ data analysis framework and interpreter from CERN" +HOMEPAGE="http://root.cern.ch/" +SRC_URI="" |