Skip to content

Commit

Permalink
Solved issue codezonediitj#10
Browse files Browse the repository at this point in the history
  • Loading branch information
bits2zbytes committed Mar 19, 2020
1 parent 9b6750a commit 29902d2
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 114 deletions.
37 changes: 0 additions & 37 deletions adaboost/core/data_structures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,43 +184,6 @@ namespace adaboost
~Matrix();
};

/* @overload
* Used for taking dot product of two vectors.
*
* @param vec1 First vector in dot product.
* @param vec2 Second vector in dot product.
* @param result For storing the result.
*/
template <class data_type_vector>
void product(const Vector<data_type_vector>& vec1,
const Vector<data_type_vector>& vec2,
data_type_vector& result);

/* @overload
* Used for multiplying a vector
* and a matrix.
*
* @param vec The vector.
* @param mat The matrix.
* @param result A vector for storing the result.
*/
template <class data_type_vector, class data_type_matrix>
void multiply(const Vector<data_type_vector>& vec,
const Matrix<data_type_matrix>& mat,
Vector<data_type_vector>& result);

/* @overload
* Used for multiplyng two matrices.
*
* @param vec1 First matrix.
* @param vec2 Second matrix.
* @param result A matrix for storing the result.
*/
template <class data_type_matrix>
void multiply(const Matrix<data_type_matrix>& mat1,
const Matrix<data_type_matrix>& mat2,
Matrix<data_type_matrix>& result);

} // namespace core
} // namespace adaboost

Expand Down
77 changes: 0 additions & 77 deletions adaboost/core/data_structures_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ namespace adaboost
this->data[index] = value;
}

template <class data_type_vector>
void Vector<data_type_vector>::
fill(data_type_vector value)
{
for(unsigned int i = 0; i < this->size; i++)
{
this->data[i] = value;
}
}

template <class data_type_vector>
unsigned int Vector<data_type_vector>::get_size() const
{
Expand Down Expand Up @@ -133,19 +123,6 @@ namespace adaboost
this->data[x*this->cols + y] = value;
}

template <class data_type_matrix>
void Matrix<data_type_matrix>::
fill(data_type_matrix value)
{
for(unsigned int i = 0; i < this->rows; i++)
{
for(unsigned int j = 0; j < this->cols; j++)
{
this->data[i*this->cols + j] = value;
}
}
}

template <class data_type_matrix>
unsigned int Matrix<data_type_matrix>::
get_rows() const
Expand Down Expand Up @@ -176,60 +153,6 @@ namespace adaboost
}
}

template <class data_type_vector>
void product(const Vector<data_type_vector>& vec1,
const Vector<data_type_vector>& vec2,
data_type_vector& result)
{
adaboost::utils::check(vec1.get_size() == vec2.get_size(),
"Size of vectors don't match.");
result = 0;
for(unsigned int i = 0; i < vec1.get_size(); i++)
{
result += (vec1.at(i)*vec2.at(i));
}
}

template <class data_type_vector, class data_type_matrix>
void multiply(const Vector<data_type_vector>& vec,
const Matrix<data_type_matrix>& mat,
Vector<data_type_vector>& result)
{
adaboost::utils::check(vec.get_size() == mat.get_rows(),
"Orders mismatch in the inputs.");
for(unsigned int j = 0; j < mat.get_cols(); j++)
{
data_type_vector _result = 0;
for(unsigned int i = 0; i < mat.get_rows(); i++)
{
_result += (vec.at(i)*mat.at(i, j));
}
result.set(j, _result);
}
}

template <class data_type_matrix>
void multiply(const Matrix<data_type_matrix>& mat1,
const Matrix<data_type_matrix>& mat2,
Matrix<data_type_matrix>& result)
{
adaboost::utils::check(mat1.get_cols() == mat2.get_rows(),
"Order of matrices don't match.");
unsigned int common_cols = mat1.get_cols();
for(unsigned int i = 0; i < result.get_rows(); i++)
{
for(unsigned int j = 0; j < result.get_cols(); j++)
{
data_type_matrix _result = 0;
for(unsigned int k = 0; k < common_cols; k++)
{
_result += (mat1.at(i, k)*mat2.at(k, j));
}
result.set(i, j, _result);
}
}
}

