Debian Codegraph @

external/gsl/gsl_linalg__exponential.c

raw symbols SCIP
1 /* linalg/exponential.c
2 *
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2007 Gerard Jungman, Brian Gough
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 /* Author: G. Jungman */
21
22 /* Calculate the matrix exponential, following
23 * Moler + Van Loan, SIAM Rev. 20, 801 (1978).
24 */
25
26 #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/
27 #include <stdlib.h>
28 #include "gsl_math.h"cxx . . $ `<file>/external/gsl/gsl_math.h`/
29 #include "gsl_mode.h"cxx . . $ `<file>/external/gsl/gsl_mode.h`/
30 #include "gsl_errno.h"cxx . . $ `<file>/external/gsl/gsl_errno.h`/
31 #include "gsl_blas.h"cxx . . $ `<file>/external/gsl/gsl_blas.h`/
32
33 #include "gsl_linalg.h"cxx . . $ `<file>/external/gsl/gsl_linalg.h`/
34
35
36 /* store one of the suggested choices for the
37 * Taylor series / square method from Moler + VanLoan
38 */
39 struct moler_vanloan_optimal_suggestioncxx . . $ moler_vanloan_optimal_suggestion#
40 {
41 int kcxx . . $ moler_vanloan_optimal_suggestion#k.;
42 int jcxx . . $ moler_vanloan_optimal_suggestion#j.;
43 };
44 typedef struct moler_vanloan_optimal_suggestioncxx . . $ moler_vanloan_optimal_suggestion# mvl_suggestion_tcxx . . $ mvl_suggestion_t#;
45
46
47 /* table from Moler and Van Loan
48 * mvl_tab[gsl_mode_t][matrix_norm_group]
49 */
50 static mvl_suggestion_tcxx . . $ mvl_suggestion_t# mvl_tabcxx . . $ mvl_tab.[3][6] =
51 {
52 /* double precision */
53 {
54 { 5, 1 }, { 5, 4 }, { 7, 5 }, { 9, 7 }, { 10, 10 }, { 8, 14 }
55 },
56
57 /* single precision */
58 {
59 { 2, 1 }, { 4, 0 }, { 7, 1 }, { 6, 5 }, { 5, 9 }, { 7, 11 }
60 },
61
62 /* approx precision */
63 {
64 { 1, 0 }, { 3, 0 }, { 5, 1 }, { 4, 5 }, { 4, 8 }, { 2, 11 }
65 }
66 };
67
68
69 inline
70 static double
71 sup_normcxx . . $ sup_norm(ba987e28edd9622e).(const gsl_matrixcxx . . $ gsl_matrix# * A)
72 {
73 double min, max;
74 gsl_matrix_minmaxcxx . . $ gsl_matrix_minmax(1029d0b06d676e45).(A, &min, &max);
75 return GSL_MAX_DBLcxx . . $ `external/gsl/gsl_math.h:177:9`!(fabs(min), fabs(max));
76 }
77
78
79 static
80 mvl_suggestion_tcxx . . $ mvl_suggestion_t#
81 obtain_suggestioncxx . . $ obtain_suggestion(539b97b6100dd8ad).(const gsl_matrixcxx . . $ gsl_matrix# * A, gsl_mode_tcxx . . $ gsl_mode_t# mode)
82 {
83 const unsigned int mode_prec = GSL_MODE_PRECcxx . . $ `external/gsl/gsl_mode.h:76:9`!(mode);
84 const double norm_A = sup_normcxx . . $ sup_norm(ba987e28edd9622e).(A);
85 if(norm_A < 0.01) return mvl_tabcxx . . $ mvl_tab.[mode_prec][0];
86 else if(norm_A < 0.1) return mvl_tabcxx . . $ mvl_tab.[mode_prec][1];
87 else if(norm_A < 1.0) return mvl_tabcxx . . $ mvl_tab.[mode_prec][2];
88 else if(norm_A < 10.0) return mvl_tabcxx . . $ mvl_tab.[mode_prec][3];
89 else if(norm_A < 100.0) return mvl_tabcxx . . $ mvl_tab.[mode_prec][4];
90 else if(norm_A < 1000.0) return mvl_tabcxx . . $ mvl_tab.[mode_prec][5];
91 else
92 {
93 /* outside the table we simply increase the number
94 * of squarings, bringing the reduced matrix into
95 * the range of the table; this is obviously suboptimal,
96 * but that is the price paid for not having those extra
97 * table entries
98 */
99 const double extra = log(1.01*norm_A/1000.0) / M_LN2cxx . . $ `<external>/11263917310783833665/math.h:1119:10`!;
100 const int extra_i = (unsigned int) ceil(extra);
101 mvl_suggestion_tcxx . . $ mvl_suggestion_t# s = mvl_tabcxx . . $ mvl_tab.[mode][5];
102 s.jcxx . . $ moler_vanloan_optimal_suggestion#j. += extra_i;
103 return s;
104 }
105 }
106
107
108 /* use series representation to calculate matrix exponential;
109 * this is used for small matrices; we use the sup_norm
110 * to measure the size of the terms in the expansion
111 */
112 static void
113 matrix_exp_seriescxx . . $ matrix_exp_series(341950b3878eec97).(
114 const gsl_matrixcxx . . $ gsl_matrix# * B,
115 gsl_matrixcxx . . $ gsl_matrix# * eB,
116 int number_of_terms
117 )
118 {
119 int count;
120 gsl_matrixcxx . . $ gsl_matrix# * temp = gsl_matrix_calloccxx . . $ gsl_matrix_calloc(5a4d020b4836594).(B->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1., B->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.);
121
122 /* init the Horner polynomial evaluation,
123 * eB = 1 + B/number_of_terms; we use
124 * eB to collect the partial results
125 */
126 gsl_matrix_memcpycxx . . $ gsl_matrix_memcpy(2d55a18be0e0c72f).(eB, B);
127 gsl_matrix_scalecxx . . $ gsl_matrix_scale(c69b6356addad039).(eB, 1.0/number_of_terms);
128 gsl_matrix_add_diagonalcxx . . $ gsl_matrix_add_diagonal(c69b6356addad039).(eB, 1.0);
129 for(count = number_of_terms-1; count >= 1; --count)
130 {
131 /* mult_temp = 1 + B eB / count */
132 gsl_blas_dgemmcxx . . $ gsl_blas_dgemm(59171c9b45f25b89).(CblasNoTranscxx . . $ CblasNoTrans., CblasNoTranscxx . . $ CblasNoTrans., 1.0, B, eB, 0.0, temp);
133 gsl_matrix_scalecxx . . $ gsl_matrix_scale(c69b6356addad039).(temp, 1.0/count);
134 gsl_matrix_add_diagonalcxx . . $ gsl_matrix_add_diagonal(c69b6356addad039).(temp, 1.0);
135
136 /* transfer partial result out of temp */
137 gsl_matrix_memcpycxx . . $ gsl_matrix_memcpy(2d55a18be0e0c72f).(eB, temp);
138 }
139
140 /* now eB holds the full result; we're done */
141 gsl_matrix_freecxx . . $ gsl_matrix_free(ab29a617cccd3636).(temp);
142 }
143
144
145 int
146 gsl_linalg_exponential_sscxx . . $ gsl_linalg_exponential_ss(221f824eb7112d16).(
147 const gsl_matrixcxx . . $ gsl_matrix# * A,
148 gsl_matrixcxx . . $ gsl_matrix# * eA,
149 gsl_mode_tcxx . . $ gsl_mode_t# mode
150 )
151 {
152 if(A->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != A->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.)
153 {
154 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`!("cannot exponentiate a non-square matrix", GSL_ENOTSQR);
155 }
156 else if(A->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != eA->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. || A->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2. != eA->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.)
157 {
158 GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`!("exponential of matrix must have same dimension as matrix", GSL_EBADLEN);
159 }
160 else
161 {
162 int i;
163 const mvl_suggestion_tcxx . . $ mvl_suggestion_t# sugg = obtain_suggestioncxx . . $ obtain_suggestion(539b97b6100dd8ad).(A, mode);
164 const double divisor = exp(M_LN2cxx . . $ `<external>/11263917310783833665/math.h:1119:10`! * sugg.jcxx . . $ moler_vanloan_optimal_suggestion#j.);
165
166 gsl_matrixcxx . . $ gsl_matrix# * reduced_A = gsl_matrix_alloccxx . . $ gsl_matrix_alloc(5a4d020b4836594).(A->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1., A->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.);
167
168 /* decrease A by the calculated divisor */
169 gsl_matrix_memcpycxx . . $ gsl_matrix_memcpy(2d55a18be0e0c72f).(reduced_A, A);
170 gsl_matrix_scalecxx . . $ gsl_matrix_scale(c69b6356addad039).(reduced_A, 1.0/divisor);
171
172 /* calculate exp of reduced matrix; store in eA as temp */
173 matrix_exp_seriescxx . . $ matrix_exp_series(341950b3878eec97).(reduced_A, eA, sugg.kcxx . . $ moler_vanloan_optimal_suggestion#k.);
174
175 /* square repeatedly; use reduced_A for scratch */
176 for(i = 0; i < sugg.jcxx . . $ moler_vanloan_optimal_suggestion#j.; ++i)
177 {
178 gsl_blas_dgemmcxx . . $ gsl_blas_dgemm(59171c9b45f25b89).(CblasNoTranscxx . . $ CblasNoTrans., CblasNoTranscxx . . $ CblasNoTrans., 1.0, eA, eA, 0.0, reduced_A);
179 gsl_matrix_memcpycxx . . $ gsl_matrix_memcpy(2d55a18be0e0c72f).(eA, reduced_A);
180 }
181
182 gsl_matrix_freecxx . . $ gsl_matrix_free(ab29a617cccd3636).(reduced_A);
183
184 return GSL_SUCCESScxx . . $ GSL_SUCCESS.;
185 }
186 }
187