2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2928
|
21 |
|
22 */ |
|
23 |
3911
|
24 // Author: A. S. Hodel <scotte@eng.auburn.edu> |
2928
|
25 |
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include <config.h> |
|
28 #endif |
|
29 |
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "utils.h" |
|
35 |
|
36 DEFUN_DLD (expm, args, , |
3548
|
37 "-*- texinfo -*-\n\ |
3372
|
38 @deftypefn {Loadable Function} {} expm (@var{a})\n\ |
|
39 Return the exponential of a matrix, defined as the infinite Taylor\n\ |
|
40 series\n\ |
|
41 @iftex\n\ |
|
42 @tex\n\ |
|
43 $$\n\ |
|
44 \\exp (A) = I + A + {A^2 \\over 2!} + {A^3 \\over 3!} + \\cdots\n\ |
|
45 $$\n\ |
|
46 @end tex\n\ |
|
47 @end iftex\n\ |
|
48 @ifinfo\n\ |
|
49 \n\ |
|
50 @example\n\ |
|
51 expm(a) = I + a + a^2/2! + a^3/3! + ...\n\ |
|
52 @end example\n\ |
|
53 \n\ |
|
54 @end ifinfo\n\ |
|
55 The Taylor series is @emph{not} the way to compute the matrix\n\ |
|
56 exponential; see Moler and Van Loan, @cite{Nineteen Dubious Ways to\n\ |
|
57 Compute the Exponential of a Matrix}, SIAM Review, 1978. This routine\n\ |
|
58 uses Ward's diagonal\n\ |
|
59 @iftex\n\ |
|
60 @tex\n\ |
|
61 Pad\\'e\n\ |
|
62 @end tex\n\ |
|
63 @end iftex\n\ |
|
64 @ifinfo\n\ |
|
65 Pade'\n\ |
|
66 @end ifinfo\n\ |
|
67 approximation method with three step preconditioning (SIAM Journal on\n\ |
|
68 Numerical Analysis, 1977). Diagonal\n\ |
|
69 @iftex\n\ |
|
70 @tex\n\ |
|
71 Pad\\'e\n\ |
|
72 @end tex\n\ |
|
73 @end iftex\n\ |
|
74 @ifinfo\n\ |
|
75 Pade'\n\ |
|
76 @end ifinfo\n\ |
|
77 approximations are rational polynomials of matrices\n\ |
|
78 @iftex\n\ |
|
79 @tex\n\ |
|
80 $D_q(a)^{-1}N_q(a)$\n\ |
|
81 @end tex\n\ |
|
82 @end iftex\n\ |
|
83 @ifinfo\n\ |
|
84 \n\ |
|
85 @example\n\ |
|
86 -1\n\ |
|
87 D (a) N (a)\n\ |
|
88 @end example\n\ |
|
89 \n\ |
|
90 @end ifinfo\n\ |
|
91 whose Taylor series matches the first\n\ |
|
92 @iftex\n\ |
|
93 @tex\n\ |
|
94 $2 q + 1 $\n\ |
|
95 @end tex\n\ |
|
96 @end iftex\n\ |
|
97 @ifinfo\n\ |
|
98 @code{2q+1}\n\ |
|
99 @end ifinfo\n\ |
|
100 terms of the Taylor series above; direct evaluation of the Taylor series\n\ |
|
101 (with the same preconditioning steps) may be desirable in lieu of the\n\ |
|
102 @iftex\n\ |
|
103 @tex\n\ |
|
104 Pad\\'e\n\ |
|
105 @end tex\n\ |
|
106 @end iftex\n\ |
|
107 @ifinfo\n\ |
|
108 Pade'\n\ |
|
109 @end ifinfo\n\ |
|
110 approximation when\n\ |
|
111 @iftex\n\ |
|
112 @tex\n\ |
|
113 $D_q(a)$\n\ |
|
114 @end tex\n\ |
|
115 @end iftex\n\ |
|
116 @ifinfo\n\ |
|
117 @code{Dq(a)}\n\ |
|
118 @end ifinfo\n\ |
|
119 is ill-conditioned.\n\ |
|
120 @end deftypefn") |
2928
|
121 { |
4233
|
122 octave_value retval; |
2928
|
123 |
|
124 int nargin = args.length (); |
|
125 |
|
126 if (nargin != 1) |
|
127 { |
5823
|
128 print_usage (); |
2928
|
129 return retval; |
|
130 } |
|
131 |
|
132 octave_value arg = args(0); |
|
133 |
5275
|
134 octave_idx_type nr = arg.rows (); |
|
135 octave_idx_type nc = arg.columns (); |
2928
|
136 |
|
137 int arg_is_empty = empty_arg ("expm", nr, nc); |
|
138 |
|
139 if (arg_is_empty < 0) |
|
140 return retval; |
|
141 if (arg_is_empty > 0) |
4233
|
142 return octave_value (Matrix ()); |
2928
|
143 |
|
144 if (nr != nc) |
|
145 { |
|
146 gripe_square_matrix_required ("expm"); |
|
147 return retval; |
|
148 } |
|
149 |
|
150 if (arg.is_real_type ()) |
|
151 { |
|
152 Matrix m = arg.matrix_value (); |
|
153 |
|
154 if (error_state) |
|
155 return retval; |
|
156 else |
|
157 retval = m.expm (); |
|
158 } |
|
159 else if (arg.is_complex_type ()) |
|
160 { |
|
161 ComplexMatrix m = arg.complex_matrix_value (); |
|
162 |
|
163 if (error_state) |
|
164 return retval; |
|
165 else |
|
166 retval = m.expm (); |
|
167 } |
|
168 else |
|
169 { |
|
170 gripe_wrong_type_arg ("expm", arg); |
|
171 } |
|
172 |
|
173 return retval; |
|
174 } |
|
175 |
|
176 /* |
|
177 ;;; Local Variables: *** |
|
178 ;;; mode: C++ *** |
|
179 ;;; End: *** |
|
180 */ |