10#ifndef VECTOR_OPS_FOR_NN
11#define VECTOR_OPS_FOR_NN
33 std::vector<std::valarray<T>>
const &A) {
36 for (
const auto &a : A) {
37 for (
const auto &x : a) {
38 std::cout << x <<
' ';
40 std::cout << std::endl;
52std::ostream &
operator<<(std::ostream &out,
const std::pair<T, T> &A) {
56 std::cout <<
"(" << A.first <<
", " << A.second <<
")";
67std::ostream &
operator<<(std::ostream &out,
const std::valarray<T> &A) {
70 for (
const auto &a : A) {
71 std::cout << a <<
' ';
73 std::cout << std::endl;
87 B.resize(A.size() + 1);
88 for (
size_t i = 0; i < A.size(); i++) {
91 B[B.size() - 1] = ele;
104 B.resize(A.size() - 1);
105 for (
size_t i = 1; i < A.size();
119std::valarray<T>
pop_back(
const std::valarray<T> &A) {
121 B.resize(A.size() - 1);
122 for (
size_t i = 0; i < A.size() - 1;
137 std::vector<std::vector<std::valarray<T>>> &B) {
139 if (A.size() != B.size()) {
140 std::cerr <<
"ERROR (" << __func__ <<
") : ";
142 <<
"Can not equally shuffle two vectors with different sizes: ";
143 std::cerr << A.size() <<
" and " << B.size() << std::endl;
144 std::exit(EXIT_FAILURE);
146 for (
size_t i = 0; i < A.size(); i++) {
148 std::srand(std::chrono::system_clock::now().time_since_epoch().count());
149 size_t random_index = std::rand() % A.size();
151 std::swap(A[i], A[random_index]);
152 std::swap(B[i], B[random_index]);
167 const std::pair<size_t, size_t> &shape,
168 const T &low,
const T &high) {
171 std::default_random_engine generator(
172 std::chrono::system_clock::now().time_since_epoch().count());
173 std::uniform_real_distribution<T> distribution(low, high);
174 for (
size_t i = 0; i < shape.first; i++) {
177 row.resize(shape.second);
178 for (
auto &r : row) {
179 r = distribution(generator);
194 const std::pair<size_t, size_t> &shape) {
196 for (
size_t i = 0; i < shape.first; i++) {
199 row.resize(shape.second);
214 const std::pair<size_t, size_t> &shape) {
216 for (
size_t i = 0; i < shape.first; i++) {
219 row.resize(shape.second);
232T
sum(
const std::vector<std::valarray<T>> &A) {
234 for (
const auto &a : A) {
247std::pair<size_t, size_t>
get_shape(
const std::vector<std::valarray<T>> &A) {
248 const size_t sub_size = (*A.begin()).size();
249 for (
const auto &a : A) {
251 if (a.size() != sub_size) {
252 std::cerr <<
"ERROR (" << __func__ <<
") : ";
253 std::cerr <<
"Supplied vector is not 2D Matrix" << std::endl;
254 std::exit(EXIT_FAILURE);
257 return std::make_pair(A.size(), sub_size);
270 const std::vector<std::vector<std::valarray<T>>> &A,
const T &low,
272 std::vector<std::vector<std::valarray<T>>> B =
277 if (shape.first != 1) {
278 std::cerr <<
"ERROR (" << __func__ <<
") : ";
280 <<
"Supplied vector is not supported for minmax scaling, shape: ";
281 std::cerr << shape << std::endl;
282 std::exit(EXIT_FAILURE);
284 for (
size_t i = 0; i < shape.second; i++) {
285 T min = B[0][0][i], max = B[0][0][i];
286 for (
size_t j = 0; j < B.size(); j++) {
288 min = std::min(min, B[j][0][i]);
289 max = std::max(max, B[j][0][i]);
291 for (
size_t j = 0; j < B.size(); j++) {
294 ((B[j][0][i] - min) / (max - min)) * (high - low) + low;
307size_t argmax(
const std::vector<std::valarray<T>> &A) {
311 if (shape.first != 1) {
312 std::cerr <<
"ERROR (" << __func__ <<
") : ";
313 std::cerr <<
"Supplied vector is ineligible for argmax" << std::endl;
314 std::exit(EXIT_FAILURE);
317 return std::distance(std::begin(A[0]),
318 std::max_element(std::begin(A[0]), std::end(A[0])));
330 const std::vector<std::valarray<T>> &A, T (*func)(
const T &)) {
331 std::vector<std::valarray<double>> B =
347std::vector<std::valarray<T>>
operator*(
const std::vector<std::valarray<T>> &A,
349 std::vector<std::valarray<double>> B =
365std::vector<std::valarray<T>>
operator/(
const std::vector<std::valarray<T>> &A,
367 std::vector<std::valarray<double>> B =
383 const std::vector<std::valarray<T>> &A) {
385 std::vector<std::valarray<T>> B;
387 for (
size_t j = 0; j < shape.second; j++) {
388 std::valarray<T> row;
389 row.resize(shape.first);
390 for (
size_t i = 0; i < shape.first; i++) {
407 const std::vector<std::valarray<T>> &A,
408 const std::vector<std::valarray<T>> &B) {
412 if (shape_a.first != shape_b.first || shape_a.second != shape_b.second) {
413 std::cerr <<
"ERROR (" << __func__ <<
") : ";
414 std::cerr <<
"Supplied vectors have different shapes ";
415 std::cerr << shape_a <<
" and " << shape_b << std::endl;
416 std::exit(EXIT_FAILURE);
418 std::vector<std::valarray<T>> C;
419 for (
size_t i = 0; i < A.size(); i++) {
420 C.push_back(A[i] + B[i]);
434 const std::vector<std::valarray<T>> &A,
435 const std::vector<std::valarray<T>> &B) {
439 if (shape_a.first != shape_b.first || shape_a.second != shape_b.second) {
440 std::cerr <<
"ERROR (" << __func__ <<
") : ";
441 std::cerr <<
"Supplied vectors have different shapes ";
442 std::cerr << shape_a <<
" and " << shape_b << std::endl;
443 std::exit(EXIT_FAILURE);
445 std::vector<std::valarray<T>> C;
446 for (
size_t i = 0; i < A.size(); i++) {
447 C.push_back(A[i] - B[i]);
460std::vector<std::valarray<T>>
multiply(
const std::vector<std::valarray<T>> &A,
461 const std::vector<std::valarray<T>> &B) {
465 if (shape_a.second != shape_b.first) {
466 std::cerr <<
"ERROR (" << __func__ <<
") : ";
467 std::cerr <<
"Vectors are not eligible for multiplication ";
468 std::cerr << shape_a <<
" and " << shape_b << std::endl;
469 std::exit(EXIT_FAILURE);
471 std::vector<std::valarray<T>> C;
473 for (
size_t i = 0; i < shape_a.first; i++) {
474 std::valarray<T> row;
475 row.resize(shape_b.second);
476 for (
size_t j = 0; j < shape_b.second; j++) {
477 for (
size_t k = 0; k < shape_a.second; k++) {
478 row[j] += A[i][k] * B[k][j];
495 const std::vector<std::valarray<T>> &A,
496 const std::vector<std::valarray<T>> &B) {
500 if (shape_a.first != shape_b.first || shape_a.second != shape_b.second) {
501 std::cerr <<
"ERROR (" << __func__ <<
") : ";
502 std::cerr <<
"Vectors have different shapes ";
503 std::cerr << shape_a <<
" and " << shape_b << std::endl;
504 std::exit(EXIT_FAILURE);
506 std::vector<std::valarray<T>> C;
507 for (
size_t i = 0; i < A.size(); i++) {
508 C.push_back(A[i] * B[i]);
std::vector< std::valarray< T > > operator-(const std::vector< std::valarray< T > > &A, const std::vector< std::valarray< T > > &B)
std::vector< std::valarray< T > > operator*(const std::vector< std::valarray< T > > &A, const T &val)
std::vector< std::valarray< T > > operator+(const std::vector< std::valarray< T > > &A, const std::vector< std::valarray< T > > &B)
std::valarray< T > insert_element(const std::valarray< T > &A, const T &ele)
size_t argmax(const std::vector< std::valarray< T > > &A)
std::vector< std::valarray< T > > multiply(const std::vector< std::valarray< T > > &A, const std::vector< std::valarray< T > > &B)
T sum(const std::vector< std::valarray< T > > &A)
std::ostream & operator<<(std::ostream &out, std::vector< std::valarray< T > > const &A)
std::vector< std::valarray< T > > transpose(const std::vector< std::valarray< T > > &A)
void unit_matrix_initialization(std::vector< std::valarray< T > > &A, const std::pair< size_t, size_t > &shape)
std::valarray< T > pop_front(const std::valarray< T > &A)
std::pair< size_t, size_t > get_shape(const std::vector< std::valarray< T > > &A)
void uniform_random_initialization(std::vector< std::valarray< T > > &A, const std::pair< size_t, size_t > &shape, const T &low, const T &high)
void zeroes_initialization(std::vector< std::valarray< T > > &A, const std::pair< size_t, size_t > &shape)
std::vector< std::vector< std::valarray< T > > > minmax_scaler(const std::vector< std::vector< std::valarray< T > > > &A, const T &low, const T &high)
std::vector< std::valarray< T > > hadamard_product(const std::vector< std::valarray< T > > &A, const std::vector< std::valarray< T > > &B)
std::vector< std::valarray< T > > apply_function(const std::vector< std::valarray< T > > &A, T(*func)(const T &))
std::valarray< T > pop_back(const std::valarray< T > &A)
std::vector< std::valarray< T > > operator/(const std::vector< std::valarray< T > > &A, const T &val)
void equal_shuffle(std::vector< std::vector< std::valarray< T > > > &A, std::vector< std::vector< std::valarray< T > > > &B)