Geeks With Blogs
Josh Reuben

 

"Thank you for your inquiry. In a nutshell: our types cover more functionality, while the API is very similar for BigInteger and somewhat less similar for complex numbers.

Complex numbers have been part of our library from day one in early 2005. Although the API is similar to Microsoft's, there are quite a lot of differences. We actually considered replacing our complex type with the .NET 4.0 one, but decided against it for a number of reasons. One is backward compatibility of our library. Another is that we don't entirely trust Microsoft's implementation.

Here's an example of why. As you know, the imaginary unit i is defined to be the square root of -1. If you evaluate     System.Numerics.Complex.ImaginaryOne == System.Numerics.Complex.Sqrt(-1)

the result is false! The cause of this remarkable fact is the way complex square roots are calculated. One of the basic rules of numerical computing – give exact results whenever it is reasonable to do so – is violated here.

The BigInteger type is more recent, and we actually had the opportunity to synchronize our API with the BigInteger that shipped with a beta of .NET 3.5. In this case we cover almost the complete API, and throw in some extras like Factorial and IsProbablePrime. The .NET 4.0 BigInteger was actually inherited from Microsoft Solver Foundation where it is used to do some calculations in exact arithmetic. The numbers they encounter there were never very large, so they didn't design the type for very large numbers. As a result, Microsoft's implementation is too slow. For example, multiplication is O(n^2) at best, as is ToString.

This is why we decided to stick with our implementation."

 

DoubleComplex Class

 

  • EOMathLib provides value types for single and double precision complex numbers.
  • A constructor takes two arguments: the real and imaginary parts of the complex number.

var c = new DoubleComplex(2, 4);

  • A third constructor argument: a boolean parameter named polar. If polar is true, the first two arguments are treated as the modulus and argument of the complex number.

var c = new DoubleComplex(2, Constants.Pi/5, true);

  • the static RootOfUnity method constructs a complex number that is one of the solutions to the equation xn = 1. The solutions to this equation are n complex numbers spaced equally on the unit circle. The first parameter of the method is the degree, n, of the root. The second parameter is the index of the root in the series, counting counter-clockwise on the unit circle. An index of 0 corresponds to the root x = 1.
  • Complex constants - I , Zero, One , Infinity ( the result of dividing any non-zero complex number by zero), Nan( result of dividing zero by zero).
  • Working with complex numbers - The Re and Im properties represent the real and the imaginary parts. The Modulus property returns the square root of the sum of the squares of the real and imaginary components. The Argument property returns the angle between the positive real axis and a line from the origin to the complex number, measured counter-clockwise.
  • Arithmetic operations:

var a = DoubleComplex(1, 2);

var b = DoubleComplex(-3, 4);

var c = 2 - 1 / (a + b);

  • The static Conjugate() method returns the conjugate of a complex number as a new instance. ConjugateMultiply method returns the product of the conjugate of 2 complex numbers.
  • Because the complex numbers don't have a natural ordering, only equality and inequality operators are available. No other comparison operators are possible.
  • Static methods are defined for the most common mathematical functions of complex numbers, including logarithmic, exponential, trigonometric and hyperbolic functions. Most elementary functions have been extended to cover the entire complex plane, whilst accounting for periodic functions that are multi-valued, and functions which have singularities (returning the limit from above or below).

 

BigInteger

  • Arbitrary Precision Integers -
  • The largest integer that can be represented using the built-in .NET types is 296-1, using the Decimal type. Many applications, like cryptography, require much larger numbers. BigInteger can represent integers with over 20 billion decimal digits. In practice, the size of numbers is limited by available memory.
  • Constructing big integers:

var a = new BigInteger(12345);

var b = new BigInteger(1e+100);

  • Big integers can also be created from a Byte array, or a text string using TryParse
  • ConstantsZero, One, MinusOne,
  • Like strings, big integers are immutable.
  • Arithmetic operations - provides methods for all basic arithmetic operators:

var a = BigInteger.Parse("650984076498398479473");

var b = BigInteger.Parse("49739876698723097627652");

var c = 2 - 3 * (a + b);

  • Modular arithmetic - In addition to the common arithmetic operations, the BigInteger type supports modular operations, where the result is the remainder of the result of the original operation after division by another integer, the modulus. ModularInverse(BigInteger, BigInteger) returns the unique number that, when multiplied by the original modulo the same modulus, results in 1.The ModularPow(BigInteger, BigInteger, BigInteger) method uses an efficient algorithm to perform exponention. If the modular inverse does not exist, an exception is thrown. The example below computes 16 to the power M17-1 modulo M17, where M17 is the 17th Mersenne prime number (22281-1).

BigInteger M17 = BigInteger.Pow(2, 2281) - 1;

BigInteger result = BigInteger.ModularPow(17, M17 - 1, M17);

  • Numerical properties: IsZero, IsOne, IsEven, IsOdd, IsPowerOfTwo (Tests whether the number is an exact power of two)
  • Numerical Functions: Abs, Max, Min, GreatestCommonDivisor, LeastCommonMultiple, Sqrt, Pow, Factorial
  • Other mathematical functions - BitCount property returns the total number of bits in a big integer. the DecimalDigitCount property returns an approximation of the number of decimal digits.

 

BigRational

  • Arbitrary Precision Rationals - A rational number is a number that can be expressed as a ratio of two integers. The numerator and the denominator are arbitrary size integers. The internal representation of rational numbers is always normalized and the number 0 is represented as 0/1.
  • Constructing rational numbers - constructor takes two arguments: two BigInteger values that represent the numerator and the denominator.

var x = new BigRational(1, 2);

  • ConstantsZero, One, MinusOne
  • Working with rational numbers – use the following properties: Numerator, Denominator, Sign
  • Details of rational arithmetic -, if one of the operands is a BigRational, and the other operand is an integer of any size (including BigInteger), then the integer operand is converted to BigRational, and, if the operation produces a numerical result, the type of the result is BigRational. Operations with non-integer types, including, Decimal, are not supported.
  • Numerical Functions: Abs, Max, Min, Floor, Ceiling, Round

 

BigFloat

  • First – read this: http://floating-point-gui.de/ !!!
  • Arbitrary Precision Floating-Point Numbers - the BigFloat range of numbers that can be represented is from aprox -10646,000,000 to 10646,000,000. The precision can be up to about 20 billion digits.
  • Accuracy and precision - The accuracy of a number is a measure of how close an approximation is to its actual value. The precision of a number is a measure of the amount of memory used to represent a value. Floating-point numbers are stored in the form mantissa and exponent (both are integers). The AccuracyGoal structure is used to specify the desired accuracy of a calculation. This structure has two special values. InheritAbsolute indicates that the result should be computed with the same number of digits after the decimal point as the arguments. InheritRelative indicates that the result should be computed with the same total number of digits as the arguments. For example, 1.57 has three digits total and two after the decimal point. Computing Tan(1.57) with accuracy goal InheritAbsolute would result in 1255.77. With accuracy goal InheritRelative , the result would be 1.26e+003.
  • Rounding - When the precision of a number is reduced, the RoundingMode enumeration lists the possibilities: TowardsNearest , TowardsNegativeInfinity , TowardsPositiveInfinity    , TowardsZero
  • Constructing big floating-point numbers - specify 32 and 64 bit integers, single or double-precision numbers, BigInteger values and BigRational values. two additional arguments: a AccuracyGoal value that specifies the desired accuracy of the approximation, and a RoundingMode value that specifies how to round the final approximation.

var f = new BigFloat(new BigRational(22, 7), AccuracyGoal.Absolute(50));

  • Floating-point constants : Zero         , One         , MinusOne     , MaxValue     , MinValue     , PositiveInfinity     , NegativeInfinity , NaN         
  • Working with floating-point numbers - big floats are immutable. The precision of BigFloat values is not a constant but depends on how it was constructed or computed. When two or more operands are involved, the precision is the smaller of the precisions of its argument. An important exception is addition and subtraction, which are calculated to be accurate within the smaller absolute accuracy of the operands. Every computational method has an overload to set non-default accuracy goal and rounding mode. When the result is too large / small to be represented, the value PositiveInfinity or NegativeInfinity is returned.
  • Miscellanious functions of floating-point numbers: Abs, CopySign, Floor, Ceiling, FractionalPart, Round, ScaleByPowerOfTwo, IsPositiveInfinity, IsNegativeInfinity, IsNaN

 

Common Function Support

  • BigInteger, BigFloat and DoubleComplex all support the following functions:
  • Logarithmic and exponential functions : Exp, RootOfUnity, Sqrt, Pow, Log, Log10
  • Trigonometric functions: Sin, Cos, Tan, ASin, ACos, ATan
  • Hyperbolic functions: Sinh, Cosh, Tanh, ASinh, ACosh, ATanh
Posted on Friday, January 21, 2011 6:19 PM Numerical Computing | Back to top


Comments on this post: Extreme Optimization – ValueType structs

# re: Extreme Optimization – ValueType structs
Requesting Gravatar...
It is something difficult to understand, yet it provides relevant information. - Antiquities of California
Left by James Washington on Dec 29, 2016 4:59 AM

Your comment:
 (will show your gravatar)


Copyright © JoshReuben | Powered by: GeeksWithBlogs.net