Mercurial > octave-nkf
comparison src/OPERATORS/op-dm-sm.cc @ 8965:42aff15e059b
Implement diag \ sparse and sparse / diag.
Not pretty, but somewhat efficient and preserves sparsity.
author | Jason Riedy <jason@acm.org> |
---|---|
date | Mon, 09 Mar 2009 17:49:14 -0400 |
parents | f4f4d65faaa0 |
children | 1bba53c0a38d |
comparison
equal
deleted
inserted
replaced
8964:f4f4d65faaa0 | 8965:42aff15e059b |
---|---|
31 #include "ops.h" | 31 #include "ops.h" |
32 | 32 |
33 #include "ov-re-diag.h" | 33 #include "ov-re-diag.h" |
34 #include "ov-re-sparse.h" | 34 #include "ov-re-sparse.h" |
35 | 35 |
36 #include "sparse-xdiv.h" | |
37 | |
36 // diagonal matrix by sparse matrix ops | 38 // diagonal matrix by sparse matrix ops |
37 | 39 |
38 DEFBINOP (mul_dm_sm, diag_matrix, sparse_matrix) | 40 DEFBINOP (mul_dm_sm, diag_matrix, sparse_matrix) |
39 { | 41 { |
40 CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_matrix&); | 42 CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_matrix&); |
54 octave_value out = octave_value (ret); | 56 octave_value out = octave_value (ret); |
55 typ.mark_as_unsymmetric (); | 57 typ.mark_as_unsymmetric (); |
56 out.matrix_type (typ); | 58 out.matrix_type (typ); |
57 return out; | 59 return out; |
58 } | 60 } |
61 } | |
62 | |
63 DEFBINOP (ldiv_dm_sm, diag_matrix, sparse_matrix) | |
64 { | |
65 CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_matrix&); | |
66 | |
67 MatrixType typ = v2.matrix_type (); | |
68 return xleftdiv (v1.diag_matrix_value (), v2.sparse_matrix_value (), typ); | |
59 } | 69 } |
60 | 70 |
61 // sparse matrix by diagonal matrix ops | 71 // sparse matrix by diagonal matrix ops |
62 | 72 |
63 DEFBINOP (mul_sm_dm, sparse_matrix, diag_matrix) | 73 DEFBINOP (mul_sm_dm, sparse_matrix, diag_matrix) |
81 out.matrix_type (typ); | 91 out.matrix_type (typ); |
82 return out; | 92 return out; |
83 } | 93 } |
84 } | 94 } |
85 | 95 |
96 DEFBINOP (div_sm_dm, sparse_matrix, diag_matrix) | |
97 { | |
98 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_diag_matrix&); | |
99 | |
100 if (v2.rows() == 1 && v2.columns() == 1) | |
101 { | |
102 double d = v2.scalar_value (); | |
103 | |
104 if (d == 0.0) | |
105 gripe_divide_by_zero (); | |
106 | |
107 return octave_value (v1.sparse_matrix_value () / d); | |
108 } | |
109 else | |
110 { | |
111 MatrixType typ = v2.matrix_type (); | |
112 return xdiv (v1.sparse_matrix_value (), v2.diag_matrix_value (), typ); | |
113 } | |
114 } | |
115 | |
86 void | 116 void |
87 install_dm_sm_ops (void) | 117 install_dm_sm_ops (void) |
88 { | 118 { |
89 INSTALL_BINOP (op_mul, octave_diag_matrix, octave_sparse_matrix, | 119 INSTALL_BINOP (op_mul, octave_diag_matrix, octave_sparse_matrix, |
90 mul_dm_sm); | 120 mul_dm_sm); |
91 | 121 |
122 INSTALL_BINOP (op_ldiv, octave_diag_matrix, octave_sparse_matrix, ldiv_dm_sm); | |
123 | |
92 INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_diag_matrix, | 124 INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_diag_matrix, |
93 mul_sm_dm); | 125 mul_sm_dm); |
126 | |
127 INSTALL_BINOP (op_div, octave_sparse_matrix, octave_diag_matrix, div_sm_dm); | |
94 } | 128 } |