op-pm-sm.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2009-2012 Jason Riedy
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-perm.h"
00034 #include "ov-re-sparse.h"
00035 #include "ov-bool-sparse.h"
00036 
00037 // Unary permutation ops, some cast to sparse
00038 
00039 //Avoid casting to a full matrix
00040 DEFUNOP_OP (uplus, perm_matrix, /* no-op */)
00041 
00042 // Not calling standard CAST_UNOP_ARG for these next two because a
00043 // dynamic_cast would fail.
00044 DEFUNOP (not, perm_matrix)
00045 {
00046   // Obviously negation of a permutation matrix destroys sparsity
00047   return octave_value ( ! a.bool_array_value ());
00048 }
00049 
00050 DEFUNOP (uminus, perm_matrix)
00051 {
00052   return octave_value ( - a.sparse_matrix_value ());
00053 }
00054 
00055 // Most other logical operations cast to SparseBoolMatrix
00056 DEFBINOP (eq_pm, perm_matrix, perm_matrix)
00057 {
00058   CAST_BINOP_ARGS (const octave_perm_matrix&, const octave_perm_matrix&);
00059   return v1.sparse_bool_matrix_value () == v2.sparse_bool_matrix_value ();
00060 }
00061 DEFBINOP (ne_pm, perm_matrix, perm_matrix)
00062 {
00063   CAST_BINOP_ARGS (const octave_perm_matrix&, const octave_perm_matrix&);
00064   return v1.sparse_bool_matrix_value () != v2.sparse_bool_matrix_value ();
00065 }
00066 DEFBINOP (el_and_pm, perm_matrix, perm_matrix)
00067 {
00068   CAST_BINOP_ARGS (const octave_perm_matrix&, const octave_perm_matrix&);
00069   return mx_el_and(v1.sparse_bool_matrix_value (),
00070                    v2.sparse_bool_matrix_value ());
00071 }
00072 DEFBINOP (el_or_pm,  perm_matrix, perm_matrix)
00073 {
00074   CAST_BINOP_ARGS (const octave_perm_matrix&, const octave_perm_matrix&);
00075   return mx_el_or(v1.sparse_bool_matrix_value (),
00076                   v2.sparse_bool_matrix_value ());
00077 }
00078 
00079 // permutation matrix by sparse matrix ops
00080 
00081 DEFBINOP (mul_pm_sm, perm_matrix, sparse_matrix)
00082 {
00083   CAST_BINOP_ARGS (const octave_perm_matrix&, const octave_sparse_matrix&);
00084 
00085   if (v2.rows() == 1 && v2.columns() == 1)
00086     {
00087       double d = v2.scalar_value ();
00088 
00089       return octave_value (v1.sparse_matrix_value () * d);
00090     }
00091   else if (v1.rows() == 1 && v1.columns() == 1)
00092     return octave_value (v2.sparse_matrix_value ());
00093   else
00094     return v1.perm_matrix_value  () * v2.sparse_matrix_value ();
00095 }
00096 
00097 DEFBINOP (ldiv_pm_sm, perm_matrix, sparse_matrix)
00098 {
00099   CAST_BINOP_ARGS (const octave_perm_matrix&, const octave_sparse_matrix&);
00100 
00101   return v1.perm_matrix_value ().inverse () * v2.sparse_matrix_value ();
00102 }
00103 
00104 // sparse matrix by diagonal matrix ops
00105 
00106 DEFBINOP (mul_sm_pm, sparse_matrix, perm_matrix)
00107 {
00108   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_perm_matrix&);
00109 
00110   if (v1.rows() == 1 && v1.columns() == 1)
00111     {
00112       double d = v1.scalar_value ();
00113 
00114       return octave_value (d * v2.sparse_matrix_value ());
00115     }
00116   else if (v2.rows() == 1 && v2.columns() == 1)
00117     return octave_value (v1.sparse_matrix_value ());
00118   else
00119     return v1.sparse_matrix_value  () * v2.perm_matrix_value ();
00120 }
00121 
00122 DEFBINOP (div_sm_pm, sparse_matrix, perm_matrix)
00123 {
00124   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_perm_matrix&);
00125 
00126   return v1.sparse_matrix_value () * v2.perm_matrix_value ().inverse ();
00127 }
00128 
00129 void
00130 install_pm_sm_ops (void)
00131 {
00132   INSTALL_UNOP (op_not, octave_perm_matrix, not);
00133   INSTALL_UNOP (op_uplus, octave_perm_matrix, uplus);
00134   INSTALL_UNOP (op_uminus, octave_perm_matrix, uminus);
00135 
00136 
00137   INSTALL_BINOP (op_mul, octave_perm_matrix, octave_sparse_matrix,
00138                  mul_pm_sm);
00139   INSTALL_BINOP (op_ldiv, octave_perm_matrix, octave_sparse_matrix,
00140                  ldiv_pm_sm);
00141   INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_perm_matrix,
00142                  mul_sm_pm);
00143   INSTALL_BINOP (op_div, octave_sparse_matrix, octave_perm_matrix,
00144                  div_sm_pm);
00145 
00146   INSTALL_BINOP (op_eq, octave_perm_matrix, octave_perm_matrix, eq_pm);
00147   INSTALL_BINOP (op_ne, octave_perm_matrix, octave_perm_matrix, ne_pm);
00148   INSTALL_BINOP (op_el_and, octave_perm_matrix, octave_perm_matrix, el_and_pm);
00149   INSTALL_BINOP (op_el_or,  octave_perm_matrix, octave_perm_matrix, el_or_pm);
00150 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines