23 #ifndef CPP_INCLUDE_CPU_OPERATIONS_H_ 24 #define CPP_INCLUDE_CPU_OPERATIONS_H_ 110 std::cerr <<
"MATRICIES ARE EMPTY";
115 return (a.array() + scalar);
134 if ((a.rows() != b.rows()) || (a.cols() != b.cols())) {
135 std::cerr <<
"MATRICIES ARE NOT THE SAME SIZE";
139 }
else if (a.rows() == 0) {
140 std::cerr <<
"MATRICIES ARE EMPTY";
150 if (a.rows() == 0 || a.cols() == 0) {
151 std::cerr <<
"EMPTY MATRIX AS ARGUEMENT!";
154 return (a.array() - scalar);
158 if ((a.rows() != b.rows()) || (a.cols() != b.cols())) {
159 std::cerr <<
"MATRICES ARE NOT THE SAME SIZE!";
161 }
else if (b.rows() == 0 || b.cols() == 0 || a.rows() == 0
163 std::cerr <<
"EMPTY MATRIX AS ARGUMENT!";
182 if ((a.rows() != b.rows()) || (a.cols() != b.cols())) {
183 std::cerr <<
"MATRICES ARE NOT THE SAME SIZE!";
185 }
else if (b.rows() == 0 || b.cols() == 0 || a.rows() == 0
187 std::cerr <<
"EMPTY MATRIX AS ARGUMENT!";
190 return (a.array() || b.array());
204 for (r = 0; r < b.rows(); ++r) {
205 for (
int c = 0; c < b.cols(); ++c) {
209 if (b.rows() == 0 || b.cols() == 0) {
210 std::cerr <<
"EMPTY MATRIX AS ARGUMENT!";
228 if ((a.rows() != b.rows()) || (a.cols() != b.cols())) {
229 std::cerr <<
"MATRICES ARE NOT THE SAME SIZE!";
232 return (a.array() && b.array());
246 std::cerr <<
"MATRIX IS EMPTY";
249 }
else if (a.cols() != a.rows()) {
250 std::cerr <<
"MATRIX IS NOT A SQUARE MATRIX!";
254 }
else if (a.determinant() == 0) {
255 std::cerr <<
"MATRIX DOES NOT HAVE AN INVERSE (DETERMINANT IS ZERO)!";
284 const int &axis = 0) {
285 int num_rows = a.rows();
286 int num_cols = a.cols();
287 float norm_value = 0;
290 for (
int j = 0; j < num_cols; j++) {
291 for (
int i = 0; i < num_rows; i++)
292 norm_value += pow(a(i, j), p);
293 norm(j) = pow(norm_value, (1.0/p));
297 }
else if (axis == 1) {
299 for (
int i = 0; i < num_rows; i++) {
300 for (
int j = 0; j < num_cols; j++)
301 norm_value += pow(a(i, j), p);
302 norm(i) = pow(norm_value, (1.0/p));
307 std::cerr <<
"Axis must be zero or one!";
334 if (a.rows() == 0 || a.cols() == 0) {
335 std::cerr <<
"EMPTY MATRIX AS ARGUMENT!";
368 if (a.size() != b.size()) {
369 std::cerr <<
"VECTORS ARE NOT THE SAME SIZE!";
375 }
else if (a.size() == 0) {
376 std::cerr <<
"VECTORS ARE EMPTY!";
396 if (a.size() == 0 || b.size() == 0) {
397 std::cerr <<
"EMPTY VECTOR AS ARGUMENT!";
400 return a * b.transpose();
414 if ((a.rows() != b.rows()) || (a.cols() != b.cols())) {
415 std::cerr <<
"MATRICES ARE NOT THE SAME SIZE!";
418 return (a.array() && b.array());
433 if (a.size() != b.size()) {
434 std::cerr <<
"VECTORS ARE NOT THE SAME SIZE!";
436 }
else if (a.size() == 0 || b.size() == 0) {
437 std::cerr <<
"EMPTY VECTOR AS ARGUMENT!";
440 return (a.array() || b.array());
454 for (i = 0; i < b.size(); ++i) {
458 std::cerr <<
"EMPTY VECTOR AS ARGUMENT!";
478 const int &axis = 0) {
479 int num_rows = a.rows();
480 int num_cols = a.cols();
483 b = a.transpose().array().colwise() /
Norm(a, p, axis).array();
484 return b.transpose();
485 }
else if (axis == 1) {
486 b = a.array().colwise() /
Norm(a, p, axis).array();
489 std::cerr <<
"Axis must be zero or one!";
507 const float constant = 1.0) {
508 int num_samples = data_matrix.rows();
510 Matrix<T> kernel_matrix(num_samples, num_samples);
512 float sigma = constant;
514 for (
int i = 0; i < num_samples; i++)
515 for (
int j = 0; j < num_samples; j++) {
517 float i_j_dist = (data_matrix.row(i) - data_matrix.row(j)).norm();
518 kernel_matrix(i, j) = exp(-i_j_dist / (2 * sigma * sigma));
521 return kernel_matrix;
535 Matrix<T> °ree_matrix_to_the_minus_half) {
537 Vector<T> d_i = kernel_matrix.rowwise().sum();
538 degree_matrix = d_i.asDiagonal();
540 degree_matrix_to_the_minus_half = d_i.array().sqrt().unaryExpr(
541 std::ptr_fun(util::reciprocal<T>)).matrix().asDiagonal();
545 #endif // CPP_INCLUDE_CPU_OPERATIONS_H_
static Vector< T > Transpose(const Vector< T > &a)
Definition: cpu_operations.h:60
KernelType
Definition: kernel_types.h:27
static T Determinant(const Matrix< T > &a)
static void GenDegreeMatrix(const Matrix< T > &kernel_matrix, Matrix< T > °ree_matrix, Matrix< T > °ree_matrix_to_the_minus_half)
Definition: cpu_operations.h:532
static Matrix< T > Multiply(const Matrix< T > &a, const T &scalar)
Definition: cpu_operations.h:74
static Vector< bool > LogicalOr(const Vector< bool > &a, const Vector< bool > &b)
Definition: cpu_operations.h:430
static Matrix< bool > LogicalAnd(const Matrix< bool > &a, const Matrix< bool > &b)
Definition: cpu_operations.h:226
static Matrix< T > Add(const Matrix< T > &a, const T &scalar)
Definition: cpu_operations.h:107
Definition: cpu_operations.h:36
Definition: kernel_types.h:28
static Matrix< T > Subtract(const Matrix< T > &a, const T &scalar)
Definition: cpu_operations.h:148
int Rank(const Matrix< T > &a)
Definition: svd_solver.h:96
Definition: svd_solver.h:36
static T FrobeniusNorm(const Matrix< T > &a)
Definition: cpu_operations.h:333
static Matrix< T > OuterProduct(const Vector< T > &a, const Vector< T > &b)
Definition: cpu_operations.h:394
static Matrix< bool > LogicalOr(const Matrix< bool > &a, const Matrix< bool > &b)
Definition: cpu_operations.h:179
static Matrix< T > Transpose(const Matrix< T > &a)
Definition: cpu_operations.h:49
static Matrix< bool > LogicalNot(const Matrix< bool > &a)
Definition: cpu_operations.h:200
static T DotProduct(const Vector< T > &a, const Vector< T > &b)
Definition: cpu_operations.h:366
Definition: cpu_operations.h:40
static T Trace(const Matrix< T > &a)
Definition: cpu_operations.h:351
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
Definition: vector.h:31
static Matrix< T > Normalize(const Matrix< T > &a, const int &p=2, const int &axis=0)
Definition: cpu_operations.h:477
static Matrix< T > GenKernelMatrix(const Matrix< T > &data_matrix, const KernelType kernel_type=kGaussianKernel, const float constant=1.0)
Definition: cpu_operations.h:504
static Vector< T > Norm(const Matrix< T > &a, const int &p=2, const int &axis=0)
Definition: cpu_operations.h:282
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
Definition: matrix.h:31
static Matrix< T > Add(const Matrix< T > &a, const Matrix< T > &b)
Definition: cpu_operations.h:132
static Vector< bool > LogicalAnd(const Vector< T > &a, const Vector< T > &b)
Definition: cpu_operations.h:413
static Matrix< T > Subtract(const Matrix< T > &a, const Matrix< T > &b)
Definition: cpu_operations.h:156
static int Rank(const Matrix< T > &a)
Definition: cpu_operations.h:321
static Matrix< T > Multiply(const Matrix< T > &a, const Matrix< T > &b)
Definition: cpu_operations.h:89
static Vector< bool > LogicalNot(const Vector< bool > &a)
Definition: cpu_operations.h:450
static Matrix< T > Inverse(const Matrix< T > &a)
Definition: cpu_operations.h:243