private void button_Matrix_Click(object sender, EventArgs e) { textBox1.Clear(); Init_Chart(chart1); tabControl1.SelectedIndex = 1; textBox1.Text = "Matix testing"; textBox1.Text += System.Environment.NewLine; textBox1.Text += System.Environment.NewLine; int t1 = System.Environment.TickCount; mcore.Types.Collection collection = new mcore.Types.Collection(2); // local test double [] in_array = { 1, 2, 1, 4, 1, 6, 1, 2, 3 }; mcore.Types.Matrix m1 = new mcore.Types.Matrix(3, 3, in_array); mcore.Types.Matrix m2 = new mcore.Types.Matrix(m1); mcore.Types.Matrix m3 = m1 + m2; mcore.Types.Matrix m4 = (m1 + m2) * (m1 + m2); mcore.Types.Matrix m5 = ((m1 + m2) * (m1 + m2)).Inverse(); mcore.Types.Matrix m6 = m4 * m5; textBox1.Text += "m3 = m1 + m2" + System.Environment.NewLine; textBox1.Text += m3.ToString() + System.Environment.NewLine; textBox1.Text += "m4 = (m1+m2)*(m1+m2)" + System.Environment.NewLine; textBox1.Text += m4.ToString() + System.Environment.NewLine; textBox1.Text += "m5 = ((m1+m2)*(m1+m2)).Inverse()" + System.Environment.NewLine; textBox1.Text += m5.ToString() + System.Environment.NewLine; textBox1.Text += "m6 = m4 * m5" + System.Environment.NewLine; textBox1.Text += m6.ToString() + System.Environment.NewLine; textBox1.Text += System.Environment.NewLine; // textBox1.Text += "Matrix testing - done"; textBox1.Text += System.Environment.NewLine; textBox1.Text += System.Environment.NewLine; textBox1.Text += "Time elapsed = " + ((System.Environment.TickCount - t1) * 0.001).ToString() + " sec"; }
private void button_Matrix_Click(object sender, EventArgs e) { textBox1.Clear(); Init_Chart(chart1); tabControl1.SelectedIndex = 1; textBox1.Text = "Matix testing"; textBox1.Text += System.Environment.NewLine; textBox1.Text += System.Environment.NewLine; int t1 = System.Environment.TickCount; mcore.Types.Collection collection = new mcore.Types.Collection(2); // local test double []in_array = {1, 2, 1, 4, 1, 6, 1, 2, 3}; mcore.Types.Matrix m1 = new mcore.Types.Matrix(3, 3, in_array); mcore.Types.Matrix m2 = new mcore.Types.Matrix(m1); mcore.Types.Matrix m3 = m1+m2; mcore.Types.Matrix m4 = (m1+m2)*(m1+m2); mcore.Types.Matrix m5 = ((m1+m2)*(m1+m2)).Inverse(); mcore.Types.Matrix m6 = m4*m5; textBox1.Text += "m3 = m1 + m2" + System.Environment.NewLine; textBox1.Text += m3.ToString() + System.Environment.NewLine; textBox1.Text += "m4 = (m1+m2)*(m1+m2)" + System.Environment.NewLine; textBox1.Text += m4.ToString() + System.Environment.NewLine; textBox1.Text += "m5 = ((m1+m2)*(m1+m2)).Inverse()" + System.Environment.NewLine; textBox1.Text += m5.ToString() + System.Environment.NewLine; textBox1.Text += "m6 = m4 * m5" + System.Environment.NewLine; textBox1.Text += m6.ToString() + System.Environment.NewLine; textBox1.Text += System.Environment.NewLine; // textBox1.Text += "Matrix testing - done"; textBox1.Text += System.Environment.NewLine; textBox1.Text += System.Environment.NewLine; textBox1.Text += "Time elapsed = " + ((System.Environment.TickCount - t1)*0.001).ToString() + " sec"; }
/// <summary> /// Считает коэфициенты регрессии для "У" набором данных "Х" /// </summary> /// <param name="x">Набор данных Х</param> /// <param name="y">К чему стремимся</param> /// <param name="power">Степенной ряд, для которого делаем счет</param> /// <returns></returns> public static double [][] Regression(mcore.Types.Collection y, mcore.Types.Collection x, int[] powers) { if (x == null) { throw new Exception("(x) is null"); } if (y == null) { throw new Exception("(y) is null"); } if (powers == null) { throw new Exception("(powers) is null"); } if (powers.Length >= y[0].Count) { throw new Exception("Length is small"); } int ValuesCount = y[0].Count; int xParamCount = x.VectorsCount; int PolinomLevel = powers.Length; double [][] result = new double[xParamCount][]; for (int i = 0; i < xParamCount; i++) { result[i] = new double[PolinomLevel]; } mcore.Types.Matrix XMat = new mcore.Types.Matrix(ValuesCount, PolinomLevel * xParamCount); mcore.Types.Matrix YMat = new mcore.Types.Matrix(ValuesCount, 1); mcore.Types.Matrix K = new Types.Matrix(); for (int i = 0; i < ValuesCount; i++) { YMat[i][0] = y[0][i]; } for (int i = 0; i < ValuesCount; i++) { for (int z = 0; z < xParamCount; z++) { for (int k = 0; k < PolinomLevel; k++) { XMat[i][k + z * (PolinomLevel)] = System.Math.Pow(x[z][i], powers[k]); } } } K = (XMat.Transpose() * XMat).Inverse() * XMat.Transpose() * YMat; // ---------- K.Rows for (int i = 0; i < xParamCount; i++) { for (int k = 0; k < PolinomLevel; k++) { result[i][k] = K[k + i * PolinomLevel][0]; } } return(result); }
/// <summary> /// Считает коэфициенты регрессии для "У" набором данных "Х" /// </summary> /// <param name="x">Набор данных Х</param> /// <param name="y">К чему стремимся</param> /// <param name="power">Степенной ряд, для которого делаем счет</param> /// <returns></returns> public static double[][] Regression( mcore.Types.Collection y, mcore.Types.Collection x, int[] powers) { if ( x == null ) throw new Exception("(x) is null"); if ( y == null ) throw new Exception("(y) is null"); if ( powers == null ) throw new Exception("(powers) is null"); if ( powers.Length >= y[0].Count ) throw new Exception("Length is small"); int ValuesCount = y[0].Count; int xParamCount = x.VectorsCount; int PolinomLevel = powers.Length; double [][]result = new double[xParamCount][]; for ( int i = 0; i < xParamCount; i++) result[i] = new double[PolinomLevel]; mcore.Types.Matrix XMat = new mcore.Types.Matrix(ValuesCount, PolinomLevel * xParamCount); mcore.Types.Matrix YMat = new mcore.Types.Matrix(ValuesCount, 1); mcore.Types.Matrix K = new Types.Matrix(); for ( int i = 0; i < ValuesCount; i++ ) YMat[i][0] = y[0][i]; for ( int i = 0; i < ValuesCount; i++) for ( int z = 0; z < xParamCount; z++) for ( int k = 0; k < PolinomLevel; k++) XMat[i][k + z*(PolinomLevel)] = System.Math.Pow(x[z][i], powers[k]); K = (XMat.Transpose() * XMat).Inverse()*XMat.Transpose()*YMat; // ---------- K.Rows for ( int i = 0; i < xParamCount; i++) for ( int k = 0; k < PolinomLevel; k++ ) result[i][k] = K[k + i*PolinomLevel][0]; return result; }