Skip to content

Commit

Permalink
Close #332 - Create function to convert Number to ComplexNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Sep 22, 2020
1 parent 878c24c commit 4901ba7
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 0 deletions.
9 changes: 9 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,15 @@ public virtual TResult Analyze(Re exp, TContext context)
public virtual TResult Analyze(Reciprocal exp, TContext context)
=> Analyze(exp as IExpression, context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <param name="context">The context.</param>
/// <returns>The result of analysis.</returns>
public virtual TResult Analyze(ToComplex exp, TContext context)
=> Analyze(exp as IExpression, context);

#endregion Complex Numbers

#region Trigonometric
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,14 @@ public virtual TResult Analyze(Re exp)
public virtual TResult Analyze(Reciprocal exp)
=> Analyze(exp as IExpression);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public virtual TResult Analyze(ToComplex exp)
=> Analyze(exp as IExpression);

#endregion Complex Numbers

#region Trigonometric
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,14 @@ public virtual string Analyze(Re exp)
public virtual string Analyze(Reciprocal exp)
=> ToString(exp, "reciprocal({0})");

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public virtual string Analyze(ToComplex exp)
=> ToString(exp, "tocomplex({0})");

#endregion Complex Numbers

#region Trigonometric
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ public interface IAnalyzer<out TResult, TContext>
/// <returns>The result of analysis.</returns>
TResult Analyze(Reciprocal exp, TContext context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <param name="context">The context.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(ToComplex exp, TContext context);

#endregion Complex Numbers

#region Trigonometric
Expand Down
7 changes: 7 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ public interface IAnalyzer<out TResult>
/// <returns>The result of analysis.</returns>
TResult Analyze(Reciprocal exp);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(ToComplex exp);

#endregion Complex Numbers

#region Trigonometric
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/Simplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,14 @@ public virtual IExpression Analyze(Re exp)
public virtual IExpression Analyze(Reciprocal exp)
=> AnalyzeUnary(exp);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public virtual IExpression Analyze(ToComplex exp)
=> AnalyzeUnary(exp);

#endregion Complex Numbers

#region Trigonometric
Expand Down
20 changes: 20 additions & 0 deletions xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,26 @@ public virtual ResultTypes Analyze(Reciprocal exp)
throw new ParameterTypeMismatchException(ResultTypes.ComplexNumber, result);
}

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public virtual ResultTypes Analyze(ToComplex exp)
{
if (exp is null)
throw ThrowHelpers.ExpNull();

var result = exp.Argument.Analyze(this);

return result switch
{
ResultTypes.Undefined => ResultTypes.ComplexNumber,
ResultTypes.Number => ResultTypes.ComplexNumber,
_ => ResultTypes.Number.ThrowFor(result),
};
}

#endregion Complex Numbers

#region Trigonometric
Expand Down
97 changes: 97 additions & 0 deletions xFunc.Maths/Expressions/ComplexNumbers/ToComplex.cs
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());
}
}
1 change: 1 addition & 0 deletions xFunc.Maths/Parser.ExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private IExpression CreateFunction(in Token token, IList<IExpression> arguments)
"phase" => new Phase(arguments),
"conjugate" => new Conjugate(arguments),
"reciprocal" => new Reciprocal(arguments),
"tocomplex" => new ToComplex(arguments),

"sum" => new Sum(arguments),
"product" => new Product(arguments),
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Tests/Analyzers/Formatters/CommonFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,14 @@ public void ReToStringTest()
Assert.Equal("re(3.1+2.5i)", exp.ToString());
}

[Fact]
public void ToComplexToStringTest()
{
var exp = new ToComplex(Number.Two);

Assert.Equal("tocomplex(2)", exp.ToString());
}

#endregion

#region Trigonometric
Expand Down
13 changes: 13 additions & 0 deletions xFunc.Tests/Analyzers/TypeAnalyzerTests/ComplexNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using xFunc.Maths.Analyzers.TypeAnalyzers;
using xFunc.Maths.Expressions;
using xFunc.Maths.Expressions.ComplexNumbers;
using xFunc.Maths.Expressions.LogicalAndBitwise;
using Xunit;

namespace xFunc.Tests.Analyzers.TypeAnalyzerTests
Expand Down Expand Up @@ -149,5 +150,17 @@ public void TestReciprocalException()

TestException(exp);
}

[Fact]
public void ToComplexUndefined()
=> Test(new ToComplex(Variable.X), ResultTypes.ComplexNumber);

[Fact]
public void ToComplexNubmer()
=> Test(new ToComplex(Number.One), ResultTypes.ComplexNumber);

[Fact]
public void ToComplexException()
=> TestException(new ToComplex(Bool.False));
}
}
53 changes: 53 additions & 0 deletions xFunc.Tests/Expressions/ComplexNumbers/ToComplexTest.cs
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);
}
}
}
8 changes: 8 additions & 0 deletions xFunc.Tests/ParserTests/ComplexNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,13 @@ public void ReciprocalTest()

ParseTest("reciprocal(3-2*i)", expected);
}

[Fact]
public void ToComplexTest()
{
var expected = new ToComplex(Number.Two);

ParseTest("tocomplex(2)", expected);
}
}
}

0 comments on commit 4901ba7

Please sign in to comment.