op-dm-scm.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-cx-diag.h"
00035 #include "ov-re-sparse.h"
00036 #include "ov-cx-sparse.h"
00037 
00038 #include "sparse-xdiv.h"
00039 
00040 // diagonal matrix by sparse matrix ops
00041 
00042 DEFBINOP (mul_dm_scm, diag_matrix, sparse_complex_matrix)
00043 {
00044   CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_complex_matrix&);
00045 
00046   if (v2.rows() == 1 && v2.columns() == 1)
00047     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00048     // a sparse matrix.
00049     {
00050       std::complex<double> d = v2.complex_value ();
00051 
00052       return octave_value (v1.diag_matrix_value () * d);
00053     }
00054   else
00055     {
00056       MatrixType typ = v2.matrix_type ();
00057       SparseComplexMatrix ret = v1.diag_matrix_value () * v2.sparse_complex_matrix_value ();
00058       octave_value out = octave_value (ret);
00059       typ.mark_as_unsymmetric ();
00060       out.matrix_type (typ);
00061       return out;
00062     }
00063 }
00064 
00065 DEFBINOP (mul_cdm_sm, complex_diag_matrix, sparse_matrix)
00066 {
00067   CAST_BINOP_ARGS (const octave_complex_diag_matrix&, const octave_sparse_matrix&);
00068 
00069   if (v2.rows() == 1 && v2.columns() == 1)
00070     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00071     // a sparse matrix.
00072     {
00073       std::complex<double> d = v2.scalar_value ();
00074 
00075       return octave_value (v1.complex_diag_matrix_value () * d);
00076     }
00077   else
00078     {
00079       MatrixType typ = v2.matrix_type ();
00080       SparseComplexMatrix ret = v1.complex_diag_matrix_value () * v2.sparse_matrix_value ();
00081       octave_value out = octave_value (ret);
00082       typ.mark_as_unsymmetric ();
00083       out.matrix_type (typ);
00084       return out;
00085     }
00086 }
00087 
00088 DEFBINOP (mul_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
00089 {
00090   CAST_BINOP_ARGS (const octave_complex_diag_matrix&, const octave_sparse_complex_matrix&);
00091 
00092   if (v2.rows() == 1 && v2.columns() == 1)
00093     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00094     // a sparse matrix.
00095     {
00096       std::complex<double> d = v2.complex_value ();
00097 
00098       return octave_value (v1.complex_diag_matrix_value () * d);
00099     }
00100   else
00101     {
00102       MatrixType typ = v2.matrix_type ();
00103       SparseComplexMatrix ret = v1.complex_diag_matrix_value () * v2.sparse_complex_matrix_value ();
00104       octave_value out = octave_value (ret);
00105       typ.mark_as_unsymmetric ();
00106       out.matrix_type (typ);
00107       return out;
00108     }
00109 }
00110 
00111 DEFBINOP (ldiv_dm_scm, diag_matrix, sparse_complex_matrix)
00112 {
00113   CAST_BINOP_ARGS (const octave_diag_matrix&,
00114                    const octave_sparse_complex_matrix&);
00115 
00116   MatrixType typ = v2.matrix_type ();
00117   return xleftdiv (v1.diag_matrix_value (), v2.sparse_complex_matrix_value (),
00118                    typ);
00119 }
00120 
00121 DEFBINOP (ldiv_cdm_sm, complex_diag_matrix, sparse_matrix)
00122 {
00123   CAST_BINOP_ARGS (const octave_complex_diag_matrix&,
00124                    const octave_sparse_matrix&);
00125 
00126   MatrixType typ = v2.matrix_type ();
00127   return xleftdiv (v1.complex_diag_matrix_value (), v2.sparse_matrix_value (),
00128                    typ);
00129 }
00130 
00131 DEFBINOP (ldiv_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
00132 {
00133   CAST_BINOP_ARGS (const octave_complex_diag_matrix&,
00134                    const octave_sparse_complex_matrix&);
00135 
00136   MatrixType typ = v2.matrix_type ();
00137   return xleftdiv (v1.complex_diag_matrix_value (), v2.sparse_complex_matrix_value (),
00138                    typ);
00139 }
00140 
00141 DEFBINOP (add_dm_scm, diag_matrix, sparse_complex_matrix)
00142 {
00143   CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_complex_matrix&);
00144 
00145   if (v2.rows() == 1 && v2.columns() == 1)
00146     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00147     // a sparse matrix.
00148     {
00149       std::complex<double> d = v2.complex_value ();
00150 
00151       return octave_value (v1.matrix_value () + d);
00152     }
00153   else
00154     return v1.diag_matrix_value () + v2.sparse_complex_matrix_value ();
00155 }
00156 
00157 DEFBINOP (add_cdm_sm, complex_diag_matrix, sparse_matrix)
00158 {
00159   CAST_BINOP_ARGS (const octave_complex_diag_matrix&, const octave_sparse_matrix&);
00160 
00161   if (v2.rows() == 1 && v2.columns() == 1)
00162     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00163     // a sparse matrix.
00164     {
00165       double d = v2.scalar_value ();
00166 
00167       return octave_value (v1.complex_matrix_value () + d);
00168     }
00169   else
00170     return v1.complex_diag_matrix_value () + v2.sparse_matrix_value ();
00171 }
00172 
00173 DEFBINOP (add_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
00174 {
00175   CAST_BINOP_ARGS (const octave_complex_diag_matrix&, const octave_sparse_complex_matrix&);
00176 
00177   if (v2.rows() == 1 && v2.columns() == 1)
00178     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00179     // a sparse matrix.
00180     {
00181       std::complex<double> d = v2.complex_value ();
00182 
00183       return octave_value (v1.complex_matrix_value () + d);
00184     }
00185   else
00186     return v1.complex_diag_matrix_value () + v2.sparse_complex_matrix_value ();
00187 }
00188 
00189 DEFBINOP (sub_dm_scm, diag_matrix, sparse_complex_matrix)
00190 {
00191   CAST_BINOP_ARGS (const octave_diag_matrix&, const octave_sparse_complex_matrix&);
00192 
00193   if (v2.rows() == 1 && v2.columns() == 1)
00194     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00195     // a sparse matrix.
00196     {
00197       std::complex<double> d = v2.complex_value ();
00198 
00199       return octave_value (v1.matrix_value () + (-d));
00200     }
00201   else
00202     return v1.diag_matrix_value () - v2.sparse_complex_matrix_value ();
00203 }
00204 
00205 DEFBINOP (sub_cdm_sm, complex_diag_matrix, sparse_matrix)
00206 {
00207   CAST_BINOP_ARGS (const octave_complex_diag_matrix&, const octave_sparse_matrix&);
00208 
00209   if (v2.rows() == 1 && v2.columns() == 1)
00210     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00211     // a sparse matrix.
00212     {
00213       double d = v2.scalar_value ();
00214 
00215       return octave_value (v1.complex_matrix_value () + (-d));
00216     }
00217   else
00218     return v1.complex_diag_matrix_value () - v2.sparse_matrix_value ();
00219 }
00220 
00221 DEFBINOP (sub_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
00222 {
00223   CAST_BINOP_ARGS (const octave_complex_diag_matrix&, const octave_sparse_complex_matrix&);
00224 
00225   if (v2.rows() == 1 && v2.columns() == 1)
00226     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00227     // a sparse matrix.
00228     {
00229       std::complex<double> d = v2.complex_value ();
00230 
00231       return octave_value (v1.complex_matrix_value () + (-d));
00232     }
00233   else
00234     return v1.complex_diag_matrix_value () - v2.sparse_complex_matrix_value ();
00235 }
00236 
00237 // sparse matrix by diagonal matrix ops
00238 
00239 DEFBINOP (mul_scm_dm, sparse_complex_matrix, diag_matrix)
00240 {
00241   CAST_BINOP_ARGS (const octave_sparse_complex_matrix&, const octave_diag_matrix&);
00242 
00243   if (v1.rows() == 1 && v1.columns() == 1)
00244     // If v1 is a scalar in disguise, return a diagonal matrix rather than
00245     // a sparse matrix.
00246     {
00247       std::complex<double> d = v1.complex_value ();
00248 
00249       return octave_value (d * v2.diag_matrix_value ());
00250     }
00251   else
00252     {
00253       MatrixType typ = v1.matrix_type ();
00254       SparseComplexMatrix ret = v1.sparse_complex_matrix_value () * v2.diag_matrix_value ();
00255       octave_value out = octave_value (ret);
00256       typ.mark_as_unsymmetric ();
00257       out.matrix_type (typ);
00258       return out;
00259     }
00260 }
00261 
00262 DEFBINOP (mul_sm_cdm, sparse_matrix, complex_diag_matrix)
00263 {
00264   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_complex_diag_matrix&);
00265 
00266   if (v1.rows() == 1 && v1.columns() == 1)
00267     // If v1 is a scalar in disguise, return a diagonal matrix rather than
00268     // a sparse matrix.
00269     {
00270       std::complex<double> d = v1.complex_value ();
00271 
00272       return octave_value (d * v2.complex_diag_matrix_value ());
00273     }
00274   else
00275     {
00276       MatrixType typ = v1.matrix_type ();
00277       SparseComplexMatrix ret = v1.sparse_matrix_value () * v2.complex_diag_matrix_value ();
00278       octave_value out = octave_value (ret);
00279       typ.mark_as_unsymmetric ();
00280       out.matrix_type (typ);
00281       return out;
00282     }
00283 }
00284 
00285 DEFBINOP (mul_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
00286 {
00287   CAST_BINOP_ARGS (const octave_sparse_complex_matrix&, const octave_complex_diag_matrix&);
00288 
00289   if (v1.rows() == 1 && v1.columns() == 1)
00290     // If v1 is a scalar in disguise, return a diagonal matrix rather than
00291     // a sparse matrix.
00292     {
00293       std::complex<double> d = v1.complex_value ();
00294 
00295       return octave_value (d * v2.complex_diag_matrix_value ());
00296     }
00297   else if (v2.rows() == 1 && v2.columns() == 1)
00298     // If v2 is a scalar in disguise, don't bother with further dispatching.
00299     {
00300       std::complex<double> d = v2.complex_value ();
00301 
00302       return octave_value (v1.sparse_complex_matrix_value () * d);
00303     }
00304   else
00305     {
00306       MatrixType typ = v1.matrix_type ();
00307       SparseComplexMatrix ret = v1.sparse_complex_matrix_value () * v2.complex_diag_matrix_value ();
00308       octave_value out = octave_value (ret);
00309       typ.mark_as_unsymmetric ();
00310       out.matrix_type (typ);
00311       return out;
00312     }
00313 }
00314 
00315 DEFBINOP (div_scm_dm, sparse_complex_matrix, diag_matrix)
00316 {
00317   CAST_BINOP_ARGS (const octave_sparse_complex_matrix&, const octave_diag_matrix&);
00318 
00319   if (v2.rows() == 1 && v2.columns() == 1)
00320     {
00321       double d = v2.scalar_value ();
00322 
00323       if (d == 0.0)
00324         gripe_divide_by_zero ();
00325 
00326       return octave_value (v1.sparse_complex_matrix_value () / d);
00327     }
00328   else
00329     {
00330       MatrixType typ = v2.matrix_type ();
00331       return xdiv (v1.sparse_complex_matrix_value (), v2.diag_matrix_value (), typ);
00332     }
00333 }
00334 
00335 DEFBINOP (div_sm_cdm, sparse_matrix, complex_diag_matrix)
00336 {
00337   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_complex_diag_matrix&);
00338 
00339   if (v2.rows() == 1 && v2.columns() == 1)
00340     {
00341       std::complex<double> d = v2.complex_value ();
00342 
00343       if (d == 0.0)
00344         gripe_divide_by_zero ();
00345 
00346       return octave_value (v1.sparse_matrix_value () / d);
00347     }
00348   else
00349     {
00350       MatrixType typ = v2.matrix_type ();
00351       return xdiv (v1.sparse_matrix_value (), v2.complex_diag_matrix_value (), typ);
00352     }
00353 }
00354 
00355 DEFBINOP (div_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
00356 {
00357   CAST_BINOP_ARGS (const octave_sparse_complex_matrix&, const octave_complex_diag_matrix&);
00358 
00359   if (v2.rows() == 1 && v2.columns() == 1)
00360     {
00361       std::complex<double> d = v2.complex_value ();
00362 
00363       if (d == 0.0)
00364         gripe_divide_by_zero ();
00365 
00366       return octave_value (v1.sparse_complex_matrix_value () / d);
00367     }
00368   else
00369     {
00370       MatrixType typ = v2.matrix_type ();
00371       return xdiv (v1.sparse_complex_matrix_value (), v2.complex_diag_matrix_value (), typ);
00372     }
00373 }
00374 
00375 DEFBINOP (add_sm_cdm, sparse_matrix, complex_diag_matrix)
00376 {
00377   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_complex_diag_matrix&);
00378 
00379   if (v2.rows() == 1 && v2.columns() == 1)
00380     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00381     // a sparse matrix.
00382     {
00383       std::complex<double> d = v2.complex_value ();
00384 
00385       return octave_value (v1.sparse_matrix_value () + d);
00386     }
00387   else
00388     return v1.sparse_matrix_value () + v2.complex_diag_matrix_value ();
00389 }
00390 
00391 DEFBINOP (add_scm_dm, sparse_complex_matrix, diag_matrix)
00392 {
00393   CAST_BINOP_ARGS (const octave_sparse_complex_matrix&, const octave_diag_matrix&);
00394 
00395   if (v2.rows() == 1 && v2.columns() == 1)
00396     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00397     // a sparse matrix.
00398     {
00399       double d = v2.scalar_value ();
00400 
00401       return octave_value (v1.sparse_complex_matrix_value () + d);
00402     }
00403   else
00404     return v1.sparse_complex_matrix_value () + v2.diag_matrix_value ();
00405 }
00406 
00407 DEFBINOP (add_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
00408 {
00409   CAST_BINOP_ARGS (const octave_sparse_complex_matrix&, const octave_complex_diag_matrix&);
00410 
00411   if (v2.rows() == 1 && v2.columns() == 1)
00412     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00413     // a sparse matrix.
00414     {
00415       std::complex<double> d = v2.complex_value ();
00416 
00417       return octave_value (v1.sparse_complex_matrix_value () + d);
00418     }
00419   else
00420     return v1.sparse_complex_matrix_value () + v2.complex_diag_matrix_value ();
00421 }
00422 
00423 DEFBINOP (sub_sm_cdm, sparse_matrix, complex_diag_matrix)
00424 {
00425   CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_complex_diag_matrix&);
00426 
00427   if (v2.rows() == 1 && v2.columns() == 1)
00428     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00429     // a sparse matrix.
00430     {
00431       std::complex<double> d = v2.complex_value ();
00432 
00433       return octave_value (v1.sparse_matrix_value () + (-d));
00434     }
00435   else
00436     return v1.sparse_matrix_value () - v2.complex_diag_matrix_value ();
00437 }
00438 
00439 DEFBINOP (sub_scm_dm, sparse_complex_matrix, diag_matrix)
00440 {
00441   CAST_BINOP_ARGS (const octave_sparse_complex_matrix&, const octave_diag_matrix&);
00442 
00443   if (v2.rows() == 1 && v2.columns() == 1)
00444     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00445     // a sparse matrix.
00446     {
00447       double d = v2.scalar_value ();
00448 
00449       return octave_value (v1.sparse_complex_matrix_value () + (-d));
00450     }
00451   else
00452     return v1.sparse_complex_matrix_value () - v2.diag_matrix_value ();
00453 }
00454 
00455 DEFBINOP (sub_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
00456 {
00457   CAST_BINOP_ARGS (const octave_sparse_complex_matrix&, const octave_complex_diag_matrix&);
00458 
00459   if (v2.rows() == 1 && v2.columns() == 1)
00460     // If v2 is a scalar in disguise, return a diagonal matrix rather than
00461     // a sparse matrix.
00462     {
00463       std::complex<double> d = v2.complex_value ();
00464 
00465       return octave_value (v1.sparse_complex_matrix_value () + (-d));
00466     }
00467   else
00468     return v1.sparse_complex_matrix_value () - v2.complex_diag_matrix_value ();
00469 }
00470 
00471 void
00472 install_dm_scm_ops (void)
00473 {
00474   INSTALL_BINOP (op_mul, octave_diag_matrix, octave_sparse_complex_matrix,
00475                  mul_dm_scm);
00476   INSTALL_BINOP (op_mul, octave_complex_diag_matrix, octave_sparse_matrix,
00477                  mul_cdm_sm);
00478   INSTALL_BINOP (op_mul, octave_complex_diag_matrix, octave_sparse_complex_matrix,
00479                  mul_cdm_scm);
00480   INSTALL_BINOP (op_ldiv, octave_diag_matrix, octave_sparse_complex_matrix, ldiv_dm_scm);
00481   INSTALL_BINOP (op_ldiv, octave_complex_diag_matrix, octave_sparse_matrix, ldiv_cdm_sm);
00482   INSTALL_BINOP (op_ldiv, octave_complex_diag_matrix, octave_sparse_complex_matrix,
00483                  ldiv_cdm_scm);
00484 
00485   INSTALL_BINOP (op_add, octave_diag_matrix, octave_sparse_complex_matrix, add_dm_scm);
00486   INSTALL_BINOP (op_add, octave_complex_diag_matrix, octave_sparse_matrix, add_cdm_sm);
00487   INSTALL_BINOP (op_add, octave_complex_diag_matrix, octave_sparse_complex_matrix,
00488                  add_cdm_scm);
00489   INSTALL_BINOP (op_sub, octave_diag_matrix, octave_sparse_complex_matrix, sub_dm_scm);
00490   INSTALL_BINOP (op_sub, octave_complex_diag_matrix, octave_sparse_matrix, sub_cdm_sm);
00491   INSTALL_BINOP (op_sub, octave_complex_diag_matrix, octave_sparse_complex_matrix,
00492                  sub_cdm_scm);
00493 
00494   INSTALL_BINOP (op_mul, octave_sparse_complex_matrix, octave_diag_matrix,
00495                  mul_scm_dm);
00496   INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_complex_diag_matrix,
00497                  mul_sm_cdm);
00498   INSTALL_BINOP (op_mul, octave_sparse_complex_matrix, octave_complex_diag_matrix,
00499                  mul_scm_cdm);
00500 
00501   INSTALL_BINOP (op_div, octave_sparse_complex_matrix, octave_diag_matrix, div_scm_dm);
00502   INSTALL_BINOP (op_div, octave_sparse_matrix, octave_complex_diag_matrix, div_sm_cdm);
00503   INSTALL_BINOP (op_div, octave_sparse_complex_matrix, octave_complex_diag_matrix, div_scm_cdm);
00504 
00505   INSTALL_BINOP (op_add, octave_sparse_complex_matrix, octave_diag_matrix, add_scm_dm);
00506   INSTALL_BINOP (op_add, octave_sparse_matrix, octave_complex_diag_matrix, add_sm_cdm);
00507   INSTALL_BINOP (op_add, octave_sparse_complex_matrix, octave_complex_diag_matrix, add_scm_cdm);
00508   INSTALL_BINOP (op_sub, octave_sparse_complex_matrix, octave_diag_matrix, sub_scm_dm);
00509   INSTALL_BINOP (op_sub, octave_sparse_matrix, octave_complex_diag_matrix, sub_sm_cdm);
00510   INSTALL_BINOP (op_sub, octave_sparse_complex_matrix, octave_complex_diag_matrix, sub_scm_cdm);
00511 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines