diff --git a/MatrixExtensions.cs b/MatrixExtensions.cs index c8fe4ec..a8bb2bf 100644 --- a/MatrixExtensions.cs +++ b/MatrixExtensions.cs @@ -38,21 +38,98 @@ namespace MatrixExtensions //functions definitions return new TermInfo[] { + new TermInfo("list", TermType.Function, + "Возвращает нулевой вектор заданной ('число') размерности.", + FunctionSections.MatricesAndVectors, true, + new ArgumentInfo(ArgumentSections.RealNumber)), + new TermInfo("listDistinct", TermType.Function, "Возвращает вектор уникальных значений, содержащихся в ('матрица').", FunctionSections.MatricesAndVectors, true, new ArgumentInfo(ArgumentSections.Matrix)), + + new TermInfo("listLength", TermType.Function, + "Возвращает длину вектора ('вектор').", + FunctionSections.MatricesAndVectors, true, + new ArgumentInfo(ArgumentSections.ColumnVector)), + + new TermInfo("listNonZeros", TermType.Function, + "Возвращает вектор не нулевых значений, содержащихся в ('матрица').", + FunctionSections.MatricesAndVectors, true, + new ArgumentInfo(ArgumentSections.Matrix)), new TermInfo("listDistinct", TermType.Function, "Возвращает вектор или вектор-строку уникальных значений, содержащихся в ('матрица'), а" + " ('число') указывает на вид возвращаемого вектора: 0 - вектор; 1 - вектор-строка.", FunctionSections.MatricesAndVectors, true, new ArgumentInfo(ArgumentSections.Matrix), new ArgumentInfo(ArgumentSections.RealNumber)), + + new TermInfo("listAdd", TermType.Function, + "Добавление нового значения ('аргумент') в конец вектора ('вектор').", + FunctionSections.MatricesAndVectors, true, + new ArgumentInfo(ArgumentSections.ColumnVector), new ArgumentInfo(ArgumentSections.Default)), + }; } public bool TryEvaluateExpression(Entry value, Store context, out Entry result) { + //list + if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "list") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + + TNumber tmp = Computation.NumericCalculation(arg1, context); + TDouble n = (TDouble)tmp.obj; + //List vector = new List(Utilites.EntryMatrix2ArrStr(tmp.obj)); + //vector.RemoveAt(vector.Count - 1); + //vector.RemoveAt(vector.Count - 1); + //List distinct = vector.Distinct().ToList(); + + List answer = new List(); + if (n.D <= 0) + { + answer.AddRange(TermsConverter.ToTerms(0.ToString())); + } + else + { + for (int i = 0; i < n.D; i++) + { + answer.AddRange(TermsConverter.ToTerms("0")); + } + + answer.AddRange(TermsConverter.ToTerms(n.D.ToString())); + } + + answer.AddRange(TermsConverter.ToTerms(1.ToString())); + answer.Add(new Term(Functions.Mat, TermType.Function, 2 + (int)n.D)); + + result = Entry.Create(answer.ToArray()); + return true; + } + + //listLength + if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "listLength") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + + List vector = new List(Utilites.EntryMatrix2ArrStr(arg1)); + + List answer = new List(); + + if (vector.Count<=2) + { + answer.AddRange(TermsConverter.ToTerms("0")); + } + else + { + answer.AddRange(TermsConverter.ToTerms(vector[vector.Count - 2])); + } + + result = Entry.Create(answer.ToArray()); + return true; + } + //listDistinct if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "listDistinct") { @@ -78,6 +155,74 @@ namespace MatrixExtensions result = Entry.Create(answer.ToArray()); return true; } + + //listAdd + if (value.Type == TermType.Function && value.ArgsCount == 2 && value.Text == "listAdd") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + Entry arg2 = Computation.Preprocessing(value.Items[1], context); + + List v = new List(Utilites.EntryMatrix2ArrStr(arg1)); + List vector = new List(3); + TNumber tmp; + TNumber tmp1 = Computation.NumericCalculation(arg2, context); + if (v.Count > 2) + { + tmp = Computation.NumericCalculation(arg1, context); + vector = new List(Utilites.EntryMatrix2ArrStr(tmp.obj)); + vector.RemoveAt(vector.Count - 1); + vector.RemoveAt(vector.Count - 1); + vector.Add(tmp1.obj.ToString()); + } + else if (v.Count == 2) + { + vector.Add(tmp1.obj.ToString()); + //vector.Add(v[0]); + //vector.Add(v[1]); + } + + + List answer = new List(); + foreach (string item in vector) + { + answer.AddRange(TermsConverter.ToTerms(item)); + } + + answer.AddRange(TermsConverter.ToTerms(vector.Count.ToString())); + answer.AddRange(TermsConverter.ToTerms(1.ToString())); + answer.Add(new Term(Functions.Mat, TermType.Function, 2 + vector.Count)); + + result = Entry.Create(answer.ToArray()); + return true; + } + + //listNonZeros + if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "listNonZeros") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + + TNumber tmp = Computation.NumericCalculation(arg1, context); + + List vector = new List(Utilites.EntryMatrix2ArrStr(tmp.obj)); + vector.RemoveAt(vector.Count - 1); + vector.RemoveAt(vector.Count - 1); + IEnumerable select = from t in vector // определяем каждый объект из teams как t + where t!="0" //фильтрация по критерию + select t; // выбираем объект + List nonZeros = new List(select); + List answer = new List(); + foreach (string item in nonZeros) + { + answer.AddRange(TermsConverter.ToTerms(item)); + } + + answer.AddRange(TermsConverter.ToTerms(nonZeros.Count.ToString())); + answer.AddRange(TermsConverter.ToTerms(1.ToString())); + answer.Add(new Term(Functions.Mat, TermType.Function, 2 + nonZeros.Count)); + + result = Entry.Create(answer.ToArray()); + return true; + } if (value.Type == TermType.Function && value.ArgsCount == 2 && value.Text == "listDistinct") {