GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Sparse-perm-op-defs.h
Go to the documentation of this file.
1 /* -*- C++ -*-
2 
3 Copyright (C) 2009-2013 Jason Riedy
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_Sparse_perm_op_defs_h)
24 #define octave_Sparse_perm_op_defs_h 1
25 
26 // Matrix multiplication
27 
28 template <typename SM>
29 SM octinternal_do_mul_colpm_sm (const octave_idx_type *pcol, const SM& a)
30 // Relabel the rows according to pcol.
31 {
32  const octave_idx_type nr = a.rows ();
33  const octave_idx_type nc = a.cols ();
34  const octave_idx_type nent = a.nnz ();
35  SM r (nr, nc, nent);
36 
38 
39  for (octave_idx_type j = 0; j <= nc; ++j)
40  r.xcidx (j) = a.cidx (j);
41 
42  for (octave_idx_type j = 0; j < nc; j++)
43  {
44  octave_quit ();
45 
46  OCTAVE_LOCAL_BUFFER (octave_idx_type, sidx, r.xcidx (j+1) - r.xcidx (j));
47  for (octave_idx_type i = r.xcidx (j), ii = 0; i < r.xcidx (j+1); i++)
48  {
49  sidx[ii++]=i;
50  r.xridx (i) = pcol[a.ridx (i)];
51  }
52  sort.sort (r.xridx () + r.xcidx (j), sidx, r.xcidx (j+1) - r.xcidx (j));
53  for (octave_idx_type i = r.xcidx (j), ii = 0; i < r.xcidx (j+1); i++)
54  r.xdata (i) = a.data (sidx[ii++]);
55  }
56 
57  return r;
58 }
59 
60 template <typename SM>
61 SM octinternal_do_mul_pm_sm (const PermMatrix& p, const SM& a)
62 {
63  const octave_idx_type nr = a.rows ();
64  if (p.cols () != nr)
65  {
66  gripe_nonconformant ("operator *", p.rows (), p.cols (), a.rows (), a.cols ());
67  return SM ();
68  }
69 
70  if (p.is_row_perm ())
71  {
72  // Form the column permutation and then call the colpm_sm routine.
73  const octave_idx_type *prow = p.pvec ().data ();
75  for (octave_idx_type i = 0; i < nr; ++i)
76  pcol[prow[i]] = i;
77  return octinternal_do_mul_colpm_sm (pcol, a);
78  }
79  else
80  return octinternal_do_mul_colpm_sm (p.pvec ().data (), a);
81 }
82 
83 template <typename SM>
84 SM octinternal_do_mul_sm_rowpm (const SM& a, const octave_idx_type *prow)
85 // For a row permutation, iterate across the source a and stuff the
86 // results into the correct destination column in r.
87 {
88  const octave_idx_type nr = a.rows ();
89  const octave_idx_type nc = a.cols ();
90  const octave_idx_type nent = a.nnz ();
91  SM r (nr, nc, nent);
92 
93  for (octave_idx_type j_src = 0; j_src < nc; ++j_src)
94  r.xcidx (prow[j_src]) = a.cidx (j_src+1) - a.cidx (j_src);
95  octave_idx_type k = 0;
96  for (octave_idx_type j = 0; j < nc; ++j)
97  {
98  const octave_idx_type tmp = r.xcidx (j);
99  r.xcidx (j) = k;
100  k += tmp;
101  }
102  r.xcidx (nc) = nent;
103 
104  octave_idx_type k_src = 0;
105  for (octave_idx_type j_src = 0; j_src < nc; ++j_src)
106  {
107  octave_quit ();
108  const octave_idx_type j = prow[j_src];
109  const octave_idx_type kend_src = a.cidx (j_src + 1);
110  for (k = r.xcidx (j); k_src < kend_src; ++k, ++k_src)
111  {
112  r.xridx (k) = a.ridx (k_src);
113  r.xdata (k) = a.data (k_src);
114  }
115  }
116  assert (k_src == nent);
117 
118  return r;
119 }
120 
121 template <typename SM>
122 SM octinternal_do_mul_sm_colpm (const SM& a, const octave_idx_type *pcol)
123 // For a column permutation, iterate across the destination r and pull
124 // data from the correct column of a.
125 {
126  const octave_idx_type nr = a.rows ();
127  const octave_idx_type nc = a.cols ();
128  const octave_idx_type nent = a.nnz ();
129  SM r (nr, nc, nent);
130 
131  for (octave_idx_type j = 0; j < nc; ++j)
132  {
133  const octave_idx_type j_src = pcol[j];
134  r.xcidx (j+1) = r.xcidx (j) + (a.cidx (j_src+1) - a.cidx (j_src));
135  }
136  assert (r.xcidx (nc) == nent);
137 
138  octave_idx_type k = 0;
139  for (octave_idx_type j = 0; j < nc; ++j)
140  {
141  octave_quit ();
142  const octave_idx_type j_src = pcol[j];
143  octave_idx_type k_src;
144  const octave_idx_type kend_src = a.cidx (j_src + 1);
145  for (k_src = a.cidx (j_src); k_src < kend_src; ++k_src, ++k)
146  {
147  r.xridx (k) = a.ridx (k_src);
148  r.xdata (k) = a.data (k_src);
149  }
150  }
151  assert (k == nent);
152 
153  return r;
154 }
155 
156 template <typename SM>
157 SM octinternal_do_mul_sm_pm (const SM& a, const PermMatrix& p)
158 {
159  const octave_idx_type nc = a.cols ();
160  if (p.rows () != nc)
161  {
162  gripe_nonconformant ("operator *", a.rows (), a.cols (), p.rows (), p.cols ());
163  return SM ();
164  }
165 
166  if (p.is_row_perm ())
167  return octinternal_do_mul_sm_rowpm (a, p.pvec ().data ());
168  else
169  return octinternal_do_mul_sm_colpm (a, p.pvec ().data ());
170 }
171 
172 #endif // octave_Sparse_perm_op_defs_h