00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined (octave_sparse_perm_op_defs_h)
00024 #define octave_sparse_perm_op_defs_h 1
00025
00026
00027
00028 template <typename SM>
00029 SM octinternal_do_mul_colpm_sm (const octave_idx_type *pcol, const SM& a)
00030
00031 {
00032 const octave_idx_type nr = a.rows ();
00033 const octave_idx_type nc = a.cols ();
00034 const octave_idx_type nent = a.nnz ();
00035 SM r (nr, nc, nent);
00036
00037 for (octave_idx_type k = 0; k < nent; ++k)
00038 {
00039 OCTAVE_QUIT;
00040 r.xridx (k) = pcol[a.ridx (k)];
00041 r.xdata (k) = a.data (k);
00042 }
00043 for (octave_idx_type j = 0; j <= nc; ++j)
00044 r.xcidx (j) = a.cidx (j);
00045
00046 r.maybe_compress (false);
00047 return r;
00048 }
00049
00050 template <typename SM>
00051 SM octinternal_do_mul_pm_sm (const PermMatrix& p, const SM& a)
00052 {
00053 const octave_idx_type nr = a.rows ();
00054 if (p.cols () != nr)
00055 {
00056 gripe_nonconformant ("operator *", p.rows (), p.cols (), a.rows (), a.cols ());
00057 return SM ();
00058 }
00059
00060 if (p.is_row_perm ())
00061 {
00062
00063 const octave_idx_type *prow = p.pvec ().data ();
00064 OCTAVE_LOCAL_BUFFER(octave_idx_type, pcol, nr);
00065 for (octave_idx_type i = 0; i < nr; ++i)
00066 pcol[prow[i]] = i;
00067 return octinternal_do_mul_colpm_sm (pcol, a);
00068 }
00069 else
00070 return octinternal_do_mul_colpm_sm (p.pvec ().data (), a);
00071 }
00072
00073 template <typename SM>
00074 SM octinternal_do_mul_sm_rowpm (const SM& a, const octave_idx_type *prow)
00075
00076
00077 {
00078 const octave_idx_type nr = a.rows ();
00079 const octave_idx_type nc = a.cols ();
00080 const octave_idx_type nent = a.nnz ();
00081 SM r (nr, nc, nent);
00082
00083 for (octave_idx_type j_src = 0; j_src < nc; ++j_src)
00084 r.xcidx (prow[j_src]) = a.cidx (j_src+1) - a.cidx (j_src);
00085 octave_idx_type k = 0;
00086 for (octave_idx_type j = 0; j < nc; ++j)
00087 {
00088 const octave_idx_type tmp = r.xcidx (j);
00089 r.xcidx (j) = k;
00090 k += tmp;
00091 }
00092 r.xcidx (nc) = nent;
00093
00094 octave_idx_type k_src = 0;
00095 for (octave_idx_type j_src = 0; j_src < nc; ++j_src)
00096 {
00097 OCTAVE_QUIT;
00098 const octave_idx_type j = prow[j_src];
00099 const octave_idx_type kend_src = a.cidx (j_src + 1);
00100 for (k = r.xcidx (j); k_src < kend_src; ++k, ++k_src)
00101 {
00102 r.xridx (k) = a.ridx (k_src);
00103 r.xdata (k) = a.data (k_src);
00104 }
00105 }
00106 assert (k_src == nent);
00107
00108 r.maybe_compress (false);
00109 return r;
00110 }
00111
00112 template <typename SM>
00113 SM octinternal_do_mul_sm_colpm (const SM& a, const octave_idx_type *pcol)
00114
00115
00116 {
00117 const octave_idx_type nr = a.rows ();
00118 const octave_idx_type nc = a.cols ();
00119 const octave_idx_type nent = a.nnz ();
00120 SM r (nr, nc, nent);
00121
00122 for (octave_idx_type j = 0; j < nc; ++j)
00123 {
00124 const octave_idx_type j_src = pcol[j];
00125 r.xcidx (j+1) = r.xcidx (j) + (a.cidx (j_src+1) - a.cidx (j_src));
00126 }
00127 assert (r.xcidx (nc) == nent);
00128
00129 octave_idx_type k = 0;
00130 for (octave_idx_type j = 0; j < nc; ++j)
00131 {
00132 OCTAVE_QUIT;
00133 const octave_idx_type j_src = pcol[j];
00134 octave_idx_type k_src;
00135 const octave_idx_type kend_src = a.cidx (j_src + 1);
00136 for (k_src = a.cidx (j_src); k_src < kend_src; ++k_src, ++k)
00137 {
00138 r.xridx (k) = a.ridx (k_src);
00139 r.xdata (k) = a.data (k_src);
00140 }
00141 }
00142 assert (k == nent);
00143
00144 r.maybe_compress (false);
00145 return r;
00146 }
00147
00148 template <typename SM>
00149 SM octinternal_do_mul_sm_pm (const SM& a, const PermMatrix& p)
00150 {
00151 const octave_idx_type nc = a.cols ();
00152 if (p.rows () != nc)
00153 {
00154 gripe_nonconformant ("operator *", a.rows (), a.cols (), p.rows (), p.cols ());
00155 return SM ();
00156 }
00157
00158 if (p.is_row_perm ())
00159 return octinternal_do_mul_sm_rowpm (a, p.pvec ().data ());
00160 else
00161 return octinternal_do_mul_sm_colpm (a, p.pvec ().data ());
00162 }
00163
00164 #endif // octave_sparse_perm_op_defs_h