|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# C# example for AngouriMath\n", |
| 8 | + "\n", |
| 9 | + "While [AngouriMath.Interactive](https://www.nuget.org/packages/AngouriMath.Interactive) is recommended for research purposes, you can also use the default AngouriMath package in C# within interactive environment." |
| 10 | + ] |
| 11 | + }, |
3 | 12 | { |
4 | 13 | "cell_type": "code", |
5 | | - "execution_count": 1, |
6 | 14 | "metadata": {}, |
7 | | - "outputs": [ |
8 | | - { |
9 | | - "data": { |
10 | | - "text/html": [ |
11 | | - "\r\n", |
12 | | - "<div>\r\n", |
13 | | - " <div id='dotnet-interactive-this-cell-18492.Microsoft.DotNet.Interactive.Http.HttpPort' style='display: none'>\r\n", |
14 | | - " The below script needs to be able to find the current output cell; this is an easy method to get it.\r\n", |
15 | | - " </div>\r\n", |
16 | | - " <script type='text/javascript'>\r\n", |
17 | | - "async function probeAddresses(probingAddresses) {\r\n", |
18 | | - " function timeout(ms, promise) {\r\n", |
19 | | - " return new Promise(function (resolve, reject) {\r\n", |
20 | | - " setTimeout(function () {\r\n", |
21 | | - " reject(new Error('timeout'))\r\n", |
22 | | - " }, ms)\r\n", |
23 | | - " promise.then(resolve, reject)\r\n", |
24 | | - " })\r\n", |
25 | | - " }\r\n", |
26 | | - "\r\n", |
27 | | - " if (Array.isArray(probingAddresses)) {\r\n", |
28 | | - " for (let i = 0; i < probingAddresses.length; i++) {\r\n", |
29 | | - "\r\n", |
30 | | - " let rootUrl = probingAddresses[i];\r\n", |
31 | | - "\r\n", |
32 | | - " if (!rootUrl.endsWith('/')) {\r\n", |
33 | | - " rootUrl = `${rootUrl}/`;\r\n", |
34 | | - " }\r\n", |
35 | | - "\r\n", |
36 | | - " try {\r\n", |
37 | | - " let response = await timeout(1000, fetch(`${rootUrl}discovery`, {\r\n", |
38 | | - " method: 'POST',\r\n", |
39 | | - " cache: 'no-cache',\r\n", |
40 | | - " mode: 'cors',\r\n", |
41 | | - " timeout: 1000,\r\n", |
42 | | - " headers: {\r\n", |
43 | | - " 'Content-Type': 'text/plain'\r\n", |
44 | | - " },\r\n", |
45 | | - " body: probingAddresses[i]\r\n", |
46 | | - " }));\r\n", |
47 | | - "\r\n", |
48 | | - " if (response.status == 200) {\r\n", |
49 | | - " return rootUrl;\r\n", |
50 | | - " }\r\n", |
51 | | - " }\r\n", |
52 | | - " catch (e) { }\r\n", |
53 | | - " }\r\n", |
54 | | - " }\r\n", |
55 | | - "}\r\n", |
56 | | - "\r\n", |
57 | | - "function loadDotnetInteractiveApi() {\r\n", |
58 | | - " probeAddresses([\"http://192.168.2.16:1000/\", \"http://127.0.0.1:1000/\"])\r\n", |
59 | | - " .then((root) => {\r\n", |
60 | | - " // use probing to find host url and api resources\r\n", |
61 | | - " // load interactive helpers and language services\r\n", |
62 | | - " let dotnetInteractiveRequire = require.config({\r\n", |
63 | | - " context: '18492.Microsoft.DotNet.Interactive.Http.HttpPort',\r\n", |
64 | | - " paths:\r\n", |
65 | | - " {\r\n", |
66 | | - " 'dotnet-interactive': `${root}resources`\r\n", |
67 | | - " }\r\n", |
68 | | - " }) || require;\r\n", |
69 | | - "\r\n", |
70 | | - " window.dotnetInteractiveRequire = dotnetInteractiveRequire;\r\n", |
71 | | - "\r\n", |
72 | | - " window.configureRequireFromExtension = function(extensionName, extensionCacheBuster) {\r\n", |
73 | | - " let paths = {};\r\n", |
74 | | - " paths[extensionName] = `${root}extensions/${extensionName}/resources/`;\r\n", |
75 | | - " \r\n", |
76 | | - " let internalRequire = require.config({\r\n", |
77 | | - " context: extensionCacheBuster,\r\n", |
78 | | - " paths: paths,\r\n", |
79 | | - " urlArgs: `cacheBuster=${extensionCacheBuster}`\r\n", |
80 | | - " }) || require;\r\n", |
81 | | - "\r\n", |
82 | | - " return internalRequire\r\n", |
83 | | - " };\r\n", |
84 | | - " \r\n", |
85 | | - " dotnetInteractiveRequire([\r\n", |
86 | | - " 'dotnet-interactive/dotnet-interactive'\r\n", |
87 | | - " ],\r\n", |
88 | | - " function (dotnet) {\r\n", |
89 | | - " dotnet.init(window);\r\n", |
90 | | - " },\r\n", |
91 | | - " function (error) {\r\n", |
92 | | - " console.log(error);\r\n", |
93 | | - " }\r\n", |
94 | | - " );\r\n", |
95 | | - " })\r\n", |
96 | | - " .catch(error => {console.log(error);});\r\n", |
97 | | - " }\r\n", |
98 | | - "\r\n", |
99 | | - "// ensure `require` is available globally\r\n", |
100 | | - "if ((typeof(require) !== typeof(Function)) || (typeof(require.config) !== typeof(Function))) {\r\n", |
101 | | - " let require_script = document.createElement('script');\r\n", |
102 | | - " require_script.setAttribute('src', 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js');\r\n", |
103 | | - " require_script.setAttribute('type', 'text/javascript');\r\n", |
104 | | - " \r\n", |
105 | | - " \r\n", |
106 | | - " require_script.onload = function() {\r\n", |
107 | | - " loadDotnetInteractiveApi();\r\n", |
108 | | - " };\r\n", |
109 | | - "\r\n", |
110 | | - " document.getElementsByTagName('head')[0].appendChild(require_script);\r\n", |
111 | | - "}\r\n", |
112 | | - "else {\r\n", |
113 | | - " loadDotnetInteractiveApi();\r\n", |
114 | | - "}\r\n", |
115 | | - "\r\n", |
116 | | - " </script>\r\n", |
117 | | - "</div>" |
118 | | - ] |
119 | | - }, |
120 | | - "metadata": {}, |
121 | | - "output_type": "display_data" |
122 | | - }, |
123 | | - { |
124 | | - "name": "stderr", |
125 | | - "output_type": "stream", |
126 | | - "text": [ |
127 | | - "\r\n", |
128 | | - "(1,1): error CS0006: Не удалось найти файл метаданных \"nuget:AngouriMath.Interactive, *-*\".\r\n", |
129 | | - "\r\n" |
130 | | - ] |
131 | | - }, |
132 | | - { |
133 | | - "ename": "Cell not executed", |
134 | | - "evalue": "compilation error", |
135 | | - "output_type": "error", |
136 | | - "traceback": [] |
137 | | - } |
138 | | - ], |
139 | 15 | "source": [ |
140 | 16 | "#r \"nuget:AngouriMath, *-*\"" |
| 17 | + ], |
| 18 | + "execution_count": null, |
| 19 | + "outputs": [] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "### Basics" |
141 | 26 | ] |
142 | 27 | }, |
143 | 28 | { |
144 | 29 | "cell_type": "code", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "Entity expr = \"a2 + sqrt(b) / 6 = 4\";\n", |
| 33 | + "expr" |
| 34 | + ], |
145 | 35 | "execution_count": null, |
| 36 | + "outputs": [] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
146 | 40 | "metadata": {}, |
147 | | - "outputs": [], |
148 | 41 | "source": [ |
149 | | - "using AngouriMath.Extensions;\n", |
150 | | - "using AngouriMath;" |
151 | | - ] |
| 42 | + "expr.Solve(\"a\")" |
| 43 | + ], |
| 44 | + "execution_count": null, |
| 45 | + "outputs": [] |
152 | 46 | }, |
153 | 47 | { |
154 | 48 | "cell_type": "code", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "\"sin(a x) / sqrt(x)\".ToEntity().Differentiate(\"x\")" |
| 52 | + ], |
155 | 53 | "execution_count": null, |
| 54 | + "outputs": [] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
156 | 58 | "metadata": {}, |
157 | | - "outputs": [], |
158 | 59 | "source": [ |
159 | | - "\"x2 + sin(sqrt(x + 1/2)) + integral(sqrt(x + derivative(sin(y), y)), x)\".ToEntity()" |
| 60 | + "\"a + b\".ToEntity().Substitute(\"a\", 5).Substitute(\"b\", 6).Evaled" |
| 61 | + ], |
| 62 | + "execution_count": null, |
| 63 | + "outputs": [] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "@\"{ 1, 2 } \\/ RR \\/ [3; +oo)\".ToEntity()" |
| 70 | + ], |
| 71 | + "execution_count": null, |
| 72 | + "outputs": [] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "var system = MathS.Equations(\n", |
| 79 | + " \"a + b\",\n", |
| 80 | + " \"sin(b) + c\"\n", |
| 81 | + ");\n", |
| 82 | + "system.Solve(\"a\", \"b\")" |
| 83 | + ], |
| 84 | + "execution_count": null, |
| 85 | + "outputs": [] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "### Expressions, systems and matrices" |
160 | 92 | ] |
161 | 93 | }, |
162 | 94 | { |
163 | 95 | "cell_type": "code", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "\"x2 + sin(sqrt(x + 1/2)) + integral(sqrt(x + derivative(sin(y), y)), x)\".ToEntity()" |
| 99 | + ], |
164 | 100 | "execution_count": null, |
| 101 | + "outputs": [] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
165 | 105 | "metadata": {}, |
166 | | - "outputs": [], |
167 | 106 | "source": [ |
168 | 107 | "var sys = MathS.Equations(\n", |
169 | 108 | " \"x + 2 - y - y2\",\n", |
170 | 109 | " \"x / y - sin(z) + 3\",\n", |
171 | 110 | " \"x - sin(z)\"\n", |
172 | 111 | ");\n", |
173 | 112 | "sys" |
174 | | - ] |
| 113 | + ], |
| 114 | + "execution_count": null, |
| 115 | + "outputs": [] |
175 | 116 | }, |
176 | 117 | { |
177 | 118 | "cell_type": "code", |
178 | | - "execution_count": null, |
179 | 119 | "metadata": {}, |
180 | | - "outputs": [], |
181 | 120 | "source": [ |
182 | 121 | "MathS.Matrix(new Entity[,]{\n", |
183 | 122 | "{ \"x / 2\", \"beta - 2^3\", \"e / sqrt(pi)\" },\n", |
184 | 123 | "{ \"epsilon\", \"integral(sin(k), k)\", \"e - pi\" },\n", |
185 | 124 | "})" |
186 | | - ] |
| 125 | + ], |
| 126 | + "execution_count": null, |
| 127 | + "outputs": [] |
187 | 128 | } |
188 | 129 | ], |
189 | 130 | "metadata": { |
|
193 | 134 | "name": ".net-csharp" |
194 | 135 | }, |
195 | 136 | "language_info": { |
196 | | - "file_extension": ".cs", |
197 | | - "mimetype": "text/x-csharp", |
198 | | - "name": "C#", |
199 | | - "pygments_lexer": "csharp", |
200 | | - "version": "8.0" |
| 137 | + "name": "C#" |
201 | 138 | } |
202 | 139 | }, |
203 | 140 | "nbformat": 4, |
|
0 commit comments