Compare commits
10 Commits
release_3.
...
release_3.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d800d8b9fd | ||
|
|
b08dee941f | ||
|
|
537da048a5 | ||
|
|
72226ef3c3 | ||
|
|
d20c4e1f29 | ||
|
|
9b8dbc1d6f | ||
|
|
7d64068fe2 | ||
|
|
3d496d2b28 | ||
|
|
73cc723049 | ||
|
|
ad3346df35 |
5
.gitattributes
vendored
@@ -1,5 +0,0 @@
|
||||
# Added this line to .gitattributes
|
||||
*.pbxproj -crlf -diff -merge
|
||||
src/translations/* linguist-documentation
|
||||
src/Resources/translations/* linguist-documentation
|
||||
test/* linguist-vendored
|
||||
4
.gitignore
vendored
@@ -13,7 +13,6 @@ Makefile
|
||||
*.prl
|
||||
*.swp
|
||||
*.user
|
||||
*.AppImage
|
||||
|
||||
# osx noise
|
||||
.DS_Store
|
||||
@@ -50,6 +49,3 @@ qwt/src/Makefile.Debug
|
||||
qwt/textengines/mathml/debug/
|
||||
qwt/textengines/mathml/release/
|
||||
build.pro.user
|
||||
build.pro.user.*
|
||||
doc/contrib/WASP Packet Protocol.pdf
|
||||
|
||||
|
||||
158
.travis.yml
@@ -1,60 +1,124 @@
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- /^[vV]\d+\.\d+(\.\d+)?(-\S*)?$/
|
||||
os:
|
||||
- osx
|
||||
- linux
|
||||
dist: xenial
|
||||
language: c++
|
||||
- master
|
||||
language:
|
||||
- objective-c
|
||||
compiler:
|
||||
- clang
|
||||
- gcc
|
||||
env:
|
||||
matrix:
|
||||
- BRANCH=master QT=qt4 QT_PATH=qt
|
||||
- BRANCH=master QT=qt5 QT_PATH=qt5 QT_VER=5.5.1
|
||||
global:
|
||||
- BRANCH=master
|
||||
- secure: iqYW7f3//ZkMVzeCEarYn0S0DqKjFU9juBh0KF6WTlUsKX902Jtsk7dFoJlNDYBf63HLgV+wW2Hc6MxI9sGiUkom0gY9/To/aeGIJFGEX2sLm/e0Ok3qN521FA0Q/OiCFsD0RC6J+yrHxzI+rf8Z1rujceUsz2KgsrfAjYYv+BY=
|
||||
- secure: C6f58PXwvvHeVOjCLbyDRjgyF0tk+bXjCNUDur8VJLhJEGNdnrt+rH+d3azYaX0n348ZnQZ14as4M4rHjN1A/3DRbdUdOS7PQZrkj/n363ck1tvYOG/tzYqkEcVoQyjIJ7ZoTuBXDQry/VJmLxQjM0glTa2upNFHMOMwqZNkF+I=
|
||||
- secure: mgf181ok4rJTHxAdSaeIb/OzUx7PQc6UjJYfhJQcZDjN1Nou+Um6OHpnooMHeEq0CdGuLSiv2xVmuQnRjI5MfG5re/a3l39hSzi9tPLWi1y7uOLCtQFooJw3H+LhiKZcgAtEnkTmEhUCEu5uZ7cp4lWhrQZWCR5mCDNtJrGHjiw=
|
||||
- secure: UkW6SpgeACb6enGZADTAtPPRe23Tffne/SRQoBsZ3kGbKzgv8kTCLY0G5N71B1xEGdLB/36piOa0yji2SMW1jbflxR7tdOsiNpfd9R5qYqWVZRulY0qite8ZCwU5dqgy87pp4ECC1hNSOTq17aa4Sc4G4egg9AOy8cntLBTtxJA=
|
||||
- secure: rb/MKPHEK8yOK5uYpXq2o+MQ7h65ft+c/xE9XmLFk1jLnK0oxe6+KeoSFlQ0n5UwLSsHrytMXVaUHex6EEt0TheXFlohi7BcNFWRWXr+wLucuaPjMnU49VErrSigjJ5VW/rcR7fORmnDwn5y06r3Rer9SC1hImCxf/pqF8hFZps=
|
||||
- secure: FXJzm98lwbc1R2eh9/CvVReVRHEPLDm+sCRjiV9HJgeCveIUauLasp9Skamz0c6OKLmkjLY8JERPT491ZgCG8YcQ9x1bM8HGbmRqnn9xSUxINIRzVmyTiLMBoT/ibHHeFjFpyiGfukOAkHwMNNsrSrbaekhiJuekaXo7iTwYQB0=
|
||||
- secure: K9+6ufffQ2a6dCTwVSI5ZtrGE+JSsj2ZeQwJ8Xk/y1yhSlbnW6Uptfs5oZ4XJ/BAQbuKbctnczP/jwDtGBG/Y0WOxn8m1DYBrkeLeToc0JZ8dn2lF6H6BEcB3VI3iPUtxAXWHNeSCk2qaC83z80Ydjc5GCxoAOwlw5/BEsRYI/s=
|
||||
- secure: n6khzr3bPnvh2lTE1NmcaPWRWkqLBUcXZL20EO0Q0HyhH2O22fwsKfnODCDtP1UEQxW+E0G3DkedSz/RmpMWbv1KpWKNNKl74wIdUE53nRIBLTULRoypCq1tDZzQqkoogbXu3pWrb23YoUuM2p5zxM9l3TiNbcrdZeQ+PInVL6o=
|
||||
- secure: mJEJUwt1jhx5qx5h5C8gnYJyaUb5fItSJGU/Tgp0IMCPCUSXtjHf9QTKITAKvg/hY8zggSwo+Vg2VzdMypdxDKqAeH/xAjq/o1+4linWphSjWpUGTqF/B8sR37WtduXtetamX+ctsbG4I77Xw+7y8OPPoCPxDct9hWxfCw6eBrI=
|
||||
- secure: BX7lVlMs6c6qOCMc7Eu0s3G7/es+3h/EGElS/4wjw4/nxp91bd0RbkOmGmgEKOFw5XRFl8gV0kEzmFhrJTdGfTnWHmdsGjSo2VzwePgW/PbDdQrTVD2rYBmmPoeTXf3S3v0P7d3pBrpuxAqkmj7bFkpnvviCN9erC4bUhSg6jEI=
|
||||
- secure: AC6pelym0R1Pq6GgcAy7E/BmlNWrni+41P8V8PMtjBKEtiyJ3tQa+KCn5g0UfGzktRHk+0l+szz3Wdy+bzl+ifiJVhXXs95B9F9El03p33UQLFPi+wFlivddP5DLc+4vIsDUiaNX97p89vo1/R96OLjpF1mUzfOMKYmAiMGP/Co=
|
||||
- secure: QXyyyutxesRVae/WFi75dxlNw2qTyWc6WQ3vmRER9BFhlrNtt8lmE+M6ghMDOhxigzSXWxHcG9r66BLOw4DML7AXg4f3NIAIYuYQrpfsWQ1TKGC/vVvIPDlNBKL62zSOoNowjup1HfZpB575wl4JkmHjoMPDfQ4yM/JVR+MKmIM=
|
||||
- secure: lefpL3SB8yN0Vn1R2e0hAFpJgFnu4rJ/vn7DucSKQvJ61K7t3LrUHFXElDQGFZMbytx4OJDeYsJtrccrIHjcsSAEIyW8TTcwn5/JopnyeQ3ukV5AYpEoQusym09XKru9E7awqvnZrXz6mdoIum5JP7+H116mPN4vsvz5nkNWxeg=
|
||||
- secure: RAGPq/thN9x6STNueDbeJWGUN75OLNcUVUkLHI/eZwPrkMaiQPfxIaJ+8eO2PszDcBojw3eYVhzbRvLnLiXmHL3buwD6oXMTyYnxGzCZ61DeDQ2cQa8ynKmRim5JL3viKMqtdyasXVoGf/u0GqMsgaLcqN6e/vXuXMGu6kkUPEQ=
|
||||
- secure: Lydms45scczE9gZvkWze2fXVbEInKW48DOCIRJWziEzxKkvXUE7WPy74fLvfvL+0JW0LH/ZLZ9EbgHcJAYYs8CBrfAeOOtnyo2DZ0ouEO13RmomSZneEKSpJxRtErcPIu+B0LzcEGyAgf7bxPVevEte50CK0pZJ7z7bHMhU23pY=
|
||||
- secure: cc0pAJjkmFNw2bO3zVACmtyHTwINAHALrtUxi+nRD+FhOO9KxuxuuwvcKCZKfp9EUOjz5PrYWKV1ZH/zt/jMix8A4Gyue2mWX8WYih7aTmJBcJWsFNTCybnClreKBCh18kHdWWhkmhk8EMINDvlqxzJZGpcNO04gxhL9wuLLrNQ=
|
||||
- secure: em0xXIm69rMHsHXYQiizeJB7dEFBkX33PsWDHwBNrX6lFBued23eL96KJC4RVbk6A+AHFtXFATrreZ14D5JH/E/37CXhe3X2R93WqiPUSH0s7NI4fFA1BroKUNAlqO4bMqDBidtNmwMPaLTXjaOnOZyvbAG7z+QV3TKC8tOeZDU=
|
||||
- secure: VFaSERlgsjzjiDQhKw8XFvQrjdvFzHHL7V3NQg+RfELHoT6I1pAGFdl/+lRBIVOiVkbQ6XnpBA28nlf0QydPHElRZdqmh0azQV/bkUXD4ffPE8q0iSqeqhAZ+5L05K5K+Gby/y8TZE4FX6e/7trFL7oq+h9x0gq5RQO8rAcTV84=
|
||||
matrix:
|
||||
include:
|
||||
- os: osx
|
||||
osx_image: xcode10.1
|
||||
compiler: clang
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
before_install:
|
||||
- travis/$TRAVIS_OS_NAME/before_install.sh
|
||||
## try early just to check, can delete later
|
||||
- date
|
||||
- brew update
|
||||
- sh travis/install-qt.sh
|
||||
##- brew install $QT
|
||||
- brew install libical
|
||||
- brew install libusb libusb-compat
|
||||
- brew install srmio
|
||||
- brew install libsamplerate
|
||||
- brew tap homebrew/science
|
||||
- brew install r
|
||||
## Disable KML for now
|
||||
##- brew install --HEAD travis/libkml.rb
|
||||
- sudo chmod -R +w /usr/local
|
||||
- curl -O http://www.ftdichip.com/Drivers/D2XX/MacOSX/D2XX1.2.2.dmg
|
||||
- git clone --depth 1 https://github.com/sintegrial/qwtplot3d.git qwtplot3d
|
||||
- cd qwtplot3d
|
||||
- CC=clang CXX=clang++ /usr/local/opt/$QT_PATH/bin/qmake -makefile -recursive QMAKE_CXXFLAGS_WARN_ON+="-Wno-unused-private-field -Wno-c++11-narrowing"
|
||||
- CC=clang CXX=clang++ make -j2
|
||||
- cd ..
|
||||
- git clone --branch 0.98 https://github.com/kypeli/kQOAuth.git kQOAuth-0.98
|
||||
- cd kQOAuth-0.98
|
||||
- CC=clang CXX=clang++ /usr/local/opt/$QT_PATH/bin/qmake -makefile -recursive QMAKE_CXXFLAGS_WARN_ON+="-Wno-unused-private-field -Wno-c++11-narrowing"
|
||||
- CC=clang CXX=clang++ make -j2 qmake_all
|
||||
- CC=clang CXX=clang++ sudo make install
|
||||
- cd ..
|
||||
- hdiutil mount D2XX1.2.2.dmg
|
||||
before_script:
|
||||
- travis/$TRAVIS_OS_NAME/before_script.sh
|
||||
- mkdir D2XX
|
||||
- cp /Volumes/release/D2XX/Object/10.5-10.7/x86_64/libftd2xx.1.2.2.dylib D2XX
|
||||
- sudo cp /Volumes/release/D2XX/Object/10.5-10.7/x86_64/libftd2xx.1.2.2.dylib /usr/local/lib
|
||||
- cp /Volumes/release/D2XX/bin/*.h D2XX
|
||||
- sed -i "" "s|libftd2xx.dylib|@executable_path/../Frameworks/libftd2xx.1.2.2.dylib|"
|
||||
src/FileIO/D2XX.cpp
|
||||
- cp qwt/qwtconfig.pri.in qwt/qwtconfig.pri
|
||||
- cp src/gcconfig.pri.in src/gcconfig.pri
|
||||
- /usr/local/opt/$QT_PATH/bin/lupdate src/src.pro
|
||||
- sed -i "" "s|#\(CONFIG += release.*\)|\1 static |" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(QMAKE_LRELEASE\).*|\1 += /usr/local/opt/$QT_PATH/bin/lrelease|"
|
||||
src/gcconfig.pri
|
||||
- sed -i "" "s|#\(QMAKE_CXXFLAGS\).*|\1_RELEASE += -mmacosx-version-min=10.7 -arch
|
||||
x86_64|" src/gcconfig.pri
|
||||
- sed -i "" "s|^#CloudDB|CloudDB|" src/gcconfig.pri
|
||||
- sed -i "" "s|^#LIBZ|LIBZ|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(SRMIO_INSTALL =.*\)|\1 /usr/local|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(D2XX_INCLUDE =.*\)|\1 ../D2XX|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(D2XX_LIBS =.*\)|\1 -L../D2XX -lftd2xx.1.2.2|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(KQOAUTH_INSTALL =.*\)|\1 /usr/local|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(KQOAUTH_INCLUDE =.*\)|\1 \$\$[QT_INSTALL_LIBS]/kqoauth.framework/Headers|"
|
||||
src/gcconfig.pri
|
||||
- sed -i "" "s|#\(KQOAUTH_LIBS =.*\)|\1 -F\$\$[QT_INSTALL_LIBS] -framework kqoauth|"
|
||||
src/gcconfig.pri
|
||||
- sed -i "" "s|#\(QWT3D_INSTALL =.*\)|\1 ../qwtplot3d|" src/gcconfig.pri
|
||||
## Disable KML for now
|
||||
##- sed -i "" "s|#\(KML_INSTALL =\).*|\1 /usr/local|" src/gcconfig.pri
|
||||
##- sed -i "" "s|#\(KML_LIBS =.*\)|\1 -L/usr/local/lib -lkmlxsd -lkmlregionator -lkmldom
|
||||
## -lkmlconvenience -lkmlengine -lkmlbase|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(ICAL_INSTALL =.*\)|\1 /usr/local|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(ICAL_LIBS =.*\)|\1 -L/usr/local/lib -lical|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(LIBUSB_INSTALL =\).*|\1 /usr/local|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(LIBUSB_LIBS =.*\)|\1 -L/usr/local/lib -lusb -lusb-1.0|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(SAMPLERATE_INSTALL =\).*|\1 /usr/local|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(SAMPLERATE_LIBS =\).*|\1 -L/usr/local/lib -lsamplerate|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(DEFINES += GC_HAVE_LION*\)|\1|" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(HTPATH = ../httpserver.*\)|\1 |" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(DEFINES += GC_WANT_ROBOT.*\)|\1 |" src/gcconfig.pri
|
||||
- sed -i "" "s|\(DEFINES += GC_VIDEO_NONE.*\)|#\1 |" src/gcconfig.pri
|
||||
- sed -i "" "s|#\(DEFINES += GC_VIDEO_QUICKTIME.*\)|\1 |" src/gcconfig.pri
|
||||
##Issues with c++11 and stdlib on travis and dependencies (e.g. plot3d)
|
||||
- sed -i "" "s|#\(DEFINES += GC_WANT_R.*\)|\1 |" src/gcconfig.pri
|
||||
- echo "QMAKE_CFLAGS_RELEASE += -mmacosx-version-min=10.7 -arch x86_64" >> src/gcconfig.pri
|
||||
- sed -i "" "s/__GC_GOOGLE_CALENDAR_CLIENT_SECRET__/"$GC_GOOGLE_CALENDAR_CLIENT_SECRET"/" src/Core/Secrets.h
|
||||
- sed -i "" "s/__GC_STRAVA_CLIENT_SECRET__/"$GC_STRAVA_CLIENT_SECRET"/" src/Core/Secrets.h
|
||||
- sed -i "" "s/__GC_DROPBOX_CLIENT_SECRET__/"$GC_DROPBOX_CLIENT_SECRET"/" src/Core/Secrets.h
|
||||
- sed -i "" "s/__GC_CYCLINGANALYTICS_CLIENT_SECRET__/"$GC_CYCLINGANALYTICS_CLIENT_SECRET"/" src/Core/Secrets.h
|
||||
- sed -i "" "s/__GC_TWITTER_CONSUMER_SECRET__/"$GC_TWITTER_CONSUMER_SECRET"/" src/Core/Secrets.h
|
||||
- cat src/gcconfig.pri
|
||||
script:
|
||||
- travis/$TRAVIS_OS_NAME/script.sh
|
||||
- CC=clang CXX=clang++ /usr/local/opt/$QT_PATH/bin/qmake -makefile -recursive QMAKE_CXXFLAGS_WARN_ON+="-Wno-unused-private-field -Wno-c++11-narrowing"
|
||||
- CC=clang CXX=clang++ make qmake_all
|
||||
- CC=clang CXX=clang++ make -j4 sub-qwt --silent
|
||||
- CC=clang CXX=clang++ make -j4 sub-src
|
||||
after_success:
|
||||
- if [[ $TRAVIS_OS_NAME == "osx" ]]; then
|
||||
export FINAL_NAME=dev-prerelease-branch-master-build-${TRAVIS_BUILD_NUMBER}.dmg;
|
||||
else
|
||||
export FINAL_NAME=dev-prerelease-branch-master-build-${TRAVIS_BUILD_NUMBER}.AppImage;
|
||||
fi
|
||||
- travis/$TRAVIS_OS_NAME/after_success.sh
|
||||
deploy:
|
||||
provider: releases
|
||||
api_key:
|
||||
secure: KlfkRM8oGP02y5LhbdxetnhqUG3YzVylvyhT8BTYjdoJtkJr7YXYpdhj9byZ9aiy1gSWI/g7A1X6/P8/McqRtgt4dEYr4Zg8QO7Y7QdTpgNQEwu8ZrkyyG/7b/rSkfFHDjrOAHslLVXuBNwWgi8YW1aTn0rY2AqDbOri7u6tt9Q=
|
||||
file: src/$FINAL_NAME
|
||||
skip_cleanup: true
|
||||
on:
|
||||
tags: true
|
||||
repo: GoldenCheetah/GoldenCheetah
|
||||
|
||||
- cd src
|
||||
- ls -laR GoldenCheetah.app
|
||||
- GoldenCheetah.app/Contents/MacOS/GoldenCheetah --help
|
||||
- /usr/local/opt/$QT_PATH/bin/macdeployqt GoldenCheetah.app -verbose=2 -dmg
|
||||
## fix up the bundle with macdeployqtfix
|
||||
- python ../travis/macdeployqtfix.py GoldenCheetah.app /usr/local/opt/$QT_PATH
|
||||
- brew remove $QT
|
||||
- mv GoldenCheetah.dmg GoldenCheetah_$QT.dmg
|
||||
- hdiutil mount GoldenCheetah_$QT.dmg
|
||||
- cd /Volumes/GoldenCheetah
|
||||
- ls -laR GoldenCheetah.app
|
||||
- GoldenCheetah.app/Contents/MacOS/GoldenCheetah --help
|
||||
##deploy broken in travis, read-only filesystem error
|
||||
##before_deploy:
|
||||
##- gem install mime-types -v 2.6.2
|
||||
##deploy:
|
||||
## provider: releases
|
||||
## api_key: $GH_OAUTH_TOKEN
|
||||
## file: GoldenCheetah_$QT.dmg
|
||||
## skip_cleanup: true
|
||||
## on:
|
||||
## tags: true
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
## How to get help
|
||||
|
||||
#### **Do you have questions about how to use GoldenCheetah?**
|
||||
|
||||
* Ask any question about how to use GoldenCheetah on [golden-cheetah-users mailing list](https://groups.google.com/forum/#!forum/golden-cheetah-users), ideally after watching the corresponding [tutorials](https://www.goldencheetah.org/#section-tutorials) and/or reading the relevant parts of the [documentation](https://github.com/GoldenCheetah/GoldenCheetah/wiki).
|
||||
|
||||
#### **Do you have questions about the source code?**
|
||||
|
||||
* Ask any question about how to build GoldenCheetah or anything related to the source code in the [golden-cheetah-developers mailing list](https://groups.google.com/forum/#!forum/golden-cheetah-developers), please see the [GoldenCheetah Developers Guide](https://github.com/GoldenCheetah/GoldenCheetah/wiki/Developers-guide) for addtional information.
|
||||
|
||||
## How to contribute to GoldenCheetah
|
||||
|
||||
#### **Did you find a bug?**
|
||||
|
||||
* **Ensure the bug was not already reported** by searching on GitHub under [Issues](https://github.com/GoldenCheetah/GoldenCheetah/issues).
|
||||
|
||||
* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/GoldenCheetah/GoldenCheetah/issues/new). Be sure to include a **title and clear description**, as much relevant information as possible, and **steps to reproduce the problem** or a **test file** demonstrating the expected behavior that is not occurring.
|
||||
|
||||
#### **Did you write a patch that fixes a bug?**
|
||||
|
||||
* Open a new GitHub pull request with the patch.
|
||||
|
||||
* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
|
||||
|
||||
* See [GoldenCheetah Guidelines for submitting a patch](https://github.com/GoldenCheetah/GoldenCheetah/wiki/Guidelines-for-submitting-a-patch) for additional information.
|
||||
|
||||
#### **Did you fix whitespace, format code, or make a purely cosmetic patch?**
|
||||
|
||||
Changes that are cosmetic in nature and do not add anything substantial to the stability or functionality of GoldenCheetah will generally not be accepted.
|
||||
|
||||
#### **Do you intend to add a new feature or change an existing one?**
|
||||
|
||||
* Suggest your change in the [golden-cheetah-developers mailing list](https://groups.google.com/forum/#!forum/golden-cheetah-developers) and start writing code.
|
||||
|
||||
* Do not open an issue on GitHub until you have collected positive feedback about the change. GitHub issues are primarily intended for bug reports and fixes.
|
||||
|
||||
#### **Do you want to contribute to GoldenCheetah translations?**
|
||||
|
||||
* Translators are very welcome, please see [GoldenCheetah Guidelines for translators](https://github.com/GoldenCheetah/GoldenCheetah/wiki/Guidelines-for-translators)
|
||||
|
||||
#### **Do you want to contribute to GoldenCheetah documentation?**
|
||||
|
||||
* The [GoldenCheetah wiki](https://github.com/GoldenCheetah/GoldenCheetah/wiki) is open to user's contributions. feel free to add what you consider relevant to make other's users experience smoother.
|
||||
|
||||
GoldenCheetah is a volunteer effort. Contributions are welcome!
|
||||
|
||||
Thanks!
|
||||
|
||||
GoldenCheetah Team
|
||||
833
COPYING
@@ -1,281 +1,622 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
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 Lesser General Public License instead.) You can apply it to
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. 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.
|
||||
them 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.
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
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.
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. 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.
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
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.
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
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.
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
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".
|
||||
0. Definitions.
|
||||
|
||||
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.
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
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.
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
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.
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
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:
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
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.
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
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.)
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
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.
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
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.
|
||||
1. Source Code.
|
||||
|
||||
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.
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
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 "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
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,
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
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,
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
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 Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
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.
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
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.
|
||||
2. Basic Permissions.
|
||||
|
||||
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.
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
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.
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
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
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey 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;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
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
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If 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.
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying 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.
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
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.
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
14. Revised Versions 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
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU 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.
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public 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.
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
NO WARRANTY
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
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.
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
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.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
@@ -287,15 +628,15 @@ 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
|
||||
state 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
|
||||
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
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
@@ -303,38 +644,32 @@ the "copyright" line and a pointer to where the full notice is found.
|
||||
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.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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:
|
||||
If the program does terminal interaction, 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'.
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program 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.
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
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:
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
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 Lesser General
|
||||
Public License instead of this License.
|
||||
The GNU 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 Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||
|
||||
|
||||
129
INSTALL-LINUX
@@ -21,6 +21,7 @@ CONTENTS
|
||||
- FTDI D2XX
|
||||
- SRMIO
|
||||
- liboauth
|
||||
- QwtPlot3d
|
||||
- libkml
|
||||
|
||||
3. ADDING OPTIONAL DEPENDENCIES WHEN BUILDING VERSION 3
|
||||
@@ -33,7 +34,7 @@ CONTENTS
|
||||
1. BASIC INSTALLATION WITH MANDATORY DEPENDENCIES
|
||||
=================================================
|
||||
|
||||
Installed Linux distribution of choice on platforms i386 or amd-64 (currently Debian-based distributions and Arch-based distributions are covered). You will not need to do this if you
|
||||
Installed Ubuntu 11.04 from CD image amd-64.iso. You will not need to do this if you
|
||||
already have a Linux distribution installed. Left this step in to highlight the
|
||||
Linux distribution the commands below were executed on.
|
||||
|
||||
@@ -41,12 +42,8 @@ login and open a terminal to get a shell prompt
|
||||
|
||||
Download MANDATORY DEPENDENCIES (browser)
|
||||
-----------------------------------------
|
||||
Download and install the Qt SDK from http://qt-project.org/
|
||||
Once that is completed test qmake is ok with: qmake --version (should report 4.9.8 or higher)
|
||||
|
||||
|
||||
DEBIAN-BASED DISTRIBUTION INSTRUCTIONS
|
||||
--------------------------------------
|
||||
Download and install the Qt 4.8 SDK from http://qt-project.org/
|
||||
Once that is completed test qmake is ok with: qmake --version (should report 4.7.0 or higher)
|
||||
|
||||
Install git with:
|
||||
$ sudo apt-get install git
|
||||
@@ -58,20 +55,6 @@ Install FLEX and BISON
|
||||
You will need flex v2.5.9 or later
|
||||
$ sudo apt-get install bison
|
||||
$ sudo apt-get install flex
|
||||
-----------------------------------
|
||||
|
||||
ARCH-BASED DISTRIBUTION INSTRUCTIONS
|
||||
------------------------------------
|
||||
|
||||
Install git:
|
||||
$ sudo pacman -S git
|
||||
|
||||
INSTALL FLEX and BISON
|
||||
----------------------
|
||||
$ sudo pacman -S flex bison
|
||||
|
||||
NEXT STEPS
|
||||
----------
|
||||
$ vi gcconfig.pri
|
||||
|
||||
Ensure you have the following lines (which are now also in gcconfig.pri.in which has
|
||||
@@ -91,20 +74,6 @@ $ make clean
|
||||
$ qmake
|
||||
$ make
|
||||
|
||||
To compile translation you need QT tool - lrelease
|
||||
If it is not found using he defaults in src/src.pro then set the full path and filename in gcconfig.pri
|
||||
QMAKE_LRELEASE = /usr/bin/lrelease
|
||||
|
||||
When build first time you get number of error messages on .qm files missing:
|
||||
"RCC: Error in 'Resources/application.qrc': Cannot find file 'translations/gc_fr.qm'"
|
||||
You can ignore these messages for your build. The .qm files will be created during the
|
||||
build at a later point in time via the "lrelease" command you configured in gcconfig.pri)
|
||||
|
||||
If your QT build includes its own local compress libs then you should comment the line below in gcconfig.pri,
|
||||
otherwise you will need to have the compress libraries installed separately.
|
||||
#LIBZ_INCLUDE =
|
||||
#LIBZ_LIBS = -lz
|
||||
|
||||
You will now have a release3 binary but with none of the release3 dependencies compiled in.
|
||||
Get latest GOLDEN CHEETAH source files
|
||||
--------------------------------------
|
||||
@@ -206,6 +175,96 @@ $ make
|
||||
|
||||
You now have SRM support built in.
|
||||
|
||||
OAUTH/CRYPT - For Tweet my ride (twitter) support
|
||||
-------------------------------------------------
|
||||
|
||||
You should be able to install liboauth and dependencies
|
||||
directly with:
|
||||
$ sudo apt-get install liboauth-dev
|
||||
|
||||
If this does not work then:
|
||||
|
||||
Download the source from: http://sourceforge.net/projects/liboauth/files/liboauth-0.9.4.tar.gz/download and
|
||||
unpack it into ~/Projects/liboauth-0.9.4 using archive manager
|
||||
|
||||
$ cd ~/Projects/liboauth-0.9.4
|
||||
|
||||
You may find that libcurl is not installed (which liboauth depends upon) so download it
|
||||
from http://curl.haxx.se/download/curl-7.21.6.tar.gz and unpack it into ~/Projects/curl-7.21.6
|
||||
using archive manager to drag and drop into your Projects folder
|
||||
|
||||
$ cd ~/Projects/curl-7.21.6
|
||||
$ ./configure
|
||||
$ make
|
||||
$ sudo make install
|
||||
|
||||
You should now have libcurl and friends installed in /usr/local
|
||||
|
||||
You may find that liboauth also requires the openssl libs installed, these are available
|
||||
pre-packaged thankfully so:
|
||||
$ sudo apt-get install libssl-dev
|
||||
|
||||
This will also install zlibg which liboauth also desires.
|
||||
|
||||
$ cd ~/Projects/liboauth-0.9.4
|
||||
$ ./configure
|
||||
$ make
|
||||
$ sudo make install
|
||||
|
||||
Now we have liboauth and libcurl in /usr/local and libssl in /usr/lib, so we can go back to
|
||||
GoldenCheetah and update the gcconfig.pri to point at the right places:
|
||||
|
||||
$ cd ~/Projects/Live/GoldenCheetah/src
|
||||
$ vi gcconfig.pri
|
||||
|
||||
Make sure the following are set:
|
||||
LIBOAUTH_INSTALL = /usr/local
|
||||
LIBCRYPTO_INSTALL = -lcrypto
|
||||
LIBCURL_INSTALL=-lcurl
|
||||
|
||||
Make clean is needed if you have previously built, since source files examine #defines before
|
||||
including this feature. You can skip it if you know why ;)
|
||||
$ make clean
|
||||
$ qmake
|
||||
$ make
|
||||
|
||||
You can now tweet your rides and amaze your friends, or alternatively tweet your rides but
|
||||
omit to mention average power to hide your weaknesses.
|
||||
|
||||
LIBQWTPLOT3D - For 3D plot
|
||||
--------------------------
|
||||
|
||||
Download the tarball from http://qwtplot3d.svn.sourceforge.net/viewvc/qwtplot3d/branches/maintain_0_2_x/qwtplot3d/?view=tar
|
||||
and unarchive it into your Projects directory using archive manager. There is no version number it just creates a
|
||||
subdirectory called qwtplot3d
|
||||
|
||||
You will need to install libgl and extensions, this can be done with;
|
||||
$ sudo apt-get install libgl1-mesa-dev
|
||||
$ sudo apt-get install libglu-dev
|
||||
|
||||
On Ubuntu 12.10 I also ran
|
||||
$ sudo apt-get install freeglut3 freeglut3-dev
|
||||
|
||||
$ cd ~/Projects/qwtplot3d
|
||||
Edit the qwtplot3d.pri and add
|
||||
CONFIG += staticlib
|
||||
|
||||
You will find that on modern Linuxes you also need to fix include/qwt3d_global.h by adding the line:
|
||||
#include <GL/glu.h>
|
||||
|
||||
then build
|
||||
$ qmake
|
||||
$ make
|
||||
|
||||
Then to let gc know where the qwtplot3d libs are you need to edit gcconfig.pri;
|
||||
$ cd ~/Projects/Live/GoldenCheetah/src
|
||||
$ vi gcconfig.pri
|
||||
|
||||
And uncomment the following line and set the install directory to where you build
|
||||
QWT3D_INSTALL = ~/Projects/qwtplot3d
|
||||
|
||||
|
||||
|
||||
LIBKML - For export to Google Earth
|
||||
-----------------------------------
|
||||
|
||||
|
||||
43
INSTALL-MAC
@@ -20,6 +20,8 @@ CONTENTS
|
||||
2. ADDING OPTIONAL DEPENDENCIES
|
||||
- FTDI D2XX
|
||||
- SRMIO
|
||||
- kQOAuth
|
||||
- QwtPlot3d
|
||||
- libkml
|
||||
- libusb
|
||||
- libical
|
||||
@@ -208,11 +210,6 @@ $ cd .. #this should put you in the top level dir containing this file
|
||||
$ qmake -recursive
|
||||
$ make
|
||||
|
||||
When build first time you get number of error messages on .qm files missing:
|
||||
"RCC: Error in 'Resources/application.qrc': Cannot find file 'translations/gc_fr.qm'"
|
||||
You can ignore these messages for your build. The .qm files will be created during the
|
||||
build at a later point in time via the "lrelease" command you configured in gcconfig.pri)
|
||||
|
||||
If make fails to find a Makefile then qmake is configured, by default, to create
|
||||
an xcode project file. You will need to change the qmake command above to tell it
|
||||
to create a makefile with the following:
|
||||
@@ -226,21 +223,12 @@ $ open src/GoldenCheetah.app
|
||||
|
||||
And you're up and running.
|
||||
|
||||
Now, if you would like to use XCode to debug the project, you can generate
|
||||
the necessary project file.
|
||||
$ qmake --recursive -spec macx-xcode
|
||||
|
||||
This will create a project as: build.xcodeproj Please be sure to build via
|
||||
the standard method before checkin to prevent build errors. If the build
|
||||
fails to launch with "couldn't be opened because you don't have permission
|
||||
to view it" Then a work around would be to open the project and attach
|
||||
the debugger.
|
||||
$ open src/GoldenCheetah.app
|
||||
|
||||
2. ADDING OPTIONAL DEPENDENCIES
|
||||
===============================
|
||||
|
||||
- SRMIO
|
||||
- kQOAuth
|
||||
- QwtPlot3d
|
||||
- libkml
|
||||
- libusb
|
||||
- libical
|
||||
@@ -256,7 +244,24 @@ should be taken into account.
|
||||
SRMIO (git)
|
||||
./configure CFLAGS="-isysroot /Developer/SDKs/MacOSX10.7.sdk -arch x86_64" CPPFLAGS=-I/usr/local/D2XX/ --disable-dependency-tracking
|
||||
|
||||
2.2 libkml - For export to Google Earth
|
||||
2.2 kQOAuth - For OAuth Authentication
|
||||
--------------------------------------
|
||||
|
||||
kQOauth (0.98)
|
||||
|
||||
qmake
|
||||
|
||||
|
||||
2.3 QwtPlot3d - For 3d plot support
|
||||
-----------------------------------
|
||||
|
||||
qwtplot3d (maintain_0_2_x)
|
||||
|
||||
Add the following to qwtplot3d.pro:
|
||||
CONFIG += x86_64 static
|
||||
QMAKE_MAC_SDK=/Developer/SDKs/MacOSX10.7.sdk
|
||||
|
||||
2.4 libkml - For export to Google Earth
|
||||
---------------------------------------
|
||||
|
||||
expat (2.0.1)
|
||||
@@ -268,7 +273,7 @@ libkml (pulled down from the svn repo)
|
||||
|
||||
note: the added CXXFLAGS helped clear the -Werror flag that stopped compilation.
|
||||
|
||||
2.3 libusb - For Garmin USB2 stick support
|
||||
2.5 libusb - For Garmin USB2 stick support
|
||||
------------------------------------------
|
||||
|
||||
libusb (0.1.12)
|
||||
@@ -285,7 +290,7 @@ make install
|
||||
|
||||
note: that the sed line updates some commands in libtool. not sure why the arch clags are not getting passed.
|
||||
|
||||
2.4 libical
|
||||
2.6 libical
|
||||
-----------
|
||||
|
||||
libical (0.46)
|
||||
|
||||
264
INSTALL-WIN32
@@ -1,207 +1,117 @@
|
||||
Please note: This is an old build instruction set for Version 2.x.
|
||||
|
||||
Most of it is still fairly valid for version 3.x.
|
||||
|
||||
Minimum build requirements are: Qt 4.8 with Qt.4.8.4 recommended.
|
||||
|
||||
All other libs are find the best that you can except for libusb which requires 0.1.12.
|
||||
|
||||
I have found that the cross compiler at http://mxe.cc/ to be great for building GC and SRMIO.
|
||||
|
||||
+++++++++++++++++++++++
|
||||
WIN32 BUILD WALKTHROUGH
|
||||
+++++++++++++++++++++++
|
||||
|
||||
Joern Rischmueller
|
||||
Gareth Coco
|
||||
|
||||
February 2017
|
||||
May 2011
|
||||
|
||||
This instruction will guide you through a standard build of GoldenCheetah (without external
|
||||
dependencies or API based services included).
|
||||
For my install, I am installing all my files into the directory: d:\coding.
|
||||
You can use any directory structure you like, you’ll just have to change paths.
|
||||
Just ensure that any folder path you use does not have a space in it.
|
||||
|
||||
Prerequisites:
|
||||
Download the NSI installer application. I use the Portable Apps version from:
|
||||
http://portableapps.com/apps/development/nsis_portable
|
||||
- Install in D:\Coding\NSISPortable\
|
||||
|
||||
To build on Windows you need the necessary toolchain to be installed. Since 2017
|
||||
GoldenCheetah is enabled to be build with the Microsoft Visual C++ 2015 toolchain,
|
||||
which is also the recommended toolchain to be used. Building with the "mingw" toolchain
|
||||
might still be possible, but may conflict with features needed from the Qt library
|
||||
which are no longer supported by the "mingw" version of Qt.
|
||||
Download Qt SDK from:
|
||||
http://qt.nokia.com/downloads/sdk-windows-cpp
|
||||
(File I am using is qt-sdk-win-opensource-2010.02.1.exe)
|
||||
- Install in D:\Coding\Qt
|
||||
|
||||
Disclaimer:
|
||||
Download the D2XX drivers:
|
||||
http://www.ftdichip.com/Drivers/D2XX.htm
|
||||
(File I am using is: CDM 202.06.00 WHQL Certified.zip)
|
||||
- Install in D:\Coding\D2XX
|
||||
|
||||
For any of the downloads and tools the document refers to, please check if you comply
|
||||
with the license agreements. I cannot any responsibility for any of the downloads you
|
||||
are doing.
|
||||
Download qwt-plot3d from:
|
||||
http://sourceforge.net/projects/qwtplot3d/files/
|
||||
(File I am using is qwtplot3d-0.2.7.zip)
|
||||
- Install in D:\Coding\qwtplot3d
|
||||
|
||||
You need:
|
||||
Edit D:\coding\qwtplot3d\src\qwt3d_function.cpp
|
||||
- Add at top of file: #include <cstdio>
|
||||
Edit D:\coding\qwtplot3d\qwtplot3d.pro
|
||||
- Comment out: #win32:TEMPLATE = vclib
|
||||
|
||||
- Microsoft Visual C++ 2015 - which is part of Visual Studio 2015
|
||||
-- Download from Microsoft - they offer a "Community Edition" which is sufficient
|
||||
to build GoldenCheetah
|
||||
|
||||
- Microsoft Windows SDK - which needs to be installed separately
|
||||
-- Download from here: https://developer.microsoft.com/de-de/windows/downloads/windows-10-sdk
|
||||
-- I am using the Windows 10 SDK - version 10.0.14393.795(since I build on Windows 10) -
|
||||
Note: building on other Windows Version(s) may require other SDK Version to be installed
|
||||
but was not tested
|
||||
-- When installing the SDK you have several options - for GoldenCheetah you need the "SDK" itself and
|
||||
in case you want to Debug also the "Debugging Tools" to be installed. All other parts (to my experience)
|
||||
are not required.
|
||||
|
||||
- Flex and Bison (below the version working for me)
|
||||
-- Download from here: https://sourceforge.net/projects/winflexbison/
|
||||
-- Use the "win_flex_bison-latest.zip" version
|
||||
-- Unzip whereever you like and make sure that the location "win_bison.exe" and "win_flex.exe
|
||||
are added to your "Path" environment variable
|
||||
|
||||
- Qt C++ Framework
|
||||
-- As of today, please use Qt 5.8.0 (which is the most recent official release) for Microsoft VC2015
|
||||
-- Download is available here: https://www.qt.io/download/ - the OpenSource version is sufficient
|
||||
to build GoldenCheetah
|
||||
You need to decide if you go for the 32Bit or the 64Bit version, all further descriptions will refer
|
||||
to the 64Bit version of Qt and the 64Bit Version of the Visual C++ Toolchain.
|
||||
32 Bit follows the same concepts, you just need to download and/or configure the tools for 32 bit.
|
||||
The
|
||||
-- Install Qt and make sure that the \bin directory of Qt is added to your "Path" environment variable
|
||||
(default path of Qt is C:\Qt\Qt5.8.0\5.8\msvc2015_64\bin)
|
||||
-- To build GoldenCheetah we recommend to use "jom.exe" which is an "nmake" clone - see https://wiki.qt.io/Jom
|
||||
With your Qt installation you also get QtCreator installed which is the free IDE for Qt. Jom is
|
||||
part of the QtCreator installation. For the later build process, make sure that the path to "jom.exe"
|
||||
is added to your "Path" environment variable.
|
||||
(default path of QtCreator/Jom is C:\Qt\Qt5.8.0\Tools\QtCreator\bin)
|
||||
|
||||
Note: Building a 32bit version / Building for Windows XP
|
||||
-- 32bit follows the same concepts, you just need to download and/or configure the tools for 32 bit.
|
||||
-- The Visual Studio standard toolchain as well as the SDK do not support Windows XP any more.
|
||||
|
||||
Then I start the “Qt Command Prompt” from the Qt SDK folder.
|
||||
|
||||
|
||||
Building from command line without additional dependencies:
|
||||
d:
|
||||
cd d:\coding\qwtplot3d\
|
||||
qmake -win32
|
||||
mingw32-make release
|
||||
|
||||
For this build, I am installing all source code,... into the directory: c:\coding
|
||||
Now check out your favourite web sites until the code is built.
|
||||
|
||||
- Download GoldenCheetah source (either using Git or downloading the ZIP file)
|
||||
-- Download from here: https://github.com/GoldenCheetah/GoldenCheetah
|
||||
-- Install in c:\coding\GoldenCheetah
|
||||
|
||||
- Setup the configuration of GoldenCheetah for the Qt windows build
|
||||
-- Copy C:\coding\GoldenCheetah\qwt\qwtconfig.pri.in to C:\coding\GoldenCheetah\qwt\qwtconfig.pri
|
||||
-- Copy C:\coding\GoldenCheetah\src\gccconfig.pri.in to C:\coding\GoldenCheetah\src\gccconfig.pri
|
||||
-- Edit the "gcconfig.pri" file (see also instructions in the file itself)
|
||||
Lines to be changed (from Default to New - or Add(ed):
|
||||
Default New
|
||||
#CONFIG += release CONFIG += release
|
||||
#DEFINES += NOWEBKIT DEFINES += NOWEBKIT
|
||||
#QMAKE_LRELEASE = /usr/bin/lrelease QMAKE_LRELEASE = lrelease
|
||||
|
||||
Default
|
||||
#WINKIT_INSTALL= "C:/Program Files (x86)/Windows Kits/8.1/Lib/winv6.3/um/x64"
|
||||
New
|
||||
WINKIT_INSTALL= "C:/Program Files (x86)/Windows Kits/10/Lib/10.0.14393.0/um/x64"
|
||||
Note: This is the default path for the SDK - depending on how you installed the
|
||||
SDK the path my deviate from the example.
|
||||
|
||||
Add Lines
|
||||
CONFIG += lex
|
||||
CONFIG += yacc
|
||||
|
||||
Default New
|
||||
#QMAKE_LEX = win_flex QMAKE_LEX = win_flex --wincompat
|
||||
#QMAKE_YACC = win_bison QMAKE_YACC = win_bison --file-prefix=y -t
|
||||
|
||||
Add Lines
|
||||
# Make sure that LEX/YACC is executed first (to allow multi-core compilation via Jom)
|
||||
lex.CONFIG += target_predeps
|
||||
yacc_impl.CONFIG += target_predeps
|
||||
yacc_decl.CONFIG += target_predeps
|
||||
|
||||
Leave the Qt Command Prompt window open as we use it later.
|
||||
|
||||
- Open the console tool of your choice in folder c:\coding\GoldenCheetah to run the build process
|
||||
(I assume cmd.com with prompt ">")
|
||||
-- Initialize the Visual C++ compile for 64 Bit Builds
|
||||
>CALL "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
|
||||
-- Run "qmake" to prepare the build for Qt
|
||||
>qmake build.pro -spec win32-msvc2015
|
||||
-- Run "jom" to prepare and run the GoldenCheetah build
|
||||
>jom qmake_all && jom
|
||||
Note: When build first time you get number of error messages on .qm files missing.
|
||||
"RCC: Error in 'Resources\application.qrc': Cannot find file 'translations/gc_fr.qm'"
|
||||
You can ignore these messages for your build. The .qm files will be created during
|
||||
the build at a later point in time via the "lrelease.exe" command you configured in
|
||||
gcconfig.pri)
|
||||
Hint: In some (still inpredictable cases) the build stops during compilation of the "qwt"
|
||||
library. Just re-start it by running "jom" again, without any other action. Your
|
||||
build will continue without problems.
|
||||
|
||||
- Wait - the compile with "jom" just uses 1 core. If you want to use more of your machine,
|
||||
you may use "jom" with option "-j <no_of_cores>" e.g. "jom -j 4".
|
||||
|
||||
|
||||
- Since your are doing an "in-source" build you will find a release version of "GoldenCheetah.exe"
|
||||
in folder: "C:\coding\GoldenCheetah\src\release". You can run the .exe from that location as long
|
||||
as the Qt libraries are on your "Path".
|
||||
|
||||
- To run GoldenCheetah.exe on a different machine (withoug Qt being installed), you need to collect
|
||||
the Qt libraries. Easiest way is the "windeployqt" tool provided with Qt.
|
||||
Details can be found here: http://doc.qt.io/qt-5/windows-deployment.html
|
||||
Download GoldenCheetah source
|
||||
(Either use GIT – git://github.com/GoldenCheetah/GoldenCheetah.git
|
||||
or download the zip file:
|
||||
http://github.com/GoldenCheetah/GoldenCheetah/zipball/master)
|
||||
- Install in D:\Coding\GoldenCheetah
|
||||
|
||||
Copy D:\Coding\GoldenCheetah\src\gccconfig.pri.in
|
||||
to D:\Coding\GoldenCheetah\src\gccconfig.pri and edit
|
||||
- Set: D2XX_INCLUDE = d:/coding/D2XX
|
||||
- Comment out #SRMIO_INSTALL
|
||||
- Comment out: CONFIG += debug
|
||||
- Uncomment: CONFIG += static
|
||||
|
||||
Building with additional dependencies:
|
||||
|
||||
We use a number of libraries for specific functions of GoldenCheetah. You find the list of dependencies
|
||||
and their sources documented in "gccconfig.pri.in". Here just a short summary which of those libraries
|
||||
are available in the official GoldenCheetah builds and some hints how to build them to be usable.
|
||||
Copy D:\Coding\GoldenCheetah\qwt\qwtconfig.pri.in
|
||||
to D:\Coding\GoldenCheetah\qwt\qwtconfig.pri and edit
|
||||
- Set win32 { INSTALLBASE = D:/Coding/Qt }
|
||||
- In #Qt4 win32 { section
|
||||
– Comment out: #CONFIG += debug # release/debug/debug_and_release
|
||||
– Add in: CONFIG += release
|
||||
|
||||
Info: I plan to provide a pre-compiled set of the dependencies for the Windows version of GoldenCheetah,
|
||||
so that not everybody has to invest the efforts to build the .dll's for the different tools.
|
||||
|
||||
|
||||
- SRM download support via SRMIO
|
||||
-- Is NOT part of the official Windows builds since I was not able to create a .DLL which works
|
||||
with an MSVC2015 based build of GoldenCheetah. If anybody finds a solution to add this dependency
|
||||
again - highly welcome.
|
||||
|
||||
- D2XX device download support
|
||||
-- Is part of the official Windows builds - we use the libs and do static linking.
|
||||
The version currently used is: "CDM v2.10.00 WHQL Certified" - while there are more recent
|
||||
version available for download.
|
||||
|
||||
- Google Earth .kml files (export)
|
||||
-- Is part of the official build as a static lib - build is done with MSVC2015 - the build uses Release 1.2
|
||||
(path of source code has changed to https://github.com/google/libkml)
|
||||
Using the “Qt Command Prompt” window from before:
|
||||
|
||||
- iCal Calendar support (prerequisite for the "Diary" view)
|
||||
-- Is part of the official build as a static lib - the build uses Release 1.0.1
|
||||
|
||||
- USB1 support via USBExpress
|
||||
-- Is part of the official build as .dll - the build uses Release 3.5.1
|
||||
|
||||
- USB2 support via libusb
|
||||
-- Is part of the official build - the build uses the pre-compiled release 1.2.6.0
|
||||
|
||||
- Video Playback via VLC
|
||||
-- Is part of the official build as .dll - the build uses the pre-compiled release 2.2.1
|
||||
|
||||
- Resampling via libsamplerate
|
||||
-- Is part of the official build as .dll - the build uses
|
||||
|
||||
cd d:\coding\GoldenCheetah
|
||||
qmake -win32 -recursive
|
||||
mingw32-make release
|
||||
|
||||
Building with integration to external services (via APIs)
|
||||
Go back to checking your email or favourite web sites while this builds.
|
||||
|
||||
As of today a number of integrations with external services like Strava, Cyclinganalytics,
|
||||
Google Calendar, Dropbox, Today's Plan, Google Drive, CloudDB) exist in the official GoldenCheetah
|
||||
builds. The permission to use API's of such services requires a dedicated registration (in this case
|
||||
for "GoldenCheetah" as the consumer of the services) where in return the GoldenCheetah team get's
|
||||
specific credentials to access the services.
|
||||
When all is finished, you should have a release version in:
|
||||
|
||||
All of the services request that the access credentials are kept private and are under responsibility
|
||||
of the team/company/individual who has registered for the API use with the company providing the services.
|
||||
D:\Coding\GoldenCheetah\src\release\
|
||||
|
||||
You can always start your personal registration process with the services to get your own credentials
|
||||
to build GoldenCheetah version which can use the service APIs. But you are doing that on your own full
|
||||
responsibility.
|
||||
|
||||
*****************************************************************************************************
|
||||
|
||||
Have fun to build your own version. Feedback/Contributions to this guide are welcome. The easiest way
|
||||
to contribute is to provide a pull-request.
|
||||
|
||||
Cheers.
|
||||
Joern
|
||||
|
||||
Next , move required build files into the d:\coding\GoldenCheetah\src\release directory.
|
||||
|
||||
cd d:\coding\GoldenCheetah
|
||||
copy /y ..\Qt\qt\bin\mingwm10.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\QtCore4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\QtGui4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\QtSql4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\QtXml4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\QtNetwork4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\QtOpenGL4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\QtWebKit4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\QtXmlPatterns4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\phonon4.dll src\release\
|
||||
copy /y ..\Qt\qt\bin\libgcc_s_dw2-1.dll src\release\
|
||||
copy /y ..\qwtplot3d\lib\qwtplot3d.dll src\release\
|
||||
mkdir src\release\sqldrivers
|
||||
copy /y ..\Qt\qt\plugins\sqldrivers\qsqlite4.dll src\release\sqldrivers\
|
||||
mkdir src\release\imageformats
|
||||
copy /y ..\Qt\qt\plugins\imageformats\qjpeg4.dll src\release\imageformats\
|
||||
|
||||
To build the self installer:
|
||||
|
||||
cd d:\coding\GoldenCheetah\src\win32
|
||||
d:\coding\NSISPortable\App\NSIS\makensis.exe GoldenCheetahInstall.nsi
|
||||
|
||||
You should find the installer file in the directory you are in.
|
||||
|
||||
Install and enjoy.
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
Issue tracker is **only** for Bugs and Features, before to open a new issue please read the Contributing document (link at the right) and use the forums if you need help.
|
||||
27
README.md
@@ -1,30 +1,9 @@
|
||||
<img src="src/Resources/images/gc.png" height="25%" width="25%">
|
||||
|
||||
# GoldenCheetah
|
||||
|
||||
## About
|
||||
|
||||
GoldenCheetah is an open-source data analysis tool primarily written in C++
|
||||
with Qt for cyclists and triathletes
|
||||
with support for training as well.
|
||||
|
||||
GoldenCheetah can connect with indoor trainers and cycling equipment such
|
||||
as cycling computers and power meters to import data.
|
||||
|
||||
In addition, GoldenCheetah can connect to cloud services.
|
||||
|
||||
It can then manipulate and view the data, as well as analyze it.
|
||||
|
||||
|
||||
|
||||
## Installing
|
||||
|
||||
Golden Cheetah install and build instructions are documented
|
||||
for each platform;
|
||||
|
||||
INSTALL-WIN32 For building on Microsoft Windows
|
||||
|
||||
INSTALL-LINUX For building on Linux
|
||||
INSTALL-LINUX For building on Ubuntu Linux
|
||||
|
||||
INSTALL-MAC For building on Apple OS X
|
||||
|
||||
@@ -35,6 +14,6 @@ Windows: [](https://scan.coverity.com/projects/goldencheetah-goldencheetah)
|
||||
|
||||
Alternatively, official builds are available from http://www.goldencheetah.org
|
||||
Alternatively official builds are available from http://www.goldencheetah.org
|
||||
|
||||
whilst the latest developer builds are available from https://github.com/GoldenCheetah/GoldenCheetah/releases
|
||||
whilst latest developer builds are available from https://github.com/GoldenCheetah/GoldenCheetah/releases
|
||||
|
||||
@@ -2,9 +2,9 @@ version: ci.{build}
|
||||
image: Visual Studio 2015
|
||||
clone_depth: 1
|
||||
init:
|
||||
# Setup QT 5.9 - 64Bit
|
||||
# Setup QT 5.6.0 - 64Bit
|
||||
|
||||
- set QTDIR=C:\Qt\5.9\msvc2015_64
|
||||
- set QTDIR=C:\Qt\5.6\msvc2015_64
|
||||
- set PATH=%QTDIR%\bin;%PATH%
|
||||
|
||||
# Setup MSVC - VS 2015
|
||||
@@ -22,7 +22,7 @@ install:
|
||||
- copy c:\libs\gcconfig64-Release.appveyor.pri src\gcconfig.pri
|
||||
|
||||
build_script:
|
||||
- qmake.exe build.pro -r -spec win32-msvc
|
||||
- qmake.exe build.pro -r -spec win32-msvc2015
|
||||
- nmake
|
||||
|
||||
#notifications:
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS = qwt
|
||||
unix:!macx {
|
||||
SUBDIRS += kqoauth
|
||||
}
|
||||
SUBDIRS += src
|
||||
CONFIG += ordered
|
||||
|
||||
@@ -1,707 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2010 Justin Knotzke (jknotzke@shampoo.ca)
|
||||
* Copyright (c) 2017 Mark Liversedge (liversedge@gmail.com)
|
||||
*
|
||||
* 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., 51
|
||||
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#include "Secrets.h"
|
||||
|
||||
#include "OAuthManager.h"
|
||||
#include "OAuthDialog.h"
|
||||
#include "Athlete.h"
|
||||
#include "Context.h"
|
||||
#include "Settings.h"
|
||||
#include "Colors.h"
|
||||
#include "TimeUtils.h"
|
||||
|
||||
#if QT_VERSION > 0x050000
|
||||
#include "GoogleDrive.h"
|
||||
#include "PolarFlow.h"
|
||||
|
||||
#include <QJsonParseError>
|
||||
#endif
|
||||
|
||||
OAuthManager::OAuthManager(Context *context, OAuthSite site, CloudService *service, QString baseURL, QString clientsecret) :
|
||||
context(context), site(site), service(service), baseURL(baseURL), clientsecret(clientsecret)
|
||||
{
|
||||
|
||||
if (service) { // ultimately this will be the only way this works
|
||||
if (service->id() == "Strava") site = this->site = STRAVA;
|
||||
if (service->id() == "Dropbox") site = this->site = DROPBOX;
|
||||
if (service->id() == "Cycling Analytics") site = this->site = CYCLING_ANALYTICS;
|
||||
if (service->id() == "Google Drive") site = this->site = GOOGLE_DRIVE;
|
||||
if (service->id() == "University of Kent") site = this->site = KENTUNI;
|
||||
if (service->id() == "Today's Plan") site = this->site = TODAYSPLAN;
|
||||
if (service->id() == "Withings") site = this->site = WITHINGS;
|
||||
if (service->id() == "PolarFlow") site = this->site = POLAR;
|
||||
if (service->id() == "SportTracks.mobi") site = this->site = SPORTTRACKS;
|
||||
if (service->id() == "Xert") site = this->site = XERT;
|
||||
}
|
||||
|
||||
// check if SSL is available - if not - message and end
|
||||
if (!QSslSocket::supportsSsl()) {
|
||||
QString text = QString(tr("SSL Security Libraries required for 'Authorise' are missing in this installation."));
|
||||
QMessageBox sslMissing(QMessageBox::Critical, tr("Authorization Error"), text);
|
||||
sslMissing.exec();
|
||||
noSSLlib = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// ignore responses to false, used by POLARFLOW when binding the user
|
||||
ignore = false;
|
||||
|
||||
// SSL is available - so authorisation can take place
|
||||
noSSLlib = false;
|
||||
}
|
||||
|
||||
void
|
||||
OAuthManager::authorize()
|
||||
{
|
||||
//
|
||||
// All services have some kind of initial authorisation URL where the user needs
|
||||
// to login and confirm they are willing to authorise the particular app and
|
||||
// provide a temporary token to get the real token with
|
||||
//
|
||||
QString urlstr = "";
|
||||
|
||||
if (site == STRAVA) {
|
||||
|
||||
urlstr = QString("https://www.strava.com/oauth/authorize?");
|
||||
urlstr.append("client_id=").append(GC_STRAVA_CLIENT_ID).append("&");
|
||||
urlstr.append("scope=view_private,write&");
|
||||
urlstr.append("redirect_uri=http://www.goldencheetah.org/&");
|
||||
urlstr.append("response_type=code&");
|
||||
urlstr.append("approval_prompt=force");
|
||||
|
||||
} else if (site == DROPBOX) {
|
||||
|
||||
urlstr = QString("https://www.dropbox.com/oauth2/authorize?");
|
||||
#ifdef GC_DROPBOX_CLIENT_ID
|
||||
urlstr.append("client_id=").append(GC_DROPBOX_CLIENT_ID).append("&");
|
||||
#endif
|
||||
urlstr.append("redirect_uri=https://goldencheetah.github.io/blank.html&");
|
||||
urlstr.append("response_type=code&");
|
||||
urlstr.append("force_reapprove=true");
|
||||
|
||||
} else if (site == CYCLING_ANALYTICS) {
|
||||
|
||||
urlstr = QString("https://www.cyclinganalytics.com/api/auth?");
|
||||
urlstr.append("client_id=").append(GC_CYCLINGANALYTICS_CLIENT_ID).append("&");
|
||||
urlstr.append("scope=modify_rides&");
|
||||
urlstr.append("redirect_uri=http://www.goldencheetah.org/&");
|
||||
urlstr.append("response_type=code&");
|
||||
urlstr.append("approval_prompt=force");
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
|
||||
} else if (site == GOOGLE_DRIVE) {
|
||||
|
||||
const QString scope = service->getSetting(GC_GOOGLE_DRIVE_AUTH_SCOPE, "drive.appdata").toString();
|
||||
// OAUTH 2.0 - Google flow for installed applications
|
||||
urlstr = QString("https://accounts.google.com/o/oauth2/auth?");
|
||||
// We only request access to the application data folder, not all files.
|
||||
urlstr.append("scope=https://www.googleapis.com/auth/" + scope + "&");
|
||||
urlstr.append("redirect_uri=urn:ietf:wg:oauth:2.0:oob&");
|
||||
urlstr.append("response_type=code&");
|
||||
urlstr.append("client_id=").append(GC_GOOGLE_DRIVE_CLIENT_ID);
|
||||
|
||||
} else if (site == KENTUNI) {
|
||||
|
||||
const QString scope = service->getSetting(GC_UOK_GOOGLE_DRIVE_AUTH_SCOPE, "drive.appdata").toString();
|
||||
|
||||
// OAUTH 2.0 - Google flow for installed applications
|
||||
urlstr = QString("https://accounts.google.com/o/oauth2/auth?");
|
||||
// We only request access to the application data folder, not all files.
|
||||
urlstr.append("scope=https://www.googleapis.com/auth/" + scope + "&");
|
||||
urlstr.append("redirect_uri=urn:ietf:wg:oauth:2.0:oob&");
|
||||
urlstr.append("response_type=code&");
|
||||
urlstr.append("client_id=").append(GC_GOOGLE_DRIVE_CLIENT_ID);
|
||||
|
||||
#endif
|
||||
|
||||
} else if (site == TODAYSPLAN) {
|
||||
|
||||
//urlstr = QString("https://whats.todaysplan.com.au/en/authorize/"); //XXX fixup below when pages.cpp goes
|
||||
if (baseURL=="") baseURL=service->getSetting(GC_TODAYSPLAN_URL, "https://whats.todaysplan.com.au").toString();
|
||||
urlstr = QString("%1/authorize/").arg(baseURL);
|
||||
urlstr.append(GC_TODAYSPLAN_CLIENT_ID);
|
||||
|
||||
} else if (site == POLAR) {
|
||||
|
||||
// OAUTH 2.0 - Google flow for installed applications
|
||||
urlstr = QString("%1?").arg(GC_POLARFLOW_OAUTH_URL);
|
||||
// We only request access to the application data folder, not all files.
|
||||
urlstr.append("response_type=code&");
|
||||
urlstr.append("client_id=").append(GC_POLARFLOW_CLIENT_ID);
|
||||
|
||||
} else if (site == SPORTTRACKS) {
|
||||
|
||||
// We only request access to the application data folder, not all files.
|
||||
urlstr = QString("https://api.sporttracks.mobi/oauth2/authorize?");
|
||||
urlstr.append("redirect_uri=http://www.goldencheetah.org&");
|
||||
urlstr.append("state=xyzzy&");
|
||||
urlstr.append("response_type=code&");
|
||||
urlstr.append("client_id=").append(GC_SPORTTRACKS_CLIENT_ID);
|
||||
|
||||
} else if (site == WITHINGS) {
|
||||
|
||||
// Withings is the only service that uses KQOauth for now.
|
||||
|
||||
#ifdef GC_HAVE_KQOAUTH
|
||||
oauthRequest = new KQOAuthRequest;
|
||||
oauthManager = new KQOAuthManager(this);
|
||||
|
||||
connect(oauthManager, SIGNAL(temporaryTokenReceived(QString,QString)), this, SLOT(onTemporaryTokenReceived(QString, QString)));
|
||||
connect(oauthManager, SIGNAL(authorizationReceived(QString,QString)), this, SLOT( onAuthorizationReceived(QString, QString)));
|
||||
connect(oauthManager, SIGNAL(accessTokenReceived(QString,QString)), this, SLOT(onAccessTokenReceived(QString,QString)));
|
||||
connect(oauthManager, SIGNAL(requestReady(QByteArray)), this, SLOT(onRequestReady(QByteArray)));
|
||||
connect(oauthManager, SIGNAL(authorizationPageRequested(QUrl)), this, SLOT(onAuthorizationPageRequested(QUrl)));
|
||||
|
||||
oauthRequest->initRequest(KQOAuthRequest::TemporaryCredentials, QUrl("https://oauth.withings.com/account/request_token"));
|
||||
//oauthRequest->setEnableDebugOutput(true);
|
||||
oauthRequest->setHttpMethod(KQOAuthRequest::GET);
|
||||
oauthRequest->setConsumerKey(GC_WITHINGS_CONSUMER_KEY);
|
||||
oauthRequest->setConsumerSecretKey(GC_WITHINGS_CONSUMER_SECRET);
|
||||
//oauthRequest->setCallbackUrl(QUrl("http://www.goldencheetah.org"));
|
||||
oauthManager->setHandleUserAuthorization(true); // false to use callback
|
||||
oauthManager->setHandleAuthorizationPageOpening(false);
|
||||
|
||||
oauthManager->executeRequest(oauthRequest);
|
||||
#endif
|
||||
|
||||
} else if (site == XERT) {
|
||||
getTokenWithCode("");
|
||||
}
|
||||
|
||||
//
|
||||
// STEP 1: LOGIN AND AUTHORISE THE APPLICATION
|
||||
//
|
||||
/*if (site == DROPBOX || site == STRAVA || site == CYCLING_ANALYTICS || site == POLAR || site == SPORTTRACKS || site == GOOGLE_DRIVE || site == KENTUNI || site == TODAYSPLAN) {
|
||||
|
||||
url = QUrl(urlstr);
|
||||
view->setUrl(url);
|
||||
|
||||
// connects
|
||||
connect(view, SIGNAL(urlChanged(const QUrl&)), this, SLOT(urlChanged(const QUrl&)));
|
||||
connect(view, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
// just ignore SSL handshake errors at all times
|
||||
void
|
||||
OAuthManager::onSslErrors(QNetworkReply *reply, const QList<QSslError>&)
|
||||
{
|
||||
reply->ignoreSslErrors();
|
||||
}
|
||||
|
||||
|
||||
#ifdef GC_HAVE_KQOAUTH
|
||||
|
||||
//
|
||||
// KQOauth call backs
|
||||
//
|
||||
void
|
||||
OAuthManager::onTemporaryTokenReceived(QString, QString)
|
||||
{
|
||||
//qDebug() << "onTemporaryTokenReceived";
|
||||
QUrl userAuthURL;
|
||||
|
||||
if (site == WITHINGS) {
|
||||
userAuthURL = "https://oauth.withings.com/account/authorize";
|
||||
}
|
||||
|
||||
if(oauthManager->lastError() == KQOAuthManager::NoError) {
|
||||
oauthManager->getUserAuthorization(userAuthURL);
|
||||
} else
|
||||
qDebug() << "error" << oauthManager->lastError();
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
OAuthManager::onAuthorizationReceived(QString, QString)
|
||||
{
|
||||
//qDebug() << "Authorization token received: " << token << verifier;
|
||||
|
||||
if (site == WITHINGS) {
|
||||
oauthManager->getUserAccessTokens(QUrl("https://oauth.withings.com/account/access_token"));
|
||||
}
|
||||
|
||||
if(oauthManager->lastError() != KQOAuthManager::NoError) {
|
||||
QString error = QString(tr("Error fetching OAuth credentials - Endpoint: /oauth/access_token"));
|
||||
QMessageBox oautherr(QMessageBox::Critical, tr("Authorization Error"), error);
|
||||
oautherr.exec();
|
||||
//accept();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
OAuthManager::onAccessTokenReceived(QString token, QString tokenSecret)
|
||||
{
|
||||
//qDebug() << "Access token received: " << token << tokenSecret;
|
||||
|
||||
QString info;
|
||||
if (site == WITHINGS) {
|
||||
service->setSetting(GC_WITHINGS_TOKEN, token);
|
||||
service->setSetting(GC_WITHINGS_SECRET, tokenSecret);
|
||||
appsettings->setCValue(context->athlete->cyclist, GC_NOKIA_REFRESH_TOKEN, "");
|
||||
|
||||
info = QString(tr("Nokia Health (Withings) authorization was successful."));
|
||||
}
|
||||
|
||||
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
//accept();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
OAuthManager::onAuthorizedRequestDone() {} // request sent - do nothing
|
||||
|
||||
void
|
||||
OAuthManager::onRequestReady(QByteArray response)
|
||||
{
|
||||
//qDebug() << "Response received: " << response;
|
||||
|
||||
QString r = response;
|
||||
if (r.contains("\"errors\"", Qt::CaseInsensitive)) {
|
||||
|
||||
QMessageBox oautherr(QMessageBox::Critical, tr("Error in authorization"),
|
||||
tr("There was an error during authorization. Please check the error description."));
|
||||
oautherr.setDetailedText(r); // probably blank
|
||||
oautherr.exec();
|
||||
|
||||
} else {
|
||||
|
||||
if (site == WITHINGS) {
|
||||
|
||||
QString userid;
|
||||
|
||||
#if QT_VERSION > 0x050000
|
||||
QUrlQuery params;
|
||||
params.setQuery(response);
|
||||
#else
|
||||
QUrl params;
|
||||
params.setEncodedQuery(response);
|
||||
#endif
|
||||
userid = params.queryItemValue("userid");
|
||||
|
||||
if (userid.isEmpty() == false) {
|
||||
service->setSetting(GC_WIUSER, userid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void OAuthManager::onAuthorizationPageRequested(QUrl url) {
|
||||
// open Authorization page in view
|
||||
view->setUrl(url);
|
||||
|
||||
}
|
||||
#endif // KQOAuth callbacks used by Withings only
|
||||
|
||||
|
||||
//
|
||||
// STEP 2: AUTHORISATION REDIRECT WITH TEMPORARY CODE
|
||||
//
|
||||
// When the URL changes, it will be the redirect with the temporary token after
|
||||
// the initial authorisation. The URL will have some parameters to indicate this
|
||||
// if they exist we should intercept the redirect to get the permanent token.
|
||||
// If they don't get passed then we don't need to do anything.
|
||||
//
|
||||
void
|
||||
OAuthManager::getTokenWithCode(QString code)
|
||||
{
|
||||
QString authheader;
|
||||
|
||||
// sites that use this scheme
|
||||
if (site == DROPBOX || site == STRAVA || site == CYCLING_ANALYTICS || site == TODAYSPLAN || site == POLAR || site == SPORTTRACKS || site == XERT) {
|
||||
|
||||
|
||||
// sporttracks insists on passing state
|
||||
if (code.endsWith("&state=xyzzy")) code = code.mid(0,code.length()-12);
|
||||
|
||||
QByteArray data;
|
||||
#if QT_VERSION > 0x050000
|
||||
QUrlQuery params;
|
||||
#else
|
||||
QUrl params;
|
||||
#endif
|
||||
QString urlstr = "";
|
||||
|
||||
// now get the final token to store
|
||||
if (site == DROPBOX) {
|
||||
|
||||
urlstr = QString("https://api.dropboxapi.com/oauth2/token?");
|
||||
urlstr.append("redirect_uri=https://goldencheetah.github.io/blank.html&");
|
||||
params.addQueryItem("grant_type", "authorization_code");
|
||||
#ifdef GC_DROPBOX_CLIENT_ID
|
||||
params.addQueryItem("client_id", GC_DROPBOX_CLIENT_ID);
|
||||
#endif
|
||||
#ifdef GC_DROPBOX_CLIENT_SECRET
|
||||
params.addQueryItem("client_secret", GC_DROPBOX_CLIENT_SECRET);
|
||||
#endif
|
||||
|
||||
} else if (site == POLAR) {
|
||||
|
||||
urlstr = QString("%1?").arg(GC_POLARFLOW_TOKEN_URL);
|
||||
urlstr.append("redirect_uri=http://www.goldencheetah.org");
|
||||
params.addQueryItem("grant_type", "authorization_code");
|
||||
#if (defined GC_POLARFLOW_CLIENT_ID) && (defined GC_POLARFLOW_CLIENT_SECRET)
|
||||
authheader = QString("%1:%2").arg(GC_POLARFLOW_CLIENT_ID).arg(GC_POLARFLOW_CLIENT_SECRET);
|
||||
#endif
|
||||
|
||||
} else if (site == SPORTTRACKS) {
|
||||
|
||||
urlstr = QString("https://api.sporttracks.mobi/oauth2/token?");
|
||||
params.addQueryItem("client_id", GC_SPORTTRACKS_CLIENT_ID);
|
||||
params.addQueryItem("client_secret", GC_SPORTTRACKS_CLIENT_SECRET);
|
||||
params.addQueryItem("redirect_uri","http://www.goldencheetah.org");
|
||||
params.addQueryItem("grant_type", "authorization_code");
|
||||
|
||||
} else if (site == STRAVA) {
|
||||
|
||||
urlstr = QString("https://www.strava.com/oauth/token?");
|
||||
params.addQueryItem("client_id", GC_STRAVA_CLIENT_ID);
|
||||
#ifdef GC_STRAVA_CLIENT_SECRET
|
||||
params.addQueryItem("client_secret", GC_STRAVA_CLIENT_SECRET);
|
||||
#endif
|
||||
|
||||
} else if (site == CYCLING_ANALYTICS) {
|
||||
|
||||
urlstr = QString("https://www.cyclinganalytics.com/api/token?");
|
||||
params.addQueryItem("client_id", GC_CYCLINGANALYTICS_CLIENT_ID);
|
||||
#ifdef GC_CYCLINGANALYTICS_CLIENT_SECRET
|
||||
params.addQueryItem("client_secret", GC_CYCLINGANALYTICS_CLIENT_SECRET);
|
||||
#endif
|
||||
params.addQueryItem("grant_type", "authorization_code");
|
||||
|
||||
} else if (site == TODAYSPLAN) {
|
||||
|
||||
if (baseURL=="") baseURL=service->getSetting(GC_TODAYSPLAN_URL, "https://whats.todaysplan.com.au").toString();
|
||||
urlstr = QString("%1/rest/oauth/access_token?").arg(baseURL);
|
||||
params.addQueryItem("client_id", GC_TODAYSPLAN_CLIENT_ID);
|
||||
#ifdef GC_TODAYSPLAN_CLIENT_SECRET
|
||||
if (clientsecret != "") //XXX get rid when pages.cpp goes
|
||||
params.addQueryItem("client_secret", clientsecret);
|
||||
else if (service->getSetting(GC_TODAYSPLAN_USERKEY, "").toString() != "")
|
||||
params.addQueryItem("client_secret", service->getSetting(GC_TODAYSPLAN_USERKEY, "").toString());
|
||||
else
|
||||
params.addQueryItem("client_secret", GC_TODAYSPLAN_CLIENT_SECRET);
|
||||
|
||||
#endif
|
||||
params.addQueryItem("grant_type", "authorization_code");
|
||||
params.addQueryItem("redirect_uri", "https://goldencheetah.github.io/blank.html");
|
||||
|
||||
} else if (site == XERT) {
|
||||
|
||||
urlstr = QString("https://www.xertonline.com/oauth/token");
|
||||
params.addQueryItem("username", service->getSetting(GC_XERTUSER, "").toString());
|
||||
params.addQueryItem("password", service->getSetting(GC_XERTPASS, "").toString());
|
||||
params.addQueryItem("grant_type", "password");
|
||||
|
||||
authheader = QString("%1:%1").arg("xert_public");
|
||||
}
|
||||
|
||||
// all services will need us to send the temporary code received
|
||||
params.addQueryItem("code", code);
|
||||
|
||||
#if QT_VERSION > 0x050000
|
||||
data.append(params.query(QUrl::FullyEncoded));
|
||||
#else
|
||||
data=params.encodedQuery();
|
||||
#endif
|
||||
|
||||
// trade-in the temporary access code retrieved by the Call-Back URL for the finale token
|
||||
QUrl url = QUrl(urlstr);
|
||||
|
||||
QNetworkRequest request = QNetworkRequest(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
|
||||
|
||||
// client id and secret are encoded and sent in the header for POLAR and XERT
|
||||
if (site == POLAR || site == XERT) request.setRawHeader("Authorization", "Basic " + authheader.toLatin1().toBase64());
|
||||
|
||||
// now get the final token - but ignore errors
|
||||
manager = new QNetworkAccessManager(this);
|
||||
connect(manager, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )), this, SLOT(onSslErrors(QNetworkReply*, const QList<QSslError> & )));
|
||||
//connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkRequestFinished(QNetworkReply*)));
|
||||
QNetworkReply *reply = manager->post(request, data);
|
||||
|
||||
QEventLoop loop;
|
||||
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
||||
loop.exec();
|
||||
networkRequestFinished(reply);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// GOOGLE DRIVE gets the code in the HTML title field (different to other services)
|
||||
//
|
||||
void
|
||||
OAuthManager::loadFinished(bool ok)
|
||||
{
|
||||
|
||||
if (site == GOOGLE_DRIVE || site == KENTUNI) {
|
||||
|
||||
if (ok && url.toString().startsWith("https://accounts.google.com/o/oauth2/auth")) {
|
||||
|
||||
// retrieve the code from the HTML page title
|
||||
QString title = view->title();
|
||||
|
||||
if (title.contains("code")) {
|
||||
|
||||
QString code = title.right(title.length()-title.indexOf("code=")-5);
|
||||
QByteArray data;
|
||||
#if QT_VERSION > 0x050000
|
||||
QUrlQuery params;
|
||||
#else
|
||||
QUrl params;
|
||||
#endif
|
||||
QString urlstr = "https://www.googleapis.com/oauth2/v3/token?";
|
||||
params.addQueryItem("client_id", GC_GOOGLE_DRIVE_CLIENT_ID);
|
||||
|
||||
if (site == GOOGLE_DRIVE || site == KENTUNI) {
|
||||
params.addQueryItem("client_secret", GC_GOOGLE_DRIVE_CLIENT_SECRET);
|
||||
}
|
||||
|
||||
params.addQueryItem("code", code);
|
||||
params.addQueryItem("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
|
||||
params.addQueryItem("grant_type", "authorization_code");
|
||||
|
||||
#if QT_VERSION > 0x050000
|
||||
data.append(params.query(QUrl::FullyEncoded));
|
||||
#else
|
||||
data=params.encodedQuery();
|
||||
#endif
|
||||
|
||||
// trade-in the temporary access code retrieved by the
|
||||
// Call-Back URL for the finale token
|
||||
QUrl url = QUrl(urlstr);
|
||||
QNetworkRequest request = QNetworkRequest(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
|
||||
// not get the final token - ignoring errors
|
||||
manager = new QNetworkAccessManager(this);
|
||||
connect(manager, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )), this, SLOT(onSslErrors(QNetworkReply*, const QList<QSslError> & )));
|
||||
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkRequestFinished(QNetworkReply*)));
|
||||
manager->post(request, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
static QString RawJsonStringGrab(const QByteArray& payload,
|
||||
const QString& needle) {
|
||||
// A RegExp based JSON string parser. Not the best, but it does the job.
|
||||
QString regex =
|
||||
// This matches the key.
|
||||
"(" + needle + "|\"" + needle + "\"|'" + needle + "')"
|
||||
// Matches the separator.
|
||||
"[\\s]*:[\\s]*"
|
||||
// matches the value.
|
||||
"(\"\\S+\"|'\\S+')";
|
||||
QRegExp q(regex);
|
||||
if (!q.isValid()) {
|
||||
// Somehow failed to build the regex.
|
||||
return "";
|
||||
}
|
||||
int start = q.indexIn(payload);
|
||||
int pos = q.pos(2);
|
||||
if (start == -1 || pos == -1) {
|
||||
// Failed to find the key or the value.
|
||||
return "";
|
||||
}
|
||||
QString ret = payload.mid(pos, q.matchedLength() + start - pos);
|
||||
// Remove " or ' from the value.
|
||||
ret.remove(0, 1);
|
||||
ret.remove(ret.size() - 1, 1);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
//
|
||||
// STEP 3: REFRESH AND ACCESS TOKEN RECEIVED
|
||||
//
|
||||
// this is when we get the refresh or access tokens after a redirect has been loaded
|
||||
// so pretty much at the end of the process. Each service can have slightly special
|
||||
// needs and certainly needs to set the right setting anyway.
|
||||
//
|
||||
void
|
||||
OAuthManager::networkRequestFinished(QNetworkReply *reply)
|
||||
{
|
||||
|
||||
// we've been told to ignore responses (used by POLAR, maybe others in future)
|
||||
if (ignore) return;
|
||||
|
||||
// we can handle SSL handshake errors, if we got here then some kind of protocol was agreed
|
||||
if (reply->error() == QNetworkReply::NoError || reply->error() == QNetworkReply::SslHandshakeFailedError) {
|
||||
|
||||
QByteArray payload = reply->readAll(); // JSON
|
||||
QString refresh_token;
|
||||
QString access_token;
|
||||
double polar_userid=0;
|
||||
|
||||
// parse the response and extract the tokens, pretty much the same for all services
|
||||
// although polar choose to also pass a user id, which is needed for future calls
|
||||
#if QT_VERSION > 0x050000
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument document = QJsonDocument::fromJson(payload, &parseError);
|
||||
if (parseError.error == QJsonParseError::NoError) {
|
||||
refresh_token = document.object()["refresh_token"].toString();
|
||||
access_token = document.object()["access_token"].toString();
|
||||
if (site == POLAR) polar_userid = document.object()["x_user_id"].toDouble();
|
||||
}
|
||||
#else
|
||||
refresh_token = RawJsonStringGrab(payload, "refresh_token");
|
||||
access_token = RawJsonStringGrab(payload, "access_token");
|
||||
#endif
|
||||
|
||||
// if we failed to extract then we have a big problem
|
||||
// google uses a refresh token so trap for them only
|
||||
if (((site == GOOGLE_DRIVE || site == KENTUNI) && refresh_token == "") || access_token == "") {
|
||||
|
||||
// Something failed.
|
||||
// Only Google uses both refresh and access tokens.
|
||||
QString error = QString(tr("Error retrieving authoriation credentials"));
|
||||
QMessageBox oautherr(QMessageBox::Critical, tr("Authorization Error"), error);
|
||||
oautherr.setDetailedText(error);
|
||||
oautherr.exec();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// now set the tokens etc
|
||||
if (site == DROPBOX) {
|
||||
|
||||
service->setSetting(GC_DROPBOX_TOKEN, access_token);
|
||||
QString info = QString(tr("Dropbox authorization was successful."));
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
|
||||
} else if (site == SPORTTRACKS) {
|
||||
|
||||
service->setSetting(GC_SPORTTRACKS_TOKEN, access_token);
|
||||
service->setSetting(GC_SPORTTRACKS_REFRESH_TOKEN, refresh_token);
|
||||
service->setSetting(GC_SPORTTRACKS_LAST_REFRESH, QDateTime::currentDateTime());
|
||||
QString info = QString(tr("SportTracks authorization was successful."));
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
|
||||
} else if (site == POLAR) {
|
||||
|
||||
service->setSetting(GC_POLARFLOW_TOKEN, access_token);
|
||||
service->setSetting(GC_POLARFLOW_USER_ID, polar_userid);
|
||||
|
||||
// we now need to bind the user, this is a one time deal.
|
||||
QString url = QString("%1/v3/users").arg(GC_POLARFLOW_URL);
|
||||
|
||||
// request using the bearer token
|
||||
QNetworkRequest request(url);
|
||||
request.setRawHeader("Authorization", (QString("Bearer %1").arg(access_token)).toLatin1());
|
||||
request.setRawHeader("Accept", "application/json");
|
||||
request.setRawHeader("Content-Type", "application/json");
|
||||
|
||||
// data to post
|
||||
QByteArray data;
|
||||
data.append(QString("{\"member-id\":\"%1\"}").arg(context->athlete->cyclist));
|
||||
|
||||
// the request will fallback to this method on networkRequestFinished
|
||||
// but we are done, so set ignore= true to get this function to just
|
||||
// return without doing anything
|
||||
ignore=true;
|
||||
QNetworkReply *bind = manager->post(request, data);
|
||||
|
||||
// blocking request
|
||||
QEventLoop loop;
|
||||
connect(bind, SIGNAL(finished()), &loop, SLOT(quit()));
|
||||
loop.exec();
|
||||
|
||||
// Bind response lists athlete details, we ignore them for now
|
||||
QByteArray r = bind->readAll();
|
||||
//qDebug()<<bind->errorString()<< "bind response="<<r;
|
||||
|
||||
QString info = QString(tr("PolarFlow authorization was successful."));
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
|
||||
} else if (site == STRAVA) {
|
||||
|
||||
service->setSetting(GC_STRAVA_TOKEN, access_token);
|
||||
QString info = QString(tr("Strava authorization was successful."));
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
|
||||
} else if (site == CYCLING_ANALYTICS) {
|
||||
|
||||
service->setSetting(GC_CYCLINGANALYTICS_TOKEN, access_token);
|
||||
QString info = QString(tr("Cycling Analytics authorization was successful."));
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
|
||||
} else if (site == KENTUNI) {
|
||||
|
||||
service->setSetting(GC_UOK_GOOGLE_DRIVE_REFRESH_TOKEN, refresh_token);
|
||||
service->setSetting(GC_UOK_GOOGLE_DRIVE_ACCESS_TOKEN, access_token);
|
||||
service->setSetting(GC_UOK_GOOGLE_DRIVE_LAST_ACCESS_TOKEN_REFRESH, QDateTime::currentDateTime());
|
||||
QString info = QString(tr("Kent University Google Drive authorization was successful."));
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
|
||||
} else if (site == GOOGLE_DRIVE) {
|
||||
|
||||
service->setSetting(GC_GOOGLE_DRIVE_REFRESH_TOKEN, refresh_token);
|
||||
service->setSetting(GC_GOOGLE_DRIVE_ACCESS_TOKEN, access_token);
|
||||
service->setSetting(GC_GOOGLE_DRIVE_LAST_ACCESS_TOKEN_REFRESH, QDateTime::currentDateTime());
|
||||
QString info = QString(tr("Google Drive authorization was successful."));
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
|
||||
} else if (site == TODAYSPLAN) {
|
||||
service->setSetting(GC_TODAYSPLAN_TOKEN, access_token);
|
||||
QString info = QString(tr("Today's Plan authorization was successful."));
|
||||
QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
information.exec();
|
||||
} else if (site == XERT) {
|
||||
|
||||
service->setSetting(GC_XERT_TOKEN, access_token);
|
||||
service->setSetting(GC_XERT_REFRESH_TOKEN, refresh_token);
|
||||
|
||||
// Try without Message Box
|
||||
|
||||
//QString info = QString(tr("Xert authorization was successful."));
|
||||
//QMessageBox information(QMessageBox::Information, tr("Information"), info);
|
||||
//information.exec();
|
||||
|
||||
service->message = "Xert authorization was successful.";
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// general error getting response
|
||||
QString error = QString(tr("Error retrieving access token, %1 (%2)")).arg(reply->errorString()).arg(reply->error());
|
||||
QMessageBox oautherr(QMessageBox::Critical, tr("SSL Token Refresh Error"), error);
|
||||
oautherr.setDetailedText(error);
|
||||
oautherr.exec();
|
||||
}
|
||||
|
||||
// job done, dialog can be closed
|
||||
//accept();
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Justin F. Knotzke (jknotzke@shampoo.ca)
|
||||
*
|
||||
* 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., 51
|
||||
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef OAUTHMANAGER_H
|
||||
#define OAUTHMANAGER_H
|
||||
#include "GoldenCheetah.h"
|
||||
#include "Pages.h"
|
||||
#include "CloudService.h"
|
||||
#ifdef GC_HAVE_KQOAUTH
|
||||
#include <kqoauthmanager.h>
|
||||
#endif
|
||||
#include <QObject>
|
||||
#include <QtGui>
|
||||
#include <QWidget>
|
||||
#include <QStackedLayout>
|
||||
#include <QUrl>
|
||||
#include <QSslSocket>
|
||||
#ifndef NOWEBKIT
|
||||
#include <QtWebKit>
|
||||
#include <QWebView>
|
||||
#include <QWebFrame>
|
||||
#endif
|
||||
|
||||
// QUrl split into QUrlQuerty in QT5
|
||||
#if QT_VERSION > 0x050000
|
||||
#include <QUrlQuery>
|
||||
#endif
|
||||
// QWebEngine if on Mac, -or- we don't have webkit
|
||||
#if defined(NOWEBKIT) || ((QT_VERSION > 0x050000) && defined(Q_OS_MAC))
|
||||
#include <QWebEngineHistory>
|
||||
#include <QWebEngineHistoryItem>
|
||||
#include <QWebEnginePage>
|
||||
#include <QWebEngineView>
|
||||
#include <QWebEngineProfile>
|
||||
#if (QT_VERSION >= 0x050600)
|
||||
#include <QWebEngineCookieStore>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
class OAuthManager : QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
G_OBJECT
|
||||
|
||||
public:
|
||||
typedef enum {
|
||||
NONE=0,
|
||||
STRAVA,
|
||||
DROPBOX,
|
||||
CYCLING_ANALYTICS,
|
||||
GOOGLE_DRIVE,
|
||||
SPORTTRACKS,
|
||||
TODAYSPLAN,
|
||||
WITHINGS,
|
||||
POLAR,
|
||||
KENTUNI,
|
||||
XERT
|
||||
} OAuthSite;
|
||||
|
||||
// will work with old config via site and new via cloudservice (which is null for calendar and withings for now)
|
||||
OAuthManager(Context *context, OAuthSite site, CloudService *service, QString baseURL="", QString clientsecret="");
|
||||
|
||||
void authorize();
|
||||
void getTokenWithCode(QString code);
|
||||
bool sslLibMissing() { return noSSLlib; }
|
||||
|
||||
private slots:
|
||||
// Strava/Cyclinganalytics/Google
|
||||
|
||||
void loadFinished(bool ok);
|
||||
void networkRequestFinished(QNetworkReply *reply);
|
||||
void onSslErrors(QNetworkReply *reply, const QList<QSslError>&error);
|
||||
|
||||
#ifdef GC_HAVE_KQOAUTH
|
||||
void onTemporaryTokenReceived(QString, QString);
|
||||
void onAuthorizationReceived(QString, QString);
|
||||
void onAccessTokenReceived(QString token, QString tokenSecret);
|
||||
void onAuthorizedRequestDone();
|
||||
void onRequestReady(QByteArray response);
|
||||
void onAuthorizationPageRequested (QUrl pageUrl);
|
||||
#endif
|
||||
|
||||
|
||||
private:
|
||||
Context *context;
|
||||
bool noSSLlib;
|
||||
bool ignore;
|
||||
OAuthSite site;
|
||||
CloudService *service;
|
||||
QString baseURL; // can be passed, but typically is blank (used by Todays Plan)
|
||||
QString clientsecret; // can be passed, but typicall is blank (used by Todays Plan)
|
||||
|
||||
QVBoxLayout *layout;
|
||||
|
||||
// QUrl split into QUrlQuerty in QT5
|
||||
#if defined(NOWEBKIT) || ((QT_VERSION > 0x050000) && defined(Q_OS_MAC))
|
||||
QWebEngineView *view;
|
||||
#else
|
||||
QWebView *view;
|
||||
#endif
|
||||
|
||||
QNetworkAccessManager* manager;
|
||||
|
||||
QUrl url;
|
||||
|
||||
#ifdef GC_HAVE_KQOAUTH
|
||||
KQOAuthManager *oauthManager;
|
||||
KQOAuthRequest *oauthRequest;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // OAUTHMANAGER_H
|
||||
@@ -1,4 +0,0 @@
|
||||
date,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
|
||||
2017-01-01T12:00:00Z,2,3,4,5,6,7,Useless data - but valid format
|
||||
2017-01-02T12:00:00Z,80.15,12.10,5.4,0,32.5,11.57,Test Value
|
||||
2017-01-03T12:00:00Z,81.15,12.10,5.4,0,32.5,12.57,"Here the comment text contain commas itself, so it needs quotes"
|
||||
|
@@ -1,4 +0,0 @@
|
||||
ts,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
|
||||
1,2,3,4,5,6,7,Is a technically valid entry but the timestamp is 1 second of Epoch so it will never be in the possible time range
|
||||
1479632565,80.15,12.10,5.4,0,32.5,11.57,Test Value
|
||||
1479632865,81.15,12.10,5.4,0,32.5,12.57,"Here the comment text contain commas itself, so it needs quotes"
|
||||
|
@@ -1,59 +0,0 @@
|
||||
Description of File-Format for "Body Measures - .CSV Import" function
|
||||
---------------------------------------------------------------------
|
||||
|
||||
Example for Timestamp is provided here:
|
||||
|
||||
----
|
||||
ts,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
|
||||
1,2,3,4,5,6,7,Is a technically valid entry, but the timestamp is 1 second of Epoch
|
||||
1479632565,80.15,12.10,5.4,0,32.5,1157,Test Value
|
||||
1479632865,80.15,12.10,5.4,0,32.5,1157,"Here the comment text contain commas itself, so it needs quotes"
|
||||
|
||||
----
|
||||
Example for Date/Time is provided here:
|
||||
|
||||
date,weightkg,fatkg,boneskg,musclekg,leankg,fatpercent,comment
|
||||
2017-01-01T12:00:00Z,2,3,4,5,6,7,Useless data - but valid format
|
||||
2017-01-02T12:00:00Z,80.15,12.10,5.4,0,32.5,1157,Test Value
|
||||
2017-01-02T12:00:00Z,80.15,12.10,5.4,0,32.5,1157,"Here the comment text contain commas itself, so it needs quotes"
|
||||
|
||||
|
||||
a) Header Line
|
||||
First Line of a valid file has to contain header information. This has to be a comma-separated
|
||||
list of the measures provided in the data lines. You do not need to provide all possible measures,
|
||||
but at least "ts" and "weightkg".
|
||||
|
||||
As mandatory field you need time/date of the measures. There are 2 options:
|
||||
- "ts" - Unix timestamp (in seconds) since Epoch. The value can be calcuated in XLS - just Google how-to.
|
||||
OR
|
||||
- "date" - ISO8601 formatted Date/Time String
|
||||
|
||||
The mandatory measure is:
|
||||
- "weightkg" - Athlete weight in kilograms
|
||||
|
||||
The optional measures are:
|
||||
- "fatkg" - Fat mass in kg
|
||||
- "boneskg" - Bone mass in kg
|
||||
- "musclekg" - Muscle mass in kg
|
||||
- "leankg" - Lean mass in kg
|
||||
- "fatpercent" - Fat percentage
|
||||
- "comment" - A description of the measures
|
||||
|
||||
Data formats:
|
||||
|
||||
- "ts" is a 64Bit Integer - any other time will be rejected
|
||||
- "date" is a ISO8601 formated date/time
|
||||
- "comment" is a String - if it contains "commas" itself the string must be enclosed by "string with comma, and more comment"
|
||||
- all other measures are float/double in format <xxxxxxx.yyy>
|
||||
|
||||
There are very strict format checks when importing. Only if the whole
|
||||
file is correct the data is imported. Any error (even in a single entry)
|
||||
will block the complete import.
|
||||
|
||||
The following error will stop the import.
|
||||
- Unknown measures in header
|
||||
- Number of measures in header is different to number in an item
|
||||
- Item measure in wrong format
|
||||
- "ts" or "weightkg" is missing
|
||||
|
||||
|
||||
@@ -23,10 +23,6 @@ isinf(p1)
|
||||
isnan(p1)
|
||||
|
||||
LIST / VECTOR REDUCE FUNCTIONS
|
||||
Vectors are about collecting data from across a date range, specified as
|
||||
expression[[Date_From:Date_To]]
|
||||
where dates can be absolute s.t. 2017/09/19 or relative s.t. Date-7
|
||||
you can use them within a formula using the following functions:
|
||||
sum(...)
|
||||
mean(...)
|
||||
max(...)
|
||||
@@ -81,9 +77,6 @@ DATA PROCESSOR FUNCTIONS
|
||||
autoprocess(filter)
|
||||
postprocess(processor, filter)
|
||||
|
||||
DAILY MEASURES ACCESS
|
||||
measure(date, "group", "field")
|
||||
|
||||
RIDE SAMPLE DATA
|
||||
|
||||
SECS
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
Description of File-Format for "HRV Measures - .CSV Import" function
|
||||
---------------------------------------------------------------------
|
||||
|
||||
Example file:
|
||||
|
||||
timestamp_measurement,HR,AVNN,SDNN,rMSSD,pNN50,LF,HF,HRV4T_Recovery_Points
|
||||
2017-04-01 09:56:21 +0000,43.92,1364.75,84.05,67.54,40.48,0.0418,0.0339,8.37
|
||||
2017-04-02 09:27:24 +0000,43.42,1348,59.46,56.38,32.56,0.0655,0.0331,8.09
|
||||
2017-04-03 07:37:03 +0000,41.38,1502.73,76.92,72.04,60.53,0.0476,0.0516,8.47
|
||||
|
||||
this was generated by HRV4Training App with unsupported columns and empty lines removed using a spreasheet.
|
||||
|
||||
a) Header Line
|
||||
First Line of a valid file has to contain header information. This has to be a comma-separated
|
||||
list of the measures provided in the data lines. You do not need to provide all possible measures,
|
||||
but at least "timestamp_measure" and "rMSSD".
|
||||
|
||||
As mandatory field you need time/date of the measures:
|
||||
- "timestamp_measure" - ISO8601 formatted Date/Time String
|
||||
|
||||
The mandatory measure is:
|
||||
- "rMSSD" - Square root of the mean of the squares of differences between adjacent NN intervals
|
||||
|
||||
The optional measures are:
|
||||
- "HR" - Average HR
|
||||
- "AVNN" - Average of all NN interval
|
||||
- "SDNN" - Standard deviation of all NN intervals
|
||||
- "pNN50" - Percentage of differences between adjacent NN intervals that are greater than 50 ms
|
||||
- "LF" - Power at Low Frequencies
|
||||
- "HF" - Power at High Frequencies
|
||||
- "HRV4T_Recovery_Points" - A log transform of rMSSD
|
||||
|
||||
Data formats:
|
||||
- "date" is a ISO8601 formated date/time
|
||||
- all other measures are float/double in format <xxxxxxx.yyy>
|
||||
|
||||
There are very strict format checks when importing. Only if the whole
|
||||
file is correct the data is imported. Any error (even in a single entry)
|
||||
will block the complete import.
|
||||
|
||||
The following error will stop the import.
|
||||
- Unknown measures in header
|
||||
- Number of measures in header is different to number in an item
|
||||
- Item measure in wrong format
|
||||
- "timestamp_measure" or "rMSSD" is missing
|
||||
|
||||
|
||||
@@ -31,8 +31,3 @@ http://localhost:12021 Default base URL
|
||||
before=yyyy/mm/dd
|
||||
series=xxx where xxx is one of watts,hr,cad,speed,nm,vam.xPower,NP
|
||||
Returns an aggregate of the best mean maximal values over the date range
|
||||
|
||||
/<athlete>/measures List supported measures groups (Body/Hrv for now)
|
||||
/<athlete>/measures/<group> Fetch Measures from <group> for a Date Range
|
||||
since=yyyy/mm/dd
|
||||
before=yyyy/mm/dd
|
||||
|
||||
|
Before Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 233 KiB |
|
Before Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 42 KiB |
|
Before Width: | Height: | Size: 107 KiB |
|
Before Width: | Height: | Size: 141 KiB |
|
Before Width: | Height: | Size: 154 KiB |
|
Before Width: | Height: | Size: 276 KiB |
|
Before Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 81 KiB |
|
Before Width: | Height: | Size: 186 KiB |
|
Before Width: | Height: | Size: 67 KiB |
|
Before Width: | Height: | Size: 178 KiB |
|
Before Width: | Height: | Size: 173 KiB |
|
Before Width: | Height: | Size: 312 KiB |
@@ -228,7 +228,6 @@ void KQOAuthManager::executeRequest(KQOAuthRequest *request) {
|
||||
urlWithParams.setQuery(query);
|
||||
#endif
|
||||
networkRequest.setUrl(urlWithParams);
|
||||
qDebug() << "urlWithParams:" << urlWithParams;
|
||||
|
||||
// Submit the request including the params.
|
||||
QNetworkReply *reply = d->networkManager->get(networkRequest);
|
||||
75
levmar/Axb.c
@@ -1,75 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 <string.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 */
|
||||
1282
levmar/Axb_core.c
@@ -1,49 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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
|
||||
#define LM_ISINF(x) (!_finite(x) && !_isnan(x)) // MSVC
|
||||
#elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(__GNUC__)
|
||||
#define LM_ISINF(x) isinf(x) // ICC, GCC
|
||||
#else
|
||||
#define LM_ISINF(x) isinf(x) // other than MSVC, ICC, GCC, let's hope this will work
|
||||
#endif
|
||||
|
||||
#endif /* _COMPILER_H_ */
|
||||
398
levmar/levmar.h
@@ -1,398 +0,0 @@
|
||||
/*
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 *************************************/
|
||||
/* Note that when compiling with CMake, this configuration section is automatically generated
|
||||
* based on the user's input, see levmar.h.in
|
||||
*/
|
||||
|
||||
/* specifies whether to use LAPACK or not. Using LAPACK is strongly recommended */
|
||||
//#define HAVE_LAPACK
|
||||
|
||||
/* specifies whether the PLASMA parallel library for multicore CPUs is available */
|
||||
/* #undef HAVE_PLASMA */
|
||||
|
||||
/* 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, an attempt is made to issue a warning if this option is turned on and OpenMP
|
||||
* is being used (note that this will work only if omp.h is included before levmar.h)
|
||||
*/
|
||||
#define LINSOLVERS_RETAIN_MEMORY
|
||||
#if (defined(_OPENMP))
|
||||
# ifdef LINSOLVERS_RETAIN_MEMORY
|
||||
# ifdef _MSC_VER
|
||||
# pragma message("LINSOLVERS_RETAIN_MEMORY is not safe in a multithreaded environment and should be turned off!")
|
||||
# else
|
||||
# warning LINSOLVERS_RETAIN_MEMORY is not safe in a multithreaded environment and should be turned off!
|
||||
# endif /* _MSC_VER */
|
||||
# endif /* LINSOLVERS_RETAIN_MEMORY */
|
||||
#endif /* _OPENMP */
|
||||
|
||||
/* specifies whether double precision routines will be compiled or not */
|
||||
#define LM_DBL_PREC
|
||||
/* specifies whether single precision routines will be compiled or not */
|
||||
#define LM_SNGL_PREC
|
||||
|
||||
/****************** End of configuration options, no changes necessary beyond this point ******************/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 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.6 (November 2011)"
|
||||
|
||||
#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, double *dscl,
|
||||
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, double *dscl,
|
||||
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, float *dscl,
|
||||
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, float *dscl,
|
||||
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 */
|
||||
|
||||
#ifdef HAVE_PLASMA
|
||||
#ifdef LM_DBL_PREC
|
||||
extern int dAx_eq_b_PLASMA_Chol(double *A, double *B, double *x, int m);
|
||||
#endif
|
||||
#ifdef LM_SNGL_PREC
|
||||
extern int sAx_eq_b_PLASMA_Chol(float *A, float *B, float *x, int m);
|
||||
#endif
|
||||
extern void levmar_PLASMA_setnbcores(int cores);
|
||||
#endif /* HAVE_PLASMA */
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* miscellaneous: 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);
|
||||
|
||||
extern void slevmar_locscale(
|
||||
void (*func)(float *p, float *hx, int m, int n, void *adata),
|
||||
float *p, float *x, int m, int n, void *adata,
|
||||
int howto, float locscl[2], float **residptr);
|
||||
|
||||
extern int slevmar_outlid(float *r, int n, float thresh, float ls[2], char *outlmap);
|
||||
|
||||
#endif /* LM_SNGL_PREC */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _LEVMAR_H_ */
|
||||
83
levmar/lm.c
@@ -1,83 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 */
|
||||
11
levmar/lm.h
@@ -1,11 +0,0 @@
|
||||
#ifndef _DEPR_LM_H_
|
||||
#define _DEPR_LM_H_
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma message("lm.h is deprecated, please use levmar.h instead!")
|
||||
#else
|
||||
#error lm.h is deprecated, please use levmar.h instead!
|
||||
#endif /* _MSC_VER */
|
||||
|
||||
#endif /* _DEPR_LM_H_ */
|
||||
|
||||
858
levmar/lm_core.c
@@ -1,858 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 */
|
||||
|
||||
#ifdef HAVE_PLASMA
|
||||
#define AX_EQ_B_PLASMA_CHOL LM_ADD_PREFIX(Ax_eq_b_PLASMA_Chol)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 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;
|
||||
register LM_REAL alpha, *jaclm, *jacTjacim;
|
||||
|
||||
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; ){
|
||||
jacTjacim=jacTjac+i*m;
|
||||
alpha=jaclm[i]; //jac[l*m+i];
|
||||
for(j=i+1; j-->0; ) /* j<=i computes lower triangular part only */
|
||||
jacTjacim[j]+=jaclm[j]*alpha; //jacTjac[i*m+j]+=jac[l*m+j]*alpha
|
||||
|
||||
/* 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
|
||||
/* 7 alternatives are available: LU, Cholesky + Cholesky with PLASMA, LDLt, 2 variants of QR decomposition and SVD.
|
||||
* For matrices with dimensions of at least a few hundreds, the PLASMA implementation of Cholesky is the fastest.
|
||||
* From the serial solvers, Cholesky is the fastest but might occasionally be inapplicable due to numerical round-off;
|
||||
* QR is slower but more robust; SVD is the slowest but most robust; LU is quite robust but
|
||||
* slower than LDLt; LDLt offers a good tradeoff between robustness 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;
|
||||
#ifdef HAVE_PLASMA
|
||||
//issolved=AX_EQ_B_PLASMA_CHOL(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_PLASMA_CHOL;
|
||||
#endif
|
||||
//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;
|
||||
register LM_REAL alpha, *jaclm, *jacTjacim;
|
||||
|
||||
/* 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; ){
|
||||
jacTjacim=jacTjac+i*m;
|
||||
alpha=jaclm[i]; //jac[l*m+i];
|
||||
for(j=i+1; j-->0; ) /* j<=i computes lower triangular part only */
|
||||
jacTjacim[j]+=jaclm[j]*alpha; //jacTjac[i*m+j]+=jac[l*m+j]*alpha
|
||||
|
||||
/* 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
|
||||
/* 7 alternatives are available: LU, Cholesky + Cholesky with PLASMA, LDLt, 2 variants of QR decomposition and SVD.
|
||||
* For matrices with dimensions of at least a few hundreds, the PLASMA implementation of Cholesky is the fastest.
|
||||
* From the serial solvers, Cholesky is the fastest but might occasionally be inapplicable due to numerical round-off;
|
||||
* QR is slower but more robust; SVD is the slowest but most robust; LU is quite robust but
|
||||
* slower than LDLt; LDLt offers a good tradeoff between robustness 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;
|
||||
#ifdef HAVE_PLASMA
|
||||
//issolved=AX_EQ_B_PLASMA_CHOL(jacTjac, jacTe, Dp, m); ++nlss; linsolver=AX_EQ_B_PLASMA_CHOL;
|
||||
#endif
|
||||
//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_PLASMA_CHOL
|
||||
#undef AX_EQ_B_QR
|
||||
#undef AX_EQ_B_QRLS
|
||||
#undef AX_EQ_B_SVD
|
||||
#undef AX_EQ_B_BK
|
||||
@@ -1,87 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 */
|
||||
#define _LSITMAX_ 150 /* max #iterations for line search */
|
||||
#define _POW_ 2.1
|
||||
|
||||
#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 */
|
||||
1154
levmar/lmbc_core.c
@@ -1,87 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 */
|
||||
@@ -1,413 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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
|
||||
@@ -1,89 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 */
|
||||
|
||||
@@ -1,506 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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
|
||||
@@ -1,80 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 */
|
||||
|
||||
@@ -1,656 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
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);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 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 the lower part of r (thus transposing it). R is rank x rank */
|
||||
for(j=0; j<rank; ++j){
|
||||
for(i=0; i<=j; ++i)
|
||||
r[j+i*rank]=a[i+j*tm];
|
||||
for(i=j+1; i<rank; ++i)
|
||||
r[j+i*rank]=0.0; // upper part is zero
|
||||
}
|
||||
/* r now contains R^T */
|
||||
|
||||
/* compute the inverse */
|
||||
TRTRI("L", "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;
|
||||
}
|
||||
|
||||
/* 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
|
||||
@@ -1,70 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 */
|
||||
114
levmar/misc.h
@@ -1,114 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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))
|
||||
|
||||
#ifdef LM_BLAS_PREFIX
|
||||
#define LM_MK_BLAS_NAME(s) LM_CAT_(LM_BLAS_PREFIX, LM_ADD_PREFIX(LM_CAT_(s, LM_BLAS_SUFFIX)))
|
||||
#else
|
||||
#define LM_MK_BLAS_NAME(s) LM_ADD_PREFIX(LM_CAT_(s, LM_BLAS_SUFFIX))
|
||||
#endif
|
||||
|
||||
|
||||
#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)
|
||||
|
||||
#define FABS(x) (((x)>=0.0)? (x) : -(x))
|
||||
|
||||
#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_ */
|
||||
@@ -1,826 +0,0 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/* BLAS matrix multiplication, LAPACK SVD & Cholesky routines */
|
||||
#define GEMM LM_MK_BLAS_NAME(gemm)
|
||||
/* 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);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#define LEVMAR_CHOLESKY LM_ADD_PREFIX(levmar_chol)
|
||||
|
||||
#else /* !HAVE_LAPACK */
|
||||
#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=n, tmp=0.0; i-->0; )
|
||||
tmp+=x[i];
|
||||
xavg=tmp/(LM_REAL)n;
|
||||
|
||||
if(x)
|
||||
for(i=n, SSerr=SStot=0.0; i-->0; ){
|
||||
tmp=x[i]-hx[i];
|
||||
SSerr+=tmp*tmp;
|
||||
|
||||
tmp=x[i]-xavg;
|
||||
SStot+=tmp*tmp;
|
||||
}
|
||||
else /* x==0 */
|
||||
for(i=n, SSerr=SStot=0.0; i-->0; ){
|
||||
tmp=-hx[i];
|
||||
SSerr+=tmp*tmp;
|
||||
|
||||
tmp=-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("L", (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 lower part of W (in column-major order!).
|
||||
* zeroing the upper part makes it lower triangular which is equivalent to
|
||||
* upper triangular in row-major order
|
||||
*/
|
||||
for(i=0; i<m; i++)
|
||||
for(j=i+1; j<m; j++)
|
||||
W[i+j*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 POTF2
|
||||
#undef GESVD
|
||||
#undef GESDD
|
||||
#undef GEMM
|
||||
#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
|
||||
@@ -1,30 +0,0 @@
|
||||
The package lmfit is distributed under the FreeBSD License:
|
||||
|
||||
--
|
||||
Copyright (c) 1980-1999 University of Chicago,
|
||||
as operator of Argonne National Laboratory
|
||||
Copyright (c) 2004-2015 Joachim Wuttke, Forschungszentrum Juelich GmbH
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
- Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
This software is provided by the copyright holders and contributors "as is"
|
||||
and any express or implied warranties, including, but not limited to, the
|
||||
implied warranties of merchantability and fitness for a particular purpose
|
||||
are disclaimed. In no event shall the copyright holder or contributors
|
||||
be liable for any direct, indirect, incidental, special, exemplary, or
|
||||
consequential damages (including, but not limited to, procurement of
|
||||
substitute goods or services; loss of use, data, or profits; or business
|
||||
interruption) however caused and on any theory of liability, whether in
|
||||
contract, strict liability, or tort (including negligence or otherwise)
|
||||
arising in any way out of the use of this software, even if advised of the
|
||||
possibility of such damage.
|
||||
--
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Library: lmfit (Levenberg-Marquardt least squares fitting)
|
||||
*
|
||||
* File: lmcurve.c
|
||||
*
|
||||
* Contents: Implements lmcurve, a simplified API for curve fitting
|
||||
* using the generic Levenberg-Marquardt routine lmmin.
|
||||
*
|
||||
* Copyright: Joachim Wuttke, Forschungszentrum Juelich GmbH (2004-2013)
|
||||
*
|
||||
* License: see ../COPYING (FreeBSD)
|
||||
*
|
||||
* Homepage: apps.jcns.fz-juelich.de/lmfit
|
||||
*
|
||||
* Note to programmers: Don't patch and fork, but copy and modify!
|
||||
* If you need to compute residues differently, then please do not patch
|
||||
* lmcurve.h and lmcurve.c, but copy them, and create differently named
|
||||
* versions of lmcurve_data_struct, lmcurve_evaluate, and lmcurve of your own.
|
||||
*/
|
||||
|
||||
#include "lmmin.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
const double *const t;
|
||||
const double *const y;
|
||||
double (*const g) (const double t, const double *par);
|
||||
} lmcurve_data_struct;
|
||||
|
||||
|
||||
void lmcurve_evaluate(
|
||||
const double *const par, const int m_dat, const void *const data,
|
||||
double *const fvec, int *const info)
|
||||
{
|
||||
for (int i = 0; i < m_dat; i++ )
|
||||
fvec[i] =
|
||||
((lmcurve_data_struct*)data)->y[i] -
|
||||
((lmcurve_data_struct*)data)->g(
|
||||
((lmcurve_data_struct*)data)->t[i], par );
|
||||
}
|
||||
|
||||
|
||||
void lmcurve(
|
||||
const int n_par, double *const par, const int m_dat,
|
||||
const double *const t, const double *const y,
|
||||
double (*const g)(const double t, const double *const par),
|
||||
const lm_control_struct *const control, lm_status_struct *const status)
|
||||
{
|
||||
lmcurve_data_struct data = {t, y, g};
|
||||
lmmin(n_par, par, m_dat, NULL, (const void *const) &data,
|
||||
lmcurve_evaluate, control, status);
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Library: lmfit (Levenberg-Marquardt least squares fitting)
|
||||
*
|
||||
* File: lmcurve.h
|
||||
*
|
||||
* Contents: Declares lmcurve, a simplified API for curve fitting
|
||||
* using the generic Levenberg-Marquardt routine lmmin.
|
||||
*
|
||||
* Copyright: Joachim Wuttke, Forschungszentrum Juelich GmbH (2004-2013)
|
||||
*
|
||||
* License: see ../COPYING (FreeBSD)
|
||||
*
|
||||
* Homepage: apps.jcns.fz-juelich.de/lmfit
|
||||
*
|
||||
* Note to programmers: Don't patch and fork, but copy and variate!
|
||||
* If you need to compute residues differently, then please do not patch
|
||||
* lmcurve.h, but copy it to a differently named file, and change lmcurve()
|
||||
* into a differently named function declaration, like we have done in
|
||||
* lmcurve_tyd.h.
|
||||
*/
|
||||
|
||||
#ifndef LMCURVE_H
|
||||
#define LMCURVE_H
|
||||
#undef __BEGIN_DECLS
|
||||
#undef __END_DECLS
|
||||
#ifdef __cplusplus
|
||||
#define __BEGIN_DECLS extern "C" {
|
||||
#define __END_DECLS }
|
||||
#else
|
||||
#define __BEGIN_DECLS /* empty */
|
||||
#define __END_DECLS /* empty */
|
||||
#endif
|
||||
|
||||
#include<lmstruct.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
void lmcurve(
|
||||
const int n_par, double* par, const int m_dat,
|
||||
const double* t, const double* y,
|
||||
double (*g)(const double t, const double* par),
|
||||
const lm_control_struct* control, lm_status_struct* status);
|
||||
|
||||
__END_DECLS
|
||||
#endif /* LMCURVE_H */
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Library: lmfit (Levenberg-Marquardt least squares fitting)
|
||||
*
|
||||
* File: lmcurve_tyd.h
|
||||
*
|
||||
* Contents: Declares lmcurve_tyd(), a variant of lmcurve() that weighs
|
||||
* data points y(t) with the inverse of the standard deviations dy.
|
||||
*
|
||||
* Copyright: Joachim Wuttke, Forschungszentrum Juelich GmbH (2004-2013)
|
||||
*
|
||||
* License: see ../COPYING (FreeBSD)
|
||||
*
|
||||
* Homepage: apps.jcns.fz-juelich.de/lmfit
|
||||
*/
|
||||
|
||||
#ifndef LMCURVETYD_H
|
||||
#define LMCURVETYD_H
|
||||
#undef __BEGIN_DECLS
|
||||
#undef __END_DECLS
|
||||
#ifdef __cplusplus
|
||||
#define __BEGIN_DECLS extern "C" {
|
||||
#define __END_DECLS }
|
||||
#else
|
||||
#define __BEGIN_DECLS /* empty */
|
||||
#define __END_DECLS /* empty */
|
||||
#endif
|
||||
|
||||
#include <lmstruct.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
void lmcurve_tyd(
|
||||
const int n_par, double* par, const int m_dat,
|
||||
const double* t, const double* y, const double* dy,
|
||||
double (*f)(double t, const double* par),
|
||||
const lm_control_struct* control, lm_status_struct* status);
|
||||
|
||||
__END_DECLS
|
||||
#endif /* LMCURVETYD_H */
|
||||
1283
lmfit/lmmin.c
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Library: lmfit (Levenberg-Marquardt least squares fitting)
|
||||
*
|
||||
* File: lmmin.h
|
||||
*
|
||||
* Contents: Declarations for Levenberg-Marquardt minimization.
|
||||
*
|
||||
* Copyright: Joachim Wuttke, Forschungszentrum Juelich GmbH (2004-2013)
|
||||
*
|
||||
* License: see ../COPYING (FreeBSD)
|
||||
*
|
||||
* Homepage: apps.jcns.fz-juelich.de/lmfit
|
||||
*/
|
||||
|
||||
#ifndef LMMIN_H
|
||||
#define LMMIN_H
|
||||
#undef __BEGIN_DECLS
|
||||
#undef __END_DECLS
|
||||
#ifdef __cplusplus
|
||||
#define __BEGIN_DECLS extern "C" {
|
||||
#define __END_DECLS }
|
||||
#else
|
||||
#define __BEGIN_DECLS /* empty */
|
||||
#define __END_DECLS /* empty */
|
||||
#endif
|
||||
|
||||
#include "lmstruct.h"
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
/* Levenberg-Marquardt minimization. */
|
||||
void lmmin(
|
||||
const int n_par, double* par, const int m_dat, const double* y,
|
||||
const void* data,
|
||||
void (*evaluate)(
|
||||
const double* par, const int m_dat, const void* data,
|
||||
double* fvec, int* userbreak),
|
||||
const lm_control_struct* control, lm_status_struct* status);
|
||||
/*
|
||||
* This routine contains the core algorithm of our library.
|
||||
*
|
||||
* It minimizes the sum of the squares of m nonlinear functions
|
||||
* in n variables by a modified Levenberg-Marquardt algorithm.
|
||||
* The function evaluation is done by the user-provided routine 'evaluate'.
|
||||
* The Jacobian is then calculated by a forward-difference approximation.
|
||||
*
|
||||
* Parameters:
|
||||
*
|
||||
* n_par is the number of variables (INPUT, positive integer).
|
||||
*
|
||||
* par is the solution vector (INPUT/OUTPUT, array of length n).
|
||||
* On input it must be set to an estimated solution.
|
||||
* On output it yields the final estimate of the solution.
|
||||
*
|
||||
* m_dat is the number of functions to be minimized (INPUT, positive integer).
|
||||
* It must fulfill m>=n.
|
||||
*
|
||||
* y contains data to be fitted. Use a null pointer if there are no data.
|
||||
*
|
||||
* data is a pointer that is ignored by lmmin; it is however forwarded
|
||||
* to the user-supplied functions evaluate and printout.
|
||||
* In a typical application, it contains experimental data to be fitted.
|
||||
*
|
||||
* evaluate is a user-supplied function that calculates the m functions.
|
||||
* Parameters:
|
||||
* n, x, m, data as above.
|
||||
* fvec is an array of length m; on OUTPUT, it must contain the
|
||||
* m function values for the parameter vector x.
|
||||
* userbreak is an integer pointer. When *userbreak is set to a
|
||||
* nonzero value, lmmin will terminate.
|
||||
*
|
||||
* control contains INPUT variables that control the fit algorithm,
|
||||
* as declared and explained in lmstruct.h
|
||||
*
|
||||
* status contains OUTPUT variables that inform about the fit result,
|
||||
* as declared and explained in lmstruct.h
|
||||
*/
|
||||
|
||||
/* Refined calculation of Eucledian norm. */
|
||||
double lm_enorm(const int, const double*);
|
||||
double lm_fnorm(const int, const double*, const double*);
|
||||
|
||||
__END_DECLS
|
||||
#endif /* LMMIN_H */
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Library: lmfit (Levenberg-Marquardt least squares fitting)
|
||||
*
|
||||
* File: lmstruct.h
|
||||
*
|
||||
* Contents: Declarations of parameter records, used in lmmin.h and lmcurve.h
|
||||
*
|
||||
* Copyright: Joachim Wuttke, Forschungszentrum Juelich GmbH (2004-2013)
|
||||
*
|
||||
* License: see ../COPYING (FreeBSD)
|
||||
*
|
||||
* Homepage: apps.jcns.fz-juelich.de/lmfit
|
||||
*/
|
||||
|
||||
#ifndef LMSTRUCT_H
|
||||
#define LMSTRUCT_H
|
||||
#undef __BEGIN_DECLS
|
||||
#undef __END_DECLS
|
||||
#ifdef __cplusplus
|
||||
#define __BEGIN_DECLS extern "C" {
|
||||
#define __END_DECLS }
|
||||
#else
|
||||
#define __BEGIN_DECLS /* empty */
|
||||
#define __END_DECLS /* empty */
|
||||
#endif
|
||||
__BEGIN_DECLS
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* Collection of input parameters for fit control. */
|
||||
typedef struct {
|
||||
double ftol; /* Relative error desired in the sum of squares.
|
||||
Termination occurs when both the actual and
|
||||
predicted relative reductions in the sum of squares
|
||||
are at most ftol. */
|
||||
double xtol; /* Relative error between last two approximations.
|
||||
Termination occurs when the relative error between
|
||||
two consecutive iterates is at most xtol. */
|
||||
double gtol; /* Orthogonality desired between fvec and its derivs.
|
||||
Termination occurs when the cosine of the angle
|
||||
between fvec and any column of the Jacobian is at
|
||||
most gtol in absolute value. */
|
||||
double epsilon; /* Step used to calculate the Jacobian, should be
|
||||
slightly larger than the relative error in the
|
||||
user-supplied functions. */
|
||||
double stepbound; /* Used in determining the initial step bound. This
|
||||
bound is set to the product of stepbound and the
|
||||
Euclidean norm of diag*x if nonzero, or else to
|
||||
stepbound itself. In most cases stepbound should lie
|
||||
in the interval (0.1,100.0). Generally, the value
|
||||
100.0 is recommended. */
|
||||
int patience; /* Used to set the maximum number of function evaluations
|
||||
to patience*(number_of_parameters+1). */
|
||||
int scale_diag; /* If 1, the variables will be rescaled internally.
|
||||
Recommended value is 1. */
|
||||
FILE* msgfile; /* Progress messages will be written to this file. */
|
||||
int verbosity; /* OR'ed: 1: print some messages; 2: print Jacobian. */
|
||||
int n_maxpri; /* -1, or max number of parameters to print. */
|
||||
int m_maxpri; /* -1, or max number of residuals to print. */
|
||||
} lm_control_struct;
|
||||
|
||||
/* Collection of output parameters for status info. */
|
||||
typedef struct {
|
||||
double fnorm; /* norm of the residue vector fvec. */
|
||||
int nfev; /* actual number of iterations. */
|
||||
int outcome; /* Status indicator. Nonnegative values are used as index
|
||||
for the message text lm_infmsg, set in lmmin.c. */
|
||||
int userbreak; /* Set when function evaluation requests termination. */
|
||||
} lm_status_struct;
|
||||
|
||||
/* Preset (and recommended) control parameter settings. */
|
||||
extern const lm_control_struct lm_control_double;
|
||||
extern const lm_control_struct lm_control_float;
|
||||
|
||||
/* Preset message texts. */
|
||||
|
||||
extern const char* lm_infmsg[];
|
||||
extern const char* lm_shortmsg[];
|
||||
|
||||
__END_DECLS
|
||||
#endif /* LMSTRUCT_H */
|
||||
@@ -241,8 +241,8 @@ inline void MVJSONUtils::replace(string & target, ///< text to be modi
|
||||
)
|
||||
{
|
||||
size_t pos = 0;
|
||||
const size_t oldLen = oldStr.length();
|
||||
const size_t newLen = newStr.length();
|
||||
unsigned int oldLen = oldStr.length();
|
||||
unsigned int newLen = newStr.length();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
|
||||
@@ -38,11 +38,7 @@ void QwtPlotGappedCurve::drawSeries(QPainter *painter, const QwtScaleMap &xMap,
|
||||
// First non-missed point will be the start of curve section.
|
||||
double x = sample(i).x();
|
||||
double y = sample(i).y();
|
||||
double yprev = 0;
|
||||
if (i>0) yprev = sample(i-1).y();
|
||||
|
||||
if ((y < (naValue_ + -0.001) || y > (naValue_ + 0.001)) && (x - last <= gapValue_) &&
|
||||
(yprev < (naValue_ + -0.001) || yprev > (naValue_ + 0.001))) {
|
||||
if ((y < (naValue_ + -0.001) || y > (naValue_ + 0.001)) && x - last <= gapValue_) {
|
||||
|
||||
int start = i-1;
|
||||
int end = i;
|
||||
|
||||