• MARS Website
  • Core API
  • SmartOpenHamburg API
  • Model Components API
  • Common API
Show / Hide Table of Contents
  • Mars.Common
    • GeoHash
    • GeoHashDecoder
    • GeohashDecodeResult
    • GeoHashEncoder
    • GeoHashPrecision
    • Hyperrectangle
    • InputHashHelper
    • PositionHelper
  • Mars.Common.Collections
    • BinaryArrayHeap<T>
    • DoubleBits
    • FibonacciHeap<T, TKey>
    • FibonacciHeapDoubleKey<T>
    • FibonacciHeapNode<T, TKey>
    • FibonacciHeapNodeDoubleKey<T>
    • HeapNode
    • IntervalSize
    • K2DTree<T>
    • K2dTreeNode<T>
    • KdTree
    • KdTree<T>
    • KdTreeBase<TNode>
    • KdTreeNode
    • KdTreeNode<T>
    • KdTreeNodeBase<TNode>
    • KdTreeNodeCollection<TNode>
    • KdTreeNodeList<T>
    • Key
    • Node<T>
    • NodeBase<T>
    • NodeDataContainer<T>
    • NodeDistance<TNode>
    • QuadTree<T>
    • Root<T>
    • TreeDataContainer<T>
  • Mars.Common.Collections.CritBit
    • ICritBitTree<TValue>
  • Mars.Common.Collections.Graph
    • EdgeData
    • GraphData
    • GraphSerializer
    • ISpatialGraph
    • KeyContainer
    • NodeData
    • SpatialGraph
    • SpatialGraphHelper
  • Mars.Common.Collections.Graph.Algorithms
    • AStar
    • CompressedPathDatabase
    • ContractionSearch
    • DepthLimitedTraversal
  • Mars.Common.Collections.Graph.Helper
    • INodeFinder
    • KdTreeNodeFinder
    • RunLengthEncoder
  • Mars.Common.Collections.KNNGraph
    • DefaultRandomGenerator
    • DistanceUtils
    • EventSources
    • EventSources.GraphBuildEventSource
    • EventSources.GraphSearchEventSource
    • IProgressReporter
    • IProvideRandomValues
    • KnnGraph<TItem, TDistance>
    • KnnGraph<TItem, TDistance>.KnnSearchResult
    • KnnGraph<TItem, TDistance>.Parameters
    • Node
    • ReverseComparer<T>
    • ReverseComparerExtensions
    • SelectionKind
    • TravelingCosts<TItem, TDistance>
  • Mars.Common.Compat
    • FormatDecoderAttribute
    • FormatEncoderAttribute
    • FormatHandlerAttribute
    • IntegerAttribute
    • NegativeIntegerAttribute
    • NonnegativeIntegerAttribute
    • NonpositiveIntegerAttribute
    • PositiveIntegerAttribute
  • Mars.Common.Data
    • DomainDataImporter
  • Mars.Common.Data.Providers
    • AscDataProvider
    • GeoJsonFeatureCollectionConverter
    • GeoJsonFeatureConverter
    • GeoJsonHelper
    • GeometryDataProvider
    • GraphMlProvider
    • HttpDataProvider
    • IDataProvider<TInput>
    • JsonFileDataProvider
    • JsonTextDataProvider
    • XmlFileDataProvider
    • XmlTextDataProvider
  • Mars.Common.Exceptions
    • DimensionMismatchException
    • ParseException
  • Mars.Common.IO
    • ExtensionMethods
    • FileClientUtils
    • FileKeys
    • HttpClientUtils
    • ObjectSerialize
    • Serializer
    • SerializerCompression
    • SparseFormat
    • SparseReader
    • SparseWriter
  • Mars.Common.IO.Attributes
    • SerializationBinderAttribute
    • SurrogateSelectorAttribute
  • Mars.Common.IO.Console
    • ChildProgressBar
    • IProgressBar
    • ProgressBar
    • ProgressBarBase
    • ProgressBarHeight
    • ProgressBarOptions
    • ProgressBarSimple
  • Mars.Common.IO.Csv
    • CsvAnalyzer
    • CsvReader
    • CsvReader.RecordEnumerator
    • CsvWriter
    • MissingFieldAction
    • ParseErrorAction
    • ValueTrimmingOptions
  • Mars.Common.IO.Events
    • ParseErrorEventArgs
  • Mars.Common.IO.Exceptions
    • MalformedCsvException
    • MissingFieldCsvException
  • Mars.Common.IO.Mapped
    • Context
    • DefaultArrayFactory
    • Extensions
    • IArrayFactory
    • ISerializableToStream
    • MappedAccessor<T>
    • MemoryMap
    • MemoryMap.CreateAccessorFunc<T>
    • MemoryMap.ReadFromDelegate<T>
    • MemoryMap.WriteToDelegate<T>
    • MemoryMapDelegates
    • MemoryMapDelegates.CreateAccessorFunc<T>
    • MemoryMapStream
  • Mars.Common.IO.Mapped.Accessors
    • MappedAccessorByte
    • MappedAccessorDouble
    • MappedAccessorInt16
    • MappedAccessorInt32
    • MappedAccessorInt64
    • MappedAccessorSingle
    • MappedAccessorUInt16
    • MappedAccessorUInt32
    • MappedAccessorUInt64
    • MappedAccessorVariable<T>
  • Mars.Common.IO.Mapped.Arrays
    • Array<T>
    • ArrayBase<T>
    • ArrayProfile
    • MappedArray<TMapped, T>
    • MappedArray<TMapped, T>.MapFrom
    • MappedArray<TMapped, T>.MapTo
    • MemoryArray<T>
    • VariableArray<T>
  • Mars.Common.IO.Mapped.Collections
    • MemoryBackedDictionary<TKey, TValue>
    • MemoryBackedList<T>
  • Mars.Common.IO.Mapped.Indexes
    • Index<T>
  • Mars.Common.IO.Mapped.Streams
    • CappedStream
  • Mars.Common.Socket
    • ByteOrder
    • CloseEventArgs
    • CloseStatusCode
    • CompressionMethod
    • ErrorEventArgs
    • Ext
    • MessageEventArgs
    • WebSocket
    • WebSocketException
    • WebSocketState
  • Mars.Common.Socket.Server
    • IWebSocketSession
    • WebHeaderCollection
    • WebSocketBehavior
    • WebSocketContext
    • WebSocketServer
    • WebSocketServiceHost
    • WebSocketServiceManager
    • WebSocketSessionManager
  • Mars.Numerics
    • Classes
    • Combinatorics
    • Constants
    • Distance
    • Elementwise
    • Jagged
    • MathematicsException
    • MathHelper
    • Matrix
    • MatrixOrder
    • MatrixType
    • Norm
    • Sort
    • Sorting
    • Sparse
    • Sparse<T>
    • Tools
    • Vector
    • VectorHelper
    • VectorType
  • Mars.Numerics.Comparers
    • ArrayComparer<T>
    • ComparerDirection
    • CustomComparer<T>
    • ElementComparer
    • ElementComparer<T>
    • GeneralComparer
    • StableComparer<T>
  • Mars.Numerics.Distances
    • Angular
    • Chebyshev
    • Cosine
    • Dirac<T>
    • Euclidean
    • Hamming
    • Hamming<T>
    • Haversine
    • Jaccard
    • Jaccard<T>
    • Kulczynski
    • Levenshtein
    • Levenshtein<T>
    • Manhattan
    • Matching
    • Minkowski
    • SquareEuclidean
    • Vincenty
    • Vincenty.Ellipsoid
  • Mars.Numerics.Distances.Base
    • IDistance<T>
    • IDistance<TFirst, TSecond>
    • IMetric<T>
    • ISimilarity<T, TU>
    • ISimilarity<T>
  • Mars.Numerics.Exceptions
    • DimensionMismatchException
    • NonPositiveDefiniteMatrixException
    • SingularMatrixException
  • Mars.Numerics.Formats
    • DefaultMatrixFormatProvider
    • IMatrixFormatProvider
    • MatrixFormatProviderBase
    • MatrixFormatter
    • OctaveMatrixFormatProvider
  • Mars.Numerics.Ranges
    • ByteRange
    • DoubleRange
    • FloatRange
    • IntRange
    • IRange<T>
  • Mars.Numerics.Statistics
    • ConstValueDistribution<T>
    • Distribution<T>
    • FastGaussianDistributionD
    • FastGaussianDistributionF
    • IDistribution
    • UniformDiscreteDistribution
    • UniformDistributionD
    • UniformDistributionF
  • Mars.Numerics.Statistics.Base
    • BinarySearch
    • DistributionBase
    • ISampleableDistribution<TObservations>
    • IUnivariateDistribution
    • IUnivariateDistribution<TObservation>
    • UnivariateDiscreteDistribution

