op-dm-sm.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2009-2012 Jason Riedy, Jaroslav Hajek
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
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 // diagonal matrix by sparse matrix ops
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     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00046     // a sparse matrix.
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     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00077     // a sparse matrix.
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     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00093     // a sparse matrix.
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 // sparse matrix by diagonal matrix ops
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     // If v1 is a scalar in disguise, return a diagonal matrix rather than
00111     // a sparse matrix.
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     // If v1 is a scalar in disguise, return a diagonal matrix rather than
00154     // a sparse matrix.
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     // If v1 is a scalar in disguise, return a diagonal matrix rather than
00170     // a sparse matrix.
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 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines