op-sm-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-re-mat.h"
00033 #include "ov-null-mat.h"
00034 #include "ops.h"
00035 
00036 #include "sparse-xpow.h"
00037 #include "sparse-xdiv.h"
00038 #include "ov-re-sparse.h"
00039 
00040 // sparse matrix unary ops.
00041 
00042 DEFUNOP_OP (not, sparse_matrix, !)
00043 DEFUNOP_OP (uplus, sparse_matrix, /* no-op */)
00044 DEFUNOP_OP (uminus, sparse_matrix, -)
00045 
00046 DEFUNOP (transpose, sparse_matrix)
00047 {
00048   CAST_UNOP_ARG (const octave_sparse_matrix&);
00049   return octave_value (v.sparse_matrix_value().transpose (),
00050                        v.matrix_type ().transpose ());
00051 }
00052 
00053 // sparse matrix by sparse matrix ops.
00054 
00055 DEFBINOP_OP (add, sparse_matrix, sparse_matrix, +)
00056 
00057 // DEFBINOP_OP (sub, sparse_matrix, sparse_matrix, -)
00058 
00059   static octave_value
00060   oct_binop_sub (const octave_base_value& a1, const octave_base_value& a2)
00061   {
00062     const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
00063     const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
00064     SparseMatrix m = v1.sparse_matrix_value () - v2.sparse_matrix_value ();
00065 
00066     return octave_value (m);
00067   }
00068 
00069 DEFBINOP_OP (mul, sparse_matrix, sparse_matrix, *)
00070 
00071 DEFBINOP (div, sparse_matrix, sparse_matrix)
00072 {
00073   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&);
00074 
00075   if (v2.rows() == 1 && v2.columns() == 1)
00076     {
00077       double d = v2.scalar_value ();
00078 
00079       if (d == 0.0)
00080         gripe_divide_by_zero ();
00081 
00082       return octave_value (v1.sparse_matrix_value () / d);
00083     }
00084   else
00085     {
00086       MatrixType typ = v2.matrix_type ();
00087       SparseMatrix ret = xdiv (v1.sparse_matrix_value (),
00088                                v2.sparse_matrix_value (), typ);
00089 
00090       v2.matrix_type (typ);
00091       return ret;
00092     }
00093 }
00094 
00095 DEFBINOPX (pow, sparse_matrix, sparse_matrix)
00096 {
00097   error ("can't do A ^ B for A and B both matrices");
00098   return octave_value ();
00099 }
00100 
00101 DEFBINOP (ldiv, sparse_matrix, sparse_matrix)
00102 {
00103   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&);
00104 
00105   if (v1.rows() == 1 && v1.columns() == 1)
00106     {
00107       double d = v1.double_value ();
00108 
00109       if (d == 0.0)
00110         gripe_divide_by_zero ();
00111 
00112       return octave_value (v2.sparse_matrix_value () / d);
00113     }
00114   else
00115     {
00116       MatrixType typ = v1.matrix_type ();
00117 
00118       SparseMatrix ret = xleftdiv (v1.sparse_matrix_value (),
00119                                    v2.sparse_matrix_value (), typ);
00120 
00121       v1.matrix_type (typ);
00122       return ret;
00123     }
00124 }
00125 
00126 DEFBINOP_FN (lt, sparse_matrix, sparse_matrix, mx_el_lt)
00127 DEFBINOP_FN (le, sparse_matrix, sparse_matrix, mx_el_le)
00128 DEFBINOP_FN (eq, sparse_matrix, sparse_matrix, mx_el_eq)
00129 DEFBINOP_FN (ge, sparse_matrix, sparse_matrix, mx_el_ge)
00130 DEFBINOP_FN (gt, sparse_matrix, sparse_matrix, mx_el_gt)
00131 DEFBINOP_FN (ne, sparse_matrix, sparse_matrix, mx_el_ne)
00132 
00133 DEFBINOP_FN (el_mul, sparse_matrix, sparse_matrix, product)
00134 DEFBINOP_FN (el_div, sparse_matrix, sparse_matrix, quotient)
00135 
00136 DEFBINOP_FN (el_pow, sparse_matrix, sparse_matrix, elem_xpow)
00137 
00138 DEFBINOP (el_ldiv, sparse_matrix, sparse_matrix)
00139 {
00140   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&);
00141   return octave_value
00142     (quotient (v2.sparse_matrix_value (), v1.sparse_matrix_value ()));
00143 }
00144 
00145 DEFBINOP_FN (el_and, sparse_matrix, sparse_matrix, mx_el_and)
00146 DEFBINOP_FN (el_or,  sparse_matrix, sparse_matrix, mx_el_or)
00147 
00148 DEFCATOP_FN (sm_sm, sparse_matrix, sparse_matrix, concat)
00149 
00150 DEFASSIGNOP_FN (assign, sparse_matrix, sparse_matrix, assign)
00151 
00152 DEFNULLASSIGNOP_FN (null_assign, sparse_matrix, delete_elements)
00153 
00154 void
00155 install_sm_sm_ops (void)
00156 {
00157   INSTALL_UNOP (op_not, octave_sparse_matrix, not);
00158   INSTALL_UNOP (op_uplus, octave_sparse_matrix, uplus);
00159   INSTALL_UNOP (op_uminus, octave_sparse_matrix, uminus);
00160   INSTALL_UNOP (op_transpose, octave_sparse_matrix, transpose);
00161   INSTALL_UNOP (op_hermitian, octave_sparse_matrix, transpose);
00162 
00163   INSTALL_BINOP (op_add, octave_sparse_matrix, octave_sparse_matrix, add);
00164   INSTALL_BINOP (op_sub, octave_sparse_matrix, octave_sparse_matrix, sub);
00165   INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_sparse_matrix, mul);
00166   INSTALL_BINOP (op_div, octave_sparse_matrix, octave_sparse_matrix, div);
00167   INSTALL_BINOP (op_pow, octave_sparse_matrix, octave_sparse_matrix, pow);
00168   INSTALL_BINOP (op_ldiv, octave_sparse_matrix, octave_sparse_matrix, ldiv);
00169   INSTALL_BINOP (op_lt, octave_sparse_matrix, octave_sparse_matrix, lt);
00170   INSTALL_BINOP (op_le, octave_sparse_matrix, octave_sparse_matrix, le);
00171   INSTALL_BINOP (op_eq, octave_sparse_matrix, octave_sparse_matrix, eq);
00172   INSTALL_BINOP (op_ge, octave_sparse_matrix, octave_sparse_matrix, ge);
00173   INSTALL_BINOP (op_gt, octave_sparse_matrix, octave_sparse_matrix, gt);
00174   INSTALL_BINOP (op_ne, octave_sparse_matrix, octave_sparse_matrix, ne);
00175   INSTALL_BINOP (op_el_mul, octave_sparse_matrix, octave_sparse_matrix,
00176                  el_mul);
00177   INSTALL_BINOP (op_el_div, octave_sparse_matrix, octave_sparse_matrix,
00178                  el_div);
00179   INSTALL_BINOP (op_el_pow, octave_sparse_matrix, octave_sparse_matrix,
00180                  el_pow);
00181   INSTALL_BINOP (op_el_ldiv, octave_sparse_matrix, octave_sparse_matrix,
00182                  el_ldiv);
00183   INSTALL_BINOP (op_el_and, octave_sparse_matrix, octave_sparse_matrix,
00184                  el_and);
00185   INSTALL_BINOP (op_el_or, octave_sparse_matrix, octave_sparse_matrix,
00186                  el_or);
00187 
00188   INSTALL_CATOP (octave_sparse_matrix, octave_sparse_matrix, sm_sm);
00189 
00190   INSTALL_ASSIGNOP (op_asn_eq, octave_sparse_matrix, octave_sparse_matrix,
00191                     assign);
00192 
00193   INSTALL_ASSIGNOP (op_asn_eq, octave_sparse_matrix, octave_null_matrix, null_assign);
00194   INSTALL_ASSIGNOP (op_asn_eq, octave_sparse_matrix, octave_null_str, null_assign);
00195   INSTALL_ASSIGNOP (op_asn_eq, octave_sparse_matrix, octave_null_sq_str, null_assign);
00196 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines