-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
linalg.mqh
177 lines (150 loc) · 5.61 KB
/
linalg.mqh
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//+------------------------------------------------------------------+
//| linalg.mqh |
//| Copyright 2023, Omega Joctan |
//| https://www.mql5.com/en/users/omegajoctan |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Omega Joctan"
#property link "https://www.mql5.com/en/users/omegajoctan"
#include "MatrixExtend.mqh"
//+------------------------------------------------------------------+
//| implementations of standard linear algebra algorithms |
//+------------------------------------------------------------------+
class LinAlg
{
public:
LinAlg(void);
~LinAlg(void);
template<typename T>
static matrix<T> dot(matrix<T> &A, matrix<T> &B);
template<typename T>
static matrix<T> norm(const matrix<T> &A, const matrix<T> &B);
template<typename T>
static double norm(const vector<T> &v1, const vector<T> &v2);
template<typename T>
static matrix<T> outer(const matrix<T> &A, const matrix<T> &B);
static bool svd(matrix &mat, matrix &U, matrix &V, vector &singular_value);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
LinAlg::LinAlg(void)
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
LinAlg::~LinAlg(void)
{
}
//+------------------------------------------------------------------+
//| Dot product of two matrices | Flexible funciton - numpy like |
//+------------------------------------------------------------------+
template<typename T>
matrix<T> LinAlg::dot(matrix<T> &A, matrix<T> &B)
{
matrix Z={};
if (A.Cols() == B.Rows()) //Valid Normal matrix multiplication
{
Z = A.MatMul(B);
return Z;
}
else
{
//---Check for one dimensional matrices | Scalar
if ((A.Rows()==1 && A.Cols()==1))
{
Z = B * A[0][0];
return Z;
}
if (B.Rows()==1 && B.Cols()==1)
{
Z = B[0][0] * A;
return Z;
}
//-- Element wise multiplication
if (A.Rows()==B.Rows() && A.Cols()==B.Cols())
{
Z = A * B;
return Z;
}
}
return Z;
}
//+------------------------------------------------------------------+
//| Matrix or vector<T> norm. | Finds the equlidean distance of the |
//| two matrices |
//+------------------------------------------------------------------+
template<typename T>
matrix<T> LinAlg::norm(const matrix<T> &A, const matrix<T> &B)
{
matrix<T> ret = {};
if (B.Cols() != A.Cols())
{
Print(__FUNCTION__," Dimensions Error");
return ret;
}
if (A.Rows()==1 || B.Rows()==1)
{
matrix<T> A_temp = A, B_temp = B;
vector<T> A_vector, B_vector;
A_vector.Swap(A_temp);
B_vector.Swap(B_temp);
ulong size = 0;
if (A_vector.Size() >= B_vector.Size())
{
size = A_vector.Size();
B_vector.Resize(size);
}
else
{
size = B_vector.Size();
A_vector.Resize(size);
}
ret.Resize(1,1);
ret[0][0] = MathSqrt( MathPow(A_vector - B_vector, 2).Sum() ) ;
return (ret);
}
ulong size = A.Rows() > B.Rows() ? A.Rows() : B.Rows();
vector<T> euc(size);
for (ulong i=0; i<A.Rows(); i++)
for (ulong j=0; j<B.Rows(); j++)
euc[i] = MathSqrt( MathPow(A.Row(i) - B.Row(j), 2).Sum() );
euc.Swap(ret);
return ret;
}
//+------------------------------------------------------------------+
//| Euclidean Distance of two vectors |
//+------------------------------------------------------------------+
template<typename T>
double LinAlg::norm(const vector<T> &v1, const vector<T> &v2)
{
double dist = 0;
if(v1.Size() != v2.Size())
Print(__FUNCTION__, " v1 and v2 not matching in size");
else
{
double c = 0;
for(ulong i=0; i<v1.Size(); i++)
c += MathPow(v1[i] - v2[i], 2);
dist = MathSqrt(c);
}
return(dist);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
template<typename T>
matrix<T> LinAlg::outer(const matrix<T> &A,const matrix<T> &B)
{
return A.Outer(B);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool LinAlg::svd(matrix &mat, matrix &U,matrix &V,vector &singular_value)
{
return mat.SVD(U,V,singular_value);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+