/// <summary>Calculates LU decomposition of M matrix</summary> /// <param name="M">Matrix to decompose</param> /// <param name="n">Matrix dimension</param> /// <param name="varindx">Swap index</param> private CLCalc.Program.Variable LUDecomp(double[,] M, int n, out CLCalc.Program.Variable varindx) { //arguments and work_dim CLCalc.Program.Variable[] args; int[] max; //Matrix to vector double[] vecM = MatrixToVector(M, ref n, ref n); CLCalc.Program.Variable varM = new Program.Variable(vecM); //Scaling transformation double[] vv = new double[n]; CLCalc.Program.Variable varvv = new Program.Variable(vv); max = new int[1] { n }; args = new CLCalc.Program.Variable[] { varM, varvv }; doubleLUScale.Execute(args, max); //In order LU factorization (Crout) int[] J = new int[1] { 0 }; CLCalc.Program.Variable varJ = new Program.Variable(J); int[] N = new int[1] { n }; CLCalc.Program.Variable varN = new Program.Variable(N); int[] indx = new int[n]; varindx = new Program.Variable(indx); args = new Program.Variable[] { varM, varJ, varN, varindx, varvv }; for (J[0] = 0; J[0] < n; J[0]++) { varJ.WriteToDevice(J); max[0] = J[0]; doubleLUCalcBetas.Execute(args, max); max[0] = n - J[0]; doubleLUCalcAlphas.Execute(args, max); max[0] = 1; doubleLUCalcPivo.Execute(args, max); max[0] = n; doubleLUTrocaCols.Execute(args, max); if (J[0] != n - 1) { max[0] = n - J[0] - 1; doubleLUDivByPivot.Execute(args, max); } } return varM; }
private CLCalc.Program.Variable LUBackSubstitute(CLCalc.Program.Variable MLUDecomp, double[] b, int n, CLCalc.Program.Variable varindx) { CLCalc.Program.Variable varx = new Program.Variable(b); CLCalc.Program.Variable varN = new Program.Variable(new int[1] { n }); int[] J = new int[1]; CLCalc.Program.Variable varJ = new Program.Variable(J); CLCalc.Program.Variable[] args = new Program.Variable[] { MLUDecomp, varx, varindx, varN, varJ }; int[] max = new int[1]; //ajeita o vetor com respeito as trocas de linha max[0] = 1; doubleLUUnscramble.Execute(args, max); //Forward subst for (int i = n - 1; i >= 1; i--) { max[0] = i; doubleLUForwardSubs.Execute(args, max); } //Backward subst for (int j = n - 1; j >= 1; j--) { max[0] = 1; J[0] = j; varJ.WriteToDevice(J); doubleLUDivide.Execute(args, max); max[0] = j; doubleLUBackSubs.Execute(args, max); } //Primeiro elemento max[0] = 1; J[0] = 0; varJ.WriteToDevice(J); doubleLUDivide.Execute(args, max); return varx; }