| [0a4f7f] | 1 | /* | 
|---|
|  | 2 | * vector_ops.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Apr 1, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [bf3817] | 8 | // include config.h | 
|---|
|  | 9 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 10 | #include <config.h> | 
|---|
|  | 11 | #endif | 
|---|
|  | 12 |  | 
|---|
| [112b09] | 13 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 14 |  | 
|---|
| [57f243] | 15 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 16 | #include "LinearAlgebra/Plane.hpp" | 
|---|
| [952f38] | 17 | #include "Helpers/Log.hpp" | 
|---|
|  | 18 | #include "Helpers/Verbose.hpp" | 
|---|
| [57f243] | 19 | #include "LinearAlgebra/gslmatrix.hpp" | 
|---|
| [0a4f7f] | 20 | #include "leastsquaremin.hpp" | 
|---|
| [952f38] | 21 | #include "Helpers/Info.hpp" | 
|---|
| [0a4f7f] | 22 | #include "Helpers/fast_functions.hpp" | 
|---|
|  | 23 | #include "Exceptions/LinearDependenceException.hpp" | 
|---|
| [215df0] | 24 | #include "Exceptions/SkewException.hpp" | 
|---|
| [0a4f7f] | 25 |  | 
|---|
|  | 26 | #include <gsl/gsl_linalg.h> | 
|---|
|  | 27 | #include <gsl/gsl_matrix.h> | 
|---|
|  | 28 | #include <gsl/gsl_permutation.h> | 
|---|
|  | 29 | #include <gsl/gsl_vector.h> | 
|---|
| [3dcb1f] | 30 | #include <gsl/gsl_multimin.h> | 
|---|
| [0a4f7f] | 31 |  | 
|---|
|  | 32 | /** | 
|---|
|  | 33 | * !@file | 
|---|
|  | 34 | * These files defines several common operation on vectors that should not | 
|---|
|  | 35 | * become part of the main vector class, because they are either to complex | 
|---|
|  | 36 | * or need methods from other subsystems that should not be moved to | 
|---|
|  | 37 | * the LinAlg-Subsystem | 
|---|
|  | 38 | */ | 
|---|
|  | 39 |  | 
|---|
|  | 40 | /** Creates a new vector as the one with least square distance to a given set of \a vectors. | 
|---|
|  | 41 | * \param *vectors set of vectors | 
|---|
|  | 42 | * \param num number of vectors | 
|---|
|  | 43 | * \return true if success, false if failed due to linear dependency | 
|---|
|  | 44 | */ | 
|---|
|  | 45 | bool LSQdistance(Vector &res,const Vector **vectors, int num) | 
|---|
|  | 46 | { | 
|---|
|  | 47 | int j; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | for (j=0;j<num;j++) { | 
|---|
|  | 50 | Log() << Verbose(1) << j << "th atom's vector: " << vectors[j] << endl; | 
|---|
|  | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 | int np = 3; | 
|---|
|  | 54 | struct LSQ_params par; | 
|---|
|  | 55 |  | 
|---|
|  | 56 | const gsl_multimin_fminimizer_type *T = | 
|---|
|  | 57 | gsl_multimin_fminimizer_nmsimplex; | 
|---|
|  | 58 | gsl_multimin_fminimizer *s = NULL; | 
|---|
|  | 59 | gsl_vector *ss, *y; | 
|---|
|  | 60 | gsl_multimin_function minex_func; | 
|---|
|  | 61 |  | 
|---|
|  | 62 | size_t iter = 0, i; | 
|---|
|  | 63 | int status; | 
|---|
|  | 64 | double size; | 
|---|
|  | 65 |  | 
|---|
|  | 66 | /* Initial vertex size vector */ | 
|---|
|  | 67 | ss = gsl_vector_alloc (np); | 
|---|
|  | 68 | y = gsl_vector_alloc (np); | 
|---|
|  | 69 |  | 
|---|
|  | 70 | /* Set all step sizes to 1 */ | 
|---|
|  | 71 | gsl_vector_set_all (ss, 1.0); | 
|---|
|  | 72 |  | 
|---|
|  | 73 | /* Starting point */ | 
|---|
|  | 74 | par.vectors = vectors; | 
|---|
|  | 75 | par.num = num; | 
|---|
|  | 76 |  | 
|---|
|  | 77 | for (i=NDIM;i--;) | 
|---|
|  | 78 | gsl_vector_set(y, i, (vectors[0]->at(i) - vectors[1]->at(i))/2.); | 
|---|
|  | 79 |  | 
|---|
|  | 80 | /* Initialize method and iterate */ | 
|---|
|  | 81 | minex_func.f = &LSQ; | 
|---|
|  | 82 | minex_func.n = np; | 
|---|
|  | 83 | minex_func.params = (void *)∥ | 
|---|
|  | 84 |  | 
|---|
|  | 85 | s = gsl_multimin_fminimizer_alloc (T, np); | 
|---|
|  | 86 | gsl_multimin_fminimizer_set (s, &minex_func, y, ss); | 
|---|
|  | 87 |  | 
|---|
|  | 88 | do | 
|---|
|  | 89 | { | 
|---|
|  | 90 | iter++; | 
|---|
|  | 91 | status = gsl_multimin_fminimizer_iterate(s); | 
|---|
|  | 92 |  | 
|---|
|  | 93 | if (status) | 
|---|
|  | 94 | break; | 
|---|
|  | 95 |  | 
|---|
|  | 96 | size = gsl_multimin_fminimizer_size (s); | 
|---|
|  | 97 | status = gsl_multimin_test_size (size, 1e-2); | 
|---|
|  | 98 |  | 
|---|
|  | 99 | if (status == GSL_SUCCESS) | 
|---|
|  | 100 | { | 
|---|
|  | 101 | printf ("converged to minimum at\n"); | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | printf ("%5d ", (int)iter); | 
|---|
|  | 105 | for (i = 0; i < (size_t)np; i++) | 
|---|
|  | 106 | { | 
|---|
|  | 107 | printf ("%10.3e ", gsl_vector_get (s->x, i)); | 
|---|
|  | 108 | } | 
|---|
|  | 109 | printf ("f() = %7.3f size = %.3f\n", s->fval, size); | 
|---|
|  | 110 | } | 
|---|
|  | 111 | while (status == GSL_CONTINUE && iter < 100); | 
|---|
|  | 112 |  | 
|---|
|  | 113 | for (i=(size_t)np;i--;) | 
|---|
|  | 114 | res[i] = gsl_vector_get(s->x, i); | 
|---|
|  | 115 | gsl_vector_free(y); | 
|---|
|  | 116 | gsl_vector_free(ss); | 
|---|
|  | 117 | gsl_multimin_fminimizer_free (s); | 
|---|
|  | 118 |  | 
|---|
|  | 119 | return true; | 
|---|
|  | 120 | }; | 
|---|