comparison src/ov-base-diag.cc @ 8367:445d27d79f4e

support permutation matrix objects
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 04 Dec 2008 08:31:56 +0100
parents 8b1a2555c4e2
children c43481a19bfe
comparison
equal deleted inserted replaced
8366:8b1a2555c4e2 8367:445d27d79f4e
68 octave_value 68 octave_value
69 octave_base_diag<DMT, MT>::do_index_op (const octave_value_list& idx, 69 octave_base_diag<DMT, MT>::do_index_op (const octave_value_list& idx,
70 bool resize_ok) 70 bool resize_ok)
71 { 71 {
72 octave_value retval; 72 octave_value retval;
73 73 typedef typename DMT::element_type el_type;
74 if (idx.length () == 2 && idx(0).is_scalar_type () 74 el_type one = 1;
75 && idx(1).is_scalar_type () && ! resize_ok) 75
76 { 76 octave_idx_type nidx = idx.length ();
77 idx_vector i = idx(0).index_vector (), j = idx(1).index_vector (); 77 idx_vector idx0, idx1;
78 // FIXME: the proxy mechanism of DiagArray2 causes problems here. 78 if (nidx == 2)
79 typedef typename DMT::element_type el_type; 79 {
80 if (! error_state) 80 idx0 = idx(0).index_vector ();
81 retval = el_type (matrix.checkelem (i(0), j(0))); 81 idx1 = idx(1).index_vector ();
82 } 82 }
83 else 83
84 retval = to_dense ().do_index_op (idx, resize_ok); 84 // This hack is to allow constructing permutation matrices using
85 // eye(n)(p,:), eye(n)(:,q) && eye(n)(p,q) where p & q are permutation
86 // vectors.
87 // Note that, for better consistency, eye(n)(:,:) still converts to a full
88 // matrix.
89 // FIXME: This check is probably unnecessary for complex matrices.
90 if (! error_state && nidx == 2 && matrix.is_multiple_of_identity (one))
91 {
92
93 bool left = idx0.is_permutation (matrix.rows ());
94 bool right = idx1.is_permutation (matrix.cols ());
95
96 if (left && right)
97 {
98 if (idx0.is_colon ()) left = false;
99 if (idx1.is_colon ()) right = false;
100 if (left && right)
101 retval = octave_value (PermMatrix (idx0, false) * PermMatrix (idx1, true),
102 is_single_type ());
103 else if (left)
104 retval = octave_value (PermMatrix (idx0, false),
105 is_single_type ());
106 else if (right)
107 retval = octave_value (PermMatrix (idx1, true),
108 is_single_type ());
109 }
110 }
111
112 // if error_state is set, we've already griped.
113 if (! error_state && ! retval.is_defined ())
114 {
115 if (nidx == 2 && ! resize_ok &&
116 idx0.is_scalar () && idx1.is_scalar ())
117 {
118 // FIXME: the proxy mechanism of DiagArray2 causes problems here.
119 retval = el_type (matrix.checkelem (idx0(0), idx1(0)));
120 }
121 else
122 retval = to_dense ().do_index_op (idx, resize_ok);
123 }
124
85 return retval; 125 return retval;
86 } 126 }
87 127
88 template <class DMT, class MT> 128 template <class DMT, class MT>
89 octave_value 129 octave_value