Initial commit: ArgyllCMS v3.5.0 and multichannel profiling research
This commit is contained in:
40
Argyll_V3.5.0/numlib/Jamfile
Executable file
40
Argyll_V3.5.0/numlib/Jamfile
Executable file
@@ -0,0 +1,40 @@
|
||||
|
||||
# Jamfile for the numlib and other general utilities
|
||||
|
||||
# Optization state and configuration. Overridden by parent-Jamfiles
|
||||
|
||||
#PREF_CCFLAGS = $(CCOPTFLAG) ; # Turn optimisation on
|
||||
PREF_CCFLAGS = $(CCDEBUGFLAG) ; # Debugging flags
|
||||
PREF_LINKFLAGS = $(LINKDEBUGFLAG) ;
|
||||
#PREF_CCFLAGS = $(CCPROFFLAG) ; # Profile flags
|
||||
#PREF_LINKFLAGS = $(LINKPROFFLAG) ; # Profile flags
|
||||
|
||||
# Products
|
||||
Libraries = libnum libui ;
|
||||
Headers = numlib.h libui.h ;
|
||||
|
||||
#Install
|
||||
#InstallFile $(DESTDIR)$(PREFIX)/h : $(Headers) ;
|
||||
#InstallLib $(DESTDIR)$(PREFIX)/lib : $(Libraries) ;
|
||||
|
||||
HDRS = ../h ../plot ;
|
||||
|
||||
# Numeric library
|
||||
Library libnum.lib : numsup.c dnsq.c powell.c dhsx.c varmet.c ludecomp.c svd.c zbrent.c rand.c sobol.c aatree.c quadprog.c gnewt.c roots.c ;
|
||||
|
||||
# Link all utilities with libnum
|
||||
LINKLIBS = libnum ;
|
||||
|
||||
# All test programs are made from a single source file
|
||||
MainsFromSources dnsqtest.c tpowell.c tconjgrad.c tvarmet.c tdhsx.c LUtest.c svdtest.c zbrenttest.c soboltest.c qptest.c ;
|
||||
|
||||
# Compile .c as .m
|
||||
if $(OS) = MACOSX {
|
||||
ObjectCcFlags ui : -ObjC ;
|
||||
}
|
||||
|
||||
# UI setup library
|
||||
Library libui.lib : ui.c ;
|
||||
|
||||
#MainsFromSources t.c ;
|
||||
|
||||
113
Argyll_V3.5.0/numlib/LUtest.c
Normal file
113
Argyll_V3.5.0/numlib/LUtest.c
Normal file
@@ -0,0 +1,113 @@
|
||||
|
||||
/* Test the LU decomposition code */
|
||||
/*
|
||||
* Copyright 1999 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include "numlib.h"
|
||||
|
||||
int test(int n, double **a, double *b);
|
||||
|
||||
int main() {
|
||||
double **a;
|
||||
double *b;
|
||||
int n = 4;
|
||||
int rv;
|
||||
|
||||
a = dmatrix(0, n-1, 0, n-1);
|
||||
b = dvector(0, n-1);
|
||||
|
||||
a[0][0] = 1.0;
|
||||
a[0][1] = 2.0;
|
||||
a[0][2] = 3.1415926;
|
||||
a[0][3] = 5.1415926;
|
||||
|
||||
a[1][0] = 3.0;
|
||||
a[1][1] = 2.0;
|
||||
a[1][2] = 4.0;
|
||||
a[1][3] = -0.1;
|
||||
|
||||
a[2][0] = -1.0;
|
||||
a[2][1] = 0.5;
|
||||
a[2][2] = 1.0;
|
||||
a[2][3] = 1.5;
|
||||
|
||||
a[3][0] = 11.0;
|
||||
a[3][1] = 9.0;
|
||||
a[3][2] = 15.0;
|
||||
a[3][3] = 0.9;
|
||||
|
||||
b[0] = 6.0;
|
||||
b[1] = 4.0;
|
||||
b[2] = 4.5;
|
||||
b[3] = -10.0;
|
||||
|
||||
if ((rv = test(n, a, b)) != 0) {
|
||||
if (rv == 1)
|
||||
printf("LU test failed due to singularity\n");
|
||||
else {
|
||||
printf("LU test failed to verify\n");
|
||||
printf("Got solution %f %f %f %f\n",b[0],b[1],b[2],b[3]);
|
||||
}
|
||||
} else {
|
||||
printf("Got verified solution %f %f %f %f\n",b[0],b[1],b[2],b[3]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
test(
|
||||
int n, /* Dimensionality */
|
||||
double **a, /* A[][] input matrix, returns LU decimposition of A */
|
||||
double *b /* B[] input array, returns solution X[] */
|
||||
) {
|
||||
int i, j;
|
||||
double rip; /* Row interchange parity */
|
||||
int *pivx;
|
||||
int rv = 0;
|
||||
|
||||
double **sa; /* save input matrix values */
|
||||
double *sb; /* save input vector values */
|
||||
|
||||
pivx = ivector(0, n-1);
|
||||
sa = dmatrix(0, n-1, 0, n-1);
|
||||
sb = dvector(0, n-1);
|
||||
|
||||
/* Copy input matrix and vector values */
|
||||
for (i = 0; i < n; i++) {
|
||||
sb[i] = b[i];
|
||||
for (j = 0; j < n; j++)
|
||||
sa[i][j] = a[i][j];
|
||||
}
|
||||
|
||||
if (lu_decomp(a, n, pivx, &rip)) {
|
||||
free_dvector(sb, 0, n-1);
|
||||
free_dmatrix(sa, 0, n-1, 0, n-1);
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
lu_backsub(a, n, pivx, b);
|
||||
|
||||
/* Check that the solution is correct */
|
||||
for (i = 0; i < n; i++) {
|
||||
double sum, temp;
|
||||
sum = 0.0;
|
||||
for (j = 0; j < n; j++)
|
||||
sum += sa[i][j] * b[j];
|
||||
//printf("~~ check %d = %f, against %f\n",i,sum,sb[i]);
|
||||
temp = fabs(sum - sb[i]);
|
||||
if (temp > 1e-6)
|
||||
rv = 2;
|
||||
}
|
||||
free_dvector(sb, 0, n-1);
|
||||
free_dmatrix(sa, 0, n-1, 0, n-1);
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return rv;
|
||||
}
|
||||
|
||||
662
Argyll_V3.5.0/numlib/License.txt
Executable file
662
Argyll_V3.5.0/numlib/License.txt
Executable file
@@ -0,0 +1,662 @@
|
||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
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 GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are 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.
|
||||
|
||||
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
|
||||
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.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"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.
|
||||
|
||||
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 "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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 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.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
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 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 work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero 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 that a certain numbered version of the GNU Affero 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 Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero 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.
|
||||
|
||||
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.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
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
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
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
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
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,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero 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 your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
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 AGPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
14
Argyll_V3.5.0/numlib/Readme.txt
Executable file
14
Argyll_V3.5.0/numlib/Readme.txt
Executable file
@@ -0,0 +1,14 @@
|
||||
Collection of numerical routines used by various other
|
||||
(mainly color) code.
|
||||
|
||||
Included are:
|
||||
|
||||
numsup Error handling, vector/array allocation, macros
|
||||
dnsq Non-linear equation solver
|
||||
powell Powell and Conjugate Gradient multi dimentional minimiser
|
||||
ludecomp LU decomposition matrix solver
|
||||
svd Singular Value decomposition matrix solver
|
||||
zbrent 1 dimentional brent root search
|
||||
rand Random number generators
|
||||
sobolo Sobol sub-random vector sequence generator
|
||||
|
||||
431
Argyll_V3.5.0/numlib/aatree.c
Normal file
431
Argyll_V3.5.0/numlib/aatree.c
Normal file
@@ -0,0 +1,431 @@
|
||||
|
||||
/*
|
||||
Andersson binary balanced tree library
|
||||
|
||||
> Created (Julienne Walker): September 10, 2005
|
||||
> Corrections (James Bucanek): April 10, 2006
|
||||
1) Typo in jsw_aerase:
|
||||
up != 0 should be top != 0
|
||||
2) Bug in jsw_aerase:
|
||||
skew ( path[top] ) should be skew ( up )
|
||||
split ( path[top] ) should be split ( up )
|
||||
3) Bug in skew and split macros:
|
||||
Condition should test for nil
|
||||
4) Bug in jsw_aerase:
|
||||
Search for successor should save the path
|
||||
> Fixed duplicate handling (Graeme W. Gill): August 16, 2009
|
||||
*/
|
||||
|
||||
#include "aatree.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <cstdlib>
|
||||
|
||||
using std::malloc;
|
||||
using std::free;
|
||||
using std::size_t;
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#ifndef HEIGHT_LIMIT
|
||||
#define HEIGHT_LIMIT 64 /* Tallest allowable tree */
|
||||
#endif
|
||||
|
||||
typedef struct aat_anode {
|
||||
int level; /* Horizontal level for balance */
|
||||
void *data; /* User-defined content */
|
||||
struct aat_anode *link[2]; /* Left (0) and right (1) links */
|
||||
} aat_anode_t;
|
||||
|
||||
struct aat_atree {
|
||||
aat_anode_t *root; /* Top of the tree */
|
||||
aat_anode_t *nil; /* End of tree sentinel */
|
||||
cmp_f cmp; /* Compare two items */
|
||||
size_t size; /* Number of items (user-defined) */
|
||||
};
|
||||
|
||||
struct aat_atrav {
|
||||
aat_atree_t *tree; /* Paired tree */
|
||||
aat_anode_t *it; /* Current node */
|
||||
aat_anode_t *path[HEIGHT_LIMIT]; /* Traversal path */
|
||||
size_t top; /* Top of stack */
|
||||
};
|
||||
|
||||
/* Remove left horizontal links */
|
||||
#define skew(t) do { \
|
||||
if ( t->link[0]->level == t->level && t->level != 0 ) { \
|
||||
aat_anode_t *save = t->link[0]; \
|
||||
t->link[0] = save->link[1]; \
|
||||
save->link[1] = t; \
|
||||
t = save; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
/* Remove consecutive horizontal links */
|
||||
#define split(t) do { \
|
||||
if ( t->link[1]->link[1]->level == t->level && t->level != 0 ) { \
|
||||
aat_anode_t *save = t->link[1]; \
|
||||
t->link[1] = save->link[0]; \
|
||||
save->link[0] = t; \
|
||||
t = save; \
|
||||
++t->level; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
/* Version of cmp that ensure there are no duplicates */
|
||||
/* Set res to comparison result */
|
||||
#define AAT_CMP( res, tree, p1, p2) \
|
||||
{ \
|
||||
if ((res = tree->cmp ( p1, p2 )) == 0) { \
|
||||
if (p1 < p2) \
|
||||
res = -1; \
|
||||
else if (p1 > p2) \
|
||||
res = 1; \
|
||||
} \
|
||||
}
|
||||
|
||||
/* Create a new node that points to our data */
|
||||
/* Return tree-nil if malloc failed. */
|
||||
static aat_anode_t *new_node ( aat_atree_t *tree, void *data )
|
||||
{
|
||||
aat_anode_t *rn = (aat_anode_t *)malloc ( sizeof *rn );
|
||||
|
||||
if ( rn == NULL )
|
||||
return tree->nil;
|
||||
|
||||
rn->level = 1;
|
||||
rn->data = data;
|
||||
rn->link[0] = rn->link[1] = tree->nil;
|
||||
|
||||
return rn;
|
||||
}
|
||||
|
||||
/* Create a new empty tree */
|
||||
aat_atree_t *aat_anew ( cmp_f cmp )
|
||||
{
|
||||
aat_atree_t *rt = (aat_atree_t *)malloc ( sizeof *rt );
|
||||
|
||||
if ( rt == NULL )
|
||||
return NULL;
|
||||
|
||||
/* Initialize sentinel */
|
||||
rt->nil = (aat_anode_t *)malloc ( sizeof *rt->nil );
|
||||
if ( rt->nil == NULL ) {
|
||||
free ( rt );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rt->nil->data = NULL; /* Simplifies some ops */
|
||||
rt->nil->level = 0;
|
||||
rt->nil->link[0] = rt->nil->link[1] = rt->nil;
|
||||
|
||||
/* Initialize tree */
|
||||
rt->root = rt->nil;
|
||||
rt->cmp = cmp;
|
||||
rt->size = 0;
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
/* Delete the whole tree (but not the data) */
|
||||
void aat_adelete ( aat_atree_t *tree )
|
||||
{
|
||||
aat_anode_t *it = tree->root;
|
||||
aat_anode_t *save;
|
||||
|
||||
/* Destruction by rotation */
|
||||
while ( it != tree->nil ) {
|
||||
if ( it->link[0] == tree->nil ) {
|
||||
/* Remove node */
|
||||
save = it->link[1];
|
||||
free ( it );
|
||||
}
|
||||
else {
|
||||
/* Rotate right */
|
||||
save = it->link[0];
|
||||
it->link[0] = save->link[1];
|
||||
save->link[1] = it;
|
||||
}
|
||||
|
||||
it = save;
|
||||
}
|
||||
|
||||
/* Finalize destruction */
|
||||
free ( tree->nil );
|
||||
free ( tree );
|
||||
}
|
||||
|
||||
/* Locate a entry based on the data's value */
|
||||
void *aat_afind ( aat_atree_t *tree, void *data )
|
||||
{
|
||||
aat_anode_t *it = tree->root;
|
||||
|
||||
while ( it != tree->nil ) {
|
||||
int cmp = tree->cmp ( it->data, data );
|
||||
|
||||
if ( cmp == 0 )
|
||||
break;
|
||||
|
||||
it = it->link[cmp < 0];
|
||||
}
|
||||
|
||||
/* nil->data == NULL */
|
||||
return it->data;
|
||||
}
|
||||
|
||||
/* Insert an entry based on the data's value. */
|
||||
/* Take a copy of the pointer to the entry. */
|
||||
/* Return 0 if malloc failed */
|
||||
int aat_ainsert ( aat_atree_t *tree, void *data )
|
||||
{
|
||||
if ( tree->root == tree->nil ) {
|
||||
/* Empty tree case */
|
||||
tree->root = new_node ( tree, data );
|
||||
if ( tree->root == tree->nil )
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
aat_anode_t *it = tree->root;
|
||||
aat_anode_t *path[HEIGHT_LIMIT];
|
||||
int top = 0, dir;
|
||||
|
||||
/* Find a spot and save the path */
|
||||
for ( ; ; ) {
|
||||
int cmp;
|
||||
|
||||
path[top++] = it;
|
||||
AAT_CMP(cmp, tree, it->data, data);
|
||||
dir = cmp < 0;
|
||||
|
||||
if ( it->link[dir] == tree->nil )
|
||||
break;
|
||||
|
||||
it = it->link[dir];
|
||||
}
|
||||
|
||||
/* Create a new item */
|
||||
it->link[dir] = new_node ( tree, data );
|
||||
if ( it->link[dir] == tree->nil )
|
||||
return 0;
|
||||
|
||||
/* Walk back and rebalance */
|
||||
while ( --top >= 0 ) {
|
||||
/* Which child? */
|
||||
if ( top != 0 )
|
||||
dir = path[top - 1]->link[1] == path[top];
|
||||
|
||||
skew ( path[top] );
|
||||
split ( path[top] );
|
||||
|
||||
/* Fix the parent */
|
||||
if ( top != 0 )
|
||||
path[top - 1]->link[dir] = path[top];
|
||||
else
|
||||
tree->root = path[top];
|
||||
}
|
||||
}
|
||||
|
||||
++tree->size;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Delete a given entry by locating it by */
|
||||
/* its value, then removing it from the tree. */
|
||||
/* The item itself isn't deleted. */
|
||||
/* Return 0 if the item wasn't found */
|
||||
int aat_aerase ( aat_atree_t *tree, void *data )
|
||||
{
|
||||
if ( tree->root == tree->nil )
|
||||
return 0;
|
||||
else {
|
||||
aat_anode_t *it = tree->root;
|
||||
aat_anode_t *path[HEIGHT_LIMIT];
|
||||
int top = 0, dir, cmp;
|
||||
|
||||
/* Find node to remove and save path */
|
||||
for ( ; ; ) {
|
||||
path[top++] = it;
|
||||
|
||||
if ( it == tree->nil )
|
||||
return 0;
|
||||
|
||||
AAT_CMP(cmp, tree, it->data, data);
|
||||
if ( cmp == 0 )
|
||||
break;
|
||||
|
||||
dir = cmp < 0;
|
||||
it = it->link[dir];
|
||||
}
|
||||
|
||||
/* Remove the found node */
|
||||
if ( it->link[0] == tree->nil
|
||||
|| it->link[1] == tree->nil )
|
||||
{
|
||||
/* Single child case */
|
||||
int dir2 = it->link[0] == tree->nil;
|
||||
|
||||
/* Unlink the item */
|
||||
if ( --top != 0 )
|
||||
path[top - 1]->link[dir] = it->link[dir2];
|
||||
else
|
||||
tree->root = it->link[1];
|
||||
|
||||
free ( it );
|
||||
}
|
||||
else {
|
||||
/* Two child case */
|
||||
aat_anode_t *heir = it->link[1];
|
||||
aat_anode_t *prev = it;
|
||||
|
||||
while ( heir->link[0] != tree->nil ) {
|
||||
path[top++] = prev = heir;
|
||||
heir = heir->link[0];
|
||||
}
|
||||
|
||||
/*
|
||||
Order is important!
|
||||
(free item, replace item, free heir)
|
||||
*/
|
||||
it->data = heir->data;
|
||||
prev->link[prev == it] = heir->link[1];
|
||||
free ( heir );
|
||||
}
|
||||
|
||||
/* Walk back up and rebalance */
|
||||
while ( --top >= 0 ) {
|
||||
aat_anode_t *up = path[top];
|
||||
|
||||
if ( top != 0 )
|
||||
dir = path[top - 1]->link[1] == up;
|
||||
|
||||
/* Rebalance (aka. black magic) */
|
||||
if ( up->link[0]->level < up->level - 1
|
||||
|| up->link[1]->level < up->level - 1 )
|
||||
{
|
||||
if ( up->link[1]->level > --up->level )
|
||||
up->link[1]->level = up->level;
|
||||
|
||||
/* Order is important! */
|
||||
skew ( up );
|
||||
skew ( up->link[1] );
|
||||
skew ( up->link[1]->link[1] );
|
||||
split ( up );
|
||||
split ( up->link[1] );
|
||||
}
|
||||
|
||||
/* Fix the parent */
|
||||
if ( top != 0 )
|
||||
path[top - 1]->link[dir] = up;
|
||||
else
|
||||
tree->root = up;
|
||||
}
|
||||
}
|
||||
|
||||
--tree->size;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Return the current number of items */
|
||||
size_t aat_asize ( aat_atree_t *tree )
|
||||
{
|
||||
return tree->size;
|
||||
}
|
||||
|
||||
/* Return a traversal object */
|
||||
aat_atrav_t *aat_atnew ( void )
|
||||
{
|
||||
return malloc ( sizeof ( aat_atrav_t ) );
|
||||
}
|
||||
|
||||
/* Destroy a traversal object */
|
||||
void aat_atdelete ( aat_atrav_t *trav )
|
||||
{
|
||||
free ( trav );
|
||||
}
|
||||
|
||||
/*
|
||||
First step in traversal,
|
||||
handles min and max
|
||||
*/
|
||||
static void *start ( aat_atrav_t *trav,
|
||||
aat_atree_t *tree, int dir )
|
||||
{
|
||||
trav->tree = tree;
|
||||
trav->it = tree->root;
|
||||
trav->top = 0;
|
||||
|
||||
/* Build a path to work with */
|
||||
if ( trav->it != tree->nil ) {
|
||||
while ( trav->it->link[dir] != tree->nil ) {
|
||||
trav->path[trav->top++] = trav->it;
|
||||
trav->it = trav->it->link[dir];
|
||||
}
|
||||
}
|
||||
|
||||
/* Could be nil, but nil->data == NULL */
|
||||
return trav->it->data;
|
||||
}
|
||||
|
||||
/*
|
||||
Subsequent traversal steps,
|
||||
handles ascending and descending
|
||||
*/
|
||||
static void *move ( aat_atrav_t *trav, int dir )
|
||||
{
|
||||
aat_anode_t *nil = trav->tree->nil;
|
||||
|
||||
if ( trav->it->link[dir] != nil ) {
|
||||
/* Continue down this branch */
|
||||
trav->path[trav->top++] = trav->it;
|
||||
trav->it = trav->it->link[dir];
|
||||
|
||||
while ( trav->it->link[!dir] != nil ) {
|
||||
trav->path[trav->top++] = trav->it;
|
||||
trav->it = trav->it->link[!dir];
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Move to the next branch */
|
||||
aat_anode_t *last;
|
||||
|
||||
do {
|
||||
if ( trav->top == 0 ) {
|
||||
trav->it = nil;
|
||||
break;
|
||||
}
|
||||
|
||||
last = trav->it;
|
||||
trav->it = trav->path[--trav->top];
|
||||
} while ( last == trav->it->link[dir] );
|
||||
}
|
||||
|
||||
/* Could be nil, but nil->data == NULL */
|
||||
return trav->it->data;
|
||||
}
|
||||
|
||||
/* Return first item */
|
||||
void *aat_atfirst ( aat_atrav_t *trav, aat_atree_t *tree )
|
||||
{
|
||||
return start ( trav, tree, 0 ); /* Min value */
|
||||
}
|
||||
|
||||
/* Return last item */
|
||||
void *aat_atlast ( aat_atrav_t *trav, aat_atree_t *tree )
|
||||
{
|
||||
return start ( trav, tree, 1 ); /* Max value */
|
||||
}
|
||||
|
||||
/* Move to next */
|
||||
void *aat_atnext ( aat_atrav_t *trav )
|
||||
{
|
||||
return move ( trav, 1 ); /* Toward larger items */
|
||||
}
|
||||
|
||||
/* Move to previous */
|
||||
void *aat_atprev ( aat_atrav_t *trav )
|
||||
{
|
||||
return move ( trav, 0 ); /* Toward smaller items */
|
||||
}
|
||||
64
Argyll_V3.5.0/numlib/aatree.h
Normal file
64
Argyll_V3.5.0/numlib/aatree.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef AATREE_H
|
||||
#define AATREE_H
|
||||
|
||||
/*
|
||||
Andersson binary balanced tree library
|
||||
|
||||
Log n performance on insert/erase
|
||||
|
||||
> Created (Julienne Walker): September 10, 2005
|
||||
|
||||
This code is in the public domain. Anyone may
|
||||
use it or change it in any way that they see
|
||||
fit. The author assumes no responsibility for
|
||||
damages incurred through use of the original
|
||||
code or any variations thereof.
|
||||
|
||||
It is requested, but not required, that due
|
||||
credit is given to the original author and
|
||||
anyone who has modified the code through
|
||||
a header comment, such as this one.
|
||||
|
||||
Various changes by Graeme W. Gill.
|
||||
See aatree.c
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <cstddef>
|
||||
|
||||
using std::size_t;
|
||||
|
||||
extern "C" {
|
||||
#else
|
||||
#include <stddef.h>
|
||||
#endif
|
||||
|
||||
/* Opaque types */
|
||||
typedef struct aat_atree aat_atree_t;
|
||||
typedef struct aat_atrav aat_atrav_t;
|
||||
|
||||
/* User-defined item handling */
|
||||
/* Return -1, 0, +1 */
|
||||
typedef int (*cmp_f) ( const void *p1, const void *p2 );
|
||||
|
||||
/* Andersson tree functions */
|
||||
aat_atree_t *aat_anew ( cmp_f cmp );
|
||||
void aat_adelete ( aat_atree_t *tree );
|
||||
void *aat_afind ( aat_atree_t *tree, void *data );
|
||||
int aat_ainsert ( aat_atree_t *tree, void *data );
|
||||
int aat_aerase ( aat_atree_t *tree, void *data );
|
||||
size_t aat_asize ( aat_atree_t *tree );
|
||||
|
||||
/* Traversal functions */
|
||||
aat_atrav_t *aat_atnew ( void );
|
||||
void aat_atdelete ( aat_atrav_t *trav );
|
||||
void *aat_atfirst ( aat_atrav_t *trav, aat_atree_t *tree );
|
||||
void *aat_atlast ( aat_atrav_t *trav, aat_atree_t *tree );
|
||||
void *aat_atnext ( aat_atrav_t *trav );
|
||||
void *aat_atprev ( aat_atrav_t *trav );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* AATREE */
|
||||
45
Argyll_V3.5.0/numlib/afiles
Executable file
45
Argyll_V3.5.0/numlib/afiles
Executable file
@@ -0,0 +1,45 @@
|
||||
Readme.txt
|
||||
License.txt
|
||||
afiles
|
||||
Jamfile
|
||||
dnsq.c
|
||||
dnsq.h
|
||||
gnewt.c
|
||||
gnewt.h
|
||||
dnsqtest.c
|
||||
numlib.h
|
||||
numsup.c
|
||||
numsup.h
|
||||
powell.h
|
||||
powell.c
|
||||
tpowell.c
|
||||
tconjgrad.c
|
||||
varmet.h
|
||||
varmet.c
|
||||
tvarmet.c
|
||||
dhsx.h
|
||||
dhsx.c
|
||||
tdhsx.c
|
||||
rand.c
|
||||
rand.h
|
||||
ludecomp.h
|
||||
ludecomp.c
|
||||
LUtest.c
|
||||
svd.h
|
||||
svd.c
|
||||
svdtest.c
|
||||
zbrent.c
|
||||
zbrent.h
|
||||
zbrenttest.c
|
||||
sobol.c
|
||||
sobol.h
|
||||
soboltest.c
|
||||
aatree.h
|
||||
aatree.c
|
||||
quadprog.h
|
||||
quadprog.c
|
||||
qptest.c
|
||||
roots.h
|
||||
roots.c
|
||||
ui.h
|
||||
ui.c
|
||||
455
Argyll_V3.5.0/numlib/dhsx.c
Normal file
455
Argyll_V3.5.0/numlib/dhsx.c
Normal file
@@ -0,0 +1,455 @@
|
||||
|
||||
/* This is better for stocastic optimisation, where the function */
|
||||
/* being evaluated may have a random component, or is not smooth. */
|
||||
|
||||
/*
|
||||
* Copyright 1999 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
/* A general purpose downhill simplex multivariate optimser, */
|
||||
/* based on the Nelder and Mead algorithm. */
|
||||
/* Code is an original expression of the algorithms decsribed in */
|
||||
/* "Numerical Recipes in C", by W.H.Press, B.P.Flannery, */
|
||||
/* S.A.Teukolsky & W.T.Vetterling. */
|
||||
|
||||
#include "numsup.h"
|
||||
|
||||
#define DEBUG
|
||||
|
||||
int dhsx_debug = 0;
|
||||
|
||||
static void simplexinit(int di, double *cp, double **p, double *r, double sv, int ii);
|
||||
static double trypoint(int di,double *cp, double **p, double *y, int hix, double hpfac,
|
||||
double (*funk)(void *fdata, double *tp), void *fdata, double *tryp);
|
||||
|
||||
#ifdef NEVER /* Experimental */
|
||||
|
||||
#define ALPHA 0.7 /* Extrapolate hight point through oposite face factor */
|
||||
#define GAMMA 1.4 /* Aditional extrapolation if ALPHA is good */
|
||||
#define BETA 0.4 /* One dimensional contraction factor (smaller is more) */
|
||||
#define DELTA 0.5 /* Multi dimensional contraction factor (smaller is more) */
|
||||
#define NONEXP 2 /* non expanding passes */
|
||||
|
||||
#else /* Standard tuning values */
|
||||
|
||||
#define ALPHA 1.0 /* [1.0] Extrapolate hight point through oposite face factor */
|
||||
#define GAMMA 2.0 /* [2.0] Aditional extrapolation if ALPHA is good */
|
||||
#define BETA 0.4 /* [0.5] One dimensional contraction factor (smaller is more) */
|
||||
#define DELTA 0.4 /* [0.5] Multi dimensional contraction factor (smaller is more) */
|
||||
#define NONEXP 3 /* [3] non expanding passes */
|
||||
|
||||
#endif
|
||||
|
||||
/* Down hill simplex function */
|
||||
/* return 0 on sucess, 1 on failure due to excessive itterations */
|
||||
/* Result will be in cp */
|
||||
int dhsx(
|
||||
double *rv, /* If not NULL, return the residual error */
|
||||
int di, /* Dimentionality */
|
||||
double *cp, /* Initial starting point, return minimum */
|
||||
double *s, /* Size of initial search area */
|
||||
double ftol, /* Finishing tollerance of error change */
|
||||
double athr, /* Absolute return value threshold. (Set high to not use) */
|
||||
int maxit, /* Maximum iterations allowed */
|
||||
double (*funk)(void *fdata, double *tp), /* Error function to evaluate */
|
||||
void *fdata /* Data needed by function */
|
||||
) {
|
||||
int ii = 0; /* Initial simplex orientation */
|
||||
int i, j;
|
||||
int nit; /* Number of iterations */
|
||||
int nsp = di+1; /* Number of simplex verticy points */
|
||||
double tryy, ysave;
|
||||
double tol;
|
||||
double **p; /* Current simplex array */
|
||||
double *y; /* Values of func at vertices */
|
||||
double **p2; /* Trial simplex array */
|
||||
double *y2; /* Trial values of func at vertices */
|
||||
int lox, hix, nhix; /* Lowest point index, highest point, next highest point */
|
||||
double *tryp; /* Temporary used by trypoint() */
|
||||
|
||||
/* Allocate array arrays */
|
||||
tryp = dvector(0, di-1); /* Trial value */
|
||||
p = dmatrix(0, nsp-1, 0, di-1); /* Vertex array of dimentions */
|
||||
y = dvector(0, nsp-1); /* Value of function at vertices */
|
||||
p2 = dmatrix(0, nsp-1, 0, di-1); /* Trial vertex array of dimentions */
|
||||
y2 = dvector(0, nsp-1); /* Trial value of function at vertices */
|
||||
|
||||
/* Init the search simplex */
|
||||
simplexinit(di, cp, p, s, 1.0, ii);
|
||||
|
||||
/* Compute initial y (function) values at simplex vertices */
|
||||
for (i = 0; i < nsp; i++) /* For all vertices */
|
||||
y[i] = (*funk)(fdata, p[i]); /* Compute error function */
|
||||
|
||||
/* Locate verticy with best value */
|
||||
lox = 0;
|
||||
for (i = 0; i < nsp; i++) {
|
||||
if (y[i] < y[lox])
|
||||
lox = i;
|
||||
}
|
||||
tryy = (*funk)(fdata, cp); /* Value at initial point */
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf(" initial point %s = %e\n",debPdv(di,cp),tryy);
|
||||
#endif /* DEBUG */
|
||||
|
||||
/* If our initial point is better than any of the simplex vertices */
|
||||
if (y[lox] > tryy) {
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf(" initial point is better than surrounding simplex\n");
|
||||
#endif /* DEBUG */
|
||||
/* Move all the vertices to match moving lox to cp */
|
||||
for (i = 0; i < nsp; i++) {
|
||||
if (i == lox)
|
||||
continue;
|
||||
for (j = 0; j < di; j++)
|
||||
p[i][j] += cp[j] - p[lox][j];
|
||||
y[i] = (*funk)(fdata, p[i]); /* Compute error function */
|
||||
}
|
||||
/* Make lox be the input point */
|
||||
for (j = 0; j < di; j++)
|
||||
p[lox][j] = cp[j];
|
||||
y[lox] = tryy;
|
||||
}
|
||||
|
||||
/* Compute current center point location as sum of vertices. */
|
||||
/* (We use this to compute moves) */
|
||||
for (j = 0; j < di; j++) { /* For all dimensions */
|
||||
double sum;
|
||||
for (i = 0, sum = 0.0; i < nsp; i++) /* For all vertices */
|
||||
sum += p[i][j];
|
||||
cp[j] = sum;
|
||||
}
|
||||
|
||||
/* Untill we find a solution or give up */
|
||||
for (nit = 0; ; nit++) {
|
||||
|
||||
/* Find highest, next highest and lowest vertex */
|
||||
lox = nhix = hix = 0;
|
||||
for (i = 0; i < nsp; i++) {
|
||||
if (y[i] < y[lox])
|
||||
lox = i;
|
||||
if (y[i] > y[hix]) {
|
||||
nhix = hix;
|
||||
hix = i;
|
||||
} else if (y[i] > y[nhix]) {
|
||||
nhix = i;
|
||||
}
|
||||
}
|
||||
|
||||
tol = y[hix] - y[lox];
|
||||
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) {
|
||||
printf("Current vs =\n");
|
||||
for (i = 0; i < nsp; i++)
|
||||
printf(" %d: %s\n",i,debPdv(di, p[i]));
|
||||
printf("Current errs = %s\n",debPdv(nsp,y));
|
||||
printf("Current y[lox] = %e, y[hix] = %e\n",y[lox], y[hix]);
|
||||
}
|
||||
#endif /* DEBUG */
|
||||
|
||||
/* If we look like we are about to finish, */
|
||||
/* see if we should re-start with a new simplex. */
|
||||
if (tol < ftol && y[lox] < athr /* Found an adequate solution */
|
||||
&& nit < maxit) {
|
||||
double scale = 0.0;
|
||||
int lox2;
|
||||
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf(" nit %d, tol %e\n",nit, tol);
|
||||
#endif /* DEBUG */
|
||||
|
||||
/* compute center location */
|
||||
tryy = 1.0/nsp;
|
||||
for (j = 0; j < di; j++) /* For all dimensions */
|
||||
cp[j] *= tryy; /* Set cp to center point of simplex */
|
||||
|
||||
/* Compute scaled distance of vertexes from center */
|
||||
for (i = 0; i < nsp; i++) {
|
||||
double dist = 0.0;
|
||||
for (j = 0; j < di; j++) {
|
||||
double tt = (cp[j] - p[i][j])/s[j];
|
||||
dist += tt * tt;
|
||||
}
|
||||
scale += sqrt(dist);
|
||||
}
|
||||
scale /= (double)nsp; /* Average scale compared to starting simplex */
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf(" ave scale = %f\n",scale);
|
||||
#endif /* DEBUG */
|
||||
|
||||
/* Enlarge search space, but not more than initial */
|
||||
scale *= 10.0;
|
||||
if (scale > 1.0)
|
||||
scale = 1.0;
|
||||
|
||||
/* Compute trial simplex with different orientation */
|
||||
if (++ii >= (di+1))
|
||||
ii = 0;
|
||||
|
||||
/* Init the search simplex */
|
||||
simplexinit(di, cp, p2, s, scale, ii);
|
||||
|
||||
/* Compute y (function) values at simplex vertices */
|
||||
for (i = 0; i < nsp; i++) /* For all vertices */
|
||||
y2[i] = (*funk)(fdata, p2[i]); /* Compute error function */
|
||||
|
||||
/* Locate verticy with best value */
|
||||
lox2 = 0;
|
||||
for (i = 0; i < nsp; i++) {
|
||||
if (y2[i] < y2[lox2])
|
||||
lox2 = i;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf(" y2lox %f ylox %f\n",y2[lox2], y[lox]);
|
||||
#endif /* DEBUG */
|
||||
|
||||
/* If any of its vertexes are better than current best, switch */
|
||||
/* to it and continue (i.e. re-start) */
|
||||
if (y2[lox2] < y[lox]) {
|
||||
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf(" restarting\n");
|
||||
#endif /* DEBUG */
|
||||
|
||||
for (i = 0; i < nsp; i++) {
|
||||
for (j = 0; j < di; j++)
|
||||
p[i][j] = p2[i][j];
|
||||
y[i] = y2[i];
|
||||
}
|
||||
|
||||
/* Compute current center point location as sum of vertices. */
|
||||
/* (We use this to compute moves) */
|
||||
for (j = 0; j < di; j++) { /* For all dimensions */
|
||||
double sum;
|
||||
for (i = 0, sum = 0.0; i < nsp; i++) /* For all vertices */
|
||||
sum += p[i][j];
|
||||
cp[j] = sum;
|
||||
}
|
||||
|
||||
/* Find highest, next highest and lowest vertex */
|
||||
lox = nhix = hix = 0;
|
||||
for (i = 0; i < nsp; i++) {
|
||||
if (y[i] < y[lox])
|
||||
lox = i;
|
||||
if (y[i] > y[hix]) {
|
||||
nhix = hix;
|
||||
hix = i;
|
||||
} else if (y[i] > y[nhix]) {
|
||||
nhix = i;
|
||||
}
|
||||
}
|
||||
|
||||
tol = y[hix] - y[lox];
|
||||
}
|
||||
}
|
||||
|
||||
if ((tol < ftol && y[lox] < athr) /* Found an adequate solution */
|
||||
|| ((nit+1) >= maxit)) { /* Or we are about to fail */
|
||||
|
||||
/* convert cp[] to center point location, */
|
||||
/* and use best out of it and any simplex verticy. */
|
||||
tryy = 1.0/nsp;
|
||||
for (j = 0; j < di; j++) /* For all dimensions */
|
||||
cp[j] *= tryy; /* Set cp to center point of simplex */
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("C point = %s\n",debPdv(di,cp));
|
||||
#endif
|
||||
tryy = (*funk)(fdata, cp); /* Compute error function */
|
||||
|
||||
if (tryy > y[lox]) { /* Center point is not the best */
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("C point val %f is not best, using sx %d val %f instead\n",tryy,lox,y[lox]);
|
||||
#endif
|
||||
tryy = y[lox];
|
||||
for (j = 0; j < di; j++)
|
||||
cp[j] = p[lox][j];
|
||||
}
|
||||
#ifdef DEBUG
|
||||
else if (dhsx_debug) printf("C point val %f is best\n",tryy);
|
||||
#endif
|
||||
free_dvector(y2, 0, nsp-1);
|
||||
free_dmatrix(p2, 0, nsp-1, 0, di-1);
|
||||
free_dvector(y, 0, nsp-1);
|
||||
free_dmatrix(p, 0, nsp-1, 0, di-1);
|
||||
free_dvector(tryp, 0, di-1);
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("Total itterations = %d\n",nit);
|
||||
#endif
|
||||
if (rv != NULL)
|
||||
*rv = tryy;
|
||||
if ((nit+1) >= maxit)
|
||||
return 1; /* Failed */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Only try expanding after a couple of iterations */
|
||||
if (nit > NONEXP) {
|
||||
/* Try moving the high point through the oposite face by ALPHA */
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("dhsx: try moving high point %d through oposite face",hix);
|
||||
#endif
|
||||
tryy = trypoint(di, cp, p, y, hix, -ALPHA, funk, fdata, tryp);
|
||||
}
|
||||
|
||||
/* If gave good result, continue on in that direction */
|
||||
if (nit > NONEXP && tryy <= y[lox]) {
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("dhsx: moving high through oposite face worked");
|
||||
#endif
|
||||
tryy = trypoint(di, cp, p, y, hix, GAMMA, funk, fdata, tryp);
|
||||
|
||||
|
||||
/* else if ALPHA move made things worse, do a one dimensional */
|
||||
/* contraction by a factor BETA */
|
||||
} else if (nit <= NONEXP || tryy >= y[nhix]) {
|
||||
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("dhsx: else try moving contracting point %d, y[ini] = %f",hix,y[hix]);
|
||||
#endif
|
||||
ysave = y[hix];
|
||||
tryy = trypoint(di, cp, p, y, hix, BETA, funk, fdata, tryp);
|
||||
|
||||
if (tryy >= ysave) {
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("dhsx: contracting didn't work, try contracting other points to low");
|
||||
#endif
|
||||
/* That still didn't help us, so move all the */
|
||||
/* other points towards the low point */
|
||||
for (i = 0; i < nsp; i++) { /* For all verts except low */
|
||||
if (i != lox) {
|
||||
for (j = 0; j < di; j++) /* For all dimensions */
|
||||
p[i][j] = DELTA * p[i][j] + (1.0 - DELTA) * p[lox][j];
|
||||
y[i] = (*funk)(fdata, p[i]); /* Compute function value for new point */
|
||||
}
|
||||
}
|
||||
/* Re-compute current center point location */
|
||||
for (j = 0; j < di; j++) {
|
||||
double sum;
|
||||
for (i = 0,sum = 0.0;i<nsp;i++)
|
||||
sum += p[i][j];
|
||||
cp[j] = sum;
|
||||
}
|
||||
} else {
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("dhsx: contracting point %d worked, tryy = %e, ysave = %e",hix,tryy,ysave);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Try moving the high point through the opposite face */
|
||||
/* by a factor of fac, and replaces the high point if */
|
||||
/* that proves to be better. Return the failed or new */
|
||||
/* function value. */
|
||||
static double trypoint(
|
||||
int di, /* Dimentionality */
|
||||
double *cp, /* nsp * center coord/Returned coordinate */
|
||||
double **p, /* Starting/Current simplex (modified by dhsx) */
|
||||
double *y, /* values of func at vertices */
|
||||
int hix, /* Index of high point we are moving */
|
||||
double hpfac, /* factor to move high point */
|
||||
double (*funk)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
void *fdata, /* Data needed by function */
|
||||
double *tryp /* temporary array of size di-1 */
|
||||
) {
|
||||
int j;
|
||||
double tt, tryy;
|
||||
|
||||
/* Compute trial high point */
|
||||
tt = (1.0 - hpfac)/di;
|
||||
for (j = 0; j < di; j++)
|
||||
tryp[j] = cp[j] * tt - p[hix][j] * (tt - hpfac);
|
||||
|
||||
/* Evaluate trial point */
|
||||
tryy = (*funk)(fdata, tryp); /* Compute error function */
|
||||
|
||||
/* If new high point pos. is better */
|
||||
if (tryy < y[hix]) {
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("Try gave improved %e from sx %d",tryy,hix);
|
||||
#endif
|
||||
y[hix] = tryy; /* Replace func val of hi with trial */
|
||||
|
||||
for (j = 0; j < di; j++) {
|
||||
cp[j] += tryp[j] - p[hix][j]; /* Recompute cp */
|
||||
p[hix][j] = tryp[j]; /* Replace co-ords of hi with trial */
|
||||
}
|
||||
} else {
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) printf("Try gave worse %e from sx %d",tryy,hix);
|
||||
#endif
|
||||
}
|
||||
return tryy; /* Function value of trial point */
|
||||
}
|
||||
|
||||
|
||||
/* Make up an initial simplex for dhsx routine */
|
||||
static void
|
||||
simplexinit(
|
||||
int di, /* Dimentionality */
|
||||
double *cp, /* Initial solution location */
|
||||
double **p, /* Simplex to initialize */
|
||||
double *s, /* initial radius for each dimention */
|
||||
double sv, /* Radius scaling value */
|
||||
int ii /* Coordinate to start with */
|
||||
) {
|
||||
double bb;
|
||||
double hh = 0.5; /* Constant */
|
||||
double rr = sqrt(3.0)/2.0; /* Constant */
|
||||
int i, j;
|
||||
for (i = 0; i < (di+1); i++) { /* For each vertex */
|
||||
/* The bounding points form a equalateral simplex */
|
||||
/* whose vertexes are on a sphere about the data */
|
||||
/* point center. The coordinate sequence is: */
|
||||
/* B = sphere radius */
|
||||
/* H = 0.5 */
|
||||
/* R = sqrt(3)/2 */
|
||||
/* 0 0 0 +B */
|
||||
/* 0 0 0 -B */
|
||||
|
||||
/* 0 0 0 +B */
|
||||
/* 0 0 +RB -HB */
|
||||
/* 0 0 -RB -HB */
|
||||
|
||||
/* 0 0 0 +B */
|
||||
/* 0 0 +RB -HB */
|
||||
/* 0 +RRb -HRB -HB */
|
||||
/* 0 -RRb -HRB -HB */
|
||||
|
||||
/* 0 0 0 +B */
|
||||
/* 0 0 +RB -HB */
|
||||
/* 0 +RRb -HRB -HB */
|
||||
/* +RRRb -HRRb -HRB -HB */
|
||||
/* -RRRb -HRRb -HRB -HB */
|
||||
|
||||
/* etc. */
|
||||
bb = 1.0; /* Initial unscaled radius */
|
||||
for (j = 0; j < di; j++) { /* For each coordinate in vertex */
|
||||
if (j > ii)
|
||||
p[i][j] = cp[j] + sv * s[j] * 0.0; /* If beyond last */
|
||||
else if (j == ii) /* If last non-zero */
|
||||
p[i][j] = cp[j] + sv * s[j] * bb;
|
||||
else if (ii == di && j == (di-1)) /* If last of last */
|
||||
p[i][j] = cp[j] + sv * s[j] * -1.0 * bb;
|
||||
else /* If before last */
|
||||
p[i][j] = cp[j] + sv * s[j] * -hh * bb;
|
||||
bb *= rr;
|
||||
}
|
||||
/* Increment coordinate number with wrap around */
|
||||
if (++ii >= (di+1))
|
||||
ii = 0;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
if (dhsx_debug) {
|
||||
for (i = 0; i < (di+1); i++)
|
||||
printf(" p[%d] = %s\n",i,debPdv(di,p[i]));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
36
Argyll_V3.5.0/numlib/dhsx.h
Normal file
36
Argyll_V3.5.0/numlib/dhsx.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef DHSX_H
|
||||
#define DHSX_H
|
||||
|
||||
/* A general purpose downhill simplex multivariate optimser, */
|
||||
/* as described in */
|
||||
/* "Numerical Recipes in C", by W.H.Press, B.P.Flannery, */
|
||||
/* S.A.Teukolsky & W.T.Vetterling. */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Down hill simplex function */
|
||||
/* return 0 on sucess, 1 on failure due to excessive itterations */
|
||||
/* Result will be in cp */
|
||||
int dhsx(
|
||||
double *rv, /* If not NULL, return the residual error */
|
||||
int di, /* Dimentionality */
|
||||
double *cp, /* Initial starting point, return minimum */
|
||||
double *s, /* Size of initial search area */
|
||||
double ftol, /* Finishing tollerance of error change */
|
||||
double athr, /* Absolute return value threshold. (Set high to not use) */
|
||||
int maxit, /* Maximum iterations allowed */
|
||||
double (*funk)(void *fdata, double *tp), /* Error function to evaluate */
|
||||
void *fdata /* Data needed by function */
|
||||
);
|
||||
|
||||
double dhsx_funk( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[]); /* Multivriate input value */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DHSX_H */
|
||||
1676
Argyll_V3.5.0/numlib/dnsq.c
Normal file
1676
Argyll_V3.5.0/numlib/dnsq.c
Normal file
File diff suppressed because it is too large
Load Diff
121
Argyll_V3.5.0/numlib/dnsq.h
Normal file
121
Argyll_V3.5.0/numlib/dnsq.h
Normal file
@@ -0,0 +1,121 @@
|
||||
#ifndef DNSQ_H
|
||||
#define DNSQ_H
|
||||
|
||||
/* dnsq non-linear equation solver public interface definition */
|
||||
/*
|
||||
* This concatenation Copyright 1998 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
||||
return values from dnsq() and dnsqe():
|
||||
|
||||
0 Improper input parameters.
|
||||
|
||||
1 Relative error between two consecutive iterates is at
|
||||
most XTOL. Normal sucess return value.
|
||||
|
||||
2 Number of calls to FCN has reached or exceeded MAXFEV.
|
||||
|
||||
3 XTOL is too small. No further improvement in the
|
||||
approximate solution X is possible.
|
||||
|
||||
4 Iteration is not making good progress, as measured by
|
||||
the improvement from the last five Jacobian evaluations.
|
||||
|
||||
5 Iteration is not making good progress, as measured
|
||||
by the improvement from the last ten iterations.
|
||||
Return value if no zero can be found from this starting
|
||||
point.
|
||||
*/
|
||||
|
||||
/* dnsq function */
|
||||
int dnsq(
|
||||
void *fdata, /* Opaque data pointer, passed to called functions */
|
||||
int (*fcn)(void *fdata, int n, double *x, double *fvec, int iflag),
|
||||
/* Pointer to function we are solving */
|
||||
int (*jac)(void *fdata, int n, double *x, double *fvec, double **fjac),
|
||||
/* Optional function to compute jacobian, NULL if not used */
|
||||
double **sjac, /* Optional initial jacobian matrix/last jacobian matrix. NULL if not used.*/
|
||||
int startsjac, /* Set to non-zero to use sjac as the initial jacobian */
|
||||
int n, /* Number of functions and variables */
|
||||
double x[], /* Initial solution estimate, RETURNs final solution */
|
||||
double fvec[], /* Array that will be RETURNed with thefunction values at the solution */
|
||||
double dtol, /* Desired tollerance of the solution */
|
||||
double tol, /* Desired tollerance of root */
|
||||
int maxfev, /* Set excessive Number of Function Evaluations */
|
||||
int ml, /* number of subdiagonals within the band of the Jacobian matrix. */
|
||||
int mu, /* number of superdiagonals within the band of the Jacobian matrix. */
|
||||
double epsfcn, /* determines suitable step for forward-difference approximation */
|
||||
double diag[], /* Optional scaling factors, use NULL for internal scaling */
|
||||
double factor, /* Determines the initial step bound */
|
||||
double maxstep, /* Determines the maximum subsequent step bound (0.0 for none) */
|
||||
int nprint, /* Turn on debugging printouts from func() every nprint itterations */
|
||||
int *nfev, /* RETURNs the number of calls to fcn() */
|
||||
int *njev /* RETURNs the number of calls to jac() */
|
||||
);
|
||||
|
||||
/* User supplied functions */
|
||||
|
||||
/* calculate the functions at x[] */
|
||||
int dnsq_fcn( /* Return < 0 on abort */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
int n, /* Dimenstionality */
|
||||
double *x, /* Multivariate input values */
|
||||
double *fvec, /* Multivariate output values */
|
||||
int iflag /* Flag set to 0 to trigger debug output */
|
||||
);
|
||||
|
||||
/* Calculate Jacobian at x[] */
|
||||
int dnsq_jac( /* Return < 0 on abort */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
int n, /* Dimensionality */
|
||||
double *x, /* Point to caluculate Jacobian at (do not alter) */
|
||||
double *fvec, /* Function values at x (do not alter) */
|
||||
double **fjac /* Return n by n Jacobian in this array */
|
||||
);
|
||||
|
||||
#define M_LARGE 1.79e+308
|
||||
#define M_DIVER 2.22e-15 /* Machine precision (10 x IEEE double) */
|
||||
#define M_SQRT_DIVER 4.71e-8 /* sqrt(M_DIVER) */
|
||||
|
||||
/* Simplified dnsq() */
|
||||
int dnsqe(
|
||||
void *fdata, /* Opaque pointer to pass to fcn() and jac() */
|
||||
int (*fcn)(void *fdata, int n, double *x, double *fvec, int iflag),
|
||||
/* Pointer to function we are solving */
|
||||
int (*jac)(void *fdata, int n, double *x, double *fvec, double **fjac),
|
||||
/* Optional function to compute jacobian, NULL if not used */
|
||||
int n, /* Number of functions and variables */
|
||||
double x[], /* Initial solution estimate, RETURNs final solution */
|
||||
double ss, /* Initial search area */
|
||||
double fvec[], /* Array that will be RETURNed with thefunction values at the solution */
|
||||
double dtol, /* Desired tollerance of the solution */
|
||||
double tol, /* Desired tollerance of root */
|
||||
int maxfev, /* Maximum number of function calls. set to 0 for automatic */
|
||||
int nprint /* Turn on debugging printouts from func() every nprint itterations */
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DNSQ_H */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
192
Argyll_V3.5.0/numlib/dnsqtest.c
Normal file
192
Argyll_V3.5.0/numlib/dnsqtest.c
Normal file
@@ -0,0 +1,192 @@
|
||||
|
||||
/*
|
||||
* Copyright 1999 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
/* Example use of dnsqe() */
|
||||
/* */
|
||||
/* The problem is to determine the values of X(1), X(2), ..., X(9), */
|
||||
/* which solve the system of tridiagonal equations */
|
||||
/* */
|
||||
/* (3-2*X(1))*X(1) -2*X(2) = -1 */
|
||||
/* -X(I-1) + (3-2*X(I))*X(I) -2*X(I+1) = -1, I=2-8 */
|
||||
/* -X(8) + (3-2*X(9))*X(9) = -1 */
|
||||
/* */
|
||||
/* Final approximate solution: */
|
||||
/* */
|
||||
/* -0.5706545E+00 */
|
||||
/* -0.6816283E+00 */
|
||||
/* -0.7017325E+00 */
|
||||
/* -0.7042129E+00 */
|
||||
/* -0.7013690E+00 */
|
||||
/* -0.6918656E+00 */
|
||||
/* -0.6657920E+00 */
|
||||
/* -0.5960342E+00 */
|
||||
/* -0.4164121E+00 */
|
||||
|
||||
#include "numlib.h"
|
||||
|
||||
/* Compute norm of a vector */
|
||||
static double denorm(int n, double *x);
|
||||
|
||||
int fcn(void *fdata, int n, double *x, double *fvec, int iflag);
|
||||
|
||||
double expect[9] = {
|
||||
-0.5706545E+00,
|
||||
-0.6816283E+00,
|
||||
-0.7017325E+00,
|
||||
-0.7042129E+00,
|
||||
-0.7013690E+00,
|
||||
-0.6918656E+00,
|
||||
-0.6657920E+00,
|
||||
-0.5960342E+00,
|
||||
-0.4164121E+00 };
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int n = 9 /* 9 */; /* Problem vector size */
|
||||
double x[9]; /* Function input values */
|
||||
double fvec[9]; /* Function output values */
|
||||
double ss; /* Search area */
|
||||
int info, j;
|
||||
double fnorm;
|
||||
int nprint = 0; /* Itteration debugging print = off */
|
||||
double tol;
|
||||
|
||||
error_program = "dnsqtest"; /* Set global error reporting string */
|
||||
|
||||
/* Driver for dnsqe example. */
|
||||
/* Not supplying Jacobian, use approximation */
|
||||
|
||||
/* The following starting values provide a rough solution. */
|
||||
for (j = 1; j <= 9; ++j) {
|
||||
x[j - 1] = -1.f;
|
||||
}
|
||||
ss = 0.1;
|
||||
|
||||
nprint = 0;
|
||||
|
||||
/* Set tol to the square root of the machine precision. */
|
||||
/* Unless high precision solutions are required, */
|
||||
/* this is the recommended setting. */
|
||||
|
||||
tol = M_SQRT_DIVER;
|
||||
|
||||
info = dnsqe(NULL, fcn, NULL, n, x, ss, fvec, tol, tol, 0, nprint);
|
||||
fnorm = denorm(n, fvec);
|
||||
fprintf(stdout,"Final L2 norm of the residuals = %e\n",fnorm);
|
||||
fprintf(stdout,"Exit return value = %d (1 = sucess)\n",info);
|
||||
fprintf(stdout,"Final approximate solution:\n");
|
||||
for (j = 0; j < n; j++) {
|
||||
fprintf(stdout,"x[%d] = %f, expect %f\n",j,x[j], expect[j]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} /* main() */
|
||||
|
||||
/* Function being solved */
|
||||
int fcn(void *fdata, int n, double *x, double *fvec, int iflag)
|
||||
{
|
||||
double temp, temp1, temp2;
|
||||
int k;
|
||||
|
||||
/* Function Body */
|
||||
for (k = 0; k < n; ++k) {
|
||||
temp = (3.0 - 2.0 * x[k]) * x[k];
|
||||
temp1 = 0.0;
|
||||
if (k != 0) {
|
||||
temp1 = x[k-1];
|
||||
}
|
||||
temp2 = 0.0;
|
||||
if (k != ((n)-1))
|
||||
temp2 = x[k+1];
|
||||
fvec[k] = temp - temp1 - 2.0 * temp2 + 1.0;
|
||||
if (iflag == 0)
|
||||
printf("x[%d] = %f, fvec[%d] + %f\n",k,x[k],k,fvec[k]);
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("~~ x[%d] = %f, fvec[%d] + %f\n",k,x[k],k,fvec[k]);
|
||||
#endif /* DEBUG */
|
||||
}
|
||||
/* Return < 0 to abort */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
static double denorm(
|
||||
int n, /* Size of x[] */
|
||||
double x[]) /* Input vector */
|
||||
{
|
||||
/* Initialized data */
|
||||
static double rdwarf = 3.834e-20;
|
||||
static double rgiant = 1.304e19;
|
||||
|
||||
/* Local variables */
|
||||
static double xabs, x1max, x3max;
|
||||
static int i;
|
||||
static double s1, s2, s3, agiant, floatn;
|
||||
double ret_val, td;
|
||||
|
||||
s1 = 0.0; /* Large component */
|
||||
s2 = 0.0; /* Intermedate component */
|
||||
s3 = 0.0; /* Small component */
|
||||
x1max = 0.0;
|
||||
x3max = 0.0;
|
||||
floatn = (double) (n + 1);
|
||||
agiant = rgiant / floatn;
|
||||
for (i = 0; i < n; i++) {
|
||||
xabs = (td = x[i], fabs(td));
|
||||
|
||||
/* Sum for intermediate components. */
|
||||
if (xabs > rdwarf && xabs < agiant) {
|
||||
td = xabs; /* Computing 2nd power */
|
||||
s2 += td * td;
|
||||
|
||||
/* Sum for small components. */
|
||||
} else if (xabs <= rdwarf) {
|
||||
if (xabs <= x3max) {
|
||||
if (xabs != 0.0) { /* Computing 2nd power */
|
||||
td = xabs / x3max;
|
||||
s3 += td * td;
|
||||
}
|
||||
} else { /* Computing 2nd power */
|
||||
td = x3max / xabs;
|
||||
s3 = 1.0 + s3 * (td * td);
|
||||
x3max = xabs;
|
||||
}
|
||||
|
||||
/* Sum for large components. */
|
||||
} else {
|
||||
if (xabs <= x1max) { /* Computing 2nd power */
|
||||
td = xabs / x1max;
|
||||
s1 += td * td;
|
||||
} else { /* Computing 2nd power */
|
||||
td = x1max / xabs;
|
||||
s1 = 1.0 + s1 * (td * td);
|
||||
x1max = xabs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Calculation of norm. */
|
||||
if (s1 != 0.0) { /* Large is present */
|
||||
ret_val = x1max * sqrt(s1 + s2 / x1max / x1max);
|
||||
} else { /* Medium and small are present */
|
||||
if (s2 == 0.0) {
|
||||
ret_val = x3max * sqrt(s3); /* Small only */
|
||||
} else {
|
||||
if (s2 >= x3max) { /* Medium larger than small */
|
||||
ret_val = sqrt(s2 * (1.0 + x3max / s2 * (x3max * s3)));
|
||||
} else { /* Small large than medium */
|
||||
ret_val = sqrt(x3max * (s2 / x3max + x3max * s3));
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
413
Argyll_V3.5.0/numlib/gnewt.c
Normal file
413
Argyll_V3.5.0/numlib/gnewt.c
Normal file
@@ -0,0 +1,413 @@
|
||||
|
||||
/*
|
||||
* Copyright 2018 Graeme Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include "numsup.h"
|
||||
#include "ludecomp.h"
|
||||
#include "gnewt.h" /* Public interface definitions */
|
||||
|
||||
#undef DEBUG
|
||||
|
||||
#ifdef DEBUG
|
||||
# define DBG(xx) printf xx;
|
||||
#else
|
||||
# define DBG(xx)
|
||||
#endif
|
||||
|
||||
|
||||
#define TOLX 1.0e-7 /* Convergence criterion on delx */
|
||||
#define STPMX 100.0 /* Maximum step multiplier */
|
||||
|
||||
static void apxjac(int n, double *x, double *fvec, double **df, void *fdata,
|
||||
void (*fcn)(void *fdata, int n, double *x, double *fvec));
|
||||
|
||||
static int linesearch(int n, double *xold, double fold, double *delf, double *delx,
|
||||
double *x, double *fvec, double *fp, double maxstep, void *fdata,
|
||||
void (*fcn)(void *fdata, int n, double *x, double *f), int *pfit, int maxjac, int it);
|
||||
|
||||
#define FMAX(A, B) ((A) > (B) ? (A) : (B))
|
||||
|
||||
int gnewt(
|
||||
void *fdata, /* Opaque pointer to pass to fcn() and jac() */
|
||||
void (*fcn)(void *fdata, int n, double *x, double *fvec),
|
||||
/* Pointer to function we are solving */
|
||||
void (*jac)(void *fdata, int n, double *x, double **fjac),
|
||||
/* Function to compute jacobian */
|
||||
int n, /* Number of functions and variables */
|
||||
double x[], /* Initial solution estimate, returns final solution */
|
||||
double rfvec[], /* Optionaly return soln. function values */
|
||||
double xtol, /* Desired tollerance of root */
|
||||
double ftol, /* Desired tollerance of the solution */
|
||||
int maxfcn, /* Maximum number of function itterations */
|
||||
int maxjac /* Maximum number of jacobian itterations */
|
||||
) {
|
||||
int i, j, it, fit, jit, *pivx, _pivx[10];
|
||||
double f, fold; /* half magnitide squared of fvec[] */
|
||||
double *delf, _delf[10]; /* del f where f = 0.5 F.F */
|
||||
double *fvec, _fvec[10]; /* F(x) */
|
||||
double **fjac, *_fjac[11], __fjac[10 * 10];
|
||||
double *xold, _xold[10];
|
||||
double bigfx, bigx, maxstep;
|
||||
double *delx, _delx[10]; /* Full step delta x */
|
||||
double sum;
|
||||
int rv = 0;
|
||||
|
||||
#ifdef DEBUG
|
||||
double *fvec_check;
|
||||
double **fjac_check;
|
||||
#endif
|
||||
|
||||
DBG(("gnewt:\n"))
|
||||
|
||||
fit = jit = 0;
|
||||
|
||||
/* Do local vector/array allocations */
|
||||
if (n <= 10) {
|
||||
pivx = _pivx;
|
||||
if (rfvec == NULL) {
|
||||
fvec = _fvec;
|
||||
} else
|
||||
fvec = rfvec;
|
||||
_fjac[0] = __fjac;
|
||||
fjac = _fjac+1; /* dmatrix_reset() will setup fjac */
|
||||
xold = _xold;
|
||||
delf = _delf;
|
||||
delx = _delx;
|
||||
} else {
|
||||
pivx = ivector(0, n-1); /* LU decomp. pivod record */
|
||||
if (rfvec == NULL) {
|
||||
fvec = dvector(0, n-1); /* Function value */
|
||||
} else
|
||||
fvec = rfvec;
|
||||
fjac = dmatrix(0, n-1, 0, n-1); /* Jacobian matrix */
|
||||
xold = dvector(0, n-1); /* Previous value of x[] */
|
||||
delf = dvector(0, n-1); /* del f */
|
||||
delx = dvector(0, n-1); /* Full step delta x */
|
||||
}
|
||||
#ifdef DEBUG
|
||||
fvec_check = dvector(0, n-1);
|
||||
fjac_check = dmatrix(0, n-1, 0, n-1);
|
||||
#endif
|
||||
|
||||
/* Initial function value */
|
||||
fcn(fdata, n, x, fvec);
|
||||
fit++;
|
||||
|
||||
DBG((" x %s\n",debPdv(n,x)))
|
||||
DBG((" fvec %s\n",debPdv(n,fvec)))
|
||||
|
||||
/* Compute half magnitide squared of function value at x */
|
||||
for (sum = 0.0, i = 0; i < n; i++)
|
||||
sum += fvec[i] * fvec[i];
|
||||
f = 0.5 * sum;
|
||||
DBG((" f %f\n",f))
|
||||
|
||||
/* test for initial value being a root */
|
||||
for (bigfx = 0.0, i = 0; i < n; i++) {
|
||||
double tt = fabs(fvec[i]);
|
||||
if (tt > bigfx)
|
||||
bigfx = tt;
|
||||
}
|
||||
if (bigfx < (0.01 * ftol)) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Compute line search x maximum step size */
|
||||
for (sum = 0.0, i = 0 ; i < n; i++)
|
||||
sum += x[i] * x[i];
|
||||
maxstep = STPMX * FMAX(sqrt(sum), (double)n);
|
||||
DBG((" maxstep %f\n",maxstep))
|
||||
|
||||
/* Until we are done */
|
||||
for (it = 0; fit < maxfcn && jit < maxjac; it++) {
|
||||
double rip;
|
||||
|
||||
DBG((" fit %d jit %d\n",fit,jit))
|
||||
|
||||
/* Compute Jacobian matrix */
|
||||
if (jac != NULL) {
|
||||
/* lu_decomp may have swapped rows - so fix it */
|
||||
dmatrix_reset(fjac, 0, n-1, 0, n-1);
|
||||
jac(fdata, n, x, fjac); /* User function */
|
||||
} else {
|
||||
apxjac(n, x, fvec, fjac, fdata, fcn); /* Numerical aproximation */
|
||||
}
|
||||
jit++;
|
||||
|
||||
#ifdef DEBUG
|
||||
copy_dmatrix(fjac_check, fjac, 0, n-1, 0, n-1);
|
||||
|
||||
DBG((" fjac = \n"))
|
||||
for (i = 0; i < n; i++)
|
||||
DBG((" %d: %s\n",i,debPdv(n, fjac[i])))
|
||||
DBG(("\n"))
|
||||
#endif
|
||||
|
||||
/* Compute del f for the line search. */
|
||||
for (i = 0; i < n; i++) {
|
||||
for (sum = 0.0, j = 0; j < n; j++)
|
||||
sum += fjac[j][i] * fvec[j]; /* Hmm. df/dx . f */
|
||||
delf[i] = sum;
|
||||
}
|
||||
|
||||
/* Save current values of x and f to be able to monitor progres */
|
||||
for (i = 0; i < n; i++)
|
||||
xold[i] = x[i];
|
||||
fold = f;
|
||||
|
||||
/* Desired delta f to make F(x) == 0 */
|
||||
for (i = 0; i < n; i++)
|
||||
delx[i] = -fvec[i];
|
||||
DBG((" -fvec %s\n",debPdv(n,delx)))
|
||||
|
||||
/* Solve for delta x using Jacobian and desired delta f */
|
||||
if (lu_decomp(fjac, n, pivx, &rip)) {
|
||||
rv = 2;
|
||||
goto done;
|
||||
}
|
||||
lu_backsub(fjac, n, pivx, delx);
|
||||
DBG((" delx %s\n",debPdv(n,delx)))
|
||||
|
||||
#ifdef DEBUG
|
||||
matrix_vect_mult(fvec_check, n, fjac_check, n, n, delx, n);
|
||||
DBG((" check -fvec : %s\n\n",debPdv(n,fvec_check)))
|
||||
#endif
|
||||
|
||||
if ((rv = linesearch(n, xold, fold, delf, delx, x, fvec, &f, maxstep, fdata, fcn,
|
||||
&fit, maxfcn, it)) != 0) {
|
||||
if (rv != 1) { /* Not run out of itterations error */
|
||||
DBG((" linesearch failed with %d\n",rv))
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
DBG((" after linesearch:\n"))
|
||||
DBG((" x %s\n",debPdv(n,x)))
|
||||
DBG((" fvec %s\n",debPdv(n,fvec)))
|
||||
|
||||
/* See if f() has converged */
|
||||
for (bigfx = 0.0, i = 0; i < n; i++) {
|
||||
if (fabs(fvec[i]) > bigfx)
|
||||
bigfx = fabs(fvec[i]);
|
||||
}
|
||||
DBG((" bigfx %f ftol %f\n",bigfx,ftol))
|
||||
if (bigfx < ftol) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Could check for zero gradient problem here... */
|
||||
|
||||
/* See if x[] has converged */
|
||||
for (bigx = 0.0, i = 0; i < n; i++) {
|
||||
double tt = (fabs(x[i] - xold[i]))/FMAX(fabs(x[i]), 1.0);
|
||||
if (tt > bigx)
|
||||
bigx = tt;
|
||||
}
|
||||
DBG((" bigx %f xtol %f\n",bigx,xtol))
|
||||
if (bigx < xtol)
|
||||
goto done;
|
||||
}
|
||||
|
||||
rv = 1;
|
||||
|
||||
done:;
|
||||
|
||||
if (n > 10) {
|
||||
if (fvec != rfvec)
|
||||
free_dvector(fvec, 0, n-1);
|
||||
free_dvector(xold, 0, n-1);
|
||||
free_dvector(delx, 0, n-1);
|
||||
free_dvector(delf, 0, n-1);
|
||||
free_dmatrix(fjac, 0, n-1, 0, n-1);
|
||||
free_ivector(pivx, 0, n-1);
|
||||
}
|
||||
#ifdef DEBUG
|
||||
free_dvector(fvec_check,0, n-1);
|
||||
free_dmatrix(fjac_check,0, n-1, 0, n-1);
|
||||
#endif
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* - - - - - - - - */
|
||||
|
||||
#define ALF 1.0e-4 /* Ensures sufficient decrease in function value. */
|
||||
|
||||
/* Search for a step size that makes progress */
|
||||
/* Return nz on error */
|
||||
static int linesearch(
|
||||
int n,
|
||||
double *xold,
|
||||
double fold,
|
||||
double *delf, /* del f */
|
||||
double *delx, /* full step delta x[] */
|
||||
double *x, /* in/out current x[] */
|
||||
double *fvec, /* return fvec at x[] */
|
||||
double *fp, /* in/out f value */
|
||||
double maxstep, /* maximum x step */
|
||||
void *fdata, /* Context for fcn */
|
||||
void (*fcn)(void *fdata, int n, double *x, double *f),
|
||||
int *pfit, /* Inc number of itts */
|
||||
int maxfcn, /* Max function its */
|
||||
int it /* Caller iteration count */
|
||||
) {
|
||||
int i;
|
||||
double f = *fp, f2;
|
||||
double lmda1, lmda2, min_lmda;
|
||||
double sum, slope, bigx;
|
||||
|
||||
DBG(("linesearch:\n"))
|
||||
|
||||
/* Comute magnitude of step */
|
||||
for (sum = 0.0, i = 0; i < n; i++)
|
||||
sum += delx[i] * delx[i];
|
||||
sum = sqrt(sum);
|
||||
|
||||
/* re-scale if step is too big */
|
||||
if (sum > maxstep) {
|
||||
for (i = 0; i < n; i++)
|
||||
delx[i] *= maxstep/sum;
|
||||
}
|
||||
|
||||
for (slope = 0.0, i = 0; i < n; i++)
|
||||
slope += delf[i] * delx[i];
|
||||
if (slope >= 0.0) {
|
||||
DBG((" slope %f >= 0.0\n",slope))
|
||||
return 3;
|
||||
}
|
||||
|
||||
bigx = 0.0;
|
||||
for (i = 0;i < n; i++) {
|
||||
double tt = fabs(delx[i])/FMAX(fabs(xold[i]), 1.0);
|
||||
if (tt > bigx)
|
||||
bigx = tt;
|
||||
}
|
||||
min_lmda = TOLX/bigx;
|
||||
|
||||
/* Try full Newton step first */
|
||||
lmda1 = 1.0;
|
||||
|
||||
DBG((" lmda1 %f min_lmda %f\n",lmda1, min_lmda))
|
||||
|
||||
/* Top of loop */
|
||||
for (; *pfit < maxfcn; it++) {
|
||||
double tmp_lmda;
|
||||
|
||||
DBG((" lmda1 %f\n",lmda1))
|
||||
|
||||
/* Take step */
|
||||
for (i = 0;i < n;i++)
|
||||
x[i] = xold[i] + lmda1 * delx[i];
|
||||
|
||||
/* Compute f = 0.5 F.F at x */
|
||||
fcn(fdata, n, x, fvec);
|
||||
(*pfit)++;
|
||||
DBG((" x %s\n",debPdv(n,x)))
|
||||
DBG((" fvec %s\n",debPdv(n,fvec)))
|
||||
for (sum = 0.0, i = 0; i < n; i++)
|
||||
sum += fvec[i] * fvec[i];
|
||||
f = 0.5 * sum;
|
||||
|
||||
//if (it == 0) printf(" linesearch: At 1st full step f %f -> %f\n", *fp, f);
|
||||
|
||||
/* Convergence on delx. */
|
||||
if (lmda1 < min_lmda) {
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
x[i] = xold[i];
|
||||
return 0;
|
||||
|
||||
} else if (f <= fold + ALF * lmda1 * slope) {
|
||||
*fp = f;
|
||||
return 0; /* Sufficient function decrease */
|
||||
|
||||
} else { /* Backtrack. */
|
||||
if (lmda1 == 1.0) /* First time */
|
||||
tmp_lmda = -slope/(2.0 * (f - fold-slope));
|
||||
|
||||
else { /* Subsequent backtracks */
|
||||
double c, d, e;
|
||||
double a, b, rhs1, rhs2;
|
||||
|
||||
rhs1 = f - fold - slope * lmda1;
|
||||
rhs2 = f2 - fold - slope * lmda2;
|
||||
c = rhs1/(lmda1 * lmda1);
|
||||
d = rhs2/(lmda2 * lmda2);
|
||||
e = lmda1 - lmda2;
|
||||
a = (c - d)/e;
|
||||
b = (-lmda2 * c + lmda1 * d)/e;
|
||||
if (a == 0.0)
|
||||
tmp_lmda = -slope/(2.0 * b);
|
||||
else {
|
||||
double disc = b * b - 3.0 * a * slope;
|
||||
|
||||
if (disc < 0.0)
|
||||
tmp_lmda = 0.5 * lmda1;
|
||||
else if (b <= 0.0)
|
||||
tmp_lmda = (-b + sqrt(disc))/(3.0 * a);
|
||||
else
|
||||
tmp_lmda = -slope/(b + sqrt(disc));
|
||||
}
|
||||
if (tmp_lmda > 0.5 * lmda1)
|
||||
tmp_lmda = 0.5 * lmda1;
|
||||
}
|
||||
}
|
||||
lmda2 = lmda1;
|
||||
lmda1 = FMAX(tmp_lmda, lmda1 * 0.1);
|
||||
f2 = f;
|
||||
}
|
||||
|
||||
*fp = f;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* - - - - - - - - */
|
||||
|
||||
/* Compute forward difference as aprox. Jacobian matrix */
|
||||
|
||||
#define JEPS 1.0e-8 /* Aprox. sqrt of machine precision */
|
||||
|
||||
static void apxjac(
|
||||
int n, /* Dimensions */
|
||||
double *x, /* Location x to compute Jacobian */
|
||||
double *fvec, /* Function value at x */
|
||||
double **df, /* Return Jacobian */
|
||||
void *fdata, /* fcn() context */
|
||||
void (*fcn)(void *fdata, int n, double *x, double *fvec)
|
||||
) {
|
||||
int i, j;
|
||||
double h, temp, *f, _f[10];
|
||||
|
||||
if (n <= 10)
|
||||
f = _f;
|
||||
else
|
||||
f = dvector(0, n);
|
||||
|
||||
for (j = 0; j < n; j++) {
|
||||
temp = x[j];
|
||||
|
||||
h = JEPS * fabs(temp);
|
||||
if (h == 0.0)
|
||||
h = JEPS;
|
||||
x[j] = temp + h; /* Add delta */
|
||||
h = x[j] - temp; /* Actual delta with fp precision limits */
|
||||
|
||||
fcn(fdata, n, x, f);
|
||||
|
||||
x[j] = temp; /* Restore value */
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
df[i][j] = (f[i] - fvec[i])/h;
|
||||
}
|
||||
|
||||
if (f != _f)
|
||||
free_dvector(f, 0, n-1);
|
||||
}
|
||||
|
||||
|
||||
59
Argyll_V3.5.0/numlib/gnewt.h
Normal file
59
Argyll_V3.5.0/numlib/gnewt.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef GNEWT_H
|
||||
#define GNEWT_H
|
||||
|
||||
/* Global newton non-linear equation solver */
|
||||
|
||||
/*
|
||||
* Copyright 2018 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
||||
return values:
|
||||
|
||||
0 Success
|
||||
|
||||
1 Ran out of itterations
|
||||
|
||||
2 Inverting Jacobian failed
|
||||
|
||||
*/
|
||||
|
||||
int gnewt(
|
||||
void *fdata, /* Opaque pointer to pass to fcn() and jac() */
|
||||
void (*fcn)(void *fdata, int n, double *x, double *fvec),
|
||||
/* Pointer to function we are solving */
|
||||
void (*jac)(void *fdata, int n, double *x, double **fjac),
|
||||
/* Function to compute jacobian */
|
||||
int n, /* Number of functions and variables */
|
||||
double x[], /* Initial solution estimate, returns final solution */
|
||||
double rfvec[], /* Optionaly return soln. function values */
|
||||
double xtol, /* Desired tollerance of root */
|
||||
double ftol, /* Desired tollerance of the solution */
|
||||
int maxfcn, /* Maximum number of function itterations */
|
||||
int maxjac /* Maximum number of jacobian itterations */
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* GNEWT_H */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
584
Argyll_V3.5.0/numlib/ludecomp.c
Normal file
584
Argyll_V3.5.0/numlib/ludecomp.c
Normal file
@@ -0,0 +1,584 @@
|
||||
/***************************************************/
|
||||
/* Linear Simultaeous equation solver */
|
||||
/***************************************************/
|
||||
|
||||
/* General simultaneous equation solver. */
|
||||
/* Code was inspired by the algorithm decsribed in section 2.3 */
|
||||
/* of "Numerical Recipes in C", by W.H.Press, B.P.Flannery, */
|
||||
/* S.A.Teukolsky & W.T.Vetterling. */
|
||||
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include "numsup.h"
|
||||
#include "ludecomp.h"
|
||||
|
||||
#undef DO_POLISH
|
||||
#undef DO_CHECK
|
||||
|
||||
/* Solve the simultaneous linear equations A.X = B */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
solve_se(
|
||||
double **a, /* A[][] input matrix, returns LU decomposition of A */
|
||||
double *b, /* B[] input array, returns solution X[] */
|
||||
int n /* Dimensionality */
|
||||
) {
|
||||
double rip; /* Row interchange parity */
|
||||
int *pivx, PIVX[10];
|
||||
#if defined(DO_POLISH) || defined(DO_CHECK)
|
||||
double **sa; /* save input matrix values */
|
||||
double *sb; /* save input vector values */
|
||||
int i, j;
|
||||
#endif
|
||||
|
||||
if (n <= 10)
|
||||
pivx = PIVX;
|
||||
else
|
||||
pivx = ivector(0, n-1);
|
||||
|
||||
#if defined(DO_POLISH) || defined(DO_CHECK)
|
||||
sa = dmatrix(0, n-1, 0, n-1);
|
||||
sb = dvector(0, n-1);
|
||||
|
||||
/* Copy input matrix and vector values */
|
||||
for (i = 0; i < n; i++) {
|
||||
sb[i] = b[i];
|
||||
for (j = 0; j < n; j++)
|
||||
sa[i][j] = a[i][j];
|
||||
}
|
||||
#endif
|
||||
|
||||
if (lu_decomp(a, n, pivx, &rip)) {
|
||||
#if defined(DO_POLISH) || defined(DO_CHECK)
|
||||
free_dvector(sb, 0, n-1);
|
||||
free_dmatrix(sa, 0, n-1, 0, n-1);
|
||||
#endif
|
||||
if (pivx != PIVX)
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
lu_backsub(a, n, pivx, b);
|
||||
|
||||
#ifdef DO_POLISH
|
||||
lu_polish(sa, a, n, sb, b, pivx); /* Improve the solution */
|
||||
#endif
|
||||
|
||||
#ifdef DO_CHECK
|
||||
/* Check that the solution is correct */
|
||||
for (i = 0; i < n; i++) {
|
||||
double sum, temp;
|
||||
sum = 0.0;
|
||||
for (j = 0; j < n; j++)
|
||||
sum += sa[i][j] * b[j];
|
||||
temp = fabs(sum - sb[i]);
|
||||
if (temp > 1e-6) {
|
||||
free_dvector(sb, 0, n-1);
|
||||
free_dmatrix(sa, 0, n-1, 0, n-1);
|
||||
if (pivx != PIVX)
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if defined(DO_POLISH) || defined(DO_CHECK)
|
||||
free_dvector(sb, 0, n-1);
|
||||
free_dmatrix(sa, 0, n-1, 0, n-1);
|
||||
#endif
|
||||
if (pivx != PIVX)
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Solve the simultaneous linear equations A.X = B, with polishing */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
polished_solve_se(
|
||||
double **a, /* A[][] input matrix, returns LU decomposition of A */
|
||||
double *b, /* B[] input array, returns solution X[] */
|
||||
int n /* Dimensionality */
|
||||
) {
|
||||
double rip; /* Row interchange parity */
|
||||
int *pivx, PIVX[10];
|
||||
double **sa; /* save input matrix values */
|
||||
double *sb; /* save input vector values */
|
||||
int i, j;
|
||||
|
||||
if (n <= 10)
|
||||
pivx = PIVX;
|
||||
else
|
||||
pivx = ivector(0, n-1);
|
||||
|
||||
sa = dmatrix(0, n-1, 0, n-1);
|
||||
sb = dvector(0, n-1);
|
||||
|
||||
/* Copy source input matrix and vector values */
|
||||
for (i = 0; i < n; i++) {
|
||||
sb[i] = b[i];
|
||||
for (j = 0; j < n; j++)
|
||||
sa[i][j] = a[i][j];
|
||||
}
|
||||
|
||||
if (lu_decomp(a, n, pivx, &rip)) {
|
||||
free_dvector(sb, 0, n-1);
|
||||
free_dmatrix(sa, 0, n-1, 0, n-1);
|
||||
if (pivx != PIVX)
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
lu_backsub(a, n, pivx, b);
|
||||
|
||||
lu_polish(sa, a, n, sb, b, pivx); /* Improve the solution */
|
||||
|
||||
#ifdef DO_CHECK
|
||||
/* Check that the solution is correct */
|
||||
for (i = 0; i < n; i++) {
|
||||
double sum, temp;
|
||||
sum = 0.0;
|
||||
for (j = 0; j < n; j++)
|
||||
sum += sa[i][j] * b[j];
|
||||
temp = fabs(sum - sb[i]);
|
||||
if (temp > 1e-6) {
|
||||
free_dvector(sb, 0, n-1);
|
||||
free_dmatrix(sa, 0, n-1, 0, n-1);
|
||||
if (pivx != PIVX)
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
free_dvector(sb, 0, n-1);
|
||||
free_dmatrix(sa, 0, n-1, 0, n-1);
|
||||
if (pivx != PIVX)
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Decompose the square matrix A[][] into lower and upper triangles */
|
||||
/* NOTE that it returns transposed inverse by normal convention. */
|
||||
/* NOTE that rows get swaped by swapping matrix pointers! */
|
||||
/* Use sym_matrix_trans() to fix this. */
|
||||
/* Return 1 if the matrix is singular. */
|
||||
int
|
||||
lu_decomp(
|
||||
double **a, /* A input array, output upper and lower triangles. */
|
||||
int n, /* Dimensionality */
|
||||
int *pivx, /* Return pivoting row permutations record */
|
||||
double *rip /* Row interchange parity, +/- 1.0, used for determinant */
|
||||
) {
|
||||
int i, j;
|
||||
double *rscale, RSCALE[10]; /* Implicit scaling of each row */
|
||||
|
||||
if (n <= 10)
|
||||
rscale = RSCALE;
|
||||
else
|
||||
rscale = dvector(0, n-1);
|
||||
|
||||
/* For each row */
|
||||
for (i = 0; i < n; i++) {
|
||||
double big;
|
||||
/* For each column in row */
|
||||
for (big = 0.0, j=0; j < n; j++) {
|
||||
double temp;
|
||||
temp = fabs(a[i][j]);
|
||||
if (temp > big)
|
||||
big = temp;
|
||||
}
|
||||
if (fabs(big) <= DBL_MIN) {
|
||||
if (rscale != RSCALE)
|
||||
free_dvector(rscale, 0, n-1);
|
||||
return 1; /* singular matrix */
|
||||
}
|
||||
rscale[i] = 1.0/big; /* Save the scaling */
|
||||
}
|
||||
|
||||
/* For each column (Crout's method) */
|
||||
for (*rip = 1.0, j = 0; j < n; j++) {
|
||||
double big;
|
||||
int k, bigi = 0;
|
||||
|
||||
/* For each row */
|
||||
for (i = 0; i < j; i++) {
|
||||
double sum;
|
||||
sum = a[i][j];
|
||||
for (k = 0; k < i; k++)
|
||||
sum -= a[i][k] * a[k][j];
|
||||
a[i][j] = sum;
|
||||
}
|
||||
|
||||
/* Find largest pivot element */
|
||||
for (big = 0.0, i = j; i < n; i++) {
|
||||
double sum, temp;
|
||||
|
||||
sum = a[i][j];
|
||||
for (k = 0; k < j; k++)
|
||||
sum -= a[i][k] * a[k][j];
|
||||
a[i][j] = sum;
|
||||
|
||||
temp = rscale[i] * fabs(sum); /* Figure of merit */
|
||||
if (temp >= big) {
|
||||
big = temp; /* Best so far */
|
||||
bigi = i; /* Remember index */
|
||||
}
|
||||
}
|
||||
|
||||
/* If we need to interchange rows */
|
||||
if (j != bigi) {
|
||||
{ /* Take advantage of matrix storage to swap pointers to rows */
|
||||
double *temp;
|
||||
temp = a[bigi];
|
||||
a[bigi] = a[j];
|
||||
a[j] = temp;
|
||||
}
|
||||
*rip = -(*rip); /* Another row interchange */
|
||||
rscale[bigi] = rscale[j]; /* Interchange scale factor */
|
||||
}
|
||||
|
||||
pivx[j] = bigi; /* Record pivot */
|
||||
if (fabs(a[j][j]) <= DBL_MIN) {
|
||||
if (rscale != RSCALE)
|
||||
free_dvector(rscale, 0, n-1);
|
||||
return 1; /* Pivot element is zero, so matrix is singular */
|
||||
}
|
||||
|
||||
/* Divide by the pivot element */
|
||||
if (j != (n-1)) {
|
||||
double temp;
|
||||
temp = 1.0/a[j][j];
|
||||
for (i = j+1; i < n; i++)
|
||||
a[i][j] *= temp;
|
||||
}
|
||||
}
|
||||
if (rscale != RSCALE)
|
||||
free_dvector(rscale, 0, n-1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Solve a set of simultaneous equations A.x = b from the */
|
||||
/* LU decomposition, by back substitution. */
|
||||
void
|
||||
lu_backsub(
|
||||
double **a, /* A[][] LU decomposed matrix */
|
||||
int n, /* Dimensionality */
|
||||
int *pivx, /* Pivoting row permutations record */
|
||||
double *b /* Input B[] vector, return X[] */
|
||||
) {
|
||||
int i, j;
|
||||
int nvi; /* When >= 0, indicates non-vanishing B[] index */
|
||||
|
||||
/* Forward substitution, undo pivoting on the fly */
|
||||
for (nvi = -1, i = 0; i < n; i++) {
|
||||
int px;
|
||||
double sum;
|
||||
|
||||
px = pivx[i];
|
||||
sum = b[px];
|
||||
b[px] = b[i];
|
||||
if (nvi >= 0) {
|
||||
for (j = nvi; j < i; j++)
|
||||
sum -= a[i][j] * b[j];
|
||||
} else {
|
||||
if (sum != 0.0)
|
||||
nvi = i; /* Found non-vanishing element */
|
||||
}
|
||||
b[i] = sum;
|
||||
}
|
||||
|
||||
/* Back substitution */
|
||||
for (i = (n-1); i >= 0; i--) {
|
||||
double sum;
|
||||
sum = b[i];
|
||||
for (j = i+1; j < n; j++)
|
||||
sum -= a[i][j] * b[j];
|
||||
b[i] = sum/a[i][i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Improve a solution of equations */
|
||||
void
|
||||
lu_polish(
|
||||
double **a, /* Original A[][] matrix */
|
||||
double **lua, /* LU decomposition of A[][] */
|
||||
int n, /* Dimensionality */
|
||||
double *b, /* B[] vector of equation */
|
||||
double *x, /* X[] solution to be polished */
|
||||
int *pivx /* Pivoting row permutations record */
|
||||
) {
|
||||
int i, j;
|
||||
double *r, R[10]; /* Residuals */
|
||||
|
||||
if (n <= 10)
|
||||
r = R;
|
||||
else
|
||||
r = dvector(0, n-1);
|
||||
|
||||
/* Accumulate the residual error */
|
||||
for (i = 0; i < n; i++) {
|
||||
double sum;
|
||||
sum = -b[i];
|
||||
for (j = 0; j < n; j++)
|
||||
sum += a[i][j] * x[j];
|
||||
r[i] = sum;
|
||||
}
|
||||
|
||||
/* Solve for the error */
|
||||
lu_backsub(lua, n, pivx, r);
|
||||
|
||||
/* Subtract error from the old solution */
|
||||
for (i = 0; i < n; i++)
|
||||
x[i] -= r[i];
|
||||
|
||||
if (r != R)
|
||||
free_dvector(r, 0, n-1);
|
||||
}
|
||||
|
||||
|
||||
/* Invert a matrix A using lu decomposition */
|
||||
/* NOTE that it returns transposed inverse by normal convention. */
|
||||
/* Use sym_matrix_trans() to fix this, or use matrix_trans_mult() */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
lu_invert(
|
||||
double **a, /* A[][] input matrix, returns inversion of A transposed*/
|
||||
int n /* Dimensionality */
|
||||
) {
|
||||
int i, j;
|
||||
double rip; /* Row interchange parity */
|
||||
int *pivx, PIVX[10];
|
||||
double **y;
|
||||
|
||||
if (n <= 10)
|
||||
pivx = PIVX;
|
||||
else
|
||||
pivx = ivector(0, n-1);
|
||||
|
||||
if (lu_decomp(a, n, pivx, &rip)) {
|
||||
if (pivx != PIVX)
|
||||
free_ivector(pivx, 0, n-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Copy lu decomp. to y[][] */
|
||||
y = dmatrix(0, n-1, 0, n-1);
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = 0; j < n; j++) {
|
||||
y[i][j] = a[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
/* Find inverse by columns */
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = 0; j < n; j++)
|
||||
a[i][j] = 0.0;
|
||||
a[i][i] = 1.0;
|
||||
lu_backsub(y, n, pivx, a[i]);
|
||||
}
|
||||
|
||||
/* Clean up */
|
||||
free_dmatrix(y, 0, n-1, 0, n-1);
|
||||
if (pivx != PIVX)
|
||||
free_ivector(pivx, 0, n-1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Invert a matrix A using lu decomposition */
|
||||
/* The normal convention (NOT transpose) inverse is returned */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
lu_invert_normal(
|
||||
double **a, /* A[][] input matrix, returns inversion of A */
|
||||
int n /* Dimensionality */
|
||||
) {
|
||||
int rv;
|
||||
|
||||
if ((rv = lu_invert(a, n)) != 0)
|
||||
return rv;
|
||||
sym_matrix_trans(a, n);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* Invert a matrix A using lu decomposition, and polish it. */
|
||||
/* NOTE that it returns transposed inverse by normal convention. */
|
||||
/* Use sym_matrix_trans() to fix this, or use matrix_trans_mult() */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
lu_polished_invert(
|
||||
double **a, /* A[][] input matrix, returns inversion of A */
|
||||
int n /* Dimensionality */
|
||||
) {
|
||||
int i, j, k;
|
||||
double **aa; /* saved a */
|
||||
double **t1, **t2;
|
||||
|
||||
aa = dmatrix(0, n-1, 0, n-1);
|
||||
t1 = dmatrix(0, n-1, 0, n-1);
|
||||
t2 = dmatrix(0, n-1, 0, n-1);
|
||||
|
||||
/* Copy a to aa */
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = 0; j < n; j++)
|
||||
aa[i][j] = a[i][j];
|
||||
}
|
||||
|
||||
/* Invert a */
|
||||
if ((i = lu_invert(a, n)) != 0) {
|
||||
free_dmatrix(aa, 0, n-1, 0, n-1);
|
||||
free_dmatrix(t1, 0, n-1, 0, n-1);
|
||||
free_dmatrix(t2, 0, n-1, 0, n-1);
|
||||
return i;
|
||||
}
|
||||
|
||||
for (k = 0; k < 20; k++) {
|
||||
matrix_trans_mult(t1, n, n, aa, n, n, a, n, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = 0; j < n; j++) {
|
||||
t2[i][j] = a[i][j];
|
||||
if (i == j)
|
||||
t1[i][j] = 2.0 - t1[i][j];
|
||||
else
|
||||
t1[i][j] = 0.0 - t1[i][j];
|
||||
}
|
||||
}
|
||||
matrix_mult(a, n, n, t2, n, n, t1, n, n);
|
||||
}
|
||||
|
||||
free_dmatrix(aa, 0, n-1, 0, n-1);
|
||||
free_dmatrix(t1, 0, n-1, 0, n-1);
|
||||
free_dmatrix(t2, 0, n-1, 0, n-1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Pseudo-Invert matrix A using lu decomposition */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
lu_psinvert(
|
||||
double **out, /* Output[0..N-1][0..M-1] */
|
||||
double **in, /* Input[0..M-1][0..N-1] input matrix */
|
||||
int m, /* In Rows */
|
||||
int n /* In Columns */
|
||||
) {
|
||||
int rv = 0;
|
||||
double **tr; /* Transpose */
|
||||
double **sq; /* Square matrix */
|
||||
|
||||
tr = dmatrix(0, n-1, 0, m-1);
|
||||
matrix_trans(tr, in, m, n);
|
||||
|
||||
/* Use left inverse */
|
||||
if (m > n) {
|
||||
sq = dmatrix(0, n-1, 0, n-1);
|
||||
|
||||
/* Multiply transpose by input */
|
||||
if ((rv = matrix_mult(sq, n, n, tr, n, m, in, m, n)) == 0) {
|
||||
|
||||
/* Invert the square matrix */
|
||||
if ((rv = lu_invert(sq, n)) == 0) {
|
||||
|
||||
/* Multiply inverted square by transpose */
|
||||
rv = matrix_mult(out, n, m, sq, n, n, tr, n, m);
|
||||
}
|
||||
}
|
||||
free_dmatrix(sq, 0, n-1, 0, n-1);
|
||||
|
||||
/* Use right inverse */
|
||||
} else {
|
||||
sq = dmatrix(0, m-1, 0, m-1);
|
||||
|
||||
/* Multiply input by transpose */
|
||||
if ((rv = matrix_mult(sq, m, m, in, m, n, tr, n, m)) == 0) {
|
||||
|
||||
/* Invert the square matrix */
|
||||
if ((rv = lu_invert(sq, m)) == 0) {
|
||||
|
||||
/* Multiply transpose by inverted square */
|
||||
rv = matrix_mult(out, n, m, tr, n, m, sq, m, m);
|
||||
}
|
||||
}
|
||||
free_dmatrix(sq, 0, m-1, 0, m-1);
|
||||
}
|
||||
free_dmatrix(tr, 0, n-1, 0, m-1);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------- */
|
||||
|
||||
// ~~~ Hmm. Need to verify this code is correct...
|
||||
|
||||
/* Use Cholesky decomposition on a symetric positive-definite matrix. */
|
||||
/* Only the upper triangle of the matrix A is accessed. */
|
||||
/* L returns the decomposition */
|
||||
/* Return nz if A is not positive-definite */
|
||||
int llt_decomp(double **L, double **A, int n) {
|
||||
int i, j, k;
|
||||
double sum;
|
||||
|
||||
/* Scan though upper triangle */
|
||||
for (i = 0; i < n; i++) {
|
||||
|
||||
for (j = i; j < n; j++) {
|
||||
|
||||
sum = A[i][j];
|
||||
for (k = i-1; k >= 0; k--) {
|
||||
sum -= A[i][k] * A[j][k];
|
||||
}
|
||||
|
||||
if (i != j) {
|
||||
L[j][i] = sum/L[i][i];
|
||||
} else {
|
||||
if (sum <= 0.0)
|
||||
return 1;
|
||||
L[i][i] = sqrt(sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Solve a set of simultaneous equations A.x = b from the */
|
||||
/* LLt decomposition, by back substitution. */
|
||||
void llt_backsub(
|
||||
double **L, /* A[][] LLt decomposition in lower triangle */
|
||||
int n, /* Dimensionality */
|
||||
double *b, /* Input B[] */
|
||||
double *x /* Return X[] (may be same as B[]) */
|
||||
) {
|
||||
int i, k;
|
||||
double sum;
|
||||
|
||||
/* Solve L.y = b, storing y in x. */
|
||||
for (i = 0; i < n; i++) {
|
||||
sum = b[i];
|
||||
for (k = i-1; k >= 0; k--)
|
||||
sum -= L[i][k] * x[k];
|
||||
x[i] = sum/L[i][i];
|
||||
}
|
||||
|
||||
/* Solve Lt.x = y */
|
||||
for (i = n; i >= 0; i--) {
|
||||
sum = x[i];
|
||||
for (k = i+1 ; k < n; k++)
|
||||
sum -= L[k][i] * x[k];
|
||||
x[i] = sum/L[i][i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
134
Argyll_V3.5.0/numlib/ludecomp.h
Normal file
134
Argyll_V3.5.0/numlib/ludecomp.h
Normal file
@@ -0,0 +1,134 @@
|
||||
|
||||
#ifndef LUDECOMP_H
|
||||
#define LUDECOMP_H
|
||||
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* NOTE:- lu decomp rearanges the rows of the matrix */
|
||||
/* by swapping pointers rather than exchanging data, */
|
||||
/* so the matrix must be addressed by the **pointer */
|
||||
/* if it is re-used after an ludecomp!!! */
|
||||
|
||||
/* Solve the simultaneous linear equations A.X = B */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
solve_se(
|
||||
double **a, /* A[][] input matrix, returns LU decimposition of A */
|
||||
double *b, /* B[] input array, returns solution X[] */
|
||||
int n /* Dimensionality */
|
||||
);
|
||||
|
||||
/* Solve the simultaneous linear equations A.X = B, with polishing */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
polished_solve_se(
|
||||
double **a, /* A[][] input matrix, returns LU decimposition of A */
|
||||
double *b, /* B[] input array, returns solution X[] */
|
||||
int n /* Dimensionality */
|
||||
);
|
||||
|
||||
/* Decompose the square matrix A[][] into lower and upper triangles */
|
||||
/* NOTE that rows get swaped by swapping matrix pointers! */
|
||||
/* Return 1 if the matrix is singular. */
|
||||
int
|
||||
lu_decomp(
|
||||
double **a, /* A input array, output upper and lower triangles. */
|
||||
int n, /* Dimensionality */
|
||||
int *pivx, /* Return pivoting row permutations record */
|
||||
double *rip /* Row interchange parity, +/- 1.0, used for determinant */
|
||||
);
|
||||
|
||||
/* Solve a set of simultaneous equations from the */
|
||||
/* LU decomposition, by back substitution. */
|
||||
void
|
||||
lu_backsub(
|
||||
double **a, /* A[][] LU decomposed matrix */
|
||||
int n, /* Dimensionality */
|
||||
int *pivx, /* Pivoting row permutations record */
|
||||
double *b /* Input B[] vecor, return X[] */
|
||||
);
|
||||
|
||||
/* Polish a solution for equations */
|
||||
void
|
||||
lu_polish(
|
||||
double **a, /* Original A[][] matrix */
|
||||
double **lua, /* LU decomposition of A[][] */
|
||||
int n, /* Dimensionality */
|
||||
double *b, /* B[] vector of equation */
|
||||
double *x, /* X[] solution to be polished */
|
||||
int *pivx /* Pivoting row permutations record */
|
||||
);
|
||||
|
||||
/* Invert a matrix A using lu decomposition */
|
||||
/* NOTE that it returns transposed inverse by normal convention. */
|
||||
/* Use sym_matrix_trans() to fix this, or use matrix_trans_mult() */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
lu_invert(
|
||||
double **a, /* A[][] input matrix, returns inversion of A transposed */
|
||||
int n /* Dimensionality */
|
||||
);
|
||||
|
||||
/* Invert a matrix A using lu decomposition */
|
||||
/* The normal convention inverse is returned */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
lu_invert_normal(
|
||||
double **a, /* A[][] input matrix, returns inversion of A */
|
||||
int n /* Dimensionality */
|
||||
);
|
||||
|
||||
/* Invert a matrix A using lu decomposition, and polish it. */
|
||||
/* NOTE that it returns transposed inverse by normal convention. */
|
||||
/* Use sym_matrix_trans() to fix this, or use matrix_trans_mult() */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
lu_polished_invert(
|
||||
double **a, /* A[][] input matrix, returns inversion of A */
|
||||
int n /* Dimensionality */
|
||||
);
|
||||
|
||||
/* Pseudo-Invert matrix A using lu decomposition */
|
||||
/* Return 1 if the matrix is singular, 0 if OK */
|
||||
int
|
||||
lu_psinvert(
|
||||
double **out, /* Output[0..N-1][0..M-1] */
|
||||
double **in, /* Input[0..M-1][0..N-1] input matrix */
|
||||
int m, /* In Rows */
|
||||
int n /* In Columns */
|
||||
);
|
||||
|
||||
/* - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
/* Use Cholesky decomposition on a symetric positive-definite matrix. */
|
||||
/* Only the upper triangle of the matrix A is accessed. */
|
||||
/* L returns the decomposition */
|
||||
/* Return nz if A is not positive-definite */
|
||||
int llt_decomp(double **L, double **A, int n);
|
||||
|
||||
|
||||
/* Solve a set of simultaneous equations A.x = b from the */
|
||||
/* LLt decomposition, by back substitution. */
|
||||
void llt_backsub(
|
||||
double **L, /* A[][] LLt decomposition in lower triangle */
|
||||
int n, /* Dimensionality */
|
||||
double *b, /* Input B[] */
|
||||
double *x /* Return X[] (may be same as B[]) */
|
||||
);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LUDECOMP_H */
|
||||
23
Argyll_V3.5.0/numlib/numlib.h
Normal file
23
Argyll_V3.5.0/numlib/numlib.h
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
#ifndef NUMLIB_H
|
||||
#define NUMLIB_H
|
||||
|
||||
/* Numerical routine library interface declarations */
|
||||
|
||||
#include "numsup.h" /* Support routines, macros */
|
||||
#include "dnsq.h" /* Non-linear equation solver */
|
||||
#include "gnewt.h" /* Global Newton non-linear equation solver */
|
||||
#include "powell.h" /* Powell and Conjugate Gradient multi dimentional minimiser */
|
||||
#include "varmet.h" /* Variable Metric multi dimentional minimiser */
|
||||
#include "dhsx.h" /* Downhill simplex multi dimentional minimiser */
|
||||
//#include "randopt.h" /* Random sample multivariate optimser */
|
||||
#include "ludecomp.h" /* LU decomposition matrix solver */
|
||||
#include "svd.h" /* Singular Value decomposition matrix solver */
|
||||
#include "zbrent.h" /* 1 dimentional brent root search */
|
||||
#include "rand.h" /* Random number generators */
|
||||
#include "sobol.h" /* Sub-random vector generators */
|
||||
#include "aatree.h" /* Anderson balanced binary sorted tree */
|
||||
#include "quadprog.h" /* Quadradic Programming solution */
|
||||
#include "roots.h" /* Quadratic, Cubic and Quartic root solving */
|
||||
|
||||
#endif /* NUMLIB_H */
|
||||
4348
Argyll_V3.5.0/numlib/numsup.c
Normal file
4348
Argyll_V3.5.0/numlib/numsup.c
Normal file
File diff suppressed because it is too large
Load Diff
1071
Argyll_V3.5.0/numlib/numsup.h
Normal file
1071
Argyll_V3.5.0/numlib/numsup.h
Normal file
File diff suppressed because it is too large
Load Diff
1194
Argyll_V3.5.0/numlib/powell.c
Normal file
1194
Argyll_V3.5.0/numlib/powell.c
Normal file
File diff suppressed because it is too large
Load Diff
112
Argyll_V3.5.0/numlib/powell.h
Normal file
112
Argyll_V3.5.0/numlib/powell.h
Normal file
@@ -0,0 +1,112 @@
|
||||
#ifndef POWELL_H
|
||||
#define POWELL_H
|
||||
|
||||
/* Powell and Conjugate Gradient multivariate minimiser */
|
||||
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DFUNC_NRV 1e100
|
||||
|
||||
/* Standard interface for powell function */
|
||||
/* return 0 on sucess, 1 on failure due to excessive itterations */
|
||||
/* Result will be in cp */
|
||||
/* Arrays start at 0 */
|
||||
int powell(
|
||||
double *rv, /* If not NULL, return the residual error */
|
||||
int di, /* Dimentionality */
|
||||
double cp[], /* Initial starting point */
|
||||
double s[], /* Size of initial search area */
|
||||
double ftol, /* Tollerance of error change to stop on */
|
||||
int maxit, /* Maximum iterations allowed */
|
||||
double (*funk)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
void *fdata, /* Opaque data needed by func() */
|
||||
void (*prog)(void *pdata, int perc), /* Optional progress percentage callback */
|
||||
void *pdata /* Opaque data needed by prog() */
|
||||
);
|
||||
|
||||
/* Conjugate Gradient optimiser using partial derivatives. */
|
||||
/* return 0 on sucess, 1 on failure due to excessive itterations */
|
||||
/* Result will be in cp */
|
||||
int conjgrad(
|
||||
double *rv, /* If not NULL, return the residual error */
|
||||
int di, /* Dimentionality */
|
||||
double cp[], /* Initial starting point */
|
||||
double s[], /* Size of initial search area */
|
||||
double ftol, /* Tollerance of error change to stop on */
|
||||
int maxit, /* Maximum iterations allowed */
|
||||
double (*func)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
double (*dfunc)(void *fdata, double dp[], double tp[]), /* Gradient & function to evaluate */
|
||||
/* dfunc() should return DFUNC_NRV if it doesn't return function value */
|
||||
void *fdata, /* Opaque data needed by function */
|
||||
void (*prog)(void *pdata, int perc), /* Optional progress percentage callback */
|
||||
void *pdata /* Opaque data needed by prog() */
|
||||
);
|
||||
|
||||
/* conjgrad using one callback function (uses numerical aprox., gradient function). */
|
||||
/* return 0 on sucess, 1 on failure due to excessive itterations */
|
||||
/* Result will be in cp */
|
||||
/* Arrays start at 0 */
|
||||
int conjgrad1(
|
||||
double *rv, /* If not NULL, return the residual error */
|
||||
int di, /* Dimentionality */
|
||||
double cp[], /* Initial starting point */
|
||||
double s[], /* Size of initial search area */
|
||||
double ftol, /* Tollerance of error change to stop on */
|
||||
int maxit, /* Maximum iterations allowed */
|
||||
double (*funk)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
void *fdata, /* Opaque data needed by func() */
|
||||
void (*prog)(void *pdata, int perc), /* Optional progress percentage callback */
|
||||
void *pdata /* Opaque data needed by prog() */
|
||||
);
|
||||
|
||||
/* Example user function declarations */
|
||||
double powell_funk( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[]); /* Multivriate input value */
|
||||
|
||||
/* Line bracketing and minimisation routine. */
|
||||
/* Return value at minimum. */
|
||||
double linmin(
|
||||
double cp[], /* Start point, and returned value */
|
||||
double xi[], /* Search vector */
|
||||
int di, /* Dimensionality */
|
||||
#ifdef ABSTOL
|
||||
double ftol, /* Absolute tolerance to stop on */
|
||||
#else
|
||||
double ftol, /* Relative tolerance to stop on */
|
||||
#endif
|
||||
double (*func)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
void *fdata /* Opaque data for func() */
|
||||
);
|
||||
|
||||
/* Line bracketing and minimisation routine using derivatives. */
|
||||
/* Return value at minimum. */
|
||||
double linmind(
|
||||
double cp[], /* Start point, and returned value */
|
||||
double xi[], /* Search vector */
|
||||
int di, /* Dimensionality */
|
||||
#ifdef ABSTOL
|
||||
double ftol, /* Absolute tolerance to stop on */
|
||||
#else
|
||||
double ftol, /* Relative tolerance to stop on */
|
||||
#endif
|
||||
double (*func)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
double (*dfunc)(void *fdata, double dp[], double tp[]), /* Gradient & function to evaluate */
|
||||
void *fdata /* Opaque data for func() */
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* POWELL_H */
|
||||
105
Argyll_V3.5.0/numlib/qptest.c
Normal file
105
Argyll_V3.5.0/numlib/qptest.c
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
|
||||
This file contains just an example on how to set-up the matrices for using with
|
||||
the quadprog() function.
|
||||
|
||||
The test problem is the following:
|
||||
|
||||
Given:
|
||||
G = 4 -2 g0^T = [6 0]
|
||||
-2 4
|
||||
|
||||
Solve:
|
||||
min f(x) = 1/2 x G x + g0 x
|
||||
s.t.
|
||||
x_1 + x_2 = 3
|
||||
x_1 >= 0
|
||||
x_2 >= 0
|
||||
x_1 + x_2 >= 2
|
||||
|
||||
The solution is x^T = [1 2] and f(x) = 12
|
||||
|
||||
*/
|
||||
|
||||
#include "stdio.h"
|
||||
#include "numlib.h"
|
||||
#include "quadprog.h"
|
||||
|
||||
int main() {
|
||||
double **GG, **G, **CE, **CI;
|
||||
double *g0, *ce0, *ci0, *x;
|
||||
int n, m, p;
|
||||
int i, j;
|
||||
double f, sum = 0.0;
|
||||
|
||||
n = 2;
|
||||
|
||||
/* Orginal, unchanged G */
|
||||
GG = dmatrix(0, n-1, 0, n-1);
|
||||
GG[0][0] = 4.0;
|
||||
GG[0][1] = -2.0;
|
||||
GG[1][0] = -2.0;
|
||||
GG[1][1] = 4.0;
|
||||
|
||||
/* Working copy - gets modified */
|
||||
G = dmatrix(0, n-1, 0, n-1);
|
||||
copy_dmatrix(G, GG, 0, n-1, 0, n-1);
|
||||
|
||||
g0 = dvector(0, n-1);
|
||||
g0[0] = 6.0;
|
||||
g0[1] = 0.0;
|
||||
|
||||
p = 1;
|
||||
|
||||
CE = dmatrix(0, n-1, 0, p-1);
|
||||
CE[0][0] = 1.0;
|
||||
CE[1][0] = 1.0;
|
||||
|
||||
ce0 = dvector(0, p-1);
|
||||
ce0[0] = -3.0;
|
||||
|
||||
m = 3;
|
||||
CI = dmatrix(0, n-1, 0, m-1);
|
||||
CI[0][0] = 1.0;
|
||||
CI[0][1] = 0.0;
|
||||
CI[0][2] = 1.0;
|
||||
CI[1][0] = 0.0;
|
||||
CI[1][1] = 1.0;
|
||||
CI[1][2] = 1.0;
|
||||
|
||||
ci0 = dvector(0, m-1);
|
||||
ci0[0] = 0.0;
|
||||
ci0[1] = 0.0;
|
||||
ci0[2] = -2.0;
|
||||
|
||||
x = dvector(0, n-1);
|
||||
|
||||
f = quadprog(x, G, g0, CE, ce0, CI, ci0, n, p, m);
|
||||
|
||||
if (f == QP_INFEASIBLE)
|
||||
printf("Failed to find solution\n");
|
||||
else {
|
||||
printf("Function value at %f %f = %f\n",x[0],x[1],f);
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
sum += x[i] * GG[i][j] * x[j];
|
||||
sum *= 0.5;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
sum += g0[i] * x[i];
|
||||
|
||||
printf("Double check funtion value %f\n",sum);
|
||||
}
|
||||
|
||||
free_dmatrix(GG, 0, n-1, 0, n-1);
|
||||
free_dmatrix(G, 0, n-1, 0, n-1);
|
||||
free_dvector(g0, 0, n-1);
|
||||
free_dmatrix(CE, 0, n-1, 0, p-1);
|
||||
free_dvector(ce0, 0, p-1);
|
||||
free_dmatrix(CI, 0, n-1, 0, m-1);
|
||||
free_dvector(ci0, 0, m-1);
|
||||
free_dvector(x, 0, n-1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
860
Argyll_V3.5.0/numlib/quadprog.c
Normal file
860
Argyll_V3.5.0/numlib/quadprog.c
Normal file
@@ -0,0 +1,860 @@
|
||||
|
||||
/*
|
||||
Quadradic Programming soliution.
|
||||
|
||||
Based on Luca Di Gaspero's QuadProgpp++, made available with
|
||||
the following license:
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2007-2016 Luca Di Gaspero
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Translation to C by Graeme W. Gill, Copyright 2017, also licensed under the
|
||||
above license.
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
The quadprog_solve() function implements the algorithm of Goldfarb and Idnani
|
||||
for the solution of a (convex) Quadratic Programming problem
|
||||
by means of an active-set dual method.
|
||||
|
||||
The problem is in the form:
|
||||
|
||||
min 0.5 * x G x + g0 x
|
||||
s.t.
|
||||
CE^T x + ce0 = 0
|
||||
CI^T x + ci0 >= 0
|
||||
|
||||
The matrix and vectors dimensions are as follows:
|
||||
G: n * n
|
||||
g0: n
|
||||
|
||||
CE: n * p
|
||||
ce0: p
|
||||
|
||||
CI: n * m
|
||||
ci0: m
|
||||
|
||||
x: n
|
||||
|
||||
References: D. Goldfarb, A. Idnani. A numerically stable dual method for solving
|
||||
strictly convex quadratic programs. Mathematical Programming 27 (1983) pp. 1-33.
|
||||
|
||||
*/
|
||||
|
||||
#include "numsup.h"
|
||||
#include "quadprog.h"
|
||||
|
||||
#undef TRACE_SOLVER /* Print progress */
|
||||
|
||||
/* Utility functions for updating some data needed by the solution method */
|
||||
INLINE static void compute_d(double *d, double **J, double *np, int n);
|
||||
INLINE static void update_z(double *z, double **J, double *d, int iq, int n);
|
||||
INLINE static void update_r(double **R, double *r, double *d, int iq, int n);
|
||||
static int add_constraint(double **R, double **J, double *d, int *piq, double *prnorm, int n);
|
||||
INLINE static void delete_constraint(double **R, double **J, int *A, double *u,
|
||||
int n, int p, int *piq, int l);
|
||||
|
||||
/* Utility functions for computing the Cholesky decomposition and solving */
|
||||
/* linear systems */
|
||||
static void cholesky_decomposition(double **A, int n);
|
||||
static void cholesky_solve(double **L, double *x, double *b, int n);
|
||||
INLINE static void forward_elimination(double **L, double *y, double *b, int n);
|
||||
INLINE static void backward_elimination(double **U, double *x, double *y, int n);
|
||||
|
||||
/* Utility functions for computing the scalar product and the euclidean */
|
||||
/* distance between two numbers */
|
||||
INLINE static double scalar_product(double *x, double *y, int n);
|
||||
INLINE static double distance(double a, double b);
|
||||
|
||||
/* Utility functions for printing vectors and matrices */
|
||||
static void print_matrix(char* name, double **A, int n, int m);
|
||||
|
||||
static void print_vector(char* name, double *v, int n);
|
||||
static void print_ivector(char* name, int *v, int n);
|
||||
|
||||
#define FMIN(A,B) ((A) < (B) ? (A) : (B))
|
||||
|
||||
#define FMAX(A,B) ((A) > (B) ? (A) : (B))
|
||||
|
||||
#define ASZ 10
|
||||
#define VSZ 20
|
||||
|
||||
double quadprog( /* Return solution cost, QP_INFEASIBLE if infeasible/error */
|
||||
double *x, /* Return x[n] value */
|
||||
double **G, /* G[n][n] Quadratic combination matrix - modified */
|
||||
double *g0, /* g0[n] Direct vector */
|
||||
double **CE, /* CE[n][p] Equality constraint matrix */
|
||||
double *ce0, /* ce0[p] Equality constraing constants */
|
||||
double **CI, /* CI[n][m] Constraint matrix */
|
||||
double *ci0, /* cie[m] Constraint constants */
|
||||
int n, /* Number of variables */
|
||||
int p, /* Number of equalities */
|
||||
int m /* Number of constraints */
|
||||
) {
|
||||
int i, j, k, l; /* indices */
|
||||
int ip; /* index of the constraint to be added to the active set */
|
||||
double f_value = QP_INFEASIBLE, psi, c1, c2, sum, ss, R_norm;
|
||||
double inf;
|
||||
double t, t1, t2; /* t is the step length, which is the minimum of the partial */
|
||||
/* step length t1 and the full step length t2 */
|
||||
int q, iq, iter = 0;
|
||||
|
||||
double **R, **J;
|
||||
double *s, *z, *r, *d, *np, *u, *x_old, *u_old;
|
||||
int *A, *A_old, *iai;
|
||||
short *iaexcl;
|
||||
|
||||
double *_R[ASZ], __R[ASZ][ASZ], *_J[ASZ], __J[ASZ][ASZ];
|
||||
double _s[VSZ], _z[VSZ], _r[VSZ], _d[VSZ], _np[VSZ], _u[VSZ], _x_old[VSZ], _u_old[VSZ];
|
||||
int _A[VSZ], _A_old[VSZ], _iai[VSZ];
|
||||
short _iaexcl[VSZ];
|
||||
|
||||
/* Avoid malloc's if we can.. */
|
||||
if (n <= ASZ) {
|
||||
R = _R;
|
||||
J = _J;
|
||||
for (i = 0; i < n; i++) {
|
||||
_R[i] = __R[i];
|
||||
_J[i] = __J[i];
|
||||
}
|
||||
} else {
|
||||
R = dmatrix(0, n-1, 0, n-1);
|
||||
J = dmatrix(0, n-1, 0, n-1);
|
||||
}
|
||||
|
||||
if (n <= VSZ) {
|
||||
x_old = _x_old;
|
||||
z = _z;
|
||||
d = _d;
|
||||
np = _np;
|
||||
} else {
|
||||
x_old = dvector(0, n-1);
|
||||
z = dvector(0, n-1);
|
||||
d = dvector(0, n-1);
|
||||
np = dvector(0, n-1);
|
||||
}
|
||||
|
||||
if ((m + p) <= VSZ) {
|
||||
s = _s;
|
||||
r = _r;
|
||||
u = _u;
|
||||
u_old = _u_old;
|
||||
A = _A;
|
||||
A_old = _A_old;
|
||||
iai = _iai;
|
||||
iaexcl = _iaexcl;
|
||||
} else {
|
||||
s = dvector(0, m + p-1);
|
||||
r = dvector(0, m + p-1);
|
||||
u = dvector(0, m + p-1);
|
||||
u_old = dvector(0, m + p-1);
|
||||
A = ivector(0, m + p-1);
|
||||
A_old = ivector(0, m + p-1);
|
||||
iai = ivector(0, m + p-1);
|
||||
iaexcl = svector(0, m + p-1);
|
||||
}
|
||||
|
||||
q = 0; /* size of the active set A (containing the indices of the active constraints) */
|
||||
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("\nStarting solve_quadprog\n");
|
||||
print_matrix("G", G, n, n);
|
||||
print_vector("g0", g0, n);
|
||||
print_matrix("CE", CE, n, p);
|
||||
print_vector("ce0", ce0, p);
|
||||
print_matrix("CI", CI, n, m);
|
||||
print_vector("ci0", ci0, m);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Preprocessing phase
|
||||
*/
|
||||
|
||||
/* compute the trace of the original matrix G */
|
||||
c1 = 0.0;
|
||||
for (i = 0; i < n; i++)
|
||||
c1 += G[i][i];
|
||||
/* decompose the matrix G in the form L^T L */
|
||||
cholesky_decomposition(G, n);
|
||||
#ifdef TRACE_SOLVER
|
||||
print_matrix("G", G, n, n);
|
||||
#endif
|
||||
|
||||
/* initialize the matrix R */
|
||||
for (i = 0; i < n; i++) {
|
||||
d[i] = 0.0;
|
||||
for (j = 0; j < n; j++)
|
||||
R[i][j] = 0.0;
|
||||
}
|
||||
R_norm = 1.0; /* this variable will hold the norm of the matrix R */
|
||||
|
||||
/* compute the inverse of the factorized matrix G^-1, this is the initial value for H */
|
||||
c2 = 0.0;
|
||||
for (i = 0; i < n; i++) {
|
||||
d[i] = 1.0;
|
||||
forward_elimination(G, z, d, n);
|
||||
for (j = 0; j < n; j++)
|
||||
J[i][j] = z[j];
|
||||
c2 += z[i];
|
||||
d[i] = 0.0;
|
||||
}
|
||||
#ifdef TRACE_SOLVER
|
||||
print_matrix("J", J, n, n);
|
||||
#endif
|
||||
|
||||
/* c1 * c2 is an estimate for cond(G) */
|
||||
|
||||
/* Find the unconstrained minimizer of the quadratic form 0.5 * x G x + g0 x */
|
||||
/* this is a feasible point in the dual space */
|
||||
/* x = G^-1 * g0 */
|
||||
cholesky_solve(G, x, g0, n);
|
||||
for (i = 0; i < n; i++)
|
||||
x[i] = -x[i];
|
||||
/* and compute the current solution value */
|
||||
f_value = 0.5 * scalar_product(g0, x, n);
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Unconstrained solution: f_value %f\n",f_value);
|
||||
print_vector("x", x, n);
|
||||
#endif
|
||||
|
||||
/* Add equality constraints to the working set A */
|
||||
iq = 0;
|
||||
for (i = 0; i < p; i++) {
|
||||
for (j = 0; j < n; j++)
|
||||
np[j] = CE[j][i];
|
||||
compute_d(d, J, np, n);
|
||||
update_z(z, J, d, iq, n);
|
||||
update_r(R, r, d, iq, n);
|
||||
#ifdef TRACE_SOLVER
|
||||
print_matrix("R", R, n, n);
|
||||
print_vector("z", z, n);
|
||||
print_vector("r", r, iq);
|
||||
print_vector("d", d, n);
|
||||
#endif
|
||||
|
||||
/* compute full step length t2: i.e., the minimum step in primal space s.t. */
|
||||
/* the contraint becomes feasible */
|
||||
t2 = 0.0;
|
||||
if (fabs(scalar_product(z, z, n)) > DBL_EPSILON) /* i.e. z != 0 */
|
||||
t2 = (-scalar_product(np, x, n) - ce0[i]) / scalar_product(z, np, n);
|
||||
|
||||
/* set x = x + t2 * z */
|
||||
for (k = 0; k < n; k++)
|
||||
x[k] += t2 * z[k];
|
||||
|
||||
/* set u = u+ */
|
||||
u[iq] = t2;
|
||||
for (k = 0; k < iq; k++)
|
||||
u[k] -= t2 * r[k];
|
||||
|
||||
/* compute the new solution value */
|
||||
f_value += 0.5 * (t2 * t2) * scalar_product(z, np, n);
|
||||
A[i] = -i - 1;
|
||||
|
||||
if (!add_constraint(R, J, d, &iq, &R_norm, n)) {
|
||||
/* Equality constraints are linearly dependent */
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Constraints are linearly dependent\n");
|
||||
#endif
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
/* set iai = K \ A */
|
||||
for (i = 0; i < m; i++)
|
||||
iai[i] = i;
|
||||
|
||||
/* Hmm. Use of crossing goto's is a little Fortran like... */
|
||||
|
||||
l1: iter++;
|
||||
#ifdef TRACE_SOLVER
|
||||
print_vector("x", x, n);
|
||||
#endif
|
||||
/* step 1: choose a violated constraint */
|
||||
for (i = p; i < iq; i++) {
|
||||
ip = A[i];
|
||||
iai[ip] = -1;
|
||||
}
|
||||
|
||||
/* compute s[x] = ci^T * x + ci0 for all elements of K \ A */
|
||||
ss = 0.0;
|
||||
psi = 0.0; /* this value will contain the sum of all infeasibilities */
|
||||
ip = 0; /* ip will be the index of the chosen violated constraint */
|
||||
for (i = 0; i < m; i++) {
|
||||
iaexcl[i] = 1;
|
||||
sum = 0.0;
|
||||
for (j = 0; j < n; j++)
|
||||
sum += CI[j][i] * x[j];
|
||||
sum += ci0[i];
|
||||
s[i] = sum;
|
||||
psi += FMIN(0.0, sum);
|
||||
}
|
||||
#ifdef TRACE_SOLVER
|
||||
print_vector("s", s, m);
|
||||
#endif
|
||||
|
||||
if (fabs(psi) <= m * DBL_EPSILON * c1 * c2* 100.0) {
|
||||
/* numerically there are no infeasibilities anymore */
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("numerically there are no infeasibilities anymore\n");
|
||||
#endif
|
||||
q = iq;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* save old values for u and A */
|
||||
for (i = 0; i < iq; i++) {
|
||||
u_old[i] = u[i];
|
||||
A_old[i] = A[i];
|
||||
}
|
||||
|
||||
/* and for x */
|
||||
for (i = 0; i < n; i++)
|
||||
x_old[i] = x[i];
|
||||
|
||||
l2: /* Step 2: check for feasibility and determine a new S-pair */
|
||||
for (i = 0; i < m; i++) {
|
||||
if (s[i] < ss && iai[i] != -1 && iaexcl[i]) {
|
||||
ss = s[i];
|
||||
ip = i;
|
||||
}
|
||||
}
|
||||
if (ss >= 0.0) {
|
||||
#ifdef TRACE_SOLVER
|
||||
printf(" ss %f >= 0.0\n",ss);
|
||||
#endif
|
||||
q = iq;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* set np = n[ip] */
|
||||
for (i = 0; i < n; i++)
|
||||
np[i] = CI[i][ip];
|
||||
/* set u = [u 0]^T */
|
||||
u[iq] = 0.0;
|
||||
/* add ip to the active set A */
|
||||
A[iq] = ip;
|
||||
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Trying with constraint %d\n",ip);
|
||||
print_vector("np", np, n);
|
||||
#endif
|
||||
|
||||
l2a:
|
||||
/* Step 2a: determine step direction */
|
||||
/* compute z = H np: the step direction in the primal space (through J, see the paper) */
|
||||
compute_d(d, J, np, n);
|
||||
update_z(z, J, d, iq, n);
|
||||
/* compute N* np (if q > 0): the negative of the step direction in the dual space */
|
||||
update_r(R, r, d, iq, n);
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Step direction z\n");
|
||||
print_vector("z", z, n);
|
||||
print_vector("r", r, iq + 1);
|
||||
print_vector("u", u, iq + 1);
|
||||
print_vector("d", d, n);
|
||||
print_ivector("A", A, iq + 1);
|
||||
#endif
|
||||
|
||||
/* Step 2b: compute step length */
|
||||
l = 0;
|
||||
/* Compute t1: partial step length (maximum step in dual space */
|
||||
/* without violating dual feasibility */
|
||||
t1 = QP_INFEASIBLE; /* +inf */
|
||||
/* find the index l s.t. it reaches the minimum of u+[x] / r */
|
||||
for (k = p; k < iq; k++) {
|
||||
if (r[k] > 0.0) {
|
||||
if (u[k] / r[k] < t1) {
|
||||
t1 = u[k] / r[k];
|
||||
l = A[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Compute t2: full step length (minimum step in primal space */
|
||||
/* such that the constraint ip becomes feasible */
|
||||
if (fabs(scalar_product(z, z, n)) > DBL_EPSILON) { /* i.e. z != 0 */
|
||||
t2 = -s[ip] / scalar_product(z, np, n);
|
||||
if (t2 < 0) /* patch suggested by Takano Akio for handling numerical inconsistencies */
|
||||
t2 = QP_INFEASIBLE;
|
||||
} else
|
||||
t2 = QP_INFEASIBLE; /* +inf */
|
||||
|
||||
/* the step is chosen as the minimum of t1 and t2 */
|
||||
t = FMIN(t1, t2);
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Step size: %e (t1 = %e, t2 = %e\n",t,t1,t2);
|
||||
#endif
|
||||
|
||||
/* Step 2c: determine new S-pair and take step: */
|
||||
|
||||
/* case (i): no step in primal or dual space */
|
||||
if (t >= QP_INFEASIBLE) {
|
||||
/* QPP is infeasible */
|
||||
// FIXME: unbounded to raise
|
||||
#ifdef TRACE_SOLVER
|
||||
printf(" QPP is infeasible\n");
|
||||
#endif
|
||||
q = iq;
|
||||
f_value = QP_INFEASIBLE;
|
||||
goto done;
|
||||
}
|
||||
/* case (ii): step in dual space */
|
||||
if (t2 >= QP_INFEASIBLE) {
|
||||
/* set u = u + t * [-r 1] and drop constraint l from the active set A */
|
||||
for (k = 0; k < iq; k++)
|
||||
u[k] -= t * r[k];
|
||||
u[iq] += t;
|
||||
iai[l] = l;
|
||||
delete_constraint(R, J, A, u, n, p, &iq, l);
|
||||
#ifdef TRACE_SOLVER
|
||||
printf(" in dual space: %f\n",f_value);
|
||||
print_vector("x", x, n);
|
||||
print_vector("z", z, n);
|
||||
print_ivector("A", A, iq + 1);
|
||||
#endif
|
||||
goto l2a;
|
||||
}
|
||||
|
||||
/* case (iii): step in primal and dual space */
|
||||
|
||||
/* set x = x + t * z */
|
||||
for (k = 0; k < n; k++)
|
||||
x[k] += t * z[k];
|
||||
/* update the solution value */
|
||||
f_value += t * scalar_product(z, np, n) * (0.5 * t + u[iq]);
|
||||
/* u = u + t * [-r 1] */
|
||||
for (k = 0; k < iq; k++)
|
||||
u[k] -= t * r[k];
|
||||
u[iq] += t;
|
||||
#ifdef TRACE_SOLVER
|
||||
printf(" in both spaces: %f\n",f_value);
|
||||
print_vector("x", x, n);
|
||||
print_vector("u", u, iq + 1);
|
||||
print_vector("r", r, iq + 1);
|
||||
print_ivector("A", A, iq + 1);
|
||||
#endif
|
||||
|
||||
if (fabs(t - t2) < DBL_EPSILON) {
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Full step has been taken %f\n",t);
|
||||
print_vector("x", x, n);
|
||||
#endif
|
||||
/* full step has been taken */
|
||||
/* Add constraint ip to the active set*/
|
||||
if (!add_constraint(R, J, d, &iq, &R_norm, n)) {
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("add_constraint failed - removing constraint ant step\n",t);
|
||||
#endif
|
||||
iaexcl[ip] = 0;
|
||||
delete_constraint(R, J, A, u, n, p, &iq, ip);
|
||||
#ifdef TRACE_SOLVER
|
||||
print_matrix("R", R, n, n);
|
||||
print_ivector("A", A, iq);
|
||||
print_ivector("iai", iai, m+p);
|
||||
#endif
|
||||
for (i = 0; i < m; i++)
|
||||
iai[i] = i;
|
||||
for (i = p; i < iq; i++) {
|
||||
A[i] = A_old[i];
|
||||
u[i] = u_old[i];
|
||||
iai[A[i]] = -1;
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
x[i] = x_old[i];
|
||||
goto l2; /* go to step 2 */
|
||||
} else
|
||||
iai[ip] = -1;
|
||||
#ifdef TRACE_SOLVER
|
||||
print_matrix("R", R, n, n);
|
||||
print_ivector("A", A, iq);
|
||||
print_ivector("iai", iai, m + p);
|
||||
#endif
|
||||
goto l1;
|
||||
}
|
||||
|
||||
/* a patial step has taken */
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Partial step has taken %f\n",t);
|
||||
print_vector("x", x, n);
|
||||
#endif
|
||||
/* drop constraint l */
|
||||
iai[l] = l;
|
||||
delete_constraint(R, J, A, u, n, p, &iq, l);
|
||||
#ifdef TRACE_SOLVER
|
||||
print_matrix("R", R, n, n);
|
||||
print_ivector("A", A, iq);
|
||||
#endif
|
||||
|
||||
/* update s[ip] = CI * x + ci0 */
|
||||
sum = 0.0;
|
||||
for (k = 0; k < n; k++)
|
||||
sum += CI[k][ip] * x[k];
|
||||
s[ip] = sum + ci0[ip];
|
||||
|
||||
#ifdef TRACE_SOLVER
|
||||
print_vector("s", s, m);
|
||||
#endif
|
||||
goto l2a;
|
||||
|
||||
done:;
|
||||
|
||||
/* Cleanup and return f value */
|
||||
if (n > ASZ) {
|
||||
free_dmatrix(R, 0, n-1, 0, n-1);
|
||||
free_dmatrix(J, 0, n-1, 0, n-1);
|
||||
}
|
||||
|
||||
if (n > VSZ) {
|
||||
free_dvector(x_old, 0, n-1);
|
||||
free_dvector(z, 0, n-1);
|
||||
free_dvector(d, 0, n-1);
|
||||
free_dvector(np, 0, n-1);
|
||||
}
|
||||
|
||||
if ((m + p) > VSZ) {
|
||||
free_dvector(s, 0, m + p-1);
|
||||
free_dvector(r, 0, m + p-1);
|
||||
free_dvector(u, 0, m + p-1);
|
||||
free_dvector(u_old, 0, m + p-1);
|
||||
free_ivector(A, 0, m + p-1);
|
||||
free_ivector(A_old, 0, m + p-1);
|
||||
free_ivector(iai, 0, m + p-1);
|
||||
free_svector(iaexcl, 0, m + p-1);
|
||||
}
|
||||
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Returning f_value %e\n",f_value);
|
||||
print_vector("Returning x", x, n);
|
||||
#endif
|
||||
|
||||
return f_value;
|
||||
}
|
||||
|
||||
INLINE static void compute_d(double *d, double **J, double *np, int n) {
|
||||
int i, j;
|
||||
double sum;
|
||||
|
||||
/* compute d = H^T * np */
|
||||
for (i = 0; i < n; i++) {
|
||||
sum = 0.0;
|
||||
for (j = 0; j < n; j++)
|
||||
sum += J[j][i] * np[j];
|
||||
d[i] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
INLINE static void update_z(double *z, double **J, double *d, int iq, int n) {
|
||||
int i, j;
|
||||
|
||||
/* setting of z = H * d */
|
||||
for (i = 0; i < n; i++) {
|
||||
z[i] = 0.0;
|
||||
for (j = iq; j < n; j++)
|
||||
z[i] += J[i][j] * d[j];
|
||||
}
|
||||
}
|
||||
|
||||
INLINE static void update_r(double **R, double *r, double *d, int iq, int n) {
|
||||
int i, j;
|
||||
double sum;
|
||||
|
||||
/* setting of r = R^-1 d */
|
||||
for (i = iq - 1; i >= 0; i--) {
|
||||
sum = 0.0;
|
||||
for (j = i + 1; j < iq; j++)
|
||||
sum += R[i][j] * r[j];
|
||||
r[i] = (d[i] - sum) / R[i][i];
|
||||
}
|
||||
}
|
||||
|
||||
static int add_constraint(double **R, double **J, double *d, int *piq, double *prnorm, int n) {
|
||||
int iq = *piq;
|
||||
int i, j, k;
|
||||
double cc, ss, h, t1, t2, xny;
|
||||
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Add constraint %d",iq);
|
||||
#endif
|
||||
|
||||
/* we have to find the Givens rotation which will reduce the element */
|
||||
/* d[j] to zero. if it is already zero we don't have to do anything, except */
|
||||
/* of decreasing j */
|
||||
for (j = n - 1; j >= iq + 1; j--) {
|
||||
/* The Givens rotation is done with the matrix (cc cs, cs -cc). */
|
||||
/* If cc is one, then element (j) of d is zero compared with element */
|
||||
/* (j - 1). Hence we don't have to do anything. */
|
||||
/* If cc is zero, then we just have to switch column (j) and column (j - 1) */
|
||||
/* of J. Since we only switch columns in J, we have to be careful how we */
|
||||
/* update d depending on the sign of gs. */
|
||||
/* Otherwise we have to apply the Givens rotation to these columns. */
|
||||
/* The i - 1 element of d has to be updated to h. */
|
||||
cc = d[j - 1];
|
||||
ss = d[j];
|
||||
h = distance(cc, ss);
|
||||
if (fabs(h) < DBL_EPSILON) /* h == 0 */
|
||||
continue;
|
||||
d[j] = 0.0;
|
||||
ss = ss / h;
|
||||
cc = cc / h;
|
||||
if (cc < 0.0) {
|
||||
cc = -cc;
|
||||
ss = -ss;
|
||||
d[j - 1] = -h;
|
||||
} else
|
||||
d[j - 1] = h;
|
||||
xny = ss / (1.0 + cc);
|
||||
for (k = 0; k < n; k++) {
|
||||
t1 = J[k][j - 1];
|
||||
t2 = J[k][j];
|
||||
J[k][j - 1] = t1 * cc + t2 * ss;
|
||||
J[k][j] = xny * (t1 + J[k][j - 1]) - t2;
|
||||
}
|
||||
}
|
||||
/* update the number of constraints added*/
|
||||
*piq = ++iq;
|
||||
/* To update R we have to put the iq components of the d vector into column iq - 1 of R */
|
||||
for (i = 0; i < iq; i++)
|
||||
R[i][iq - 1] = d[i];
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("/%d\n", iq);
|
||||
print_matrix("R", R, iq, iq);
|
||||
print_matrix("J", J, n, n);
|
||||
print_vector("d", d, iq);
|
||||
#endif
|
||||
|
||||
if (fabs(d[iq - 1]) <= DBL_EPSILON * *prnorm) {
|
||||
/* problem degenerate */
|
||||
return 0;
|
||||
}
|
||||
*prnorm = (double)FMAX(*prnorm, fabs(d[iq - 1]));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void delete_constraint(double **R, double **J, int *A, double *u,
|
||||
int n, int p, int *piq, int l) {
|
||||
int iq = *piq;
|
||||
int i, j, k, qq = -1; // just to prevent warnings from smart compilers
|
||||
double cc, ss, h, xny, t1, t2;
|
||||
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("Delete constraint %d %d",l,iq);
|
||||
#endif
|
||||
|
||||
/* Find the index qq for active constraint l to be removed */
|
||||
for (i = p; i < iq; i++) {
|
||||
if (A[i] == l) {
|
||||
qq = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* remove the constraint from the active set and the duals */
|
||||
for (i = qq; i < iq - 1; i++) {
|
||||
A[i] = A[i + 1];
|
||||
u[i] = u[i + 1];
|
||||
for (j = 0; j < n; j++)
|
||||
R[j][i] = R[j][i + 1];
|
||||
}
|
||||
|
||||
A[iq - 1] = A[iq];
|
||||
u[iq - 1] = u[iq];
|
||||
A[iq] = 0;
|
||||
u[iq] = 0.0;
|
||||
for (j = 0; j < iq; j++)
|
||||
R[j][iq - 1] = 0.0;
|
||||
|
||||
/* constraint has been fully removed */
|
||||
*piq = --iq;
|
||||
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("/%d\n",iq);
|
||||
#endif
|
||||
|
||||
if (iq == 0)
|
||||
return;
|
||||
|
||||
for (j = qq; j < iq; j++) {
|
||||
cc = R[j][j];
|
||||
ss = R[j + 1][j];
|
||||
h = distance(cc, ss);
|
||||
if (fabs(h) < DBL_EPSILON) // h == 0
|
||||
continue;
|
||||
cc = cc / h;
|
||||
ss = ss / h;
|
||||
R[j + 1][j] = 0.0;
|
||||
if (cc < 0.0) {
|
||||
R[j][j] = -h;
|
||||
cc = -cc;
|
||||
ss = -ss;
|
||||
} else
|
||||
R[j][j] = h;
|
||||
|
||||
xny = ss / (1.0 + cc);
|
||||
for (k = j + 1; k < iq; k++) {
|
||||
t1 = R[j][k];
|
||||
t2 = R[j + 1][k];
|
||||
R[j][k] = t1 * cc + t2 * ss;
|
||||
R[j + 1][k] = xny * (t1 + R[j][k]) - t2;
|
||||
}
|
||||
for (k = 0; k < n; k++) {
|
||||
t1 = J[k][j];
|
||||
t2 = J[k][j + 1];
|
||||
J[k][j] = t1 * cc + t2 * ss;
|
||||
J[k][j + 1] = xny * (J[k][j] + t1) - t2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
INLINE static double distance(double a, double b) {
|
||||
double a1, b1, t;
|
||||
a1 = fabs(a);
|
||||
b1 = fabs(b);
|
||||
if (a1 > b1) {
|
||||
t = (b1 / a1);
|
||||
return a1 * sqrt(1.0 + t * t);
|
||||
} else if (b1 > a1) {
|
||||
t = (a1 / b1);
|
||||
return b1 * sqrt(1.0 + t * t);
|
||||
}
|
||||
return a1 * sqrt(2.0);
|
||||
}
|
||||
|
||||
|
||||
INLINE static double scalar_product(double *x, double *y, int n) {
|
||||
int i;
|
||||
double sum;
|
||||
|
||||
sum = 0.0;
|
||||
for (i = 0; i < n; i++)
|
||||
sum += x[i] * y[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
// !!! this doesn't work for semi-definite matricies, ie.
|
||||
// they will have diagonal sum == 0.0 !!!
|
||||
static void cholesky_decomposition(double **A, int n) {
|
||||
int i, j, k;
|
||||
double sum;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
for (j = i; j < n; j++) {
|
||||
sum = A[i][j];
|
||||
for (k = i - 1; k >= 0; k--)
|
||||
sum -= A[i][k] * A[j][k];
|
||||
#ifdef TRACE_SOLVER
|
||||
printf("sum[%d][%d] = %f\n",i,j,sum);
|
||||
#endif
|
||||
if (i == j) {
|
||||
if (sum <= 0.0)
|
||||
{
|
||||
// raise error
|
||||
print_matrix("A", A, n, n);
|
||||
error("QuadProg:cholesky decomposition, matrix is not postive definite, sum: %e",sum);
|
||||
}
|
||||
A[i][i] = sqrt(sum);
|
||||
} else
|
||||
A[j][i] = sum / A[i][i];
|
||||
}
|
||||
for (k = i + 1; k < n; k++)
|
||||
A[i][k] = A[k][i];
|
||||
}
|
||||
}
|
||||
|
||||
static void cholesky_solve(double **L, double *x, double *b, int n) {
|
||||
double *y, _y[VSZ];
|
||||
|
||||
if (n <= VSZ)
|
||||
y = _y;
|
||||
else
|
||||
y = dvector(0, n-1);
|
||||
|
||||
/* Solve L * y = b */
|
||||
forward_elimination(L, y, b, n);
|
||||
|
||||
/* Solve L^T * x = y */
|
||||
backward_elimination(L, x, y, n);
|
||||
|
||||
if (n > VSZ)
|
||||
free_dvector(y, 0, n-1);
|
||||
}
|
||||
|
||||
INLINE static void forward_elimination(double **L, double *y, double *b, int n) {
|
||||
int i, j;
|
||||
|
||||
y[0] = b[0] / L[0][0];
|
||||
for (i = 1; i < n; i++) {
|
||||
y[i] = b[i];
|
||||
for (j = 0; j < i; j++)
|
||||
y[i] -= L[i][j] * y[j];
|
||||
y[i] = y[i] / L[i][i];
|
||||
}
|
||||
}
|
||||
|
||||
INLINE static void backward_elimination(double **U, double *x, double *y, int n) {
|
||||
int i, j;
|
||||
|
||||
x[n - 1] = y[n - 1] / U[n - 1][n - 1];
|
||||
for (i = n - 2; i >= 0; i--) {
|
||||
x[i] = y[i];
|
||||
for (j = i + 1; j < n; j++)
|
||||
x[i] -= U[i][j] * x[j];
|
||||
x[i] = x[i] / U[i][i];
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------- */
|
||||
|
||||
static void print_matrix(char *name, double **A, int n, int m) {
|
||||
int i, j;
|
||||
|
||||
printf("%s: \n",name);
|
||||
for (i = 0; i < n; i++) {
|
||||
printf(" ");
|
||||
for (j = 0; j < m; j++)
|
||||
printf("%f%s", A[i][j], j < (m-1) ? ", " : "");
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void print_vector(char *name, double *v, int n) {
|
||||
int i;
|
||||
|
||||
printf("%s: \n",name);
|
||||
printf(" ");
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%f%s", v[i], i < (n-1) ? ", " : "");
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
static void print_ivector(char *name, int *v, int n) {
|
||||
int i;
|
||||
|
||||
printf("%s: \n",name);
|
||||
printf(" ");
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%d%s", v[i], i < (n-1) ? ", " : "");
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
73
Argyll_V3.5.0/numlib/quadprog.h
Normal file
73
Argyll_V3.5.0/numlib/quadprog.h
Normal file
@@ -0,0 +1,73 @@
|
||||
|
||||
#ifndef QUADPROG_H
|
||||
#define QUADPROG_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
Quadradic Programming solution.
|
||||
|
||||
Based on Luca Di Gaspero's QuadProgpp++, made available under the MIT license.
|
||||
|
||||
Translation to C by Graeme W. Gill, Copyright 2017, also licensed under the
|
||||
MIT license.
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
The quadprog_solve() function implements the algorithm of Goldfarb and Idnani
|
||||
for the solution of a (convex) Quadratic Programming problem
|
||||
by means of an active-set dual method.
|
||||
|
||||
The problem is in the form:
|
||||
|
||||
min 0.5 * x G x + g0 x
|
||||
s.t.
|
||||
CE^t x + ce0 = 0
|
||||
CI^t x + ci0 >= 0
|
||||
|
||||
The matrix and vectors dimensions are as follows:
|
||||
G: n * n
|
||||
g0: n
|
||||
|
||||
CE: n * p
|
||||
ce0: p
|
||||
|
||||
CI: n * m
|
||||
ci0: m
|
||||
|
||||
x: n
|
||||
|
||||
References: D. Goldfarb, A. Idnani. A numerically stable dual method for solving
|
||||
strictly convex quadratic programs. Mathematical Programming 27 (1983) pp. 1-33.
|
||||
|
||||
NOTE that this function doesn't work if the objective function (i.e. G + g0) has lower dimension
|
||||
than n. Using dummy terms doesn't seem to work around this.
|
||||
|
||||
It only works with convex problems - i.e. those where it is possible to reach
|
||||
the optimum starting anywhere on the constraint surface.
|
||||
|
||||
*/
|
||||
|
||||
#define QP_INFEASIBLE 1.0E300
|
||||
|
||||
double quadprog( /* Return solution cost, QP_INFEASIBLE if infeasible/error */
|
||||
double *x, /* Return x[n] value */
|
||||
double **G, /* G[n][n] Quadratic combination matrix - modified */
|
||||
double *g0, /* g0[n] Direct vector */
|
||||
double **CE, /* CE[n][p] Equality constraint matrix */
|
||||
double *ce0, /* ce0[p] Equality constraing constants */
|
||||
double **CI, /* CI[n][m] Constraint matrix */
|
||||
double *ci0, /* cie[m] Constraint constants */
|
||||
int n, /* Number of variables */
|
||||
int p, /* Number of equalities */
|
||||
int m /* Number of constraints */
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*define QUADPROG_H */
|
||||
199
Argyll_V3.5.0/numlib/rand.c
Normal file
199
Argyll_V3.5.0/numlib/rand.c
Normal file
@@ -0,0 +1,199 @@
|
||||
/* Integer and floating point random number generator routines */
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include "numsup.h"
|
||||
#include "rand.h"
|
||||
|
||||
/* 32 bit pseudo random sequencer based on XOR feedback */
|
||||
/* generates number between 1 and 4294967295 */
|
||||
#define PSRAND32(S) (((S) & 0x80000000) ? (((S) << 1) ^ 0xa398655d) : ((S) << 1))
|
||||
|
||||
/* 32 bit linear congruent generator */
|
||||
/* generates number between 0 and 4294967295 */
|
||||
/* (From Knuth & H.W.Lewis) */
|
||||
#define PSRAND32L(S) ((S) * 1664525L + 1013904223L)
|
||||
|
||||
/* - - - - - - - - - - - - - - - */
|
||||
/* Global state random generator */
|
||||
|
||||
static rand_state g_rand = { 0 }; /* Use default seed for global state generator */
|
||||
|
||||
/* Return a 32 bit number between 0 and 4294967295 */
|
||||
/* Use Knuth shuffle to improve PSRAND32 sequence */
|
||||
unsigned int
|
||||
rand32( /* Return 32 bit random number */
|
||||
unsigned int seed /* Optional seed. Non-zero re-initialized with that seed */
|
||||
) {
|
||||
return rand32_th(NULL, seed);
|
||||
}
|
||||
|
||||
/* return a random number between 0.0 and 1.0 */
|
||||
/* based on rand32 */
|
||||
double ranno(void) {
|
||||
return rand32_th(NULL, 0) / 4294967295.0;
|
||||
}
|
||||
|
||||
/* Return a uniform random double in the range min to max */
|
||||
double
|
||||
d_rand(double min, double max) {
|
||||
return d_rand_th(NULL, min, max);
|
||||
}
|
||||
|
||||
/* Return a squared distribution random double in the range min to max */
|
||||
double
|
||||
d2_rand(double min, double max) {
|
||||
return d2_rand_th(NULL, min, max);
|
||||
}
|
||||
|
||||
/* Return a random integer in the range min to max inclusive */
|
||||
int
|
||||
i_rand(int min, int max) {
|
||||
return i_rand_th(NULL, min, max);
|
||||
}
|
||||
|
||||
/* Return a random floating point number with a gausian/normal */
|
||||
/* distribution, centered about 0.0, with standard deviation 1.0 */
|
||||
/* This uses the Box-Muller transformation */
|
||||
double norm_rand(void) {
|
||||
return norm_rand_th(NULL);
|
||||
}
|
||||
|
||||
/* - - - - - - - - - - - - - - - */
|
||||
/* Explicit state random generator */
|
||||
|
||||
/* Init rand_state to default */
|
||||
void rand_init(rand_state *p) {
|
||||
if (p == NULL)
|
||||
p = &g_rand;
|
||||
memset((void *)p, 0, sizeof(rand_state));
|
||||
}
|
||||
|
||||
/* Return a 32 bit number between 0 and 4294967295 */
|
||||
/* Use Knuth shuffle to improve PSRAND32 sequence */
|
||||
unsigned int
|
||||
rand32_th(rand_state *p,
|
||||
unsigned int seed /* Optional seed. Non-zero re-initialized with that seed */
|
||||
) {
|
||||
int i;
|
||||
|
||||
if (p == NULL)
|
||||
p = &g_rand;
|
||||
|
||||
if (seed != 0) {
|
||||
//printf("~1 rand 0x%x seed 0x%x\n",p,seed);
|
||||
rand_init(p);
|
||||
p->ran = seed;
|
||||
}
|
||||
|
||||
/* Init random storage locations */
|
||||
if (p->pvs_inited == 0) {
|
||||
if (p->ran == 0)
|
||||
p->ran = RAND_SEED;
|
||||
for (i = 0; i < RAND_TSIZE; i++)
|
||||
p->pvs[i] = p->ran = PSRAND32(p->ran);
|
||||
p->last = p->ran;
|
||||
p->pvs_inited = 1;
|
||||
}
|
||||
i = p->last % RAND_TSIZE; /* New location */
|
||||
p->last = p->pvs[i]; /* Value generated */
|
||||
p->pvs[i] = p->ran = PSRAND32(p->ran); /* New value */
|
||||
|
||||
//printf("~1 rand 0x%x ret 0x%x\n",p,p->last-1);
|
||||
return p->last-1;
|
||||
}
|
||||
|
||||
/* return a random number between 0.0 and 1.0 */
|
||||
/* based on rand32 */
|
||||
double ranno_th(rand_state *p) {
|
||||
return rand32_th(p, 0) / 4294967295.0;
|
||||
}
|
||||
|
||||
/* Return a uniform random double in the range min to max */
|
||||
double
|
||||
d_rand_th(rand_state *p, double min, double max) {
|
||||
return min + (max - min) * ranno_th(p);
|
||||
}
|
||||
|
||||
/* Return a squared distribution random double in the range min to max */
|
||||
double
|
||||
d2_rand_th(rand_state *p, double min, double max) {
|
||||
double val = ranno_th(p);
|
||||
return min + (max - min) * val * val;
|
||||
}
|
||||
|
||||
/* Return a random integer in the range min to max inclusive */
|
||||
int
|
||||
i_rand_th(rand_state *p, int min, int max) {
|
||||
return min + (int)floor(0.5 + ((double)(max - min)) * ranno_th(p));
|
||||
}
|
||||
|
||||
/* Return a random floating point number with a gausian/normal */
|
||||
/* distribution, centered about 0.0, with standard deviation 1.0 */
|
||||
/* This uses the Box-Muller transformation */
|
||||
double norm_rand_th(rand_state *p) {
|
||||
if (p == NULL)
|
||||
p = &g_rand;
|
||||
|
||||
if (p->r2 == 0) { /* No previously calculated number */
|
||||
double v1, v2, t1, t2, r1;
|
||||
do {
|
||||
v1 = d_rand_th(p, -1.0, 1.0);
|
||||
v2 = d_rand_th(p, -1.0, 1.0);
|
||||
t1 = v1 * v1 + v2 * v2;
|
||||
} while (t1 == 0.0 || t1 >= 1.0);
|
||||
t2 = sqrt(-2.0 * log(t1)/t1);
|
||||
p->nr2 = v2 * t2; /* One for next time */
|
||||
p->r2 = 1;
|
||||
r1 = v1 * t2;
|
||||
return r1;
|
||||
} else { /* Return previously calculated number */
|
||||
p->r2 = 0;
|
||||
return p->nr2;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set random dvector */
|
||||
void vect_rand(double *d, double min, double max, int len) {
|
||||
int i;
|
||||
for (i = 0; i < len; i++)
|
||||
d[i] = d_rand(min, max);
|
||||
}
|
||||
|
||||
/* =================================================================== */
|
||||
|
||||
/* Scale normal value by this to give it a mean absolute deviation of 1.0 */
|
||||
/* for a given multi-variate dimension */
|
||||
|
||||
double NORM_RAND_ABS_SCALE[NORM_RAND_ABS_SCALE_MAXD+1] = {
|
||||
0.0,
|
||||
NORM_RAND_ABS_SCALE_1,
|
||||
NORM_RAND_ABS_SCALE_2,
|
||||
NORM_RAND_ABS_SCALE_3,
|
||||
NORM_RAND_ABS_SCALE_4,
|
||||
NORM_RAND_ABS_SCALE_5,
|
||||
NORM_RAND_ABS_SCALE_6,
|
||||
NORM_RAND_ABS_SCALE_7,
|
||||
NORM_RAND_ABS_SCALE_8,
|
||||
NORM_RAND_ABS_SCALE_9,
|
||||
NORM_RAND_ABS_SCALE_10,
|
||||
NORM_RAND_ABS_SCALE_11,
|
||||
NORM_RAND_ABS_SCALE_12,
|
||||
NORM_RAND_ABS_SCALE_13,
|
||||
NORM_RAND_ABS_SCALE_14,
|
||||
NORM_RAND_ABS_SCALE_15,
|
||||
NORM_RAND_ABS_SCALE_16,
|
||||
NORM_RAND_ABS_SCALE_17,
|
||||
NORM_RAND_ABS_SCALE_18,
|
||||
NORM_RAND_ABS_SCALE_19
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
114
Argyll_V3.5.0/numlib/rand.h
Normal file
114
Argyll_V3.5.0/numlib/rand.h
Normal file
@@ -0,0 +1,114 @@
|
||||
#ifndef RAND_H
|
||||
#define RAND_H
|
||||
|
||||
/*
|
||||
* Copyright 1998 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* - - - - - - - - - - - - - - - */
|
||||
/* Global state random generator */
|
||||
|
||||
/* Return a random number between 0 and 4294967294 */
|
||||
unsigned int
|
||||
rand32( /* Return 32 bit random number */
|
||||
unsigned int seed); /* Optional seed. Non-zero re-initialized with that seed */
|
||||
|
||||
/* Return a random integer in the range min to max inclusive */
|
||||
int i_rand(int min, int max);
|
||||
|
||||
/* Return a uniform random double in the range min to max inclusive */
|
||||
double d_rand(double min, double max);
|
||||
|
||||
/* Return a squared distribution random double in the range min to max inclusive */
|
||||
double d2_rand(double min, double max);
|
||||
|
||||
/* Return a random floating point number with a gausian/normal */
|
||||
/* distribution, centered about 0.0, with standard deviation 1.0 */
|
||||
/* and an average deviation of sqrt(2/pi) */
|
||||
/* 0.79788456080286535587989211986876 = */
|
||||
double norm_rand(void);
|
||||
|
||||
/* Scale normal vector components by the following to give the vector */
|
||||
/* a mean absolute deviation of 1.0 for the corresponding dimension. */
|
||||
/* [ Formula is 1.0/(sqrt(2.0) * gamma_func((d+1.0)/2.0)/gamma_func(d/2.0)) ] */
|
||||
|
||||
#define NORM_RAND_ABS_SCALE_1 1.25331413731550320
|
||||
#define NORM_RAND_ABS_SCALE_2 0.79788456080287129
|
||||
#define NORM_RAND_ABS_SCALE_3 0.62665706865775450
|
||||
#define NORM_RAND_ABS_SCALE_4 0.53192304053525241
|
||||
#define NORM_RAND_ABS_SCALE_5 0.46999280149331241
|
||||
#define NORM_RAND_ABS_SCALE_6 0.42553843242820782
|
||||
#define NORM_RAND_ABS_SCALE_7 0.39166066791109894
|
||||
#define NORM_RAND_ABS_SCALE_8 0.36474722779559393
|
||||
#define NORM_RAND_ABS_SCALE_9 0.34270308442220981
|
||||
#define NORM_RAND_ABS_SCALE_10 0.32421975804054332
|
||||
#define NORM_RAND_ABS_SCALE_11 0.30843277597999841
|
||||
#define NORM_RAND_ABS_SCALE_12 0.29474523458229918
|
||||
#define NORM_RAND_ABS_SCALE_13 0.28273004464831558
|
||||
#define NORM_RAND_ABS_SCALE_14 0.27207252422983330
|
||||
#define NORM_RAND_ABS_SCALE_15 0.26253504145918249
|
||||
#define NORM_RAND_ABS_SCALE_16 0.25393435594782049
|
||||
#define NORM_RAND_ABS_SCALE_17 0.24612660136794409
|
||||
#define NORM_RAND_ABS_SCALE_18 0.23899704089211726
|
||||
#define NORM_RAND_ABS_SCALE_19 0.23245290129195134
|
||||
|
||||
/* Table of the above values indexed by dimension */
|
||||
#define NORM_RAND_ABS_SCALE_MAXD 19
|
||||
extern double NORM_RAND_ABS_SCALE[NORM_RAND_ABS_SCALE_MAXD+1];
|
||||
|
||||
/* - - - - - - - - - - - - - - - */
|
||||
/* Explicit state random generator */
|
||||
/* Use NULL for global state */
|
||||
|
||||
#define RAND_TSIZE 2843 /* Prime */
|
||||
#define RAND_SEED 0x12345678 /* Default seed */
|
||||
|
||||
/* Should set to 0 to default intialize */
|
||||
typedef struct {
|
||||
int pvs_inited;
|
||||
unsigned int ran, last;
|
||||
unsigned int pvs[RAND_TSIZE];
|
||||
|
||||
/* normal distribution 2nd value */
|
||||
int r2; /* 2nd value available */
|
||||
double nr2; /* 2nd value */
|
||||
} rand_state;
|
||||
|
||||
/* Init rand_state to default */
|
||||
void rand_init(rand_state *p);
|
||||
|
||||
/* Return a random number between 0 and 4294967294 */
|
||||
unsigned int
|
||||
rand32_th(rand_state *p,
|
||||
unsigned int seed); /* Optional seed. Non-zero re-initialized with that seed */
|
||||
|
||||
/* Return a random integer in the range min to max inclusive */
|
||||
int i_rand_th(rand_state *p, int min, int max);
|
||||
|
||||
/* Return a uniform random double in the range min to max inclusive */
|
||||
double d_rand_th(rand_state *p, double min, double max);
|
||||
|
||||
/* Return a squared distribution random double in the range min to max inclusive */
|
||||
double d2_rand_th(rand_state *p, double min, double max);
|
||||
|
||||
/* Return a random floating point number with a gausian/normal */
|
||||
/* distribution, centered about 0.0, with standard deviation 1.0 */
|
||||
/* and an average deviation of 0.564 */
|
||||
double norm_rand_th(rand_state *p);
|
||||
|
||||
/* Set random dvector */
|
||||
void vect_rand(double *d, double min, double max, int len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* RAND_H */
|
||||
217
Argyll_V3.5.0/numlib/roots.c
Normal file
217
Argyll_V3.5.0/numlib/roots.c
Normal file
@@ -0,0 +1,217 @@
|
||||
|
||||
/*
|
||||
* Roots3And4.c
|
||||
*
|
||||
* Utility functions to find cubic and quartic roots.
|
||||
*
|
||||
* Coefficients are passed like this:
|
||||
*
|
||||
* c[0] + c[1]*x + c[2]*x^2 + c[3]*x^3 + c[4]*x^4 = 0
|
||||
*
|
||||
* The functions return the number of non-complex roots and
|
||||
* put the values into the s array.
|
||||
*
|
||||
* Author: Jochen Schwarze (schwarze@isa.de)
|
||||
*
|
||||
* Jan 26, 1990 Version for Graphics Gems
|
||||
* Oct 11, 1990 Fixed sign problem for negative q's in SolveQuartic
|
||||
* (reported by Mark Podlipec),
|
||||
* Old-style function definitions,
|
||||
* IsZero() as a macro
|
||||
* Nov 23, 1990 Some systems do not declare acos() and cbrt() in
|
||||
* <math.h>, though the functions exist in the library.
|
||||
* If large coefficients are used, EQN_EPS should be
|
||||
* reduced considerably (e.g. to 1E-30), results will be
|
||||
* correct but multiple roots might be reported more
|
||||
* than once.
|
||||
* Apr 18, 2018 Reformat for inclusing in ArgyllCMS - GWG
|
||||
*
|
||||
* The authors and the publisher hold no copyright restrictions
|
||||
* on any of these files; this source code is public domain, and
|
||||
* is freely available to the entire computer graphics community
|
||||
* for study, use, and modification. We do request that the
|
||||
* comment at the top of each file, identifying the original
|
||||
* author and its original publication in the book Graphics
|
||||
* Gems, be retained in all programs that use these files.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
/* epsilon surrounding for near zero values */
|
||||
#define EQN_EPS 1e-9
|
||||
#define IsZero(x) ((x) > -EQN_EPS && (x) < EQN_EPS)
|
||||
|
||||
#define CBRT(x) ((x) > 0.0 ? pow((double)(x), 1.0/3.0) : \
|
||||
((x) < 0.0 ? -pow((double)-(x), 1.0/3.0) : 0.0))
|
||||
|
||||
int SolveQuadric(double c[3], double s[2]) {
|
||||
double p, q, D;
|
||||
|
||||
/* normal form: x^2 + px + q = 0 */
|
||||
p = c[1] / (2 * c[2]);
|
||||
q = c[0] / c[2];
|
||||
|
||||
D = p * p - q;
|
||||
|
||||
if (IsZero(D)) {
|
||||
s[ 0 ] = - p;
|
||||
return 1;
|
||||
} else if (D < 0) {
|
||||
return 0;
|
||||
} else /* if (D > 0) */ {
|
||||
double sqrt_D = sqrt(D);
|
||||
|
||||
s[0] = sqrt_D - p;
|
||||
s[1] = - sqrt_D - p;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int SolveCubic(double c[4], double s[3]) {
|
||||
int i, num;
|
||||
double sub;
|
||||
double A, B, C;
|
||||
double sq_A, p, q;
|
||||
double cb_p, D;
|
||||
|
||||
/* normal form: x^3 + Ax^2 + Bx + C = 0 */
|
||||
A = c[2] / c[3];
|
||||
B = c[1] / c[3];
|
||||
C = c[0] / c[3];
|
||||
|
||||
/* substitute x = y - A/3 to eliminate quadric term: */
|
||||
/* x^3 +px + q = 0 */
|
||||
sq_A = A * A;
|
||||
p = 1.0/3 * (-1.0/3 * sq_A + B);
|
||||
q = 1.0/2 * (2.0/27 * A * sq_A - 1.0/3 * A * B + C);
|
||||
|
||||
/* use Cardano's formula */
|
||||
cb_p = p * p * p;
|
||||
D = q * q + cb_p;
|
||||
|
||||
if (IsZero(D)) {
|
||||
if (IsZero(q)) { /* one triple solution */
|
||||
s[0] = 0.0;
|
||||
num = 1;
|
||||
} else { /* one single and one double solution */
|
||||
double u = CBRT(-q);
|
||||
s[0] = 2.0 * u;
|
||||
s[1] = - u;
|
||||
num = 2;
|
||||
}
|
||||
} else if (D < 0) { /* Casus irreducibilis: three real solutions */
|
||||
double phi = 1.0/3 * acos(-q / sqrt(-cb_p));
|
||||
double t = 2 * sqrt(-p);
|
||||
|
||||
s[0] = t * cos(phi);
|
||||
s[1] = -t * cos(phi + M_PI / 3.0);
|
||||
s[2] = -t * cos(phi - M_PI / 3.0);
|
||||
num = 3;
|
||||
} else { /* one real solution */
|
||||
double sqrt_D = sqrt(D);
|
||||
double u = CBRT(sqrt_D - q);
|
||||
double v = -CBRT(sqrt_D + q);
|
||||
|
||||
s[0] = u + v;
|
||||
num = 1;
|
||||
}
|
||||
|
||||
/* resubstitute */
|
||||
sub = 1.0/3.0 * A;
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
s[i] -= sub;
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
int SolveQuartic(double c[5], double s[4]) {
|
||||
double coeffs[4];
|
||||
double z, u, v, sub;
|
||||
double A, B, C, D;
|
||||
double sq_A, p, q, r;
|
||||
int i, num;
|
||||
|
||||
/* normal form: x^4 + Ax^3 + Bx^2 + Cx + D = 0 */
|
||||
A = c[3] / c[4];
|
||||
B = c[2] / c[4];
|
||||
C = c[1] / c[4];
|
||||
D = c[0] / c[4];
|
||||
|
||||
/* substitute x = y - A/4 to eliminate cubic term:
|
||||
x^4 + px^2 + qx + r = 0 */
|
||||
sq_A = A * A;
|
||||
p = - 3.0/8 * sq_A + B;
|
||||
q = 1.0/8 * sq_A * A - 1.0/2 * A * B + C;
|
||||
r = - 3.0/256*sq_A*sq_A + 1.0/16*sq_A*B - 1.0/4*A*C + D;
|
||||
|
||||
if (IsZero(r)) {
|
||||
/* no absolute term: y(y^3 + py + q) = 0 */
|
||||
|
||||
coeffs[0] = q;
|
||||
coeffs[1] = p;
|
||||
coeffs[2] = 0;
|
||||
coeffs[3] = 1.0;
|
||||
|
||||
num = SolveCubic(coeffs, s);
|
||||
|
||||
s[num++] = 0;
|
||||
} else {
|
||||
/* solve the resolvent cubic ... */
|
||||
coeffs[0] = 1.0/2.0 * r * p - 1.0/8.0 * q * q;
|
||||
coeffs[1] = - r;
|
||||
coeffs[2] = - 1.0/2.0 * p;
|
||||
coeffs[3] = 1.0;
|
||||
|
||||
(void) SolveCubic(coeffs, s);
|
||||
|
||||
/* ... and take the one real solution ... */
|
||||
z = s[0];
|
||||
|
||||
/* ... to build two quadric equations */
|
||||
u = z * z - r;
|
||||
v = 2 * z - p;
|
||||
|
||||
if (IsZero(u))
|
||||
u = 0;
|
||||
else if (u > 0)
|
||||
u = sqrt(u);
|
||||
else
|
||||
return 0;
|
||||
|
||||
if (IsZero(v))
|
||||
v = 0;
|
||||
else if (v > 0)
|
||||
v = sqrt(v);
|
||||
else
|
||||
return 0;
|
||||
|
||||
coeffs[0] = z - u;
|
||||
coeffs[1] = q < 0 ? -v : v;
|
||||
coeffs[2] = 1.0;
|
||||
|
||||
num = SolveQuadric(coeffs, s);
|
||||
|
||||
coeffs[0]= z + u;
|
||||
coeffs[1] = q < 0 ? v : -v;
|
||||
coeffs[2] = 1.0;
|
||||
|
||||
num += SolveQuadric(coeffs, s + num);
|
||||
}
|
||||
|
||||
/* resubstitute */
|
||||
sub = 1.0/4.0 * A;
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
s[i] -= sub;
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
23
Argyll_V3.5.0/numlib/roots.h
Normal file
23
Argyll_V3.5.0/numlib/roots.h
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
/*
|
||||
* Roots3And4.c
|
||||
*
|
||||
* Utility functions to find cubic and quartic roots,
|
||||
* coefficients are passed like this:
|
||||
*
|
||||
* c[0] + c[1]*x + c[2]*x^2 + c[3]*x^3 + c[4]*x^4 = 0
|
||||
*
|
||||
* The functions return the number of non-complex roots and
|
||||
* put the values into the s array.
|
||||
*
|
||||
* Author: Jochen Schwarze (schwarze@isa.de)
|
||||
* (From Graphics Gems I)
|
||||
*/
|
||||
|
||||
int SolveQuadric(double c[3], double s[2]);
|
||||
|
||||
int SolveCubic(double c[4], double s[3]);
|
||||
|
||||
int SolveQuartic(double c[5], double s[4]);
|
||||
|
||||
|
||||
210
Argyll_V3.5.0/numlib/sobol.c
Normal file
210
Argyll_V3.5.0/numlib/sobol.c
Normal file
@@ -0,0 +1,210 @@
|
||||
|
||||
/***************************************************/
|
||||
/* Sobol sub-random vector sequence generator */
|
||||
/***************************************************/
|
||||
|
||||
/* Code is an expression of the algorithm decsribed in */
|
||||
/* the SSOBOL.F fortran source file, with additional */
|
||||
/* guidance from "Numerical Recipes in C", by W.H.Press, B.P.Flannery, */
|
||||
/* S.A.Teukolsky & W.T.Vetterling. */
|
||||
|
||||
/*
|
||||
* Copyright 2002 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include "numsup.h"
|
||||
#include "sobol.h"
|
||||
|
||||
/*
|
||||
* The array poly gives successive primitive
|
||||
* polynomials coded in binary, e.g.
|
||||
45 = 100101
|
||||
* has bits 5, 2, and 0 set (counting from the
|
||||
* right) and therefore represents
|
||||
X**5 + X**2 + X**0
|
||||
|
||||
* These polynomials are in the order used by
|
||||
* sobol in ussr comput. maths. math. phys. 16 (1977),
|
||||
* 236-242.
|
||||
*/
|
||||
|
||||
static int sobol_poly[SOBOL_MAXDIM] = {
|
||||
1, 3, 7, 11, 13, 19, 25, 37, 59, 47,
|
||||
61, 55, 41, 67, 97, 91, 109, 103, 115, 131,
|
||||
193, 137, 145, 143, 241, 157, 185, 167, 229, 171,
|
||||
213, 191, 253, 203, 211, 239, 247, 285, 369, 299
|
||||
};
|
||||
|
||||
/*
|
||||
* The initialization of the array vinit is from
|
||||
* Sobol and Levitan, the production of points uniformly
|
||||
* distributed in a multidimensional cube (in Russian),
|
||||
* preprint ipm akad. nauk sssr, no. 40, moscow 1976.
|
||||
* For a polynomial of degree m, m initial
|
||||
* values are needed : these are the values given here.
|
||||
* subsequent values are calculated during initialisation.
|
||||
*/
|
||||
|
||||
static int vinit[8][SOBOL_MAXDIM] = {
|
||||
{
|
||||
0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1
|
||||
},
|
||||
{
|
||||
0, 0, 1, 3, 1, 3, 1, 3, 3, 1,
|
||||
3, 1, 3, 1, 3, 1, 1, 3, 1, 3,
|
||||
1, 3, 1, 3, 3, 1, 3, 1, 3, 1,
|
||||
3, 1, 1, 3, 1, 3, 1, 3, 1, 3
|
||||
},
|
||||
{
|
||||
0, 0, 0, 7, 5, 1, 3, 3, 7, 5,
|
||||
5, 7, 7, 1, 3, 3, 7, 5, 1, 1,
|
||||
5, 3, 3, 1, 7, 5, 1, 3, 3, 7,
|
||||
5, 1, 1, 5, 7, 7, 5, 1, 3, 3
|
||||
},
|
||||
{
|
||||
0, 0, 0, 0, 0, 1, 7, 9, 13, 11,
|
||||
1, 3, 7, 9, 5, 13, 13, 11, 3, 15,
|
||||
5, 3, 15, 7, 9, 13, 9, 1, 11, 7,
|
||||
5, 15, 1, 15, 11, 5, 3, 1, 7, 9
|
||||
},
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 9, 3, 27,
|
||||
15, 29, 21, 23, 19, 11, 25, 7, 13, 17,
|
||||
1, 25, 29, 3, 31, 11, 5, 23, 27, 19,
|
||||
21, 5, 1, 17, 13, 7, 15, 9, 31, 9
|
||||
},
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 37, 33, 7, 5, 11, 39, 63,
|
||||
27, 17, 15, 23, 29, 3, 21, 13, 31, 25,
|
||||
9, 49, 33, 19, 29, 11, 19, 27, 15, 25
|
||||
},
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 13,
|
||||
33, 115, 41, 79, 17, 29, 119, 75, 73, 105,
|
||||
7, 59, 65, 21, 3, 113, 61, 89, 45, 107
|
||||
},
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 7, 23, 39
|
||||
}
|
||||
};
|
||||
|
||||
/* Get the next sobol vector */
|
||||
/* return nz if we've run out */
|
||||
static int next_sobol(sobol *s, double *v) {
|
||||
int i, p;
|
||||
unsigned int c;
|
||||
|
||||
s->count++;
|
||||
|
||||
/* Find the position of the right-hand zero in count */
|
||||
for (c = s->count, p = 0; (c & 1) == 0; p++, c >>= 1)
|
||||
;
|
||||
|
||||
if(p > SOBOL_MAXBIT)
|
||||
return 1; /* Run out */
|
||||
|
||||
for (i = 0; i < s->dim; i++) {
|
||||
s->lastq[i] ^= s->dir[p][i];
|
||||
v[i] = s->lastq[i] * s->recipd;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Free up the object */
|
||||
static void del_sobol(sobol *s) {
|
||||
if (s != NULL)
|
||||
free(s);
|
||||
}
|
||||
|
||||
/* reset the count */
|
||||
static void reset_sobol(sobol *s) {
|
||||
int i;
|
||||
|
||||
/* Set up first vector and values */
|
||||
s->count = 0;
|
||||
for (i = 0; i < s->dim; i++)
|
||||
s->lastq[i] = 0;
|
||||
}
|
||||
|
||||
/* Return NULL on error */
|
||||
sobol *new_sobol(int dim) {
|
||||
sobol *s = NULL;
|
||||
int i, j, p;
|
||||
|
||||
if (dim < 1 || dim > SOBOL_MAXDIM) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((s = (sobol *)malloc(sizeof(sobol))) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
s->dim = dim;
|
||||
s->next = next_sobol;
|
||||
s->reset = reset_sobol;
|
||||
s->del = del_sobol;
|
||||
|
||||
/* Initialize the direction table */
|
||||
for (i = 0; i < dim; i++) {
|
||||
|
||||
if (i == 0) {
|
||||
for (j = 0; j < SOBOL_MAXBIT; j++)
|
||||
s->dir[j][i] = 1;
|
||||
} else {
|
||||
int m; /* Degree */
|
||||
int pm; /* Polinomial mask */
|
||||
|
||||
/* Find degree of polynomial from binary encoding */
|
||||
for (m = 0, pm = sobol_poly[i] >> 1; pm != 0; m++, pm >>= 1)
|
||||
;
|
||||
|
||||
/* The leading elements of row i come from vinit[][] */
|
||||
for (j = 0; j < m; j++) {
|
||||
s->dir[j][i] = vinit[j][i];
|
||||
}
|
||||
|
||||
/* Calculate remaining elements of row i as explained */
|
||||
/* in bratley and fox, section 2 */
|
||||
pm = sobol_poly[i];
|
||||
for (j = m; j < SOBOL_MAXBIT; j++) {
|
||||
int k;
|
||||
int newv = s->dir[j-m][i];
|
||||
for (k = 0; k < m; k++) {
|
||||
if (pm & (1 << (m-k-1))) {
|
||||
newv ^= s->dir[j-k-1][i] << (k+1);
|
||||
}
|
||||
}
|
||||
s->dir[j][i] = newv;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Multiply columns of v by appropriate power of 2 */
|
||||
for (p = 2, j = SOBOL_MAXBIT-2; j >= 0; j--, p <<= 1) {
|
||||
for (i = 0; i < dim; i++)
|
||||
s->dir[j][i] *= p;
|
||||
}
|
||||
|
||||
/* recipd is 1/(common denominator of the elements in v) */
|
||||
s->recipd = 1.0/(1 << SOBOL_MAXBIT);
|
||||
|
||||
/* Set up first vector and values */
|
||||
s->count = 0;
|
||||
for (i = 0; i < dim; i++)
|
||||
s->lastq[i] = 0;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
52
Argyll_V3.5.0/numlib/sobol.h
Normal file
52
Argyll_V3.5.0/numlib/sobol.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef SOBOL_H
|
||||
#define SOBOL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SOBOL_MAXBIT 30
|
||||
#define SOBOL_MAXDIM 40
|
||||
|
||||
/* Object definition */
|
||||
struct _sobol {
|
||||
/* Private: */
|
||||
int dim; /* dimension we're set for */
|
||||
unsigned int count;
|
||||
double recipd;
|
||||
int lastq[SOBOL_MAXDIM];
|
||||
int dir[SOBOL_MAXBIT][SOBOL_MAXDIM];
|
||||
|
||||
/* Public: */
|
||||
/* Methods */
|
||||
|
||||
/* Get the next sobol vector, return nz if we've run out */
|
||||
/* Values are between 0.0 and 1.0 */
|
||||
int (*next)(struct _sobol *s, double *v);
|
||||
|
||||
/* Rest to the begining of the sequence */
|
||||
void (*reset)(struct _sobol *s);
|
||||
|
||||
/* We're done with the object */
|
||||
void (*del)(struct _sobol *s);
|
||||
|
||||
}; typedef struct _sobol sobol;
|
||||
|
||||
/* Return NULL on error */
|
||||
sobol *new_sobol(int dim);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SOBOL_H */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
127
Argyll_V3.5.0/numlib/soboltest.c
Normal file
127
Argyll_V3.5.0/numlib/soboltest.c
Normal file
@@ -0,0 +1,127 @@
|
||||
|
||||
/* Test the sobol sub-random vector sequencer */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "numlib.h"
|
||||
|
||||
/* Reference vectors */
|
||||
/* First 10 of dimension 40 */
|
||||
static double refv[10][40] = {
|
||||
{
|
||||
0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000,
|
||||
0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000,
|
||||
0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000,
|
||||
0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000
|
||||
}, {
|
||||
0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.2500, 0.7500,
|
||||
0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.7500, 0.2500, 0.7500, 0.2500,
|
||||
0.7500, 0.2500, 0.7500, 0.2500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500,
|
||||
0.2500, 0.7500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500
|
||||
}, {
|
||||
0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.7500, 0.2500,
|
||||
0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.2500, 0.7500, 0.2500, 0.7500,
|
||||
0.2500, 0.7500, 0.2500, 0.7500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500,
|
||||
0.7500, 0.2500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500, 0.2500, 0.7500
|
||||
}, {
|
||||
0.3750, 0.3750, 0.6250, 0.1250, 0.8750, 0.8750, 0.1250, 0.6250, 0.1250, 0.8750,
|
||||
0.3750, 0.6250, 0.1250, 0.3750, 0.6250, 0.1250, 0.6250, 0.3750, 0.3750, 0.8750,
|
||||
0.8750, 0.6250, 0.1250, 0.8750, 0.1250, 0.8750, 0.8750, 0.1250, 0.6250, 0.6250,
|
||||
0.3750, 0.3750, 0.3750, 0.3750, 0.6250, 0.1250, 0.8750, 0.8750, 0.1250, 0.6250
|
||||
}, {
|
||||
0.8750, 0.8750, 0.1250, 0.6250, 0.3750, 0.3750, 0.6250, 0.1250, 0.6250, 0.3750,
|
||||
0.8750, 0.1250, 0.6250, 0.8750, 0.1250, 0.6250, 0.1250, 0.8750, 0.8750, 0.3750,
|
||||
0.3750, 0.1250, 0.6250, 0.3750, 0.6250, 0.3750, 0.3750, 0.6250, 0.1250, 0.1250,
|
||||
0.8750, 0.8750, 0.8750, 0.8750, 0.1250, 0.6250, 0.3750, 0.3750, 0.6250, 0.1250
|
||||
}, {
|
||||
0.6250, 0.1250, 0.3750, 0.3750, 0.1250, 0.6250, 0.8750, 0.8750, 0.3750, 0.1250,
|
||||
0.1250, 0.3750, 0.3750, 0.6250, 0.8750, 0.8750, 0.3750, 0.1250, 0.6250, 0.6250,
|
||||
0.1250, 0.8750, 0.8750, 0.6250, 0.3750, 0.1250, 0.6250, 0.8750, 0.8750, 0.3750,
|
||||
0.1250, 0.6250, 0.6250, 0.1250, 0.3750, 0.3750, 0.1250, 0.6250, 0.8750, 0.8750
|
||||
}, {
|
||||
0.1250, 0.6250, 0.8750, 0.8750, 0.6250, 0.1250, 0.3750, 0.3750, 0.8750, 0.6250,
|
||||
0.6250, 0.8750, 0.8750, 0.1250, 0.3750, 0.3750, 0.8750, 0.6250, 0.1250, 0.1250,
|
||||
0.6250, 0.3750, 0.3750, 0.1250, 0.8750, 0.6250, 0.1250, 0.3750, 0.3750, 0.8750,
|
||||
0.6250, 0.1250, 0.1250, 0.6250, 0.8750, 0.8750, 0.6250, 0.1250, 0.3750, 0.3750
|
||||
}, {
|
||||
0.1875, 0.3125, 0.3125, 0.6875, 0.5625, 0.1875, 0.0625, 0.9375, 0.1875, 0.0625,
|
||||
0.6875, 0.8125, 0.5625, 0.6875, 0.1875, 0.6875, 0.1875, 0.0625, 0.0625, 0.8125,
|
||||
0.9375, 0.3125, 0.5625, 0.3125, 0.4375, 0.4375, 0.6875, 0.4375, 0.8125, 0.5625,
|
||||
0.9375, 0.8125, 0.1875, 0.3125, 0.3125, 0.6875, 0.5625, 0.1875, 0.0625, 0.9375
|
||||
}, {
|
||||
0.6875, 0.8125, 0.8125, 0.1875, 0.0625, 0.6875, 0.5625, 0.4375, 0.6875, 0.5625,
|
||||
0.1875, 0.3125, 0.0625, 0.1875, 0.6875, 0.1875, 0.6875, 0.5625, 0.5625, 0.3125,
|
||||
0.4375, 0.8125, 0.0625, 0.8125, 0.9375, 0.9375, 0.1875, 0.9375, 0.3125, 0.0625,
|
||||
0.4375, 0.3125, 0.6875, 0.8125, 0.8125, 0.1875, 0.0625, 0.6875, 0.5625, 0.4375
|
||||
}, {
|
||||
0.9375, 0.0625, 0.5625, 0.9375, 0.3125, 0.4375, 0.8125, 0.6875, 0.4375, 0.8125,
|
||||
0.9375, 0.0625, 0.8125, 0.4375, 0.4375, 0.4375, 0.9375, 0.3125, 0.8125, 0.5625,
|
||||
0.1875, 0.0625, 0.3125, 0.0625, 0.1875, 0.6875, 0.9375, 0.6875, 0.5625, 0.3125,
|
||||
0.6875, 0.0625, 0.9375, 0.0625, 0.5625, 0.9375, 0.3125, 0.4375, 0.8125, 0.6875
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
sobol *s;
|
||||
int i;
|
||||
double vec[40];
|
||||
int fail = 0;
|
||||
|
||||
printf("Starting sobol test\n");
|
||||
|
||||
if ((s = new_sobol(40)) == NULL) {
|
||||
printf("new_sobol failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
int j;
|
||||
if (s->next(s, vec)) {
|
||||
printf("Next failed\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Vector %d = ",i);
|
||||
for (j = 0; j < 40; j++) {
|
||||
printf("%f ",vec[j]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
for (j = 0; j < 40; j++) {
|
||||
if (refv[i][j] != vec[j]) {
|
||||
printf("Warning: failed at vector %d, dim %d (%f != %f)\n",i,j,refv[i][j],vec[j]);
|
||||
fail = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s->del(s);
|
||||
if (fail) {
|
||||
printf("Sobol test FAILED\n");
|
||||
return 1;
|
||||
} else {
|
||||
printf("Sobol test done OK\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
749
Argyll_V3.5.0/numlib/svd.c
Normal file
749
Argyll_V3.5.0/numlib/svd.c
Normal file
@@ -0,0 +1,749 @@
|
||||
|
||||
/*
|
||||
* Singular Value Decomposition,
|
||||
* from the Burton S. Garbow's EISPACK FORTRAN code,
|
||||
* based on the algorithm of Golub and Reinsch in
|
||||
* "Handbook of Automatic Computation",
|
||||
* with some guidance from R. B Davie's newmat09.
|
||||
*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include "numsup.h"
|
||||
#include "svd.h"
|
||||
|
||||
#ifndef NEVER
|
||||
/* Compute the pythagorian distance sqrt(a^2 + b^2) */
|
||||
/* taking care of under or overflow. */
|
||||
double pythag(
|
||||
double a,
|
||||
double b) {
|
||||
double aba, abb;
|
||||
|
||||
aba = fabs(a);
|
||||
abb = fabs(b);
|
||||
|
||||
if (aba > abb) {
|
||||
double boa;
|
||||
boa = abb/aba;
|
||||
return aba * sqrt(1.0 + boa * boa);
|
||||
} else {
|
||||
double aob;
|
||||
if (abb == 0.0)
|
||||
return 0.0;
|
||||
aob = aba/abb;
|
||||
return abb * sqrt(1.0 + aob * aob);
|
||||
}
|
||||
}
|
||||
#else /* Quicker, but less robust */
|
||||
#define pythag(a, b) sqrt((a) * (a) + (b) * (b))
|
||||
#endif
|
||||
|
||||
#define MAXITS 30
|
||||
|
||||
/* Compute Singular Value Decomposition of A = U.W.Vt */
|
||||
/* Return status value: */
|
||||
/* 0 - no error */
|
||||
/* 1 - Too many itterations */
|
||||
int svdecomp(
|
||||
double **a, /* A[0..m-1][0..n-1], return U[][] */
|
||||
double *w, /* return W[0..n-1] */
|
||||
double **v, /* return V[0..n-1][0..n-1] (not transpose!) */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
) {
|
||||
double eps = DBL_EPSILON; /* 1.0 + eps = 1.0 */
|
||||
double tol = DBL_MIN/eps; /* Minumum +ve value/eps */
|
||||
double *rv1, RV1[100];
|
||||
double anm;
|
||||
int i, j, k;
|
||||
int its;
|
||||
|
||||
if (n <= 100)
|
||||
rv1 = RV1; /* Allocate fast off stack */
|
||||
else
|
||||
rv1 = dvector(0, n-1);
|
||||
|
||||
/* Housholder reduction of A to bidiagonal form */
|
||||
anm = 0.0;
|
||||
rv1[0] = 0.0; /* Will always == 0.0 */
|
||||
for (i = 0; i < n; i++) { /* For each element in the diagonal of A */
|
||||
int ip1 = i + 1;
|
||||
|
||||
/* Deal with lower column at i */
|
||||
w[i] = 0.0;
|
||||
if (i < m) { /* If it makes sense to go from row i .. m-1 */
|
||||
double ss, ff = 0.0;
|
||||
|
||||
for (ss = 0.0, k = m-1; k >= i; k--) { /* Sum of squares of column */
|
||||
ff = a[k][i];
|
||||
ss += ff * ff;
|
||||
} /* Note ff = A[i][i] */
|
||||
if (ss >= tol) {
|
||||
double gg, hh;
|
||||
gg = sqrt(ss);
|
||||
w[i] = gg = ff < 0.0 ? gg : -gg; /* gg has -sign of ff */
|
||||
hh = ff * gg - ss;
|
||||
a[i][i] = ff - gg;
|
||||
|
||||
/* For all lower columns to the right of this one */
|
||||
for (j = ip1; j < n; j++) { /* Column j */
|
||||
double tt;
|
||||
for (ss = 0.0, k = i; k < m; k++)
|
||||
ss += a[k][j] * a[k][i]; /* Sum of products of i and j columns */
|
||||
tt = ss / hh;
|
||||
for (k = i; k < m; k++)
|
||||
a[k][j] += tt * a[k][i]; /* Add sumprod/hh to column j */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Deal with upper super row at i */
|
||||
if (ip1 < n) { /* If it makes sense to go from column i+1 .. n-1 */
|
||||
rv1[ip1] = 0.0;
|
||||
if (i < m) { /* If it makes sense to process row i */
|
||||
double ss, ff = 0.0;
|
||||
for (ss = 0.0, k = n-1; k >= ip1; k--) { /* Sum of squares of row */
|
||||
ff = a[i][k];
|
||||
ss += ff * ff;
|
||||
} /* Note ff = A[i][ip1] */
|
||||
if (ss >= tol) {
|
||||
double gg, hh;
|
||||
gg = sqrt(ss);
|
||||
rv1[ip1] = gg = ff < 0.0 ? gg : -gg; /* gg has -sign of ff */
|
||||
hh = ff * gg - ss;
|
||||
a[i][ip1] = (ff - gg);
|
||||
|
||||
/* For all upper rows below this one */
|
||||
for (j = ip1; j < m; j++) {
|
||||
double tt;
|
||||
for (ss = 0.0, k = ip1; k < n; k++) /* Sum of products of i and j rows */
|
||||
ss += a[j][k] * a[i][k];
|
||||
tt = ss / hh;
|
||||
for (k = ip1; k < n; k++)
|
||||
a[j][k] += tt * a[i][k]; /* Add sumprod/hh to row j */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
double tt;
|
||||
tt = fabs(w[i]) + fabs(rv1[i]);
|
||||
if (tt > anm)
|
||||
anm = tt;
|
||||
}
|
||||
}
|
||||
|
||||
/* Accumulation of right hand transformations */
|
||||
for (i = n-1; i >= 0; i--) {
|
||||
int ip1 = i + 1;
|
||||
if (ip1 < n) {
|
||||
double gg;
|
||||
gg = rv1[ip1];
|
||||
if (gg != 0.0) {
|
||||
gg = 1.0 / gg;
|
||||
for (j = ip1; j < n; j++)
|
||||
v[j][i] = (a[i][j] / a[i][ip1]) * gg; /* Double division to avoid underflow */
|
||||
for (j = ip1; j < n; j++) {
|
||||
double ss;
|
||||
for (ss = 0.0, k = ip1; k < n; k++)
|
||||
ss += a[i][k] * v[k][j];
|
||||
for (k = ip1; k < n; k++)
|
||||
v[k][j] += ss * v[k][i];
|
||||
}
|
||||
}
|
||||
for (j = ip1; j < n; j++)
|
||||
v[i][j] = v[j][i] = 0.0;
|
||||
}
|
||||
v[i][i] = 1.0;
|
||||
}
|
||||
/* Accumulation of left hand transformations */
|
||||
for (i = n < m ? n-1 : m-1; i >= 0; i--) {
|
||||
int ip1 = i + 1;
|
||||
double gg = w[i];
|
||||
if (ip1 < n)
|
||||
for (j = ip1; j < n; j++)
|
||||
a[i][j] = 0.0;
|
||||
if (gg == 0.0) {
|
||||
for (j = i; j < m; j++)
|
||||
a[j][i] = 0.0;
|
||||
} else {
|
||||
gg = 1.0 / gg;
|
||||
if (ip1 < n) {
|
||||
for (j = ip1; j < n; j++) {
|
||||
double ss, ff;
|
||||
for (ss = 0.0, k = ip1; k < m; k++)
|
||||
ss += a[k][i] * a[k][j];
|
||||
ff = (ss / a[i][i]) * gg; /* Double division to avoid underflow */
|
||||
for (k = i; k < m; k++)
|
||||
a[k][j] += ff * a[k][i];
|
||||
}
|
||||
}
|
||||
for (j = i; j < m; j++)
|
||||
a[j][i] *= gg;
|
||||
}
|
||||
a[i][i] += 1.0;
|
||||
}
|
||||
|
||||
eps *= anm;
|
||||
|
||||
/* Fully diagonalize bidiagonal result, by */
|
||||
/* successive QR rotations. */
|
||||
for (k = (n-1); k >= 0; k--) { /* For all the singular values */
|
||||
for (its = 0;; its++) {
|
||||
int flag;
|
||||
int lm1 = 0;
|
||||
int ll;
|
||||
double zz;
|
||||
|
||||
/* Test for splitting */
|
||||
for (flag = 1, ll = k; ll >= 0; ll--) {
|
||||
lm1 = ll - 1;
|
||||
if (fabs(rv1[ll]) <= eps) { /* Note always stops at 0 because rv1[0] = 0.0 */
|
||||
flag = 0;
|
||||
break;
|
||||
}
|
||||
if (fabs(w[lm1]) <= eps)
|
||||
break;
|
||||
}
|
||||
if (flag != 0) {
|
||||
double cc = 0.0;
|
||||
double ss = 1.0;
|
||||
for (i = ll; i <= k; i++) {
|
||||
double ff, gg, hh;
|
||||
gg = rv1[i];
|
||||
rv1[i] = cc * gg;
|
||||
ff = ss * gg;
|
||||
if (fabs(ff) <= eps)
|
||||
break; /* Found acceptable solution */
|
||||
gg = w[i];
|
||||
w[i] = hh = pythag(ff, gg);
|
||||
hh = 1.0 / hh;
|
||||
cc = gg * hh;
|
||||
ss = -ff * hh;
|
||||
|
||||
/* Apply rotation */
|
||||
for (j = 0; j < m; j++) {
|
||||
double y1, z1;
|
||||
y1 = a[j][lm1];
|
||||
z1 = a[j][i];
|
||||
a[j][lm1] = y1 * cc + z1 * ss;
|
||||
a[j][i] = z1 * cc - y1 * ss;
|
||||
}
|
||||
}
|
||||
}
|
||||
zz = w[k];
|
||||
if (k == ll) { /* Convergence */
|
||||
if (zz < 0.0) {
|
||||
w[k] = -zz; /* Make singular value non-negative */
|
||||
for (j = 0; j < n; j++)
|
||||
v[j][k] = (-v[j][k]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (its == MAXITS) {
|
||||
/* fprintf(stderr,"No convergence in %d SVDCMP iterations",MAXITS); */
|
||||
if (rv1 != RV1)
|
||||
free_dvector(rv1, 0, n-1);
|
||||
return 1;
|
||||
}
|
||||
{
|
||||
double ff, gg, hh, cc, ss, xx, yy;
|
||||
int km1;
|
||||
|
||||
km1 = k - 1;
|
||||
xx = w[ll];
|
||||
yy = w[km1];
|
||||
gg = rv1[km1];
|
||||
hh = rv1[k];
|
||||
ff = ((yy - zz) * (yy + zz) + (gg - hh) * (gg + hh)) / (2.0 * hh * yy);
|
||||
gg = pythag(ff, 1.0);
|
||||
gg = ff < 0.0 ? -gg : gg;
|
||||
ff = ((xx - zz) * (xx + zz) + hh * ((yy / (ff + gg)) - hh)) / xx;
|
||||
cc = ss = 1.0;
|
||||
|
||||
for (j = ll; j <= km1; j++) {
|
||||
double f2, g2, y2, h2, z2;
|
||||
int jp1 = j + 1;
|
||||
g2 = rv1[jp1];
|
||||
y2 = w[jp1];
|
||||
h2 = ss * g2;
|
||||
g2 = cc * g2;
|
||||
rv1[j] = z2 = pythag(ff, h2);
|
||||
cc = ff / z2;
|
||||
ss = h2 / z2;
|
||||
f2 = xx * cc + g2 * ss;
|
||||
g2 = g2 * cc - xx * ss;
|
||||
h2 = y2 * ss;
|
||||
y2 = y2 * cc;
|
||||
|
||||
/* Apply rotation */
|
||||
for (i = 0; i < n; i++) {
|
||||
double x1, z1;
|
||||
x1 = v[i][j];
|
||||
z1 = v[i][jp1];
|
||||
v[i][j] = x1 * cc + z1 * ss;
|
||||
v[i][jp1] = z1 * cc - x1 * ss;
|
||||
}
|
||||
w[j] = z2 = pythag(f2, h2);
|
||||
if (z2 != 0.0) { /* Rotation can be arbitrary */
|
||||
z2 = 1.0 / z2;
|
||||
cc = f2 * z2;
|
||||
ss = h2 * z2;
|
||||
}
|
||||
ff = (cc * g2) + (ss * y2);
|
||||
xx = (cc * y2) - (ss * g2);
|
||||
|
||||
/* Apply rotation */
|
||||
for (i = 0; i < m; i++) {
|
||||
double y1, z1;
|
||||
y1 = a[i][j];
|
||||
z1 = a[i][jp1];
|
||||
a[i][j] = y1 * cc + z1 * ss;
|
||||
a[i][jp1] = z1 * cc - y1 * ss;
|
||||
}
|
||||
}
|
||||
rv1[ll] = 0.0;
|
||||
rv1[k] = ff;
|
||||
w[k] = xx;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (rv1 != RV1)
|
||||
free_dvector(rv1, 0, n-1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------- */
|
||||
/* Threshold the singular values W[] */
|
||||
void svdthresh(
|
||||
double w[], /* Singular values */
|
||||
int n /* Number of unknowns */
|
||||
) {
|
||||
int i;
|
||||
double maxw;
|
||||
|
||||
/* Threshold the w[] values */
|
||||
for (maxw = 0.0, i = 0; i < n; i++) {
|
||||
if (w[i] > maxw)
|
||||
maxw = w[i];
|
||||
}
|
||||
maxw *= 1.0e-12;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (w[i] < maxw)
|
||||
w[i] = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------- */
|
||||
/* Threshold the singular values W[] to give */
|
||||
/* a specific degree of freedom. */
|
||||
void svdsetthresh(
|
||||
double w[], /* Singular values */
|
||||
int n, /* Number of unknowns */
|
||||
int dof /* Expected degree of freedom */
|
||||
) {
|
||||
int i, j;
|
||||
|
||||
/* Set the dof smallest elements to zero */
|
||||
/* (This algorithm is simple but not quick) */
|
||||
for (j = 0; j < dof;) {
|
||||
int k;
|
||||
double minv = 1e38;
|
||||
for (k = j = i = 0; i < n; i++) {
|
||||
if (w[i] == 0.0) {
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
if (w[i] < minv) {
|
||||
minv = w[i];
|
||||
k = i;
|
||||
}
|
||||
}
|
||||
if (j < dof) /* Zero next smallest */
|
||||
w[k] = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------- */
|
||||
#ifndef NEVER /* [und] Use debug version */
|
||||
|
||||
/* Use output of svdcmp() to solve overspecified and/or */
|
||||
/* singular equation A.x = b */
|
||||
int svdbacksub(
|
||||
double **u, /* U[0..m-1][0..n-1] U, W, V SVD decomposition of A[][] */
|
||||
double *w, /* W[0..n-1] */
|
||||
double **v, /* V[0..n-1][0..n-1] (not transpose!) */
|
||||
double b[], /* B[0..m-1] Right hand side of equation */
|
||||
double x[], /* X[0..n-1] Return solution. (May be the same as b[]) */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
) {
|
||||
int i, j;
|
||||
double *tmp, TMP[100]; /* Intermediate value of B . U-1 . W-1 */
|
||||
|
||||
if (n <= 100)
|
||||
tmp = TMP;
|
||||
else
|
||||
tmp = dvector(0, n-1);
|
||||
|
||||
/* A . X = B == U . W . Vt . X = B */
|
||||
/* and U, W, and Vt are trivialy invertable */
|
||||
|
||||
/* Compute B . U-1 . W-1 */
|
||||
for (j = 0; j < n; j++) {
|
||||
if (w[j]) {
|
||||
double s = 0.0;
|
||||
i = 0;
|
||||
for (; i < (m-7); i += 8) {
|
||||
s += b[i+0] * u[i+0][j];
|
||||
s += b[i+1] * u[i+1][j];
|
||||
s += b[i+2] * u[i+2][j];
|
||||
s += b[i+3] * u[i+3][j];
|
||||
s += b[i+4] * u[i+4][j];
|
||||
s += b[i+5] * u[i+5][j];
|
||||
s += b[i+6] * u[i+6][j];
|
||||
s += b[i+7] * u[i+7][j];
|
||||
}
|
||||
for (; i < m; i++)
|
||||
s += b[i] * u[i][j];
|
||||
s /= w[j];
|
||||
tmp[j] = s;
|
||||
} else {
|
||||
tmp[j] = 0.0;
|
||||
}
|
||||
}
|
||||
/* Compute T. V-1 */
|
||||
for (j = 0; j < n; j++) {
|
||||
double s = 0.0;
|
||||
i = 0;
|
||||
for (; i < (n-7); i += 8) {
|
||||
s += v[j][i+0] * tmp[i+0];
|
||||
s += v[j][i+1] * tmp[i+1];
|
||||
s += v[j][i+2] * tmp[i+2];
|
||||
s += v[j][i+3] * tmp[i+3];
|
||||
s += v[j][i+4] * tmp[i+4];
|
||||
s += v[j][i+5] * tmp[i+5];
|
||||
s += v[j][i+6] * tmp[i+6];
|
||||
s += v[j][i+7] * tmp[i+7];
|
||||
}
|
||||
for (; i < n; i++)
|
||||
s += v[j][i] * tmp[i];
|
||||
x[j] = s;
|
||||
}
|
||||
if (tmp != TMP)
|
||||
free_dvector(tmp, 0, n-1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else /* Diagnostic version */
|
||||
|
||||
/* Use output of svdcmp() to solve overspecified and/or */
|
||||
/* singular equation A.x = b */
|
||||
int svdbacksub(
|
||||
double **u, /* U[0..m-1][0..n-1] U, W, V SVD decomposition of A[][] */
|
||||
double *w, /* W[0..n-1] */
|
||||
double **v, /* V[0..n-1][0..n-1] (not transpose!) */
|
||||
double b[], /* B[0..m-1] Right hand side of equation */
|
||||
double x[], /* X[0..n-1] Return solution. (May be the same as b[]) */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
) {
|
||||
int i, j;
|
||||
double *tmp, TMP[100]; /* Intermediate value of B . U-1 . W-1 */
|
||||
|
||||
printf("svdbacksub diag:\n");
|
||||
|
||||
if (n <= 100)
|
||||
tmp = TMP;
|
||||
else
|
||||
tmp = dvector(0, n-1);
|
||||
|
||||
/* A . X = B == U . W . Vt . X = B */
|
||||
/* and U, W, and Vt are trivialy invertable */
|
||||
|
||||
/* Compute B . U-1 . W-1 */
|
||||
for (j = 0; j < n; j++) {
|
||||
if (w[j]) {
|
||||
double s = 0.0;
|
||||
i = 0;
|
||||
for (; i < m; i++) {
|
||||
s += b[i] * u[i][j];
|
||||
printf("s += b[%d] %f * u[%d][%d] %f => %f\n", i, b[i], i, j, u[i][j], s);
|
||||
}
|
||||
s /= w[j];
|
||||
printf("s /= w[%d] %f => %f\n", j, w[j], s);
|
||||
tmp[j] = s;
|
||||
} else {
|
||||
tmp[j] = 0.0;
|
||||
}
|
||||
}
|
||||
/* Compute T. V-1 */
|
||||
for (j = 0; j < n; j++) {
|
||||
double s = 0.0;
|
||||
i = 0;
|
||||
for (; i < n; i++) {
|
||||
s += v[j][i] * tmp[i];
|
||||
printf("s += v[%d][%d] %f * tmp[%d] %f => %f\n", j, i, v[j][i], i, tmp[i], s);
|
||||
}
|
||||
x[j] = s;
|
||||
printf("x[%d] = %f\n", j, x[j]);
|
||||
}
|
||||
if (tmp != TMP)
|
||||
free_dvector(tmp, 0, n-1);
|
||||
|
||||
printf("svdbacksub done:\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* Solve the equation A.x = b using SVD */
|
||||
/* (The w[] values are thresholded for best accuracy) */
|
||||
/* Return non-zero if no solution found */
|
||||
int svdsolve(
|
||||
double **a, /* A[0..m-1][0..n-1] input A[][], will return U[][] */
|
||||
double b[], /* B[0..m-1] Right hand side of equation, return solution */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
) {
|
||||
int i;
|
||||
double *w, W[8];
|
||||
double **v, *VP[8], V[8][8];
|
||||
double maxw;
|
||||
|
||||
if (n <= 8) {
|
||||
w = W;
|
||||
VP[0] = V[0]; VP[1] = V[1]; VP[2] = V[2]; VP[3] = V[3];
|
||||
VP[4] = V[4]; VP[5] = V[5]; VP[6] = V[6]; VP[7] = V[7];
|
||||
v = VP;
|
||||
} else {
|
||||
w = dvector(0, n-1);
|
||||
v = dmatrix(0, n-1, 0, n-1);
|
||||
}
|
||||
|
||||
/* Singular value decompose */
|
||||
if (svdecomp(a, w, v, m, n)) {
|
||||
if (w != W) {
|
||||
free_dvector(w, 0, n-1);
|
||||
free_dmatrix(v, 0, n-1, 0, n-1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Threshold the w[] values */
|
||||
for (maxw = 0.0, i = 0; i < n; i++) {
|
||||
if (w[i] > maxw)
|
||||
maxw = w[i];
|
||||
}
|
||||
maxw *= 1.0e-12;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (w[i] < maxw)
|
||||
w[i] = 0.0;
|
||||
}
|
||||
|
||||
/* Back substitute to solve the equation */
|
||||
svdbacksub(a, w, v, b, b, m, n);
|
||||
|
||||
if (w != W) {
|
||||
free_dvector(w, 0, n-1);
|
||||
free_dmatrix(v, 0, n-1, 0, n-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* Solve the equation A.x = b using SVD */
|
||||
/* The top s out of n singular values will be used */
|
||||
/* Return non-zero if no solution found */
|
||||
int svdsolve_s(
|
||||
double **a, /* A[0..m-1][0..n-1] input A[][], will return U[][] */
|
||||
double b[], /* B[0..m-1] Right hand side of equation, return solution */
|
||||
int m, /* Number of equations */
|
||||
int n, /* Number of unknowns */
|
||||
int s /* Number of singular values */
|
||||
) {
|
||||
int i, j;
|
||||
double *w, W[8];
|
||||
int *sw, SW[8];
|
||||
double **v, *VP[8], V[8][8];
|
||||
double maxw;
|
||||
|
||||
if (n <= 8) {
|
||||
w = W;
|
||||
sw = SW;
|
||||
VP[0] = V[0]; VP[1] = V[1]; VP[2] = V[2]; VP[3] = V[3];
|
||||
VP[4] = V[4]; VP[5] = V[5]; VP[6] = V[6]; VP[7] = V[7];
|
||||
v = VP;
|
||||
} else {
|
||||
w = dvector(0, n-1);
|
||||
sw = ivector(0, n-1);
|
||||
v = dmatrix(0, n-1, 0, n-1);
|
||||
}
|
||||
|
||||
/* Singular value decompose */
|
||||
if (svdecomp(a, w, v, m, n)) {
|
||||
if (w != W) {
|
||||
free_dvector(w, 0, n-1);
|
||||
free_dmatrix(v, 0, n-1, 0, n-1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Create sorted index of w[] */
|
||||
for (maxw = 0.0, i = 0; i < n; i++) {
|
||||
sw[i] = i;
|
||||
if (w[i] > maxw)
|
||||
maxw = w[i];
|
||||
}
|
||||
maxw *= 1.0e-12;
|
||||
|
||||
/* Really dumb exchange sort.. */
|
||||
for (i = 0; i < (n-1); i++) {
|
||||
for (j = i+1; j < n; j++) {
|
||||
if (w[sw[i]] > w[sw[j]]) {
|
||||
int tt = sw[i];
|
||||
sw[i] = sw[j];
|
||||
sw[j] = tt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Set the (n - s) smallest values to zero */
|
||||
s = n - s;
|
||||
if (s < 0)
|
||||
s = 0;
|
||||
if (s > n)
|
||||
s = n;
|
||||
for (i = 0; i < s; i++)
|
||||
w[sw[i]] = 0.0;
|
||||
|
||||
/* And threshold them too */
|
||||
for (maxw = 0.0, i = 0; i < n; i++) {
|
||||
if (w[i] < maxw)
|
||||
w[i] = 0.0;
|
||||
}
|
||||
|
||||
/* Back substitute to solve the equation */
|
||||
svdbacksub(a, w, v, b, b, m, n);
|
||||
|
||||
if (w != W) {
|
||||
free_dvector(w, 0, n-1);
|
||||
free_ivector(sw, 0, n-1);
|
||||
free_dmatrix(v, 0, n-1, 0, n-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
/* Solve the equation A.x = b using Direct calculation, LU or SVD as appropriate */
|
||||
/* Return non-zero if no solution found */
|
||||
|
||||
#include "ludecomp.h"
|
||||
|
||||
int gen_solve_se(
|
||||
double **a, /* A[0..m-1][0..n-1] input A[][], will return U[][] */
|
||||
double b[], /* B[0..m-1] Right hand side of equation, return solution */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
) {
|
||||
if (n == m) {
|
||||
if (n == 1) { /* So simple, solve it directly */
|
||||
double tt = a[0][0];
|
||||
if (fabs(tt) <= DBL_MIN)
|
||||
return 1;
|
||||
b[0] = b[0]/tt;
|
||||
return 0;
|
||||
} else {
|
||||
return solve_se(a, b, n);
|
||||
}
|
||||
} else {
|
||||
return svdsolve(a, b, m, n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------- */
|
||||
|
||||
/* Compute the inverse matrix Ai[[0..n-1][0..m-1] from SVD components */
|
||||
void svdinverse(
|
||||
double **u, /* U[0..m-1][0..n-1] U, W, V SVD decomposition of A[][] */
|
||||
double *w, /* W[0..n-1] */
|
||||
double **v, /* V[0..n-1][0..n-1] (not transpose!) */
|
||||
double **ia, /* iA[0..n-1][0..m-1] return inverse of A */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
) {
|
||||
int i, j, k;
|
||||
|
||||
for (j = 0; j < n; j++) { /* Outer of result */
|
||||
for (i = 0; i < m; i++) { /* Inner of result */
|
||||
ia[j][i] = 0.0;
|
||||
for (k = 0; k < n; k++) {
|
||||
if (w[k] != 0.0)
|
||||
ia[j][i] += v[j][k] * u[i][k] / w[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Compute x from b using inverse A matrix */
|
||||
void svdmulia(
|
||||
double **ia, /* iA[0..n-1][0..m-1] inverse of A */
|
||||
double b[], /* B[0..m-1] Right hand side of equation */
|
||||
double x[], /* X[0..n-1] Return solution. (Must be different to b[]) */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
) {
|
||||
int i, j;
|
||||
|
||||
for (j = 0; j < n; j++) {
|
||||
double s = 0.0;
|
||||
i = 0.0;
|
||||
for (; i < (m-7); i += 8) {
|
||||
s += b[i+0] * ia[j][i+0];
|
||||
s += b[i+1] * ia[j][i+1];
|
||||
s += b[i+2] * ia[j][i+2];
|
||||
s += b[i+3] * ia[j][i+3];
|
||||
s += b[i+4] * ia[j][i+4];
|
||||
s += b[i+5] * ia[j][i+5];
|
||||
s += b[i+6] * ia[j][i+6];
|
||||
s += b[i+7] * ia[j][i+7];
|
||||
}
|
||||
for (; i < m; i++)
|
||||
s += b[i] * ia[j][i];
|
||||
x[j] = s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
141
Argyll_V3.5.0/numlib/svd.h
Normal file
141
Argyll_V3.5.0/numlib/svd.h
Normal file
@@ -0,0 +1,141 @@
|
||||
#ifndef SVD_H
|
||||
#define SVD_H
|
||||
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
||||
U[] decomposes A[]'s columns into orthogonal, singular vectors.
|
||||
U[]'s columns are vectors that sum to 1.0, i.e. they leave a vectors normal unchanged.
|
||||
The inverse of U[] is its transpose.
|
||||
U[]'s columns corresponding to non-zero W[] are the orthonormal vectors that span
|
||||
the (input) RANGE space. Columns corresponding to zero W[] are zero.
|
||||
|
||||
W[] will return singlular values, i.e. the weighting of the singular vectors.
|
||||
It's inverse is is the reciprical of its elements.
|
||||
|
||||
V[] composes the singular vectors back into A[]'s rows.
|
||||
V[]'s columns and rows are orthonormal.
|
||||
V[]'s columns corresponding to non-zero W[] are the orthonormal vectors that span
|
||||
the (output) RANGE space.
|
||||
V[]'s columns corresponding to zero W[] are the (output) othonormal vectors that span
|
||||
the NULL space.
|
||||
The inverse of V[] is its transpose.
|
||||
|
||||
To re-form, A = U.W.Vt, i.e. multiply by transpose of V.
|
||||
|
||||
i.e. mult. input vector[m] by U[] converts to [n] compact, orthogonal
|
||||
basis vectors. W then scales them appropiately, setting null space
|
||||
vectors to 0. V[] then transforms from the orthogonal basis vectors
|
||||
to A[]'s output space.
|
||||
|
||||
To reveal NULL space, make sure n >= m, since U[] vectors corrsponding
|
||||
to zero's are set to zero.
|
||||
|
||||
Inverse of A = V.1/W.Ut ?
|
||||
*/
|
||||
|
||||
/* Compute Singular Value Decomposition of A = U.W.Vt */
|
||||
/* Return status value: */
|
||||
/* 0 = no error */
|
||||
/* 1 - Too many itterations */
|
||||
int svdecomp(
|
||||
double **a, /* A[0..m-1][0..n-1], return U[0..m-1][0..n-1] */
|
||||
double *w, /* return W[0..n-1] = singular values */
|
||||
double **v, /* return V[0..n-1][0..n-1] (not transpose!) */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
);
|
||||
|
||||
/* Threshold the singular values W[] */
|
||||
void svdthresh(
|
||||
double w[], /* Singular values */
|
||||
int n /* Number of unknowns */
|
||||
);
|
||||
|
||||
/* Threshold the singular values W[] to give a specific dof */
|
||||
void svdsetthresh(
|
||||
double w[], /* Singular values */
|
||||
int n, /* Number of unknowns */
|
||||
int dof /* Expected degree of freedom */
|
||||
);
|
||||
|
||||
/* Use output of svdcmp() to solve overspecified and/or */
|
||||
/* singular equation A.x = b */
|
||||
int svdbacksub(
|
||||
double **u, /* U[0..m-1][0..n-1] U, W, V SVD decomposition of A[][] */
|
||||
double *w, /* W[0..n-1] */
|
||||
double **v, /* V[0..n-1][0..n-1] (not transpose!) */
|
||||
double b[], /* B[0..m-1] Right hand side of equation */
|
||||
double x[], /* X[0..n-1] Return solution. (May be the same as b[]) */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
);
|
||||
|
||||
/* Solve the equation A.x = b using SVD */
|
||||
/* (The w[] values are thresholded for best accuracy) */
|
||||
/* Return non-zero if no solution found */
|
||||
/* !!! Note that A[][] will be changed !!! */
|
||||
int svdsolve(
|
||||
double **a, /* A[0..m-1][0..n-1] input A[][], will return U[][] */
|
||||
double b[], /* B[0..m-1] Right hand side of equation, return solution */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
);
|
||||
|
||||
/* Solve the equation A.x = b using SVD */
|
||||
/* The top s out of n singular values will be used */
|
||||
/* Return non-zero if no solution found */
|
||||
/* !!! Note that A[][] will be changed !!! */
|
||||
int svdsolve_s(
|
||||
double **a, /* A[0..m-1][0..n-1] input A[][], will return U[][] */
|
||||
double b[], /* B[0..m-1] Right hand side of equation, return solution */
|
||||
int m, /* Number of equations */
|
||||
int n, /* Number of unknowns */
|
||||
int s /* Number of unknowns */
|
||||
);
|
||||
|
||||
/* Solve the equation A.x = b using Direct calculation, LU or SVD as appropriate */
|
||||
/* Return non-zero if no solution found */
|
||||
/* !!! Note that A[][] will be changed !!! */
|
||||
int gen_solve_se(
|
||||
double **a, /* A[0..m-1][0..n-1] input A[][], will return U[][] */
|
||||
double b[], /* B[0..m-1] Right hand side of equation, return solution */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
);
|
||||
|
||||
/* Compute the inverse matrix Ai[[0..n-1][0..m-1] from SVD components */
|
||||
void svdinverse(
|
||||
double **u, /* U[0..m-1][0..n-1] U, W, V SVD decomposition of A[][] */
|
||||
double *w, /* W[0..n-1] */
|
||||
double **v, /* V[0..n-1][0..n-1] (not transpose!) */
|
||||
double **ia, /* iA[0..n-1][0..m-1] return inverse of A */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
);
|
||||
|
||||
/* Compute x from b using inverse A matrix */
|
||||
void svdmulia(
|
||||
double **ia, /* iA[0..n-1][0..m-1] inverse of A */
|
||||
double b[], /* B[0..m-1] Right hand side of equation */
|
||||
double x[], /* X[0..n-1] Return solution. (Must be different to b[]) */
|
||||
int m, /* Number of equations */
|
||||
int n /* Number of unknowns */
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SVD_H */
|
||||
206
Argyll_V3.5.0/numlib/svdtest.c
Normal file
206
Argyll_V3.5.0/numlib/svdtest.c
Normal file
@@ -0,0 +1,206 @@
|
||||
|
||||
/* SVD test */
|
||||
/* Verify that the SVD solver does what it is supposed to. */
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
/* We assume two device dependent variables, and one objective function value */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "numlib.h"
|
||||
|
||||
int main(void) {
|
||||
int its;
|
||||
int i,j,x;
|
||||
double **a; /* A[0..M-1][0..N-1] input */
|
||||
double **u; /* U[0..M-1][0..N-1] output */
|
||||
double *w; /* W[0..N-1] output */
|
||||
double **v; /* V[0..N-1][0..N-1] output */
|
||||
double **a2; /* A[0..M-1][0..N-1] check on a */
|
||||
double **t; /* A[0..M-1][0..N-1] temp */
|
||||
int m,n;
|
||||
|
||||
printf("Test SVD\n");
|
||||
|
||||
for (its = 400; its > 0; its--) {
|
||||
int bad;
|
||||
int bad0;
|
||||
|
||||
m = i_rand(1,30); /* Number of equations */
|
||||
n = i_rand(1,30); /* Number of unknowns */
|
||||
|
||||
a = dmatrix(0,m-1, 0,n-1);
|
||||
t = dmatrix(0,m-1, 0,n-1);
|
||||
a2 = dmatrix(0,m-1, 0,n-1);
|
||||
u = dmatrix(0,m-1, 0,n-1);
|
||||
w = dvector(0,n-1);
|
||||
v = dmatrix(0,n-1, 0,n-1);
|
||||
|
||||
printf("Testing %d by %d\n",m,n);
|
||||
|
||||
/* Create A matrix */
|
||||
for (j = 0; j < m; j++)
|
||||
for (i = 0; i < n; i++)
|
||||
a[j][i] = d_rand(-10.0, 10.0);
|
||||
|
||||
/* Setup u */
|
||||
for (j = 0; j < m; j++)
|
||||
for (i = 0; i < n; i++)
|
||||
u[j][i] = a[j][i];
|
||||
|
||||
/* decompose A into U, W and V */
|
||||
svdecomp(u, w, v, m, n);
|
||||
|
||||
/* Check results by computing a2 = U.W.Vt */
|
||||
for (j = 0; j < m; j++) { /* U.W */
|
||||
for (i = 0; i < n; i++) {
|
||||
t[j][i] = 0.0;
|
||||
for (x = 0; x < n; x++) {
|
||||
if (x == i)
|
||||
t[j][i] += u[j][x] * w[x];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (j = 0; j < m; j++) { /* .Vt */
|
||||
for (i = 0; i < n; i++) {
|
||||
a2[j][i] = 0.0;
|
||||
for (x = 0; x < n; x++) {
|
||||
a2[j][i] += t[j][x] * v[i][x];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Now check */
|
||||
bad = 0;
|
||||
for (j = 0; j < m; j++)
|
||||
for (i = 0; i < n; i++) {
|
||||
double tt;
|
||||
tt = a2[j][i] - a[j][i];
|
||||
tt = fabs(tt);
|
||||
if (tt > 0.0000001) {
|
||||
bad = 1;
|
||||
}
|
||||
}
|
||||
if (bad)
|
||||
printf("A == U.W.Vt Check failed!\n");
|
||||
|
||||
|
||||
/* Check that U and Ut are inverses */
|
||||
bad = bad0 = 0;
|
||||
for (j = 0; j < n; j++) {
|
||||
for (i = 0; i < n; i++) {
|
||||
double t2, tt = 0.0;
|
||||
for (x = 0; x < m; x++) {
|
||||
tt += u[x][j] * u[x][i];
|
||||
}
|
||||
t2 = tt;
|
||||
if (i == j)
|
||||
tt -= 1.0;
|
||||
tt = fabs(tt);
|
||||
if (tt > 0.0000001) {
|
||||
if (i == j && fabs(t2) < 0.0000001)
|
||||
bad0++; /* Unexpected zero diagonal */
|
||||
else {
|
||||
bad = 1;
|
||||
printf("Possible U error at %d %d = %f \n",j,i,tt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Expect n-m diagnals to be 0 instead of 1 if m < n */
|
||||
if (bad || (m >= n && bad0) || (m < n && bad0 != n-m))
|
||||
printf("U,Ut == 1 Check failed!\n");
|
||||
|
||||
/* Check that V and Vt are inverses */
|
||||
bad = 0;
|
||||
for (j = 0; j < n; j++) {
|
||||
for (i = 0; i < n; i++) {
|
||||
double tt = 0.0;
|
||||
for (x = 0; x < n; x++) {
|
||||
tt += v[j][x] * v[i][x];
|
||||
}
|
||||
if (i == j)
|
||||
tt -= 1.0;
|
||||
tt = fabs(tt);
|
||||
if (tt > 0.0000001) {
|
||||
bad = 1;
|
||||
printf("V Error at %d %d = %f \n",j,i,tt);
|
||||
}
|
||||
}
|
||||
if (bad)
|
||||
printf("V,Vt == 1 Check failed!\n");
|
||||
}
|
||||
|
||||
#ifdef NEVER
|
||||
printf("A = %f %f %f\n %f %f %f\n %f %f %f\n\n",
|
||||
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2], a[2][0], a[2][1], a[2][2]);
|
||||
|
||||
printf("u = %f %f %f\n %f %f %f\n %f %f %f\n\n",
|
||||
u[0][0], u[0][1], u[0][2], u[1][0], u[1][1], u[1][2], u[2][0], u[2][1], u[2][2]);
|
||||
|
||||
printf("w = %f %f %f\n\n", w[0],w[1],w[2]);
|
||||
|
||||
printf("V = %f %f %f\n %f %f %f\n %f %f %f\n\n",
|
||||
v[0][0], v[0][1], v[0][2], v[1][0], v[1][1], v[1][2], v[2][0], v[2][1], v[2][2]);;
|
||||
|
||||
printf("A2 = %f %f %f\n %f %f %f\n %f %f %f\n\n",
|
||||
a2[0][0], a2[0][1], a2[0][2], a2[1][0], a2[1][1],
|
||||
a2[1][2], a2[2][0], a2[2][1], a2[2][2]);
|
||||
#endif
|
||||
|
||||
free_dmatrix(a, 0,m-1, 0,n-1);
|
||||
free_dmatrix(t, 0,m-1, 0,n-1);
|
||||
free_dmatrix(a2, 0,m-1, 0,n-1);
|
||||
free_dmatrix(u, 0,m-1, 0,n-1);
|
||||
free_dvector(w, 0,n-1);
|
||||
free_dmatrix(v, 0,n-1, 0,n-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
199
Argyll_V3.5.0/numlib/tconjgrad.c
Normal file
199
Argyll_V3.5.0/numlib/tconjgrad.c
Normal file
@@ -0,0 +1,199 @@
|
||||
/* Code to test the conjgrad minimiser */
|
||||
/*
|
||||
* Copyright 1999, 2018 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "numlib.h"
|
||||
|
||||
/* Final approximate solution: */
|
||||
|
||||
double expect[9] = {
|
||||
-0.5706545E+00,
|
||||
-0.6816283E+00,
|
||||
-0.7017325E+00,
|
||||
-0.7042129E+00,
|
||||
-0.7013690E+00,
|
||||
-0.6918656E+00,
|
||||
-0.6657920E+00,
|
||||
-0.5960342E+00,
|
||||
-0.4164121E+00 };
|
||||
|
||||
double fcn( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[] /* Multivriate input value */
|
||||
);
|
||||
|
||||
static double dfcn(
|
||||
void *fdata,
|
||||
double dp[],
|
||||
double tp[]
|
||||
);
|
||||
|
||||
#define N 9
|
||||
|
||||
static void progress(void *pdata, int perc) {
|
||||
printf("%c% 3d%%",cr_char,perc);
|
||||
if (perc == 100)
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double cp[N]; /* Function input values */
|
||||
double s[N]; /* Search area */
|
||||
double err;
|
||||
int j;
|
||||
int nprint = 0; /* Itteration debugging print = off */
|
||||
int rc;
|
||||
|
||||
error_program = "tconjgrad"; /* Set global error reporting string */
|
||||
check_if_not_interactive();
|
||||
|
||||
/* ====== test with partial derivative ====== */
|
||||
/* The following starting values provide a rough solution. */
|
||||
for (j = 0; j < N; j++) {
|
||||
cp[j] = -1.f;
|
||||
s[j] = 0.9;
|
||||
}
|
||||
|
||||
nprint = 0;
|
||||
|
||||
/* Set tol to the square root of the machine precision. */
|
||||
/* Unless high precision solutions are required, */
|
||||
/* this is the recommended setting. */
|
||||
|
||||
rc = conjgrad(
|
||||
&err,
|
||||
N, /* Dimentionality */
|
||||
cp, /* Initial starting point */
|
||||
s, /* Size of initial search area */
|
||||
0.00000001, /* Tollerance of error change to stop on */
|
||||
1000, /* Maximum iterations allowed */
|
||||
fcn, /* Error function to evaluate */
|
||||
dfcn, /* Partial derivative function */
|
||||
NULL, /* Opaque data needed by function */
|
||||
progress, /* Progress callback */
|
||||
NULL /* Context for callback */
|
||||
);
|
||||
|
||||
|
||||
fprintf(stdout,"Using partial derivative:\n");
|
||||
fprintf(stdout,"Status = %d, final approximate solution err = %f:\n",rc,err);
|
||||
for (j = 0; j < N; j++) {
|
||||
fprintf(stdout,"cp[%d] = %e, expect %e\n",j,cp[j],expect[j]);
|
||||
}
|
||||
|
||||
/* ====== test without partial derivative ====== */
|
||||
/* The following starting values provide a rough solution. */
|
||||
for (j = 0; j < N; j++) {
|
||||
cp[j] = -1.f;
|
||||
s[j] = 0.9;
|
||||
}
|
||||
|
||||
nprint = 0;
|
||||
|
||||
/* Set tol to the square root of the machine precision. */
|
||||
/* Unless high precision solutions are required, */
|
||||
/* this is the recommended setting. */
|
||||
|
||||
rc = conjgrad(
|
||||
&err,
|
||||
N, /* Dimentionality */
|
||||
cp, /* Initial starting point */
|
||||
s, /* Size of initial search area */
|
||||
0.00000001, /* Tollerance of error change to stop on */
|
||||
1000, /* Maximum iterations allowed */
|
||||
fcn, /* Error function to evaluate */
|
||||
NULL, /* No partial derivative function */
|
||||
NULL, /* Opaque data needed by function */
|
||||
progress, /* Progress callback */
|
||||
NULL /* Context for callback */
|
||||
);
|
||||
|
||||
|
||||
fprintf(stdout,"\nNot using partial derivative:\n");
|
||||
fprintf(stdout,"Status = %d, final approximate solution err = %f:\n",rc,err);
|
||||
for (j = 0; j < N; j++) {
|
||||
fprintf(stdout,"cp[%d] = %e, expect %e\n",j,cp[j],expect[j]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} /* main() */
|
||||
|
||||
/* Function being minimized */
|
||||
double fcn( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[] /* Multivriate input value */
|
||||
) {
|
||||
double err, tt;
|
||||
double temp, temp1, temp2;
|
||||
int k;
|
||||
|
||||
/* Function Body */
|
||||
err = 0.0;
|
||||
for (k = 0; k < N; ++k) {
|
||||
temp = (3.0 - 2.0 * tp[k]) * tp[k];
|
||||
temp1 = 0.0;
|
||||
if (k != 0) {
|
||||
temp1 = tp[k-1];
|
||||
}
|
||||
temp2 = 0.0;
|
||||
if (k != ((N)-1))
|
||||
temp2 = tp[k+1];
|
||||
tt = temp - temp1 - 2.0 * temp2 + 1.0;
|
||||
err += tt * tt;
|
||||
}
|
||||
err = sqrt(err);
|
||||
//printf("Returning %16.14f\n",err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
/* Compute aprox. forward difference */
|
||||
|
||||
#define JEPS 1.0e-8 /* Aprox. sqrt of machine precision */
|
||||
|
||||
static double dfcn(
|
||||
void *fdata,
|
||||
double dp[],
|
||||
double tp[]
|
||||
) {
|
||||
int i;
|
||||
double d, h, temp;
|
||||
|
||||
d = fcn(fdata, tp);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
temp = tp[i];
|
||||
|
||||
h = JEPS * fabs(temp);
|
||||
if (h == 0.0)
|
||||
h = JEPS;
|
||||
tp[i] = temp + h; /* Add delta */
|
||||
h = tp[i] - temp; /* Actual delta with fp precision limits */
|
||||
|
||||
dp[i] = (fcn(fdata, tp) - d)/h;
|
||||
|
||||
tp[i] = temp; /* Restore value */
|
||||
}
|
||||
|
||||
// return DFUNC_NRV;
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
117
Argyll_V3.5.0/numlib/tdhsx.c
Normal file
117
Argyll_V3.5.0/numlib/tdhsx.c
Normal file
@@ -0,0 +1,117 @@
|
||||
|
||||
/*
|
||||
* Code to test the downhill simplex minimiser
|
||||
*
|
||||
* Copyright 1999 - 2005 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "numlib.h"
|
||||
|
||||
/* Final approximate solution: */
|
||||
|
||||
double expect[9] = {
|
||||
-0.5706545E+00,
|
||||
-0.6816283E+00,
|
||||
-0.7017325E+00,
|
||||
-0.7042129E+00,
|
||||
-0.7013690E+00,
|
||||
-0.6918656E+00,
|
||||
-0.6657920E+00,
|
||||
-0.5960342E+00,
|
||||
-0.4164121E+00 };
|
||||
|
||||
double fcn( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[]); /* Multivriate input value */
|
||||
|
||||
#define N 9
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double cp[N]; /* Function input values */
|
||||
double s[N]; /* Search area */
|
||||
double err;
|
||||
int j;
|
||||
int nprint = 0; /* Itteration debugging print = off */
|
||||
int rv;
|
||||
|
||||
error_program = "tdhsx"; /* Set global error reporting string */
|
||||
|
||||
/* The following starting values provide a rough solution. */
|
||||
for (j = 0; j < N; j++) {
|
||||
cp[j] = -1.f;
|
||||
s[j] = 0.9;
|
||||
}
|
||||
|
||||
nprint = 0;
|
||||
|
||||
/* Set tol to the square root of the machine precision. */
|
||||
/* Unless high precision solutions are required, */
|
||||
/* this is the recommended setting. */
|
||||
|
||||
rv = dhsx(
|
||||
&err, /* return residual */
|
||||
N, /* Dimentionality */
|
||||
cp, /* Initial starting point */
|
||||
s, /* Size of initial search area */
|
||||
0.0000001, /* Tollerance of error change to stop on */
|
||||
1e6, /* Absolute error to stop on */
|
||||
// 10.0, /* Tollerance of error change to stop on */
|
||||
// 0.0001, /* Absolute error to stop on */
|
||||
1000, /* Maximum iterations allowed */
|
||||
fcn, /* Error function to evaluate */
|
||||
NULL); /* Opaque data needed by function */
|
||||
|
||||
|
||||
fprintf(stdout,"Return code %d, residual err = %f:\n",rv, err);
|
||||
for (j = 0; j < N; j++) {
|
||||
fprintf(stdout,"cp[%d] = %e, expect %e\n",j,cp[j],expect[j]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} /* main() */
|
||||
|
||||
/* Function being minimized */
|
||||
double fcn( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[] /* Multivriate input value */
|
||||
) {
|
||||
double err, tt;
|
||||
double temp, temp1, temp2;
|
||||
int k;
|
||||
|
||||
/* Function Body */
|
||||
err = 0.0;
|
||||
for (k = 0; k < N; ++k) {
|
||||
temp = (3.0 - 2.0 * tp[k]) * tp[k];
|
||||
temp1 = 0.0;
|
||||
if (k != 0) {
|
||||
temp1 = tp[k-1];
|
||||
}
|
||||
temp2 = 0.0;
|
||||
if (k != ((N)-1))
|
||||
temp2 = tp[k+1];
|
||||
tt = temp - temp1 - 2.0 * temp2 + 1.0;
|
||||
err += tt * tt;
|
||||
}
|
||||
err = sqrt(err);
|
||||
// printf("Returning %16.14f\n",err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
123
Argyll_V3.5.0/numlib/tpowell.c
Normal file
123
Argyll_V3.5.0/numlib/tpowell.c
Normal file
@@ -0,0 +1,123 @@
|
||||
/* Code to test the powell minimiser */
|
||||
/*
|
||||
* Copyright 1999 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "numlib.h"
|
||||
|
||||
/* Final approximate solution: */
|
||||
|
||||
double expect[9] = {
|
||||
-0.5706545E+00,
|
||||
-0.6816283E+00,
|
||||
-0.7017325E+00,
|
||||
-0.7042129E+00,
|
||||
-0.7013690E+00,
|
||||
-0.6918656E+00,
|
||||
-0.6657920E+00,
|
||||
-0.5960342E+00,
|
||||
-0.4164121E+00 };
|
||||
|
||||
double fcn( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[]); /* Multivriate input value */
|
||||
|
||||
#define N 9
|
||||
|
||||
static void progress(void *pdata, int perc) {
|
||||
printf("%c% 3d%%",cr_char,perc);
|
||||
if (perc == 100)
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double cp[N]; /* Function input values */
|
||||
double s[N]; /* Search area */
|
||||
double err;
|
||||
int j;
|
||||
int nprint = 0; /* Itteration debugging print = off */
|
||||
int rc;
|
||||
|
||||
error_program = "tpowell"; /* Set global error reporting string */
|
||||
check_if_not_interactive();
|
||||
|
||||
/* The following starting values provide a rough solution. */
|
||||
for (j = 0; j < N; j++) {
|
||||
cp[j] = -1.f;
|
||||
s[j] = 0.9;
|
||||
}
|
||||
|
||||
nprint = 0;
|
||||
|
||||
/* Set tol to the square root of the machine precision. */
|
||||
/* Unless high precision solutions are required, */
|
||||
/* this is the recommended setting. */
|
||||
|
||||
rc = powell(
|
||||
&err,
|
||||
N, /* Dimentionality */
|
||||
cp, /* Initial starting point */
|
||||
s, /* Size of initial search area */
|
||||
0.00000001, /* Tollerance of error change to stop on */
|
||||
1000, /* Maximum iterations allowed */
|
||||
fcn, /* Error function to evaluate */
|
||||
NULL, /* Opaque data needed by function */
|
||||
progress, /* Progress callback */
|
||||
NULL /* Context for callback */
|
||||
);
|
||||
|
||||
|
||||
fprintf(stdout,"Status = %d, final approximate solution err = %f:\n",rc,err);
|
||||
for (j = 0; j < N; j++) {
|
||||
fprintf(stdout,"cp[%d] = %e, expect %e\n",j,cp[j],expect[j]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} /* main() */
|
||||
|
||||
/* Function being minimized */
|
||||
double fcn( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[] /* Multivriate input value */
|
||||
) {
|
||||
double err, tt;
|
||||
double temp, temp1, temp2;
|
||||
int k;
|
||||
|
||||
/* Function Body */
|
||||
err = 0.0;
|
||||
for (k = 0; k < N; ++k) {
|
||||
temp = (3.0 - 2.0 * tp[k]) * tp[k];
|
||||
temp1 = 0.0;
|
||||
if (k != 0) {
|
||||
temp1 = tp[k-1];
|
||||
}
|
||||
temp2 = 0.0;
|
||||
if (k != ((N)-1))
|
||||
temp2 = tp[k+1];
|
||||
tt = temp - temp1 - 2.0 * temp2 + 1.0;
|
||||
err += tt * tt;
|
||||
}
|
||||
err = sqrt(err);
|
||||
//printf("Returning %16.14f\n",err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
197
Argyll_V3.5.0/numlib/tvarmet.c
Normal file
197
Argyll_V3.5.0/numlib/tvarmet.c
Normal file
@@ -0,0 +1,197 @@
|
||||
/* Code to test the conjgrad minimiser */
|
||||
/*
|
||||
* Copyright 1999, 2018 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "numlib.h"
|
||||
|
||||
/* Final approximate solution: */
|
||||
|
||||
double expect[9] = {
|
||||
-0.5706545E+00,
|
||||
-0.6816283E+00,
|
||||
-0.7017325E+00,
|
||||
-0.7042129E+00,
|
||||
-0.7013690E+00,
|
||||
-0.6918656E+00,
|
||||
-0.6657920E+00,
|
||||
-0.5960342E+00,
|
||||
-0.4164121E+00 };
|
||||
|
||||
double fcn( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[] /* Multivriate input value */
|
||||
);
|
||||
|
||||
static double dfcn(
|
||||
void *fdata,
|
||||
double dp[],
|
||||
double tp[]
|
||||
);
|
||||
|
||||
#define N 9
|
||||
|
||||
#ifdef NEVER
|
||||
static void progress(void *pdata, int perc) {
|
||||
printf("%c% 3d%%",cr_char,perc);
|
||||
if (perc == 100)
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double cp[N]; /* Function input values */
|
||||
double s[N]; /* Search area */
|
||||
double err;
|
||||
int j;
|
||||
int nprint = 0; /* Itteration debugging print = off */
|
||||
int rc;
|
||||
|
||||
error_program = "tvarmet"; /* Set global error reporting string */
|
||||
check_if_not_interactive();
|
||||
|
||||
/* ====== test with partial derivative ====== */
|
||||
/* The following starting values provide a rough solution. */
|
||||
for (j = 0; j < N; j++) {
|
||||
cp[j] = -1.f;
|
||||
s[j] = 0.9;
|
||||
}
|
||||
|
||||
nprint = 0;
|
||||
|
||||
/* Set tol to the square root of the machine precision. */
|
||||
/* Unless high precision solutions are required, */
|
||||
/* this is the recommended setting. */
|
||||
|
||||
rc = varmet(
|
||||
&err,
|
||||
N, /* Dimentionality */
|
||||
cp, /* Initial starting point */
|
||||
s, /* Size of initial search area */
|
||||
0.00000001, /* Tollerance of error change to stop on */
|
||||
1000, /* Maximum iterations allowed */
|
||||
fcn, /* Error function to evaluate */
|
||||
dfcn, /* Partial derivative function */
|
||||
NULL /* Opaque data needed by function */
|
||||
);
|
||||
|
||||
|
||||
fprintf(stdout,"Using partial derivative:\n");
|
||||
fprintf(stdout,"Status = %d, final approximate solution err = %f:\n",rc,err);
|
||||
for (j = 0; j < N; j++) {
|
||||
fprintf(stdout,"cp[%d] = %e, expect %e\n",j,cp[j],expect[j]);
|
||||
}
|
||||
|
||||
/* ====== test without partial derivative ====== */
|
||||
/* The following starting values provide a rough solution. */
|
||||
for (j = 0; j < N; j++) {
|
||||
cp[j] = -1.f;
|
||||
s[j] = 0.9;
|
||||
}
|
||||
|
||||
nprint = 0;
|
||||
|
||||
/* Set tol to the square root of the machine precision. */
|
||||
/* Unless high precision solutions are required, */
|
||||
/* this is the recommended setting. */
|
||||
|
||||
rc = varmet(
|
||||
&err,
|
||||
N, /* Dimentionality */
|
||||
cp, /* Initial starting point */
|
||||
s, /* Size of initial search area */
|
||||
0.00000001, /* Tollerance of error change to stop on */
|
||||
1000, /* Maximum iterations allowed */
|
||||
fcn, /* Error function to evaluate */
|
||||
NULL, /* No partial derivative function */
|
||||
NULL /* Opaque data needed by function */
|
||||
);
|
||||
|
||||
|
||||
fprintf(stdout,"\nNot using partial derivative:\n");
|
||||
fprintf(stdout,"Status = %d, final approximate solution err = %f:\n",rc,err);
|
||||
for (j = 0; j < N; j++) {
|
||||
fprintf(stdout,"cp[%d] = %e, expect %e\n",j,cp[j],expect[j]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} /* main() */
|
||||
|
||||
/* Function being minimized */
|
||||
double fcn( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[] /* Multivriate input value */
|
||||
) {
|
||||
double err, tt;
|
||||
double temp, temp1, temp2;
|
||||
int k;
|
||||
|
||||
/* Function Body */
|
||||
err = 0.0;
|
||||
for (k = 0; k < N; ++k) {
|
||||
temp = (3.0 - 2.0 * tp[k]) * tp[k];
|
||||
temp1 = 0.0;
|
||||
if (k != 0) {
|
||||
temp1 = tp[k-1];
|
||||
}
|
||||
temp2 = 0.0;
|
||||
if (k != ((N)-1))
|
||||
temp2 = tp[k+1];
|
||||
tt = temp - temp1 - 2.0 * temp2 + 1.0;
|
||||
err += tt * tt;
|
||||
}
|
||||
err = sqrt(err);
|
||||
//printf("Returning %16.14f\n",err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
/* Compute aprox. forward difference */
|
||||
|
||||
#define JEPS 1.0e-8 /* Aprox. sqrt of machine precision */
|
||||
|
||||
static double dfcn(
|
||||
void *fdata,
|
||||
double dp[],
|
||||
double tp[]
|
||||
) {
|
||||
int i;
|
||||
double d, h, temp;
|
||||
|
||||
d = fcn(fdata, tp);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
temp = tp[i];
|
||||
|
||||
h = JEPS * fabs(temp);
|
||||
if (h == 0.0)
|
||||
h = JEPS;
|
||||
tp[i] = temp + h; /* Add delta */
|
||||
h = tp[i] - temp; /* Actual delta with fp precision limits */
|
||||
|
||||
dp[i] = (fcn(fdata, tp) - d)/h;
|
||||
|
||||
tp[i] = temp; /* Restore value */
|
||||
}
|
||||
|
||||
// return DFUNC_NRV;
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
428
Argyll_V3.5.0/numlib/ui.c
Normal file
428
Argyll_V3.5.0/numlib/ui.c
Normal file
@@ -0,0 +1,428 @@
|
||||
|
||||
|
||||
#define __UI_C__
|
||||
|
||||
/*
|
||||
* Do OS specific setup for using UI windows.
|
||||
*
|
||||
* Copyright 2014 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*
|
||||
* Typically we need to set things up and then call the
|
||||
* "normal" main, called "uimain" in ArgyllCMS utils,
|
||||
* created by ui.h #defining main to uimain.
|
||||
*/
|
||||
|
||||
#ifdef UNIX
|
||||
|
||||
#if __APPLE__
|
||||
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||
/* OS X */
|
||||
|
||||
/*
|
||||
|
||||
OS X is dumb in relying on an event loop that
|
||||
_has_ to be in the main thread to operate.
|
||||
Since we can invoke UI operations from any thread,
|
||||
the way we work around this is to intercept the
|
||||
main thread, spawn a secondary thread to run the
|
||||
application main(), and then do nothing but
|
||||
service the events in the main thread.
|
||||
We can also pass functions back to be run in the main thread,
|
||||
and wait for events in the main thread to be processed.
|
||||
|
||||
Note though that Cocoa has poor thread safety :-
|
||||
ie. NSRunLoop can't be used to access events - use CFRunLoop
|
||||
|
||||
Should bracket all drawing code between the lockFocusIfCanDraw and
|
||||
unlockFocus methods of NSView ? Or is this only a problem if
|
||||
different threads try and draw to the same window ?
|
||||
|
||||
*/
|
||||
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <pthread.h>
|
||||
# include "ui.h"
|
||||
|
||||
# include <Foundation/Foundation.h>
|
||||
# include <AppKit/AppKit.h>
|
||||
|
||||
/* (Duplicate declaration from numsup.h) */
|
||||
|
||||
/* Tell App Nap that this is user initiated */
|
||||
void osx_userinitiated_start();
|
||||
|
||||
/* Done with user initiated */
|
||||
void osx_userinitiated_end();
|
||||
|
||||
/* This is a mechanism to force libui to link */
|
||||
int ui_initialized = 0;
|
||||
|
||||
static int g_argc;
|
||||
static char **g_argv;
|
||||
|
||||
pthread_t ui_thid = 0; /* Thread ID of main thread running io run loop */
|
||||
pthread_t ui_main_thid = 0; /* Thread ID of thread running application main() */
|
||||
|
||||
static pthread_mutex_t ui_lock1, ui_lock2; /* Protect wait code */
|
||||
static pthread_cond_t ui_cond1, ui_cond2; /* Signal to waiting thread */
|
||||
static int ui_event1 = 0, ui_event2 = 0; /* Sync event was received */
|
||||
|
||||
extern int uimain(int argc, char *argv[]);
|
||||
|
||||
/* Thread that calls the real application main() */
|
||||
static void *callMain(void *p) {
|
||||
int rv;
|
||||
|
||||
ui_main_thid = pthread_self();
|
||||
|
||||
NSAutoreleasePool *tpool = [NSAutoreleasePool new];
|
||||
|
||||
/* Turn App Nap off */
|
||||
osx_userinitiated_start();
|
||||
|
||||
/* Should we catch and report exceptions ? */
|
||||
|
||||
rv = uimain(g_argc, g_argv);
|
||||
|
||||
/* Restore App Nap state */
|
||||
osx_userinitiated_end();
|
||||
|
||||
[tpool release];
|
||||
|
||||
/* Cleanup, since main thread won't return */
|
||||
pthread_cond_destroy(&ui_cond1);
|
||||
pthread_cond_destroy(&ui_cond2);
|
||||
pthread_mutex_destroy(&ui_lock1);
|
||||
pthread_mutex_destroy(&ui_lock2);
|
||||
|
||||
exit(rv);
|
||||
}
|
||||
|
||||
/* Dumy method for NSThread to start */
|
||||
@interface MainClass : NSObject
|
||||
+(void)dummyfunc:(id)param;
|
||||
@end
|
||||
|
||||
@implementation MainClass
|
||||
+(void)dummyfunc:(id)param{
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
pthread_mutex_init(&ui_lock1, NULL);
|
||||
pthread_mutex_init(&ui_lock2, NULL);
|
||||
pthread_cond_init(&ui_cond1, NULL);
|
||||
pthread_cond_init(&ui_cond2, NULL);
|
||||
|
||||
ui_thid = pthread_self();
|
||||
|
||||
/* Create an NSApp */
|
||||
static NSAutoreleasePool *pool = nil;
|
||||
|
||||
pool = [NSAutoreleasePool new];
|
||||
|
||||
[NSApplication sharedApplication]; /* Creates NSApp */
|
||||
|
||||
[NSApp finishLaunching];
|
||||
|
||||
/* We need to create at least one NSThread to tell Cocoa that we are using */
|
||||
/* threads, and to protect Cococa objects. (We don't actually have to start the thread.) */
|
||||
[NSThread detachNewThreadSelector:@selector(dummyfunc:) toTarget:[MainClass class] withObject:nil];
|
||||
|
||||
/* Call the real main() in another thread */
|
||||
int rv;
|
||||
pthread_attr_t stackSzAtrbt;
|
||||
pthread_t thid;
|
||||
g_argc = argc;
|
||||
g_argv = argv;
|
||||
|
||||
ui_initialized = 1;
|
||||
|
||||
/* Default stack size is 512K - this is a bit small - raise it */
|
||||
if ((rv = pthread_attr_init(&stackSzAtrbt)) != 0
|
||||
|| (rv = pthread_attr_setstacksize(&stackSzAtrbt, 8 * 1024 * 1024)) != 0) {
|
||||
fprintf(stderr,"ui: thread_attr_setstacksize failed with %d\n",rv);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((rv = pthread_create(&thid, &stackSzAtrbt, callMain, (void *)NULL)) != 0) {
|
||||
fprintf(stderr,"ui: pthread_create failed with %d\n",rv);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Service the run queue */
|
||||
{
|
||||
NSEvent *event;
|
||||
NSDate *to;
|
||||
|
||||
/* Process events, looking for application events. */
|
||||
for (;;) {
|
||||
/* Hmm. Assume to is autorelease */
|
||||
to = [NSDate dateWithTimeIntervalSinceNow:1.0];
|
||||
/* Hmm. Assume event is autorelease */
|
||||
if ((event = [NSApp nextEventMatchingMask:NSAnyEventMask
|
||||
untilDate:to inMode:NSDefaultRunLoopMode dequeue:YES]) != nil) {
|
||||
|
||||
/* call function message */
|
||||
if ([event type] == NSApplicationDefined
|
||||
&& [event subtype] == 1) {
|
||||
void *cntx = (void *)[event data1];
|
||||
void (*function)(void *cntx) = (void (*)(void *)) [event data2];
|
||||
|
||||
function(cntx);
|
||||
|
||||
pthread_mutex_lock(&ui_lock1);
|
||||
ui_event1 = 1;
|
||||
pthread_cond_signal(&ui_cond1);
|
||||
pthread_mutex_unlock(&ui_lock1);
|
||||
[event release];
|
||||
|
||||
/* event flush message */
|
||||
} else if ([event type] == NSApplicationDefined
|
||||
&& [event subtype] == 2) {
|
||||
pthread_mutex_lock(&ui_lock2);
|
||||
ui_event2 = 1;
|
||||
pthread_cond_signal(&ui_cond2);
|
||||
pthread_mutex_unlock(&ui_lock2);
|
||||
[event release];
|
||||
|
||||
/* Everything else */
|
||||
} else {
|
||||
[NSApp sendEvent:event];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Note that we don't actually clean this up on exit - */
|
||||
/* possibly we can't. */
|
||||
// [NSApp terminate: nil];
|
||||
}
|
||||
|
||||
/* Call this if we decide we are actually going to display something in the GUI. */
|
||||
/* We switch to "interact with the Dock" mode. */
|
||||
void ui_UsingGUI() {
|
||||
static int attached = 0;
|
||||
ProcessSerialNumber psn = { 0, 0 };
|
||||
|
||||
if (!attached) {
|
||||
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
|
||||
/* Make the application appear in the Dock, and interact with the desktop properly. */
|
||||
/* (Unbundled applications default to NSApplicationActivationPolicyProhibited) */
|
||||
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
||||
|
||||
#else
|
||||
# if MAC_OS_X_VERSION_MIN_REQUIRED >= 1030
|
||||
/* Make the application appear in the Dock, and interact with the desktop properly. */
|
||||
/* We don't need resources or a bundle if we do this. */
|
||||
if (GetCurrentProcess(&psn) == noErr) {
|
||||
OSStatus stat;
|
||||
if (psn.lowLongOfPSN != 0 && (stat = TransformProcessType(&psn,
|
||||
kProcessTransformToForegroundApplication)) != noErr) {
|
||||
fprintf(stderr,"TransformProcess failed with code %d\n",(int)stat);
|
||||
|
||||
/* An older trick uses an undocumented API:
|
||||
CPSEnableForegroundOperation(&processSerialNum, 0, 0, 0, 0);
|
||||
*/
|
||||
} else {
|
||||
// fprintf(stderr,"TransformProcess succeeded\n");
|
||||
}
|
||||
}
|
||||
# endif /* OS X 10.3 */
|
||||
#endif /* !OS X 10.6 */
|
||||
|
||||
/* We seem to need this, because otherwise we don't get focus automatically */
|
||||
[NSApp activateIgnoringOtherApps: YES];
|
||||
|
||||
attached = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Run a function in the main thread and return when it is complete. */
|
||||
/* (It's up to the function to record it's result status in its context) */
|
||||
void ui_runInMainThreadAndWait(void *cntx, void (*function)(void *cntx)) {
|
||||
|
||||
NSEvent *event;
|
||||
NSPoint point = { 0.0, 0.0 };
|
||||
int rv;
|
||||
|
||||
pthread_mutex_lock(&ui_lock1);
|
||||
ui_event1 = 0;
|
||||
|
||||
event = [NSEvent otherEventWithType:NSApplicationDefined
|
||||
location:point
|
||||
modifierFlags:0
|
||||
timestamp:0.0
|
||||
windowNumber:0
|
||||
context:nil
|
||||
subtype:1
|
||||
data1:(long)cntx /* long same size as * */
|
||||
data2:(long)function];
|
||||
[NSApp postEvent:event atStart:NO];
|
||||
|
||||
// unlock and wait for signal
|
||||
for (;;) { /* Ignore spurious wakeups */
|
||||
if ((rv = pthread_cond_wait(&ui_cond1, &ui_lock1)) != 0) {
|
||||
break; // Hmm.
|
||||
}
|
||||
if (ui_event1) /* Got what we were waiting for */
|
||||
break;
|
||||
}
|
||||
pthread_mutex_unlock(&ui_lock1);
|
||||
}
|
||||
|
||||
|
||||
/* We are about to change the UI */
|
||||
void ui_aboutToWait() {
|
||||
|
||||
pthread_mutex_lock(&ui_lock2);
|
||||
ui_event2 = 0;
|
||||
}
|
||||
|
||||
/* Wait until we are sure our UI change is complete, */
|
||||
/* because our event has trickled through. */
|
||||
void ui_waitForEvents() {
|
||||
NSEvent *event;
|
||||
NSPoint point = { 0.0, 0.0 };
|
||||
int rv;
|
||||
|
||||
event = [NSEvent otherEventWithType:NSApplicationDefined
|
||||
location:point
|
||||
modifierFlags:0
|
||||
timestamp:0.0
|
||||
windowNumber:0
|
||||
context:nil
|
||||
subtype:2
|
||||
data1:0
|
||||
data2:0];
|
||||
[NSApp postEvent:event atStart:NO];
|
||||
|
||||
// unlock and wait for signal
|
||||
for (;;) { /* Ignore spurious wakeups */
|
||||
if ((rv = pthread_cond_wait(&ui_cond2, &ui_lock2)) != 0) {
|
||||
break; // Hmm.
|
||||
}
|
||||
if (ui_event2) /* Got what we were waiting for */
|
||||
break;
|
||||
}
|
||||
pthread_mutex_unlock(&ui_lock2);
|
||||
}
|
||||
|
||||
#else /* !APPLE */
|
||||
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||
/* UNIX */
|
||||
|
||||
/* This is a mechanism to force libui to link */
|
||||
int ui_initialized = 1; /* Nothing needs initializing */
|
||||
|
||||
/* Call this if we decide we are actually going to display */
|
||||
/* something in the GUI */
|
||||
void ui_UsingGUI() {
|
||||
}
|
||||
|
||||
#endif /* !APPLE */
|
||||
|
||||
#endif /* UNIX */
|
||||
|
||||
#ifdef NT
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||
/* NT */
|
||||
|
||||
#ifdef NEVER
|
||||
|
||||
/*
|
||||
This code isn't generally usable for console programs, because
|
||||
Microsoft changed the default behaviour of cmd.exe in Vista to
|
||||
not wait for a gui mode .exe to terminate. A non-gui .exe always
|
||||
pops a new console if run from explorer.
|
||||
|
||||
So this approach is still viable for primarily gui programs
|
||||
to output debug to stdout, but a different shell or "cmd.exe /E:OFF"
|
||||
is needed to interact with it via the shell.
|
||||
|
||||
Alternatives are messy - mark the exe as console and
|
||||
have it shut down the popup console in gui mode (looks
|
||||
ugly), or have a stub app.bat that invokes the
|
||||
real .exe, forcing cmd.exe to operate in the mode where
|
||||
it waits for execution to finish.
|
||||
*/
|
||||
|
||||
/* This is a mechanism to force libui to link */
|
||||
int ui_initialized = 0;
|
||||
|
||||
/*
|
||||
|
||||
On MSWin we can rely on WinMain to be called instead of
|
||||
main(), so that we can re-attache the stdio so that the
|
||||
resulting exe works the same when involked either from
|
||||
a shell, or directly from explorer.
|
||||
|
||||
*/
|
||||
|
||||
/* May have to add link flag -Wl,-subsystem,windows */
|
||||
/* since MingW is stupid about noticing WinMain or pragma */
|
||||
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
|
||||
# pragma comment( linker, "/subsystem:windows" )
|
||||
//# pragma comment( linker, "/subsystem:console /ENTRY:WinMainCRTStartup" )
|
||||
//# pragma comment( linker, "/subsystem:windows /ENTRY:mainCRTStartup" )
|
||||
//# pragma comment( linker, "/subsystem:windows /ENTRY:WinMainCRTStartup" )
|
||||
|
||||
extern int uimain(int argc, char *argv[]);
|
||||
|
||||
APIENTRY WinMain(
|
||||
HINSTANCE hInstance,
|
||||
HINSTANCE hPrevInstance,
|
||||
LPSTR lpCmdLine,
|
||||
int nCmdShow
|
||||
) {
|
||||
{ /* Only works on >= XP though */
|
||||
BOOL (WINAPI *AttachConsole)(DWORD dwProcessId);
|
||||
|
||||
*(FARPROC *)&AttachConsole =
|
||||
GetProcAddress(LoadLibraryA("kernel32.dll"), "AttachConsole");
|
||||
|
||||
if (AttachConsole != NULL && AttachConsole(((DWORD)-1)))
|
||||
{
|
||||
if (_fileno(stdout) < 0)
|
||||
freopen("CONOUT$","wb",stdout);
|
||||
if (_fileno(stderr) < 0)
|
||||
freopen("CONOUT$","wb",stderr);
|
||||
if (_fileno(stdin) < 0)
|
||||
freopen("CONIN$","rb",stdin);
|
||||
#ifdef __cplusplus
|
||||
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well
|
||||
std::ios::sync_with_stdio();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
ui_initialized = 1;
|
||||
|
||||
return uimain(__argc, __argv);
|
||||
}
|
||||
|
||||
#else /* !NEVER */
|
||||
|
||||
/* This is a mechanism to force libui to link */
|
||||
int ui_initialized = 1; /* Nothing needs initializing */
|
||||
|
||||
#endif /* !NEVER */
|
||||
|
||||
/* Call this if we decide we are actually going to display */
|
||||
/* something in the GUI */
|
||||
void ui_UsingGUI() {
|
||||
}
|
||||
|
||||
#endif /* NT */
|
||||
63
Argyll_V3.5.0/numlib/ui.h
Normal file
63
Argyll_V3.5.0/numlib/ui.h
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
#ifndef __UI_H__
|
||||
|
||||
/*
|
||||
* Do OS specific setup for using UI windows.
|
||||
* Include this only in file with main().
|
||||
*
|
||||
* Copyright 2014 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*
|
||||
* Typically we need to set things up and then call the
|
||||
* "normal" main, called "uimain" in ArgyllCMS utils.
|
||||
*/
|
||||
|
||||
/* Because linkers are dumb (they pass over a library containing main() */
|
||||
/* and then complain that there is no entry point!), we force linking */
|
||||
/* of libui if ui.h gets #included. */
|
||||
extern int ui_initialized;
|
||||
static int *pui_initialized = &ui_initialized;
|
||||
|
||||
/* Call this if we decide we are actually going to display */
|
||||
/* something in the GUI */
|
||||
void ui_UsingGUI();
|
||||
|
||||
#ifdef UNIX
|
||||
# ifdef __APPLE__
|
||||
|
||||
extern pthread_t ui_thid; /* Thread ID of main thread running io run loop */
|
||||
extern pthread_t ui_main_thid; /* Thread ID of thread running application main() */
|
||||
|
||||
/* Run a function in the main thread and return when it is complete */
|
||||
void ui_runInMainThreadAndWait(void *cntx, void (*function)(void *context));
|
||||
|
||||
/* We are about to change the UI */
|
||||
void ui_aboutToWait();
|
||||
|
||||
/* Wait until we are sure our UI change is complete */
|
||||
void ui_waitForEvents();
|
||||
|
||||
#ifndef __UI_C__
|
||||
# define main uimain
|
||||
#endif
|
||||
|
||||
#else /* Linux etc. */
|
||||
|
||||
#endif /* Linux etc. */
|
||||
#endif /* UNIX */
|
||||
|
||||
#ifdef NT
|
||||
|
||||
#ifdef NEVER /* Not practical - see ui.c for full explanation */
|
||||
#ifndef __UI_C__
|
||||
# define main uimain
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#define __UI_H__
|
||||
#endif /* __UI_H__ */
|
||||
485
Argyll_V3.5.0/numlib/varmet.c
Normal file
485
Argyll_V3.5.0/numlib/varmet.c
Normal file
@@ -0,0 +1,485 @@
|
||||
|
||||
/* Multi-dimentional minizer using Variable Metric method */
|
||||
/* This is good for smoother, well behaved functions. */
|
||||
|
||||
/* Code is an original expression of the algorithms decsribed in */
|
||||
/* "Numerical Recipes in C", by W.H.Press, B.P.Flannery, */
|
||||
/* S.A.Teukolsky & W.T.Vetterling. */
|
||||
|
||||
/*
|
||||
* Copyright 2000, 2006, 2007, 2017 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
/* Note that all arrays are indexed from 0 */
|
||||
|
||||
#include "numsup.h"
|
||||
#include "powell.h"
|
||||
#include "varmet.h"
|
||||
|
||||
#undef VDEBUG /* Varmet debug */
|
||||
#undef LDEBUG /* Line min debug */
|
||||
#undef PLOTL /* Plot line search response */
|
||||
|
||||
#if defined(VDEBUG)
|
||||
# undef VDBG
|
||||
# define VDBG(xxx) printf xxx ;
|
||||
#else
|
||||
# undef VDBG
|
||||
# define VDBG(xxx)
|
||||
#endif
|
||||
|
||||
#if defined(LDEBUG)
|
||||
# undef LDBG
|
||||
# define LDBG(xxx) printf xxx ;
|
||||
#else
|
||||
# undef LDBG
|
||||
# define LDBG(xxx)
|
||||
#endif
|
||||
|
||||
|
||||
#define FMAX(A,B) ((A) > (B) ? (A) : (B))
|
||||
#define EPS 1.0e-10 /* Machine precision. */
|
||||
#define XTOL (4 * EPS) /* X value stop value */
|
||||
#define MAXLEN 100.0 /* Maximum step length */
|
||||
|
||||
void linesearch(int di, double cpold[], double fpold, double g[], double p[], double cpnew[],
|
||||
double *pfp, double maxstep, double (*func)(void *fdata, double tp[]), void *fdata);
|
||||
|
||||
static double fake_dfunc(void *fdata, double (*func)(void *fdata, double tp[]), int di,
|
||||
double dp[], double tp[]);
|
||||
|
||||
/* return 0 on sucess, 1 on failure due to excessive itterions */
|
||||
/* Result will be in cp */
|
||||
/* Note that we could use gradient in line minimiser, */
|
||||
/* but haven't bothered yet. */
|
||||
int varmet(
|
||||
double *rv, /* If not NULL, return the residual error */
|
||||
int di, /* Dimentionality */
|
||||
double cp[], /* Initial starting point */
|
||||
double s[], /* Size of initial search area */
|
||||
double ftol, /* relative function value change tolleranc to stop on */
|
||||
//double xtol, /* relative cp change tolleranc to stop on */
|
||||
//double gtol, /* Gradient value tollerance to stop on */
|
||||
int maxit, /* Maximum iterations allowed */
|
||||
double (*func)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
double (*dfunc)(void *fdata, double dp[], double tp[]), /* Gradient function to evaluate */
|
||||
void *fdata /* Opaque data needed by function */
|
||||
) {
|
||||
int iter, fails;
|
||||
double fp, sumsq, maxstep;
|
||||
double *sdir, sumsdir; /* Search direction */
|
||||
double *dp, *lastdp;
|
||||
double **hessian; /* Hessian matrix */
|
||||
double *hlastdp; /* Hessian times lastdp */
|
||||
double *cpnew; /* new cp value from linemin */
|
||||
double *dels; /* Delta's from each step */
|
||||
double test;
|
||||
|
||||
double den, fac, fad, fae;
|
||||
double sumdg;
|
||||
int i, j;
|
||||
|
||||
double xtol = XTOL; /* relative cp change tolleranc to stop on */
|
||||
|
||||
double pfp = 1e38, stopth, curdel;
|
||||
|
||||
sdir = dvector(0, di-1);
|
||||
dp = dvector(0, di-1);
|
||||
lastdp = dvector(0, di-1);
|
||||
hessian = dmatrix(0, di-1, 0, di-1);
|
||||
hlastdp = dvector(0, di-1);
|
||||
cpnew = dvector(0, di-1);
|
||||
dels = dvector(0, di-1);
|
||||
|
||||
VDBG((" doing partial deriv.\n"));
|
||||
if (dfunc != NULL)
|
||||
fp = (*dfunc)(fdata, dp, cp);
|
||||
else
|
||||
fp = fake_dfunc(fdata, func, di, dp, cp);
|
||||
|
||||
if (fp == DFUNC_NRV)
|
||||
fp = (*func)(fdata, cp);
|
||||
|
||||
/* Initial line direction and pde squared */
|
||||
sumsq = 0.0;
|
||||
for (i = 0; i < di ;i++) {
|
||||
sdir[i] = -dp[i];
|
||||
sumsq += cp[i] * cp[i];
|
||||
}
|
||||
|
||||
VDBG((" initial fp %f dp %s\n", fp, debPdv(di, dp)));
|
||||
|
||||
/* Initialize inverse Hessian to unity */
|
||||
for (i = 0; i < di ;i++) {
|
||||
for (j = 0; j < di ; j++) {
|
||||
if (i == j)
|
||||
hessian[i][j] = 1.0;
|
||||
else
|
||||
hessian[i][j] = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Maximum line search step size */
|
||||
maxstep = MAXLEN * FMAX(sqrt(sumsq), (double)di);
|
||||
VDBG((" maxstep %g\n", maxstep));
|
||||
|
||||
/* Until we give up */
|
||||
for (fails = iter = 0; fails < di && iter < maxit; iter++) {
|
||||
|
||||
/* Search in direction sdir */
|
||||
VDBG((" it %d: doing line search\n",iter));
|
||||
linesearch(di, cp, fp, dp, sdir, cpnew, &fp, maxstep, func, fdata);
|
||||
|
||||
for (i = 0; i < di; i++) {
|
||||
sdir[i] = cpnew[i] - cp[i]; /* Compare search direction, */
|
||||
cp[i] = cpnew[i]; /* and the current value. */
|
||||
}
|
||||
|
||||
/* Check the relative change in x, and stop if it's below */
|
||||
/* the machine precision */
|
||||
for (test = 0.0, i = 0 ; i < di; i++) {
|
||||
double tt = fabs(sdir[i]) / FMAX(fabs(cp[i]), 1.0);
|
||||
if (tt > test)
|
||||
test = tt;
|
||||
}
|
||||
|
||||
if (test < xtol) {
|
||||
// VDBG((" converged because test %g < xtol %g\n",test,xtol));
|
||||
VDBG((" failed to make progres (iter %d < maxit %d): test %g < xtol %g\n",iter,maxit,test,xtol));
|
||||
|
||||
/* Try a move in the dp direction */
|
||||
sumsq = 0.0;
|
||||
for (i = 0; i < di ;i++) {
|
||||
sdir[i] = -dp[i];
|
||||
sumsq += cp[i] * cp[i];
|
||||
}
|
||||
|
||||
maxstep = MAXLEN * FMAX(sqrt(sumsq), (double)di);
|
||||
|
||||
VDBG((" retrying\n"));
|
||||
|
||||
fails++;
|
||||
|
||||
continue;
|
||||
// break;
|
||||
}
|
||||
fails = 0;
|
||||
VDBG((" not converged because test %g >= xtol %g\n",test,xtol));
|
||||
|
||||
/* Check relative change in function value */
|
||||
curdel = fabs(pfp - fp);
|
||||
dels[iter % di] = curdel;
|
||||
|
||||
if (iter > di) { /* Enough to compute a moving average del */
|
||||
double avgdel;
|
||||
|
||||
for (avgdel = 0.0, i = 0 ; i < di; i++)
|
||||
avgdel += dels[i] * dels[i];
|
||||
avgdel = sqrt(avgdel/(double)di); /* Moving RMS average */
|
||||
|
||||
stopth = ftol * 0.5 * (fabs(pfp) + fabs(fp) + DBL_EPSILON);
|
||||
|
||||
/* If average relative change is below threshold */
|
||||
if (avgdel <= stopth) {
|
||||
VDBG(("Reached stop tollerance avgdel %g <= stopth %g\n",avgdel,stopth))
|
||||
break;
|
||||
} else {
|
||||
VDBG(("Not stopping because avgdel %g > stopth %g\n",avgdel,stopth))
|
||||
}
|
||||
}
|
||||
|
||||
pfp = fp;
|
||||
|
||||
for (i = 0; i < di; i++) /* Save previous partial deriv. */
|
||||
lastdp[i] = dp[i];
|
||||
|
||||
VDBG((" it %d: doing partial deriv\n",iter));
|
||||
if (dfunc != NULL)
|
||||
(*dfunc)(fdata, dp, cp); /* and get the new gradient. */
|
||||
else
|
||||
fake_dfunc(fdata, func, di, dp, cp);
|
||||
|
||||
#ifdef NEVER /* This doesn't seem useful */
|
||||
/* Check the largest relative gradient, and stop if it's below */
|
||||
/* our stopping tollerance */
|
||||
den = FMAX(fp, 1.0);
|
||||
for (test = 0.0, i = 0; i < di; i++) {
|
||||
double tt = fabs(dp[i]) * FMAX(fabs(cp[i]),1.0) / den;
|
||||
if (tt > test)
|
||||
test = tt;
|
||||
}
|
||||
|
||||
if (test < gtol) {
|
||||
VDBG((" converged because test %g < ftol %g\n",test,gtol));
|
||||
break;
|
||||
}
|
||||
VDBG((" Not converged because test %g >= ftol %g\n",test,gtol));
|
||||
#endif
|
||||
|
||||
for (i = 0 ; i < di; i++)
|
||||
lastdp[i] = dp[i] - lastdp[i]; /* Compute diference of gradients, */
|
||||
|
||||
for (i = 0; i < di; i++) { /* and difernce times current matrix. */
|
||||
hlastdp[i] = 0.0;
|
||||
for (j = 0; j < di; j++)
|
||||
hlastdp[i] += hessian[i][j] * lastdp[j];
|
||||
}
|
||||
|
||||
/* Calculate dot products for the denominator */
|
||||
fac = fae = sumdg = sumsdir = 0.0;
|
||||
for (i = 0; i < di; i++) {
|
||||
fac += lastdp[i] * sdir[i];
|
||||
fae += lastdp[i] * hlastdp[i];
|
||||
sumdg += lastdp[i] * lastdp[i];
|
||||
sumsdir += sdir[i] * sdir[i];
|
||||
}
|
||||
if (fac > sqrt(EPS * sumdg * sumsdir)) { /* Skip update if fac not sufficiently posive */
|
||||
fac = 1.0/fac;
|
||||
fad = 1.0/fae;
|
||||
/* The vector that makes BFGS different from DFP: */
|
||||
for (i = 0; i < di;i++)
|
||||
lastdp[i] = fac * sdir[i] - fad * hlastdp[i];
|
||||
for (i = 0; i < di;i++) { /* The BFGS updating formula: */
|
||||
for (j = i; j < di; j++) {
|
||||
hessian[i][j] += fac * sdir[i] * sdir[j]
|
||||
- fad * hlastdp[i] * hlastdp[j] + fae * lastdp[i] * lastdp[j];
|
||||
hessian[j][i] = hessian[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = 0; i < di; i++) { /* Now calculate the next direction to go, */
|
||||
sdir[i] = 0.0;
|
||||
for (j = 0; j < di; j++)
|
||||
sdir[i] -= hessian[i][j] * dp[j];
|
||||
}
|
||||
}
|
||||
|
||||
VDBG((" Returning %g\n",fp));
|
||||
|
||||
free_dvector(dels, 0, di-1);
|
||||
free_dvector(cpnew, 0, di-1);
|
||||
free_dvector(hlastdp, 0, di-1);
|
||||
free_dmatrix(hessian, 0, di-1, 0, di-1);
|
||||
free_dvector(lastdp, 0, di-1);
|
||||
free_dvector(dp, 0, di-1);
|
||||
free_dvector(sdir, 0, di-1);
|
||||
if (rv != NULL)
|
||||
*rv = fp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* -------------------------------------- */
|
||||
|
||||
/* Numerical approximation to gradient */
|
||||
static double fake_dfunc(void *fdata, double (*func)(void *fdata, double tp[]), int di,
|
||||
double dp[], double tp[]) {
|
||||
double rv, h, temp;
|
||||
int i;
|
||||
|
||||
rv = (*func)(fdata, tp);
|
||||
|
||||
for (i = 0; i < di; i++) {
|
||||
double drv;
|
||||
|
||||
temp = tp[i];
|
||||
|
||||
h = 1e-8 * fabs(temp);
|
||||
if (h == 0.0)
|
||||
h = 1e-8;
|
||||
tp[i] = temp + h; /* Add delta */
|
||||
h = tp[i] - temp; /* Actual delta with fp precision limits */
|
||||
drv = (*func)(fdata, tp);
|
||||
dp[i] = (drv - rv)/h;
|
||||
tp[i] = temp; /* Restore value */
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* -------------------------------------- */
|
||||
|
||||
#ifdef PLOTL /* Plot the step direction */
|
||||
# include "aconfig.h"
|
||||
# include "plot.h"
|
||||
#endif
|
||||
|
||||
#define ALPHA 1.0e-4 /* Ensures sufficient decrease in function value. */
|
||||
#define LXTOL 1.0e-7 /* [1e-7] Min step length termination criteria on linesearch. */
|
||||
|
||||
void linesearch(
|
||||
int di,
|
||||
double cpold[], /* Incoming (old) current value */
|
||||
double fpold, /* Incomming current function value */
|
||||
double dp[], /* Partial derivative */
|
||||
double sdir[], /* Search direction */
|
||||
double cpnew[], /* Return (new) value */
|
||||
double *pfp, /* Return objective value */
|
||||
double maxstep,
|
||||
double (*func)(void *fdata, double tp[]),
|
||||
void *fdata
|
||||
) {
|
||||
double sum, slope;
|
||||
double slen, slen_2 = 0.0, slen_min;
|
||||
double test, fp_2 = 0.0;
|
||||
int i;
|
||||
|
||||
for (sum = 0.0, i = 0; i < di; i++)
|
||||
sum += sdir[i] * sdir[i];
|
||||
sum = sqrt(sum);
|
||||
|
||||
LDBG((" fpold %f, sdir length %f\n",fpold,sum));
|
||||
|
||||
if (sum > maxstep) {
|
||||
LDBG((" sdir scaled to maxstep %f\n",maxstep));
|
||||
for (i = 0; i < di; i++)
|
||||
sdir[i] *= maxstep/sum; /* Scale if attempted step is too big. */
|
||||
}
|
||||
for (slope = 0.0, i = 0; i < di; i++)
|
||||
slope += dp[i] * sdir[i];
|
||||
|
||||
LDBG((" dp . sdir = slope = %f\n",slope));
|
||||
|
||||
if (slope >= 0.0) { /* Hmm. */
|
||||
warning("varmet:linesearch: slope is >= 0");
|
||||
#ifdef PLOTL
|
||||
goto do_return;
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Compute largest ratio of sdir component to max(current value,1.0) */
|
||||
for (test = 0.0, i = 0;i < di; i++) {
|
||||
double tt = fabs(sdir[i])/FMAX(fabs(cpold[i]), 1.0);
|
||||
if (tt > test)
|
||||
test = tt;
|
||||
}
|
||||
|
||||
slen_min = LXTOL/test; /* min. step length termination criteria */
|
||||
slen = 1.0; /* Try full step */
|
||||
|
||||
/* Start of iteration loop. */
|
||||
for (;;) {
|
||||
double slen_t;
|
||||
|
||||
LDBG((" top of loop: slen %f slen_min %g slen_2 %f\n",slen, slen_min, slen_2));
|
||||
|
||||
/* trial point */
|
||||
for (i = 0; i < di;i++)
|
||||
cpnew[i] = cpold[i] + slen * sdir[i];
|
||||
|
||||
*pfp = (*func)(fdata, cpnew);
|
||||
LDBG((" func %f, fpold %f %s\n",*pfp, fpold, *pfp < fpold ? "BETTER" : "same/worse"));
|
||||
|
||||
if (slen < slen_min) {
|
||||
LDBG((" return slen %f fp %f because slen %g < slen_min %g (cpold)\n",slen_2, fp_2, slen, slen_min));
|
||||
for (i = 0; i < di; i++)
|
||||
cpnew[i] = cpold[i];
|
||||
*pfp = fp_2;
|
||||
slen = slen_min;
|
||||
|
||||
#ifdef PLOTL
|
||||
goto do_return;
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
|
||||
} else if (*pfp <= (fpold + ALPHA * slen * slope)) {
|
||||
LDBG((" return slen %f fp %f because func %f <= %f = fpold %f + %f * slen %f * slope %f (cpnew)\n",slen, *pfp, *pfp, fpold + ALPHA * slen * slope, fpold, ALPHA, slen, slope));
|
||||
#ifdef PLOTL
|
||||
goto do_return;
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
|
||||
/* Backtracking */
|
||||
} else {
|
||||
LDBG((" slen %g >= slen_min %g\n",slen, slen_min));
|
||||
|
||||
LDBG((" backtracking:\n"));
|
||||
|
||||
/* First time through model as a quadratic */
|
||||
if (slen == 1.0) {
|
||||
slen_t = -slope/(2.0 * (*pfp - fpold - slope));
|
||||
LDBG((" 1: slen_t %f = -slope %f/(2.0 * (*pfp %f - fpold %f - slope %f)\n",slen_t,-slope, *pfp, fpold, slope));
|
||||
|
||||
/* 2nd and subsequent times through model as a cubic */
|
||||
} else {
|
||||
double aa, bb;
|
||||
double rhs_1, rhs_2;
|
||||
|
||||
LDBG((" 2:\n"));
|
||||
|
||||
/* Components of cubic solution */
|
||||
rhs_1 = *pfp - fpold - slen * slope;
|
||||
rhs_2 = fp_2 - fpold - slen_2 * slope;
|
||||
aa = (rhs_1/(slen * slen) - rhs_2/(slen_2 * slen_2))/(slen - slen_2);
|
||||
bb = (-slen_2 * rhs_1/(slen * slen)+slen * rhs_2/(slen_2 * slen_2))/(slen - slen_2);
|
||||
LDBG((" 2: rhs_1 %f rhs_2 %f aa %f bb %f)\n",rhs_1,rhs_2,aa,bb));
|
||||
|
||||
/* Denominator is zero */
|
||||
if (aa == 0.0) {
|
||||
slen_t = -slope/(2.0 * bb);
|
||||
LDBG((" 3: aa == 0, slen_t %f = -slope %f/(2.0 * bb %f)\n",slen_t,-slope, bb));
|
||||
} else {
|
||||
double dd = bb * bb - 3.0 * aa * slope;
|
||||
LDBG((" 2: dd == %f)\n",dd));
|
||||
if (dd < 0.0) {
|
||||
slen_t = 0.5 * slen;
|
||||
LDBG((" 4: dd %f < 0, slen_t %f = 0.5 * slen %f)\n",dd, slen_t, slen));
|
||||
} else if (bb <= 0.0) {
|
||||
slen_t = (-bb + sqrt(dd))/(3.0 * aa);
|
||||
LDBG((" 5: bb %f <= 0.0, slen_t %f = (-bb %f + sqrt(dd %f))/(3.0 * aa %f)\n",bb, slen_t, -bb, dd, aa));
|
||||
} else {
|
||||
slen_t = -slope/(bb + sqrt(dd));
|
||||
LDBG((" 6: bb %f > 0.0, slen_t %f = -slope %f /(bb %f + sqrt(dd %f)\n",bb, slen_t, -slope, bb, dd));
|
||||
}
|
||||
}
|
||||
if (slen_t > 0.5 * slen) {
|
||||
LDBG((" 7: slen_t %f > (0.5 * slen %f = %f)\n",slen_t, slen, 0.5 * slen));
|
||||
slen_t = 0.5 * slen;
|
||||
}
|
||||
}
|
||||
}
|
||||
fp_2 = *pfp;
|
||||
slen_2 = slen;
|
||||
slen = FMAX(slen_t, 0.1 * slen);
|
||||
}
|
||||
|
||||
#ifdef PLOTL /* Plot the step direction */
|
||||
do_return:;
|
||||
{
|
||||
# define RES 51
|
||||
double x1, x2;
|
||||
double xx[RES];
|
||||
double y1[RES];
|
||||
int j;
|
||||
|
||||
/* Plot range */
|
||||
x1 = log10(0.00001);
|
||||
x2 = log10(1.0);
|
||||
|
||||
printf("Computing plot points:\n");
|
||||
for (j = 0; j < RES; j++) {
|
||||
double cp[24];
|
||||
double vv = j/(RES-1.0) * (x2 - x1) + x1;
|
||||
double vl;
|
||||
|
||||
vl = pow(10.0, vv);
|
||||
|
||||
for (i = 0; i < di;i++)
|
||||
cp[i] = cpold[i] + vl * sdir[i];
|
||||
|
||||
xx[j] = vv;
|
||||
y1[j] = (*func)(fdata, cp);
|
||||
}
|
||||
printf(" log10 step = %f:\n",log10(slen));
|
||||
do_plot(xx, y1, NULL, NULL, RES);
|
||||
}
|
||||
#endif /* PLOTL */
|
||||
|
||||
}
|
||||
|
||||
55
Argyll_V3.5.0/numlib/varmet.h
Normal file
55
Argyll_V3.5.0/numlib/varmet.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef VARMET_H
|
||||
#define VARMET_H
|
||||
|
||||
/* Variable Metric multivariate minimiser */
|
||||
|
||||
/*
|
||||
* Copyright 2000, 2007 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Variable Metric Gradient optimiser */
|
||||
/* return 0 on sucess, 1 on failure due to excessive itterations */
|
||||
/* Result will be in cp */
|
||||
int varmet(
|
||||
double *rv, /* If not NULL, return the residual error */
|
||||
int di, /* Dimentionality */
|
||||
double cp[], /* Initial starting point */
|
||||
double s[], /* Size of initial search area */
|
||||
double ftol, /* Tollerance of error change to stop on */
|
||||
int maxit, /* Maximum iterations allowed */
|
||||
double (*func)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
double (*dfunc)(void *fdata, double dp[], double tp[]), /* Gradient function to evaluate */
|
||||
void *fdata /* Opaque data needed by function */
|
||||
);
|
||||
|
||||
/* Example user function declarations */
|
||||
double varmet_funk( /* Return function value */
|
||||
void *fdata, /* Opaque data pointer */
|
||||
double tp[]); /* Multivriate input value */
|
||||
|
||||
/* Line in multi-dimensional space minimiser */
|
||||
double brentnd( /* vector multiplier return value */
|
||||
double ax, /* Minimum of multiplier range */
|
||||
double bx, /* Starting point multiplier of search */
|
||||
double cx, /* Maximum of multiplier range */
|
||||
double ftol, /* Tollerance to stop search */
|
||||
double *xmin, /* Return value of multiplier at minimum */
|
||||
int n, /* Dimensionality */
|
||||
double (*func)(void *fdata, double tp[]), /* Error function to evaluate */
|
||||
void *fdata, /* Opaque data */
|
||||
double pcom[], /* Base vector point */
|
||||
double xicom[]); /* Vector that will be multiplied and added to pcom[] */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* VARMET_H */
|
||||
197
Argyll_V3.5.0/numlib/zbrent.c
Normal file
197
Argyll_V3.5.0/numlib/zbrent.c
Normal file
@@ -0,0 +1,197 @@
|
||||
|
||||
/* 1 dimentional root finding code */
|
||||
/* inspired by the Van Wijngaarden-Dekker-Brent */
|
||||
/* method algorithm presented in */
|
||||
/* "Numerical Recipes in C", by W.H.Press, */
|
||||
/* B.P.Flannery, S.A.Teukolsky & W.T.Vetterling. */
|
||||
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#include "numsup.h"
|
||||
#include "zbrent.h"
|
||||
|
||||
#undef DEBUG
|
||||
|
||||
#define ZBRACK_MAXTRY 40 /* Maximum tries to bracket */
|
||||
#define ZBRACK_GOLD 1.618034 /* Golden ratio */
|
||||
|
||||
/* Bracket search function */
|
||||
/* return 0 on sucess */
|
||||
/* -1 on no range */
|
||||
/* -2 on too many itterations */
|
||||
int zbrac(
|
||||
double *x1p, /* Input and output bracket values */
|
||||
double *x2p, /* Min and Max */
|
||||
double (*func)(void *fdata, double tp), /* function to evaluate */
|
||||
void *fdata /* Opaque data pointer */
|
||||
) {
|
||||
int i;
|
||||
double x1, x2; /* Bracket under consideration */
|
||||
double f1, f2; /* Function values at points x1 and x2 */
|
||||
double gold = ZBRACK_GOLD;
|
||||
|
||||
x1 = *x1p;
|
||||
x2 = *x2p;
|
||||
if (x1 == x2) /* Nowhere to go */
|
||||
return -1;
|
||||
|
||||
f1 = (*func)(fdata, x1); /* Initial function values */
|
||||
f2 = (*func)(fdata, x2);
|
||||
|
||||
for (i = 0; i < ZBRACK_MAXTRY; i++) {
|
||||
if ((f1 * f2) < 0.0) {
|
||||
*x1p = x1;
|
||||
*x2p = x2;
|
||||
return 0; /* If signs are opposite, we're done */
|
||||
}
|
||||
if (fabs(f2) > fabs(f1)) { /* Move smaller in direction away from larger */
|
||||
double tx1, tf1;
|
||||
tx1 = x1 + gold * (x1 - x2);
|
||||
tf1 = (*func)(fdata, tx1);
|
||||
if (tf1 < f1) { /* It improved */
|
||||
x1 = tx1;
|
||||
f1 = tf1;
|
||||
} else {
|
||||
gold *= 0.5;
|
||||
}
|
||||
} else {
|
||||
double tx2, tf2;
|
||||
tx2 = x1 + gold * (x2 - x1);
|
||||
tf2 = (*func)(fdata, tx2);
|
||||
if (tf2 > f2) { /* It improved */
|
||||
x2 = tx2;
|
||||
f2 = tf2;
|
||||
} else {
|
||||
gold *= 0.5;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
#undef ZBRACK_GOLD
|
||||
#undef ZBRACK_MAXTRY
|
||||
|
||||
|
||||
#define ZBRENT_MAXIT 100
|
||||
|
||||
/* Root finder */
|
||||
/* return 0 on sucess */
|
||||
/* -1 on root not bracketed */
|
||||
/* -2 on too many itterations */
|
||||
int zbrent(
|
||||
double *rv, /* Return value */
|
||||
double ax, /* Bracket to search */
|
||||
double bx, /* (Min, Max) */
|
||||
double tol, /* Desired tollerance */
|
||||
double (*func)(void *fdata, double tp), /* function to evaluate */
|
||||
void *fdata /* Opaque data pointer */
|
||||
) {
|
||||
int i;
|
||||
double cx; /* Trial points, bx = best current */
|
||||
double af ,bf, cf; /* Function values at those points */
|
||||
|
||||
af = (*func)(fdata, ax);
|
||||
bf = (*func)(fdata, bx);
|
||||
|
||||
/* Sanity check bracketing */
|
||||
if (af * bf > 0.0)
|
||||
return -1; /* No good */
|
||||
|
||||
cx = bx; /* Force bisection for first itter */
|
||||
cf = bf;
|
||||
for (i = 0; i < ZBRENT_MAXIT; i++) {
|
||||
double xdel; /* Bisection delta to bx */
|
||||
double del = 1e80; /* Delta to be applied to bx */
|
||||
double pdel = 1e80; /* Last del from interpolation step */
|
||||
double tol1; /* Minimum reasonable change in bx */
|
||||
|
||||
/* Make bx and cx straddle root */
|
||||
if (bf * cf > 0.0) { /* bx and cx don't straddle root */
|
||||
cx = ax; /* ax must, so make cx = ax */
|
||||
cf = af;
|
||||
pdel = del = bx - ax;
|
||||
}
|
||||
|
||||
/* Make bx be point closest to solution */
|
||||
if (fabs(cf) < fabs(bf)) {
|
||||
ax = bx; /* swap bx & cx, and make ax == new cx */
|
||||
af = bf;
|
||||
bx = cx;
|
||||
bf = cf;
|
||||
cx = ax;
|
||||
cf = af;
|
||||
}
|
||||
tol1 = (0.5 * tol) + (2.0 * DBL_EPSILON * fabs(bx)); /* Minimum tollerable bx move */
|
||||
xdel = 0.5 * (cx - bx); /* Delta to bx for bisection move */
|
||||
|
||||
if (bf == 0.0 || fabs(xdel) <= tol1) { /* If exact soln, or last was min move */
|
||||
*rv = bx;
|
||||
return 0;
|
||||
}
|
||||
if (fabs(pdel) >= tol1 && fabs(af) > fabs(bf)) { /* Try inv. quadratic interpolation */
|
||||
double P, Q;
|
||||
|
||||
if (ax == cx) { /* Only have 2 points, use extrapolation */
|
||||
double R;
|
||||
R = bf / cf;
|
||||
P = (cx - bx) * R;
|
||||
Q = R - 1.0;
|
||||
} else { /* Brent's interpolation of 3 points */
|
||||
double R, S, T;
|
||||
R = bf / cf;
|
||||
S = bf / af;
|
||||
T = af / cf;
|
||||
P = S * ((T * (R - T) * (cx - bx)) - ((1.0 - R) * (bx - ax)));
|
||||
Q = (T - 1.0) * (R - 1.0) * (S - 1.0);
|
||||
}
|
||||
if (P < 0.0) /* Keep sign of P/Q with abs(P) */
|
||||
Q = -Q;
|
||||
P = fabs(P);
|
||||
{
|
||||
double min1, min2;
|
||||
min1 = (3.0 * xdel * Q) - (tol1 * fabs(Q));
|
||||
min2 = fabs(pdel * Q);
|
||||
if (min2 < min1)
|
||||
min1 = min2;
|
||||
|
||||
if ((2.0 * P) < min1) { /* Interpolation looks OK */
|
||||
pdel = del; /* Remember last delta */
|
||||
del = P / Q; /* Next delta */
|
||||
} else {
|
||||
pdel = del = xdel; /* Use bisection */
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pdel = del = xdel; /* Use bisection */
|
||||
}
|
||||
ax = bx; /* a keeps previous best point */
|
||||
af = bf;
|
||||
if (fabs(del) > tol1) /* Delta looks reasonable */
|
||||
bx += del;
|
||||
else
|
||||
bx += (xdel > 0.0 ? tol1 : -tol1); /* Do minimum move in direction of bisection */
|
||||
bf = (*func)(fdata, bx);
|
||||
}
|
||||
return -2; /* Too many iterations */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
44
Argyll_V3.5.0/numlib/zbrent.h
Normal file
44
Argyll_V3.5.0/numlib/zbrent.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef ROOT_H
|
||||
#define ROOT_H
|
||||
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 1 dimentional root finding code */
|
||||
|
||||
/* Bracket search function */
|
||||
/* return 0 on sucess */
|
||||
/* -1 on no range */
|
||||
/* -2 on too many itterations */
|
||||
int zbrac(
|
||||
double *x1, /* Input and output bracket values */
|
||||
double *x2,
|
||||
double (*func)(void *fdata, double tp), /* function to evaluate */
|
||||
void *fdata); /* Opaque data pointer */
|
||||
|
||||
/* Root finder */
|
||||
/* return 0 on sucess */
|
||||
/* -1 on root not bracketed */
|
||||
/* -2 on too many itterations */
|
||||
int zbrent(
|
||||
double *rv, /* Return value */
|
||||
double x1, /* Bracket to search */
|
||||
double x2, /* (Min, Max) */
|
||||
double tol, /* Desired tollerance */
|
||||
double (*func)(void *fdata, double tp), /* function to evaluate */
|
||||
void *fdata); /* Opaque data pointer */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ROOT_H */
|
||||
56
Argyll_V3.5.0/numlib/zbrenttest.c
Normal file
56
Argyll_V3.5.0/numlib/zbrenttest.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/* Do a test of the brent 1d root finder */
|
||||
|
||||
/*
|
||||
* Copyright 2000 Graeme W. Gill
|
||||
* All rights reserved.
|
||||
*
|
||||
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
|
||||
* see the License.txt file for licencing details.
|
||||
*/
|
||||
|
||||
/* Solves (3 - 2*X) * X = -1 */
|
||||
|
||||
#include "numlib.h"
|
||||
|
||||
int its = 0;
|
||||
double fcn(void *fdata, double tp);
|
||||
|
||||
/* Expected solution */
|
||||
double expect = -2.80776403408306e-01;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int rv;
|
||||
double x1, x2, soln;
|
||||
|
||||
/* Attempt bracket the solution */
|
||||
x1 = -0.1;
|
||||
x2 = 0.1;
|
||||
rv = zbrac(&x1, &x2, fcn, NULL);
|
||||
if (rv != 0) {
|
||||
error("zbrack failed with rv = %d\n",rv);
|
||||
}
|
||||
printf("Got bracket %f, %f in %d itterations\n",x1,x2,its);
|
||||
|
||||
/* Solve the equation */
|
||||
its = 0;
|
||||
rv = zbrent(&soln, x1, x2, 1e-6, fcn, NULL);
|
||||
if (rv != 0) {
|
||||
error("xbrent failed with rv = %d\n",rv);
|
||||
}
|
||||
|
||||
printf("Solution = %f, expected = %f in %d itterations\n",soln, expect, its);
|
||||
return 0;
|
||||
|
||||
} /* main() */
|
||||
|
||||
/* Function being solved */
|
||||
double fcn(void *fdata, double tp)
|
||||
{
|
||||
double temp;
|
||||
its++;
|
||||
temp = ((3.0 - 2.0 * tp) * tp) + 1.0;
|
||||
/* printf("~~ %f returns %f\n",tp,temp); */
|
||||
return temp;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user