Class NumericTypeExtensions
- Namespace
- AiDotNet.Extensions
- Assembly
- AiDotNet.dll
Provides extension methods for working with numeric types in AI and machine learning contexts.
public static class NumericTypeExtensions
- Inheritance
-
NumericTypeExtensions
- Inherited Members
Remarks
For Beginners: This class contains helper methods that let you check what kind of numbers you're working with and convert between different number formats. In AI and machine learning, we often need to work with both regular numbers (like 1, 2.5, -3) and complex numbers (which have both a real and imaginary part, like 3+2i).
Methods
IsComplexType<T>()
Determines whether a type represents a complex number.
public static bool IsComplexType<T>()
Returns
- bool
True if the type is a complex numeric type; otherwise, false.
Type Parameters
TThe type to check.
Remarks
For Beginners: Complex numbers are numbers that have two parts: a real part and an imaginary part. The imaginary part involves the square root of -1, which is represented by the symbol 'i' in mathematics.
For example, 3+2i is a complex number where 3 is the real part and 2i is the imaginary part. Complex numbers are important in many areas of science and engineering, including signal processing, electrical engineering, quantum physics, and certain AI algorithms.
This method checks if the data type you're using represents a complex number in our library.
IsRealType<T>()
Determines whether a type represents a real number.
public static bool IsRealType<T>()
Returns
- bool
True if the type is a real numeric type; otherwise, false.
Type Parameters
TThe type to check.
Remarks
For Beginners: Real numbers are the kind of numbers you're familiar with from everyday life - whole numbers (like 1, 2, 3), fractions (like 0.5), and negative numbers (like -10). This method checks if the data type you're using represents these kinds of numbers.
In programming, there are several types that can represent real numbers with different ranges and precision:
- int, long, short: Whole numbers only
- float, double, decimal: Can represent fractions
- byte, sbyte, uint, ulong, ushort: Various specialized number types
ToRealOrComplex<T>(Complex<T>)
Converts a complex number to either its real part or keeps it as a complex number, depending on the target type.
public static T ToRealOrComplex<T>(this Complex<T> complex)
Parameters
complexComplex<T>The complex number to convert.
Returns
- T
If T is a real type, returns the real part of the complex number. If T is a complex type, returns the complex number itself.
Type Parameters
TThe target type for conversion.
Remarks
For Beginners: This method helps you convert between complex numbers and regular (real) numbers.
If you're trying to convert to a real number type (like double or int), this method will extract just the real part of the complex number and discard the imaginary part. For example, if you have the complex number 3+2i and convert it to a double, you'll get just 3.
If you're converting to another complex number type, it will keep both the real and imaginary parts.
This is useful when you have algorithms that can work with either real or complex numbers, and you need to ensure the output is in the correct format.
Exceptions
- InvalidOperationException
Thrown when T is neither a real nor a complex type.