comparison src/DLD-FUNCTIONS/pinv.cc @ 8840:c690e3772583

support diagonal matrices in pinv
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 23 Feb 2009 14:54:56 +0100
parents 82be108cc558
children a2878ba31a9e
comparison
equal deleted inserted replaced
8839:fcba62cc4549 8840:c690e3772583
27 #include "defun-dld.h" 27 #include "defun-dld.h"
28 #include "error.h" 28 #include "error.h"
29 #include "gripes.h" 29 #include "gripes.h"
30 #include "oct-obj.h" 30 #include "oct-obj.h"
31 #include "utils.h" 31 #include "utils.h"
32 #include "ops.h"
33 #include "ov-re-diag.h"
34 #include "ov-cx-diag.h"
35 #include "ov-flt-re-diag.h"
36 #include "ov-flt-cx-diag.h"
37 #include "ov-perm.h"
38 #include "ov-flt-perm.h"
32 39
33 DEFUN_DLD (pinv, args, , 40 DEFUN_DLD (pinv, args, ,
34 "-*- texinfo -*-\n\ 41 "-*- texinfo -*-\n\
35 @deftypefn {Loadable Function} {} pinv (@var{x}, @var{tol})\n\ 42 @deftypefn {Loadable Function} {} pinv (@var{x}, @var{tol})\n\
36 Return the pseudoinverse of @var{x}. Singular values less than\n\ 43 Return the pseudoinverse of @var{x}. Singular values less than\n\
63 if (arg_is_empty < 0) 70 if (arg_is_empty < 0)
64 return retval; 71 return retval;
65 else if (arg_is_empty > 0) 72 else if (arg_is_empty > 0)
66 return octave_value (Matrix ()); 73 return octave_value (Matrix ());
67 74
68 if (arg.is_single_type ()) 75 bool isfloat = arg.is_single_type ();
76
77 if (arg.is_diag_matrix ())
78 {
79 if (nargin == 2)
80 warning ("pinv: tol is ignored for diagonal matrices");
81
82 const octave_base_value& a = arg.get_rep ();
83 if (arg.is_complex_type ())
84 {
85 if (isfloat)
86 {
87 CAST_CONV_ARG (const octave_float_complex_diag_matrix&);
88 retval = v.float_complex_diag_matrix_value ().pseudo_inverse ();
89 }
90 else
91 {
92 CAST_CONV_ARG (const octave_complex_diag_matrix&);
93 retval = v.complex_diag_matrix_value ().pseudo_inverse ();
94 }
95 }
96 else
97 {
98 if (isfloat)
99 {
100 CAST_CONV_ARG (const octave_float_diag_matrix&);
101 retval = v.float_diag_matrix_value ().pseudo_inverse ();
102 }
103 else
104 {
105 CAST_CONV_ARG (const octave_diag_matrix&);
106 retval = v.diag_matrix_value ().pseudo_inverse ();
107 }
108 }
109 }
110 else if (arg.is_perm_matrix ())
111 {
112 const octave_base_value& a = arg.get_rep ();
113 if (isfloat)
114 {
115 CAST_CONV_ARG (const octave_float_perm_matrix&);
116 retval = v.perm_matrix_value ().inverse ();
117 }
118 else
119 {
120 CAST_CONV_ARG (const octave_perm_matrix&);
121 retval = v.perm_matrix_value ().inverse ();
122 }
123 }
124 else if (isfloat)
69 { 125 {
70 float tol = 0.0; 126 float tol = 0.0;
71 if (nargin == 2) 127 if (nargin == 2)
72 tol = args(1).float_value (); 128 tol = args(1).float_value ();
73 129