Skip to content

Commit 4b63514

Browse files
committed
Initial commit: MathFlow - C# math expression library
Features: - Expression parsing and evaluation - Symbolic mathematics (differentiation, simplification) - Numerical methods (integration, equation solving) - Complex number support - Vector operations - Statistical functions - LaTeX/MathML conversion - Full test suite (50 tests passing)
0 parents  commit 4b63514

32 files changed

Lines changed: 4232 additions & 0 deletions

.gitignore

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
4+
# User-specific files
5+
*.rsuser
6+
*.suo
7+
*.user
8+
*.userosscache
9+
*.sln.docstates
10+
11+
# User-specific files (MonoDevelop/Xamarin Studio)
12+
*.userprefs
13+
14+
# Build results
15+
[Dd]ebug/
16+
[Dd]ebugPublic/
17+
[Rr]elease/
18+
[Rr]eleases/
19+
x64/
20+
x86/
21+
[Aa][Rr][Mm]/
22+
[Aa][Rr][Mm]64/
23+
bld/
24+
[Bb]in/
25+
[Oo]bj/
26+
[Ll]og/
27+
[Ll]ogs/
28+
29+
# Visual Studio 2015/2017 cache/options directory
30+
.vs/
31+
32+
# Visual Studio Code
33+
.vscode/
34+
35+
# Rider
36+
.idea/
37+
*.sln.iml
38+
39+
# MSTest test Results
40+
[Tt]est[Rr]esult*/
41+
[Bb]uild[Ll]og.*
42+
43+
# NUnit
44+
*.VisualState.xml
45+
TestResult.xml
46+
nunit-*.xml
47+
48+
# Build Results of an ATL Project
49+
[Dd]ebugPS/
50+
[Rr]eleasePS/
51+
dlldata.c
52+
53+
# .NET Core
54+
project.lock.json
55+
project.fragment.lock.json
56+
artifacts/
57+
58+
# Files built by Visual Studio
59+
*_i.c
60+
*_p.c
61+
*_h.h
62+
*.ilk
63+
*.meta
64+
*.obj
65+
*.iobj
66+
*.pch
67+
*.pdb
68+
*.ipdb
69+
*.pgc
70+
*.pgd
71+
*.rsp
72+
*.sbr
73+
*.tlb
74+
*.tli
75+
*.tlh
76+
*.tmp
77+
*.tmp_proj
78+
*_wpftmp.csproj
79+
*.log
80+
*.tlog
81+
*.vspscc
82+
*.vssscc
83+
.builds
84+
*.pidb
85+
*.svclog
86+
*.scc
87+
88+
# NuGet Packages
89+
*.nupkg
90+
*.snupkg
91+
# The packages folder can be ignored because of Package Restore
92+
**/[Pp]ackages/*
93+
# except build/, which is used as an MSBuild target.
94+
!**/[Pp]ackages/build/
95+
# Uncomment if necessary however generally it will be regenerated when needed
96+
#!**/[Pp]ackages/repositories.config
97+
# NuGet v3's project.json files produces more ignorable files
98+
*.nuget.props
99+
*.nuget.targets
100+
101+
# Microsoft Azure Build Output
102+
csx/
103+
*.build.csdef
104+
105+
# Windows Store app package directories and files
106+
AppPackages/
107+
BundleArtifacts/
108+
Package.StoreAssociation.xml
109+
_pkginfo.txt
110+
*.appx
111+
*.appxbundle
112+
*.appxupload
113+
114+
# Visual Studio cache files
115+
# files ending in .cache can be ignored
116+
*.[Cc]ache
117+
# but keep track of directories ending in .cache
118+
!?*.[Cc]ache/
119+
120+
# Others
121+
ClientBin/
122+
~$*
123+
*~
124+
*.dbmdl
125+
*.dbproj.schemaview
126+
*.jfm
127+
*.pfx
128+
*.publishsettings
129+
orleans.codegen.cs
130+
131+
# Node.js Tools for Visual Studio
132+
.ntvs_analysis.dat
133+
node_modules/
134+
135+
# Python Tools for Visual Studio (PTVS)
136+
__pycache__/
137+
*.pyc
138+
139+
# Local History for Visual Studio
140+
.localhistory/
141+
142+
# BeatPulse healthcheck temp database
143+
healthchecksdb
144+
145+
# Backup folder for Package Reference Convert tool in Visual Studio 2017
146+
MigrationBackup/
147+
148+
# macOS
149+
.DS_Store

Examples/Examples.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\MathFlow.Core\MathFlow.Core.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

