- refactored common functions into matutils
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@855 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: matutils.hpp
|
||||
* Author: jens
|
||||
*
|
||||
* Created on 21. Januar 2022, 08:28
|
||||
*/
|
||||
|
||||
#ifndef MATUTILS_HPP
|
||||
#define MATUTILS_HPP
|
||||
|
||||
#include <math.h>
|
||||
|
||||
namespace Matutils
|
||||
{
|
||||
inline void uniform(arma::mat& srcDst, double stdDev=1.0, double mu=0.5)
|
||||
{
|
||||
#if 0
|
||||
for (size_t i=0; i < srcDst.n_rows; i++)
|
||||
{
|
||||
for (size_t j=0; j < srcDst.n_cols; j++)
|
||||
{
|
||||
srcDst(i, j) = stdDev*(Noise_Uniform(&m_noise) + mu - 0.5);
|
||||
}
|
||||
}
|
||||
#else
|
||||
srcDst = stdDev*(arma::randu(arma::size(srcDst)) + mu - 0.5);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline arma::mat sample(const arma::mat &src)
|
||||
{
|
||||
arma::mat dst = src;
|
||||
uniform(dst);
|
||||
|
||||
#if 0
|
||||
for (size_t i=0; i < src.n_rows; i++)
|
||||
{
|
||||
for (size_t j=0; j < src.n_cols; j++)
|
||||
{
|
||||
dst(i, j) = src(i, j) >= dst(i, j);
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
#else
|
||||
arma::umat res = (dst < src);
|
||||
return arma::conv_to<arma::mat>::from(res);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
inline arma::mat normalize(const arma::mat& src)
|
||||
{
|
||||
double mean = arma::accu(src)/src.n_elem;
|
||||
arma::mat x = src - mean;
|
||||
arma::mat x2 = x % x;
|
||||
double stddev = sqrt(arma::accu(x2)/x2.n_elem);
|
||||
|
||||
std::cout << "mean" << " : " << std::endl << mean << std::endl;
|
||||
std::cout << "stddev" << ": " << std::endl << stddev << std::endl;
|
||||
return x/stddev;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif /* MATUTILS_HPP */
|
||||
|
||||
Reference in New Issue
Block a user