op-s-sm.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2004-2012 David Bateman
00004 Copyright (C) 1998-2004 Andy Adler
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include "gripes.h"
00029 #include "oct-obj.h"
00030 #include "ov.h"
00031 #include "ov-typeinfo.h"
00032 #include "ov-scalar.h"
00033 #include "ops.h"
00034 #include "xpow.h"
00035 
00036 #include "sparse-xpow.h"
00037 #include "sparse-xdiv.h"
00038 #include "ov-re-sparse.h"
00039 
00040 // scalar by sparse matrix ops.
00041 
00042 DEFBINOP_OP (add, scalar, sparse_matrix, +)
00043 DEFBINOP_OP (sub, scalar, sparse_matrix, -)
00044 DEFBINOP_OP (mul, scalar, sparse_matrix, *)
00045 
00046 DEFBINOP (div, scalar, sparse_matrix)
00047 {
00048   CAST_BINOP_ARGS (const octave_scalar&, const octave_sparse_matrix&);
00049 
00050   if (v2.rows() == 1 && v2.columns() == 1)
00051     {
00052       double d = v2.scalar_value ();
00053 
00054       if (d == 0.0)
00055         gripe_divide_by_zero ();
00056 
00057       return octave_value (SparseMatrix (1, 1, v1.scalar_value () / d));
00058     }
00059   else
00060     {
00061       MatrixType typ = v2.matrix_type ();
00062       Matrix m1 = Matrix (1, 1, v1.double_value ());
00063       SparseMatrix m2 = v2.sparse_matrix_value ();
00064       Matrix ret = xdiv (m1, m2, typ);
00065       v2.matrix_type (typ);
00066       return ret;
00067     }
00068 }
00069 
00070 DEFBINOP (pow, scalar, sparse_matrix)
00071 {
00072   CAST_BINOP_ARGS (const octave_scalar&, const octave_sparse_matrix&);
00073   return xpow (v1.scalar_value (), v2.matrix_value ());
00074 }
00075 
00076 DEFBINOP (ldiv, scalar, sparse_matrix)
00077 {
00078   CAST_BINOP_ARGS (const octave_scalar&, const octave_sparse_matrix&);
00079 
00080   double d = v1.double_value ();
00081   octave_value retval;
00082 
00083   if (d == 0.0)
00084     gripe_divide_by_zero ();
00085 
00086   retval = octave_value (v2.sparse_matrix_value () / d);
00087 
00088   return retval;
00089 }
00090 
00091 DEFBINOP_FN (lt, scalar, sparse_matrix, mx_el_lt)
00092 DEFBINOP_FN (le, scalar, sparse_matrix, mx_el_le)
00093 DEFBINOP_FN (eq, scalar, sparse_matrix, mx_el_eq)
00094 DEFBINOP_FN (ge, scalar, sparse_matrix, mx_el_ge)
00095 DEFBINOP_FN (gt, scalar, sparse_matrix, mx_el_gt)
00096 DEFBINOP_FN (ne, scalar, sparse_matrix, mx_el_ne)
00097 
00098 DEFBINOP_OP (el_mul, scalar, sparse_matrix, *)
00099 DEFBINOP_FN (el_div, scalar, sparse_matrix, x_el_div)
00100 DEFBINOP_FN (el_pow, scalar, sparse_matrix, elem_xpow)
00101 
00102 DEFBINOP (el_ldiv, scalar, sparse_matrix)
00103 {
00104   CAST_BINOP_ARGS (const octave_scalar&, const octave_sparse_matrix&);
00105 
00106   double d = v1.double_value ();
00107   octave_value retval;
00108 
00109   if (d == 0.0)
00110     gripe_divide_by_zero ();
00111 
00112   retval = octave_value (v2.sparse_matrix_value () / d);
00113 
00114   return retval;
00115 }
00116 
00117 DEFBINOP_FN (el_and, scalar, sparse_matrix, mx_el_and)
00118 DEFBINOP_FN (el_or,  scalar, sparse_matrix, mx_el_or)
00119 
00120 DEFCATOP (s_sm, scalar, sparse_matrix)
00121 {
00122   CAST_BINOP_ARGS (octave_scalar&, const octave_sparse_matrix&);
00123   SparseMatrix tmp (1, 1, v1.scalar_value ());
00124   return octave_value (tmp.concat (v2.sparse_matrix_value (), ra_idx));
00125 }
00126 
00127 DEFCONV (sparse_matrix_conv, scalar, sparse_matrix)
00128 {
00129   CAST_CONV_ARG (const octave_scalar&);
00130 
00131   return new octave_sparse_matrix (SparseMatrix (v.matrix_value ()));
00132 }
00133 
00134 void
00135 install_s_sm_ops (void)
00136 {
00137   INSTALL_BINOP (op_add, octave_scalar, octave_sparse_matrix, add);
00138   INSTALL_BINOP (op_sub, octave_scalar, octave_sparse_matrix, sub);
00139   INSTALL_BINOP (op_mul, octave_scalar, octave_sparse_matrix, mul);
00140   INSTALL_BINOP (op_div, octave_scalar, octave_sparse_matrix, div);
00141   INSTALL_BINOP (op_pow, octave_scalar, octave_sparse_matrix, pow);
00142   INSTALL_BINOP (op_ldiv, octave_scalar, octave_sparse_matrix, ldiv);
00143   INSTALL_BINOP (op_lt, octave_scalar, octave_sparse_matrix, lt);
00144   INSTALL_BINOP (op_le, octave_scalar, octave_sparse_matrix, le);
00145   INSTALL_BINOP (op_eq, octave_scalar, octave_sparse_matrix, eq);
00146   INSTALL_BINOP (op_ge, octave_scalar, octave_sparse_matrix, ge);
00147   INSTALL_BINOP (op_gt, octave_scalar, octave_sparse_matrix, gt);
00148   INSTALL_BINOP (op_ne, octave_scalar, octave_sparse_matrix, ne);
00149   INSTALL_BINOP (op_el_mul, octave_scalar, octave_sparse_matrix, el_mul);
00150   INSTALL_BINOP (op_el_div, octave_scalar, octave_sparse_matrix, el_div);
00151   INSTALL_BINOP (op_el_pow, octave_scalar, octave_sparse_matrix, el_pow);
00152   INSTALL_BINOP (op_el_ldiv, octave_scalar, octave_sparse_matrix, el_ldiv);
00153   INSTALL_BINOP (op_el_and, octave_scalar, octave_sparse_matrix, el_and);
00154   INSTALL_BINOP (op_el_or, octave_scalar, octave_sparse_matrix, el_or);
00155 
00156   INSTALL_CATOP (octave_scalar, octave_sparse_matrix, s_sm);
00157 
00158   INSTALL_ASSIGNCONV (octave_scalar, octave_sparse_matrix, octave_matrix);
00159 
00160   INSTALL_WIDENOP (octave_scalar, octave_sparse_matrix, sparse_matrix_conv);
00161 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines