comparison src/OPERATORS/op-pm-sm.cc @ 8968:91d53dc37f79

Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations. Nothing terribly fancy in any of this. There probably is some mechanism for using the permutation vectors and some assign or index method in the sparse classes, but I've never understood all the intricacies. I'm opting for a simple implementation at the cost of possibly duplicating some functionality.
author Jason Riedy <jason@acm.org>
date Tue, 10 Mar 2009 21:54:44 -0400
parents
children fb6b6fcafa62
comparison
equal deleted inserted replaced
8967:5bbbf482909a 8968:91d53dc37f79
1 /*
2
3 Copyright (C) 2009 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "gripes.h"
28 #include "oct-obj.h"
29 #include "ov.h"
30 #include "ov-typeinfo.h"
31 #include "ops.h"
32
33 #include "ov-perm.h"
34 #include "ov-re-sparse.h"
35
36 // permutation matrix by sparse matrix ops
37
38 DEFBINOP (mul_pm_sm, perm_matrix, sparse_matrix)
39 {
40 CAST_BINOP_ARGS (const octave_perm_matrix&, const octave_sparse_matrix&);
41
42 if (v2.rows() == 1 && v2.columns() == 1)
43 {
44 double d = v2.scalar_value ();
45
46 return octave_value (v1.sparse_matrix_value () * d);
47 }
48 else if (v1.rows() == 1 && v1.columns() == 1)
49 return octave_value (v2.sparse_matrix_value ());
50 else
51 return v1.perm_matrix_value () * v2.sparse_matrix_value ();
52 }
53
54 DEFBINOP (ldiv_pm_sm, perm_matrix, sparse_matrix)
55 {
56 CAST_BINOP_ARGS (const octave_perm_matrix&, const octave_sparse_matrix&);
57
58 return v1.perm_matrix_value ().inverse () * v2.sparse_matrix_value ();
59 }
60
61 // sparse matrix by diagonal matrix ops
62
63 DEFBINOP (mul_sm_pm, sparse_matrix, perm_matrix)
64 {
65 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_perm_matrix&);
66
67 if (v1.rows() == 1 && v1.columns() == 1)
68 {
69 double d = v1.scalar_value ();
70
71 return octave_value (d * v2.sparse_matrix_value ());
72 }
73 else if (v2.rows() == 1 && v2.columns() == 1)
74 return octave_value (v1.sparse_matrix_value ());
75 else
76 return v1.sparse_matrix_value () * v2.perm_matrix_value ();
77 }
78
79 DEFBINOP (div_sm_pm, sparse_matrix, perm_matrix)
80 {
81 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_perm_matrix&);
82
83 return v1.sparse_matrix_value () * v2.perm_matrix_value ().inverse ();
84 }
85
86 void
87 install_pm_sm_ops (void)
88 {
89 INSTALL_BINOP (op_mul, octave_perm_matrix, octave_sparse_matrix,
90 mul_pm_sm);
91 INSTALL_BINOP (op_ldiv, octave_perm_matrix, octave_sparse_matrix,
92 ldiv_pm_sm);
93 INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_perm_matrix,
94 mul_sm_pm);
95 INSTALL_BINOP (op_div, octave_sparse_matrix, octave_perm_matrix,
96 div_sm_pm);
97 }