-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
254 lines (198 loc) · 7.74 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// amcli - command line interface for CAS AngouriMath
// Copyright (C) 2022 Angouri
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using AngouriMath;
using AngouriMath.Extensions;
using HonkSharp;
using HonkSharp.Functional;
var cliArgs = System.Environment.GetCommandLineArgs();
var reader = new ArgReader(cliArgs);
Entity expr;
Entity.Variable v;
string res;
var cmd = reader.Next();
switch (cmd)
{
case "help" or "-h" or "--help":
Console.WriteLine("""
.d8b. .88b d88. .o88b. db d888888b
d8' `8b 88'YbdP`88 d8P Y8 88 `88'
88ooo88 88 88 88 8P 88 88
88~~~88 88 88 88 8b 88 88
88 88 88 88 88 Y8b d8 88booo. .88.
YP YP YP YP YP `Y88P' Y88888P Y888888P
amcli (c) 2022 Angouri
This is free software. You're free to use, modify and redistribute it.
GNU GPL v3 license. Made with AngouriMath (MIT Expat) and .NET (MIT Expat).
Why use it?
- Free and cross-platform
- CLI interface for script automations
- Piping for complex operations
- Fast and small
COMMANDS
EVAL
amcli eval - to evaluate into a single number, boolean, or a+bi format
for complex numbers. Expects one argument.
Example:
$ amcli eval "1 / 2"
0.5
$ amcli eval "e ^ pi > pi ^ e"
true
DIFF
amcli diff - to differentiate the expression over the given variable
(the first argument). Expects two arguments.
Example:
$ amcli diff "x" "sin(x)"
cos(x)
$ amcli diff "x" "1 + x^2"
2 * x
$ echo "1 + x^2" | amcli diff "x"
2 * x
SIMP
amcli simp - to simplify the expression. Expects one argument.
Example:
$ amcli simp "sin(x)^2 + cos(x)^2"
1
FSIMP
amcli fsimp - to simplify the expression "faster". This one works
closer to eval than to simp, but unlike eval, it won't try to
collapse to a single number or boolean (e. g. sqrt(3) will stay as
it is). Expects one argument.
Example:
$ amcli fsimp "sin(x)^2 + cos(x)^2"
1
SOLVE
amcli solve - to solve a *statement* over the given variable. A
*statement* is an expression, otherwise evaluable to true or false
(e. g. "x > 3" is a statement, but "x ^ 2" is not).
When the solution set is a finite solution, all solutions are written
line-by-line. Otherwise, it's written as one line.
Example:
$ amcli solve "x" "x2 - 1 = 0"
1
-1
$ amcli solve x "x2 > 1"
(-oo; -1) \/ (1; +oo)
LATEX
amcli latex - to convert an expression into LaTeX format. Expects one
argument.
Example:
$ amcli latex "1/2"
\frac{1}{2}
$ amcli latex "(sqrt(3) + x) / limit(sin(x) / x, x, 0)"
\frac{\sqrt{3}+x}{\lim_{x\to 0} \left[\frac{\sin\left(x\right)}{x}\right]}
SUB
amcli sub - to substitute an expression instead of a variable. Expects
three arguments (variable to substitute, expression to be substituted
instead of the variable, expression).
Example:
$ amcli sub x pi "sin(x / 3)"
sin(pi / 3)
$ amcli sub x "pi / 3" "sin(x)"
sin(pi / 3)
PIPING
Any argument can be received either as a CLI argument or through
standard input. For example,
amcli eval "1 + 1"
is equivalent to
echo "1 + 1" | amcli eval
This allows to pipe complex evaluations:
echo "sin(x) * cos(y)" \ # 0. initial expression
| amcli diff x \ # 1. differentiate over x
| amcli sub x y \ # 2. substitute y instead of x
| amcli diff y \ # 3. differentiate over y
| amcli sub y "pi/3" \ # 4. substitute pi/3 instead of y
| amcli simplify # 5. simplify
Prints
-1/2 * sqrt(3)
Special symbol "_" (underscore) can be used to use stdinput instead
of an argument. For instance, if you want to substitute the result of
an operation into another expression:
echo "e^x" \
| amcli sub u _ "u / (1 + u)" \
| amcli sub x 10 \
| amcli eval
Here the result of `echo` is substituted instead of the second argument
of `amcli sub`, not the last one.
OTHER
You can bind amcli to @ using aliases. On Unix-like operating systems,
add
alias @=amcli
(or specify the full path)
""");
break;
case "eval":
expr = reader.Next().ToEntity().Evaled;
res = expr.ToString();
if (expr is Entity.Number.Rational rat)
res = rat.RealPart.EDecimal.ToString();
Console.WriteLine(res);
break;
case "diff":
v = (Entity.Variable)reader.Next();
expr = reader.Next().ToEntity();
Console.WriteLine(expr.Differentiate(v));
break;
case "solve":
v = (Entity.Variable)reader.Next();
expr = reader.Next().ToEntity();
var sols = expr.Solve(v);
if (sols is Entity.Set.FiniteSet fs)
foreach (var sol in fs)
Console.WriteLine(sol);
else
Console.WriteLine(sols);
break;
case "latex":
expr = reader.Next().ToEntity();
Console.WriteLine(expr.Latexise());
break;
case "simp":
expr = reader.Next().ToEntity();
Console.WriteLine(expr.Simplify());
break;
case "fsimp":
expr = reader.Next().ToEntity();
Console.WriteLine(expr.InnerSimplified);
break;
case "sub":
v = (Entity.Variable)reader.Next();
var withWhat = reader.Next().ToEntity();
expr = reader.Next();
Console.WriteLine(expr.Substitute(v, withWhat));
break;
default:
Console.WriteLine($"Unrecognized command `{cmd}`");
break;
}
public sealed class ArgReader
{
private readonly string[] args;
private int curr = 1;
public ArgReader(string[] args)
=> this.args = args;
public string Next()
{
if (curr < args.Length)
{
var res = args[curr];
if (res is "_")
res = Console.ReadLine()!;
curr++;
return res;
}
return Console.ReadLine()!;
}
}