Pull to refresh

AngouriMath 1.3 update

Reading time5 min
Views4.2K

What is it about?

AngouriMath is a free open-source symbolic algebra library, written in and for .NET, that I've been working since the end of 2019. The previous major release happened on the second of March, I made an article about it too.

Now, let me tell what we, together with contributors, did within these four months!

AngouriMath is made for C#, F#, Interactive, and to an extent, C++, so let me draw a distinction, what package affects what environment:

C#

F#

Interactive

C++

AngouriMath

AngouriMath.FSharp

AngouriMath.Interactive

AngouriMath.CPP

Matrices

This is the main change in terms of breaking API and behaviour. Now, instead of tensor, there's matrix, which is Entity.Matrix. There is also vector, but it doesn't have its own type. Instead, it's a single-column matrix. So for matrices there's only one type.

The syntax for parsing is

Entity a = "[1, 2, 3]";

it becomes a vector of three elements.

Entity a = "[1, 2, 3]T"

becomes a transposed vector, that is, instead of column vector, it's a row vector. Here's how we create a 2x2 matrix:

Entity a = "[[1, 2], [3, 4]]"

[1, 2] is a row.

The properties and functions of matrix:

Entity.Matrix m = ...
m.Adjugate   // null if it doesn't exist
m.AsScalar() // casts a 1x1 to scalar
m.Inverse    // null if it doesn't exist
m.Pow(int)   // finds a matrix power or throws
m.ReducedRowEchelonForm

See more at the docs.

There are also improvements in AngouriMath.FSharp's matrices. All matrix-related functions moved to AngouriMath.FSharp.Matrices. Now there is a number of operators for matrices specifically all of which end with dot. For example,

a +. b
a -. b
a *. b
a /. b
a **. b // matrix power
a ***. b // tensor product
a ****. b // tensor power

Parser and LaTeX

The parser now supports Cyrillic and Greek letters, as well as it skips \n and \r letters, which might be helpful for building matrices.

There's also now a setting MathS.Settings.ExplicitParsingOnly, which, when set to true, does not insert omitted operators, so while

Entity a = "2x + 3(x + 1)";

is fine when the setting is disabled, it won't be considered valid with the setting enabled.

There is also MathS.Parse which returns an Either of Entity and reason why parsing failed. Either is an anonymous type union from Honk#, which I'll mention later. Example of code using it:

using AngouriMath;
using System;

var res = MathS.Parse("2x + 3")
    .Switch(
        valid => valid,
        failure => failure.Reason.Switch<Entity>(
                unknown => $"Unknown reason: {unknown.Reason}",
                missingOperator => $"Operator missed: {missingOperator.Details}",
                internalError => $"Internal error: {internalError.Details}"
        )
    );

Console.WriteLine(res);

Latexise now doesn't produce unneeded \times, and instead, \dot-s are only produced when needed (e. g. between variables). A few bugs in it fixed.

New Functions

MathS.Series.Maclaurin, MathS.Series.Taylor , MathS.Series.TaylorTerms are functions to generate Taylor expansion expressions.

A lot of experimental API added for number theory. The final function is the symbolic form of sine and cosine based on existing analytical angles, for example, here's the precise value of sin(17/42 pi):

1/2 * ((sqrt(1 - sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) ^ 2) ^ 2 - sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) ^ 2) * sqrt(1 - sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) ^ 2) - 2 * sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) * sqrt(1 - sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) ^ 2) * sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2))) - -(2 * sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) * sqrt(1 - sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) ^ 2) * sqrt(1 - sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) ^ 2) + (sqrt(1 - sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) ^ 2) ^ 2 - sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2)) ^ 2) * sqrt(1/2 - 1/2 * sqrt(1 - sqrt(1 - (1/6 * (-1 + ((7 + 21 * sqrt(-3)) / 2) ^ (1/3) + ((7 - 21 * sqrt(-3)) / 2) ^ (1/3))) ^ 2) ^ 2))) * sqrt(3) / 2

All added functions: SymbolicFormOfSineSymbolicFormOfCosineExpandSineOfSumExpandConsineOfSumExpandSineArgumentMultipliedExpandCosineArgumentMultipliedGetSineOfHalvedAngleGetCosineOfHalvedAngleDecomposeRationalGetSineOfHalvedAngle

A few synonyms added for hyperbolic functions. All functions from AngouriMath.FSharp now get InnerSimplified applied (for instance, sqrt 4 will return 2 while MathS.Sqrt(4) will still return a power node).

AngouriMath.Interactive now has a AggressiveOperators module, which overrides the default operators, such as +, -, *, /, **, <, >, >=, <=, =. It is not recommended to use it as a library, but might be useful for some small calculations, like I did it in the terminal.

AngouriMath.CPP: wrapper for C++

Now there's a native version of the library that can be consumed from unmanaged code. It is possible thanks to NativeAOT, new feature which allows to compile managed .NET assemblies into native code. Here's an example:

#include <AngouriMath.h>
AngouriMath::Entity expr("x * y + 2sin(x * y) - y^x");
std::cout << expr.Differentiate("x") << "\n";

Output:

y + cos(x * y) * y * 2 - y ^ x * ln(y)

It is currently in the experimental phase. You can try it by installing it following a short instruction. Very few functions are still exposed, so it can be barely used for now, but hope it will be developed further in the future.

My Reddit post about it.

Using AngouriMath in your projects

Now, aside from netstandard2.0, it multitargets .NET Framework 4.7.2+ and netstandard2.1. The former allows to use the library in legacy projects more conveniently.

Honk# now helps to maintain the project easier, but since it's shipped with AngouriMath now, it might be a good idea to try its syntax in some cases.

Conclusion

There are a lot of minor features and fixes that are not very interesting to read about. Most of them and other updates are listed in the what's new page on the website.

Thank you for your attention! Feel free to ask questions.

References

  1. Github of the project

  2. Website

  3. Release notes

  4. Nuget (C#, F#, Interactive)

  5. Try in binder

  6. NativeAOT for native compilation

  7. dotnet/interactive for Jupyter and notebooks

  8. Honk# for convenient coding

  9. My twitter

Thanks

Aside from myself, I want to thank these people for helping with the project:

Contributors: Happypig375, ryynmapes, MomoDeve, TheSeems, KuhakuPixel, jaimeadf, yoshiask.

Consultants: Happypig375, mednik.

Sponsors: zyzhu.

Tags:
Hubs:
Total votes 5: ↑5 and ↓0+5
Comments0

Articles