Matrix_Solve_lib
Download scriptThis script serves as a specialized mathematical toolkit designed to handle complex matrix calculations within the MetaTrader platform. Rather than performing simple arithmetic, it provides a structured environment for linear algebra—the foundation for advanced financial modeling, data analysis, and systemic trend forecasting.
The core logic can be broken down into three primary functional areas:
1. Structural Transformations
The script includes tools to reorganize and manipulate the shape of data sets. This is essential when the orientation of information needs to change without altering the data itself:
- Transposition: Flips a data set over its diagonal (switching rows with columns).
- Dynamic Modification: Provides specific commands to surgically remove a designated row or column from a dataset, allowing the system to "clean" data or isolate specific variables for analysis.
2. Core Algebraic Operations
These functions perform the heavy lifting of mathematical processing:
- Matrix Multiplication: Combines two distinct datasets to produce a new result. This is a fundamental operation used in projecting complex relationships between different variables.
- Inverse Calculation: Determines the "opposite" of a matrix. In practical terms, this is often used to solve systems of linear equations—finding unknown values when you have several interconnected known variables.
3. Advanced Determinants and Minors
The script contains sophisticated logic for high-level mathematical analysis:
- Determinant Calculation: A scalar value that summarizes the properties of a square matrix (such as whether a system of equations has a unique solution). The script uses an iterative reduction method to calculate this efficiently.
- Minor Calculation: Identifies specific subsets of a matrix. It can also calculate "Algebraic Cofactors," which are essential for understanding the internal dependencies within a complex data grid.
Technical Safeguards
To ensure the script remains stable during high-intensity calculations, it includes a safety mechanism for division. If a calculation encounters a zero (which would normally cause a software crash), the logic replaces it with an extremely small number to maintain continuity and prevent errors from halting the trading environment.
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
Äëÿ îïåðàöèé òðàíñïîíèðîâàíèÿ, óìíîæåíèÿ,óäàëåíèÿ ñòðîêè èëè ñòîëáöà ìàòðèöû ìîãóò áûòü ïðÿìîóãîëüíûìè,
ïîýòîìó èñïîëüçóåòñÿ äâà ïàðàìåòðà, äëèííà ñòðîêè è âûñîòà ñòîëáöà.
Îñòàëüíûå ôóíêöèè ðàáîòàþò ñ êâàäðàòíûìè ìàòðèöàìè, è èñïîëüçóþò îäèí ïàðàìåòð äëèííà ñòðîêè.
void transpose() -îïåðàöèÿ ÒÐÀÍÑÏÎÍÈÐÎÂÀÍÈß
void multiplying() -îïåðàöèÿ ÓÌÍÎÆÅÍÈß
void inverse() -îïåðàöèÿ ÎÁÐÀÙÅÍÈß
void pull_line() -çàäàííóþ h ÓÄÀËßÅÌ ÑÒÐÎÊÓ
void pull_column() -çàäàííûé v ÓÄÀËßÅÌ ÑÒÎËÁÅÖ
double minor_AD() -âû÷èñëåíèå ÌÈÍÎÐÀ(Àëãåáðàè÷åñêîå Äîïîëíåíèå)
double det() -âû÷èñëåíèå ÎÏÐÅÄÅËÈÒÅËß
double ox() -âû÷èñëåíèå 1/X
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
//+------------------------------------------------------------------+
//| Matrix_Solve_lib.mq4 |
//| Urain |
//+------------------------------------------------------------------+
#property copyright "Urain"
#property link ""
#property library
//=====================================================================================================
//ÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆ
//=====================================================================================================
//VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_VOID_
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void transpose -îïåðàöèÿ ÒÐÀÍÑÏÎÍÈÐÎÂÀÍÈß
a - ìàòðèöà
n - äëèííà ñòðîêè
m - äëèííà ïòîëáöà
ðåçóëüòàò â òîìæå ìàññèâå
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void transpose( double& a[], int n, int m)
{//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
int i,j,s=0; int nm=n*m;
double b[];ArrayResize(b,nm);
for(j=0;j<n;j++)//i-ñòîëáöè j-ñòðîêè
{for(i=0;i<m;i++)
{b[s]=a[i*n+j];
s++;
}
}
for(i=0;i<s;i++)a[i]=b[i];
return;
}//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void multiplying -îïåðàöèÿ ÓÌÍÎÆÅÍÈß
a - ìàòðèöà q q
n
n
b - ìàòðèöà m m
q
q
q - äëèííà ñòðîêè a[qxn] è ñòîëáöà b[mxq]
n - äëèííà ñòîëáöà a[]
m - äëèííà ñòðîêè b[]
ðåçóëüòàò â ìàññèâå a[]
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void multiplying( double& a[], double& b[], int n, int q, int m)
{//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
int i,j,k,s=0; int qn=q*n, mq=m*q, nm=n*m;
double c[];ArrayResize(c,nm);
transpose( b, m, q);
for(j=0;j<n;j++)
{for(k=0;k<m;k++)
{double temp=0.0;
for(i=0;i<q;i++)temp+=a[j*q+i]*b[k*q+i];
c[s]=temp;s++;
}
}
ArrayResize(a,nm);
for(i=0;i<nm;i++)a[i]=c[i];
return;
}//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void inverse -îïåðàöèÿ ÎÁÐÀÙÅÍÈß
a - ìàòðèöà
n - äëèííà ñòðîêè,ñòîëáöà
ðåçóëüòàò â ìàññèâå a[]
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void inverse( double& a[], int n)
{//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
int i,j,s=0; int N=n,NN=n*n; double AD =0.0;
double b[];ArrayResize(b,NN);for(i=0;i<NN;i++)b[i]=a[i];
double Det=det( b, N); Det=ox( Det);
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
{AD = minor_AD( b, N, i, j, 1);
a[s]= AD*Det;
s++;
}
}
transpose( a, N, N);
return;
}//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void pull_line - òÿíóòü çàäàííóþ h ñòðîêó ÓÄÀËßÅÌ ÑÒÐÎÊÓ
a - ìàòðèöà
n - äëèííà ñòðîêè
m - äëèííà còîëáöà
ðåçóëüòàò â ìàññèâå a[]
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void pull_line( double& a[], int h, int n, int m)
{//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
int i,j,s=0; int nm=n*(m-1);//i-ñòîëáöè j-ñòðîêè
double b[];ArrayResize(b,nm);
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{if(i!=h)
{b[s]=a[i*n+j];
s++;
}
}
}
ArrayResize(a,nm);
for(i=0;i<nm;i++)a[i]=b[i];
return;
}//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void pull_column - òÿíóòü çàäàííóþ h ñòîëáåö ÓÄÀËßÅÌ ÑÒÎËÁÅÖ
a - ìàòðèöà
n - äëèííà ñòðîêè
m - äëèííà còîëáöà
ðåçóëüòàò â ìàññèâå a[]
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
void pull_column( double& a[], int v, int n, int m)
{//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
int i,j,s=0; int nm=(n-1)*m;//i-ñòîëáöè j-ñòðîêè
double b[];ArrayResize(b,nm);
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
{if(j!=v)
{b[s]=a[i*n+j];
s++;
}
}
}
ArrayResize(a,nm);
for(i=0;i<nm;i++)a[i]=b[i];
return;
}//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
// DOUBLE__DOUBLE__DOUBLE__DOUBLE__DOUBLE__DOUBLE__DOUBLE__DOUBLE__DOUBLE__DOUBLE__DOUBLE__DOUBLE__
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
double minor_AD - âû÷èñëåíèå ÌÈÍÎÐÀ
åñëè fl=true òî âû÷èñëÿåòñÿ Àëãåáðàè÷åñêîå Äîïîëíåíèå
a - ìàòðèöà
I - íîìåð ñòðîêè
J - íîìåð còîëáöà
ðåçóëüòàò â Minor
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
double minor_AD( double a[], int n, int I, int J, bool fl=0)
{//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
int i,j,s=0; int N=n,NN=n*n;
double b[];ArrayResize(b,NN);for(i=0;i<NN;i++)b[i]=a[i];
pull_line( b, I, N, N);
pull_column( b, J, N, N);
double Minor=det( b, N-1);
if(fl)Minor*=MathPow(-1.0,I+J);
return(Minor);
}//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
double det - âû÷èñëåíèå ÎÏÐÅÄÅËÈÒÅËß
a - ìàòðèöà
n - äëèííà ñòðîêè
m - äëèííà còîëáöà
ðåçóëüòàò â Det
/*///+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
double det( double a[], int n)
{//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
int N=n,NN=n*n;
int i,j,s=0; double Det=a[0];
double b[];ArrayResize(b,NN);
for(i=0;i<NN;i++)b[i]=a[i];
double c[];//ñòðîêè
double d[];//ñòîëáöû
while(N>1)
{ArrayResize(c,N-1);//ñòðîêè
ArrayResize(d,N-1);//ñòîëáöû
for(i=1;i<N;i++)c[i-1]=b[i];
for(i=1;i<N;i++)d[i-1]=b[i*N];
double Glav=b[0]; Glav=ox( Glav);
pull_line( b, 0, N, N);
pull_column( b, 0, N, N);
s=0;
for(i=0;i<N-1;i++)
{for(j=0;j<N-1;j++)
{b[s]-=c[j]*d[i]*Glav;
s++;
}
}
Det*=b[0];
N--;NN=N*N;
}
return(Det);
}//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
/*///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
////////////////////////////////////////////// ÎÄÍÀ ÈÊÑÎÂÀß
/*///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
double ox( double x)
{//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
double y=0.0;
if(x!=0.0) y=1.0/x;
else y= 1.0/MathPow(10.0,300);
return(y);
}//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
//=====================================================================================================
//ÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆÆ
//=====================================================================================================
Comments