├── CODEOWNERS
├── docs
├── index.md
├── images
│ ├── logo.png
│ └── favicon.ico
├── api
│ └── .gitignore
├── toc.yml
├── .gitignore
└── articles
│ └── toc.yml
├── xFunc.png
├── CI
└── codecov.yml
├── xFunc.Cli
├── Properties
│ └── launchSettings.json
└── Options
│ ├── DebugInfoOptions.cs
│ ├── BaseOptions.cs
│ ├── InteractiveOptions.cs
│ ├── ParseOptions.cs
│ ├── SolveOptions.cs
│ └── RunFileOptions.cs
├── .github
├── dependabot.yml
├── ISSUE_TEMPLATE
│ └── bug_report.md
├── FUNDING.yml
└── workflows
│ ├── codeql.yml
│ └── github-pages.yml
├── Directory.Build.targets
├── xFunc.Maths
├── Analyzers
│ ├── ISimplifier.cs
│ ├── IDifferentiator.cs
│ ├── Formatters
│ │ └── IFormatter.cs
│ ├── TypeAnalyzers
│ │ └── ITypeAnalyzer.cs
│ └── DifferentiatorContext.cs
├── Results
│ └── Result.EmptyResult.cs
├── IParser.cs
├── Expressions
│ ├── EmptyValue.cs
│ ├── Units
│ │ ├── Converters
│ │ │ ├── IConverter.cs
│ │ │ ├── IConverter{TValue}.cs
│ │ │ ├── AreaConverter.cs
│ │ │ ├── MassConverter.cs
│ │ │ ├── TimeConverter.cs
│ │ │ ├── AngleConverter.cs
│ │ │ ├── PowerConverter.cs
│ │ │ ├── LengthConverter.cs
│ │ │ ├── VolumeConverter.cs
│ │ │ ├── Converter.cs
│ │ │ └── TemperatureConverter.cs
│ │ ├── AreaUnits
│ │ │ └── Area.cs
│ │ ├── MassUnits
│ │ │ └── Mass.cs
│ │ ├── TimeUnits
│ │ │ └── Time.cs
│ │ ├── AngleUnits
│ │ │ └── Angle.cs
│ │ ├── PowerUnits
│ │ │ └── Power.cs
│ │ ├── VolumeUnits
│ │ │ └── Volume.cs
│ │ ├── LengthUnits
│ │ │ └── Length.cs
│ │ ├── TemperatureUnits
│ │ │ └── Temperature.cs
│ │ ├── UnitIsNotSupportedException.cs
│ │ └── ValueIsNotSupportedException.cs
│ ├── Parameters
│ │ └── ParameterType.cs
│ ├── Programming
│ │ ├── Dec.cs
│ │ ├── Inc.cs
│ │ ├── AddAssign.cs
│ │ ├── DivAssign.cs
│ │ ├── MulAssign.cs
│ │ ├── SubAssign.cs
│ │ ├── LeftShiftAssign.cs
│ │ └── RightShiftAssign.cs
│ ├── Matrices
│ │ └── InvalidMatrixException.cs
│ └── LogicalAndBitwise
│ │ └── Not.cs
├── Tokenization
│ ├── Lexer.StringToken.cs
│ └── Lexer.SymbolToken.cs
├── InvalidResultException.cs
└── ParseException.cs
├── xFunc.Benchmark
├── Benchmarks
│ ├── CtorBenchmark.cs
│ ├── GcdBenchmark.cs
│ ├── DifferentiatorBenchmark.cs
│ ├── SimplifierBenchmark.cs
│ ├── ProcessorBenchmark.cs
│ ├── StatisticalBenchmark.cs
│ ├── TransposeBenchmark.cs
│ ├── MatrixBenchmark.cs
│ └── VectorBenchmark.cs
├── Program.cs
└── xFunc.Benchmark.csproj
├── xFunc.Tests
├── Expressions
│ ├── BaseExpressionTests.cs
│ ├── Domains
│ │ └── DomainBuilderTests.cs
│ ├── UnaryTest.cs
│ ├── ToRationalTest.cs
│ ├── ComplexNumbers
│ │ ├── ToComplexTest.cs
│ │ ├── ConjugateTest.cs
│ │ ├── ReTest.cs
│ │ ├── ReciprocalTest.cs
│ │ ├── ImTest.cs
│ │ └── PhaseTest.cs
│ ├── LogicalAndBitwise
│ │ ├── NOrTest.cs
│ │ ├── NAndTest.cs
│ │ ├── ImplicationTest.cs
│ │ ├── EqualityTest.cs
│ │ └── NotTest.cs
│ ├── RationalTest.cs
│ ├── Statistical
│ │ ├── CountTest.cs
│ │ ├── SumTest.cs
│ │ ├── ProductTest.cs
│ │ ├── AvgTest.cs
│ │ ├── MinTest.cs
│ │ ├── VarTest.cs
│ │ ├── MaxTest.cs
│ │ ├── VarpTest.cs
│ │ ├── StdevpTest.cs
│ │ └── StdevTest.cs
│ ├── LbTest.cs
│ ├── ExpTest.cs
│ ├── Parameters
│ │ └── ParameterValueTest.cs
│ ├── Trigonometric
│ │ ├── ArccosTest.cs
│ │ ├── ArcsecTest.cs
│ │ ├── ArccotTest.cs
│ │ ├── ArccscTest.cs
│ │ ├── ArcsinTest.cs
│ │ └── ArctanTest.cs
│ ├── Hyperbolic
│ │ ├── HyperbolicArcosineTest.cs
│ │ ├── HyperbolicArsecantTest.cs
│ │ ├── HyperbolicArtangentTest.cs
│ │ ├── HyperbolicArcosecantTest.cs
│ │ ├── HyperbolicArcotangentTest.cs
│ │ └── HyperbolicArsineTest.cs
│ ├── Programming
│ │ ├── LeftShiftTest.cs
│ │ └── RightShiftTest.cs
│ ├── LnTest.cs
│ ├── ToOctTest.cs
│ ├── LgTest.cs
│ ├── BinaryTest.cs
│ ├── FactTest.cs
│ ├── SimplifyTest.cs
│ ├── ToHexTest.cs
│ ├── Units
│ │ ├── Converters
│ │ │ └── ConverterTests.cs
│ │ ├── PowerUnits
│ │ │ └── PowerUnitTests.cs
│ │ └── AngleUnits
│ │ │ ├── ToDegreeTest.cs
│ │ │ └── ToRadianTest.cs
│ ├── ModTest.cs
│ ├── LogTest.cs
│ └── LCMTest.cs
├── ParserTests
│ ├── NotBalancedParenthesisTests.cs
│ ├── UnitTests.cs
│ ├── PowerUnitTests.cs
│ ├── UnassignTests.cs
│ ├── TemperatureUnitTests.cs
│ ├── EqualityOperatorTests.cs
│ ├── BaseParserTests.cs
│ ├── LogTests.cs
│ ├── WhileTests.cs
│ ├── StringTests.cs
│ ├── MassUnitTests.cs
│ ├── TrigonometricTests.cs
│ ├── NumberTests.cs
│ ├── ReverseTrigonometricTests.cs
│ ├── HyperbolicTests.cs
│ ├── VolumeUnitTests.cs
│ ├── ReverseHyperbolicTests.cs
│ ├── RelationalOperatorTests.cs
│ ├── GCDTests.cs
│ ├── ConditionalOperatorTests.cs
│ ├── TimeUnitTests.cs
│ ├── ForTests.cs
│ └── AreaUnitTests.cs
├── AllExpressionsData.cs
├── Results
│ ├── BooleanResultTest.cs
│ ├── NumberResultTest.cs
│ ├── MassResultTest.cs
│ ├── AreaResultTest.cs
│ ├── PowerResultTest.cs
│ ├── TimeResultTest.cs
│ ├── AngleResultTest.cs
│ ├── VolumeResultTest.cs
│ ├── LengthResultTest.cs
│ ├── LambdaResultTest.cs
│ ├── RationalResultTest.cs
│ ├── VectorResultTest.cs
│ ├── TemperatureResultTest.cs
│ ├── StringResultTest.cs
│ └── MatrixResultTest.cs
├── Analyzers
│ ├── TypeAnalyzerTests
│ │ ├── NumericConvertionTests.cs
│ │ └── ProgrammingTests
│ │ │ └── AssignmentOperatorsTests.cs
│ └── SimplifierTests
│ │ └── BaseSimplifierTest.cs
└── BaseTest.cs
├── SECURITY.md
├── .gitignore
├── version.json
├── .vscode
├── settings.json
└── launch.json
├── .gitattributes
├── xFunc.slnx
├── .stylecop.json
├── xFunc.ruleset
├── Directory.Packages.props
└── LICENSE
/CODEOWNERS:
--------------------------------------------------------------------------------
1 | *.* @sys27
2 |
--------------------------------------------------------------------------------
/docs/index.md:
--------------------------------------------------------------------------------
1 | ../README.md
--------------------------------------------------------------------------------
/xFunc.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sys27/xFunc/HEAD/xFunc.png
--------------------------------------------------------------------------------
/CI/codecov.yml:
--------------------------------------------------------------------------------
1 | coverage:
2 | precision: 2
3 | round: down
4 | range: "80...90"
5 |
--------------------------------------------------------------------------------
/docs/images/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sys27/xFunc/HEAD/docs/images/logo.png
--------------------------------------------------------------------------------
/docs/images/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sys27/xFunc/HEAD/docs/images/favicon.ico
--------------------------------------------------------------------------------
/docs/api/.gitignore:
--------------------------------------------------------------------------------
1 | ###############
2 | # temp file #
3 | ###############
4 | *.yml
5 | .manifest
6 |
--------------------------------------------------------------------------------
/docs/toc.yml:
--------------------------------------------------------------------------------
1 | - name: Articles
2 | href: articles/
3 | - name: Api Documentation
4 | href: api/
5 | homepage: api/xFunc.Maths.Processor.yml
6 |
--------------------------------------------------------------------------------
/docs/.gitignore:
--------------------------------------------------------------------------------
1 | ###############
2 | # folder #
3 | ###############
4 | /**/DROP/
5 | /**/TEMP/
6 | /**/packages/
7 | /**/bin/
8 | /**/obj/
9 | _site
10 |
--------------------------------------------------------------------------------
/xFunc.Cli/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "xFunc.Cli": {
4 | "commandName": "Project",
5 | "commandLineArgs": "interactive"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: "nuget"
4 | directory: "/"
5 | schedule:
6 | interval: "daily"
7 | target-branch: "dev"
8 | assignees:
9 | - "sys27"
10 |
--------------------------------------------------------------------------------
/Directory.Build.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | <_LocalTopLevelSourceRoot Include="@(SourceRoot)" Condition="'%(SourceRoot.NestedRoot)' == ''" />
5 |
6 |
7 |
--------------------------------------------------------------------------------
/xFunc.Maths/Analyzers/ISimplifier.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Analyzers;
5 |
6 | ///
7 | /// The interface for mathematical expression simplifier.
8 | ///
9 | public interface ISimplifier : IAnalyzer;
--------------------------------------------------------------------------------
/xFunc.Maths/Analyzers/IDifferentiator.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Analyzers;
5 |
6 | ///
7 | /// The interface for differentiator.
8 | ///
9 | public interface IDifferentiator : IAnalyzer;
--------------------------------------------------------------------------------
/xFunc.Benchmark/Benchmarks/CtorBenchmark.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Benchmark.Benchmarks;
5 |
6 | public class CtorBenchmark
7 | {
8 | [Benchmark]
9 | public IParser ParserCtor()
10 | {
11 | return new Parser();
12 | }
13 | }
--------------------------------------------------------------------------------
/xFunc.Cli/Options/DebugInfoOptions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Cli.Options;
5 |
6 | public abstract class DebugInfoOptions
7 | {
8 | [Option('d', "debug", Default = false, Required = false, HelpText = "Show stack trace.")]
9 | public bool Debug { get; set; }
10 | }
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | labels: "bug"
5 | ---
6 |
7 | **Describe the bug**
8 | A clear and concise description of what the bug is.
9 |
10 | **To Reproduce**
11 | Steps to reproduce the behavior:
12 | 1.
13 | 2.
14 | 3.
15 |
16 | **Expected behavior**
17 | A clear and concise description of what you expected to happen.
18 |
19 | **Actual behavior**
20 |
--------------------------------------------------------------------------------
/xFunc.Maths/Analyzers/Formatters/IFormatter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Analyzers.Formatters;
5 |
6 | ///
7 | /// The common interface for expression formatters.
8 | ///
9 | ///
10 | public interface IFormatter : IAnalyzer;
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/BaseExpressionTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.Expressions;
5 |
6 | public abstract class BaseExpressionTests : BaseTest
7 | {
8 | protected void TestNotSupported(IExpression exp)
9 | => Assert.Throws(() => exp.Execute());
10 | }
--------------------------------------------------------------------------------
/xFunc.Cli/Options/BaseOptions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Cli.Options;
5 |
6 | public abstract class BaseOptions : DebugInfoOptions
7 | {
8 | [Value(0, Required = true, MetaName = "String Expression", HelpText = "The string expression.")]
9 | public string StringExpression { get; set; }
10 | }
--------------------------------------------------------------------------------
/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
3 | ## Supported Versions
4 |
5 | | Version | Supported |
6 | | ------- | ------------------ |
7 | | 4.1.x | :white_check_mark: |
8 | | 4.0.x | :white_check_mark: |
9 | | < 4.0 | :x: |
10 |
11 | ## Reporting a Vulnerability
12 |
13 | All security issues can be reported publicly via the [GitHub Issues](https://github.com/sys27/xFunc/issues) page. You should receive a response within several days.
14 | If for some reason you do not, please ping the author again. :blush:
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | [Bb]in/
2 | [Oo]bj/
3 | [Dd]ebug/
4 | [Rr]elease/
5 | TestResults
6 | packages
7 | .idea/
8 | .vs/
9 | BenchmarkDotNet.Artifacts/
10 |
11 | desktop.ini
12 | .DS_Store
13 |
14 | coverage
15 | coverage.xml
16 | coverage.cobertura.xml
17 |
18 | *.com
19 | *.dll
20 | *.exe
21 | *.pdb
22 | *.obj
23 |
24 | *.zip
25 | *.log
26 |
27 | *.mdf
28 | *.ldf
29 | *.suo
30 | *.vssscc
31 | *.vspscc
32 | *.psess
33 | *.vsp
34 | *.user
35 | *.nupkg
36 | *.nuspec
37 | *.diff
38 | *.design
39 | *.pfx
40 |
41 | [Pp]laylist\
42 | *.playlist
43 |
44 | Thumbs.db
--------------------------------------------------------------------------------
/version.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3 | "version": "4.5.0-preview.{height}",
4 | "assemblyVersion": {
5 | "precision": "revision"
6 | },
7 | "publicReleaseRefSpec": [
8 | "^refs/heads/master$",
9 | "^refs/heads/dev$"
10 | ],
11 | "nugetPackageVersion": {
12 | "semVer": 2
13 | },
14 | "cloudBuild": {
15 | "setVersionVariables": true,
16 | "buildNumber": {
17 | "enabled": true
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "files.exclude": {
3 | "**/.git": true,
4 | "**/.svn": true,
5 | "**/.hg": true,
6 | "**/CVS": true,
7 | "**/.DS_Store": true,
8 | "**/.vs": true,
9 | "**/bin": true,
10 | "**/obj": true,
11 | "**/packages": true,
12 | "**/coverage": true,
13 | "**/.idea": true,
14 | "coverage.xml": true,
15 | "coverage.cobertura.xml": true,
16 | "xFunc": true,
17 | "desktop.ini": true
18 | },
19 | "dotnet.defaultSolution": "xFunc.sln"
20 | }
21 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/Domains/DomainBuilderTests.cs:
--------------------------------------------------------------------------------
1 | using xFunc.Maths.Expressions.Domains;
2 |
3 | namespace xFunc.Tests.Expressions.Domains;
4 |
5 | public class DomainBuilderTests
6 | {
7 | [Test]
8 | public void AddRangeExceeded()
9 | {
10 | var domain = new DomainBuilder(1)
11 | .AddRange(r => r.Start(NumberValue.NegativeInfinity).End(NumberValue.PositiveInfinity));
12 |
13 | Assert.Throws(() =>
14 | domain.AddRange(r => r.Start(NumberValue.NegativeInfinity).End(NumberValue.PositiveInfinity)));
15 | }
16 | }
--------------------------------------------------------------------------------
/docs/articles/toc.yml:
--------------------------------------------------------------------------------
1 | - name: Get Started
2 | href: get-started.md
3 | - name: ChangeLog
4 | href: change-log.md
5 | - name: Breaking Changes
6 | href: breaking-changes.md
7 | - name: Performance comparison
8 | href: performance-comparison.md
9 | - name: Supported Function and Operators
10 | href: supported-functions-and-operations.md
11 | - name: Simplification rules
12 | href: simplification-rules.md
13 | - name: Advanced
14 | items:
15 | - name: How to build project
16 | href: how-to-build.md
17 | - name: How to implement own expression
18 | href: custom-expression.md
--------------------------------------------------------------------------------
/xFunc.Cli/Options/InteractiveOptions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Cli.Options;
5 |
6 | [Verb("interactive", HelpText = "Run interactive mode.")]
7 | public class InteractiveOptions : DebugInfoOptions
8 | {
9 | [Usage(ApplicationAlias = "xfunc")]
10 | public static IEnumerable Examples
11 | => new List
12 | {
13 | new Example("Run iteractive mode", new InteractiveOptions())
14 | };
15 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Analyzers/TypeAnalyzers/ITypeAnalyzer.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Analyzers.TypeAnalyzers;
5 |
6 | ///
7 | /// Type Analyzer checks the expression tree for argument type and result type. If result type is Undefined, then Type Analyzer cannot determine the right type and bypass current expression.
8 | ///
9 | ///
10 | public interface ITypeAnalyzer : IAnalyzer;
--------------------------------------------------------------------------------
/xFunc.slnx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/.stylecop.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
3 | "settings": {
4 | "documentationRules": {
5 | "xmlHeader": false,
6 | "companyName": "Dmytro Kyshchenko",
7 | "copyrightText": "Copyright (c) {companyName}. All rights reserved.\nLicensed under the {licenseName} license. See {licenseFile} file in the project root for full license information.",
8 | "variables": {
9 | "licenseName": "MIT",
10 | "licenseFile": "LICENSE"
11 | }
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/NotBalancedParenthesisTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class NotBalancedParenthesisTests : BaseParserTests
7 | {
8 | [Test]
9 | [TestCase("sin(2(")]
10 | [TestCase("sin)2)")]
11 | [TestCase("sin)2(")]
12 | [TestCase("{2,1")]
13 | [TestCase("}2,1")]
14 | [TestCase("(2")]
15 | [TestCase("func(2")]
16 | public void NotBalancedTest(string function)
17 | => ParseErrorTest(function);
18 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Results/Result.EmptyResult.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using System.Diagnostics.CodeAnalysis;
5 |
6 | namespace xFunc.Maths.Results;
7 |
8 | public abstract partial class Result
9 | {
10 | ///
11 | /// Represents the empty result.
12 | ///
13 | [ExcludeFromCodeCoverage]
14 | public sealed class EmptyResult : Result
15 | {
16 | ///
17 | public override string ToString()
18 | => string.Empty;
19 | }
20 | }
--------------------------------------------------------------------------------
/xFunc.Cli/Options/ParseOptions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Cli.Options;
5 |
6 | [Verb("parse", HelpText = "Parse string expression.")]
7 | public class ParseOptions : BaseOptions
8 | {
9 | [Usage(ApplicationAlias = "xfunc")]
10 | public static IEnumerable Examples
11 | => new List
12 | {
13 | new Example(
14 | "Parse string expression",
15 | new ParseOptions { StringExpression = "1 + 1" })
16 | };
17 | }
--------------------------------------------------------------------------------
/xFunc.Maths/IParser.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths;
5 |
6 | ///
7 | /// The interface for parser.
8 | ///
9 | public interface IParser
10 | {
11 | ///
12 | /// Parses the specified .
13 | ///
14 | /// The string expression.
15 | /// The parsed expression.
16 | ///
17 | IExpression Parse(string expression);
18 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/EmptyValue.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions;
5 |
6 | ///
7 | /// Represents the empty value (needed for functions that don't return any value).
8 | ///
9 | public sealed class EmptyValue
10 | {
11 | private EmptyValue()
12 | {
13 | }
14 |
15 | ///
16 | /// Gets the single instance of .
17 | ///
18 | public static EmptyValue Instance { get; } = new EmptyValue();
19 | }
--------------------------------------------------------------------------------
/xFunc.Cli/Options/SolveOptions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Cli.Options;
5 |
6 | [Verb("solve", HelpText = "Calculate result of expression.")]
7 | public class SolveOptions : BaseOptions
8 | {
9 | [Usage(ApplicationAlias = "xfunc")]
10 | public static IEnumerable Examples
11 | => new List
12 | {
13 | new Example(
14 | "Calculate string expression",
15 | new SolveOptions { StringExpression = "1 + 1" }),
16 | };
17 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/UnitTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class UnitTests : BaseParserTests
7 | {
8 | [Test]
9 | [TestCase("convert(1, 'rad')")]
10 | [TestCase("convert(1, \"rad\")")]
11 | public void ConvertParseTest(string function)
12 | {
13 | var converter = new Converter();
14 | var convert = new xFunc.Maths.Expressions.Units.Convert(converter, Number.One, new StringExpression("rad"));
15 |
16 | ParseTest(function, convert);
17 | }
18 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/PowerUnitTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class PowerUnitTests : BaseParserTests
7 | {
8 | [Test]
9 | public void ParseWatt()
10 | => ParseTest("1 'W'", PowerValue.Watt(1).AsExpression());
11 |
12 | [Test]
13 | public void ParseKilowatt()
14 | => ParseTest("1 'kW'", PowerValue.Kilowatt(1).AsExpression());
15 |
16 | [Test]
17 | public void ParseHorsepower()
18 | => ParseTest("1 'hp'", PowerValue.Horsepower(1).AsExpression());
19 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/UnassignTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class UnassignTests : BaseParserTests
7 | {
8 | [Test]
9 | public void UndefParseTest()
10 | {
11 | var expected = new Unassign(new Variable("f"));
12 |
13 | ParseTest("unassign(f)", expected);
14 | }
15 |
16 | [Test]
17 | [TestCase("unassign x)")]
18 | [TestCase("unassign()")]
19 | [TestCase("unassign(x")]
20 | public void UndefMissingPartsTest(string function)
21 | => ParseErrorTest(function);
22 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/TemperatureUnitTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class TemperatureUnitTests : BaseParserTests
7 | {
8 | [Test]
9 | public void ParseCelsius()
10 | => ParseTest("1 '°C'", TemperatureValue.Celsius(1).AsExpression());
11 |
12 | [Test]
13 | public void ParseFahrenheit()
14 | => ParseTest("1 '°F'", TemperatureValue.Fahrenheit(1).AsExpression());
15 |
16 | [Test]
17 | public void ParseKelvin()
18 | => ParseTest("1 'K'", TemperatureValue.Kelvin(1).AsExpression());
19 | }
--------------------------------------------------------------------------------
/xFunc.Cli/Options/RunFileOptions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Cli.Options;
5 |
6 | [Verb("run", HelpText = "Run all expressions from a file.")]
7 | public class RunFileOptions : DebugInfoOptions
8 | {
9 | [Value(0, Required = true, MetaName = "File", HelpText = "Path to a file.")]
10 | public string File { get; set; }
11 |
12 | [Usage(ApplicationAlias = "xfunc")]
13 | public static IEnumerable Examples
14 | => new List
15 | {
16 | new Example("Run all expressions from a file", new RunFileOptions { File = "./file.xf" })
17 | };
18 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Units/Converters/IConverter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Units.Converters;
5 |
6 | ///
7 | /// The interface for unit converter.
8 | ///
9 | public interface IConverter
10 | {
11 | ///
12 | /// Converts to specified .
13 | ///
14 | /// The value to convert.
15 | /// The unit to convert to.
16 | /// The converter value.
17 | object Convert(object value, string unit);
18 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Parameters/ParameterType.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Parameters;
5 |
6 | ///
7 | /// Contains types of parameter.
8 | ///
9 | public enum ParameterType
10 | {
11 | ///
12 | /// The normal parameter.
13 | ///
14 | Normal,
15 |
16 | ///
17 | /// The read-only parameter. It can be added/removed, but it can't be changed.
18 | ///
19 | ReadOnly,
20 |
21 | ///
22 | /// The constant parameter. It can be added, but can't be changed or removed.
23 | ///
24 | Constant,
25 | }
--------------------------------------------------------------------------------
/xFunc.Benchmark/Benchmarks/GcdBenchmark.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Benchmark.Benchmarks;
5 |
6 | public class GcdBenchmark
7 | {
8 | private IExpression gcd;
9 | private IExpression lcm;
10 |
11 | [GlobalSetup]
12 | public void Setup()
13 | {
14 | var processor = new Processor();
15 |
16 | gcd = processor.Parse("gcd(2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8)");
17 | lcm = processor.Parse("gcd(2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8)");
18 | }
19 |
20 | [Benchmark]
21 | public object GcdExecute() => gcd.Execute();
22 |
23 | [Benchmark]
24 | public object LcmExecute() => lcm.Execute();
25 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/EqualityOperatorTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class EqualityOperatorTests : BaseParserTests
7 | {
8 | [Test]
9 | public void EqualTest()
10 | => ParseTest("x == 0", new Equal(Variable.X, Number.Zero));
11 |
12 | [Test]
13 | public void NotEqualTest()
14 | => ParseTest("x != 0", new NotEqual(Variable.X, Number.Zero));
15 |
16 | [Test]
17 | public void PrecedenceTest()
18 | {
19 | var expected = new And(
20 | new Variable("a"),
21 | new NotEqual(new Variable("b"), new Variable("c"))
22 | );
23 |
24 | ParseTest("a & b != c", expected);
25 | }
26 | }
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | # github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4 | patreon: DmytroKyshchenko
5 | # open_collective: # Replace with a single Open Collective username
6 | # ko_fi: # Replace with a single Ko-fi username
7 | # tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | # community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | # liberapay: # Replace with a single Liberapay username
10 | # issuehunt: # Replace with a single IssueHunt username
11 | # lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
12 | # polar: # Replace with a single Polar username
13 | # buy_me_a_coffee:
14 | # custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
15 |
--------------------------------------------------------------------------------
/xFunc.Benchmark/Benchmarks/DifferentiatorBenchmark.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Benchmark.Benchmarks;
5 |
6 | public class DifferentiatorBenchmark
7 | {
8 | private Differentiator differentiator;
9 |
10 | private IExpression complexExp;
11 |
12 | [GlobalSetup]
13 | public void Setup()
14 | {
15 | differentiator = new Differentiator();
16 |
17 | var processor = new Processor();
18 |
19 | complexExp = processor.Parse("(2 * abs(3 * sin(4 * cos(5 * tan(6 * ctg(x ^ 2))))) - ln(x ^ 2)) + arcsin(arccos(arctan(arcctg(x ^ 10))))");
20 | }
21 |
22 | [Benchmark]
23 | public IExpression ComplexExpression()
24 | => complexExp.Analyze(differentiator, new DifferentiatorContext());
25 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/BaseParserTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public abstract class BaseParserTests
7 | {
8 | protected readonly Parser parser;
9 |
10 | protected BaseParserTests() => parser = new Parser();
11 |
12 | protected void ParseTest(string function, IExpression expected)
13 | {
14 | var exp = parser.Parse(function);
15 |
16 | Assert.That(exp, Is.EqualTo(expected));
17 | }
18 |
19 | protected void ParseErrorTest(string function)
20 | => Assert.Throws(() => parser.Parse(function));
21 |
22 | protected void ParseErrorTest(string function) where T : Exception
23 | => Assert.Throws(() => parser.Parse(function));
24 | }
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/UnaryTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.Expressions;
5 |
6 | public class UnaryTest
7 | {
8 | [Test]
9 | public void EqualsTest1()
10 | {
11 | var sine1 = new Sin(Number.Two);
12 | var sine2 = new Sin(Number.Two);
13 |
14 | Assert.That(sine2, Is.EqualTo(sine1));
15 | }
16 |
17 | [Test]
18 | public void EqualsTest2()
19 | {
20 | var sine = new Sin(Number.Two) as IExpression;
21 | var ln = new Ln(Number.Two) as IExpression;
22 |
23 | Assert.That(ln, Is.Not.EqualTo(sine));
24 | }
25 |
26 | [Test]
27 | public void ArgNullExceptionTest()
28 | {
29 | Assert.Throws(() => new Sin(null));
30 | }
31 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/LogTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class LogTests : BaseParserTests
7 | {
8 | [Test]
9 | public void ParseLog()
10 | => ParseTest("log(9, 3)", new Log(new Number(9), new Number(3)));
11 |
12 | [Test]
13 | public void ParseLogWithOneParam()
14 | => ParseErrorTest("log(9)");
15 |
16 | [Test]
17 | [TestCase("lb(2)")]
18 | [TestCase("log2(2)")]
19 | public void LbTest(string function)
20 | => ParseTest(function, new Lb(Number.Two));
21 |
22 | [Test]
23 | public void LgTest()
24 | => ParseTest("lg(2)", new Lg(Number.Two));
25 |
26 | [Test]
27 | public void LnTest()
28 | => ParseTest("ln(2)", new Ln(Number.Two));
29 | }
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/ToRationalTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.Expressions;
5 |
6 | public class ToRationalTest : BaseExpressionTests
7 | {
8 | [Test]
9 | public void ExecuteTest()
10 | {
11 | var exp = new ToRational(Number.Two);
12 | var expected = new RationalValue(2, 1);
13 | var actual = exp.Execute();
14 |
15 | Assert.That(actual, Is.EqualTo(expected));
16 | }
17 |
18 | [Test]
19 | public void ExecuteExceptionTest()
20 | => TestNotSupported(new ToRational(Bool.True));
21 |
22 | [Test]
23 | public void CloneTest()
24 | {
25 | var exp = new ToRational(Number.Two);
26 | var clone = exp.Clone();
27 |
28 | Assert.That(clone, Is.EqualTo(exp));
29 | }
30 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/WhileTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class WhileTests : BaseParserTests
7 | {
8 | [Test]
9 | public void WhileTest()
10 | {
11 | var expected = new While(
12 | new Assign(Variable.X, new Add(Variable.X, Number.One)),
13 | new Equal(Number.One, Number.One)
14 | );
15 |
16 | ParseTest("while(x := x + 1, 1 == 1)", expected);
17 | }
18 |
19 | [Test]
20 | [TestCase("while x := x + 1, 1 == 1)")]
21 | [TestCase("while(, 1 == 1)")]
22 | [TestCase("while(x := x + 1 1 == 1)")]
23 | [TestCase("while(x := x + 1, )")]
24 | [TestCase("while(x := x + 1, 1 == 1")]
25 | public void WhileMissingPartsTest(string function)
26 | => ParseErrorTest(function);
27 | }
--------------------------------------------------------------------------------
/xFunc.Benchmark/Benchmarks/SimplifierBenchmark.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Benchmark.Benchmarks;
5 |
6 | public class SimplifierBenchmark
7 | {
8 | private Simplifier simplifier;
9 |
10 | private IExpression exp;
11 |
12 | [GlobalSetup]
13 | public void Setup()
14 | {
15 | simplifier = new Simplifier();
16 |
17 | var processor = new Processor();
18 |
19 | exp = processor.Parse("0 + x + x + 0 + 1 + 2 + 3 + x + (2 * x) + (3 * x) + (x * 4) - 0 - x - 0 - 1 - 2 - 3 - (2 * x) - (x * 3) + (x * 0) - (0 * x) + (1 * x) - (x * 1) * (x * x) * (2 * x) * (x * 3) + (x ^ 0) + (x ^ 0) + (e ^ ln(1)) + cos(arccos(0)) + (x * 0) + tan(arctan(0)) + sin(arcsin(x)) - (0 * x)");
20 | }
21 |
22 | [Benchmark]
23 | public IExpression Simplify()
24 | => exp.Analyze(simplifier);
25 | }
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/ComplexNumbers/ToComplexTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using System.Numerics;
5 |
6 | namespace xFunc.Tests.Expressions.ComplexNumbers;
7 |
8 | public class ToComplexTest : BaseExpressionTests
9 | {
10 | [Test]
11 | public void ExecuteTest()
12 | {
13 | var exp = new ToComplex(Number.Two);
14 | var result = (Complex)exp.Execute();
15 | var expected = new Complex(2, 0);
16 |
17 | Assert.That(result, Is.EqualTo(expected));
18 | }
19 |
20 | [Test]
21 | public void ExecuteBoolTest()
22 | => TestNotSupported(new ToComplex(Bool.False));
23 |
24 | [Test]
25 | public void CloneTest()
26 | {
27 | var exp = new ToComplex(Number.Two);
28 | var clone = exp.Clone();
29 |
30 | Assert.That(clone, Is.EqualTo(exp));
31 | }
32 | }
--------------------------------------------------------------------------------
/xFunc.ruleset:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/Directory.Packages.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/ComplexNumbers/ConjugateTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using System.Numerics;
5 |
6 | namespace xFunc.Tests.Expressions.ComplexNumbers;
7 |
8 | public class ConjugateTest : BaseExpressionTests
9 | {
10 | [Test]
11 | public void ExecuteComplexNumberTest()
12 | {
13 | var complex = new Complex(3.1, 2.5);
14 | var exp = new Conjugate(new ComplexNumber(complex));
15 |
16 | Assert.That(exp.Execute(), Is.EqualTo(Complex.Conjugate(complex)));
17 | }
18 |
19 | [Test]
20 | public void ExecuteExceptionTest()
21 | => TestNotSupported(new Conjugate(Number.Two));
22 |
23 | [Test]
24 | public void CloneTest()
25 | {
26 | var exp = new Conjugate(new ComplexNumber(new Complex(2, 2)));
27 | var clone = exp.Clone();
28 |
29 | Assert.That(clone, Is.EqualTo(exp));
30 | }
31 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/StringTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class StringTests : BaseParserTests
7 | {
8 | [Test]
9 | [TestCase("\"\"", "")]
10 | [TestCase("''", "")]
11 | [TestCase("\"hello\"", "hello")]
12 | [TestCase("'hello'", "hello")]
13 | [TestCase("\"hello, 'inline'\"", "hello, 'inline'")]
14 | [TestCase("'hello, \"inline\"'", "hello, \"inline\"")]
15 | [TestCase("\"sin(x)\"", "sin(x)")]
16 | public void Quotes(string exp, string result)
17 | => ParseTest(exp, new StringExpression(result));
18 |
19 | [Test]
20 | [TestCase("\"hello")]
21 | [TestCase("'hello")]
22 | [TestCase("hello\"")]
23 | [TestCase("hello'")]
24 | [TestCase("\"hello, 'inside'")]
25 | public void MissingQuote(string exp)
26 | => ParseErrorTest(exp);
27 | }
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/ComplexNumbers/ReTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using System.Numerics;
5 |
6 | namespace xFunc.Tests.Expressions.ComplexNumbers;
7 |
8 | public class ReTest : BaseExpressionTests
9 | {
10 | [Test]
11 | public void ExecuteComplexNumberTest()
12 | {
13 | var complex = new Complex(3.1, 2.5);
14 | var exp = new Re(new ComplexNumber(complex));
15 | var expected = new NumberValue(complex.Real);
16 |
17 | Assert.That(exp.Execute(), Is.EqualTo(expected));
18 | }
19 |
20 | [Test]
21 | public void ExecuteExceptionTest()
22 | => TestNotSupported(new Re(Number.Two));
23 |
24 | [Test]
25 | public void CloneTest()
26 | {
27 | var exp = new Re(new ComplexNumber(new Complex(2, 2)));
28 | var clone = exp.Clone();
29 |
30 | Assert.That(clone, Is.EqualTo(exp));
31 | }
32 | }
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/ComplexNumbers/ReciprocalTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using System.Numerics;
5 |
6 | namespace xFunc.Tests.Expressions.ComplexNumbers;
7 |
8 | public class ReciprocalTest : BaseExpressionTests
9 | {
10 | [Test]
11 | public void ExecuteComplexNumberTest()
12 | {
13 | var complex = new Complex(3.1, 2.5);
14 | var exp = new Reciprocal(new ComplexNumber(complex));
15 |
16 | Assert.That(exp.Execute(), Is.EqualTo(Complex.Reciprocal(complex)));
17 | }
18 |
19 | [Test]
20 | public void ExecuteExceptionTest()
21 | => TestNotSupported(new Reciprocal(Number.Two));
22 |
23 | [Test]
24 | public void CloneTest()
25 | {
26 | var exp = new Reciprocal(new ComplexNumber(new Complex(2, 2)));
27 | var clone = exp.Clone();
28 |
29 | Assert.That(clone, Is.EqualTo(exp));
30 | }
31 | }
--------------------------------------------------------------------------------
/xFunc.Benchmark/Program.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using BenchmarkDotNet.Configs;
5 | using BenchmarkDotNet.Diagnosers;
6 | using BenchmarkDotNet.Jobs;
7 | using BenchmarkDotNet.Running;
8 | using BenchmarkDotNet.Toolchains.CsProj;
9 |
10 | namespace xFunc.Benchmark;
11 |
12 | public class Program
13 | {
14 | public static void Main(string[] args)
15 | {
16 | if (args is null || args.Length == 0)
17 | args = ["--filter", "*"];
18 |
19 | BenchmarkSwitcher
20 | .FromAssembly(typeof(Program).Assembly)
21 | .Run(args,
22 | ManualConfig.Create(DefaultConfig.Instance)
23 | .AddJob(Job.MediumRun
24 | .WithToolchain(CsProjCoreToolchain.NetCoreApp10_0))
25 | .AddDiagnoser(MemoryDiagnoser.Default)
26 | .StopOnFirstError());
27 | }
28 | }
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/ComplexNumbers/ImTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using System.Numerics;
5 |
6 | namespace xFunc.Tests.Expressions.ComplexNumbers;
7 |
8 | public class ImTest : BaseExpressionTests
9 | {
10 | [Test]
11 | public void ExecuteComplexNumberTest()
12 | {
13 | var complex = new Complex(3.1, 2.5);
14 | var exp = new Im(new ComplexNumber(complex));
15 | var expected = new NumberValue(complex.Imaginary);
16 |
17 | Assert.That(exp.Execute(), Is.EqualTo(expected));
18 | }
19 |
20 | [Test]
21 | public void ExecuteExceptionTest()
22 | => TestNotSupported(new Im(Number.Two));
23 |
24 | [Test]
25 | public void CloneTest()
26 | {
27 | var exp = new Im(new ComplexNumber(new Complex(2, 2)));
28 | var clone = exp.Clone();
29 |
30 | Assert.That(clone, Is.EqualTo(exp));
31 | }
32 | }
--------------------------------------------------------------------------------
/xFunc.Tests/Expressions/ComplexNumbers/PhaseTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using System.Numerics;
5 |
6 | namespace xFunc.Tests.Expressions.ComplexNumbers;
7 |
8 | public class PhaseTest : BaseExpressionTests
9 | {
10 | [Test]
11 | public void ExecuteTest()
12 | {
13 | var complex = new Complex(3.1, 2.5);
14 | var exp = new Phase(new ComplexNumber(complex));
15 | var expected = AngleValue.Radian(complex.Phase);
16 |
17 | Assert.That(exp.Execute(), Is.EqualTo(expected));
18 | }
19 |
20 | [Test]
21 | public void ExecuteExceptionTest()
22 | => TestNotSupported(new Phase(Number.Two));
23 |
24 | [Test]
25 | public void CloneTest()
26 | {
27 | var exp = new Phase(new ComplexNumber(new Complex(2, 2)));
28 | var clone = exp.Clone();
29 |
30 | Assert.That(clone, Is.EqualTo(exp));
31 | }
32 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Units/AreaUnits/Area.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Units.AreaUnits;
5 |
6 | ///
7 | /// Represents a area number.
8 | ///
9 | public class Area : Unit
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// A area value.
15 | public Area(AreaValue value)
16 | : base(value)
17 | {
18 | }
19 |
20 | ///
21 | protected override TResult AnalyzeInternal(IAnalyzer analyzer)
22 | => analyzer.Analyze(this);
23 |
24 | ///
25 | protected override TResult AnalyzeInternal(
26 | IAnalyzer analyzer,
27 | TContext context)
28 | => analyzer.Analyze(this, context);
29 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Units/MassUnits/Mass.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Units.MassUnits;
5 |
6 | ///
7 | /// Represents a mass number.
8 | ///
9 | public class Mass : Unit
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// A mass value.
15 | public Mass(MassValue value)
16 | : base(value)
17 | {
18 | }
19 |
20 | ///
21 | protected override TResult AnalyzeInternal(IAnalyzer analyzer)
22 | => analyzer.Analyze(this);
23 |
24 | ///
25 | protected override TResult AnalyzeInternal(
26 | IAnalyzer analyzer,
27 | TContext context)
28 | => analyzer.Analyze(this, context);
29 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Units/TimeUnits/Time.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Units.TimeUnits;
5 |
6 | ///
7 | /// Represents a time number.
8 | ///
9 | public class Time : Unit
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// A time value.
15 | public Time(TimeValue value)
16 | : base(value)
17 | {
18 | }
19 |
20 | ///
21 | protected override TResult AnalyzeInternal(IAnalyzer analyzer)
22 | => analyzer.Analyze(this);
23 |
24 | ///
25 | protected override TResult AnalyzeInternal(
26 | IAnalyzer analyzer,
27 | TContext context)
28 | => analyzer.Analyze(this, context);
29 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Units/AngleUnits/Angle.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Units.AngleUnits;
5 |
6 | ///
7 | /// Represents an angle number.
8 | ///
9 | public class Angle : Unit
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// An angle.
15 | public Angle(AngleValue value)
16 | : base(value)
17 | {
18 | }
19 |
20 | ///
21 | protected override TResult AnalyzeInternal(IAnalyzer analyzer)
22 | => analyzer.Analyze(this);
23 |
24 | ///
25 | protected override TResult AnalyzeInternal(
26 | IAnalyzer analyzer,
27 | TContext context)
28 | => analyzer.Analyze(this, context);
29 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Tokenization/Lexer.StringToken.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Tokenization;
5 |
6 | ///
7 | /// The lexer for mathematical expressions.
8 | ///
9 | internal ref partial struct Lexer
10 | {
11 | private bool CreateStringToken(char quote)
12 | {
13 | if (function[0] != quote)
14 | return false;
15 |
16 | var endIndex = function[1..].IndexOf(quote) + 1;
17 | if (endIndex == 0)
18 | throw new TokenizeException(Resource.StringTokenizeException);
19 |
20 | var stringValue = function[1..endIndex];
21 |
22 | Current = Token.String(stringValue.ToString());
23 |
24 | function = function[(endIndex + 1)..];
25 |
26 | return true;
27 | }
28 |
29 | private bool CreateStringToken()
30 | => CreateStringToken('"') ||
31 | CreateStringToken('\'');
32 | }
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Units/PowerUnits/Power.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Units.PowerUnits;
5 |
6 | ///
7 | /// Represents a power number.
8 | ///
9 | public class Power : Unit
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// A power number.
15 | public Power(PowerValue value)
16 | : base(value)
17 | {
18 | }
19 |
20 | ///
21 | protected override TResult AnalyzeInternal(IAnalyzer analyzer)
22 | => analyzer.Analyze(this);
23 |
24 | ///
25 | protected override TResult AnalyzeInternal(
26 | IAnalyzer analyzer,
27 | TContext context)
28 | => analyzer.Analyze(this, context);
29 | }
--------------------------------------------------------------------------------
/.github/workflows/codeql.yml:
--------------------------------------------------------------------------------
1 | name: "CodeQL"
2 |
3 | on:
4 | push:
5 | branches: [ "dev", master ]
6 | pull_request:
7 | branches: [ "dev", master ]
8 | schedule:
9 | - cron: '44 18 * * 3'
10 |
11 | jobs:
12 | analyze:
13 | name: Analyze
14 | runs-on: ubuntu-latest
15 | permissions:
16 | actions: read
17 | contents: read
18 | security-events: write
19 |
20 | strategy:
21 | fail-fast: false
22 | matrix:
23 | include:
24 | - language: csharp
25 | build-mode: none
26 |
27 | steps:
28 | - name: Checkout repository
29 | uses: actions/checkout@v4
30 | with:
31 | fetch-depth: 0
32 |
33 | - name: Initialize CodeQL
34 | uses: github/codeql-action/init@v3
35 | with:
36 | languages: ${{ matrix.language }}
37 | build-mode: ${{ matrix.build-mode }}
38 |
39 | - name: Perform CodeQL Analysis
40 | uses: github/codeql-action/analyze@v3
41 | with:
42 | category: "/language:${{matrix.language}}"
43 |
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Units/VolumeUnits/Volume.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Units.VolumeUnits;
5 |
6 | ///
7 | /// Represents a area number.
8 | ///
9 | public class Volume : Unit
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// A area value.
15 | public Volume(VolumeValue value)
16 | : base(value)
17 | {
18 | }
19 |
20 | ///
21 | protected override TResult AnalyzeInternal(IAnalyzer analyzer)
22 | => analyzer.Analyze(this);
23 |
24 | ///
25 | protected override TResult AnalyzeInternal(
26 | IAnalyzer analyzer,
27 | TContext context)
28 | => analyzer.Analyze(this, context);
29 | }
--------------------------------------------------------------------------------
/xFunc.Benchmark/xFunc.Benchmark.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net10.0
6 | latest
7 | false
8 | enable
9 | true
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/xFunc.Maths/Expressions/Units/LengthUnits/Length.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Maths.Expressions.Units.LengthUnits;
5 |
6 | ///
7 | /// Represents a length number.
8 | ///
9 | public class Length : Unit
10 | {
11 | ///
12 | /// Initializes a new instance of the class.
13 | ///
14 | /// A length value.
15 | public Length(LengthValue value)
16 | : base(value)
17 | {
18 | }
19 |
20 | ///
21 | protected override TResult AnalyzeInternal(IAnalyzer analyzer)
22 | => analyzer.Analyze(this);
23 |
24 | ///
25 | protected override TResult AnalyzeInternal(
26 | IAnalyzer analyzer,
27 | TContext context)
28 | => analyzer.Analyze(this, context);
29 | }
--------------------------------------------------------------------------------
/xFunc.Tests/ParserTests/MassUnitTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | namespace xFunc.Tests.ParserTests;
5 |
6 | public class MassUnitTests : BaseParserTests
7 | {
8 | [Test]
9 | public void ParseKilogram()
10 | => ParseTest("1 'kg'", MassValue.Kilogram(1).AsExpression());
11 |
12 | [Test]
13 | public void ParseGram()
14 | => ParseTest("1 'g'", MassValue.Gram(1).AsExpression());
15 |
16 | [Test]
17 | public void ParseMilligram()
18 | => ParseTest("1 'mg'", MassValue.Milligram(1).AsExpression());
19 |
20 | [Test]
21 | public void ParseTonne()
22 | => ParseTest("1 't'", MassValue.Tonne(1).AsExpression());
23 |
24 | [Test]
25 | public void ParseOunce()
26 | => ParseTest("1 'oz'", MassValue.Ounce(1).AsExpression());
27 |
28 | [Test]
29 | public void ParsePound()
30 | => ParseTest("1 'lb'", MassValue.Pound(1).AsExpression());
31 | }
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.2.0",
3 | "configurations": [
4 | {
5 | "name": "xFunc.Benchmark",
6 | "type": "coreclr",
7 | "request": "launch",
8 | "preLaunchTask": "Build",
9 | "program": "${workspaceFolder}/xFunc.Benchmark/bin/Debug/net6.0/xFunc.Benchmark.dll",
10 | "args": [],
11 | "cwd": "${workspaceFolder}/xFunc.Benchmark",
12 | "console": "internalConsole",
13 | "stopAtEntry": false
14 | },
15 | {
16 | "name": "xFunc.DotnetTool",
17 | "type": "coreclr",
18 | "request": "launch",
19 | "preLaunchTask": "Build",
20 | "program": "${workspaceFolder}/xFunc.DotnetTool/bin/Debug/net6.0/xFunc.DotnetTool.dll",
21 | "args": [
22 | "interactive"
23 | ],
24 | "cwd": "${workspaceFolder}/xFunc.DotnetTool",
25 | "console": "integratedTerminal",
26 | "stopAtEntry": false
27 | }
28 | ]
29 | }
--------------------------------------------------------------------------------
/xFunc.Tests/AllExpressionsData.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Dmytro Kyshchenko. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | using System.Collections;
5 | using System.Reflection;
6 |
7 | namespace xFunc.Tests;
8 |
9 | public class AllExpressionsData : IEnumerable