Skip to content

Commit

Permalink
Merge pull request #5 from Paramdigma/feature/gradientDescent-tests
Browse files Browse the repository at this point in the history
Feature/gradient descent tests
  • Loading branch information
AlanRynne authored Jun 11, 2020
2 parents b5fff64 + 304ddf2 commit 4c27ff3
Show file tree
Hide file tree
Showing 14 changed files with 381 additions and 109 deletions.
35 changes: 35 additions & 0 deletions src/Geometry/3D/Circle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Paramdigma.Core.Geometry.Interfaces;

namespace Paramdigma.Core.Geometry
{
public class Circle : ICurve
{
public Plane Plane;
public double Radius;

public Circle(Plane plane, double radius)
{
this.Plane = plane;
this.Radius = radius;
}

public Point3d PointAt(double t)
{
var radians = t * 2 * Math.PI;
var x = this.Radius * Math.Cos(radians);
var y = this.Radius * Math.Sin(radians);
return Plane.PointAt(x, y, 0);
}

public Vector3d TangentAt(double t) => NormalAt(t).Cross(this.Plane.ZAxis);

public Vector3d NormalAt(double t) => (this.Plane.Origin - PointAt(t)).Unit();

public Vector3d BinormalAt(double t) => TangentAt(t).Cross(NormalAt(t));


public Plane FrameAt(double t) => new Plane(PointAt(t), NormalAt(t), BinormalAt(t), TangentAt(t));

}
}
30 changes: 30 additions & 0 deletions src/Geometry/3D/Plane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,35 @@ public double[] GetPlaneEquation()
/// </summary>
/// <returns>Plane clone.</returns>
public Plane Clone() => new Plane(new Point3d(Origin), new Vector3d(XAxis), new Vector3d(YAxis), new Vector3d(ZAxis));

public override bool Equals(object obj)
{
if (!(obj is Plane))
{
return false;
}

var plane = (Plane)obj;
return plane.Origin == this.Origin
&& plane.XAxis == this.XAxis
&& plane.YAxis == this.YAxis;
}

/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
// Choose large primes to avoid hashing collisions
const int hashingBase = (int)2166136261;
const int hashingMultiplier = 16777619;

int hash = hashingBase;
hash = (hash * hashingMultiplier) ^ this.Origin.GetHashCode();
hash = (hash * hashingMultiplier) ^ this.XAxis.GetHashCode();
hash = (hash * hashingMultiplier) ^ this.YAxis.GetHashCode();
return hash;
}
}
}
}
26 changes: 10 additions & 16 deletions src/Geometry/3D/Point3d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public class Point3d : BasePoint
/// </summary>
/// <returns><see cref="Point3d"/>.</returns>
public Point3d()
: base()
{
}
: base() { }

/// <summary>
/// Initializes a new instance of the <see cref="Point3d"/> class by cartesian coordinates.
Expand All @@ -24,39 +22,31 @@ public Point3d()
/// <param name="zCoord">Z coordinate.</param>
/// <returns></returns>
public Point3d(double xCoord, double yCoord, double zCoord)
: base(xCoord, yCoord, zCoord)
{
}
: base(xCoord, yCoord, zCoord) { }

/// <summary>
/// Initializes a new instance of the <see cref="Point3d"/> class from a 2-dimensional point.
/// </summary>
/// <param name="point">2d point to convert.</param>
/// <returns><see cref="Point3d"/>.</returns>
public Point3d(Point2d point)
: base(point.X, point.Y, 0)
{
}
: base(point.X, point.Y, 0) { }

/// <summary>
/// Initializes a new instance of the <see cref="Point3d"/> class.
/// </summary>
/// <param name="point">3d point to copy.</param>
/// <returns><see cref="Point3d"/>.</returns>
public Point3d(Point3d point)
: base(point)
{
}
: base(point) { }

/// <summary>
/// Initializes a new instance of the <see cref="Point3d"/> class from a 4-dimensional point by dividing the cartesian coordinates by the weight.
/// </summary>
/// <param name="point">4d point to convert.</param>
/// <returns><see cref="Point3d"/>.</returns>
public Point3d(Point4d point)
: this(point.X / point.Weight, point.Y / point.Weight, point.Z / point.Weight)
{
}
: this(point.X / point.Weight, point.Y / point.Weight, point.Z / point.Weight) { }

/// <summary>
/// Gets a new Unset point.
Expand Down Expand Up @@ -121,7 +111,11 @@ public Point3d(Point4d point)
/// </summary>
/// <param name="point">Point.</param>
/// <returns><see cref="Point3d"/>.</returns>
public static Point3d operator -(Point3d point) => new Point3d(-point.X, -point.Y, -point.Z);
public static Point3d operator -(Point3d point)
=> new Point3d(
point.X != 0 ? -point.X : 0,
point.Y != 0 ? -point.Y : 0,
point.Z != 0 ? -point.Z : 0);

/// <summary>
/// Multiplies a point with a number.
Expand Down
26 changes: 11 additions & 15 deletions src/Geometry/3D/Vector3d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,22 @@ public class Vector3d : BasePoint
/// Initializes a new instance of the <see cref="Vector3d"/> class.
/// </summary>
public Vector3d()
: base()
{
}
: base() { }

