-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #332 - Create function to convert Number to ComplexNumber
- Loading branch information
Showing
13 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2012-2020 Dmytro Kyshchenko | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Numerics; | ||
using xFunc.Maths.Analyzers; | ||
|
||
namespace xFunc.Maths.Expressions.ComplexNumbers | ||
{ | ||
/// <summary> | ||
/// Represents the 'tocomplex' function. | ||
/// </summary> | ||
public class ToComplex : UnaryExpression | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ToComplex"/> class. | ||
/// </summary> | ||
/// <param name="argument">The argument of function.</param> | ||
/// <seealso cref="IExpression"/> | ||
public ToComplex(IExpression argument) | ||
: base(argument) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ToComplex"/> class. | ||
/// </summary> | ||
/// <param name="arguments">The argument of function.</param> | ||
/// <seealso cref="IExpression"/> | ||
internal ToComplex(IList<IExpression> arguments) | ||
: base(arguments) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Returns a number. | ||
/// </summary> | ||
/// <param name="parameters">A collection of variables.</param> | ||
/// <returns>A result of the execution.</returns> | ||
/// <seealso cref="ExpressionParameters"/> | ||
public override object Execute(ExpressionParameters? parameters) | ||
{ | ||
var result = Argument.Execute(parameters); | ||
|
||
return result switch | ||
{ | ||
double number => (Complex)number, | ||
_ => throw new ResultIsNotSupportedException(this, result), | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Analyzes the current expression. | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of the result.</typeparam> | ||
/// <param name="analyzer">The analyzer.</param> | ||
/// <returns> | ||
/// The analysis result. | ||
/// </returns> | ||
private protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> analyzer) | ||
=> analyzer.Analyze(this); | ||
|
||
/// <summary> | ||
/// Analyzes the current expression. | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of the result.</typeparam> | ||
/// <typeparam name="TContext">The type of additional parameter for analyzer.</typeparam> | ||
/// <param name="analyzer">The analyzer.</param> | ||
/// <param name="context">The context.</param> | ||
/// <returns>The analysis result.</returns> | ||
[ExcludeFromCodeCoverage] | ||
private protected override TResult AnalyzeInternal<TResult, TContext>( | ||
IAnalyzer<TResult, TContext> analyzer, | ||
TContext context) | ||
=> analyzer.Analyze(this, context); | ||
|
||
/// <summary> | ||
/// Clones this instance of the <see cref="ToComplex"/> class. | ||
/// </summary> | ||
/// <returns>Returns the new instance of <see cref="IExpression"/> that is a clone of this instance.</returns> | ||
public override IExpression Clone() | ||
=> new ToComplex(Argument.Clone()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2012-2020 Dmytro Kyshchenko | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System.Numerics; | ||
using xFunc.Maths.Expressions; | ||
using xFunc.Maths.Expressions.ComplexNumbers; | ||
using xFunc.Maths.Expressions.LogicalAndBitwise; | ||
using Xunit; | ||
|
||
namespace xFunc.Tests.Expressions.ComplexNumbers | ||
{ | ||
public class ToComplexTest | ||
{ | ||
[Fact] | ||
public void ExecuteTest() | ||
{ | ||
var exp = new ToComplex(Number.Two); | ||
var result = (Complex)exp.Execute(); | ||
var expected = new Complex(2, 0); | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void ExecuteBoolTest() | ||
{ | ||
var exp = new ToComplex(Bool.False); | ||
|
||
Assert.Throws<ResultIsNotSupportedException>(() => exp.Execute()); | ||
} | ||
|
||
[Fact] | ||
public void CloneTest() | ||
{ | ||
var exp = new ToComplex(Number.Two); | ||
var clone = exp.Clone(); | ||
|
||
Assert.Equal(exp, clone); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters