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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
3911
|
23 // Author: A. S. Hodel <scotte@eng.auburn.edu> |
2928
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
|
29 #include "defun-dld.h" |
|
30 #include "error.h" |
|
31 #include "gripes.h" |
|
32 #include "oct-obj.h" |
|
33 #include "utils.h" |
|
34 |
|
35 DEFUN_DLD (expm, args, , |
3548
|
36 "-*- texinfo -*-\n\ |
3372
|
37 @deftypefn {Loadable Function} {} expm (@var{a})\n\ |
|
38 Return the exponential of a matrix, defined as the infinite Taylor\n\ |
|
39 series\n\ |
|
40 @iftex\n\ |
|
41 @tex\n\ |
|
42 $$\n\ |
|
43 \\exp (A) = I + A + {A^2 \\over 2!} + {A^3 \\over 3!} + \\cdots\n\ |
|
44 $$\n\ |
|
45 @end tex\n\ |
|
46 @end iftex\n\ |
|
47 @ifinfo\n\ |
|
48 \n\ |
|
49 @example\n\ |
|
50 expm(a) = I + a + a^2/2! + a^3/3! + ...\n\ |
|
51 @end example\n\ |
|
52 \n\ |
|
53 @end ifinfo\n\ |
|
54 The Taylor series is @emph{not} the way to compute the matrix\n\ |
|
55 exponential; see Moler and Van Loan, @cite{Nineteen Dubious Ways to\n\ |
|
56 Compute the Exponential of a Matrix}, SIAM Review, 1978. This routine\n\ |
|
57 uses Ward's diagonal\n\ |
|
58 @iftex\n\ |
|
59 @tex\n\ |
|
60 Pad\\'e\n\ |
|
61 @end tex\n\ |
|
62 @end iftex\n\ |
|
63 @ifinfo\n\ |
|
64 Pade'\n\ |
|
65 @end ifinfo\n\ |
|
66 approximation method with three step preconditioning (SIAM Journal on\n\ |
|
67 Numerical Analysis, 1977). Diagonal\n\ |
|
68 @iftex\n\ |
|
69 @tex\n\ |
|
70 Pad\\'e\n\ |
|
71 @end tex\n\ |
|
72 @end iftex\n\ |
|
73 @ifinfo\n\ |
|
74 Pade'\n\ |
|
75 @end ifinfo\n\ |
|
76 approximations are rational polynomials of matrices\n\ |
|
77 @iftex\n\ |
|
78 @tex\n\ |
|
79 $D_q(a)^{-1}N_q(a)$\n\ |
|
80 @end tex\n\ |
|
81 @end iftex\n\ |
|
82 @ifinfo\n\ |
|
83 \n\ |
|
84 @example\n\ |
|
85 -1\n\ |
|
86 D (a) N (a)\n\ |
|
87 @end example\n\ |
|
88 \n\ |
|
89 @end ifinfo\n\ |
|
90 whose Taylor series matches the first\n\ |
|
91 @iftex\n\ |
|
92 @tex\n\ |
|
93 $2 q + 1 $\n\ |
|
94 @end tex\n\ |
|
95 @end iftex\n\ |
|
96 @ifinfo\n\ |
|
97 @code{2q+1}\n\ |
|
98 @end ifinfo\n\ |
|
99 terms of the Taylor series above; direct evaluation of the Taylor series\n\ |
|
100 (with the same preconditioning steps) may be desirable in lieu of the\n\ |
|
101 @iftex\n\ |
|
102 @tex\n\ |
|
103 Pad\\'e\n\ |
|
104 @end tex\n\ |
|
105 @end iftex\n\ |
|
106 @ifinfo\n\ |
|
107 Pade'\n\ |
|
108 @end ifinfo\n\ |
|
109 approximation when\n\ |
|
110 @iftex\n\ |
|
111 @tex\n\ |
|
112 $D_q(a)$\n\ |
|
113 @end tex\n\ |
|
114 @end iftex\n\ |
|
115 @ifinfo\n\ |
|
116 @code{Dq(a)}\n\ |
|
117 @end ifinfo\n\ |
|
118 is ill-conditioned.\n\ |
|
119 @end deftypefn") |
2928
|
120 { |
4233
|
121 octave_value retval; |
2928
|
122 |
|
123 int nargin = args.length (); |
|
124 |
|
125 if (nargin != 1) |
|
126 { |
|
127 print_usage ("expm"); |
|
128 return retval; |
|
129 } |
|
130 |
|
131 octave_value arg = args(0); |
|
132 |
|
133 int nr = arg.rows (); |
|
134 int nc = arg.columns (); |
|
135 |
|
136 int arg_is_empty = empty_arg ("expm", nr, nc); |
|
137 |
|
138 if (arg_is_empty < 0) |
|
139 return retval; |
|
140 if (arg_is_empty > 0) |
4233
|
141 return octave_value (Matrix ()); |
2928
|
142 |
|
143 if (nr != nc) |
|
144 { |
|
145 gripe_square_matrix_required ("expm"); |
|
146 return retval; |
|
147 } |
|
148 |
|
149 if (arg.is_real_type ()) |
|
150 { |
|
151 Matrix m = arg.matrix_value (); |
|
152 |
|
153 if (error_state) |
|
154 return retval; |
|
155 else |
|
156 retval = m.expm (); |
|
157 } |
|
158 else if (arg.is_complex_type ()) |
|
159 { |
|
160 ComplexMatrix m = arg.complex_matrix_value (); |
|
161 |
|
162 if (error_state) |
|
163 return retval; |
|
164 else |
|
165 retval = m.expm (); |
|
166 } |
|
167 else |
|
168 { |
|
169 gripe_wrong_type_arg ("expm", arg); |
|
170 } |
|
171 |
|
172 return retval; |
|
173 } |
|
174 |
|
175 /* |
|
176 ;;; Local Variables: *** |
|
177 ;;; mode: C++ *** |
|
178 ;;; End: *** |
|
179 */ |