A lite library for operations in linear algebra

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MATRIX_LIB.h Source File

MATRIX_LIB.h

00001 #ifndef MATRIX_LIB_H
00002 #define MATRIX_LIB_H
00003 //
00004 #include <vector>
00005 #include <math.h>
00006 
00007 // For printing matrix out
00008 ////////////////////////
00009 #include <sstream>
00010 #include <string>
00011 //////////////////////// end For printing matrix out
00012 
00013 
00014 #include "MATRIX_PRIMITIVE.h"
00015 
00016 using std::vector;
00017 // using std::string;
00018 
00019 //
00020 namespace MP = MATRIX_PRIMITIVE;
00021 //
00022 
00023 /*
00024 // Debugging
00025 //////////////////
00026 #include <iostream>
00027 using std::cout;
00028 ////////////////// end Debugging
00029 */
00030 
00031 class Mat{
00032 public:
00033     // Matrix data
00034     vector<vector<float> > data; // data is a m by n matrix
00035 
00036 
00037     // Create a nRow_in by nCol_in matrix
00038     Mat(void); // No pre-assignments, empty matrix
00039     Mat(size_t nRow_in, size_t nCol_in); //
00040     Mat(size_t nRow_in, size_t nCol_in, float element_in); // Pre-assign elements
00041 
00042     // Public methods
00043     //----------------------------------//
00044     // Size ---
00045     // "Get" the size of the matrix
00046     size_t nRow();
00047     size_t nCol();
00048     size_t nElement(); // Number of elements
00049     // const functions
00050     size_t nRow() const;
00051     size_t nCol() const;
00052     size_t nElement() const; // Number of elements
00053     // "Set" and "get" the new size of the matrix
00054     size_t nRow(size_t nRow_in);
00055     size_t nCol(size_t nCol_in);
00056     // "Set" and "get" the new size of the matrix with assignment to new elements
00057     size_t nRow(size_t nRow_in, float element_in);
00058     size_t nCol(size_t nCol_in, float element_in);
00059     // Resize on both row and column
00060     void resize(size_t nRow_in, size_t nCol_in);
00061     void resize(size_t nRow_in, size_t nCol_in, float element_in); // Assign the new element_in to new elements
00062     // Reshape the matrix, keep all the elements but change the shpae of the matrix (eg. 1 by 6 -> 2 by 3 or 3 by 2)
00063     void reshape(size_t nRow_in, size_t n_Col_in, bool is_Row_first); // If is_Row_first, keep the elements' order the same in the row direction; otherwise, keep the order the same in column direction.
00064     // Synchronize the _nRow and _nCol with the true size of data
00065     void syncSizeInData();
00066 
00067     // Assignment ---
00068     void assign(std::stringstream &ss_in, size_t nRow_in, size_t nCol_in); // The stringstream ss_in contains a nRow_in by nCol_in matrix.
00069     void assign(float* Matrix_in, size_t nRow_in, size_t nCol_in); // From primitive 1-D array in c++, Matrix_in is a nRow_in by nCol_in matrix.
00070     void assign(const vector<float> &vec_in, bool is_RowVec); // From 1-D vector
00071     void assign(const vector<vector<float> > &MatData_in); // A = MatData_in, assign a primitive matrix directly
00072     // Partial asignment
00073     void setPart(const Mat &M_part, size_t m_from, size_t n_from); // The block starts from (m_from, n_from)
00074     // Spetial assignments
00075     void zeros(size_t nRow_in, size_t nCol_in); // zeros(m,n)
00076     void ones(size_t nRow_in, size_t nCol_in); // ones(m,n)
00077     void eye(size_t nRow_in, size_t nCol_in); // Identity matrix, eye(m,n)
00078     void diag(const Mat &M_diag); // Transform the row/column vector to a diagonal matrix and assign into this matrix
00079 
00080     // Get elements ---
00081     float at(size_t m, size_t n); // Get the element at (m,n)
00082     Mat getPart(size_t m_from, size_t m_to, size_t n_from, size_t n_to); // Get partial, M_part = M(m_from:m_to, n_from:n_to)
00083     Mat getCol(size_t n); // Get a specific column vector in 2-D matrix form
00084     Mat getRow(size_t m); // Get a specific row vector in 2-D matrix form
00085     vector<float> getColVec(size_t n); // Return a c++ vector, M(:,n)
00086     vector<float> getRowVec(size_t m); // Return a c++ vector, M(m,:)
00087 
00088     // Print out matrix ---
00089     std::string print(void); // Print this matrix out as string
00090 
00091 
00092 
00093     // Operations =====
00094 
00095     // Comparison
00096     bool is_equal(const Mat &M_in);
00097 
00098     // Self-operation (affecting on this matrix) ---
00099     void scaleUp(float scale); // M *= scale
00100     void scaleUp_Mat(const Mat &M_right); // M = M.times(M_right), element-wise multiplication
00101     //
00102     void increase(float scale); // M += scale, for each element in M
00103     void decrease(float scale); // M -= scale, for each element in M
00104     //
00105     void increase(const Mat &M_right); // M += M_right
00106     void decrease(const Mat &M_right); // M -= M_right
00107 
00108 
00109     // Single-operation, output another matrix ---
00110     Mat& T(void); // Transpose, return the transposed version of this matrix
00111     Mat& inverse(void); // Inverse, return the inversion of this matrix
00112     Mat& intPower(int power); // M^power, M^0 -> I, M^-1 -> M_inverse
00113 
00114     // Duo-operation, output another matrix ---
00115     // Plus
00116     Mat& plus(float scale); // (M + scale), for each element in M
00117     Mat& minus(float scale); // (M - scale), for each element in M
00118     Mat& minus(float scale, bool is_reversed); // is_reversed -> (scale - M), for each element in M
00119     Mat& plus(const Mat &M_right);
00120     Mat& minus(const Mat &M_right);
00121     // Concatenation
00122     Mat cat_below(const Mat &M_in); // Below this matrix, [A; B]
00123     Mat cat_right(const Mat &M_in); // Right-side of this matrix, [A, B]
00124     Mat cat(const Mat &M_in, bool is_horizontal); // is_horizontal --> cat_Right(); otherwise --> cat_Below()
00125 
00126 
00127     // Scalar/Element-wise multiplication
00128     Mat& times(float scale); // Scalar multiplication
00129     Mat& times(const Mat &M_right); // Element-wise multiplication
00130 
00131     // Matrix multiplication
00132     // Note: this matrix is the "left" one
00133     Mat& dot(const Mat &M_right); // Similar to the nomenclature of numpy in Python
00134     Mat& dot(bool Transpose_left, const Mat &M_right, bool Transpose_right); // Extended version for conbining the ability of transpose of both mtrices
00135 
00136     // Operator overloading
00137     //----------------------------//
00138     // A <- b, assignment
00139     Mat& operator = (float scale); // Assign a real number as 1 by 1 matrix
00140     Mat& operator = (const vector<float> &colVec_in); // A = vec_in, assign as a column vector
00141     Mat& operator = (const vector<vector<float> > &MatData_in); // A = MatData_in, assign a primitive matrix directly
00142     // b <- A, get value, using implicit type conversion
00143     // Note: no implicit conversion to float, since it would be ambiguous in other operator overriding
00144     explicit operator float (); // Get the first element as float, good for a 1 by 1 matrix
00145     explicit operator std::vector<float> (); // Get the column vector as std::vector<float> in c++
00146     // A[]
00147     vector<float>& operator [] (size_t m); // Indexing the m-th row, equivalent to data[m]
00148     // A == B
00149     bool operator == (Mat const& B); // is_equal()
00150     // A^z, z \in Z
00151     Mat& operator ^ (int power); // A^power, this->intPower()
00152 
00153     //----------------------------//
00154     // end Operator overloading
00155 private:
00156 
00157     size_t _nRow; // Number of rows
00158     size_t _nCol; // Number of columns
00159 
00160 
00161     // Private methods
00162     // void get_maxBound(size_t &M_max, size_t &N_max, size_t M_new, size_t N_new); // Cauculate the approproate size to contain all the matrices
00163 
00164 };
00165 
00166 // Operator overloading
00167 //---------------------------------------//
00168 
00169 
00170 // -A
00171 Mat& operator - (const Mat &A);
00172 // A + B
00173 Mat& operator + (float a, const Mat &B);
00174 Mat& operator + (const Mat &A, float b);
00175 Mat& operator + (const Mat &A, const Mat &B);
00176 // A - B
00177 Mat& operator - (float a, const Mat &B);
00178 Mat& operator - (const Mat &A, float b);
00179 Mat& operator - (const Mat &A, const Mat &B);
00180 // A * B
00181 Mat& operator * (float a, const Mat &B);
00182 Mat& operator * (const Mat &A, float b);
00183 Mat& operator * (const Mat &A, const Mat &B); // Matrix multiplication
00184 // A/B, including matrix inversion (eg, (1.0/B) <--> B^-1)
00185 Mat& operator / (float a, const Mat &B); // a*(B^-1), for that B to be inversed
00186 Mat& operator / (const Mat &A, float b); // A*(1/b), scalar multiplication of 1/b
00187 Mat& operator / (const Mat &A, const Mat &B); // A*(B^-1), for that B to be inversed
00188 // A += B
00189 Mat& operator += (Mat &A, float b);
00190 Mat& operator += (Mat &A, const Mat &B);
00191 // A -= B
00192 Mat& operator -= (Mat &A, float b);
00193 Mat& operator -= (Mat &A, const Mat &B);
00194 // A *= B
00195 Mat& operator *= (Mat &A, float b);
00196 Mat& operator *= (Mat &A, const Mat &B); // Matrix multiplication
00197 
00198 //---------------------------------------//
00199 // end Operator overloading
00200 
00201 #endif