Class MathHelper

Provides useful mathematical algorithms and functions.
Inheritance
System.Object
MathHelper
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Mars.Numerics
Assembly: Mars.Numerics.dll
Syntax
public static class MathHelper

Fields

| Improve this Doc View Source

E

Defines the value of E as a System.Single.
Declaration
public const float E = 2.718282F
Field Value
Type Description
System.Single
| Improve this Doc View Source

Log10E

Defines the base-10 logarithm of E.
Declaration
public const float Log10E = 0.4342945F
Field Value
Type Description
System.Single
| Improve this Doc View Source

Log2E

Defines the base-2 logarithm of E.
Declaration
public const float Log2E = 1.442695F
Field Value
Type Description
System.Single
| Improve this Doc View Source

Pi

Defines the value of Pi as a System.Single.
Declaration
public const float Pi = 3.141593F
Field Value
Type Description
System.Single
| Improve this Doc View Source

PiOver2

Defines the value of Pi divided by two as a System.Single.
Declaration
public const float PiOver2 = 1.5707964F
Field Value
Type Description
System.Single
| Improve this Doc View Source

PiOver3

Defines the value of Pi divided by three as a System.Single.
Declaration
public const float PiOver3 = 1.047198F
Field Value
Type Description
System.Single
| Improve this Doc View Source

