DotNet Example: Complex Numbers

Description

This sample demonstrates using a Class file, and also loading, registering and using a .NET class from a DLL in the standard location. It also demonstrates using a constructor to load a value into a .NET class when it is created.

Xbasic lacks native support for complex arithmetic, but the .NET Framework has it. Now you can perform calculations with complex numbers from Alpha Anywhere, using the .NET System.Numerics.Complex class.

Create and save the class file Numerics.Complex:

dim Sv as DotNet::Services
dim assy as DotNet::AssemblyReference
assy.filename = DotNetPath()+"System.Numerics.dll"
Sv.RegisterClass("Numerics", "Complex", "System.Numerics.Complex", assy)

Once you DIM a variable as Numerics::Complex, you will be able to browse the .NET class interactively:

Run the following interactive session:

dim cp as Numerics::Complex
dim cval as Numerics::Complex = new Numerics::Complex(1.0, -1.0)
?cval.ToString()
= "(1, -1)"

?Numerics::Complex.Abs(cval)
= 1.4142135623731

?cp.Abs(cval)
= 1.4142135623731

?cp.Acos(cval).ToString()
= "(0.904556894302381, 1.06127506190504)"

?cp.Multiply(cval,cval).ToString()
= "(0, -2)"

?cp.Add(cval,cval).ToString()
= "(2, -2)"
 
?cp.Conjugate(cval).ToString()
= "(1, 1)"

?cval.Real
= 1
 
?cval.Imaginary
= -1
 
?cp
= PUBLIC STATIC Abs AS N (value AS System::Numerics::Complex)  'Abs
PUBLIC STATIC Acos AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Acos
PUBLIC STATIC Add AS System::Numerics::Complex (left AS System::Numerics::Complex, right AS System::Numerics::Complex)  'Add
PUBLIC STATIC Asin AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Asin
PUBLIC STATIC Atan AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Atan
PUBLIC Complex(real AS N, imaginary AS N)  'Public Constructor
PUBLIC STATIC Conjugate AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Conjugate
PUBLIC STATIC Cos AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Cos
PUBLIC STATIC Cosh AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Cosh
PUBLIC STATIC Divide AS System::Numerics::Complex (dividend AS System::Numerics::Complex, divisor AS System::Numerics::Complex)  'Divide
PUBLIC Equals AS L (obj AS A)  'Equals
PUBLIC STATIC Exp AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Exp
PUBLIC STATIC FromPolarCoordinates AS System::Numerics::Complex (magnitude AS N, phase AS N)  'FromPolarCoordinates
PUBLIC GetHashCode AS N ()  'GetHashCode
PUBLIC GetType AS System::Type ()  'GetType
PUBLIC STATIC Log AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Log
PUBLIC STATIC Log10 AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Log10
PUBLIC STATIC Multiply AS System::Numerics::Complex (left AS System::Numerics::Complex, right AS System::Numerics::Complex)  'Multiply
PUBLIC STATIC Negate AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Negate
PUBLIC STATIC Pow AS System::Numerics::Complex (value AS System::Numerics::Complex, power AS N)  'Pow
PUBLIC STATIC Reciprocal AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Reciprocal
PUBLIC STATIC ReferenceEquals AS L (objA AS A, objB AS A)  'ReferenceEquals
PUBLIC STATIC Sin AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Sin
PUBLIC STATIC Sinh AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Sinh
PUBLIC STATIC Sqrt AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Sqrt
PUBLIC STATIC Subtract AS System::Numerics::Complex (left AS System::Numerics::Complex, right AS System::Numerics::Complex)  'Subtract
PUBLIC STATIC Tan AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Tan
PUBLIC STATIC Tanh AS System::Numerics::Complex (value AS System::Numerics::Complex)  'Tanh
PUBLIC ToString AS C ()  'ToString
Imaginary = 0
+ImaginaryOne.
Magnitude = 0
+One.
Phase = 0
Real = 0
+Zero.

See Also