Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions Sources/Accord.Math/TaylorSeries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Accord Math Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © Diego Catalano, 2014
// diego.catalano at live.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Accord.Math
{

/// <summary>
/// Taylor Series Expansions.
///
/// <para>In mathematics, a Taylor series is a representation of a function as an infinite sum of terms that are calculated from
/// the values of the function's derivatives at a single point.</para>
///
/// </summary>
/// <remarks>
/// http://www.haverford.edu/physics/MathAppendices/Taylor_Series.pdf
/// </remarks>
public static class TaylorSeries
{

/// <summary>
/// Compute Sin using Taylor Series.
/// </summary>
/// <param name="x">An angle, in radians.</param>
/// <param name="nTerms">Number of terms.</param>
/// <returns>Sin of the x.</returns>
public static double Sin(double x, int nTerms)
{
if (nTerms < 2) return x;
if (nTerms == 2)
{
return x - (x * x * x) / 6D;
}
else
{

double mult = x * x * x;
double fact = 6;
double sign = 1;
int factS = 5;
double result = x - mult / fact;
for (int i = 3; i <= nTerms; i++)
{
mult *= x * x;
fact *= factS * (factS - 1);
factS += 2;
result += sign * (mult / fact);
sign *= -1;
}

return result;
}
}

/// <summary>
/// Compute Cos using Taylor Series.
/// </summary>
/// <param name="x">An angle, in radians.</param>
/// <param name="nTerms">Number of terms.</param>
/// <returns>Cos of the x.</returns>
public static double Cos(double x, int nTerms)
{
if (nTerms < 2) return 1;
if (nTerms == 2)
{
return 1 - (x * x) / 2D;
}
else
{

double mult = x * x;
double fact = 2;
double sign = 1;
int factS = 4;
double result = 1 - mult / fact;
for (int i = 3; i <= nTerms; i++)
{
mult *= x * x;
fact *= factS * (factS - 1);
factS += 2;
result += sign * (mult / fact);
sign *= -1;
}

return result;
}
}

/// <summary>
/// Compute Hiperbolic Sin using Taylor Series.
/// </summary>
/// <param name="x">An angle, in radians.</param>
/// <param name="nTerms">Number of terms.</param>
/// <returns>Hiperbolic sin of the x.</returns>
public static double Sinh(double x, int nTerms)
{
if (nTerms < 2) return x;
if (nTerms == 2)
{
return x + (x * x * x) / 6D;
}
else
{

double mult = x * x * x;
double fact = 6;
int factS = 5;
double result = x + mult / fact;
for (int i = 3; i <= nTerms; i++)
{
mult *= x * x;
fact *= factS * (factS - 1);
factS += 2;
result += mult / fact;
}

return result;
}
}

/// <summary>
/// Compute Hiperbolic Cos using Taylor Series.
/// </summary>
/// <param name="x">An angle, in radians.</param>
/// <param name="nTerms">Number of terms.</param>
/// <returns>Hiperbolic cos of the x.</returns>
public static double Cosh(double x, int nTerms)
{
if (nTerms < 2) return x;
if (nTerms == 2)
{
return 1 + (x * x) / 2D;
}
else
{

double mult = x * x;
double fact = 2;
int factS = 4;
double result = 1 + mult / fact;
for (int i = 3; i <= nTerms; i++)
{
mult *= x * x;
fact *= factS * (factS - 1);
factS += 2;
result += mult / fact;
}

return result;
}
}

/// <summary>
/// Compute Exp using Taylor Series.
/// </summary>
/// <param name="x">An angle, in radians.</param>
/// <param name="nTerms">Number of terms.</param>
/// <returns>Exp of the x.</returns>
public static double Exp(double x, int nTerms)
{
if (nTerms < 2) return 1 + x;
if (nTerms == 2)
{
return 1 + x + (x * x) / 2;
}
else
{

double mult = x * x;
double fact = 2;
double result = 1 + x + mult / fact;
for (int i = 3; i <= nTerms; i++)
{
mult *= x;
fact *= i;
result += mult / fact;
}

return result;
}
}
}
}
22 changes: 20 additions & 2 deletions Sources/Accord.Math/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ public static int Direction(IntPoint previous, IntPoint next)
return code;
}

/// <summary>
/// Gets the greatest common divisor between two integers.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <returns>The greatest common divisor.</returns>
public static int GreatestCommonDivisor(int a, int b)
{
int x = a - b * (int)Math.Floor((double)(a / b));
while (x != 0)
{
a = b;
b = x;
x = a - b * (int)Math.Floor((double)(a / b));
}
return b;
}

/// <summary>
/// Returns the next power of 2 after the input value x.
/// </summary>
Expand Down Expand Up @@ -185,8 +203,8 @@ public static int PreviousPowerOf2(int x)
/// Hypotenuse calculus without overflow/underflow
/// </summary>
///
/// <param name="a">first value</param>
/// <param name="b">second value</param>
/// <param name="a">First value</param>
/// <param name="b">Second value</param>
/// <returns>The hypotenuse Sqrt(a^2 + b^2)</returns>
///
public static double Hypotenuse(double a, double b)
Expand Down
1 change: 1 addition & 0 deletions Sources/Accord.Statistics/Accord.Statistics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<Compile Include="Analysis\Performance\ReceiverOperatingCharacteristic.cs" />
<Compile Include="Analysis\StepwiseLogisticRegressionAnalysis.cs" />
<Compile Include="Circular.cs" />
<Compile Include="Dissimilarity.cs" />
<Compile Include="Distributions\Accord.Statistics.Distributions.cs" />
<Compile Include="Distributions\Density Kernels\Accord.Statistics.Distributions.DensityKernels.cs" />
<Compile Include="Distributions\Density Kernels\IDensityKernel.cs" />
Expand Down
Loading