Skip to content

Commit

Permalink
Merge pull request #2731 from lambday/develop
Browse files Browse the repository at this point in the history
Fix for issue #2729
  • Loading branch information
lambday committed Feb 20, 2015
2 parents 17b4490 + 1c79aec commit 2e45f03
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 141 deletions.
4 changes: 2 additions & 2 deletions src/shogun/kernel/GaussianARDKernel.cpp
Expand Up @@ -29,7 +29,7 @@ void CGaussianARDKernel::init()
SG_ADD(&m_width, "width", "Kernel width", MS_AVAILABLE, GRADIENT_AVAILABLE);
}

#ifdef HAVE_CXX11
#ifdef HAVE_LINALG_LIB
#include <shogun/mathematics/linalg/linalg.h>
CGaussianARDKernel::CGaussianARDKernel(int32_t size, float64_t width)
: CLinearARDKernel(size)
Expand Down Expand Up @@ -122,4 +122,4 @@ float64_t CGaussianARDKernel::distance(int32_t idx_a, int32_t idx_b)
float64_t result=compute_helper(avec, avec);
return result/m_width;
}
#endif /* HAVE_CXX11 */
#endif /* HAVE_LINALG_LIB */
8 changes: 4 additions & 4 deletions src/shogun/kernel/GaussianARDKernel.h
Expand Up @@ -33,7 +33,7 @@ namespace shogun
*
* where \f$\tau\f$ is the kernel width.
*
* There are three variants based on \f$||\cdot||\f$.
* There are three variants based on \f$||\cdot||\f$.
* The default case is
* \f$\sum_{i=1}^{p}{{[\lambda \times ({\bf x_i}-{\bf y_i})] }^2}\f$
* where \f$\lambda\f$ is a scalar and \f$p\f$ is # of features
Expand All @@ -50,7 +50,7 @@ namespace shogun
* The last case is
* \f$({\bf x}-{\bf y})^T \Lambda^T \Lambda ({\bf x}-{\bf y})\f$
* where \f$\Lambda\f$ is a \f$d\f$-by-\f$p\f$ matrix,
* \f$p\f$ is # of features and \f$d\f$ can be \f$ d \ge p\f$ or \f$ d \le p\f$
* \f$p\f$ is # of features and \f$d\f$ can be \f$ d \ge p\f$ or \f$ d \le p\f$
* To use this case, please call set_matrix_weights(\f$\Lambda\f$)
* where \f$\Lambda\f$ is a \f$d\f$-by-\f$p\f$ matrix
*
Expand Down Expand Up @@ -91,7 +91,7 @@ class CGaussianARDKernel: public CLinearARDKernel
/** kernel width */
float64_t m_width;

#ifdef HAVE_CXX11
#ifdef HAVE_LINALG_LIB
public:
/** constructor
*
Expand Down Expand Up @@ -160,7 +160,7 @@ class CGaussianARDKernel: public CLinearARDKernel
*/
virtual float64_t distance(int32_t idx_a, int32_t idx_b);

#endif /* HAVE_CXX11 */
#endif /* HAVE_LINALG_LIB */
};
}
#endif /* _GAUSSIANARDKERNEL_H_ */
8 changes: 4 additions & 4 deletions src/shogun/kernel/LinearARDKernel.cpp
Expand Up @@ -32,7 +32,7 @@ void CLinearARDKernel::init()
GRADIENT_AVAILABLE);
}

#ifdef HAVE_CXX11
#ifdef HAVE_LINALG_LIB
#include <shogun/mathematics/linalg/linalg.h>