PiOver4

Defines the value of Pi divided by four as a System.Single.
Declaration
public const float PiOver4 = 0.7853982F
Field Value
Type Description
System.Single
| Improve this Doc View Source

PiOver6

Defines the value of Pi divided by six as a System.Single.
Declaration
public const float PiOver6 = 0.5235988F
Field Value
Type Description
System.Single
| Improve this Doc View Source

ThreePiOver2

Defines the value of Pi multiplied by 3 and divided by two as a System.Single.
Declaration
public const float ThreePiOver2 = 4.712389F
Field Value
Type Description
System.Single
| Improve this Doc View Source

ToDegreeFactorD

Gets the double precision factor to convert radian to degree
Declaration
public const double ToDegreeFactorD = 57.29577951308232
Field Value
Type Description
System.Double
| Improve this Doc View Source

ToDegreeFactorF

Gets the single precision factor to convert degree to radian
Declaration
public const float ToDegreeFactorF = 57.295776F
Field Value
Type Description
System.Single
| Improve this Doc View Source

ToRadianFactorD

Gets the double precision factor to convert degree to radian.
Declaration
public const double ToRadianFactorD = 0.01745329238474369
Field Value
Type Description
System.Double
| Improve this Doc View Source

ToRadianFactorF

Gets the single precision factor to convert degree to radian.
Declaration
public const float ToRadianFactorF = 0.017453292F
Field Value
Type Description
System.Single
| Improve this Doc View Source

TwoPi

Defines the value of Pi multiplied by two as a System.Single.
Declaration
public const float TwoPi = 6.283185F
Field Value
Type Description
System.Single

Methods

| Improve this Doc View Source

BinomialCoefficient(Int32, Int32)

Computes the binomial coefficient of (n, k), also read as "n choose k".
Declaration
public static long BinomialCoefficient(int n, int k)
Parameters
Type Name Description
System.Int32 n n, must be a value equal to or greater than 0.
System.Int32 k k, a value in the range [0, n].
Returns
Type Description
System.Int64 The binomial coefficient.
Remarks
This method returns a binomial coefficient. The result is the k'th element in the n'th row of Pascal's triangle (using zero-based indices for k and n). This method returns 0 for negative n.
| Improve this Doc View Source

Bitmask(UInt32)

Creates the smallest bitmask that is greater than or equal to the given value.
Declaration
public static uint Bitmask(uint value)
Parameters
Type Name Description
System.UInt32 value The value.
Returns
Type Description
System.UInt32 A bitmask where the left bits are 0 and the right bits are 1. The value of the bitmask is ≥ value.
Remarks

This result can also be interpreted as finding the smallest x such that 2x > value and returning 2x - 1.

Another useful application: Bitmask(x) + 1 returns the next power of 2 that is greater than x.

| Improve this Doc View Source

Clamp<T>(T, T, T)

Clamps the specified value.
Declaration
public static T Clamp<T>(T value, T min, T max)
    where T : IComparable<T>
Parameters
Type Name Description
T value The value which should be clamped.
T min The min limit.
T max The max limit.
Returns
Type Description
T value clamped to the interval [min, max].
Type Parameters
Name Description
T The type of the value.
Remarks
Values within the limits are not changed. Values exceeding the limits are cut off.
| Improve this Doc View Source

Factorial(Int32)

Calculates the factorial of a given natural number.
Declaration
public static long Factorial(int n)
Parameters
Type Name Description
System.Int32 n The number.
Returns
Type Description
System.Int64 n!
| Improve this Doc View Source

FastFloor(Double)

Returns the largest integer less than or equal to the specified decimal number.
Declaration
public static int FastFloor(double d)
Parameters
Type Name Description
System.Double d A decimal number.
Returns
Type Description
System.Int32 The largest integer less than or equal to d.
Remarks
Note that the method returns an integral value of type System.Math
| Improve this Doc View Source

Frac(Double)

Calculates the fractional part of a specified double-precision floating-point number.
Declaration
public static double Frac(double d)
Parameters
Type Name Description
System.Double d The number.
Returns
Type Description
System.Double The fractional part of d.
| Improve this Doc View Source

Frac(Single)

Calculates the fractional part of a specified single-precision floating-point number.
Declaration
public static float Frac(float f)
Parameters
Type Name Description
System.Single f The number.
Returns
Type Description
System.Single The fractional part of f.
| Improve this Doc View Source

Gaussian(Double, Double, Double, Double)

Computes the Gaussian function y = k * e^( -(x-μ)2/(2σ2) (double-precision).
Declaration
public static double Gaussian(double x, double coefficient, double expectedValue, double standardDeviation)
Parameters
Type Name Description
System.Double x The argument x.
System.Double coefficient The coefficient k.
System.Double expectedValue The expected value μ.
System.Double standardDeviation The standard deviation σ.
Returns
Type Description
System.Double The height of the Gaussian bell curve at x.
Remarks
This method computes the Gaussian bell curve.
| Improve this Doc View Source

Gaussian(Single, Single, Single, Single)

Computes the Gaussian function y = k * e^( -(x-μ)2/(2σ2) (single precision).
Declaration
public static float Gaussian(float x, float coefficient, float expectedValue, float standardDeviation)
Parameters
Type Name Description
System.Single x The argument x.
System.Single coefficient The coefficient k.
System.Single expectedValue The expected value μ.
System.Single standardDeviation The standard deviation σ.
Returns
Type Description
System.Single The height of the Gaussian bell curve at x.
Remarks
This method computes the Gaussian bell curve.
| Improve this Doc View Source

Hypotenuse(Double, Double)

Computes Sqrt(a*a + b*b) without underflow/overflow (double-precision).
Declaration
public static double Hypotenuse(double cathetusA, double cathetusB)
Parameters
Type Name Description
System.Double cathetusA Cathetus a.
System.Double cathetusB Cathetus b.
Returns
Type Description
System.Double The hypotenuse c, which is Sqrt(a*a + b*b).
| Improve this Doc View Source

Hypotenuse(Single, Single)

Computes Sqrt(a*a + b*b) without underflow/overflow (single-precision).
Declaration
public static float Hypotenuse(float cathetusA, float cathetusB)
Parameters
Type Name Description
System.Single cathetusA Cathetus a.
System.Single cathetusB Cathetus b.
Returns
Type Description
System.Single The hypotenuse c, which is Sqrt(a*a + b*b).
| Improve this Doc View Source

InverseSqrtFast(Double)

Returns an approximation of the inverse square root of left number.
Declaration
public static double InverseSqrtFast(double x)
Parameters
Type Name Description
System.Double x A number.
Returns
Type Description
System.Double An approximation of the inverse square root of the specified number, with an upper error bound of 0.001
Remarks
This is an improved implementation of the the method known as Carmack's inverse square root which is found in the Quake III source code. This implementation comes from http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see http://www.beyond3d.com/content/articles/8/
| Improve this Doc View Source

InverseSqrtFast(Single)

Returns an approximation of the inverse square root of left number.
Declaration
public static float InverseSqrtFast(float x)
Parameters
Type Name Description
System.Single x A number.
Returns
Type Description
System.Single An approximation of the inverse square root of the specified number, with an upper error bound of 0.001
Remarks
This is an improved implementation of the the method known as Carmack's inverse square root which is found in the Quake III source code. This implementation comes from http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see http://www.beyond3d.com/content/articles/8/
| Improve this Doc View Source

IsPowerOfTwo(Int32)

Determines whether the specified value is a power of two.
Declaration
public static bool IsPowerOfTwo(int value)
Parameters
Type Name Description
System.Int32 value The value.
Returns
Type Description
System.Boolean true if value is a power of two; otherwise, false.
| Improve this Doc View Source

Log2GreaterOrEqual(UInt32)

Returns the smallest non-negative integer x such that 2x ≥ value.
Declaration
public static uint Log2GreaterOrEqual(uint value)
Parameters
Type Name Description
System.UInt32 value The value.
Returns
Type Description
System.UInt32 The smallest non-negative integer x such that 2x ≥ value. Exception: If value is 0, 0 is returned.
| Improve this Doc View Source

Log2LessOrEqual(UInt32)

Returns the largest non-negative integer x such that 2x ≤ value.
Declaration
public static uint Log2LessOrEqual(uint value)
Parameters
Type Name Description
System.UInt32 value The value.
Returns
Type Description
System.UInt32 The largest non-negative integer x such that 2x ≤ value. Exception: If value is 0 then 0 is returned.
| Improve this Doc View Source

Magnitude(IList<Single>)

Calculates magnitude of the vector.
Declaration
public static float Magnitude(IList<float> vector)
Parameters
Type Name Description
System.Collections.Generic.IList<System.Single> vector The vector to calculate magnitude for.
Returns
Type Description
System.Single The magnitude.
| Improve this Doc View Source

MagnitudeSimd(Single[])

Calculates magnitude of the vector using SIMD (single instruction multiple data parallelism).
Declaration
public static float MagnitudeSimd(float[] vector)
Parameters
Type Name Description
System.Single[] vector The vector to calculate magnitude for.
Returns
Type Description
System.Single The magnitude.
| Improve this Doc View Source

NextPowerOf2(UInt32)

Returns the smallest power of two that is greater than the given value.
Declaration
public static uint NextPowerOf2(uint value)
Parameters
Type Name Description
System.UInt32 value The value.
Returns
Type Description
System.UInt32 The smallest power of two (2x) that is greater than value.
Remarks
For example, NextPowerOf2(7) is 8 and NextPowerOf2(8) is 16.
| Improve this Doc View Source

NextPowerOfTwo(Double)

Returns the next power of two that is larger than the specified number.
Declaration
public static double NextPowerOfTwo(double n)
Parameters
Type Name Description
System.Double n The specified number.
Returns
Type Description
System.Double The next power of two.
| Improve this Doc View Source

NextPowerOfTwo(Int32)

Returns the next power of two that is larger than the specified number.
Declaration
public static int NextPowerOfTwo(int n)
Parameters
Type Name Description
System.Int32 n The specified number.
Returns
Type Description
System.Int32 The next power of two.
| Improve this Doc View Source

NextPowerOfTwo(Int64)

Returns the next power of two that is larger than the specified number.
Declaration
public static long NextPowerOfTwo(long n)
Parameters
Type Name Description
System.Int64 n The specified number.
Returns
Type Description
System.Int64 The next power of two.
| Improve this Doc View Source

NextPowerOfTwo(Single)

Returns the next power of two that is larger than the specified number.
Declaration
public static float NextPowerOfTwo(float n)
Parameters
Type Name Description
System.Single n The specified number.
Returns
Type Description
System.Single The next power of two.
| Improve this Doc View Source

NormalizeSimd(Single[])

Calculates normalization of the vector using SIMD (single instruction multiple data).
Declaration
public static void NormalizeSimd(float[] vector)
Parameters
Type Name Description
System.Single[] vector The vector to calculate magnitude for.
| Improve this Doc View Source

Swap(ref Double, ref Double)

Swaps two double values.
Declaration
public static void Swap(ref double a, ref double b)
Parameters
Type Name Description
System.Double a The first value.
System.Double b The second value.
| Improve this Doc View Source

Swap(ref Single, ref Single)

Swaps two float values.
Declaration
public static void Swap(ref float a, ref float b)
Parameters
Type Name Description
System.Single a The first value.
System.Single b The second value.
| Improve this Doc View Source

Swap<T>(ref T, ref T)

Swaps the content of two variables.
Declaration
public static void Swap<T>(ref T obj1, ref T obj2)
Parameters
Type Name Description
T obj1 First variable.
T obj2 Second variable.
Type Parameters
Name Description
T The type of the objects.
| Improve this Doc View Source

ToBearing(Double)

Gets the compass-bearing in degree for the specified radian
Declaration
public static double ToBearing(double radian)
Parameters
Type Name Description
System.Double radian The radian, getting the bearing.
Returns
Type Description
System.Double The compass-bearing
| Improve this Doc View Source

ToDegrees(Double)

Converts an angle value from radians to degrees (double-precision).
Declaration
public static double ToDegrees(double radians)
Parameters
Type Name Description
System.Double radians The angle in radians.
Returns
Type Description
System.Double The angle in degrees.
| Improve this Doc View Source

ToDegrees(Single)

Converts an angle value from radians to degrees (single-precision).
Declaration
public static float ToDegrees(float radians)
Parameters
Type Name Description
System.Single radians The angle in radians.
Returns
Type Description
System.Single The angle in degrees.
| Improve this Doc View Source

ToRadians(Double)

Converts an angle value from degrees to radians (double-precision).
Declaration
public static double ToRadians(double degree)
Parameters
Type Name Description
System.Double degree The angle in degrees.
Returns
Type Description
System.Double The angle in radians.
| Improve this Doc View Source

ToRadians(Single)

Converts an angle value from degrees to radians (single-precision).
Declaration
public static float ToRadians(float degree)
Parameters
Type Name Description
System.Single degree The angle in degrees.
Returns
Type Description
System.Single The angle in radians.
  • Improve this Doc
  • View Source
In This Article
Back to top Copyright © MARS GROUP. HAW Hamburg