From 52a36643655d6f13bbbaae270865bef577c7745e Mon Sep 17 00:00:00 2001 From: rumata-ap Date: Sat, 25 Jul 2020 00:44:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D1=8C=D1=82?= =?UTF-8?q?=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MatrixExtensions.cs | 128 +++++++++++++++++++++++++++++++++++++ MatrixExtensions.csproj | 106 ++++++++++++++++++++++++++++++ MatrixExtensions.sln | 25 ++++++++ Properties/AssemblyInfo.cs | 36 +++++++++++ Utilites.cs | 55 ++++++++++++++++ 5 files changed, 350 insertions(+) create mode 100644 MatrixExtensions.cs create mode 100644 MatrixExtensions.csproj create mode 100644 MatrixExtensions.sln create mode 100644 Properties/AssemblyInfo.cs create mode 100644 Utilites.cs diff --git a/MatrixExtensions.cs b/MatrixExtensions.cs new file mode 100644 index 0000000..c8fe4ec --- /dev/null +++ b/MatrixExtensions.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using SMath.Manager; +using SMath.Math; +using SMath.Math.Numeric; + +namespace MatrixExtensions +{ + public class MatrixExtensions : IPluginHandleEvaluation, IPluginLowLevelEvaluationFast + { + AssemblyInfo[] asseblyInfos; + + public void Initialize() + { + asseblyInfos = new AssemblyInfo[] { new AssemblyInfo("SMath Studio", new Version(0, 99), new Guid("a37cba83-b69c-4c71-9992-55ff666763bd")) }; + + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); + + System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + { + System.Reflection.Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); + for (int i = 0; i < asms.Length; ++i) + { + if (asms[i].FullName == args.Name) + return asms[i]; + } + return null; + } + } + + public TermInfo[] GetTermsHandled(SessionProfile sessionProfile) + { + //functions definitions + return new TermInfo[] + { + new TermInfo("listDistinct", 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)), + }; + } + + public bool TryEvaluateExpression(Entry value, Store context, out Entry result) + { + //listDistinct + if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "listDistinct") + { + 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); + List distinct = vector.Distinct().ToList(); + + List answer = new List(); + foreach (string item in distinct) + { + answer.AddRange(TermsConverter.ToTerms(item)); + } + + answer.AddRange(TermsConverter.ToTerms(distinct.Count.ToString())); + answer.AddRange(TermsConverter.ToTerms(1.ToString())); + answer.Add(new Term(Functions.Mat, TermType.Function, 2 + distinct.Count)); + + result = Entry.Create(answer.ToArray()); + return true; + } + + if (value.Type == TermType.Function && value.ArgsCount == 2 && value.Text == "listDistinct") + { + Entry arg1 = Computation.Preprocessing(value.Items[0], context); + Entry arg2 = Computation.Preprocessing(value.Items[1], context); + + int dir = Utilites.Entry2Int(arg2); + TNumber tmp = Computation.NumericCalculation(arg1, context); + + List vector = new List(Utilites.EntryMatrix2ArrStr(tmp.obj)); + vector.RemoveAt(vector.Count - 1); + vector.RemoveAt(vector.Count - 1); + List distinct = vector.Distinct().ToList(); + + //string res = "null"; + + List answer = new List(); + foreach (string item in distinct) + { + answer.AddRange(TermsConverter.ToTerms(item)); + } + + if (dir==0) + { + answer.AddRange(TermsConverter.ToTerms(distinct.Count.ToString())); + answer.AddRange(TermsConverter.ToTerms(1.ToString())); + answer.Add(new Term(Functions.Mat, TermType.Function, 2 + distinct.Count)); + } + else + { + answer.AddRange(TermsConverter.ToTerms(1.ToString())); + answer.AddRange(TermsConverter.ToTerms(distinct.Count.ToString())); + answer.Add(new Term(Functions.Mat, TermType.Function, 2 + distinct.Count)); + } + + result = Entry.Create(answer.ToArray()); + return true; + } + + result = null; + return false; + } + + public void Dispose() + { + } + } +} diff --git a/MatrixExtensions.csproj b/MatrixExtensions.csproj new file mode 100644 index 0000000..8af398f --- /dev/null +++ b/MatrixExtensions.csproj @@ -0,0 +1,106 @@ + + + + + Debug + AnyCPU + {6DD7D794-EB0C-4386-8FAF-64C1C9AEED27} + Library + Properties + MatrixExtensions + MatrixExtensions + v4.5 + 512 + 1.0.* + false + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + + ..\..\..\Main\SMathStudio\canvas\bin\Debug + + C:\Program Files (x86)\SMath Studio + + + C:\Program Files (x86)\SMathStudio + + + + + + + + + + + + $(SMathDir)\SMath.Controls.dll + False + + + $(SMathDir)\SMath.Manager.dll + False + + + $(SMathDir)\SMath.Math.Numeric.dll + False + + + $(SMathDir)\SMath.Math.Symbolic.dll + False + + + + + + + + + + + + + + + + + + %(ProgramInfo.Version) + config.$(ProgramVersion.Replace(".", "_")).ini + + $(SMathDir)\extensions\plugins\$(ProjectGuid.TrimStart("{").TrimEnd("}")) + + $(APPDATA)\SMath\extensions\plugins\$(ProjectGuid.TrimStart("{").TrimEnd("}")) + + + + + + + + + + + + + \ No newline at end of file diff --git a/MatrixExtensions.sln b/MatrixExtensions.sln new file mode 100644 index 0000000..fe135e5 --- /dev/null +++ b/MatrixExtensions.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30320.27 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MatrixExtensions", "MatrixExtensions.csproj", "{6DD7D794-EB0C-4386-8FAF-64C1C9AEED27}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6DD7D794-EB0C-4386-8FAF-64C1C9AEED27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6DD7D794-EB0C-4386-8FAF-64C1C9AEED27}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6DD7D794-EB0C-4386-8FAF-64C1C9AEED27}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6DD7D794-EB0C-4386-8FAF-64C1C9AEED27}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FDF65821-5E33-4A1A-9519-311235CF2749} + EndGlobalSection +EndGlobal diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d732a00 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Общие сведения об этой сборке предоставляются следующим набором +// набора атрибутов. Измените значения этих атрибутов для изменения сведений, +// связанные со сборкой. +[assembly: AssemblyTitle("MatrixExtensions")] +[assembly: AssemblyDescription("Сustom functions for working with matrices")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Aleksandr Ponomarev")] +[assembly: AssemblyProduct("MatrixExtensions")] +[assembly: AssemblyCopyright("Copyright © 2020 SMathStudio")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Установка значения False для параметра ComVisible делает типы в этой сборке невидимыми +// для компонентов COM. Если необходимо обратиться к типу в этой сборке через +// COM, задайте атрибуту ComVisible значение TRUE для этого типа. +[assembly: ComVisible(false)] + +// Следующий GUID служит для идентификации библиотеки типов, если этот проект будет видимым для COM +[assembly: Guid("6dd7d794-eb0c-4386-8faf-64c1c9aeed27")] + +// Сведения о версии сборки состоят из указанных ниже четырех значений: +// +// Основной номер версии +// Дополнительный номер версии +// Номер сборки +// Редакция +// +// Можно задать все значения или принять номера сборки и редакции по умолчанию +// используя "*", как показано ниже: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Utilites.cs b/Utilites.cs new file mode 100644 index 0000000..a4967f4 --- /dev/null +++ b/Utilites.cs @@ -0,0 +1,55 @@ +using SMath.Manager; +using SMath.Math.Numeric; +using System; +using System.Collections.Generic; + +namespace MatrixExtensions +{ + static class Utilites + { + internal static string[] EntryMatrix2ArrStr(Entry matrix) + { + string source = matrix.ToString(); + source = source.Trim(new char[] { 'm', 'a', 't', '(', ')' }); + return source.Split(','); + } + + internal static string[] EntryMatrix2ArrStr(BaseEntry matrix) + { + string source = matrix.ToString(); + source = source.Trim(new char[] { 'm', 'a', 't', '(', ')' }); + return source.Split(','); + } + + internal static double[] EntryVec2Arr(Entry vector) + { + string source = vector.ToString(); + source = source.Trim(new char[] { 'm', 'a', 't', '(', ')' }); + //source = source.Replace("{", ""); + //source = source.Replace("}", ""); + string[] src = source.Split(','); + + int n = src.Length - 2; + double[] res = new double[n]; + for (int i = 0; i < n; i++) + { + if (src[i].IndexOf('^') >= 0) res[i] = 0; + else res[i] = Convert.ToDouble(src[i]); + } + + return res; + } + + internal static double Entry2Double(Entry prime) + { + string source = prime.ToString(); + return Convert.ToDouble(source); + } + + internal static int Entry2Int(Entry prime) + { + string source = prime.ToString(); + return Convert.ToInt32(source); + } + } +}