Skip to content

Commit

Permalink
Precalculate common angles for trigonometric functions (#292)
Browse files Browse the repository at this point in the history
* Close #269 - Precalculate common angles for trigonometric functions

* Fix ComplexNumber format
  • Loading branch information
sys27 committed Aug 31, 2020
1 parent 8692b78 commit 57b487c
Show file tree
Hide file tree
Showing 20 changed files with 784 additions and 108 deletions.
522 changes: 522 additions & 0 deletions xFunc.Maths/AngleExtensions.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Abs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override object Execute(ExpressionParameters parameters)
return result switch
{
double number => Math.Abs(number),
Angle angle => Angle.Abs(angle),
Angle angle => AngleExtensions.Abs(angle),
Complex complex => Complex.Abs(complex),
Vector vector => vector.Abs(parameters),
_ => throw new ResultIsNotSupportedException(this, result),
Expand Down
63 changes: 25 additions & 38 deletions xFunc.Maths/Expressions/Angles/Angle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,51 +402,38 @@ private static AngleUnit GetCommonUnit(AngleUnit left, AngleUnit right)
};

/// <summary>
/// Converts <see cref="Angle"/> to <see cref="AngleNumber"/>.
/// Normalizes the current angle between [0, 2pi).
/// </summary>
/// <returns>The angle number.</returns>
public AngleNumber AsExpression()
=> new AngleNumber(this);
/// <returns>The normalized angle.</returns>
public Angle Normalize()
{
const double degreeFullCircle = 360.0;
const double radianFullCircle = 2 * Math.PI;
const double gradianFullCircle = 400.0;

/// <summary>
/// Returns the absolute value of a specified angle.
/// </summary>
/// <param name="angle">The angle.</param>
/// <returns>The angle, <c>x</c>, that such that 0 ≤ <c>x</c> ≤ <c>MaxValue</c>.</returns>
public static Angle Abs(Angle angle)
=> new Angle(Math.Abs(angle.Value), angle.Unit);
static double NormalizeInternal(double value, double circle)
{
value %= circle;
if (value < 0)
value += circle;

/// <summary>
/// Returns the smallest integral value that is greater than or equal to the specified angle number.
/// </summary>
/// <param name="angle">The angle.</param>
/// <returns>The smallest integral value.</returns>
public static Angle Ceiling(Angle angle)
=> new Angle(Math.Ceiling(angle.Value), angle.Unit);
return value;
}

/// <summary>
/// Returns the largest integral value less than or equal to the specified angle number.
/// </summary>
/// <param name="angle">The angle.</param>
/// <returns>The largest integral value.</returns>
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);
return Unit switch
{
AngleUnit.Radian => Radian(NormalizeInternal(Value, radianFullCircle)),
AngleUnit.Gradian => Gradian(NormalizeInternal(Value, gradianFullCircle)),
_ => Degree(NormalizeInternal(Value, degreeFullCircle)),
};
}

/// <summary>
/// Returns the fractional part of the angle number.
/// Converts <see cref="Angle"/> to <see cref="AngleNumber"/>.
/// </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);
/// <returns>The angle number.</returns>
public AngleNumber AsExpression()
=> new AngleNumber(this);

/// <summary>
/// Gets a value.
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Ceil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override object Execute(ExpressionParameters parameters)
return result switch
{
double number => Math.Ceiling(number),
Angle angle => Angle.Ceiling(angle),
Angle angle => AngleExtensions.Ceiling(angle),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Floor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override object Execute(ExpressionParameters parameters)
return result switch
{
double number => Math.Floor(number),
Angle angle => Angle.Floor(angle),
Angle angle => AngleExtensions.Floor(angle),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Frac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override object Execute(ExpressionParameters parameters)
return result switch
{
double number => MathExtensions.Frac(number),
Angle angle => Angle.Frac(angle),
Angle angle => AngleExtensions.Frac(angle),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
13 changes: 6 additions & 7 deletions xFunc.Maths/Expressions/Trigonometric/Cos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Numerics;
using xFunc.Maths.Analyzers;
Expand Down Expand Up @@ -53,8 +52,8 @@ internal Cos(IList<IExpression> arguments)
/// A result of the calculation.
/// </returns>
/// <seealso cref="ExpressionParameters" />
protected override double ExecuteInternal(Angle angle) =>
Math.Cos(angle.Value);
protected override double ExecuteInternal(Angle angle)
=> AngleExtensions.Cos(angle);

/// <summary>
/// Calculates the this mathematical expression (complex number).
Expand All @@ -63,8 +62,8 @@ internal Cos(IList<IExpression> arguments)
/// <returns>
/// A result of the calculation.
/// </returns>
protected override Complex ExecuteComplex(Complex complex) =>
Complex.Cos(complex);
protected override Complex ExecuteComplex(Complex complex)
=> Complex.Cos(complex);

/// <summary>
/// Analyzes the current expression.
Expand Down Expand Up @@ -94,7 +93,7 @@ private protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> a
/// Clones this instance.
/// </summary>
/// <returns>The new instance of <see cref="IExpression"/> that is a clone of this instance.</returns>
public override IExpression Clone() =>
new Cos(Argument.Clone());
public override IExpression Clone()
=> new Cos(Argument.Clone());
}
}
12 changes: 6 additions & 6 deletions xFunc.Maths/Expressions/Trigonometric/Cot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ internal Cot(IList<IExpression> arguments)
/// A result of the calculation.
/// </returns>
/// <seealso cref="ExpressionParameters" />
protected override double ExecuteInternal(Angle angle) =>
MathExtensions.Cot(angle.Value);
protected override double ExecuteInternal(Angle angle)
=> AngleExtensions.Cot(angle);

/// <summary>
/// Calculates the this mathematical expression (complex number).
Expand All @@ -62,8 +62,8 @@ internal Cot(IList<IExpression> arguments)
/// <returns>
/// A result of the calculation.
/// </returns>
protected override Complex ExecuteComplex(Complex complex) =>
ComplexExtensions.Cot(complex);
protected override Complex ExecuteComplex(Complex complex)
=> ComplexExtensions.Cot(complex);

/// <summary>
/// Analyzes the current expression.
Expand Down Expand Up @@ -93,7 +93,7 @@ private protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> a
/// Clones this instance.
/// </summary>
/// <returns>The new instance of <see cref="IExpression"/> that is a clone of this instance.</returns>
public override IExpression Clone() =>
new Cot(Argument.Clone());
public override IExpression Clone()
=> new Cot(Argument.Clone());
}
}
12 changes: 6 additions & 6 deletions xFunc.Maths/Expressions/Trigonometric/Csc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ internal Csc(IList<IExpression> arguments)
/// A result of the calculation.
/// </returns>
/// <seealso cref="ExpressionParameters" />
protected override double ExecuteInternal(Angle angle) =>
MathExtensions.Csc(angle.Value);
protected override double ExecuteInternal(Angle angle)
=> AngleExtensions.Csc(angle);

/// <summary>
/// Calculates the this mathematical expression (complex number).
Expand All @@ -62,8 +62,8 @@ internal Csc(IList<IExpression> arguments)
/// <returns>
/// A result of the calculation.
/// </returns>
protected override Complex ExecuteComplex(Complex complex) =>
ComplexExtensions.Csc(complex);
protected override Complex ExecuteComplex(Complex complex)
=> ComplexExtensions.Csc(complex);

/// <summary>
/// Analyzes the current expression.
Expand Down Expand Up @@ -93,7 +93,7 @@ private protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> a
/// Clones this instance.
/// </summary>
/// <returns>The new instance of <see cref="IExpression"/> that is a clone of this instance.</returns>
public override IExpression Clone() =>
new Csc(Argument.Clone());
public override IExpression Clone()
=> new Csc(Argument.Clone());
}
}
12 changes: 6 additions & 6 deletions xFunc.Maths/Expressions/Trigonometric/Sec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ internal Sec(IList<IExpression> arguments)
/// A result of the calculation.
/// </returns>
/// <seealso cref="ExpressionParameters" />
protected override double ExecuteInternal(Angle angle) =>
MathExtensions.Sec(angle.Value);
protected override double ExecuteInternal(Angle angle)
=> AngleExtensions.Sec(angle);

/// <summary>
/// Calculates the this mathematical expression (complex number).
Expand All @@ -62,8 +62,8 @@ internal Sec(IList<IExpression> arguments)
/// <returns>
/// A result of the calculation.
/// </returns>
protected override Complex ExecuteComplex(Complex complex) =>
ComplexExtensions.Sec(complex);
protected override Complex ExecuteComplex(Complex complex)
=> ComplexExtensions.Sec(complex);

/// <summary>
/// Analyzes the current expression.
Expand Down Expand Up @@ -93,7 +93,7 @@ private protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> a
/// Clones this instance.
/// </summary>
/// <returns>The new instance of <see cref="IExpression"/> that is a clone of this instance.</returns>
public override IExpression Clone() =>
new Sec(Argument.Clone());
public override IExpression Clone()
=> new Sec(Argument.Clone());
}
}
13 changes: 6 additions & 7 deletions xFunc.Maths/Expressions/Trigonometric/Sin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Numerics;
using xFunc.Maths.Analyzers;
Expand Down Expand Up @@ -53,8 +52,8 @@ internal Sin(IList<IExpression> arguments)
/// A result of the calculation.
/// </returns>
/// <seealso cref="ExpressionParameters" />
protected override double ExecuteInternal(Angle angle) =>
Math.Sin(angle.Value);
protected override double ExecuteInternal(Angle angle)
=> AngleExtensions.Sin(angle);

/// <summary>
/// Calculates the this mathematical expression (complex number).
Expand All @@ -63,8 +62,8 @@ internal Sin(IList<IExpression> arguments)
/// <returns>
/// A result of the calculation.
/// </returns>
protected override Complex ExecuteComplex(Complex complex) =>
Complex.Sin(complex);
protected override Complex ExecuteComplex(Complex complex)
=> Complex.Sin(complex);

/// <summary>
/// Analyzes the current expression.
Expand Down Expand Up @@ -94,7 +93,7 @@ private protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> a
/// Clones this instance.
/// </summary>
/// <returns>The new instance of <see cref="IExpression"/> that is a clone of this instance.</returns>
public override IExpression Clone() =>
new Sin(Argument.Clone());
public override IExpression Clone()
=> new Sin(Argument.Clone());
}
}
13 changes: 6 additions & 7 deletions xFunc.Maths/Expressions/Trigonometric/Tan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Numerics;
using xFunc.Maths.Analyzers;
Expand Down Expand Up @@ -53,8 +52,8 @@ internal Tan(IList<IExpression> arguments)
/// A result of the calculation.
/// </returns>
/// <seealso cref="ExpressionParameters" />
protected override double ExecuteInternal(Angle angle) =>
Math.Tan(angle.Value);
protected override double ExecuteInternal(Angle angle)
=> AngleExtensions.Tan(angle);

/// <summary>
/// Calculates the this mathematical expression (complex number).
Expand All @@ -63,8 +62,8 @@ internal Tan(IList<IExpression> arguments)
/// <returns>
/// A result of the calculation.
/// </returns>
protected override Complex ExecuteComplex(Complex complex) =>
Complex.Tan(complex);
protected override Complex ExecuteComplex(Complex complex)
=> Complex.Tan(complex);

/// <summary>
/// Analyzes the current expression.
Expand Down Expand Up @@ -94,7 +93,7 @@ private protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> a
/// Clones this instance.
/// </summary>
/// <returns>The new instance of <see cref="IExpression"/> that is a clone of this instance.</returns>
public override IExpression Clone() =>
new Tan(Argument.Clone());
public override IExpression Clone()
=> new Tan(Argument.Clone());
}
}
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Trunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override object Execute(ExpressionParameters parameters)
return result switch
{
double number => Math.Truncate(number),
Angle angle => Angle.Truncate(angle),
Angle angle => AngleExtensions.Truncate(angle),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down

0 comments on commit 57b487c

Please sign in to comment.