dharmatech / Symbolism

Computer Algebra and Symbolic Computation in C#
220 stars 31 forks source link

ported to C# 4 #3

Open AlekseyCherepanov opened 8 years ago

AlekseyCherepanov commented 8 years ago

Avoiding features: "using static", string interpolations with $ and null-checks with ?. op.

String interpolations were replaced with string.Format.

null-checks are done use if statement.

"using static" is a bit trickier:

Also avoided => for method definitions.

I applied the following one-liners to parts of code, not to full files (C-| in emacs): perl -pe '@a = (); $a = 0; s/\$//; s/{([^}]+)}/push @a, $1; "{" . $a++ . "}"/ge; s/"."/"string.Format($&, " . (join ", ", @a) . ")"/ge' perl -lpe 's/=> /{ return / && s/\s$/ }/'

Feel free to ignore this pull-request if you would like to keep your code as C# 6. I find "using static" feature useful, while my inheritance is a dirty hack.

dharmatech commented 8 years ago

Wow, this is impressive Aleksey! Very cool that you were able to backport everything. I'll definitely leave this pull request here as a reference in case anyone else needs to port to C# 4. If you're free and willing to comment, would love to know more about why you needed to stick with C# 4.0.

AlekseyCherepanov commented 8 years ago

On Wed, Nov 04, 2015 at 07:52:24PM -0800, dharmatech wrote:

Wow, this is impressive Aleksey!

Much more impressive is that you have a firm style. :-) It simplified things greatly.

Very cool that you were able to backport everything. I'll definitely leave this pull request here as a reference in case anyone else needs to port to C# 4.

Thank you!

If you're free and willing to comment, would love to know more about why you needed to stick with C# 4.0.

C# 6 was not convenient for me because I teach students and there is Visual Studio 2012 in classrooms, VS 2012 does not support C# 6. On my computer, I have Mono, my Mono supports C# 4 (I heard that newer Mono understands C# 5 though). So I just ported it.

Also I tried other systems: this one http://www.codeproject.com/Articles/604792/Computer-Algebra-System-for-NET-Framework and Math.NET Symbolics https://github.com/mathnet/mathnet-symbolics But they did not provide functionality I was looking for.

I teach students Difference equations (not differential equations) and considered to touch symbolic computations a bit. There are a lot of sums and products. So we define a formula with custom functions to represent them. The formula of solution for inhomogeneous linear first-order difference equation (for instance, u(x + 1) = 2 * u(x) + 1):

var f_solution = P(k, x0, x - 1, p(k)) * (C + S(k, x0, x - 1, f(k) * (P(m, x0, k, p(m))^(-1)) ));

And then, we manipulate the formula. I had to write some code to make it work, but it goes rather well (except that operator priority caught me: 1 - q^n turned out to be (1 - q)^n while I meant 1 - (q^n)).

I'll open some issues to talk about problems and possible new features.

Thanks!

Regards, Aleksey Cherepanov

dharmatech commented 8 years ago

(except that operator priority caught me: 1 - q^n turned out to be (1 - q)^n while I meant 1 - (q^n))

Yes, unfotunately the ^ operator in C# has quite a low precedence. I've requested an exponentiation operator from the C# team here: https://github.com/dotnet/roslyn/issues/4594

If they decide to add an operator for exponentiation, it will likely be **. Feel free to lobby them in that issue. ;-)

dharmatech commented 8 years ago

I'll open some issues to talk about problems and possible new features.

Yes, please do!

dharmatech commented 8 years ago

C# 6 was not convenient for me because I teach students and there is Visual Studio 2012 in classrooms, VS 2012 does not support C# 6. ... I teach students Difference equations (not differential equations) and considered to touch symbolic computations a bit.

Wow, that's very cool. This is the first I've ever heard of Symbolism being used in a classroom. I wasn't expecting that! But I'm glad to hear it suited your needs.