CLinearARDKernel::CLinearARDKernel(int32_t size) : CDotKernel(size)
Expand Down Expand Up @@ -78,7 +78,7 @@ SGMatrix<float64_t> CLinearARDKernel::compute_right_product(SGVector<float64_t>r
right=SGMatrix<float64_t>(right_vec.vector,right_vec.vlen,1,false);
scalar_weight*=m_weights[0];
}
else
else
{
right=SGMatrix<float64_t> (m_weights.num_rows,1);

Expand Down Expand Up @@ -110,7 +110,7 @@ float64_t CLinearARDKernel::compute_helper(SGVector<float64_t> avec, SGVector<fl
left=SGMatrix<float64_t>(avec.vector,1,avec.vlen,false);
scalar_weight=m_weights[0];
}
else
else
{
left=SGMatrix<float64_t>(1,m_weights.num_rows);

Expand Down Expand Up @@ -288,4 +288,4 @@ void CLinearARDKernel::set_matrix_weights(SGMatrix<float64_t> weights)
{
set_weights(weights);
}
#endif //HAVE_CXX11
#endif //HAVE_LINALG_LIB
8 changes: 4 additions & 4 deletions src/shogun/kernel/LinearARDKernel.h
Expand Up @@ -36,7 +36,7 @@ enum EARDKernelType
* k({\bf x},{\bf y})= \frac{||{\bf x}-{\bf y}||}
* \f]
*
* There are three variants based on \f$||\cdot||\f$.
* There are three variants based on \f$||\cdot||\f$.
* The default case is
* \f$\sum_{i=1}^{p}{{[\lambda \times ({\bf x_i}-{\bf y_i})] }^2}\f$
* where \f$\lambda\f$ is a scalar and \f$p\f$ is # of features
Expand All @@ -53,7 +53,7 @@ enum EARDKernelType
* The last case is
* \f$({\bf x}-{\bf y})^T \Lambda^T \Lambda ({\bf x}-{\bf y})\f$
* where \f$\Lambda\f$ is a \f$d\f$-by-\f$p\f$ matrix,
* \f$p\f$ is # of features and \f$d\f$ can be \f$ d \ge p\f$ or \f$ d \le p\f$
* \f$p\f$ is # of features and \f$d\f$ can be \f$ d \ge p\f$ or \f$ d \le p\f$
* To use this case, please call set_matrix_weights(\f$\Lambda\f$),
* where \f$\Lambda\f$ is a \f$d\f$-by-\f$p\f$ matrix
*
Expand Down Expand Up @@ -105,7 +105,7 @@ class CLinearARDKernel: public CDotKernel
/** type of ARD kernel */
EARDKernelType m_ARD_type;

#ifdef HAVE_CXX11
#ifdef HAVE_LINALG_LIB
public:
/** constructor
*
Expand Down Expand Up @@ -219,7 +219,7 @@ class CLinearARDKernel: public CDotKernel
*/
virtual SGMatrix<float64_t> compute_right_product(SGVector<float64_t>right_vec, float64_t & scalar_weight);

#endif /* HAVE_CXX11 */
#endif /* HAVE_LINALG_LIB */
};
}
#endif /* _LINEARARDKERNEL_H_ */
80 changes: 43 additions & 37 deletions src/shogun/mathematics/linalg/internal/implementation/Add.h
Expand Up @@ -64,7 +64,7 @@ struct add
{
/** Scalar type */
typedef typename Matrix::Scalar T;

/**
* Performs the operation C = alpha*A + beta*B. Works for both matrices and vectors
* @param A first matrix
Expand All @@ -77,11 +77,12 @@ struct add
};

/**
* @brief Specialization of add for the Native backend
* @brief Partial specialization of add for the Native backend
*/
template <> template <class Matrix>
template <class Matrix>
struct add<Backend::NATIVE, Matrix>
{
/** Scalar type */
typedef typename Matrix::Scalar T;

/**
Expand All @@ -101,7 +102,7 @@ struct add<Backend::NATIVE, Matrix>
"Matrices should have same number of columns!\n");
compute(A.matrix, B.matrix, C.matrix, alpha, beta, A.num_rows*A.num_cols);
}

/**
* Performs the operation C = alpha*A + beta*B.
* @param A first vector
Expand All @@ -126,100 +127,105 @@ struct add<Backend::NATIVE, Matrix>
* @param beta constant to be multiplied by the second vector
* @param len length of the vectors/matrices
*/
static void compute(T* A, T* B, T* C,
T alpha, T beta, index_t len)
static void compute(T* A, T* B, T* C, T alpha, T beta, index_t len)
{
for (int32_t i=0; i<len; i++)
C[i]=alpha*A[i]+beta*B[i];
}
}
};

