Skip to content

Commit

Permalink
Close #279 - Add Trunc and Frac functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Aug 28, 2020
1 parent 0ef3575 commit c5b4215
Show file tree
Hide file tree
Showing 18 changed files with 667 additions and 11 deletions.
18 changes: 18 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ public virtual TResult Analyze(Fact exp, TContext context)
public virtual TResult Analyze(Floor exp, TContext context)
=> throw new NotSupportedException();

/// <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(Trunc exp, TContext context)
=> throw new NotSupportedException();

/// <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(Frac exp, TContext context)
=> throw new NotSupportedException();

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ public virtual TResult Analyze(Fact exp)
public virtual TResult Analyze(Floor exp)
=> throw new NotSupportedException();

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public virtual TResult Analyze(Trunc exp)
=> throw new NotSupportedException();

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public virtual TResult Analyze(Frac exp)
=> throw new NotSupportedException();

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions xFunc.Maths/Analyzers/Differentiator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using xFunc.Maths.Expressions;
using xFunc.Maths.Expressions.Angles;
using xFunc.Maths.Expressions.Hyperbolic;
using xFunc.Maths.Expressions.Trigonometric;

Expand Down Expand Up @@ -274,6 +275,19 @@ public override IExpression Analyze(Number exp, DifferentiatorContext context)
return new Number(0);
}

/// <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 override IExpression Analyze(AngleNumber exp, DifferentiatorContext context)
{
return new Number(0);
}

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ public string Analyze(Floor exp)
return ToString(exp, "floor({0})");
}

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

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

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ public interface IAnalyzer<out TResult, TContext>
/// <returns>The result of analysis.</returns>
TResult Analyze(Floor 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(Trunc 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(Frac exp, TContext context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ public interface IAnalyzer<out TResult>
/// <returns>The result of analysis.</returns>
TResult Analyze(Floor exp);

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

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

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions xFunc.Maths/Analyzers/Simplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,24 @@ public IExpression Analyze(Exp exp)
[ExcludeFromCodeCoverage]
public IExpression Analyze(Floor exp) => AnalyzeUnary(exp);

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

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

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
40 changes: 38 additions & 2 deletions xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public ResultTypes Analyze(Ceil exp)
ResultTypes.Undefined => ResultTypes.Undefined,
ResultTypes.Number => ResultTypes.Number,
ResultTypes.AngleNumber => ResultTypes.AngleNumber,
_ => ResultTypes.Number.ThrowFor(result),
_ => ResultTypes.NumberOrAngle.ThrowFor(result),
};
}

Expand Down Expand Up @@ -348,7 +348,43 @@ public ResultTypes Analyze(Floor exp)
ResultTypes.Undefined => ResultTypes.Undefined,
ResultTypes.Number => ResultTypes.Number,
ResultTypes.AngleNumber => ResultTypes.AngleNumber,
_ => ResultTypes.Number.ThrowFor(result),
_ => ResultTypes.NumberOrAngle.ThrowFor(result),
};
}

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public ResultTypes Analyze(Trunc exp)
{
var result = exp.Argument.Analyze(this);

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

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public ResultTypes Analyze(Frac exp)
{
var result = exp.Argument.Analyze(this);

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

Expand Down
16 changes: 16 additions & 0 deletions xFunc.Maths/Expressions/Angles/Angle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ public static Angle Ceiling(Angle angle)
public static Angle Floor(Angle angle)
=> new Angle(Math.Floor(angle.Value), angle.Unit);

/// <summary>
/// Calculates the integral part of a specified angle number.
/// </summary>
/// <param name="angle">An angle to truncate.</param>
/// <returns>The integral part of angle number.</returns>
public static Angle Truncate(Angle angle)
=> new Angle(Math.Truncate(angle.Value), angle.Unit);

/// <summary>
/// Returns the fractional part of the angle number.
/// </summary>
/// <param name="angle">The angle number.</param>
/// <returns>The fractional part.</returns>
public static Angle Frac(Angle angle)
=> new Angle(MathExtensions.Frac(angle.Value), angle.Unit);

/// <summary>
/// Gets a value.
/// </summary>
Expand Down
99 changes: 99 additions & 0 deletions xFunc.Maths/Expressions/Frac.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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 xFunc.Maths.Analyzers;
using xFunc.Maths.Expressions.Angles;

namespace xFunc.Maths.Expressions
{
/// <summary>
/// Represents the 'frac' function.
/// </summary>
public class Frac : UnaryExpression
{
/// <summary>
/// Initializes a new instance of the <see cref="Frac"/> class.
/// </summary>
/// <param name="argument">The expression that represents a double-precision floating-point number to be rounded down.</param>
public Frac(IExpression argument)
: base(argument)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Frac"/> class.
/// </summary>
/// <param name="arguments">The argument of function.</param>
/// <seealso cref="IExpression"/>
internal Frac(IList<IExpression> arguments)
: base(arguments)
{
}

/// <summary>
/// Executes this expression.
/// </summary>
/// <param name="parameters">An object that contains all parameters and functions for expressions.</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 => MathExtensions.Frac(number),
Angle angle => Angle.Frac(angle),
_ => 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>
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="IExpression" />.
/// </summary>
/// <returns>
/// Returns the new instance of <see cref="IExpression" /> that is a clone of this instance.
/// </returns>
public override IExpression Clone() =>
new Frac(Argument.Clone());
}
}

0 comments on commit c5b4215

Please sign in to comment.