00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026
00027 #include "gripes.h"
00028 #include "oct-obj.h"
00029 #include "ov.h"
00030 #include "ov-typeinfo.h"
00031 #include "ops.h"
00032
00033 #include "ov-re-diag.h"
00034 #include "ov-re-sparse.h"
00035
00036 #include "sparse-xdiv.h"
00037
00038
00039
00040 DEFBINOP (mul_dm_sm, diag_matrix, sparse_matrix)
00041 {
00042 CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_matrix&);
00043
00044 if (v2.rows() == 1 && v2.columns() == 1)
00045
00046
00047 {
00048 double d = v2.scalar_value ();
00049
00050 return octave_value (v1.diag_matrix_value () * d);
00051 }
00052 else
00053 {
00054 MatrixType typ = v2.matrix_type ();
00055 SparseMatrix ret = v1.diag_matrix_value () * v2.sparse_matrix_value ();
00056 octave_value out = octave_value (ret);
00057 typ.mark_as_unsymmetric ();
00058 out.matrix_type (typ);
00059 return out;
00060 }
00061 }
00062
00063 DEFBINOP (ldiv_dm_sm, diag_matrix, sparse_matrix)
00064 {
00065 CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_matrix&);
00066
00067 MatrixType typ = v2.matrix_type ();
00068 return xleftdiv (v1.diag_matrix_value (), v2.sparse_matrix_value (), typ);
00069 }
00070
00071 DEFBINOP (add_dm_sm, diag_matrix, sparse_matrix)
00072 {
00073 CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_matrix&);
00074
00075 if (v2.rows() == 1 && v2.columns() == 1)
00076
00077
00078 {
00079 double d = v2.scalar_value ();
00080
00081 return octave_value (v1.matrix_value () + d);
00082 }
00083 else
00084 return v1.diag_matrix_value () + v2.sparse_matrix_value ();
00085 }
00086
00087 DEFBINOP (sub_dm_sm, diag_matrix, sparse_matrix)
00088 {
00089 CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_matrix&);
00090
00091 if (v2.rows() == 1 && v2.columns() == 1)
00092
00093
00094 {
00095 double d = v2.scalar_value ();
00096
00097 return octave_value (v1.matrix_value () - d);
00098 }
00099 else
00100 return v1.diag_matrix_value () - v2.sparse_matrix_value ();
00101 }
00102
00103
00104
00105 DEFBINOP (mul_sm_dm, sparse_matrix, diag_matrix)
00106 {
00107 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_diag_matrix&);
00108
00109 if (v1.rows() == 1 && v1.columns() == 1)
00110
00111
00112 {
00113 double d = v1.scalar_value ();
00114
00115 return octave_value (d * v2.diag_matrix_value ());
00116 }
00117 else
00118 {
00119 MatrixType typ = v1.matrix_type ();
00120 SparseMatrix ret = v1.sparse_matrix_value () * v2.diag_matrix_value ();
00121 octave_value out = octave_value (ret);
00122 typ.mark_as_unsymmetric ();
00123 out.matrix_type (typ);
00124 return out;
00125 }
00126 }
00127
00128 DEFBINOP (div_sm_dm, sparse_matrix, diag_matrix)
00129 {
00130 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_diag_matrix&);
00131
00132 if (v2.rows() == 1 && v2.columns() == 1)
00133 {
00134 double d = v2.scalar_value ();
00135
00136 if (d == 0.0)
00137 gripe_divide_by_zero ();
00138
00139 return octave_value (v1.sparse_matrix_value () / d);
00140 }
00141 else
00142 {
00143 MatrixType typ = v2.matrix_type ();
00144 return xdiv (v1.sparse_matrix_value (), v2.diag_matrix_value (), typ);
00145 }
00146 }
00147
00148 DEFBINOP (add_sm_dm, sparse_matrix, diag_matrix)
00149 {
00150 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_diag_matrix&);
00151
00152 if (v1.rows() == 1 && v1.columns() == 1)
00153
00154
00155 {
00156 double d = v1.scalar_value ();
00157
00158 return octave_value (d + v2.matrix_value ());
00159 }
00160 else
00161 return v1.sparse_matrix_value () + v2.diag_matrix_value ();
00162 }
00163
00164 DEFBINOP (sub_sm_dm, sparse_matrix, diag_matrix)
00165 {
00166 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_diag_matrix&);
00167
00168 if (v1.rows() == 1 && v1.columns() == 1)
00169
00170
00171 {
00172 double d = v1.scalar_value ();
00173
00174 return octave_value (d - v2.matrix_value ());
00175 }
00176 else
00177 return v1.sparse_matrix_value () - v2.diag_matrix_value ();
00178 }
00179
00180 void
00181 install_dm_sm_ops (void)
00182 {
00183 INSTALL_BINOP (op_mul, octave_diag_matrix, octave_sparse_matrix,
00184 mul_dm_sm);
00185
00186 INSTALL_BINOP (op_add, octave_diag_matrix, octave_sparse_matrix, add_dm_sm);
00187 INSTALL_BINOP (op_sub, octave_diag_matrix, octave_sparse_matrix, sub_dm_sm);
00188 INSTALL_BINOP (op_ldiv, octave_diag_matrix, octave_sparse_matrix, ldiv_dm_sm);
00189
00190 INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_diag_matrix,
00191 mul_sm_dm);
00192
00193 INSTALL_BINOP (op_add, octave_sparse_matrix, octave_diag_matrix, add_sm_dm);
00194 INSTALL_BINOP (op_sub, octave_sparse_matrix, octave_diag_matrix, sub_sm_dm);
00195 INSTALL_BINOP (op_div, octave_sparse_matrix, octave_diag_matrix, div_sm_dm);
00196 }