Examples/Program.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using MathFlow.Core;
2+
using MathFlow.Core.ComplexMath;
3+
using MathFlow.Core.Extensions;
4+
using MathFlow.Core.LinearAlgebra;
5+
using MathFlow.Core.Statistics;
6+
7+
namespace MathFlow.Examples;
8+
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
Console.WriteLine("=== MathFlow Examples ===\n");
14+
15+
// Basic expression evaluation
16+
BasicExamples();
17+
18+
// Symbolic mathematics
19+
SymbolicMathExamples();
20+
21+
// Complex numbers
22+
ComplexNumberExamples();
23+
24+
// Vector operations
25+
VectorExamples();
26+
27+
// Statistical calculations
28+
StatisticsExamples();
29+
}
30+
31+
static void BasicExamples()
32+
{
33+
Console.WriteLine("## Basic Expression Evaluation");
34+
var engine = new MathEngine();
35+
36+
Console.WriteLine($"2 + 3 * 4 = {engine.Calculate("2 + 3 * 4")}");
37+
Console.WriteLine($"sin(pi/2) = {engine.Calculate("sin(pi/2)")}");
38+
Console.WriteLine($"sqrt(16) + log10(100) = {engine.Calculate("sqrt(16) + log10(100)")}");
39+
40+
// With variables
41+
var vars = new Dictionary<string, double> { ["x"] = 3, ["y"] = 4 };
42+
Console.WriteLine($"x^2 + y^2 where x=3, y=4 = {engine.Calculate("x^2 + y^2", vars)}");
43+
Console.WriteLine();
44+
}
45+
46+
static void SymbolicMathExamples()
47+
{
48+
Console.WriteLine("## Symbolic Mathematics");
49+
var engine = new MathEngine();
50+
51+
// Differentiation
52+
var derivative = engine.Differentiate("x^3 - 2*x^2 + x - 1", "x");
53+
Console.WriteLine($"d/dx(x^3 - 2x^2 + x - 1) = {derivative}");
54+
55+
// Simplification
56+
var simplified = engine.Simplify("2*x + 3*x - x");
57+
Console.WriteLine($"Simplify: 2x + 3x - x = {simplified}");
58+
59+
// Integration
60+
var integral = engine.Integrate("x^2", "x", 0, 1);
61+
Console.WriteLine($"∫[0,1] x^2 dx = {integral:F6}");
62+
63+
// Root finding
64+
var root = engine.FindRoot("x^2 - 2", 1);
65+
Console.WriteLine($"Root of x^2 - 2 = {root:F6} (√2)");
66+
Console.WriteLine();
67+
}
68+
69+
static void ComplexNumberExamples()
70+
{
71+
Console.WriteLine("## Complex Numbers");
72+
73+
var z1 = new ComplexNumber(3, 4);
74+
var z2 = new ComplexNumber(1, -2);
75+
76+
Console.WriteLine($"z1 = {z1}");
77+
Console.WriteLine($"z2 = {z2}");
78+
Console.WriteLine($"|z1| = {z1.Magnitude:F2}");
79+
Console.WriteLine($"z1 + z2 = {z1 + z2}");
80+
Console.WriteLine($"z1 * z2 = {z1 * z2}");
81+
Console.WriteLine($"z1 / z2 = {z1 / z2}");
82+
Console.WriteLine($"z1* (conjugate) = {z1.Conjugate}");
83+
84+
var polar = ComplexNumber.FromPolar(5, Math.PI / 4);
85+
Console.WriteLine($"From polar(5, π/4) = {polar}");
86+
Console.WriteLine();
87+
}
88+
89+
static void VectorExamples()
90+
{
91+
Console.WriteLine("## Vector Operations");
92+
93+
var v1 = new Vector(3, 4, 0);
94+
var v2 = new Vector(1, 2, 2);
95+
96+
Console.WriteLine($"v1 = {v1}");
97+
Console.WriteLine($"v2 = {v2}");
98+
Console.WriteLine($"|v1| = {v1.Magnitude:F2}");
99+
Console.WriteLine($"v1 + v2 = {v1 + v2}");
100+
Console.WriteLine($"v1 · v2 = {v1.Dot(v2)}");
101+
Console.WriteLine($"v1 × v2 = {v1.Cross(v2)}");
102+
Console.WriteLine($"Angle = {v1.AngleTo(v2).ToDegrees():F1}°");
103+
Console.WriteLine($"v1 normalized = {v1.Normalized()}");
104+
Console.WriteLine();
105+
}
106+
107+
static void StatisticsExamples()
108+
{
109+
Console.WriteLine("## Statistical Functions");
110+
111+
var data = new[] { 2.5, 3.1, 4.2, 3.8, 5.1, 3.9, 4.5, 3.3 };
112+
var data2 = new[] { 5.2, 6.1, 7.8, 7.2, 9.3, 7.5, 8.1, 6.8 };
113+
114+
Console.WriteLine($"Data: [{string.Join(", ", data)}]");
115+
Console.WriteLine($"Mean = {StatisticalFunctions.Mean(data):F2}");
116+
Console.WriteLine($"Median = {StatisticalFunctions.Median(data):F2}");
117+
Console.WriteLine($"StdDev = {StatisticalFunctions.StandardDeviation(data):F2}");
118+
119+
var (q1, q2, q3) = StatisticalFunctions.Quartiles(data);
120+
Console.WriteLine($"Quartiles: Q1={q1:F2}, Q2={q2:F2}, Q3={q3:F2}");
121+
122+
Console.WriteLine($"Correlation with data2 = {StatisticalFunctions.Correlation(data, data2):F3}");
123+
124+
var (slope, intercept) = StatisticalFunctions.LinearRegression(data, data2);
125+
Console.WriteLine($"Linear regression: y = {slope:F2}x + {intercept:F2}");
126+
}
127+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 MathFlow
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)