-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.cpp
More file actions
68 lines (53 loc) · 1.88 KB
/
algorithms.cpp
File metadata and controls
68 lines (53 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Created by Kejsty, Katarina Kejstova on 15.11.16.
//
#include "algorithms.h"
#include <cassert>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <math.h>
#include <algorithm>
namespace algorithms {
matrix matrixMultiplication(const matrix &fst, const matrix &snd) {
// assert( fst.size( ) > 0 );
// assert( snd.size( ) > 0 );
// assert( snd[ 0 ].size( ) > 0 );
// assert( fst[ 0 ].size( ) == snd.size( ));
matrix newMatrix( fst.size( ), std::vector<double>( snd[ 0 ].size( )));
for ( size_t i = 0; i < fst.size( ); ++i ) {
for ( size_t j = 0; j < snd[ 0 ].size( ); ++j ) {
newMatrix[ i ][ j ] = 0;
for ( size_t k = 0; k < snd.size( ); ++k )
newMatrix[ i ][ j ] += ( fst[ i ][ k ] * snd[ k ][ j ] );
}
}
return newMatrix;
}
matrix transposeMatrix(const matrix &m) {
matrix outMatrix(m[0].size(), std::vector<double>(m.size()));
size_t size = m.size();
for (size_t x = 0; x < size; ++x) {
for ( size_t y = 0; y < m[ 0 ].size( ); ++y ) {
outMatrix[ y ][ x ] = m[ x ][ y ];
}
}
return outMatrix;
}
matrix matrixSum( matrix &fst, const matrix &snd) {
// assert( fst.size( ) > 0 );
// assert( snd.size( ) > 0 );
// assert( snd.size() == fst.size() );
// assert( fst[ 0 ].size( ) == snd[0].size( ));
for ( size_t i = 0; i < fst.size() ; ++i ) {
std::transform(fst[i].begin(), fst[i].end(), snd[i].begin(), fst[i].begin(), std::plus<double >());
}
return fst;
}
double sigmoid( double x)
{
double exp_val;
exp_val = exp((double) -x);
return 1 / (1 + exp_val);
}
}