From 918270d9301fa21246f4040dd13d61795e10ba7c Mon Sep 17 00:00:00 2001 From: rumata-ap Date: Sat, 1 Aug 2020 14:02:23 +0300 Subject: [PATCH] edit --- MatrixExtensions.cs | 96 +++++++++++++++++++++++++++++++++++++++++++-- MatrixL.cs | 28 ++++++++++--- Utilites.cs | 32 +++++++++++++++ 3 files changed, 148 insertions(+), 8 deletions(-) diff --git a/MatrixExtensions.cs b/MatrixExtensions.cs index 5a522eb..939e7f5 100644 --- a/MatrixExtensions.cs +++ b/MatrixExtensions.cs @@ -97,6 +97,28 @@ namespace MatrixExtensions new ArgumentInfo(ArgumentSections.ColumnVector), new ArgumentInfo(ArgumentSections.RealNumber), new ArgumentInfo(ArgumentSections.RealNumber)), + new TermInfo("matrixRInsert", TermType.Function, + "Вставка нулевой строки в матрицу ('матрица') по указанному индексу ('число').", + FunctionSections.MatricesAndVectors, true, + new ArgumentInfo(ArgumentSections.Matrix), new ArgumentInfo(ArgumentSections.RealNumber)), + + new TermInfo("matrixRInsert", TermType.Function, + "Вставка строки в матрицу ('1:матрица') по указанному индексу ('число') из вектора ('3:матрица').", + FunctionSections.MatricesAndVectors, true, + new ArgumentInfo(ArgumentSections.Matrix), new ArgumentInfo(ArgumentSections.RealNumber), + new ArgumentInfo(ArgumentSections.Matrix)), + + new TermInfo("matrixCInsert", TermType.Function, + "Вставка нулевого столбца в матрицу ('матрица') по указанному индексу ('число').", + FunctionSections.MatricesAndVectors, true, + new ArgumentInfo(ArgumentSections.Matrix), new ArgumentInfo(ArgumentSections.RealNumber)), + + new TermInfo("matrixCInsert", TermType.Function, + "Вставка столбца в матрицу ('1:матрица') по указанному индексу ('число') из вектора ('3:матрица').", + FunctionSections.MatricesAndVectors, true, + new ArgumentInfo(ArgumentSections.Matrix), new ArgumentInfo(ArgumentSections.RealNumber), + new ArgumentInfo(ArgumentSections.Matrix)), + }; } @@ -242,8 +264,7 @@ namespace MatrixExtensions m[0, 0] = tmp1; } - List answer = new List(m.ToTerms()); - result = Entry.Create(answer.ToArray()); + result = Entry.Create(m.ToTerms()); return true; } @@ -383,7 +404,6 @@ namespace MatrixExtensions return true; } - if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "listInsert") { Entry arg1 = Computation.Preprocessing(value.Items[0], context); @@ -460,6 +480,76 @@ namespace MatrixExtensions return true; } + if (value.Type == TermType.Function && value.ArgsCount == 2 && value.Text == "matrixRInsert") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + Entry arg2 = Computation.Preprocessing(value.Items[1], context); + + TNumber tmp1 = Computation.NumericCalculation(arg1, context); + TNumber tmp2 = Computation.NumericCalculation(arg2, context); + + MatrixL matrixL = Utilites.TMatrixToMatrixL(tmp1); + matrixL.InsertRow(tmp2.ToInt32()-1); + TMatrix m = Utilites.MatrixLToTMatrix(matrixL); + //MatrixL vectorL = Utilites.TMatrixToMatrixL(tmp2); + result = Entry.Create(m.ToTerms()); + return true; + } + + if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "matrixRInsert") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + Entry arg2 = Computation.Preprocessing(value.Items[1], context); + Entry arg3 = Computation.Preprocessing(value.Items[2], context); + + TNumber tmp1 = Computation.NumericCalculation(arg1, context); + TNumber tmp2 = Computation.NumericCalculation(arg2, context); + TNumber tmp3 = Computation.NumericCalculation(arg3, context); + + MatrixL matrixL = Utilites.TMatrixToMatrixL(tmp1); + List vectorL = Utilites.TMatrixToMatrixL(tmp3).ToList(); + matrixL.InsertRow(tmp2.ToInt32() - 1, vectorL); + TMatrix m = Utilites.MatrixLToTMatrix(matrixL); + //MatrixL vectorL = Utilites.TMatrixToMatrixL(tmp2); + result = Entry.Create(m.ToTerms()); + return true; + } + + if (value.Type == TermType.Function && value.ArgsCount == 2 && value.Text == "matrixCInsert") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + Entry arg2 = Computation.Preprocessing(value.Items[1], context); + + TNumber tmp1 = Computation.NumericCalculation(arg1, context); + TNumber tmp2 = Computation.NumericCalculation(arg2, context); + + MatrixL matrixL = Utilites.TMatrixToMatrixL(tmp1); + matrixL.InsertCol(tmp2.ToInt32()-1); + TMatrix m = Utilites.MatrixLToTMatrix(matrixL); + //MatrixL vectorL = Utilites.TMatrixToMatrixL(tmp2); + result = Entry.Create(m.ToTerms()); + return true; + } + + if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "matrixCInsert") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + Entry arg2 = Computation.Preprocessing(value.Items[1], context); + Entry arg3 = Computation.Preprocessing(value.Items[2], context); + + TNumber tmp1 = Computation.NumericCalculation(arg1, context); + TNumber tmp2 = Computation.NumericCalculation(arg2, context); + TNumber tmp3 = Computation.NumericCalculation(arg3, context); + + MatrixL matrixL = Utilites.TMatrixToMatrixL(tmp1); + List vectorL = Utilites.TMatrixToMatrixL(tmp3).ToList(); + matrixL.InsertCol(tmp2.ToInt32() - 1, vectorL); + TMatrix m = Utilites.MatrixLToTMatrix(matrixL); + //MatrixL vectorL = Utilites.TMatrixToMatrixL(tmp2); + result = Entry.Create(m.ToTerms()); + return true; + } + result = null; return false; } diff --git a/MatrixL.cs b/MatrixL.cs index b120a2f..98d9f11 100644 --- a/MatrixL.cs +++ b/MatrixL.cs @@ -161,13 +161,17 @@ namespace MatrixExtensions int n = r; if (col.Count < r) n = col.Count; - foreach (List item in src) + for (int i = 0; i < n; i++) { - for (int i = 0; i < n; i++) item[i] = col[i]; - - if (col.Count < r) + for (int j = 0; j < r; j++) { - for (int i = n; i < r; i++) item[i] = init; + //src[j].Insert(idxC, col[i]); + src[j][idxC] = col[i]; + if (col.Count < r) + { + //src[j].Insert(idxC, init); + src[j][idxC] = col[i]; + } } } } @@ -192,5 +196,19 @@ namespace MatrixExtensions } else throw new ArgumentException("Матрица пустая либо не верно указан индекс столбца для удаления."); } + + public List ToList() + { + List res = new List(r * c); + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + res.Add(src[i][j]); + } + } + + return res; + } } } diff --git a/Utilites.cs b/Utilites.cs index a4967f4..c74f32d 100644 --- a/Utilites.cs +++ b/Utilites.cs @@ -51,5 +51,37 @@ namespace MatrixExtensions string source = prime.ToString(); return Convert.ToInt32(source); } + + internal static MatrixL TMatrixToMatrixL(TNumber src) + { + if (src.obj.Type != BaseEntryType.Matrix) return null; + int r = src.Rows().ToInt32(); + int c = src.Cols().ToInt32(); + MatrixL res = new MatrixL(r, c, new TNumber(0,1)); + TMatrix matrix = (TMatrix)src.obj; + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + res[i, j] = matrix[i, j]; + } + } + + return res; + } + + internal static TMatrix MatrixLToTMatrix(MatrixL src) + { + TMatrix res = new TMatrix(new TNumber[src.R, src.C]); + for (int i = 0; i < src.R; i++) + { + for (int j = 0; j < src.C; j++) + { + res[i, j] = src[i, j]; + } + } + + return res; + } } }