<commit_before>/***********************************************
 * Author: <NAME> - <EMAIL>
 * Create: 2017-07-27 14:48
 * Last modified : 2017-07-27 17:14
 * Filename      : MatrixHelper.cpp
 * Description   : 
 **********************************************/

#include "algebra/MatrixHelper.h"
#include "utils/ParallelOperator.h"
#include <cmath>
#include <string.h>

namespace abcdl{
namespace algebra{

template<class T>
void MatrixHelper<T>::dot(Matrix<T>& mat,
						  const Matrix<T>& mat_a,
						  const Matrix<T>& mat_b){
}

template<class T>
void MatrixHelper<T>::outer(Matrix<T>& mat,
							const Matrix<T>& mat_a,
							const Matrix<T>& mat_b){
}

template<class T>
void MatrixHelper<T>::pow(Matrix<T>& mat,
						  const Matrix<T>& mat_a,
						  const T& exponent){
    auto lamda = [](T* a, const T& b, const T& c){*a = std::pow(b, c);};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), exponent, mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::log(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ *a = std::log(b);};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::exp(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ *a = std::exp(std::min(b, (T)EXP_MAX));};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::sigmoid(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ *a = 1 / (1 + std::exp(-(std::min((T)SIGMOID_MAX, std::max(b, (T)SIGMOID_MIN)))));};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::softmax(Matrix<T>& mat, const Matrix<T>& mat_a){
//    auto lamda = [](T* a){ *a = std::log(*a);};
//    po.parallel_mul2one<T>(_data, get_size(), lamda);
}

template<class T>
void MatrixHelper<T>::tanh(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ *a = 2.0 /(1.0 + std::exp(std::min((T)EXP_MAX, -2 * b))) - 1.0;};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::relu(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ if(b < 0) {*a = 0;} else { *a = b;}};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::zero_like(Matrix<T>& mat, const Matrix<T>& mat_a){
	mat.set_data(mat_a.data(), mat_a.rows(), mat_a.cols()); 
}

template<class T>
void transpose(Matrix<T>& mat, const Matrix<T>& mat_a){
	if(&mat != &mat_a){
		mat.set_data(mat_a.data(), mat_a.rows(), mat_b.cols());
	}

	std::size_t rows = mat.rows();
	std::size_t cols = mat.cols();
	if(mat.rows() == 1 || mat.cols() == 1){
		mat.reshape(cols, rows);<commit_msg>initialized<commit_after>/***********************************************
 * Author: <NAME> - <EMAIL>
 * Create: 2017-07-27 14:48
 * Last modified : 2017-07-27 17:14
 * Filename      : MatrixHelper.cpp
 * Description   : 
 **********************************************/

#include "algebra/MatrixHelper.h"
#include "utils/ParallelOperator.h"
#include <cmath>
#include <string.h>

namespace abcdl{
namespace algebra{

template<class T>
void MatrixHelper<T>::dot(Matrix<T>& mat,
						  const Matrix<T>& mat_a,
						  const Matrix<T>& mat_b){
}

template<class T>
void MatrixHelper<T>::outer(Matrix<T>& mat,
							const Matrix<T>& mat_a,
							const Matrix<T>& mat_b){
}

template<class T>
void MatrixHelper<T>::pow(Matrix<T>& mat,
						  const Matrix<T>& mat_a,
						  const T& exponent){
    auto lamda = [](T* a, const T& b, const T& c){*a = std::pow(b, c);};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), exponent, mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::log(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ *a = std::log(b);};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::exp(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ *a = std::exp(std::min(b, (T)EXP_MAX));};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::sigmoid(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ *a = 1 / (1 + std::exp(-(std::min((T)SIGMOID_MAX, std::max(b, (T)SIGMOID_MIN)))));};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::softmax(Matrix<T>& mat, const Matrix<T>& mat_a){
//    auto lamda = [](T* a){ *a = std::log(*a);};
//    po.parallel_mul2one<T>(_data, get_size(), lamda);
}

template<class T>
void MatrixHelper<T>::tanh(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ *a = 2.0 /(1.0 + std::exp(std::min((T)EXP_MAX, -2 * b))) - 1.0;};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::relu(Matrix<T>& mat, const Matrix<T>& mat_a){
    auto lamda = [](T* a, const T& b){ if(b < 0) {*a = 0;} else { *a = b;}};
    po.parallel_mul2one_copy<T>(mat_a.data(), mat_a.get_size(), mat.data(), lamda);
}

template<class T>
void MatrixHelper<T>::zero_like(Matrix<T>& mat, const Matrix<T>& mat_a){
	mat.set_data(mat_a.data(), mat_a.rows(), mat_a.cols()); 
}

template<class T>
void transpose(Matrix<T>& mat, const Matrix<T>& mat_a){
	if(&mat != &mat_a){
		mat.set_data(mat_a.data(), mat_a.rows(), mat_a.cols());
	}

	std::size_t rows = mat.rows();
	std::size_t cols = mat.cols();
	if(mat.rows() == 1 || mat.cols() == 1){
		mat.reshape(cols, rows);
{'fraction_non_alphanumeric': 0.24193548387096775, 'fraction_numerical': 0.015099519560741249, 'mean_word_length': 4.811565304087737, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1978821', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2301, 'n_words': 497}