#ifdef HAVE_EIGEN3

/**
* @brief Specialization of add for the Eigen3 backend
/**
* @brief Partial specialization of add for the Eigen3 backend
*/
template <> template <class Matrix>
template <class Matrix>
struct add<Backend::EIGEN3, Matrix>
{
/** Scalar type */
typedef typename Matrix::Scalar T;

/** Eigen3 matrix type */
typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> MatrixXt;

/** Eigen3 vector type */
typedef Eigen::Matrix<T,Eigen::Dynamic,1> VectorXt;
/**

/**
* Performs the operation C = alpha*A + beta*B using Eigen3
* @param A first matrix
* @param B second matrix
* @param C matrix to store the result
* @param alpha constant to be multiplied by the first matrix
* @param beta constant to be multiplied by the second matrix
*/
static void compute(SGMatrix<T> A, SGMatrix<T> B, SGMatrix<T> C,
static void compute(SGMatrix<T> A, SGMatrix<T> B, SGMatrix<T> C,
T alpha, T beta)
{
Eigen::Map<MatrixXt> A_eig = A;
Eigen::Map<MatrixXt> B_eig = B;
Eigen::Map<MatrixXt> C_eig = C;
C_eig = alpha*A_eig + beta*B_eig;
Eigen::Map<MatrixXt> A_eig=A;
Eigen::Map<MatrixXt> B_eig=B;
Eigen::Map<MatrixXt> C_eig=C;

C_eig=alpha*A_eig+beta*B_eig;
}
/**

/**
* Performs the operation C = alpha*A + beta*B using Eigen3
* @param A first vector
* @param B second vector
* @param C vector to store the result
* @param alpha constant to be multiplied by the first vector
* @param beta constant to be multiplied by the second vector
*/
static void compute(SGVector<T> A, SGVector<T> B, SGVector<T> C,
static void compute(SGVector<T> A, SGVector<T> B, SGVector<T> C,
T alpha, T beta)
{
Eigen::Map<VectorXt> A_eig = A;
Eigen::Map<VectorXt> B_eig = B;
Eigen::Map<VectorXt> C_eig = C;
C_eig = alpha*A_eig + beta*B_eig;
Eigen::Map<VectorXt> A_eig=A;
Eigen::Map<VectorXt> B_eig=B;
Eigen::Map<VectorXt> C_eig=C;

C_eig=alpha*A_eig+beta*B_eig;
}
};
#endif // HAVE_EIGEN3

#ifdef HAVE_VIENNACL

/**
* @brief Specialization of add for the ViennaCL backend
/**
* @brief Partial specialization of add for the ViennaCL backend
*/
template <> template <class Matrix>
template <class Matrix>
struct add<Backend::VIENNACL, Matrix>
{
/** Scalar type */
typedef typename Matrix::Scalar T;
/**

/**
* Performs the operation C = alpha*A + beta*B using Viennacl
* @param A first matrix
* @param B second matrix
* @param C matrix to store the result
* @param alpha constant to be multiplied by the first matrix
* @param beta constant to be multiplied by the second matrix
*/
static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C,
static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C,
T alpha, T beta)
{
C.vcl_matrix() = alpha*A.vcl_matrix() + beta*B.vcl_matrix();
C.vcl_matrix()=alpha*A.vcl_matrix()+beta*B.vcl_matrix();
}
/**

/**
* Performs the operation C = alpha*A + beta*B using Viennacl
* @param A first vector
* @param B second vector
* @param C vector to store the result
* @param alpha constant to be multiplied by the first vector
* @param beta constant to be multiplied by the second vector
*/
static void compute(CGPUVector<T> A, CGPUVector<T> B, CGPUVector<T> C,
static void compute(CGPUVector<T> A, CGPUVector<T> B, CGPUVector<T> C,
T alpha, T beta)
{
C.vcl_vector() = alpha*A.vcl_vector() + beta*B.vcl_vector();
C.vcl_vector()=alpha*A.vcl_vector()+beta*B.vcl_vector();
}
};

Expand Down

0 comments on commit 2e45f03

Please sign in to comment.