/// <summary>
/// Initializes a new instance of the <see cref="Vector3d"/> class with the same values as the provided vector.
/// </summary>
/// <param name="vector">Vector to copy values from.</param>
public Vector3d(Vector3d vector)
: base(vector)
{
}
: base(vector) { }

/// <summary>
/// Initializes a new instance of the <see cref="Vector3d"/> class from a point.
/// Initializes a new instance of the <see cref="Vector3d"/> class from a v.
/// </summary>
/// <param name="point">Point to copy values from.</param>
/// <returns>New vector with the same coordinate values as the given point.</returns>
/// <returns>New vector with the same coordinate values as the given v.</returns>
public Vector3d(Point3d point)
: base(point)
{
}
: base(point) { }

/// <summary>
/// Initializes a new instance of the <see cref="Vector3d"/> class given it's 3 coordinates.
Expand All @@ -42,9 +36,7 @@ public Vector3d(Point3d point)
/// <param name="zCoord">Z coordinate.</param>
/// <returns>Vector entity with the specified coordinate values.</returns>
public Vector3d(double xCoord, double yCoord, double zCoord)
: base(xCoord, yCoord, zCoord)
{
}
: base(xCoord, yCoord, zCoord) { }

/// <summary>
/// Gets the Euclidean length squared of this vector.
Expand Down Expand Up @@ -188,7 +180,11 @@ public static double Angle(Vector3d u, Vector3d v)
/// </summary>
/// <param name="v">Vector to negate.</param>
/// <returns>New vector entity with all the values negated.</returns>
public static Vector3d operator -(Vector3d v) => new Vector3d(-v.X, -v.Y, -v.Z);
public static Vector3d operator -(Vector3d v)
=> new Vector3d(
v.X != 0 ? -v.X : 0,
v.Y != 0 ? -v.Y : 0,
v.Z != 0 ? -v.Z : 0);

/// <summary>
/// Divide a vector by a number.
Expand Down
8 changes: 4 additions & 4 deletions src/Geometry/Base/BasePoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ public void Divide(double scalar)
/// </summary>
public void Negate()
{
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
this.x = this.x != 0 ? -this.x : 0;
this.y = this.y != 0 ? -this.y : 0;
this.z = this.z != 0 ? -this.z : 0;
}

/// <summary>
Expand All @@ -183,7 +183,7 @@ public void Negate()
/// </summary>
/// <param name="obj">Object to compare to.</param>
/// <returns>Returns a copy of this BasePoint instance.</returns>

/// <inheritdoc/>
public override bool Equals(object obj)
{
Expand Down
28 changes: 1 addition & 27 deletions src/Optimization/GradientDescent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class GradientDescent
/// Run gradient descent algorithm.
/// </summary>
/// <param name="function">Delegate function to compute the fitness.</param>
/// <param name="inputValues">Input values to compute the fitnessfrom.</param>
/// <param name="inputValues">Input values to compute the fitness from.</param>
public void Minimize(FitnessFunction function, List<double> inputValues)
{
Result.Values = inputValues;
Expand Down Expand Up @@ -122,32 +122,6 @@ private double ComputePartialDerivative(
return partialDerivative;
}

private double Compute5PointPartialDerivative(
int inputIndex,
FitnessFunction func,
List<double> inputValues,
double step)
{
double partialDerivative, hPos, hNeg, hPos2, hNeg2;

// Compute 2-point errors
inputValues[inputIndex] += step;
hPos = func(inputValues);
inputValues[inputIndex] += step;
hPos2 = func(inputValues);
inputValues[inputIndex] -= 3 * step;
hNeg = func(inputValues);
inputValues[inputIndex] -= step;
hNeg2 = func(inputValues);

inputValues[inputIndex] += 2 * step; // Reset value to original

// Compute partial derivative using 5-point method
partialDerivative = (-hPos2 + (8 * hPos) - (8 * hNeg) + hNeg2) / (12 * step);

return partialDerivative;
}

/// <summary>
/// Initializes a new instance of the <see cref="GradientDescent"/> class with given options.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Optimization/GradientDescentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Paramdigma.Core.Optimization
/// <summary>
/// Contains the different options of a Gradient Descent minimization.
/// </summary>
public class GradientDescentOptions
public struct GradientDescentOptions
{
/// <summary>
/// Threshold to stop minimization.
Expand Down Expand Up @@ -31,7 +31,7 @@ public class GradientDescentOptions
public double ErrorThreshold;

/// <summary>
/// Initializes a new instance of the <see cref="GradientDescentOptions"/> class given an existing one.
/// Initializes a new instance of the <see cref="GradientDescentOptions"/> struct given an existing one.
/// </summary>
/// <param name="options">Options to duplicate.</param>
public GradientDescentOptions(GradientDescentOptions options)
Expand All @@ -46,7 +46,7 @@ public GradientDescentOptions(GradientDescentOptions options)
// TODO: Fill in this fields!

/// <summary>
/// Initializes a new instance of the <see cref="GradientDescentOptions"/> class given all it's values individually.
/// Initializes a new instance of the <see cref="GradientDescentOptions"/> struct given all it's values individually.
/// </summary>
/// <param name="threshold"></param>
/// <param name="maxIterations"></param>
Expand Down
Loading

0 comments on commit 4c27ff3

Please sign in to comment.