00001 /*
00002 File: SubMat.cc
00003
00004 Function: Implements SubMat.h
00005
00006 Author(s): Andrew Willmott
00007
00008 Copyright: (c) 1995-2000, Andrew Willmott
00009
00010 Notes:
00011
00012 */
00013
00014
00015 #include "vl/SubMat.h"
00016 #include "vl/Mat.h"
00017
00018
00019 // --- SubMat Constructors & Destructors --------------------------------------
00020
00021
00022 TSubMat::TSubMat(Int m, Int n, Int span, TMReal data[]) :
00023 rows(m), cols(n),
00024 span(span), data(data)
00025 {
00026 }
00027
00028 TSubMat::TSubMat(const TSubMat &m) :
00029 rows(m.rows), cols(m.cols),
00030 span(m.span), data(m.data)
00031 {
00032 }
00033
00034
00035 // --- SubMat Assignment Operators --------------------------------------------
00036
00037
00038 TSubMat &TSubMat::operator = (const TSubMat &m)
00039 {
00040 Assert(Rows() == m.Rows(), "(Mat::=) Matrix rows don't match");
00041 for (Int i = 0; i < Rows(); i++)
00042 SELF[i] = m[i];
00043
00044 return(SELF);
00045 }
00046
00047 TSubMat &TSubMat::operator = (const TMat &m)
00048 {
00049 Assert(Rows() == m.Rows(), "(Mat::=) Matrix rows don't match");
00050 for (Int i = 0; i < Rows(); i++)
00051 SELF[i] = m[i];
00052
00053 return(SELF);
00054 }
00055
00056
00057 // --- Sub functions: Mat ------------------------------------------------------
00058
00059
00060 TSubMat sub(const TMat &m, Int top, Int left, Int height, Int width)
00061 {
00062 Assert(left >= 0 && width > 0 && left + width <= m.Cols(), "(sub(Mat)) illegal subset of matrix");
00063 Assert(top >= 0 && height > 0 && top + height <= m.Rows(), "(sub(Mat)) illegal subset of matrix");
00064
00065 TSubMat result(height, width, m.Cols(), m.Ref() + top * m.Cols() + left);
00066
00067 return(result);
00068 }
00069
00070 TSubMat sub(const TMat &m, Int nrows, Int ncols)
00071 {
00072 Assert(ncols > 0 && nrows > 0 && nrows <= m.Rows() && ncols <= m.Cols(),
00073 "(sub(Mat)) illegal subset of matrix");
00074
00075 TSubMat result(nrows, ncols, m.Cols(), m.Ref());
00076
00077 return(result);
00078 }
00079
00080 TMSubVec col(const TMat &m, Int i)
00081 {
00082 CheckRange(i, 0, m.Cols(), "(col(Mat)) illegal column index");
00083
00084 return(TMSubVec(m.Rows(), m.Cols(), m.Ref() + i));
00085 }
00086
00087 TMSubVec row(const TMat &m, Int i)
00088 {
00089 CheckRange(i, 0, m.Rows(), "(row(Mat)) illegal row index");
00090
00091 return(TMSubVec(m.Cols(), 1, m[i].Ref()));
00092 }
00093
00094 TMSubVec diag(const TMat &m, Int diagNum)
00095 {
00096 CheckRange(diagNum, 1 - m.Rows(), m.Cols(), "(row(Mat)) illegal row index");
00097
00098 if (diagNum == 0)
00099 return(TMSubVec(Min(m.Rows(), m.Cols()), m.Cols() + 1, m.Ref()));
00100 else if (diagNum < 0)
00101 return(TMSubVec(Min(m.Rows() + diagNum, m.Cols()), m.Cols() + 1,
00102 m.Ref() - diagNum * m.Cols()));
00103 else
00104 return(TMSubVec(Min(m.Cols() - diagNum, m.Rows()), m.Cols() + 1,
00105 m.Ref() + diagNum));
00106 }
00107
00108 // --- Sub functions: SubMat ---------------------------------------------------
00109
00110
00111 TSubMat sub(const TSubMat &m, Int top, Int left, Int height, Int width)
00112 {
00113 Assert(left >= 0 && width > 0 && left + width <= m.Cols(),
00114 "(sub(SubMat)) illegal subset of matrix");
00115 Assert(top >= 0 && height > 0 && top + height <= m.Rows(),
00116 "(sub(SubMat)) illegal subset of matrix");
00117
00118 TSubMat result(height, width, m.span, m.data + top * m.span + left);
00119
00120 return(result);
00121 }
00122
00123 TSubMat sub(const TSubMat &m, Int nrows, Int ncols)
00124 {
00125 Assert(ncols > 0 && nrows > 0 && nrows <= m.Rows() && ncols <= m.Cols(),
00126 "(sub(SubMat)) illegal subset of matrix");
00127
00128 TSubMat result(nrows, ncols, m.span, m.data);
00129
00130 return(result);
00131 }
00132
00133 TMSubVec col(const TSubMat &m, Int i)
00134 {
00135 CheckRange(i, 0, m.Cols(), "(col(SubMat)) illegal column index");
00136
00137 return(TMSubVec(m.rows, m.span, m.data + i));
00138 }
00139
00140 TMSubVec row(const TSubMat &m, Int i)
00141 {
00142 CheckRange(i, 0, m.Rows(), "(row(SubMat)) illegal row index");
00143
00144 return(TMSubVec(m.cols, 1, m.data + i * m.span));
00145 }
00146
00147 TMSubVec diag(const TSubMat &m, Int diagNum)
00148 {
00149 CheckRange(diagNum, 1 - m.Rows(), m.Cols(), "(row(Mat)) illegal row index");
00150
00151 if (diagNum == 0)
00152 return(TMSubVec(Min(m.rows, m.cols), m.span + 1, m.data));
00153 else if (diagNum < 0)
00154 return(TMSubVec(Min(m.rows + diagNum, m.cols), m.span + 1,
00155 m.data - diagNum * m.span));
00156 else
00157 return(TMSubVec(Min(m.cols - diagNum, m.rows), m.span + 1,
00158 m.data + diagNum));
00159 }