├── .editorconfig ├── .gitattributes ├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ └── bug_report.md ├── dependabot.yml └── workflows │ ├── codeql.yml │ └── github-pages.yml ├── .gitignore ├── .stylecop.json ├── .vscode ├── launch.json ├── settings.json └── tasks.json ├── CI ├── azure-pipelines.yml └── codecov.yml ├── CODEOWNERS ├── Directory.Build.targets ├── Directory.Packages.props ├── LICENSE ├── README.md ├── SECURITY.md ├── docs ├── .gitignore ├── api │ └── .gitignore ├── articles │ ├── breaking-changes.md │ ├── change-log.md │ ├── custom-expression.md │ ├── get-started.md │ ├── how-to-build.md │ ├── performance-comparison.md │ ├── simplification-rules.md │ ├── supported-functions-and-operations.md │ └── toc.yml ├── docfx.json ├── images │ ├── favicon.ico │ └── logo.png ├── index.md └── toc.yml ├── version.json ├── xFunc Grammar.txt ├── xFunc.Benchmark ├── Benchmarks │ ├── ConvertBenchmark.cs │ ├── CtorBenchmark.cs │ ├── DifferentiatorBenchmark.cs │ ├── GcdBenchmark.cs │ ├── MatrixBenchmark.cs │ ├── ProcessorBenchmark.cs │ ├── SimplifierBenchmark.cs │ ├── StatisticalBenchmark.cs │ ├── TransposeBenchmark.cs │ ├── TypeAnalyzerBenchmark.cs │ └── VectorBenchmark.cs ├── Program.cs └── xFunc.Benchmark.csproj ├── xFunc.Cli ├── Options │ ├── BaseOptions.cs │ ├── DebugInfoOptions.cs │ ├── InteractiveOptions.cs │ ├── ParseOptions.cs │ ├── RunFileOptions.cs │ └── SolveOptions.cs ├── Program.cs ├── Properties │ └── launchSettings.json └── xFunc.Cli.csproj ├── xFunc.Maths ├── Analyzers │ ├── Analyzer{TResult,TContext}.cs │ ├── Analyzer{TResult}.cs │ ├── Differentiator.cs │ ├── DifferentiatorContext.cs │ ├── Formatters │ │ ├── CommonFormatter.cs │ │ └── IFormatter.cs │ ├── IAnalyzer{TResult,TContext}.cs │ ├── IAnalyzer{TResult}.cs │ ├── IDifferentiator.cs │ ├── ISimplifier.cs │ ├── Simplifier.cs │ └── TypeAnalyzers │ │ ├── BinaryParameterTypeMismatchException.cs │ │ ├── DifferentParameterTypeMismatchException.cs │ │ ├── ITypeAnalyzer.cs │ │ ├── ParameterTypeMismatchException.cs │ │ ├── ResultTypes.cs │ │ ├── ResultTypesExtensions.cs │ │ └── TypeAnalyzer.cs ├── Builder.cs ├── ComplexExtensions.cs ├── Expressions │ ├── Abs.cs │ ├── Add.cs │ ├── Assign.cs │ ├── BinaryExpression.cs │ ├── CallExpression.cs │ ├── Ceil.cs │ ├── ComplexNumbers │ │ ├── ComplexNumber.cs │ │ ├── Conjugate.cs │ │ ├── Im.cs │ │ ├── Phase.cs │ │ ├── Re.cs │ │ ├── Reciprocal.cs │ │ └── ToComplex.cs │ ├── Curry.cs │ ├── Del.cs │ ├── DelegateExpression.cs │ ├── Derivative.cs │ ├── DifferentParametersExpression.cs │ ├── Div.cs │ ├── Domains │ │ ├── Domain.cs │ │ ├── DomainBuilder.cs │ │ ├── DomainRange.cs │ │ └── DomainRangeBuilder.cs │ ├── EmptyValue.cs │ ├── ExecutionException.cs │ ├── Exp.cs │ ├── Fact.cs │ ├── Floor.cs │ ├── Frac.cs │ ├── GCD.cs │ ├── Hyperbolic │ │ ├── Arcosh.cs │ │ ├── Arcoth.cs │ │ ├── Arcsch.cs │ │ ├── Arsech.cs │ │ ├── Arsinh.cs │ │ ├── Artanh.cs │ │ ├── Cosh.cs │ │ ├── Coth.cs │ │ ├── Csch.cs │ │ ├── HyperbolicExpression.cs │ │ ├── InverseHyperbolicExpression.cs │ │ ├── Sech.cs │ │ ├── Sinh.cs │ │ └── Tanh.cs │ ├── IExpression.cs │ ├── LCM.cs │ ├── Lambda.cs │ ├── LambdaExpression.cs │ ├── LambdaExtensions.cs │ ├── Lb.cs │ ├── Lg.cs │ ├── Ln.cs │ ├── Log.cs │ ├── LogicalAndBitwise │ │ ├── And.cs │ │ ├── Bool.cs │ │ ├── Equality.cs │ │ ├── Implication.cs │ │ ├── NAnd.cs │ │ ├── NOr.cs │ │ ├── Not.cs │ │ ├── Or.cs │ │ └── XOr.cs │ ├── Matrices │ │ ├── CrossProduct.cs │ │ ├── Determinant.cs │ │ ├── DotProduct.cs │ │ ├── InvalidMatrixException.cs │ │ ├── Inverse.cs │ │ ├── Matrix.cs │ │ ├── MatrixValue.cs │ │ ├── Transpose.cs │ │ ├── Vector.cs │ │ └── VectorValue.cs │ ├── Mod.cs │ ├── Mul.cs │ ├── Number.cs │ ├── NumberValue.cs │ ├── Parameters │ │ ├── ExpressionParameters.ScopedExpressionParameters.cs │ │ ├── ExpressionParameters.cs │ │ ├── Parameter.cs │ │ ├── ParameterIsReadOnlyException.cs │ │ ├── ParameterType.cs │ │ └── ParameterValue.cs │ ├── Pow.cs │ ├── Programming │ │ ├── AddAssign.cs │ │ ├── ConditionalAnd.cs │ │ ├── ConditionalOr.cs │ │ ├── Dec.cs │ │ ├── DivAssign.cs │ │ ├── Equal.cs │ │ ├── For.cs │ │ ├── GreaterOrEqual.cs │ │ ├── GreaterThan.cs │ │ ├── If.cs │ │ ├── Inc.cs │ │ ├── LeftShift.cs │ │ ├── LeftShiftAssign.cs │ │ ├── LessOrEqual.cs │ │ ├── LessThan.cs │ │ ├── MulAssign.cs │ │ ├── NotEqual.cs │ │ ├── RightShift.cs │ │ ├── RightShiftAssign.cs │ │ ├── SubAssign.cs │ │ ├── VariableBinaryExpression.cs │ │ ├── VariableUnaryExpression.cs │ │ └── While.cs │ ├── Rational.cs │ ├── RationalValue.cs │ ├── Root.cs │ ├── Round.cs │ ├── Sign.cs │ ├── Simplify.cs │ ├── Sqrt.cs │ ├── Statistical │ │ ├── Avg.cs │ │ ├── Count.cs │ │ ├── Max.cs │ │ ├── Min.cs │ │ ├── Product.cs │ │ ├── StatisticalExpression.cs │ │ ├── Stdev.cs │ │ ├── Stdevp.cs │ │ ├── Sum.cs │ │ ├── Var.cs │ │ └── Varp.cs │ ├── StringExpression.cs │ ├── Sub.cs │ ├── ToBin.cs │ ├── ToHex.cs │ ├── ToNumber.cs │ ├── ToOct.cs │ ├── ToRational.cs │ ├── Trigonometric │ │ ├── Arccos.cs │ │ ├── Arccot.cs │ │ ├── Arccsc.cs │ │ ├── Arcsec.cs │ │ ├── Arcsin.cs │ │ ├── Arctan.cs │ │ ├── Cos.cs │ │ ├── Cot.cs │ │ ├── Csc.cs │ │ ├── InverseTrigonometricExpression.cs │ │ ├── Sec.cs │ │ ├── Sin.cs │ │ ├── Tan.cs │ │ └── TrigonometricExpression.cs │ ├── Trunc.cs │ ├── UnaryExpression.cs │ ├── UnaryMinus.cs │ ├── Unassign.cs │ ├── Units │ │ ├── AngleUnits │ │ │ ├── Angle.cs │ │ │ ├── AngleUnit.cs │ │ │ ├── AngleValue.cs │ │ │ ├── ToDegree.cs │ │ │ ├── ToGradian.cs │ │ │ └── ToRadian.cs │ │ ├── AreaUnits │ │ │ ├── Area.cs │ │ │ ├── AreaUnit.cs │ │ │ └── AreaValue.cs │ │ ├── Convert.cs │ │ ├── Converters │ │ │ ├── AngleConverter.cs │ │ │ ├── AreaConverter.cs │ │ │ ├── Converter.cs │ │ │ ├── IConverter.cs │ │ │ ├── IConverter{TValue}.cs │ │ │ ├── LengthConverter.cs │ │ │ ├── MassConverter.cs │ │ │ ├── PowerConverter.cs │ │ │ ├── TemperatureConverter.cs │ │ │ ├── TimeConverter.cs │ │ │ └── VolumeConverter.cs │ │ ├── LengthUnits │ │ │ ├── Length.cs │ │ │ ├── LengthUnit.cs │ │ │ └── LengthValue.cs │ │ ├── MassUnits │ │ │ ├── Mass.cs │ │ │ ├── MassUnit.cs │ │ │ └── MassValue.cs │ │ ├── PowerUnits │ │ │ ├── Power.cs │ │ │ ├── PowerUnit.cs │ │ │ └── PowerValue.cs │ │ ├── TemperatureUnits │ │ │ ├── Temperature.cs │ │ │ ├── TemperatureUnit.cs │ │ │ └── TemperatureValue.cs │ │ ├── TimeUnits │ │ │ ├── Time.cs │ │ │ ├── TimeUnit.cs │ │ │ └── TimeValue.cs │ │ ├── Unit.cs │ │ ├── UnitIsNotSupportedException.cs │ │ ├── ValueIsNotSupportedException.cs │ │ └── VolumeUnits │ │ │ ├── Volume.cs │ │ │ ├── VolumeUnit.cs │ │ │ └── VolumeValue.cs │ └── Variable.cs ├── IParser.cs ├── InvalidResultException.cs ├── MathExtensions.cs ├── ParseException.cs ├── Parser.ExpressionFactory.cs ├── Parser.TokenReader.cs ├── Parser.cs ├── Processor.cs ├── Resources │ ├── Resource.Designer.cs │ └── Resource.resx ├── Results │ ├── Result.AngleResult.cs │ ├── Result.AreaResult.cs │ ├── Result.BooleanResult.cs │ ├── Result.ComplexNumberResult.cs │ ├── Result.EmptyResult.cs │ ├── Result.LambdaResult.cs │ ├── Result.LengthResult.cs │ ├── Result.MassResult.cs │ ├── Result.MatrixResult.cs │ ├── Result.NumberResult.cs │ ├── Result.PowerResult.cs │ ├── Result.RationalResult.cs │ ├── Result.StringResult.cs │ ├── Result.TemperatureResult.cs │ ├── Result.TimeResult.cs │ ├── Result.VectorResult.cs │ ├── Result.VolumeResult.cs │ └── Result.cs ├── Tokenization │ ├── Lexer.IdToken.cs │ ├── Lexer.NumberToken.cs │ ├── Lexer.OperatorToken.cs │ ├── Lexer.StringToken.cs │ ├── Lexer.SymbolToken.cs │ ├── Lexer.cs │ ├── ParseNumbers.cs │ ├── Token.cs │ └── TokenKind.cs ├── TokenizeException.cs └── xFunc.Maths.csproj ├── xFunc.Tests ├── AllExpressionsData.cs ├── Analyzers │ ├── DifferentiatorTests │ │ ├── DifferentiatorTest.cs │ │ └── NullArgumentTest.cs │ ├── Formatters │ │ └── CommonFormatterTest.cs │ ├── SimplifierTests │ │ ├── AddSimplifierTest.cs │ │ ├── AngleSimplifierTest.cs │ │ ├── BaseSimplifierTest.cs │ │ ├── DivSimplifierTest.cs │ │ ├── LogSimplifierTest.cs │ │ ├── MulSimplifierTest.cs │ │ ├── PowerSimplifierTest.cs │ │ ├── SimplifierTest.cs │ │ ├── SubSimplifierTest.cs │ │ ├── ToNumberSimplifierTest.cs │ │ └── TrigonometricSimplifierTest.cs │ └── TypeAnalyzerTests │ │ ├── AbsTests.cs │ │ ├── AddTests.cs │ │ ├── CeilTests.cs │ │ ├── ComplexNumberTests.cs │ │ ├── DivTests.cs │ │ ├── FloorTests.cs │ │ ├── FracTests.cs │ │ ├── HyperbolicTests.cs │ │ ├── LogTests.cs │ │ ├── LogicalBitwiseTests.cs │ │ ├── LogicalTests.cs │ │ ├── MatrixTests.cs │ │ ├── MulTests.cs │ │ ├── NumericConvertionTests.cs │ │ ├── PowTests.cs │ │ ├── ProgrammingTests │ │ ├── AssignmentOperatorsTests.cs │ │ ├── ConditionalOperatorsTests.cs │ │ ├── EqualityOperatorsTests.cs │ │ ├── ProgrammingTest.cs │ │ ├── RelationalOperatorsTests.cs │ │ └── ShiftOperatorsTests.cs │ │ ├── RootTests.cs │ │ ├── RoundTests.cs │ │ ├── SignTests.cs │ │ ├── StandardTests.cs │ │ ├── StatisticalTests.cs │ │ ├── SubTests.cs │ │ ├── ToNumberTests.cs │ │ ├── TrigonometricTests.cs │ │ ├── TruncTests.cs │ │ ├── TypeAnalyzerBaseTests.cs │ │ └── UnaryMinusTests.cs ├── BaseTest.cs ├── BuilderTest.cs ├── Expressions │ ├── AbsTest.cs │ ├── AddTest.cs │ ├── AssignTest.cs │ ├── BaseExpressionTests.cs │ ├── BinaryTest.cs │ ├── CallExpressionTest.cs │ ├── CeilTest.cs │ ├── ComplexNumbers │ │ ├── ComplexNumberTest.cs │ │ ├── ConjugateTest.cs │ │ ├── ImTest.cs │ │ ├── PhaseTest.cs │ │ ├── ReTest.cs │ │ ├── ReciprocalTest.cs │ │ └── ToComplexTest.cs │ ├── CurryTest.cs │ ├── DelTest.cs │ ├── DelegateExpressionTest.cs │ ├── DerivativeTest.cs │ ├── DivTest.cs │ ├── Domains │ │ ├── DomainBuilderTests.cs │ │ ├── DomainRangeTests.cs │ │ └── DomainTests.cs │ ├── ExpTest.cs │ ├── FactTest.cs │ ├── FloorTest.cs │ ├── FracTest.cs │ ├── GCDTest.cs │ ├── Hyperbolic │ │ ├── HyperbolicArcosecantTest.cs │ │ ├── HyperbolicArcosineTest.cs │ │ ├── HyperbolicArcotangentTest.cs │ │ ├── HyperbolicArsecantTest.cs │ │ ├── HyperbolicArsineTest.cs │ │ ├── HyperbolicArtangentTest.cs │ │ ├── HyperbolicCosecantTest.cs │ │ ├── HyperbolicCosineTest.cs │ │ ├── HyperbolicCotangentTest.cs │ │ ├── HyperbolicSecantTest.cs │ │ ├── HyperbolicSineTest.cs │ │ └── HyperbolicTangentTest.cs │ ├── LCMTest.cs │ ├── LambdaExpressionTest.cs │ ├── LambdaTests.cs │ ├── LbTest.cs │ ├── LgTest.cs │ ├── LnTest.cs │ ├── LogTest.cs │ ├── LogicalAndBitwise │ │ ├── AndTest.cs │ │ ├── BoolTest.cs │ │ ├── EqualityTest.cs │ │ ├── ImplicationTest.cs │ │ ├── NAndTest.cs │ │ ├── NOrTest.cs │ │ ├── NotTest.cs │ │ ├── OrTest.cs │ │ └── XOrTest.cs │ ├── Matrices │ │ ├── CrossProductTests.cs │ │ ├── DeterminantTest.cs │ │ ├── DotProductTests.cs │ │ ├── InverseTest.cs │ │ ├── MatrixTest.cs │ │ ├── MatrixValueTests.cs │ │ ├── TransposeTest.cs │ │ ├── VectorTest.cs │ │ └── VectorValueTests.cs │ ├── ModTest.cs │ ├── MulTest.cs │ ├── NumberTest.cs │ ├── NumberValueTest.cs │ ├── Parameters │ │ ├── ExpressionParameterTest.cs │ │ ├── ParameterTest.cs │ │ └── ParameterValueTest.cs │ ├── PowTest.cs │ ├── Programming │ │ ├── AddAssignTest.cs │ │ ├── ConditionalAndTest.cs │ │ ├── ConditionalOrTest.cs │ │ ├── DecTest.cs │ │ ├── DivAssignTest.cs │ │ ├── EqualTest.cs │ │ ├── ForTest.cs │ │ ├── GreaterOrEqualTest.cs │ │ ├── GreaterTest.cs │ │ ├── IfTest.cs │ │ ├── IncTest.cs │ │ ├── LeftShiftAssignTest.cs │ │ ├── LeftShiftTest.cs │ │ ├── LessOrEqualTest.cs │ │ ├── LessThanTest.cs │ │ ├── MulAssignTest.cs │ │ ├── NotEqualTest.cs │ │ ├── RightShiftAssignTest.cs │ │ ├── RightShiftTest.cs │ │ ├── SubAssignTest.cs │ │ └── WhileTest.cs │ ├── RationalTest.cs │ ├── RationalValueTests.cs │ ├── RootTest.cs │ ├── RoundTest.cs │ ├── SignTest.cs │ ├── SimplifyTest.cs │ ├── SqrtTest.cs │ ├── Statistical │ │ ├── AvgTest.cs │ │ ├── CountTest.cs │ │ ├── MaxTest.cs │ │ ├── MinTest.cs │ │ ├── ProductTest.cs │ │ ├── StatisticalTests.cs │ │ ├── StdevTest.cs │ │ ├── StdevpTest.cs │ │ ├── SumTest.cs │ │ ├── VarTest.cs │ │ └── VarpTest.cs │ ├── StringExpressionTests.cs │ ├── SubTest.cs │ ├── ToBinTest.cs │ ├── ToHexTest.cs │ ├── ToNumberTest.cs │ ├── ToOctTest.cs │ ├── ToRationalTest.cs │ ├── Trigonometric │ │ ├── ArccosTest.cs │ │ ├── ArccotTest.cs │ │ ├── ArccscTest.cs │ │ ├── ArcsecTest.cs │ │ ├── ArcsinTest.cs │ │ ├── ArctanTest.cs │ │ ├── CosecantTest.cs │ │ ├── CosineTest.cs │ │ ├── CotangentTest.cs │ │ ├── SecantTest.cs │ │ ├── SineTest.cs │ │ └── TangentTest.cs │ ├── TruncTest.cs │ ├── UnaryMinusTest.cs │ ├── UnaryTest.cs │ ├── UnassignTest.cs │ ├── Units │ │ ├── AngleUnits │ │ │ ├── AngleTest.cs │ │ │ ├── AngleUnitTest.cs │ │ │ ├── AngleValueTest.cs │ │ │ ├── ToDegreeTest.cs │ │ │ ├── ToGradianTest.cs │ │ │ └── ToRadianTest.cs │ │ ├── AreaUnits │ │ │ ├── AreaTest.cs │ │ │ ├── AreaUnitTest.cs │ │ │ └── AreaValueTest.cs │ │ ├── ConvertTests.cs │ │ ├── Converters │ │ │ ├── AngleConverterTests.cs │ │ │ ├── AreaConverterTests.cs │ │ │ ├── ConverterTests.cs │ │ │ ├── LengthConverterTests.cs │ │ │ ├── MassConverterTests.cs │ │ │ ├── PowerConverterTests.cs │ │ │ ├── TemperatureConverterTests.cs │ │ │ ├── TimeConverterTests.cs │ │ │ └── VolumeConverterTests.cs │ │ ├── LengthUnits │ │ │ ├── LengthTest.cs │ │ │ ├── LengthUnitTest.cs │ │ │ └── LengthValueTest.cs │ │ ├── MassUnits │ │ │ ├── MassTest.cs │ │ │ ├── MassUnitTest.cs │ │ │ └── MassValueTest.cs │ │ ├── PowerUnits │ │ │ ├── PowerTest.cs │ │ │ ├── PowerUnitTests.cs │ │ │ └── PowerValueTest.cs │ │ ├── TemperatureUnits │ │ │ ├── TemperatureTest.cs │ │ │ ├── TemperatureUnitTests.cs │ │ │ └── TemperatureValueTest.cs │ │ ├── TimeUnits │ │ │ ├── TimeTest.cs │ │ │ ├── TimeUnitTest.cs │ │ │ └── TimeValueTest.cs │ │ └── VolumeUnits │ │ │ ├── VolumeTest.cs │ │ │ ├── VolumeUnitTest.cs │ │ │ └── VolumeValueTest.cs │ └── VariableTest.cs ├── ParserTests │ ├── AngleTests.cs │ ├── AreaUnitTests.cs │ ├── AssignTests.cs │ ├── BaseParserTests.cs │ ├── ComplexNumberTests.cs │ ├── ConditionalOperatorTests.cs │ ├── EqualityOperatorTests.cs │ ├── ForTests.cs │ ├── GCDTests.cs │ ├── HyperbolicTests.cs │ ├── IfTests.cs │ ├── LambdaTests.cs │ ├── LengthUnitTests.cs │ ├── LogTests.cs │ ├── LogicalOperatorTests.cs │ ├── MassUnitTests.cs │ ├── NotBalancedParenthesisTests.cs │ ├── NumberTests.cs │ ├── ParserTest.cs │ ├── PowerTests.cs │ ├── PowerUnitTests.cs │ ├── RelationalOperatorTests.cs │ ├── ReverseHyperbolicTests.cs │ ├── ReverseTrigonometricTests.cs │ ├── StringTests.cs │ ├── TemperatureUnitTests.cs │ ├── TimeUnitTests.cs │ ├── TrigonometricTests.cs │ ├── UnassignTests.cs │ ├── UnitTests.cs │ ├── VolumeUnitTests.cs │ └── WhileTests.cs ├── ProcessorTest.cs ├── Results │ ├── AngleResultTest.cs │ ├── AreaResultTest.cs │ ├── BooleanResultTest.cs │ ├── ComplexNumberResultTest.cs │ ├── LambdaResultTest.cs │ ├── LengthResultTest.cs │ ├── MassResultTest.cs │ ├── MatrixResultTest.cs │ ├── NumberResultTest.cs │ ├── PowerResultTest.cs │ ├── RationalResultTest.cs │ ├── StringResultTest.cs │ ├── TemperatureResultTest.cs │ ├── TimeResultTest.cs │ ├── VectorResultTest.cs │ └── VolumeResultTest.cs └── xFunc.Tests.csproj ├── xFunc.png ├── xFunc.ruleset └── xFunc.sln /.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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.github/workflows/github-pages.yml: -------------------------------------------------------------------------------- 1 | name: Deploy static content to Pages 2 | 3 | on: 4 | push: 5 | branches: ["dev"] 6 | 7 | workflow_dispatch: 8 | 9 | permissions: 10 | contents: read 11 | pages: write 12 | id-token: write 13 | 14 | concurrency: 15 | group: "pages" 16 | cancel-in-progress: false 17 | 18 | jobs: 19 | deploy: 20 | environment: 21 | name: github-pages 22 | url: ${{ steps.deployment.outputs.page_url }} 23 | runs-on: ubuntu-latest 24 | steps: 25 | - name: Checkout 26 | uses: actions/checkout@v4 27 | with: 28 | fetch-depth: 0 29 | 30 | - uses: actions/setup-dotnet@v4 31 | with: 32 | dotnet-version: '9.0.x' 33 | 34 | - run: dotnet build -c Release xFunc.sln 35 | - run: dotnet tool update -g docfx 36 | - run: docfx docs/docfx.json 37 | 38 | - name: Setup Pages 39 | uses: actions/configure-pages@v3 40 | 41 | - name: Upload artifact 42 | uses: actions/upload-pages-artifact@v3 43 | with: 44 | path: './docs/_site/' 45 | 46 | - name: Deploy to GitHub Pages 47 | id: deployment 48 | uses: actions/deploy-pages@v4 49 | -------------------------------------------------------------------------------- /.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 -------------------------------------------------------------------------------- /.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 | } -------------------------------------------------------------------------------- /.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 | } -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /CI/codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | precision: 2 3 | round: down 4 | range: "80...90" 5 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | *.* @sys27 2 | -------------------------------------------------------------------------------- /Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | <_LocalTopLevelSourceRoot Include="@(SourceRoot)" Condition="'%(SourceRoot.NestedRoot)' == ''"/> 8 | 9 | 10 | -------------------------------------------------------------------------------- /Directory.Packages.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright © 2012-2024 Dmytro Kyshchenko 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /docs/.gitignore: -------------------------------------------------------------------------------- 1 | ############### 2 | # folder # 3 | ############### 4 | /**/DROP/ 5 | /**/TEMP/ 6 | /**/packages/ 7 | /**/bin/ 8 | /**/obj/ 9 | _site 10 | -------------------------------------------------------------------------------- /docs/api/.gitignore: -------------------------------------------------------------------------------- 1 | ############### 2 | # temp file # 3 | ############### 4 | *.yml 5 | .manifest 6 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /docs/images/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sys27/xFunc/ed48d73bad5d59adb8fbfb826e9432383fc0fbd6/docs/images/favicon.ico -------------------------------------------------------------------------------- /docs/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sys27/xFunc/ed48d73bad5d59adb8fbfb826e9432383fc0fbd6/docs/images/logo.png -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | ../README.md -------------------------------------------------------------------------------- /docs/toc.yml: -------------------------------------------------------------------------------- 1 | - name: Articles 2 | href: articles/ 3 | - name: Api Documentation 4 | href: api/ 5 | homepage: api/xFunc.Maths.Processor.yml 6 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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.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.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.Benchmark/Benchmarks/MatrixBenchmark.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.Immutable; 5 | 6 | namespace xFunc.Benchmark.Benchmarks; 7 | 8 | public class MatrixBenchmark 9 | { 10 | private Matrix matrix1; 11 | private Matrix matrix2; 12 | 13 | [Params(2, 10, 100)] 14 | public int Size; 15 | 16 | [GlobalSetup] 17 | public void Setup() 18 | { 19 | matrix1 = CreateMatrix(); 20 | matrix2 = CreateMatrix(); 21 | } 22 | 23 | private Matrix CreateMatrix() 24 | { 25 | var vectors = ImmutableArray.CreateBuilder(Size); 26 | for (var i = 0; i < Size; i++) 27 | { 28 | var vector = ImmutableArray.CreateBuilder(Size); 29 | for (var j = 0; j < Size; j++) 30 | vector.Add(new Number(Random.Shared.Next())); 31 | 32 | vectors.Add(new Vector(vector.ToImmutableArray())); 33 | } 34 | 35 | return new Matrix(vectors.ToImmutableArray()); 36 | } 37 | 38 | [Benchmark] 39 | public object AddMatrix() 40 | => new Add(matrix1, matrix2).Execute(); 41 | 42 | [Benchmark] 43 | public object SubMatrix() 44 | => new Sub(matrix1, matrix2).Execute(); 45 | 46 | [Benchmark] 47 | public object MulMatrix() 48 | => new Mul(matrix1, matrix2).Execute(); 49 | 50 | [Benchmark] 51 | public object MulMatrixByNumber() 52 | => new Mul(matrix1, Number.Two).Execute(); 53 | } -------------------------------------------------------------------------------- /xFunc.Benchmark/Benchmarks/ProcessorBenchmark.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 ProcessorBenchmark 7 | { 8 | private Processor processor; 9 | 10 | [GlobalSetup] 11 | public void Setup() 12 | { 13 | processor = new Processor(); 14 | } 15 | 16 | [Benchmark] 17 | public IExpression Parse() 18 | => processor.Parse("(100.1 + 2 * (3 * sin(4 * cos(5 * tan(6 * ctg(10 * x)))) * 3) / (func(a, b, c) ^ 2)) - (cos(y) - 111.3) & (true | false impl true eq false) + (det({{1, 2}, {3, 4}}) * 10 * log(2, 3)) + re(3 + 2 * i) - im(2 - 9 * i) + (9 + 2 * i)"); 19 | 20 | [Benchmark] 21 | public Result Solve() 22 | => processor.Solve("count(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + (2 * sin(4 * cos(6 * tan(8 * cot(pi / 4) ^ 2) ^ 3) ^ 4) ^ 5 + 2 * sin(4 * cos(6 * tan(8 * cot(pi / 4) ^ 2) ^ 3) ^ 4) ^ 5 + 2 * sin(4 * cos(6 * tan(8 * cot(pi / 4) ^ 2) ^ 3) ^ 4) ^ 5 + 2 * sin(4 * cos(6 * tan(8 * cot(pi / 4) ^ 2) ^ 3) ^ 4) ^ 5) * 10 ^ 6 + 10!"); 23 | } -------------------------------------------------------------------------------- /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.Benchmark/Benchmarks/StatisticalBenchmark.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 StatisticalBenchmark 7 | { 8 | private IExpression stdev; 9 | private IExpression stdevp; 10 | 11 | private IExpression var; 12 | private IExpression varp; 13 | 14 | [GlobalSetup] 15 | public void Setup() 16 | { 17 | var processor = new Processor(); 18 | 19 | stdev = processor.Parse("stdev(2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8)"); 20 | stdevp = processor.Parse("stdevp(2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8)"); 21 | 22 | var = processor.Parse("var(2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8)"); 23 | varp = processor.Parse("varp(2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8)"); 24 | } 25 | 26 | [Benchmark] 27 | public object StdevExecute() => stdev.Execute(); 28 | 29 | [Benchmark] 30 | public object StdevpExecute() => stdevp.Execute(); 31 | 32 | [Benchmark] 33 | public object VarExecute() => var.Execute(); 34 | 35 | [Benchmark] 36 | public object VarpExecute() => varp.Execute(); 37 | } -------------------------------------------------------------------------------- /xFunc.Benchmark/Benchmarks/TransposeBenchmark.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.Immutable; 5 | 6 | namespace xFunc.Benchmark.Benchmarks; 7 | 8 | public class TransposeBenchmark 9 | { 10 | private Transpose transpose; 11 | 12 | [ParamsSource(nameof(GetSizes))] 13 | public (int rows, int columns) Size; 14 | 15 | public IEnumerable<(int rows, int columns)> GetSizes() 16 | { 17 | yield return (2, 3); 18 | yield return (8, 10); 19 | } 20 | 21 | [GlobalSetup] 22 | public void Setup() 23 | { 24 | var matrix = CreateMatrix(); 25 | 26 | transpose = new Transpose(matrix); 27 | } 28 | 29 | private Matrix CreateMatrix() 30 | { 31 | var vectors = ImmutableArray.CreateBuilder(Size.rows); 32 | for (var i = 0; i < Size.rows; i++) 33 | { 34 | var vector = ImmutableArray.CreateBuilder(Size.columns); 35 | for (var j = 0; j < Size.columns; j++) 36 | vector.Add(new Number(Random.Shared.Next())); 37 | 38 | vectors.Add(new Vector(vector.ToImmutableArray())); 39 | } 40 | 41 | return new Matrix(vectors.ToImmutableArray()); 42 | } 43 | 44 | [Benchmark] 45 | public object TransposeMatrix() => transpose.Execute(); 46 | } -------------------------------------------------------------------------------- /xFunc.Benchmark/Benchmarks/VectorBenchmark.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.Immutable; 5 | using xFunc.Maths.Expressions.Statistical; 6 | 7 | namespace xFunc.Benchmark.Benchmarks; 8 | 9 | public class VectorBenchmark 10 | { 11 | private Vector vector1; 12 | private Vector vector2; 13 | 14 | [Params(2, 10, 100)] 15 | public int Size; 16 | 17 | [GlobalSetup] 18 | public void Setup() 19 | { 20 | vector1 = CreateVector(); 21 | vector2 = CreateVector(); 22 | } 23 | 24 | private Vector CreateVector() 25 | { 26 | var vector = ImmutableArray.CreateBuilder(Size); 27 | for (var j = 0; j < Size; j++) 28 | vector.Add(new Number(Random.Shared.Next())); 29 | 30 | return new Vector(vector.ToImmutableArray()); 31 | } 32 | 33 | [Benchmark] 34 | public object AbsVector() 35 | => new Abs(vector1).Execute(); 36 | 37 | [Benchmark] 38 | public object AddVectors() 39 | => new Add(vector1, vector2).Execute(); 40 | 41 | [Benchmark] 42 | public object SubVectors() 43 | => new Sub(vector1, vector2).Execute(); 44 | 45 | [Benchmark] 46 | public object MulVectors() 47 | => new Mul(vector1, vector2).Execute(); 48 | 49 | [Benchmark] 50 | public object MulVectorByNumber() 51 | => new Mul(vector1, Number.Two).Execute(); 52 | 53 | [Benchmark] 54 | public object SumVector() 55 | => new Sum(new[] { vector1 }).Execute(); 56 | } -------------------------------------------------------------------------------- /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.NetCoreApp90)) 25 | .AddDiagnoser(MemoryDiagnoser.Default) 26 | .StopOnFirstError()); 27 | } 28 | } -------------------------------------------------------------------------------- /xFunc.Benchmark/xFunc.Benchmark.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net9.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.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 | } -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /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.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.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.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.Cli/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "xFunc.Cli": { 4 | "commandName": "Project", 5 | "commandLineArgs": "interactive" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /xFunc.Maths/Analyzers/DifferentiatorContext.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 context for differentiator. 8 | /// 9 | public class DifferentiatorContext 10 | { 11 | /// 12 | /// Initializes a new instance of the class. 13 | /// 14 | public DifferentiatorContext() 15 | : this(Variable.X) 16 | { 17 | } 18 | 19 | /// 20 | /// Initializes a new instance of the class. 21 | /// 22 | /// The variable. 23 | public DifferentiatorContext(Variable variable) 24 | => Variable = variable ?? throw new ArgumentNullException(nameof(variable)); 25 | 26 | /// 27 | /// Gets the variable. 28 | /// 29 | public Variable Variable { get; } 30 | } -------------------------------------------------------------------------------- /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.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.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/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.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.Maths/Expressions/LogicalAndBitwise/Not.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.Expressions.LogicalAndBitwise; 7 | 8 | /// 9 | /// Represents a bitwise NOT operator. 10 | /// 11 | public class Not : UnaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The argument of function. 17 | /// 18 | public Not(IExpression expression) 19 | : base(expression) 20 | { 21 | } 22 | 23 | /// 24 | public override object Execute(ExpressionParameters? parameters) 25 | { 26 | var arg = Argument.Execute(parameters); 27 | 28 | return arg switch 29 | { 30 | bool boolean => !boolean, 31 | NumberValue number => ~number, 32 | _ => throw ExecutionException.For(this), 33 | }; 34 | } 35 | 36 | /// 37 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 38 | => analyzer.Analyze(this); 39 | 40 | /// 41 | [ExcludeFromCodeCoverage] 42 | protected override TResult AnalyzeInternal( 43 | IAnalyzer analyzer, 44 | TContext context) 45 | => analyzer.Analyze(this, context); 46 | 47 | /// 48 | public override IExpression Clone(IExpression? argument = null) 49 | => new Not(argument ?? Argument); 50 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Matrices/InvalidMatrixException.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.Matrices; 5 | 6 | /// 7 | /// Thrown in matrix building. 8 | /// 9 | [Serializable] 10 | public class InvalidMatrixException : Exception 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | public InvalidMatrixException() 16 | { 17 | } 18 | 19 | /// 20 | /// Initializes a new instance of the class. 21 | /// 22 | /// The message that describes the error. 23 | public InvalidMatrixException(string message) 24 | : base(message) 25 | { 26 | } 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// 31 | /// A that describes the error. 32 | /// The exception that is the cause of the current exception. 33 | public InvalidMatrixException(string message, Exception inner) 34 | : base(message, inner) 35 | { 36 | } 37 | } -------------------------------------------------------------------------------- /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.Maths/Expressions/Programming/AddAssign.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.Expressions.Programming; 7 | 8 | /// 9 | /// Represents the "+=" operator. 10 | /// 11 | public class AddAssign : VariableBinaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The variable. 17 | /// The expression. 18 | public AddAssign(Variable variable, IExpression exp) 19 | : base(variable, exp) 20 | { 21 | } 22 | 23 | /// 24 | protected override object Execute(NumberValue variableValue, NumberValue value) 25 | => variableValue + value; 26 | 27 | /// 28 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 29 | => analyzer.Analyze(this); 30 | 31 | /// 32 | [ExcludeFromCodeCoverage] 33 | protected override TResult AnalyzeInternal( 34 | IAnalyzer analyzer, 35 | TContext context) 36 | => analyzer.Analyze(this, context); 37 | 38 | /// 39 | public override IExpression Clone(Variable? variable = null, IExpression? value = null) 40 | => new AddAssign(variable ?? Variable, value ?? Value); 41 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Programming/Dec.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.Expressions.Programming; 7 | 8 | /// 9 | /// Represents the decrement operator. 10 | /// 11 | public class Dec : VariableUnaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The variable. 17 | public Dec(Variable argument) 18 | : base(argument) 19 | { 20 | } 21 | 22 | /// 23 | protected override object Execute(NumberValue number) => number - 1; 24 | 25 | /// 26 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 27 | => analyzer.Analyze(this); 28 | 29 | /// 30 | [ExcludeFromCodeCoverage] 31 | protected override TResult AnalyzeInternal( 32 | IAnalyzer analyzer, 33 | TContext context) 34 | => analyzer.Analyze(this, context); 35 | 36 | /// 37 | public override IExpression Clone(Variable? variable = null) 38 | => new Dec(variable ?? Variable); 39 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Programming/DivAssign.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.Expressions.Programming; 7 | 8 | /// 9 | /// Represents the "/=" operator. 10 | /// 11 | public class DivAssign : VariableBinaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The variable. 17 | /// The expression. 18 | public DivAssign(Variable variable, IExpression exp) 19 | : base(variable, exp) 20 | { 21 | } 22 | 23 | /// 24 | protected override object Execute(NumberValue variableValue, NumberValue value) 25 | => variableValue / value; 26 | 27 | /// 28 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 29 | => analyzer.Analyze(this); 30 | 31 | /// 32 | [ExcludeFromCodeCoverage] 33 | protected override TResult AnalyzeInternal( 34 | IAnalyzer analyzer, 35 | TContext context) 36 | => analyzer.Analyze(this, context); 37 | 38 | /// 39 | public override IExpression Clone(Variable? variable = null, IExpression? value = null) 40 | => new DivAssign(variable ?? Variable, value ?? Value); 41 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Programming/Inc.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.Expressions.Programming; 7 | 8 | /// 9 | /// Represents the increment operator. 10 | /// 11 | public class Inc : VariableUnaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The variable. 17 | public Inc(Variable argument) 18 | : base(argument) 19 | { 20 | } 21 | 22 | /// 23 | protected override object Execute(NumberValue number) => number + 1; 24 | 25 | /// 26 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 27 | => analyzer.Analyze(this); 28 | 29 | /// 30 | [ExcludeFromCodeCoverage] 31 | protected override TResult AnalyzeInternal( 32 | IAnalyzer analyzer, 33 | TContext context) 34 | => analyzer.Analyze(this, context); 35 | 36 | /// 37 | public override IExpression Clone(Variable? variable = null) 38 | => new Inc(variable ?? Variable); 39 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Programming/LeftShiftAssign.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.Expressions.Programming; 7 | 8 | /// 9 | /// Represents the '<<=' operator. 10 | /// 11 | public class LeftShiftAssign : VariableBinaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The variable. 17 | /// The expression. 18 | public LeftShiftAssign(Variable variable, IExpression exp) 19 | : base(variable, exp) 20 | { 21 | } 22 | 23 | /// 24 | protected override object Execute(NumberValue variableValue, NumberValue value) 25 | => NumberValue.LeftShift(variableValue, value); 26 | 27 | /// 28 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 29 | => analyzer.Analyze(this); 30 | 31 | /// 32 | [ExcludeFromCodeCoverage] 33 | protected override TResult AnalyzeInternal( 34 | IAnalyzer analyzer, 35 | TContext context) 36 | => analyzer.Analyze(this, context); 37 | 38 | /// 39 | public override IExpression Clone(Variable? variable = null, IExpression? value = null) 40 | => new LeftShiftAssign(variable ?? Variable, value ?? Value); 41 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Programming/MulAssign.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.Expressions.Programming; 7 | 8 | /// 9 | /// Represents the "*=" operator. 10 | /// 11 | public class MulAssign : VariableBinaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The variable. 17 | /// The expression. 18 | public MulAssign(Variable variable, IExpression exp) 19 | : base(variable, exp) 20 | { 21 | } 22 | 23 | /// 24 | protected override object Execute(NumberValue variableValue, NumberValue value) 25 | => variableValue * value; 26 | 27 | /// 28 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 29 | => analyzer.Analyze(this); 30 | 31 | /// 32 | [ExcludeFromCodeCoverage] 33 | protected override TResult AnalyzeInternal( 34 | IAnalyzer analyzer, 35 | TContext context) 36 | => analyzer.Analyze(this, context); 37 | 38 | /// 39 | public override IExpression Clone(Variable? variable = null, IExpression? value = null) 40 | => new MulAssign(variable ?? Variable, value ?? Value); 41 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Programming/RightShiftAssign.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.Expressions.Programming; 7 | 8 | /// 9 | /// Represents the '<<=' operator. 10 | /// 11 | public class RightShiftAssign : VariableBinaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The variable. 17 | /// The expression. 18 | public RightShiftAssign(Variable variable, IExpression exp) 19 | : base(variable, exp) 20 | { 21 | } 22 | 23 | /// 24 | protected override object Execute(NumberValue variableValue, NumberValue value) 25 | => NumberValue.RightShift(variableValue, value); 26 | 27 | /// 28 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 29 | => analyzer.Analyze(this); 30 | 31 | /// 32 | [ExcludeFromCodeCoverage] 33 | protected override TResult AnalyzeInternal( 34 | IAnalyzer analyzer, 35 | TContext context) 36 | => analyzer.Analyze(this, context); 37 | 38 | /// 39 | public override IExpression Clone(Variable? variable = null, IExpression? value = null) 40 | => new RightShiftAssign(variable ?? Variable, value ?? Value); 41 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Programming/SubAssign.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.Expressions.Programming; 7 | 8 | /// 9 | /// Represents the "-=" operator. 10 | /// 11 | public class SubAssign : VariableBinaryExpression 12 | { 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | /// The variable. 17 | /// The expression. 18 | public SubAssign(Variable variable, IExpression exp) 19 | : base(variable, exp) 20 | { 21 | } 22 | 23 | /// 24 | protected override object Execute(NumberValue variableValue, NumberValue value) 25 | => variableValue - value; 26 | 27 | /// 28 | protected override TResult AnalyzeInternal(IAnalyzer analyzer) 29 | => analyzer.Analyze(this); 30 | 31 | /// 32 | [ExcludeFromCodeCoverage] 33 | protected override TResult AnalyzeInternal( 34 | IAnalyzer analyzer, 35 | TContext context) 36 | => analyzer.Analyze(this, context); 37 | 38 | /// 39 | public override IExpression Clone(Variable? variable = null, IExpression? value = null) 40 | => new SubAssign(variable ?? Variable, value ?? Value); 41 | } -------------------------------------------------------------------------------- /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/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/Converters/AngleConverter.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 angle unit converter. 8 | /// 9 | public class AngleConverter : IConverter, IConverter 10 | { 11 | /// 12 | public AngleValue Convert(object value, string unit) 13 | { 14 | if (value is null) 15 | throw new ArgumentNullException(nameof(value)); 16 | if (string.IsNullOrWhiteSpace(unit)) 17 | throw new ArgumentNullException(nameof(unit)); 18 | 19 | if (!AngleUnit.FromName(unit, out var angleUnit)) 20 | throw new UnitIsNotSupportedException(unit); 21 | 22 | return value switch 23 | { 24 | AngleValue angleValue => angleValue.To(angleUnit), 25 | NumberValue numberValue => new AngleValue(numberValue, angleUnit), 26 | _ => throw new ValueIsNotSupportedException(value), 27 | }; 28 | } 29 | 30 | /// 31 | object IConverter.Convert(object value, string unit) 32 | => Convert(value, unit); 33 | 34 | /// 35 | public bool CanConvertTo(object value, string unit) 36 | => value is AngleValue or NumberValue && AngleUnit.Names.Contains(unit.ToLower()); 37 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/Converters/AreaConverter.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 area unit converter. 8 | /// 9 | public class AreaConverter : IConverter, IConverter 10 | { 11 | /// 12 | public AreaValue Convert(object value, string unit) 13 | { 14 | if (value is null) 15 | throw new ArgumentNullException(nameof(value)); 16 | if (string.IsNullOrWhiteSpace(unit)) 17 | throw new ArgumentNullException(nameof(unit)); 18 | 19 | if (!AreaUnit.FromName(unit, out var areaUnit)) 20 | throw new UnitIsNotSupportedException(unit); 21 | 22 | return value switch 23 | { 24 | AreaValue areaValue => areaValue.To(areaUnit), 25 | NumberValue numberValue => new AreaValue(numberValue, areaUnit), 26 | _ => throw new ValueIsNotSupportedException(value), 27 | }; 28 | } 29 | 30 | /// 31 | object IConverter.Convert(object value, string unit) 32 | => Convert(value, unit); 33 | 34 | /// 35 | public bool CanConvertTo(object value, string unit) 36 | => value is AreaValue or NumberValue && AreaUnit.Names.Contains(unit.ToLower()); 37 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/Converters/Converter.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 unit converter. 8 | /// 9 | public class Converter : IConverter 10 | { 11 | private readonly IConverter[] converters; 12 | 13 | /// 14 | /// Initializes a new instance of the class. 15 | /// 16 | public Converter() 17 | => converters = 18 | [ 19 | new AngleConverter(), 20 | new PowerConverter(), 21 | new TemperatureConverter(), 22 | new MassConverter(), 23 | new AreaConverter(), 24 | new LengthConverter(), 25 | new TimeConverter(), 26 | new VolumeConverter() 27 | ]; 28 | 29 | /// 30 | public object Convert(object value, string unit) 31 | { 32 | if (value is null) 33 | throw new ArgumentNullException(nameof(value)); 34 | if (string.IsNullOrWhiteSpace(unit)) 35 | throw new ArgumentNullException(nameof(unit)); 36 | 37 | unit = unit.ToLowerInvariant(); 38 | 39 | foreach (var converter in converters) 40 | if (converter.CanConvertTo(value, unit)) 41 | return converter.Convert(value, unit); 42 | 43 | throw new UnitIsNotSupportedException(unit); 44 | } 45 | } -------------------------------------------------------------------------------- /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/Units/Converters/IConverter{TValue}.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 | /// The type of return value of converter. 10 | public interface IConverter 11 | { 12 | /// 13 | /// Converts to specified . 14 | /// 15 | /// The value to convert. 16 | /// The unit to convert to. 17 | /// The converter value. 18 | TValue Convert(object value, string unit); 19 | 20 | /// 21 | /// Determines whether the current converter can convert to specified unit. 22 | /// 23 | /// The value to convert. 24 | /// The unit to convert to. 25 | /// 26 | /// true if converter can convert using ; otherwise, false. 27 | /// 28 | bool CanConvertTo(object value, string unit); 29 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/Converters/LengthConverter.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 length unit converter. 8 | /// 9 | public class LengthConverter : IConverter, IConverter 10 | { 11 | /// 12 | public LengthValue Convert(object value, string unit) 13 | { 14 | if (value is null) 15 | throw new ArgumentNullException(nameof(value)); 16 | if (string.IsNullOrWhiteSpace(unit)) 17 | throw new ArgumentNullException(nameof(unit)); 18 | 19 | if (!LengthUnit.FromName(unit, out var lengthUnit)) 20 | throw new UnitIsNotSupportedException(unit); 21 | 22 | return value switch 23 | { 24 | LengthValue lengthValue => lengthValue.To(lengthUnit), 25 | NumberValue numberValue => new LengthValue(numberValue, lengthUnit), 26 | _ => throw new ValueIsNotSupportedException(value), 27 | }; 28 | } 29 | 30 | /// 31 | object IConverter.Convert(object value, string unit) 32 | => Convert(value, unit); 33 | 34 | /// 35 | public bool CanConvertTo(object value, string unit) 36 | => value is LengthValue or NumberValue && LengthUnit.Names.Contains(unit.ToLower()); 37 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/Converters/MassConverter.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 mass unit converter. 8 | /// 9 | public class MassConverter : IConverter, IConverter 10 | { 11 | /// 12 | public MassValue Convert(object value, string unit) 13 | { 14 | if (value is null) 15 | throw new ArgumentNullException(nameof(value)); 16 | if (string.IsNullOrWhiteSpace(unit)) 17 | throw new ArgumentNullException(nameof(unit)); 18 | 19 | if (!MassUnit.FromName(unit, out var massUnit)) 20 | throw new UnitIsNotSupportedException(unit); 21 | 22 | return value switch 23 | { 24 | MassValue massValue => massValue.To(massUnit), 25 | NumberValue numberValue => new MassValue(numberValue, massUnit), 26 | _ => throw new ValueIsNotSupportedException(value), 27 | }; 28 | } 29 | 30 | /// 31 | object IConverter.Convert(object value, string unit) 32 | => Convert(value, unit); 33 | 34 | /// 35 | public bool CanConvertTo(object value, string unit) 36 | => value is MassValue or NumberValue && MassUnit.Names.Contains(unit.ToLower()); 37 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/Converters/PowerConverter.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 power unit converter. 8 | /// 9 | public class PowerConverter : IConverter, IConverter 10 | { 11 | /// 12 | public PowerValue Convert(object value, string unit) 13 | { 14 | if (value is null) 15 | throw new ArgumentNullException(nameof(value)); 16 | if (string.IsNullOrWhiteSpace(unit)) 17 | throw new ArgumentNullException(nameof(unit)); 18 | 19 | if (!PowerUnit.FromName(unit, out var powerUnit)) 20 | throw new UnitIsNotSupportedException(unit); 21 | 22 | return value switch 23 | { 24 | PowerValue powerValue => powerValue.To(powerUnit), 25 | NumberValue numberValue => new PowerValue(numberValue, powerUnit), 26 | _ => throw new ValueIsNotSupportedException(value), 27 | }; 28 | } 29 | 30 | /// 31 | object IConverter.Convert(object value, string unit) 32 | => Convert(value, unit); 33 | 34 | /// 35 | public bool CanConvertTo(object value, string unit) 36 | => value is PowerValue or NumberValue && PowerUnit.Names.Contains(unit.ToLower()); 37 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/Converters/TemperatureConverter.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 temperature unit converter. 8 | /// 9 | public class TemperatureConverter : IConverter, IConverter 10 | { 11 | /// 12 | public TemperatureValue Convert(object value, string unit) 13 | { 14 | if (value is null) 15 | throw new ArgumentNullException(nameof(value)); 16 | if (string.IsNullOrWhiteSpace(unit)) 17 | throw new ArgumentNullException(nameof(unit)); 18 | 19 | if (!TemperatureUnit.FromName(unit, out var temperatureUnit)) 20 | throw new UnitIsNotSupportedException(unit); 21 | 22 | return value switch 23 | { 24 | TemperatureValue temperatureValue => temperatureValue.To(temperatureUnit), 25 | NumberValue numberValue => new TemperatureValue(numberValue, temperatureUnit), 26 | _ => throw new ValueIsNotSupportedException(value), 27 | }; 28 | } 29 | 30 | /// 31 | object IConverter.Convert(object value, string unit) 32 | => Convert(value, unit); 33 | 34 | /// 35 | public bool CanConvertTo(object value, string unit) 36 | => value is TemperatureValue or NumberValue && TemperatureUnit.Names.Contains(unit.ToLower()); 37 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/Converters/TimeConverter.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 time unit converter. 8 | /// 9 | public class TimeConverter : IConverter, IConverter 10 | { 11 | /// 12 | public TimeValue Convert(object value, string unit) 13 | { 14 | if (value is null) 15 | throw new ArgumentNullException(nameof(value)); 16 | if (string.IsNullOrWhiteSpace(unit)) 17 | throw new ArgumentNullException(nameof(unit)); 18 | 19 | if (!TimeUnit.FromName(unit, out var timeUnit)) 20 | throw new UnitIsNotSupportedException(unit); 21 | 22 | return value switch 23 | { 24 | TimeValue timeValue => timeValue.To(timeUnit), 25 | NumberValue numberValue => new TimeValue(numberValue, timeUnit), 26 | _ => throw new ValueIsNotSupportedException(value), 27 | }; 28 | } 29 | 30 | /// 31 | object IConverter.Convert(object value, string unit) 32 | => Convert(value, unit); 33 | 34 | /// 35 | public bool CanConvertTo(object value, string unit) 36 | => value is TimeValue or NumberValue && TimeUnit.Names.Contains(unit.ToLower()); 37 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/Converters/VolumeConverter.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 volume unit converter. 8 | /// 9 | public class VolumeConverter : IConverter, IConverter 10 | { 11 | /// 12 | public VolumeValue Convert(object value, string unit) 13 | { 14 | if (value is null) 15 | throw new ArgumentNullException(nameof(value)); 16 | if (string.IsNullOrWhiteSpace(unit)) 17 | throw new ArgumentNullException(nameof(unit)); 18 | 19 | if (!VolumeUnit.FromName(unit, out var volumeUnit)) 20 | throw new UnitIsNotSupportedException(unit); 21 | 22 | return value switch 23 | { 24 | VolumeValue volumeValue => volumeValue.To(volumeUnit), 25 | NumberValue numberValue => new VolumeValue(numberValue, volumeUnit), 26 | _ => throw new ValueIsNotSupportedException(value), 27 | }; 28 | } 29 | 30 | /// 31 | object IConverter.Convert(object value, string unit) 32 | => Convert(value, unit); 33 | 34 | /// 35 | public bool CanConvertTo(object value, string unit) 36 | => value is VolumeValue or NumberValue && VolumeUnit.Names.Contains(unit.ToLower()); 37 | } -------------------------------------------------------------------------------- /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.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/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 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/TemperatureUnits/Temperature.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.TemperatureUnits; 5 | 6 | /// 7 | /// Represents an temperature number. 8 | /// 9 | public class Temperature : Unit 10 | { 11 | /// 12 | /// Initializes a new instance of the class. 13 | /// 14 | /// A temperature number. 15 | public Temperature(TemperatureValue 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/UnitIsNotSupportedException.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; 5 | 6 | /// 7 | /// Represents the exception that is thrown when converter uses unsupported unit. 8 | /// 9 | [Serializable] 10 | public class UnitIsNotSupportedException : Exception 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | /// The unsupported unit. 16 | public UnitIsNotSupportedException(string? unit) 17 | : this(unit, null) 18 | { 19 | } 20 | 21 | /// 22 | /// Initializes a new instance of the class. 23 | /// 24 | /// The unsupported unit. 25 | /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. 26 | public UnitIsNotSupportedException(string? unit, Exception? inner) 27 | : base(string.Format(Resource.UnitIsNotSupportedException, unit), inner) 28 | { 29 | Unit = unit; 30 | } 31 | 32 | /// 33 | /// Gets the unsupported unit. 34 | /// 35 | public string? Unit { get; } 36 | } -------------------------------------------------------------------------------- /xFunc.Maths/Expressions/Units/ValueIsNotSupportedException.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; 5 | 6 | /// 7 | /// Represents the exception that is thrown when converter uses unsupported value to convert. 8 | /// 9 | [Serializable] 10 | public class ValueIsNotSupportedException : Exception 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | /// The value to convert. 16 | public ValueIsNotSupportedException(object value) 17 | : this(value, null) 18 | { 19 | } 20 | 21 | /// 22 | /// Initializes a new instance of the class. 23 | /// 24 | /// The value to convert. 25 | /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. 26 | public ValueIsNotSupportedException(object value, Exception? inner) 27 | : base(string.Format(Resource.ValueIsNotSupportedException, value, value.GetType().Name), inner) 28 | { 29 | Value = value; 30 | } 31 | 32 | /// 33 | /// Gets a value to converts. 34 | /// 35 | public object? Value { get; } 36 | } -------------------------------------------------------------------------------- /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.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/InvalidResultException.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 | /// Throws when a result is invalid. 8 | /// 9 | [Serializable] 10 | public class InvalidResultException : Exception 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | public InvalidResultException() 16 | { 17 | } 18 | 19 | /// 20 | /// Initializes a new instance of the class. 21 | /// 22 | /// The message that describes the error. 23 | public InvalidResultException(string message) 24 | : base(message) 25 | { 26 | } 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// 31 | /// The message that describes the error. 32 | /// The inner exception. 33 | public InvalidResultException(string message, Exception inner) 34 | : base(message, inner) 35 | { 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Maths/ParseException.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 exception that is thrown in the process of parsing expression tree. 8 | /// 9 | [Serializable] 10 | public class ParseException : Exception 11 | { 12 | /// 13 | /// Initializes a new instance of the class. 14 | /// 15 | public ParseException() 16 | { 17 | } 18 | 19 | /// 20 | /// Initializes a new instance of the class with a specified error message. 21 | /// 22 | /// A that describes the error. 23 | public ParseException(string message) 24 | : base(message) 25 | { 26 | } 27 | 28 | /// 29 | /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. 30 | /// 31 | /// A that describes the error. 32 | /// The exception that is the cause of the current exception. 33 | public ParseException(string message, Exception inner) 34 | : base(message, inner) 35 | { 36 | } 37 | } -------------------------------------------------------------------------------- /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.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/Tokenization/Lexer.SymbolToken.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 static xFunc.Maths.Tokenization.TokenKind; 5 | 6 | namespace xFunc.Maths.Tokenization; 7 | 8 | /// 9 | /// The lexer for mathematical expressions. 10 | /// 11 | internal ref partial struct Lexer 12 | { 13 | private bool CreateSymbol() 14 | { 15 | var symbol = function[0] switch 16 | { 17 | '(' => OpenParenthesisSymbol, 18 | ')' => CloseParenthesisSymbol, 19 | '{' => OpenBraceSymbol, 20 | '}' => CloseBraceSymbol, 21 | ',' => CommaSymbol, 22 | '∠' => AngleSymbol, 23 | '°' => DegreeSymbol, 24 | ':' => ColonSymbol, 25 | '?' => QuestionMarkSymbol, 26 | _ => Empty, 27 | }; 28 | 29 | if (symbol == Empty) 30 | return false; 31 | 32 | function = function[1..]; 33 | 34 | Current = Token.Create(symbol); 35 | return true; 36 | } 37 | } -------------------------------------------------------------------------------- /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 10 | { 11 | public IEnumerator GetEnumerator() 12 | { 13 | var iExp = typeof(IExpression); 14 | var asm = Assembly.GetAssembly(iExp); 15 | if (asm is null) 16 | throw new InvalidOperationException(); 17 | 18 | var exclude = new HashSet 19 | { 20 | typeof(Builder) 21 | }; 22 | 23 | var types = asm 24 | .GetTypes() 25 | .Where(type => iExp.IsAssignableFrom(type) && !type.IsAbstract && !exclude.Contains(type)) 26 | .Select(type => new[] { type }); 27 | 28 | return types.GetEnumerator(); 29 | } 30 | 31 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 32 | } -------------------------------------------------------------------------------- /xFunc.Tests/Analyzers/SimplifierTests/BaseSimplifierTest.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.Reflection; 5 | using NUnit.Framework.Internal; 6 | 7 | namespace xFunc.Tests.Analyzers.SimplifierTests; 8 | 9 | public abstract class BaseSimplifierTest : BaseTest 10 | { 11 | protected readonly ISimplifier simplifier; 12 | 13 | protected BaseSimplifierTest() 14 | { 15 | simplifier = new Simplifier(); 16 | } 17 | 18 | protected void SimplifyTest(IExpression exp, IExpression expected) 19 | { 20 | var simple = exp.Analyze(simplifier); 21 | 22 | Assert.That(simple, Is.EqualTo(expected)); 23 | } 24 | 25 | protected void TestNullExp(Type type) 26 | { 27 | try 28 | { 29 | var method = typeof(Simplifier) 30 | .GetMethod(nameof(Simplifier.Analyze), [type]); 31 | method.Invoke(simplifier, [null]); 32 | } 33 | catch (TargetInvocationException e) 34 | { 35 | if (e.InnerException is ArgumentNullException) 36 | return; 37 | 38 | throw; 39 | } 40 | 41 | throw new NUnitException("The exception is expected."); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Analyzers/TypeAnalyzerTests/NumericConvertionTests.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.Analyzers.TypeAnalyzerTests; 5 | 6 | public class NumericConvertionTests : TypeAnalyzerBaseTests 7 | { 8 | [Test] 9 | [TestCase(typeof(ToBin))] 10 | [TestCase(typeof(ToOct))] 11 | [TestCase(typeof(ToHex))] 12 | public void UndefinedTest(Type type) 13 | { 14 | var exp = Create(type, Variable.X); 15 | 16 | Test(exp, ResultTypes.String); 17 | } 18 | 19 | [Test] 20 | [TestCase(typeof(ToBin))] 21 | [TestCase(typeof(ToOct))] 22 | [TestCase(typeof(ToHex))] 23 | public void NumberTest(Type type) 24 | { 25 | var exp = Create(type, new Number(10)); 26 | 27 | Test(exp, ResultTypes.String); 28 | } 29 | 30 | [Test] 31 | [TestCase(typeof(ToBin))] 32 | [TestCase(typeof(ToOct))] 33 | [TestCase(typeof(ToHex))] 34 | public void BoolTest(Type type) 35 | { 36 | var exp = Create(type, Bool.False); 37 | 38 | TestException(exp); 39 | } 40 | } -------------------------------------------------------------------------------- /xFunc.Tests/Analyzers/TypeAnalyzerTests/ProgrammingTests/AssignmentOperatorsTests.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.Analyzers.TypeAnalyzerTests.ProgrammingTests; 5 | 6 | public class AssignmentOperatorsTests : TypeAnalyzerBaseTests 7 | { 8 | [Test] 9 | [TestCase(typeof(AddAssign))] 10 | [TestCase(typeof(SubAssign))] 11 | [TestCase(typeof(MulAssign))] 12 | [TestCase(typeof(DivAssign))] 13 | [TestCase(typeof(LeftShiftAssign))] 14 | [TestCase(typeof(RightShiftAssign))] 15 | public void TestAssignUndefined(Type type) 16 | { 17 | var exp = Create(type, Variable.X, Variable.X); 18 | 19 | Test(exp, ResultTypes.Number); 20 | } 21 | 22 | [Test] 23 | [TestCase(typeof(AddAssign))] 24 | [TestCase(typeof(SubAssign))] 25 | [TestCase(typeof(MulAssign))] 26 | [TestCase(typeof(DivAssign))] 27 | [TestCase(typeof(LeftShiftAssign))] 28 | [TestCase(typeof(RightShiftAssign))] 29 | public void TestAssignNumber(Type type) 30 | { 31 | var exp = Create(type, Variable.X, new Number(10)); 32 | 33 | Test(exp, ResultTypes.Number); 34 | } 35 | 36 | [Test] 37 | [TestCase(typeof(AddAssign))] 38 | [TestCase(typeof(SubAssign))] 39 | [TestCase(typeof(MulAssign))] 40 | [TestCase(typeof(DivAssign))] 41 | [TestCase(typeof(LeftShiftAssign))] 42 | [TestCase(typeof(RightShiftAssign))] 43 | public void TestAssignException(Type type) 44 | { 45 | var exp = Create(type, Variable.X, Bool.False); 46 | 47 | TestException(exp); 48 | } 49 | } -------------------------------------------------------------------------------- /xFunc.Tests/BaseTest.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; 5 | 6 | public abstract class BaseTest 7 | { 8 | protected IExpression Create(Type type, IExpression argument) 9 | => (IExpression)Activator.CreateInstance(type, argument); 10 | 11 | protected IExpression Create(Type type, IExpression left, IExpression right) 12 | => (IExpression)Activator.CreateInstance(type, left, right); 13 | 14 | protected IExpression Create(Type type, IList arguments) 15 | => (IExpression)Activator.CreateInstance(type, arguments); 16 | 17 | protected T Create(Type type, IList arguments) where T : IExpression 18 | => (T)Activator.CreateInstance(type, arguments); 19 | 20 | protected BinaryExpression CreateBinary(Type type, IExpression left, IExpression right) 21 | => (BinaryExpression)Activator.CreateInstance(type, left, right); 22 | 23 | protected DifferentParametersExpression CreateDiff(Type type, IList arguments) 24 | => (DifferentParametersExpression)Activator.CreateInstance(type, arguments); 25 | } -------------------------------------------------------------------------------- /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.Tests/Expressions/BinaryTest.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 BinaryTest 7 | { 8 | [Test] 9 | public void EqualsTest1() 10 | { 11 | var add1 = new Add(Number.Two, new Number(3)); 12 | var add2 = new Add(Number.Two, new Number(3)); 13 | 14 | Assert.That(add2, Is.EqualTo(add1)); 15 | } 16 | 17 | [Test] 18 | public void EqualsTest2() 19 | { 20 | var add = new Add(Number.Two, new Number(3)) as IExpression; 21 | var sub = new Sub(Number.Two, new Number(3)) as IExpression; 22 | 23 | Assert.That(sub, Is.Not.EqualTo(add)); 24 | } 25 | 26 | [Test] 27 | public void EqualsSameTest() 28 | { 29 | var exp = new Add(Number.One, Number.One); 30 | 31 | Assert.That(exp.Equals(exp), Is.True); 32 | } 33 | 34 | [Test] 35 | public void EqualsNullTest() 36 | { 37 | var exp = new Add(Number.One, Number.One); 38 | 39 | Assert.That(exp.Equals(null), Is.False); 40 | } 41 | 42 | [Test] 43 | public void LeftNullExceptionTest() 44 | { 45 | Assert.Throws(() => new Add(null, Number.One)); 46 | } 47 | 48 | [Test] 49 | public void RightNullExceptionTest() 50 | { 51 | Assert.Throws(() => new Add(Number.One, null)); 52 | } 53 | } -------------------------------------------------------------------------------- /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/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.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.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.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 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/ExpTest.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; 7 | 8 | public class ExpTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteTest1() 12 | { 13 | var exp = new Exp(Number.Two); 14 | var expected = NumberValue.Exp(new NumberValue(2)); 15 | 16 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void ExecuteTest2() 21 | { 22 | var expected = new Complex(4, 2); 23 | var exp = new Exp(new ComplexNumber(expected)); 24 | 25 | Assert.That(exp.Execute(), Is.EqualTo(Complex.Exp(expected))); 26 | } 27 | 28 | [Test] 29 | public void ExecuteExceptionTest() 30 | => TestNotSupported(new Exp(Bool.False)); 31 | 32 | [Test] 33 | public void CloneTest() 34 | { 35 | var exp = new Exp(Number.Zero); 36 | var clone = exp.Clone(); 37 | 38 | Assert.That(clone, Is.EqualTo(exp)); 39 | } 40 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/FactTest.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 FactTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest1() 10 | { 11 | var fact = new Fact(new Number(4)); 12 | var expected = new NumberValue(24.0); 13 | 14 | Assert.That(fact.Execute(), Is.EqualTo(expected)); 15 | } 16 | 17 | [Test] 18 | public void ExecuteTest2() 19 | { 20 | var fact = new Fact(Number.Zero); 21 | var expected = new NumberValue(1.0); 22 | 23 | Assert.That(fact.Execute(), Is.EqualTo(expected)); 24 | } 25 | 26 | [Test] 27 | public void ExecuteTest3() 28 | { 29 | var fact = new Fact(Number.One); 30 | var expected = new NumberValue(1.0); 31 | 32 | Assert.That(fact.Execute(), Is.EqualTo(expected)); 33 | } 34 | 35 | [Test] 36 | public void ExecuteTest4() 37 | { 38 | var fact = new Fact(new Number(-1)); 39 | var actual = (NumberValue)fact.Execute(); 40 | 41 | Assert.That(actual.IsNaN); 42 | } 43 | 44 | [Test] 45 | public void ExecuteTestException() 46 | => TestNotSupported(new Fact(Bool.False)); 47 | 48 | [Test] 49 | public void CloneTest() 50 | { 51 | var exp = new Fact(Number.Zero); 52 | var clone = exp.Clone(); 53 | 54 | Assert.That(clone, Is.EqualTo(exp)); 55 | } 56 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Hyperbolic/HyperbolicArcosecantTest.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.Hyperbolic; 7 | 8 | public class HyperbolicArcosecantTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arcsch(new Number(0.5)); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(1.2279471772995156); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arcsch(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(0.23133469857397318).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(-0.15038560432786197).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arcsch(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arcsch(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Hyperbolic/HyperbolicArcosineTest.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.Hyperbolic; 7 | 8 | public class HyperbolicArcosineTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arcosh(new Number(7)); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(2.6339157938496336); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arcosh(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(1.9686379257930964).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(0.606137822387294).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arcosh(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arcosh(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Hyperbolic/HyperbolicArcotangentTest.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.Hyperbolic; 7 | 8 | public class HyperbolicArcotangentTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arcoth(new Number(7)); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(0.14384103622589042); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arcoth(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(0.2290726829685388).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(-0.16087527719832109).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arcoth(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arcoth(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Hyperbolic/HyperbolicArsecantTest.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.Hyperbolic; 7 | 8 | public class HyperbolicArsecantTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arsech(new Number(0.5)); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(1.3169578969248166); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arsech(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(0.15735549884498526).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(-1.3408334244176887).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arsech(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arsech(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Hyperbolic/HyperbolicArsineTest.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.Hyperbolic; 7 | 8 | public class HyperbolicArsineTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arsinh(new Number(0.5)); 14 | var result = (AngleValue)exp.Execute(); 15 | var expected = AngleValue.Radian(0.48121182505960347); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arsinh(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(1.983387029916535432347076).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(0.5706527843210994007).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arsinh(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arsinh(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Hyperbolic/HyperbolicArtangentTest.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.Hyperbolic; 7 | 8 | public class HyperbolicArtangentTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Artanh(new Number(0.6)); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(0.6931471805599453); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Artanh(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(0.2290726829685388).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(1.4099210495965755).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Artanh(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Artanh(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LCMTest.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.Immutable; 5 | 6 | namespace xFunc.Tests.Expressions; 7 | 8 | public class LCMTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void NullArgTest() 12 | => Assert.Throws(() => new LCM(null)); 13 | 14 | [Test] 15 | public void ExecuteTest1() 16 | { 17 | var exp = new LCM(new Number(12), new Number(16)); 18 | var expected = new NumberValue(48.0); 19 | 20 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 21 | } 22 | 23 | [Test] 24 | public void ExecuteTest2() 25 | { 26 | var exp = new LCM(new IExpression[] { new Number(4), new Number(16), new Number(8) }); 27 | var expected = new NumberValue(16.0); 28 | 29 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 30 | } 31 | 32 | [Test] 33 | public void ExecuteNotSupportedTest() 34 | => TestNotSupported(new LCM(new IExpression[] { Bool.False, Bool.True })); 35 | 36 | [Test] 37 | public void CloneTest() 38 | { 39 | var exp = new LCM(Variable.X, Number.Zero); 40 | var clone = exp.Clone(); 41 | 42 | Assert.That(clone, Is.EqualTo(exp)); 43 | } 44 | 45 | [Test] 46 | public void CloneWithArgsTest() 47 | { 48 | var exp = new LCM(Variable.X, Number.Zero); 49 | var args = new IExpression[] { Variable.X, Variable.Y, }.ToImmutableArray(); 50 | var clone = exp.Clone(args); 51 | var expected = new LCM(args); 52 | 53 | Assert.That(clone, Is.EqualTo(expected)); 54 | } 55 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LbTest.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 LbTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest() 10 | { 11 | var exp = new Lb(Number.Two); 12 | var expected = new NumberValue(Math.Log(2, 2)); 13 | 14 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 15 | } 16 | 17 | [Test] 18 | public void ExecuteRationalTest() 19 | { 20 | var exp = new Lb(new Rational(new Number(2), new Number(3))); 21 | var expected = new NumberValue(-0.5849625007211563); 22 | 23 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 24 | } 25 | 26 | [Test] 27 | public void ExecuteTestException() 28 | => TestNotSupported(new Lb(Bool.False)); 29 | 30 | [Test] 31 | public void CloneTest() 32 | { 33 | var exp = new Lb(new Number(5)); 34 | var clone = exp.Clone(); 35 | 36 | Assert.That(clone, Is.EqualTo(exp)); 37 | } 38 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LgTest.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; 7 | 8 | public class LgTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Lg(Number.Two); 14 | var expected = new NumberValue(Math.Log10(2)); 15 | 16 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void ExecuteComplexTest() 21 | { 22 | var complex = new Complex(2, 3); 23 | var exp = new Lg(new ComplexNumber(complex)); 24 | var expected = Complex.Log10(complex); 25 | 26 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 27 | } 28 | 29 | [Test] 30 | public void ExecuteRationalTest() 31 | { 32 | var exp = new Lg(new Rational(new Number(2), new Number(3))); 33 | var expected = new NumberValue(-0.17609125905568124); 34 | 35 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 36 | } 37 | 38 | [Test] 39 | public void ExecuteTestException() 40 | => TestNotSupported(new Lg(Bool.False)); 41 | 42 | [Test] 43 | public void CloneTest() 44 | { 45 | var exp = new Lg(Number.Zero); 46 | var clone = exp.Clone(); 47 | 48 | Assert.That(clone, Is.EqualTo(exp)); 49 | } 50 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LnTest.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; 7 | 8 | public class LnTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Ln(Number.Two); 14 | var expected = new NumberValue(Math.Log(2)); 15 | 16 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void ExecuteComplexTest() 21 | { 22 | var complex = new Complex(2, 3); 23 | var exp = new Ln(new ComplexNumber(complex)); 24 | 25 | Assert.That(exp.Execute(), Is.EqualTo(Complex.Log(complex))); 26 | } 27 | 28 | [Test] 29 | public void ExecuteRationalTest() 30 | { 31 | var exp = new Ln(new Rational(new Number(2), new Number(3))); 32 | var expected = new NumberValue(-0.4054651081081645); 33 | 34 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 35 | } 36 | 37 | [Test] 38 | public void ExecuteTestException() 39 | => TestNotSupported(new Ln(Bool.False)); 40 | 41 | [Test] 42 | public void CloneTest() 43 | { 44 | var exp = new Ln(new Number(5)); 45 | var clone = exp.Clone(); 46 | 47 | Assert.That(clone, Is.EqualTo(exp)); 48 | } 49 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LogTest.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; 7 | 8 | public class LogTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Log(Number.Two, new Number(10)); 14 | var expected = new NumberValue(Math.Log(10, 2)); 15 | 16 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void ExecuteComplexTest() 21 | { 22 | var complex = new Complex(2, 3); 23 | var exp = new Log(new Number(4), new ComplexNumber(complex)); 24 | 25 | Assert.That(exp.Execute(), Is.EqualTo(Complex.Log(complex, 4))); 26 | } 27 | 28 | [Test] 29 | public void ExecuteRationalTest() 30 | { 31 | var exp = new Log(new Number(3), new Rational(new Number(2), new Number(3))); 32 | var expected = new NumberValue(-0.3690702464285426); 33 | 34 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 35 | } 36 | 37 | [Test] 38 | public void ExecuteLeftResultIsNotSupported() 39 | => TestNotSupported(new Log(Bool.False, Bool.True)); 40 | 41 | [Test] 42 | public void ExecuteRightResultIsNotSupported() 43 | => TestNotSupported(new Log(new Number(10), Bool.True)); 44 | 45 | [Test] 46 | public void CloneTest() 47 | { 48 | var exp = new Log(Number.Zero, new Number(5)); 49 | var clone = exp.Clone(); 50 | 51 | Assert.That(clone, Is.EqualTo(exp)); 52 | } 53 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LogicalAndBitwise/EqualityTest.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.LogicalAndBitwise; 5 | 6 | public class EqualityTest 7 | { 8 | [Test] 9 | public void ExecuteTest1() 10 | { 11 | var eq = new Equality(Bool.True, Bool.True); 12 | 13 | Assert.That((bool) eq.Execute(), Is.True); 14 | } 15 | 16 | [Test] 17 | public void ExecuteTest2() 18 | { 19 | var eq = new Equality(Bool.True, Bool.False); 20 | 21 | Assert.That((bool) eq.Execute(), Is.False); 22 | } 23 | 24 | [Test] 25 | public void ExecuteResultIsNotSupported() 26 | { 27 | var eq = new Equality(Number.One, Number.Two); 28 | 29 | Assert.Throws(() => eq.Execute()); 30 | } 31 | 32 | [Test] 33 | public void CloneTest() 34 | { 35 | var exp = new Equality(Bool.True, Bool.False); 36 | var clone = exp.Clone(); 37 | 38 | Assert.That(clone, Is.EqualTo(exp)); 39 | } 40 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LogicalAndBitwise/ImplicationTest.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.LogicalAndBitwise; 5 | 6 | public class ImplicationTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest1() 10 | { 11 | var impl = new Implication(Bool.True, Bool.False); 12 | 13 | Assert.That((bool) impl.Execute(), Is.False); 14 | } 15 | 16 | [Test] 17 | public void ExecuteTest2() 18 | { 19 | var impl = new Implication(Bool.True, Bool.True); 20 | 21 | Assert.That((bool) impl.Execute(), Is.True); 22 | } 23 | 24 | [Test] 25 | public void ExecuteResultIsNotSupported() 26 | => TestNotSupported(new Implication(Number.One, Number.Two)); 27 | 28 | [Test] 29 | public void CloneTest() 30 | { 31 | var exp = new Implication(Bool.True, Bool.False); 32 | var clone = exp.Clone(); 33 | 34 | Assert.That(clone, Is.EqualTo(exp)); 35 | } 36 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LogicalAndBitwise/NAndTest.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.LogicalAndBitwise; 5 | 6 | public class NAndTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest1() 10 | { 11 | var nand = new NAnd(Bool.True, Bool.True); 12 | 13 | Assert.That((bool) nand.Execute(), Is.False); 14 | } 15 | 16 | [Test] 17 | public void ExecuteTest2() 18 | { 19 | var nand = new NAnd(Bool.False, Bool.True); 20 | 21 | Assert.That((bool) nand.Execute(), Is.True); 22 | } 23 | 24 | [Test] 25 | public void ExecuteResultIsNotSupported() 26 | => TestNotSupported(new NAnd(Number.One, Number.Two)); 27 | 28 | [Test] 29 | public void CloneTest() 30 | { 31 | var exp = new NAnd(Bool.True, Bool.False); 32 | var clone = exp.Clone(); 33 | 34 | Assert.That(clone, Is.EqualTo(exp)); 35 | } 36 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LogicalAndBitwise/NOrTest.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.LogicalAndBitwise; 5 | 6 | public class NOrTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest1() 10 | { 11 | var nor = new NOr(Bool.False, Bool.True); 12 | 13 | Assert.That((bool) nor.Execute(), Is.False); 14 | } 15 | 16 | [Test] 17 | public void ExecuteTest2() 18 | { 19 | var nor = new NOr(Bool.False, Bool.False); 20 | 21 | Assert.That((bool) nor.Execute(), Is.True); 22 | } 23 | 24 | [Test] 25 | public void ExecuteResultIsNotSupported() 26 | => TestNotSupported(new NOr(Number.One, Number.Two)); 27 | 28 | [Test] 29 | public void CloneTest() 30 | { 31 | var exp = new NOr(Bool.True, Bool.False); 32 | var clone = exp.Clone(); 33 | 34 | Assert.That(clone, Is.EqualTo(exp)); 35 | } 36 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/LogicalAndBitwise/NotTest.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.LogicalAndBitwise; 5 | 6 | public class NotTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest1() 10 | { 11 | var exp = new Not(Number.Two); 12 | var expected = new NumberValue(-3.0); 13 | 14 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 15 | } 16 | 17 | [Test] 18 | public void ExecuteTest3() 19 | { 20 | var exp = new Not(Bool.True); 21 | 22 | Assert.That((bool) exp.Execute(), Is.False); 23 | } 24 | 25 | [Test] 26 | public void ExecuteTestValueIsNotInt() 27 | { 28 | var exp = new Not(new Number(1.5)); 29 | 30 | Assert.Throws(() => exp.Execute()); 31 | } 32 | 33 | [Test] 34 | public void ExecuteResultIsNotSupported() 35 | => TestNotSupported(new Not(new ComplexNumber(1))); 36 | 37 | [Test] 38 | public void CloneTest() 39 | { 40 | var exp = new Not(Bool.False); 41 | var clone = exp.Clone(); 42 | 43 | Assert.That(clone, Is.EqualTo(exp)); 44 | } 45 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/ModTest.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 ModTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest1() 10 | { 11 | var exp = new Mod(new Number(25), new Number(7)); 12 | var result = exp.Execute(); 13 | var expected = new NumberValue(4.0); 14 | 15 | Assert.That(result, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void ExecuteTest2() 20 | { 21 | var exp = new Mod(new Number(25), new Number(5)); 22 | var result = exp.Execute(); 23 | 24 | Assert.That(result, Is.EqualTo(new NumberValue(0.0))); 25 | } 26 | 27 | [Test] 28 | public void ExecuteTest3() 29 | { 30 | var exp = new Mod(Number.Zero, new Number(5)); 31 | var result = exp.Execute(); 32 | 33 | Assert.That(result, Is.EqualTo(new NumberValue(0.0))); 34 | } 35 | 36 | [Test] 37 | public void ExecuteTest4() 38 | { 39 | var exp = new Mod(new Number(5), Number.Zero); 40 | var result = (NumberValue)exp.Execute(); 41 | 42 | Assert.That(result.IsNaN); 43 | } 44 | 45 | [Test] 46 | public void ExecuteResultIsNotSupported() 47 | => TestNotSupported(new Mod(Bool.True, Bool.False)); 48 | 49 | [Test] 50 | public void CloneTest() 51 | { 52 | var exp = new Mod(new Number(5), Number.Zero); 53 | var clone = exp.Clone(); 54 | 55 | Assert.That(clone, Is.EqualTo(exp)); 56 | } 57 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Parameters/ParameterValueTest.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.Parameters; 5 | 6 | public class ParameterValueTest 7 | { 8 | [Test] 9 | public void EqualNullTest() 10 | { 11 | var value = new ParameterValue(1); 12 | 13 | Assert.That(value.Equals(null as object), Is.False); 14 | } 15 | 16 | [Test] 17 | public void EqualObjectTest() 18 | { 19 | var x = new ParameterValue(1); 20 | var y = new ParameterValue(1) as object; 21 | 22 | Assert.That(x.Equals(y), Is.True); 23 | } 24 | 25 | [Test] 26 | public void EqualOperatorTest() 27 | { 28 | var x = new ParameterValue(1); 29 | var y = new ParameterValue(1); 30 | 31 | Assert.That(x == y, Is.True); 32 | } 33 | 34 | [Test] 35 | public void NotEqualOperatorTest() 36 | { 37 | var x = new ParameterValue(1); 38 | var y = new ParameterValue(2); 39 | 40 | Assert.That(x != y, Is.True); 41 | } 42 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Programming/LeftShiftTest.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.Programming; 5 | 6 | public class LeftShiftTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest() 10 | { 11 | var exp = new LeftShift(Number.One, new Number(10)); 12 | var actual = exp.Execute(); 13 | var expected = new NumberValue(1024.0); 14 | 15 | Assert.That(actual, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void ExecuteDoubleLeftTest() 20 | { 21 | var exp = new LeftShift(new Number(1.5), new Number(10)); 22 | 23 | Assert.Throws(() => exp.Execute()); 24 | } 25 | 26 | [Test] 27 | public void ExecuteDoubleRightTest() 28 | { 29 | var exp = new LeftShift(Number.One, new Number(10.1)); 30 | 31 | Assert.Throws(() => exp.Execute()); 32 | } 33 | 34 | [Test] 35 | public void ExecuteBoolTest() 36 | => TestNotSupported(new LeftShift(Bool.False, Bool.True)); 37 | 38 | [Test] 39 | public void CloneTest() 40 | { 41 | var exp = new LeftShift(Number.One, new Number(10)); 42 | var clone = exp.Clone(); 43 | 44 | Assert.That(clone, Is.EqualTo(exp)); 45 | } 46 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Programming/RightShiftTest.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.Programming; 5 | 6 | public class RightShiftTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest() 10 | { 11 | var exp = new RightShift(new Number(512), new Number(9)); 12 | var actual = exp.Execute(); 13 | var expected = new NumberValue(1.0); 14 | 15 | Assert.That(actual, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void ExecuteDoubleLeftTest() 20 | { 21 | var exp = new RightShift(new Number(1.5), new Number(10)); 22 | 23 | Assert.Throws(() => exp.Execute()); 24 | } 25 | 26 | [Test] 27 | public void ExecuteDoubleRightTest() 28 | { 29 | var exp = new RightShift(Number.One, new Number(10.1)); 30 | 31 | Assert.Throws(() => exp.Execute()); 32 | } 33 | 34 | [Test] 35 | public void ExecuteBoolTest() 36 | => TestNotSupported(new RightShift(Bool.False, Bool.True)); 37 | 38 | [Test] 39 | public void CloneTest() 40 | { 41 | var exp = new RightShift(Number.One, new Number(10)); 42 | var clone = exp.Clone(); 43 | 44 | Assert.That(clone, Is.EqualTo(exp)); 45 | } 46 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/RationalTest.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 RationalTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteTest() 10 | { 11 | var exp = new Rational(Number.One, Number.Two); 12 | var expected = new RationalValue(1, 2); 13 | var actual = exp.Execute(); 14 | 15 | Assert.That(actual, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void ExecuteLeftExceptionTest() 20 | => TestNotSupported(new Rational(Bool.True, Number.Two)); 21 | 22 | [Test] 23 | public void ExecuteRightExceptionTest() 24 | => TestNotSupported(new Rational(Number.One, Bool.True)); 25 | 26 | [Test] 27 | public void CloneTest() 28 | { 29 | var exp = new Rational(Number.One, Number.Two); 30 | var clone = exp.Clone(); 31 | 32 | Assert.That(clone, Is.EqualTo(exp)); 33 | } 34 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/SimplifyTest.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 NSubstitute; 5 | 6 | namespace xFunc.Tests.Expressions; 7 | 8 | public class SimplifyTest 9 | { 10 | [Test] 11 | public void SimplifierNull() 12 | => Assert.Throws(() => new Simplify(null, null)); 13 | 14 | [Test] 15 | public void ExecuteTest() 16 | { 17 | var simplifier = Substitute.For(); 18 | simplifier.Analyze(Arg.Any()).Returns(info => info.Arg()); 19 | 20 | var lambda = new Sin(Variable.X).ToLambda(Variable.X.Name); 21 | var exp = new Simplify(simplifier, lambda.AsExpression()); 22 | 23 | Assert.That(exp.Execute(), Is.EqualTo(lambda)); 24 | } 25 | 26 | [Test] 27 | public void ExecuteNonLambdaTest() 28 | { 29 | var simplifier = Substitute.For(); 30 | var simplify = new Simplify(simplifier, Number.One); 31 | 32 | Assert.Throws(() => simplify.Execute()); 33 | } 34 | 35 | [Test] 36 | public void ExecuteNullTest() 37 | { 38 | Assert.Throws(() => new Simplify(null, new Sin(Variable.X)).Execute()); 39 | } 40 | 41 | [Test] 42 | public void CloneTest() 43 | { 44 | var exp = new Simplify(new Simplifier(), new Sin(Variable.X)); 45 | var clone = exp.Clone(); 46 | 47 | Assert.That(clone, Is.EqualTo(exp)); 48 | } 49 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/AvgTest.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.Statistical; 5 | 6 | public class AvgTest 7 | { 8 | [Test] 9 | public void OneNumberTest() 10 | { 11 | var exp = new Avg(new[] { Number.Two }); 12 | var result = exp.Execute(); 13 | 14 | Assert.That(result, Is.EqualTo(new NumberValue(2.0))); 15 | } 16 | 17 | [Test] 18 | public void TwoNumberTest() 19 | { 20 | var exp = new Avg(new[] { Number.Two, new Number(4) }); 21 | var result = exp.Execute(); 22 | 23 | Assert.That(result, Is.EqualTo(new NumberValue(3.0))); 24 | } 25 | 26 | [Test] 27 | public void ThreeNumberTest() 28 | { 29 | var exp = new Avg(new[] { new Number(9), Number.Two, new Number(4) }); 30 | var result = exp.Execute(); 31 | 32 | Assert.That(result, Is.EqualTo(new NumberValue(5.0))); 33 | } 34 | 35 | [Test] 36 | public void VectorTest() 37 | { 38 | var exp = new Avg(new[] { new Vector(new IExpression[] { Number.One, Number.Two, new Number(3) }) }); 39 | var result = exp.Execute(); 40 | 41 | Assert.That(result, Is.EqualTo(new NumberValue(2.0))); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/CountTest.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.Statistical; 5 | 6 | public class CountTest 7 | { 8 | [Test] 9 | public void OneNumberTest() 10 | { 11 | var exp = new Count(new[] { Number.Two }); 12 | var result = exp.Execute(); 13 | 14 | Assert.That(result, Is.EqualTo(new NumberValue(1.0))); 15 | } 16 | 17 | [Test] 18 | public void TwoNumberTest() 19 | { 20 | var exp = new Count(new[] { Number.Two, new Number(4) }); 21 | var result = exp.Execute(); 22 | 23 | Assert.That(result, Is.EqualTo(new NumberValue(2.0))); 24 | } 25 | 26 | [Test] 27 | public void VectorTest() 28 | { 29 | var exp = new Count(new[] { new Vector(new IExpression[] { Number.One, Number.Two, new Number(3) }) }); 30 | var result = exp.Execute(); 31 | 32 | Assert.That(result, Is.EqualTo(new NumberValue(3.0))); 33 | } 34 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/MaxTest.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.Statistical; 5 | 6 | public class MaxTest 7 | { 8 | [Test] 9 | public void OneNumberTest() 10 | { 11 | var exp = new Max(new[] { Number.Two }); 12 | var result = exp.Execute(); 13 | var expected = new NumberValue(2.0); 14 | 15 | Assert.That(result, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void TwoNumberTest() 20 | { 21 | var exp = new Max(new[] { Number.Two, new Number(4) }); 22 | var result = exp.Execute(); 23 | var expected = new NumberValue(4.0); 24 | 25 | Assert.That(result, Is.EqualTo(expected)); 26 | } 27 | 28 | [Test] 29 | public void ThreeNumberTest() 30 | { 31 | var exp = new Max(new[] { new Number(9), Number.Two, new Number(4) }); 32 | var result = exp.Execute(); 33 | var expected = new NumberValue(9.0); 34 | 35 | Assert.That(result, Is.EqualTo(expected)); 36 | } 37 | 38 | [Test] 39 | public void VectorTest() 40 | { 41 | var exp = new Max(new[] { new Vector(new IExpression[] { Number.One, Number.Two, new Number(3) }) }); 42 | var result = exp.Execute(); 43 | var expected = new NumberValue(3.0); 44 | 45 | Assert.That(result, Is.EqualTo(expected)); 46 | } 47 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/MinTest.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.Statistical; 5 | 6 | public class MinTest 7 | { 8 | [Test] 9 | public void OneNumberTest() 10 | { 11 | var exp = new Min(new[] { Number.Two }); 12 | var result = exp.Execute(); 13 | 14 | Assert.That(result, Is.EqualTo(new NumberValue(2.0))); 15 | } 16 | 17 | [Test] 18 | public void TwoNumberTest() 19 | { 20 | var exp = new Min(new[] { Number.Two, new Number(4) }); 21 | var result = exp.Execute(); 22 | 23 | Assert.That(result, Is.EqualTo(new NumberValue(2.0))); 24 | } 25 | 26 | [Test] 27 | public void ThreeNumberTest() 28 | { 29 | var exp = new Min(new[] { new Number(9), Number.Two, new Number(4) }); 30 | var result = exp.Execute(); 31 | 32 | Assert.That(result, Is.EqualTo(new NumberValue(2.0))); 33 | } 34 | 35 | [Test] 36 | public void VectorTest() 37 | { 38 | var exp = new Min(new[] { new Vector(new IExpression[] { Number.One, Number.Two, new Number(3) }) }); 39 | var result = exp.Execute(); 40 | 41 | Assert.That(result, Is.EqualTo(new NumberValue(1.0))); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/ProductTest.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.Statistical; 5 | 6 | public class ProductTest 7 | { 8 | [Test] 9 | public void TwoNumbersTest() 10 | { 11 | var sum = new Product(new[] { new Number(3), Number.Two }); 12 | var actual = sum.Execute(); 13 | var expected = new NumberValue(6.0); 14 | 15 | Assert.That(actual, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void OneNumberTest() 20 | { 21 | var sum = new Product(new[] { Number.Two }); 22 | var actual = sum.Execute(); 23 | var expected = new NumberValue(2.0); 24 | 25 | Assert.That(actual, Is.EqualTo(expected)); 26 | } 27 | 28 | [Test] 29 | public void VectorTest() 30 | { 31 | var sum = new Product(new[] { new Vector(new IExpression[] { new Number(4), Number.Two }) }); 32 | var actual = sum.Execute(); 33 | var expected = new NumberValue(8.0); 34 | 35 | Assert.That(actual, Is.EqualTo(expected)); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/StdevTest.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 | using Vector = xFunc.Maths.Expressions.Matrices.Vector; 6 | 7 | namespace xFunc.Tests.Expressions.Statistical; 8 | 9 | public class StdevTest 10 | { 11 | [Test] 12 | public void OneNumberTest() 13 | { 14 | var exp = new Stdev(new[] { new Number(4) }); 15 | var result = (Complex)exp.Execute(); 16 | 17 | Assert.That(Complex.IsNaN(result)); 18 | } 19 | 20 | [Test] 21 | public void TwoNumberTest() 22 | { 23 | var exp = new Stdev(new[] { new Number(4), new Number(9) }); 24 | var result = (NumberValue)exp.Execute(); 25 | var expected = new NumberValue(3.53553390593274); 26 | 27 | Assert.That(result, Is.EqualTo(expected)); 28 | } 29 | 30 | [Test] 31 | public void ThreeNumberTest() 32 | { 33 | var exp = new Stdev(new[] { new Number(9), Number.Two, new Number(4) }); 34 | var result = (NumberValue)exp.Execute(); 35 | var expected = new NumberValue(3.60555127546399); 36 | 37 | Assert.That(result, Is.EqualTo(expected)); 38 | } 39 | 40 | [Test] 41 | public void VectorTest() 42 | { 43 | var exp = new Stdev(new[] { new Vector(new IExpression[] { Number.Two, new Number(4), new Number(9) }) }); 44 | var result = (NumberValue)exp.Execute(); 45 | var expected = new NumberValue(3.60555127546399); 46 | 47 | Assert.That(result, Is.EqualTo(expected)); 48 | } 49 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/StdevpTest.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.Statistical; 5 | 6 | public class StdevpTest 7 | { 8 | [Test] 9 | public void OneNumberTest() 10 | { 11 | var exp = new Stdevp(new[] { new Number(4) }); 12 | var result = (NumberValue)exp.Execute(); 13 | var expected = new NumberValue(0.0); 14 | 15 | Assert.That(result, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void TwoNumberTest() 20 | { 21 | var exp = new Stdevp(new[] { new Number(4), new Number(9) }); 22 | var result = (NumberValue)exp.Execute(); 23 | var expected = new NumberValue(2.5); 24 | 25 | Assert.That(result, Is.EqualTo(expected)); 26 | } 27 | 28 | [Test] 29 | public void ThreeNumberTest() 30 | { 31 | var exp = new Stdevp(new[] { new Number(9), Number.Two, new Number(4) }); 32 | var result = (NumberValue)exp.Execute(); 33 | var expected = new NumberValue(2.94392028877595); 34 | 35 | Assert.That(result, Is.EqualTo(expected)); 36 | } 37 | 38 | [Test] 39 | public void VectorTest() 40 | { 41 | var exp = new Stdevp(new[] { new Vector(new[] { Number.Two, new Number(4), new Number(9) }) }); 42 | var result = (NumberValue)exp.Execute(); 43 | var expected = new NumberValue(2.94392028877595); 44 | 45 | Assert.That(result, Is.EqualTo(expected)); 46 | } 47 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/SumTest.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.Statistical; 5 | 6 | public class SumTest 7 | { 8 | [Test] 9 | public void TwoNumbersTest() 10 | { 11 | var sum = new Sum(new[] { Number.One, Number.Two }); 12 | var actual = sum.Execute(); 13 | var expected = new NumberValue(3.0); 14 | 15 | Assert.That(actual, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void OneNumberTest() 20 | { 21 | var sum = new Sum(new[] { Number.Two }); 22 | var actual = sum.Execute(); 23 | var expected = new NumberValue(2.0); 24 | 25 | Assert.That(actual, Is.EqualTo(expected)); 26 | } 27 | 28 | [Test] 29 | public void VectorTest() 30 | { 31 | var sum = new Sum(new[] { new Vector(new IExpression[] { Number.One, Number.Two }) }); 32 | var actual = sum.Execute(); 33 | var expected = new NumberValue(3.0); 34 | 35 | Assert.That(actual, Is.EqualTo(expected)); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/VarTest.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.Statistical; 5 | 6 | public class VarTest 7 | { 8 | [Test] 9 | public void OneNumberTest() 10 | { 11 | var exp = new Var(new[] { new Number(4) }); 12 | var result = (NumberValue)exp.Execute(); 13 | 14 | Assert.That(result.IsNaN); 15 | } 16 | 17 | [Test] 18 | public void TwoNumberTest() 19 | { 20 | var exp = new Var(new[] { new Number(4), new Number(9) }); 21 | var result = exp.Execute(); 22 | var expected = new NumberValue(12.5); 23 | 24 | Assert.That(result, Is.EqualTo(expected)); 25 | } 26 | 27 | [Test] 28 | public void ThreeNumberTest() 29 | { 30 | var exp = new Var(new[] { new Number(9), Number.Two, new Number(4) }); 31 | var result = exp.Execute(); 32 | var expected = new NumberValue(13.0); 33 | 34 | Assert.That(result, Is.EqualTo(expected)); 35 | } 36 | 37 | [Test] 38 | public void VectorTest() 39 | { 40 | var exp = new Var(new[] { new Vector(new IExpression[] { Number.Two, new Number(4), new Number(9) }) }); 41 | var result = exp.Execute(); 42 | var expected = new NumberValue(13.0); 43 | 44 | Assert.That(result, Is.EqualTo(expected)); 45 | } 46 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Statistical/VarpTest.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.Statistical; 5 | 6 | public class VarpTest 7 | { 8 | [Test] 9 | public void OneNumberTest() 10 | { 11 | var exp = new Varp(new[] { new Number(4) }); 12 | var result = exp.Execute(); 13 | 14 | Assert.That(result, Is.EqualTo(new NumberValue(0.0))); 15 | } 16 | 17 | [Test] 18 | public void TwoNumberTest() 19 | { 20 | var exp = new Varp(new[] { new Number(4), new Number(9) }); 21 | var result = (NumberValue)exp.Execute(); 22 | var expected = new NumberValue(6.25); 23 | 24 | Assert.That(result, Is.EqualTo(expected)); 25 | } 26 | 27 | [Test] 28 | public void ThreeNumberTest() 29 | { 30 | var exp = new Varp(new[] { new Number(9), Number.Two, new Number(4) }); 31 | var result = (NumberValue)exp.Execute(); 32 | var expected = new NumberValue(8.66666666666667); 33 | 34 | Assert.That(result, Is.EqualTo(expected)); 35 | } 36 | 37 | [Test] 38 | public void VectorTest() 39 | { 40 | var exp = new Varp(new[] { new Vector(new IExpression[] { Number.Two, new Number(4), new Number(9) }) }); 41 | var result = (NumberValue)exp.Execute(); 42 | var expected = new NumberValue(8.66666666666667); 43 | 44 | Assert.That(result, Is.EqualTo(expected)); 45 | } 46 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/ToHexTest.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 ToHexTest : BaseExpressionTests 7 | { 8 | [Test] 9 | [TestCase(0x7, "0x07")] 10 | [TestCase(0x7FF, "0x07FF")] 11 | [TestCase(0x7FFFF, "0x07FFFF")] 12 | [TestCase(0x7FFFFFF, "0x07FFFFFF")] 13 | public void ExecuteNumberTest(double number, string result) 14 | { 15 | var exp = new ToHex(new Number(number)); 16 | 17 | Assert.That(exp.Execute(), Is.EqualTo(result)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteNumberExceptionTest() 22 | { 23 | var exp = new ToHex(new Number(2.5)); 24 | 25 | Assert.Throws(() => exp.Execute()); 26 | } 27 | 28 | [Test] 29 | public void ExecuteLongMaxNumberTest() 30 | { 31 | var exp = new ToHex(new Number(int.MaxValue)); 32 | 33 | Assert.That(exp.Execute(), Is.EqualTo("0x7FFFFFFF")); 34 | } 35 | 36 | [Test] 37 | public void ExecuteNegativeNumberTest() 38 | { 39 | var exp = new ToHex(new Number(-2)); 40 | 41 | Assert.That(exp.Execute(), Is.EqualTo("0xFFFFFFFE")); 42 | } 43 | 44 | [Test] 45 | public void ExecuteBoolTest() 46 | => TestNotSupported(new ToHex(Bool.False)); 47 | 48 | [Test] 49 | public void CloseTest() 50 | { 51 | var exp = new ToHex(new Number(10)); 52 | var clone = exp.Clone(); 53 | 54 | Assert.That(clone, Is.EqualTo(exp)); 55 | } 56 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/ToOctTest.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 ToOctTest : BaseExpressionTests 7 | { 8 | [Test] 9 | public void ExecuteNumberTest() 10 | { 11 | var exp = new ToOct(Number.Two); 12 | 13 | Assert.That(exp.Execute(), Is.EqualTo("02")); 14 | } 15 | 16 | [Test] 17 | public void ExecuteNumberExceptionTest() 18 | { 19 | var exp = new ToOct(new Number(2.5)); 20 | 21 | Assert.Throws(() => exp.Execute()); 22 | } 23 | 24 | [Test] 25 | public void ExecuteLongMaxNumberTest() 26 | { 27 | var exp = new ToOct(new Number(int.MaxValue)); 28 | 29 | Assert.That(exp.Execute(), Is.EqualTo("017777777777")); 30 | } 31 | 32 | [Test] 33 | public void ExecuteNegativeNumberTest() 34 | { 35 | var exp = new ToOct(new Number(-2)); 36 | 37 | Assert.That(exp.Execute(), Is.EqualTo("037777777776")); 38 | } 39 | 40 | [Test] 41 | public void ExecuteBoolTest() 42 | => TestNotSupported(new ToOct(Bool.False)); 43 | 44 | [Test] 45 | public void CloseTest() 46 | { 47 | var exp = new ToOct(new Number(10)); 48 | var clone = exp.Clone(); 49 | 50 | Assert.That(clone, Is.EqualTo(exp)); 51 | } 52 | } -------------------------------------------------------------------------------- /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/Expressions/Trigonometric/ArccosTest.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.Trigonometric; 7 | 8 | public class ArccosTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arccos(Number.One); 14 | var expected = AngleValue.Radian(0); 15 | 16 | Assert.That(exp.Execute(), Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void ExecuteComplexNumberTest() 21 | { 22 | var complex = new Complex(3, 2); 23 | var exp = new Arccos(new ComplexNumber(complex)); 24 | var result = (Complex)exp.Execute(); 25 | 26 | Assert.That(result.Real, Is.EqualTo(0.60613782238729386).Within(15)); 27 | Assert.That(result.Imaginary, Is.EqualTo(-1.9686379257930964).Within(15)); 28 | } 29 | 30 | [Test] 31 | public void ExecuteTestException() 32 | => TestNotSupported(new Arccos(Bool.False)); 33 | 34 | [Test] 35 | public void CloneTest() 36 | { 37 | var exp = new Arccos(Number.One); 38 | var clone = exp.Clone(); 39 | 40 | Assert.That(clone, Is.EqualTo(exp)); 41 | } 42 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Trigonometric/ArccotTest.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.Trigonometric; 7 | 8 | public class ArccotTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arccot(Number.One); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(0.7853981633974483); 16 | Assert.That(result, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void ExecuteComplexNumberTest() 21 | { 22 | var complex = new Complex(3, 2); 23 | var exp = new Arccot(new ComplexNumber(complex)); 24 | var result = (Complex)exp.Execute(); 25 | 26 | Assert.That(result.Real, Is.EqualTo(0.23182380450040308).Within(15)); 27 | Assert.That(result.Imaginary, Is.EqualTo(-0.14694666622552988).Within(15)); 28 | } 29 | 30 | [Test] 31 | public void ExecuteTestException() 32 | => TestNotSupported(new Arccot(Bool.False)); 33 | 34 | [Test] 35 | public void CloneTest() 36 | { 37 | var exp = new Arccot(Number.One); 38 | var clone = exp.Clone(); 39 | 40 | Assert.That(clone, Is.EqualTo(exp)); 41 | } 42 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Trigonometric/ArccscTest.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.Trigonometric; 7 | 8 | public class ArccscTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arccsc(Number.One); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(1.5707963267948966); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arccsc(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(0.22996290237720782).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(-0.15735549884498545).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arccsc(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arccsc(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Trigonometric/ArcsecTest.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.Trigonometric; 7 | 8 | public class ArcsecTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arcsec(Number.One); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(0); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arcsec(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(1.3408334244176887).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(0.15735549884498545).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arcsec(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arcsec(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Trigonometric/ArcsinTest.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.Trigonometric; 7 | 8 | public class ArcsinTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arcsin(Number.One); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(1.5707963267948966); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arcsin(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(0.96465850440760248).Within(14)); 28 | Assert.That(result.Imaginary, Is.EqualTo(1.9686379257930975).Within(14)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arcsin(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arcsin(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Trigonometric/ArctanTest.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.Trigonometric; 7 | 8 | public class ArctanTest : BaseExpressionTests 9 | { 10 | [Test] 11 | public void ExecuteNumberTest() 12 | { 13 | var exp = new Arctan(Number.One); 14 | var result = exp.Execute(); 15 | var expected = AngleValue.Radian(0.7853981633974483); 16 | 17 | Assert.That(result, Is.EqualTo(expected)); 18 | } 19 | 20 | [Test] 21 | public void ExecuteComplexNumberTest() 22 | { 23 | var complex = new Complex(3, 2); 24 | var exp = new Arctan(new ComplexNumber(complex)); 25 | var result = (Complex)exp.Execute(); 26 | 27 | Assert.That(result.Real, Is.EqualTo(1.3389725222944935).Within(15)); 28 | Assert.That(result.Imaginary, Is.EqualTo(0.14694666622552977).Within(15)); 29 | } 30 | 31 | [Test] 32 | public void ExecuteTestException() 33 | => TestNotSupported(new Arctan(Bool.False)); 34 | 35 | [Test] 36 | public void CloneTest() 37 | { 38 | var exp = new Arctan(Number.One); 39 | var clone = exp.Clone(); 40 | 41 | Assert.That(clone, Is.EqualTo(exp)); 42 | } 43 | } -------------------------------------------------------------------------------- /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/Expressions/Units/AngleUnits/ToDegreeTest.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.Units.AngleUnits; 5 | 6 | public class ToDegreeTest 7 | { 8 | [Test] 9 | public void ExecuteNumberTest() 10 | { 11 | var exp = new ToDegree(new Number(10)); 12 | var actual = exp.Execute(); 13 | var expected = AngleValue.Degree(10); 14 | 15 | Assert.That(actual, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void ExecuteAngleTest() 20 | { 21 | var exp = new ToDegree(AngleValue.Degree(10).AsExpression()); 22 | var actual = exp.Execute(); 23 | var expected = AngleValue.Degree(10); 24 | 25 | Assert.That(actual, Is.EqualTo(expected)); 26 | } 27 | 28 | [Test] 29 | public void ExecuteBoolTest() 30 | { 31 | Assert.Throws(() => new ToDegree(Bool.False).Execute()); 32 | } 33 | 34 | [Test] 35 | public void NullAnalyzerTest1() 36 | { 37 | var exp = new ToDegree(new Number(10)); 38 | 39 | Assert.Throws(() => exp.Analyze(null)); 40 | } 41 | 42 | [Test] 43 | public void NullAnalyzerTest2() 44 | { 45 | var exp = new ToDegree(new Number(10)); 46 | 47 | Assert.Throws(() => exp.Analyze(null, null)); 48 | } 49 | 50 | [Test] 51 | public void CloneTest() 52 | { 53 | var exp = new ToDegree(new Number(10)); 54 | var clone = exp.Clone(); 55 | 56 | Assert.That(clone, Is.EqualTo(exp)); 57 | } 58 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Units/AngleUnits/ToRadianTest.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.Units.AngleUnits; 5 | 6 | public class ToRadianTest 7 | { 8 | [Test] 9 | public void ExecuteNumberTest() 10 | { 11 | var exp = new ToRadian(new Number(10)); 12 | var actual = exp.Execute(); 13 | var expected = AngleValue.Radian(10); 14 | 15 | Assert.That(actual, Is.EqualTo(expected)); 16 | } 17 | 18 | [Test] 19 | public void ExecuteAngleTest() 20 | { 21 | var exp = new ToRadian(AngleValue.Radian(10).AsExpression()); 22 | var actual = exp.Execute(); 23 | var expected = AngleValue.Radian(10); 24 | 25 | Assert.That(actual, Is.EqualTo(expected)); 26 | } 27 | 28 | [Test] 29 | public void ExecuteBoolTest() 30 | { 31 | Assert.Throws(() => new ToRadian(Bool.False).Execute()); 32 | } 33 | 34 | [Test] 35 | public void NullAnalyzerTest1() 36 | { 37 | var exp = new ToRadian(new Number(10)); 38 | 39 | Assert.Throws(() => exp.Analyze(null)); 40 | } 41 | 42 | [Test] 43 | public void NullAnalyzerTest2() 44 | { 45 | var exp = new ToRadian(new Number(10)); 46 | 47 | Assert.Throws(() => exp.Analyze(null, null)); 48 | } 49 | 50 | [Test] 51 | public void CloneTest() 52 | { 53 | var exp = new ToRadian(new Number(10)); 54 | var clone = exp.Clone(); 55 | 56 | Assert.That(clone, Is.EqualTo(exp)); 57 | } 58 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Units/Converters/ConverterTests.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.Units.Converters; 5 | 6 | public class ConverterTests 7 | { 8 | [Test] 9 | [TestCase(null, null)] 10 | [TestCase(1, null)] 11 | public void ConvertNull(object value, string unit) 12 | { 13 | var converter = new Converter(); 14 | 15 | Assert.Throws(() => converter.Convert(value, unit)); 16 | } 17 | 18 | public static IEnumerable GetConvertTestsData() 19 | { 20 | var angle = AngleValue.Radian(90); 21 | 22 | yield return [angle, "rad", angle.ToRadian()]; 23 | 24 | var power = PowerValue.Watt(10); 25 | 26 | yield return [power, "w", power.ToWatt()]; 27 | 28 | var temperature = TemperatureValue.Celsius(10); 29 | 30 | yield return [temperature, "k", temperature.ToKelvin()]; 31 | } 32 | 33 | [Test] 34 | [TestCaseSource(nameof(GetConvertTestsData))] 35 | public void ConvertTests(object value, string unit, object expected) 36 | { 37 | var converter = new Converter(); 38 | var result = converter.Convert(value, unit); 39 | 40 | Assert.That(result, Is.EqualTo(expected)); 41 | } 42 | 43 | [Test] 44 | public void ConvertUnsupportedUnit() 45 | { 46 | var converter = new Converter(); 47 | 48 | Assert.Throws(() => converter.Convert(new NumberValue(10), "xxx")); 49 | } 50 | } -------------------------------------------------------------------------------- /xFunc.Tests/Expressions/Units/PowerUnits/PowerUnitTests.cs: -------------------------------------------------------------------------------- 1 | namespace xFunc.Tests.Expressions.Units.PowerUnits; 2 | 3 | public class PowerUnitTests 4 | { 5 | [Test] 6 | public void EqualTest() 7 | { 8 | var left = PowerUnit.Watt; 9 | var right = PowerUnit.Watt; 10 | 11 | Assert.That(left.Equals(right), Is.True); 12 | } 13 | 14 | [Test] 15 | public void NotEqualTest() 16 | { 17 | var left = PowerUnit.Watt; 18 | var right = PowerUnit.Kilowatt; 19 | 20 | Assert.That(left.Equals(right), Is.False); 21 | } 22 | 23 | [Test] 24 | public void ObjectEqualTest() 25 | { 26 | var left = PowerUnit.Watt; 27 | var right = PowerUnit.Watt as object; 28 | 29 | Assert.That(left.Equals(right), Is.True); 30 | } 31 | 32 | [Test] 33 | public void ObjectNotEqualTest() 34 | { 35 | var left = PowerUnit.Watt; 36 | var right = new object(); 37 | 38 | Assert.That(left.Equals(right), Is.False); 39 | } 40 | 41 | [Test] 42 | public void EqualOperatorTest() 43 | { 44 | var left = PowerUnit.Watt; 45 | var right = PowerUnit.Watt; 46 | 47 | Assert.That(left == right, Is.True); 48 | } 49 | 50 | [Test] 51 | public void NotEqualOperatorTest() 52 | { 53 | var left = PowerUnit.Watt; 54 | var right = PowerUnit.Kilowatt; 55 | 56 | Assert.That(left != right, Is.True); 57 | } 58 | 59 | [Test] 60 | [TestCase(null)] 61 | [TestCase("")] 62 | [TestCase(" ")] 63 | public void FromNameEmptyString(string name) 64 | => Assert.Throws(() => PowerUnit.FromName(name, out _)); 65 | } -------------------------------------------------------------------------------- /xFunc.Tests/ParserTests/AreaUnitTests.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 AreaUnitTests : BaseParserTests 7 | { 8 | [Test] 9 | public void ParseSquareMeter() 10 | => ParseTest("1 'm^2'", AreaValue.Meter(1).AsExpression()); 11 | 12 | [Test] 13 | public void ParseSquareMillimeter() 14 | => ParseTest("1 'mm^2'", AreaValue.Millimeter(1).AsExpression()); 15 | 16 | [Test] 17 | public void ParseSquareCentimeter() 18 | => ParseTest("1 'cm^2'", AreaValue.Centimeter(1).AsExpression()); 19 | 20 | [Test] 21 | public void ParseSquareKilometer() 22 | => ParseTest("1 'km^2'", AreaValue.Kilometer(1).AsExpression()); 23 | 24 | [Test] 25 | public void ParseSquareInch() 26 | => ParseTest("1 'in^2'", AreaValue.Inch(1).AsExpression()); 27 | 28 | [Test] 29 | public void ParseSquareFoot() 30 | => ParseTest("1 'ft^2'", AreaValue.Foot(1).AsExpression()); 31 | 32 | [Test] 33 | public void ParseSquareYard() 34 | => ParseTest("1 'yd^2'", AreaValue.Yard(1).AsExpression()); 35 | 36 | [Test] 37 | public void ParseSquareMile() 38 | => ParseTest("1 'mi^2'", AreaValue.Mile(1).AsExpression()); 39 | 40 | [Test] 41 | public void ParseHectare() 42 | => ParseTest("1 'ha'", AreaValue.Hectare(1).AsExpression()); 43 | 44 | [Test] 45 | public void ParseAcre() 46 | => ParseTest("1 'ac'", AreaValue.Acre(1).AsExpression()); 47 | 48 | [Test] 49 | public void ParseSquareUnitWithoutExponent() 50 | => ParseErrorTest("1 'mi^'"); 51 | } -------------------------------------------------------------------------------- /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/ParserTests/ConditionalOperatorTests.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 ConditionalOperatorTests : BaseParserTests 7 | { 8 | [Test] 9 | public void ConditionalAndTest() 10 | { 11 | var expected = new ConditionalAnd( 12 | new Equal(Variable.X, Number.Zero), 13 | new NotEqual(Variable.Y, Number.Zero) 14 | ); 15 | 16 | ParseTest("x == 0 && y != 0", expected); 17 | } 18 | 19 | [Test] 20 | public void ConditionalOrTest() 21 | { 22 | var expected = new ConditionalOr( 23 | new Equal(Variable.X, Number.Zero), 24 | new NotEqual(Variable.Y, Number.Zero) 25 | ); 26 | 27 | ParseTest("x == 0 || y != 0", expected); 28 | } 29 | 30 | [Test] 31 | public void ConditionalPrecedenceTest() 32 | { 33 | var expected = new ConditionalOr( 34 | Variable.X, 35 | new ConditionalAnd(Variable.Y, new Variable("z")) 36 | ); 37 | 38 | ParseTest("x || y && z", expected); 39 | } 40 | 41 | [Test] 42 | [TestCase("x == 0 &&")] 43 | [TestCase("x == 0 ||")] 44 | public void ConditionalMissingSecondOperand(string function) 45 | => ParseErrorTest(function); 46 | } -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /xFunc.Tests/ParserTests/ForTests.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 ForTests : BaseParserTests 7 | { 8 | [Test] 9 | public void ForTest() 10 | { 11 | var expected = new For( 12 | Number.Two, 13 | new Assign(Variable.X, Number.Zero), 14 | new LessThan(Variable.X, new Number(10)), 15 | new Assign(Variable.X, new Add(Variable.X, Number.One)) 16 | ); 17 | 18 | ParseTest("for(2, x := 0, x < 10, x := x + 1)", expected); 19 | } 20 | 21 | [Test] 22 | public void ForWithIncTest() 23 | { 24 | var expected = new For( 25 | Number.Two, 26 | new Assign(Variable.X, Number.Zero), 27 | new LessThan(Variable.X, new Number(10)), 28 | new Inc(Variable.X) 29 | ); 30 | 31 | ParseTest("for(2, x := 0, x < 10, x++)", expected); 32 | } 33 | 34 | [Test] 35 | [TestCase("for 2, x := 0, x < 10, x++)")] 36 | [TestCase("for(, x := 0, x < 10, x++)")] 37 | [TestCase("for(2 x := 0, x < 10, x++)")] 38 | [TestCase("for(2, , x < 10, x++)")] 39 | [TestCase("for(2, x := z x < 10, x++)")] 40 | [TestCase("for(2, x := 0, , x++)")] 41 | [TestCase("for(2, x := 0, x < 10 x++)")] 42 | [TestCase("for(2, x := 0, x < 10, )")] 43 | [TestCase("for(2, x := 0, x < 10, x++")] 44 | public void ForMissingPartsTest(string function) 45 | => ParseErrorTest(function); 46 | } -------------------------------------------------------------------------------- /xFunc.Tests/ParserTests/GCDTests.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.Immutable; 5 | 6 | namespace xFunc.Tests.ParserTests; 7 | 8 | public class GCDTests : BaseParserTests 9 | { 10 | [Test] 11 | public void CtorNullTest() 12 | => Assert.Throws(() => new GCD(new ImmutableArray())); 13 | 14 | [Test] 15 | [TestCase("gcd(12, 16)")] 16 | [TestCase("gcf(12, 16)")] 17 | [TestCase("hcf(12, 16)")] 18 | public void GCDTest(string function) 19 | => ParseTest(function, new GCD(new Number(12), new Number(16))); 20 | 21 | [Test] 22 | public void GCDOfThreeTest() 23 | { 24 | var expected = new GCD(new IExpression[] { new Number(12), new Number(16), new Number(8) }); 25 | 26 | ParseTest("gcd(12, 16, 8)", expected); 27 | } 28 | 29 | [Test] 30 | public void UnaryMinusAfterCommaTest() 31 | { 32 | var expected = new GCD(Number.Two, new UnaryMinus(Variable.X)); 33 | 34 | ParseTest("gcd(2, -x)", expected); 35 | } 36 | 37 | [Test] 38 | [TestCase("lcm(12, 16)")] 39 | [TestCase("scm(12, 16)")] 40 | public void LCMTest(string function) 41 | => ParseTest(function, new LCM(new Number(12), new Number(16))); 42 | } -------------------------------------------------------------------------------- /xFunc.Tests/ParserTests/HyperbolicTests.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 HyperbolicTests : BaseParserTests 7 | { 8 | [Test] 9 | [TestCase("sinh(2)")] 10 | [TestCase("sh(2)")] 11 | public void SinhTest(string function) 12 | => ParseTest(function, new Sinh(Number.Two)); 13 | 14 | [Test] 15 | [TestCase("cosh(2)")] 16 | [TestCase("ch(2)")] 17 | public void CoshTest(string function) 18 | => ParseTest(function, new Cosh(Number.Two)); 19 | 20 | [Test] 21 | [TestCase("tanh(2)")] 22 | [TestCase("th(2)")] 23 | public void TanhTest(string function) 24 | => ParseTest(function, new Tanh(Number.Two)); 25 | 26 | [Test] 27 | [TestCase("coth(2)")] 28 | [TestCase("cth(2)")] 29 | public void CothTest(string function) 30 | => ParseTest(function, new Coth(Number.Two)); 31 | 32 | [Test] 33 | public void SechTest() 34 | => ParseTest("sech(2)", new Sech(Number.Two)); 35 | 36 | [Test] 37 | public void CschTest() 38 | => ParseTest("csch(2)", new Csch(Number.Two)); 39 | } -------------------------------------------------------------------------------- /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/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 | } -------------------------------------------------------------------------------- /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.Tests/ParserTests/NumberTests.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 NumberTests : BaseParserTests 7 | { 8 | [Test] 9 | public void NumberFormatTest() 10 | => ParseErrorTest("0."); 11 | 12 | [Test] 13 | [TestCase("1.2345E-10", 0.00000000012345)] 14 | [TestCase("1.2345E10", 12345000000)] 15 | [TestCase("1.2345E+10", 12345000000)] 16 | [TestCase("1.2e2", 120)] 17 | public void ExpNegativeNumber(string exp, double number) 18 | => ParseTest(exp, new Number(number)); 19 | 20 | [Test] 21 | [TestCase("0b01100110")] 22 | [TestCase("0B01100110")] 23 | public void BinTest(string function) 24 | => ParseTest(function, new Number(0b01100110)); 25 | 26 | [Test] 27 | [TestCase("0XFF00")] 28 | [TestCase("0xff00")] 29 | public void HexTest(string function) 30 | => ParseTest(function, new Number(0xFF00)); 31 | 32 | [Test] 33 | public void OctTest() 34 | => ParseTest("0436", new Number(286)); 35 | } -------------------------------------------------------------------------------- /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/RelationalOperatorTests.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 RelationalOperatorTests : BaseParserTests 7 | { 8 | [Test] 9 | public void LessThenTest() 10 | { 11 | var expected = new LessThan(Variable.X, Number.Zero); 12 | 13 | ParseTest("x < 0", expected); 14 | } 15 | 16 | [Test] 17 | public void LessOrEqualTest() 18 | { 19 | var expected = new LessOrEqual(Variable.X, Number.Zero); 20 | 21 | ParseTest("x <= 0", expected); 22 | } 23 | 24 | [Test] 25 | public void GreaterThenTest() 26 | { 27 | var expected = new GreaterThan(Variable.X, Number.Zero); 28 | 29 | ParseTest("x > 0", expected); 30 | } 31 | 32 | [Test] 33 | public void GreaterOrEqualTest() 34 | { 35 | var expected = new GreaterOrEqual(Variable.X, Number.Zero); 36 | 37 | ParseTest("x >= 0", expected); 38 | } 39 | 40 | [Test] 41 | public void PrecedenceTest() 42 | { 43 | var expected = new NotEqual( 44 | new Variable("a"), 45 | new LessThan(new Variable("b"), new Variable("c")) 46 | ); 47 | 48 | ParseTest("a != b < c", expected); 49 | } 50 | } -------------------------------------------------------------------------------- /xFunc.Tests/ParserTests/ReverseHyperbolicTests.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 ReverseHyperbolicTests : BaseParserTests 7 | { 8 | [Test] 9 | [TestCase("arsinh(2)")] 10 | [TestCase("arsh(2)")] 11 | public void ArsinhTest(string function) 12 | => ParseTest(function, new Arsinh(Number.Two)); 13 | 14 | [Test] 15 | [TestCase("arcosh(2)")] 16 | [TestCase("arch(2)")] 17 | public void ArcoshTest(string function) 18 | => ParseTest(function, new Arcosh(Number.Two)); 19 | 20 | [Test] 21 | [TestCase("artanh(2)")] 22 | [TestCase("arth(2)")] 23 | public void ArtanhTest(string function) 24 | => ParseTest(function, new Artanh(Number.Two)); 25 | 26 | [Test] 27 | [TestCase("arcoth(2)")] 28 | [TestCase("arcth(2)")] 29 | public void ArcothTest(string function) 30 | => ParseTest(function, new Arcoth(Number.Two)); 31 | 32 | [Test] 33 | [TestCase("arsech(2)")] 34 | [TestCase("arsch(2)")] 35 | public void ArsechTest(string function) 36 | => ParseTest(function, new Arsech(Number.Two)); 37 | 38 | [Test] 39 | public void ArcschTest() 40 | => ParseTest("arcsch(2)", new Arcsch(Number.Two)); 41 | } -------------------------------------------------------------------------------- /xFunc.Tests/ParserTests/ReverseTrigonometricTests.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 ReverseTrigonometricTests : BaseParserTests 7 | { 8 | [Test] 9 | public void ArcsinTest() 10 | => ParseTest("arcsin(2)", new Arcsin(Number.Two)); 11 | 12 | [Test] 13 | public void ArccosTest() 14 | => ParseTest("arccos(2)", new Arccos(Number.Two)); 15 | 16 | [Test] 17 | [TestCase("arctan(2)")] 18 | [TestCase("arctg(2)")] 19 | public void ArctanTest(string function) 20 | => ParseTest(function, new Arctan(Number.Two)); 21 | 22 | [Test] 23 | [TestCase("arccot(2)")] 24 | [TestCase("arcctg(2)")] 25 | public void ArccotTest(string function) 26 | => ParseTest(function, new Arccot(Number.Two)); 27 | 28 | [Test] 29 | public void ArcsecTest() 30 | => ParseTest("arcsec(2)", new Arcsec(Number.Two)); 31 | 32 | [Test] 33 | [TestCase("arccsc(2)")] 34 | [TestCase("arccosec(2)")] 35 | public void ArccscTest(string function) 36 | => ParseTest(function, new Arccsc(Number.Two)); 37 | } -------------------------------------------------------------------------------- /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/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.Tests/ParserTests/TimeUnitTests.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 TimeUnitTests : BaseParserTests 7 | { 8 | [Test] 9 | public void ParseSecond() 10 | => ParseTest("1 's'", TimeValue.Second(1).AsExpression()); 11 | 12 | [Test] 13 | public void ParseNanosecond() 14 | => ParseTest("1 'ns'", TimeValue.Nanosecond(1).AsExpression()); 15 | 16 | [Test] 17 | public void ParseMicrosecond() 18 | => ParseTest("1 'μs'", TimeValue.Microsecond(1).AsExpression()); 19 | 20 | [Test] 21 | public void ParseMillisecond() 22 | => ParseTest("1 'ms'", TimeValue.Millisecond(1).AsExpression()); 23 | 24 | [Test] 25 | public void ParseMinute() 26 | => ParseTest("1 'min'", TimeValue.Minute(1).AsExpression()); 27 | 28 | [Test] 29 | public void ParseHour() 30 | => ParseTest("1 'h'", TimeValue.Hour(1).AsExpression()); 31 | 32 | [Test] 33 | public void ParseDay() 34 | => ParseTest("1 'day'", TimeValue.Day(1).AsExpression()); 35 | 36 | [Test] 37 | public void ParseWeek() 38 | => ParseTest("1 'week'", TimeValue.Week(1).AsExpression()); 39 | 40 | [Test] 41 | public void ParseYear() 42 | => ParseTest("1 'year'", TimeValue.Year(1).AsExpression()); 43 | } -------------------------------------------------------------------------------- /xFunc.Tests/ParserTests/TrigonometricTests.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 TrigonometricTests : BaseParserTests 7 | { 8 | [Test] 9 | public void SinTest() 10 | => ParseTest("sin(2)", new Sin(Number.Two)); 11 | 12 | [Test] 13 | public void CosTest() 14 | => ParseTest("cos(2)", new Cos(Number.Two)); 15 | 16 | [Test] 17 | [TestCase("tan(2)")] 18 | [TestCase("tg(2)")] 19 | public void TanTest(string function) 20 | => ParseTest(function, new Tan(Number.Two)); 21 | 22 | [Test] 23 | [TestCase("cot(2)")] 24 | [TestCase("ctg(2)")] 25 | public void CotTest(string function) 26 | => ParseTest(function, new Cot(Number.Two)); 27 | 28 | [Test] 29 | public void SecTest() 30 | => ParseTest("sec(2)", new Sec(Number.Two)); 31 | 32 | [Test] 33 | [TestCase("csc(2)")] 34 | [TestCase("cosec(2)")] 35 | public void CscTest(string function) 36 | => ParseTest(function, new Csc(Number.Two)); 37 | } -------------------------------------------------------------------------------- /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/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/VolumeUnitTests.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 VolumeUnitTests : BaseParserTests 7 | { 8 | [Test] 9 | public void ParseSquareMeter() 10 | => ParseTest("1 'm^3'", VolumeValue.Meter(1).AsExpression()); 11 | 12 | [Test] 13 | public void ParseSquareCentimeter() 14 | => ParseTest("1 'cm^3'", VolumeValue.Centimeter(1).AsExpression()); 15 | 16 | [Test] 17 | public void ParseSquareLiter() 18 | => ParseTest("1 'l'", VolumeValue.Liter(1).AsExpression()); 19 | 20 | [Test] 21 | public void ParseSquareInch() 22 | => ParseTest("1 'in^3'", VolumeValue.Inch(1).AsExpression()); 23 | 24 | [Test] 25 | public void ParseSquareFoot() 26 | => ParseTest("1 'ft^3'", VolumeValue.Foot(1).AsExpression()); 27 | 28 | [Test] 29 | public void ParseSquareYard() 30 | => ParseTest("1 'yd^3'", VolumeValue.Yard(1).AsExpression()); 31 | 32 | [Test] 33 | public void ParseGallon() 34 | => ParseTest("1 'gal'", VolumeValue.Gallon(1).AsExpression()); 35 | 36 | [Test] 37 | public void ParseSquareUnitWithoutExponent() 38 | => ParseErrorTest("1 'cm^'"); 39 | } -------------------------------------------------------------------------------- /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.Tests/Results/AngleResultTest.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.Results; 5 | 6 | public class AngleResultTest 7 | { 8 | [Test] 9 | public void TryGetAngleTest() 10 | { 11 | var expected = AngleValue.Degree(90); 12 | var angleResult = new Result.AngleResult(expected); 13 | var result = angleResult.TryGetAngle(out var angleValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(angleValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetAngleFalseTest() 21 | { 22 | var angleResult = new Result.NumberResult(NumberValue.One); 23 | var result = angleResult.TryGetAngle(out var angleValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(angleValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var angle = AngleValue.Degree(10); 33 | var result = new Result.AngleResult(angle); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("10 'degree'")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/AreaResultTest.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.Results; 5 | 6 | public class AreaResultTest 7 | { 8 | [Test] 9 | public void TryGetAreaTest() 10 | { 11 | var expected = AreaValue.Kilometer(10); 12 | var areaResult = new Result.AreaResult(expected); 13 | var result = areaResult.TryGetArea(out var areaValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(areaValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetAreaFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetArea(out var areaValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(areaValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var areaValue = AreaValue.Meter(10); 33 | var result = new Result.AreaResult(areaValue); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("10 'm^2'")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/BooleanResultTest.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.Results; 5 | 6 | public class BooleanResultTest 7 | { 8 | [Test] 9 | public void TryGetBooleanTest() 10 | { 11 | var areaResult = new Result.BooleanResult(true); 12 | var result = areaResult.TryGetBoolean(out var boolValue); 13 | 14 | Assert.That(result, Is.True); 15 | Assert.That(boolValue, Is.True); 16 | } 17 | 18 | [Test] 19 | public void TryGetBoolFalseTest() 20 | { 21 | var areaResult = new Result.NumberResult(NumberValue.One); 22 | var result = areaResult.TryGetBoolean(out var boolValue); 23 | 24 | Assert.That(result, Is.False); 25 | Assert.That(boolValue, Is.Null); 26 | } 27 | 28 | [Test] 29 | public void ToStringTest() 30 | { 31 | var result = new Result.BooleanResult(true); 32 | 33 | Assert.That(result.ToString(), Is.EqualTo("True")); 34 | } 35 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/LambdaResultTest.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.Results; 5 | 6 | public class LambdaResultTest 7 | { 8 | [Test] 9 | public void TryGetLambdaTest() 10 | { 11 | var expected = Variable.X.ToLambda(); 12 | var areaResult = new Result.LambdaResult(expected); 13 | var result = areaResult.TryGetLambda(out var lambdaValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(lambdaValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetLambdaFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetLambda(out var lambdaValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(lambdaValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var lambda = new Lambda(new[] { "x" }, Variable.X); 33 | var result = new Result.LambdaResult(lambda); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("(x) => x")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/LengthResultTest.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.Results; 5 | 6 | public class LengthResultTest 7 | { 8 | [Test] 9 | public void TryGetLengthTest() 10 | { 11 | var expected = LengthValue.Meter(10); 12 | var areaResult = new Result.LengthResult(expected); 13 | var result = areaResult.TryGetLength(out var lengthValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(lengthValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetLengthFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetLength(out var lengthValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(lengthValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var lengthValue = LengthValue.Meter(10); 33 | var result = new Result.LengthResult(lengthValue); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("10 'm'")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/MassResultTest.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.Results; 5 | 6 | public class MassResultTest 7 | { 8 | [Test] 9 | public void TryGetMassTest() 10 | { 11 | var expected = MassValue.Gram(10); 12 | var areaResult = new Result.MassResult(expected); 13 | var result = areaResult.TryGetMass(out var massValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(massValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetMassFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetMass(out var areaValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(areaValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var power = MassValue.Gram(10); 33 | var result = new Result.MassResult(power); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("10 'g'")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/MatrixResultTest.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.Results; 5 | 6 | public class MatrixResultTest 7 | { 8 | [Test] 9 | public void TryGetMatrixTest() 10 | { 11 | var expected = MatrixValue.Create(NumberValue.One); 12 | var areaResult = new Result.MatrixResult(expected); 13 | var result = areaResult.TryGetMatrix(out var matrixValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(matrixValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetMatrixFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetMatrix(out var matrixValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(matrixValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var matrixValue = MatrixValue.Create(new NumberValue[][] 33 | { 34 | [NumberValue.One, NumberValue.Two], 35 | [NumberValue.Two, NumberValue.One], 36 | }); 37 | var result = new Result.MatrixResult(matrixValue); 38 | 39 | Assert.That(result.ToString(), Is.EqualTo("{{1, 2}, {2, 1}}")); 40 | } 41 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/NumberResultTest.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.Results; 5 | 6 | public class NumberResultTest 7 | { 8 | [Test] 9 | public void TryGetNumberTest() 10 | { 11 | var expected = NumberValue.One; 12 | var areaResult = new Result.NumberResult(expected); 13 | var result = areaResult.TryGetNumber(out var numberValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(numberValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetNumberFalseTest() 21 | { 22 | var areaResult = new Result.AngleResult(AngleValue.Degree(10)); 23 | var result = areaResult.TryGetNumber(out var numberValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(numberValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var result = new Result.NumberResult(10.2); 33 | 34 | Assert.That(result.ToString(), Is.EqualTo("10.2")); 35 | } 36 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/PowerResultTest.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.Results; 5 | 6 | public class PowerResultTest 7 | { 8 | [Test] 9 | public void TryGetPowerTest() 10 | { 11 | var expected = PowerValue.Watt(10); 12 | var areaResult = new Result.PowerResult(expected); 13 | var result = areaResult.TryGetPower(out var powerValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(powerValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetPowerFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetPower(out var powerValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(powerValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var power = PowerValue.Watt(10); 33 | var result = new Result.PowerResult(power); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("10 'W'")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/RationalResultTest.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.Results; 5 | 6 | public class RationalResultTest 7 | { 8 | [Test] 9 | public void TryGetRationalTest() 10 | { 11 | var expected = new RationalValue(1, 2); 12 | var areaResult = new Result.RationalResult(expected); 13 | var result = areaResult.TryGetRational(out var rationalValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(rationalValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetRationalFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetRational(out var rationalValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(rationalValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var angle = new RationalValue(1, 2); 33 | var result = new Result.RationalResult(angle); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("1 // 2")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/StringResultTest.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.Results; 5 | 6 | public class StringResultTest 7 | { 8 | [Test] 9 | public void TryGetStringTest() 10 | { 11 | var expected = "hello"; 12 | var areaResult = new Result.StringResult(expected); 13 | var result = areaResult.TryGetString(out var stringValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(stringValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetStringFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetString(out var stringValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(stringValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void NullTest() 31 | => Assert.Throws(() => new Result.StringResult(null!)); 32 | 33 | [Test] 34 | public void ToStringTest() 35 | { 36 | var result = new Result.StringResult("hello"); 37 | 38 | Assert.That(result.ToString(), Is.EqualTo("hello")); 39 | } 40 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/TemperatureResultTest.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.Results; 5 | 6 | public class TemperatureResultTest 7 | { 8 | [Test] 9 | public void TryGetTemperatureTest() 10 | { 11 | var expected = TemperatureValue.Celsius(10); 12 | var areaResult = new Result.TemperatureResult(expected); 13 | var result = areaResult.TryGetTemperature(out var temperatureValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(temperatureValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetTemperatureFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetTemperature(out var temperatureValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(temperatureValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var power = TemperatureValue.Celsius(10); 33 | var result = new Result.TemperatureResult(power); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("10 '°C'")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/TimeResultTest.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.Results; 5 | 6 | public class TimeResultTest 7 | { 8 | [Test] 9 | public void TryGetTimeTest() 10 | { 11 | var expected = TimeValue.Second(10); 12 | var areaResult = new Result.TimeResult(expected); 13 | var result = areaResult.TryGetTime(out var timeValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(timeValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetTimeFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetTime(out var timeValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(timeValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var lengthValue = TimeValue.Second(10); 33 | var result = new Result.TimeResult(lengthValue); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("10 's'")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/VectorResultTest.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.Results; 5 | 6 | public class VectorResultTest 7 | { 8 | [Test] 9 | public void TryGetVectorTest() 10 | { 11 | var expected = VectorValue.Create(new NumberValue(1)); 12 | var areaResult = new Result.VectorResult(expected); 13 | var result = areaResult.TryGetVector(out var vectorValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(vectorValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetVectorFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetVector(out var areaValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(areaValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var vectorValue = VectorValue.Create(NumberValue.One, NumberValue.Two); 33 | var result = new Result.VectorResult(vectorValue); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("{1, 2}")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.Tests/Results/VolumeResultTest.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.Results; 5 | 6 | public class VolumeResultTest 7 | { 8 | [Test] 9 | public void TryGetVolumeTest() 10 | { 11 | var expected = VolumeValue.Meter(10); 12 | var areaResult = new Result.VolumeResult(expected); 13 | var result = areaResult.TryGetVolume(out var volumeValue); 14 | 15 | Assert.That(result, Is.True); 16 | Assert.That(volumeValue, Is.EqualTo(expected)); 17 | } 18 | 19 | [Test] 20 | public void TryGetVolumeFalseTest() 21 | { 22 | var areaResult = new Result.NumberResult(NumberValue.One); 23 | var result = areaResult.TryGetVolume(out var volumeValue); 24 | 25 | Assert.That(result, Is.False); 26 | Assert.That(volumeValue, Is.Null); 27 | } 28 | 29 | [Test] 30 | public void ToStringTest() 31 | { 32 | var volume = VolumeValue.Meter(10); 33 | var result = new Result.VolumeResult(volume); 34 | 35 | Assert.That(result.ToString(), Is.EqualTo("10 'm^3'")); 36 | } 37 | } -------------------------------------------------------------------------------- /xFunc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sys27/xFunc/ed48d73bad5d59adb8fbfb826e9432383fc0fbd6/xFunc.png -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------