bsxfun.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2012 Jordi GutiƩrrez Hermoso <jordigh@octave.org>
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 #if !defined (bsxfun_h)
00023 #define bsxfun_h 1
00024 
00025 #include <algorithm>
00026 
00027 #include "Array.h"
00028 #include "dim-vector.h"
00029 #include "lo-error.h"
00030 
00031 inline
00032 bool
00033 is_valid_bsxfun (const std::string& name, const dim_vector& dx,
00034                  const dim_vector& dy)
00035 {
00036   for (int i = 0; i < std::min (dx.length (), dy.length ()); i++)
00037     {
00038       octave_idx_type xk = dx(i), yk = dy(i);
00039       // Check the three conditions for valid bsxfun dims
00040       if (! ( (xk == yk) || (xk == 1 && yk > 1) || (xk > 1 && yk == 1)))
00041         return false;
00042     }
00043 
00044   (*current_liboctave_warning_with_id_handler)
00045     ("Octave:broadcast", "%s: automatic broadcasting operation applied", name.c_str ());
00046 
00047   return true;
00048 }
00049 
00050 // since we can't change the size of the assigned-to matrix, we cannot
00051 // apply singleton expansion to it, so the conditions to check are
00052 // different here.
00053 inline
00054 bool
00055 is_valid_inplace_bsxfun (const std::string& name, const dim_vector& dr,
00056                          const dim_vector& dx)
00057 {
00058   octave_idx_type drl = dr.length (), dxl = dx.length ();
00059   if (drl < dxl)
00060     return false;
00061 
00062   for (int i = 0; i < drl; i++)
00063     {
00064       octave_idx_type rk = dr(i), xk = dx(i);
00065 
00066       // Only two valid canditions to check; can't stretch rk
00067       if (! ( (rk == xk) || (rk > 1 && xk == 1)))
00068         return false;
00069     }
00070 
00071   (*current_liboctave_warning_with_id_handler)
00072     ("Octave:broadcast", "%s: automatic broadcasting operation applied", name.c_str ());
00073 
00074   return true;
00075 }
00076 
00077 #include "bsxfun-defs.cc"
00078 
00079 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines