inline void load(std::ifstream& ifs, MatD& params){ Real val = 0.0; for (int i = 0; i < params.cols(); ++i){ for (int j = 0; j < params.rows(); ++j){ ifs.read((char*)&val, sizeof(Real)); params.coeffRef(j, i) = val; } } }
inline void save(std::ofstream& ofs, const MatD& params){ Real val = 0.0; for (int i = 0; i < params.cols(); ++i){ for (int j = 0; j < params.rows(); ++j){ val = params.coeff(j, i); ofs.write((char*)&val, sizeof(Real)); } } }
inline Real cosDis(const MatD& a, const MatD& b){ return (a.array()*b.array()).sum()/(a.norm()*b.norm()); //return a.col(0).dot(b.col(0))/(a.norm()*b.norm()); }
static const size_t N = 9; typedef cppmat::tiny::array<double,2,M,N> Arr; // ================================================================================================= TEST_CASE("cppmat::tiny::array", "matrix.h") { // ================================================================================================= // arithmetic // ================================================================================================= SECTION( "array += array" ) { MatD a = MatD::Random(M,N); MatD b = MatD::Random(M,N); Arr A = Arr::Copy(a.data(), a.data()+a.size()); Arr B = Arr::Copy(b.data(), b.data()+b.size()); for ( size_t i = 0 ; i < M ; ++i ) for ( size_t j = 0 ; j < N ; ++j ) a(i,j) += b(i,j); A += B; Equal(A, a); } // -------------------------------------------------------------------------------------------------
typedef cppmat::tiny::diagonal ::matrix<double,M,N> dMat; typedef cppmat::tiny::symmetric::matrix<double,M,N> sMat; typedef cppmat::tiny ::matrix<double,M,N> Mat; // ================================================================================================= TEST_CASE("cppmat::tiny::diagonal::matrix", "matrix.h") { // ================================================================================================= // arithmetic // ================================================================================================= SECTION( "matrix += matrix" ) { MatD a = makeDiagonal(MatD::Random(M,N)); MatD b = makeDiagonal(MatD::Random(M,N)); dMat A = dMat::CopyDense(a.data(), a.data()+a.size()); dMat B = dMat::CopyDense(b.data(), b.data()+b.size()); for ( size_t i = 0 ; i < M ; ++i ) for ( size_t j = 0 ; j < N ; ++j ) a(i,j) += b(i,j); A += B; Equal(A, a); } // -------------------------------------------------------------------------------------------------
//f'(x) = f(x)(1-f(x)) inline MatD ActFunc::logisticPrime(const MatD& x){ return x.array()*(1.0-x.array()); }
static const size_t N = 11; typedef cppmat::symmetric::matrix<double> sMat; // ================================================================================================= TEST_CASE("cppmat::symmetric::matrix", "matrix.h") { // ================================================================================================= // arithmetic // ================================================================================================= SECTION( "matrix += matrix" ) { MatD a = makeSymmetric(MatD::Random(M,N)); MatD b = makeSymmetric(MatD::Random(M,N)); sMat A = sMat::CopyDense(M, N, a.data(), a.data()+a.size()); sMat B = sMat::CopyDense(M, N, b.data(), b.data()+b.size()); for ( size_t i = 0 ; i < M ; ++i ) for ( size_t j = 0 ; j < N ; ++j ) a(i,j) += b(i,j); A += B; Equal(A, a); } // -------------------------------------------------------------------------------------------------
inline void ActFunc::logistic(MatD& x){ x = x.unaryExpr(std::ptr_fun((Real (*)(const Real))ActFunc::logistic)); }
//f'(x) = 1-(f(x))^2 inline MatD ActFunc::tanhPrime(const MatD& x){ return 1.0-x.array().square(); }
inline void ActFunc::tanh(MatD& x){ x = x.unaryExpr(std::ptr_fun(::tanh)); }
inline void ActFunc::tanh(MatD& x){ x = x.array().tanh(); }
inline void ActFunc::logistic(MatD& x){ x = x.unaryExpr(std::ptr_fun((double (*)(const double))ActFunc::logistic)); }