#include "instantiated_templates_data_structures.hpp"

} // namespace core
Expand Down
38 changes: 38 additions & 0 deletions adaboost/core/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,44 @@ namespace adaboost
const Vector<data_type_1>& vec,
data_type_1& result);

/*
* @overload
* Used for taking dot product of two vectors.
*
* @param vec1 First vector in dot product.
* @param vec2 Second vector in dot product.
* @param result For storing the result.
*/
template <class data_type_vector>
void product(const Vector<data_type_vector>& vec1,
const Vector<data_type_vector>& vec2,
data_type_vector& result);

/* @overload
* Used for multiplyng two matrices.
*
* @param vec1 First matrix.
* @param vec2 Second matrix.
* @param result A matrix for storing the result.
*/
template <class data_type_matrix>
void multiply(const Matrix<data_type_matrix>& mat1,
const Matrix<data_type_matrix>& mat2,
Matrix<data_type_matrix>& result);

/* @overload
* Used for multiplying a vector
* and a matrix.
*
* @param vec The vector.
* @param mat The matrix.
* @param result A vector for storing the result.
*/
template <class data_type_vector, class data_type_matrix>
void multiply(const Vector<data_type_vector>& vec,
const Matrix<data_type_matrix>& mat,
Vector<data_type_vector>& result);

} // namespace core
} // namespace adaboost

Expand Down
80 changes: 80 additions & 0 deletions adaboost/core/operations_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,86 @@ namespace adaboost
result = arg_max;
}

template <class data_type_vector>
void Vector<data_type_vector>::
fill(data_type_vector value)
{
for(unsigned int i = 0; i < this->size; i++)
{
this->data[i] = value;
}
}


template <class data_type_matrix>
void Matrix<data_type_matrix>::
fill(data_type_matrix value)
{
for(unsigned int i = 0; i < this->rows; i++)
{
for(unsigned int j = 0; j < this->cols; j++)
{
this->data[i*this->cols + j] = value;
}
}
}


template <class data_type_vector, class data_type_matrix>
void multiply(const Vector<data_type_vector>& vec,
const Matrix<data_type_matrix>& mat,
Vector<data_type_vector>& result)
{
adaboost::utils::check(vec.get_size() == mat.get_rows(),
"Orders mismatch in the inputs.");
for(unsigned int j = 0; j < mat.get_cols(); j++)
{
data_type_vector _result = 0;
for(unsigned int i = 0; i < mat.get_rows(); i++)
{
_result += (vec.at(i)*mat.at(i, j));
}
result.set(j, _result);
}
}

template <class data_type_matrix>
void multiply(const Matrix<data_type_matrix>& mat1,
const Matrix<data_type_matrix>& mat2,
Matrix<data_type_matrix>& result)
{
adaboost::utils::check(mat1.get_cols() == mat2.get_rows(),
"Order of matrices don't match.");
unsigned int common_cols = mat1.get_cols();
for(unsigned int i = 0; i < result.get_rows(); i++)
{
for(unsigned int j = 0; j < result.get_cols(); j++)
{
data_type_matrix _result = 0;
for(unsigned int k = 0; k < common_cols; k++)
{
_result += (mat1.at(i, k)*mat2.at(k, j));
}
result.set(i, j, _result);
}
}
}

template <class data_type_vector>
void product(const Vector<data_type_vector>& vec1,
const Vector<data_type_vector>& vec2,
data_type_vector& result)
{
adaboost::utils::check(vec1.get_size() == vec2.get_size(),
"Size of vectors don't match.");
result = 0;
for(unsigned int i = 0; i < vec1.get_size(); i++)
{
result += (vec1.at(i)*vec2.at(i));
}
}


#include "instantiated_templates_operations.hpp"

} // namespace core
Expand Down

0 comments on commit 29902d2

Please sign in to comment.