Extreme Optimization > QuickStart Samples > Complex Numbers QuickStart Sample (MC++)

Extreme Optimization QuickStart Samples

Complex Numbers QuickStart Sample (Managed C++)

Illustrates the use of the DoubleComplex structure for representing complex numbers in Managed Extensions for C++.

C# code VB.NET code Back to QuickStart Samples

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>

// Illustrates the use of the DoubleComplex class.

using namespace System;
// The DoubleComplex class resides in the Extreme.Mathematics namespace.
using namespace Extreme::Mathematics;

int _tmain()
{
    //
    // DoubleComplex constants:
    //
    Console::WriteLine("DoubleComplex::Zero = {0}", __box(DoubleComplex::Zero));
    Console::WriteLine("DoubleComplex::One = {0}", __box(DoubleComplex::One));
    // The imaginary unit is given by DoubleComplex::I:
    Console::WriteLine("DoubleComplex::I = {0}", __box(DoubleComplex::I));
    Console::WriteLine();

    //
    // Construct some complex numbers
    //
    // Real and imaginary parts:
    //   a = 2 + 4i
    DoubleComplex a = DoubleComplex(2, 4);
    Console::WriteLine("a = {0}", __box(a));
    //   b = 1 - 3i
    DoubleComplex b = DoubleComplex(1, -3);
    Console::WriteLine("b = {0}", __box(b));
    // From a real number:
    //   c = -3 + 0i
    DoubleComplex c = DoubleComplex(-3);
    Console::WriteLine("c = {0}", __box(c));
    // Polar form:
    //   d = 2 (cos(Pi/3) + i sin(Pi/3))
    DoubleComplex d = DoubleComplex(2, Constants::Pi/3, true);
    // To print this number, use the overloaded ToString
    // method and specify the format string for the real 
    // and imaginary parts:
    Console::WriteLine("d = {0}", __box(d));
    Console::WriteLine();

    //
    // Parts of complex numbers
    //
    Console::WriteLine("Parts of a = {0}:", __box(a));
    Console::WriteLine("Real part of a = {0}", __box(a.Re));
    Console::WriteLine("Imaginary part of a = {0}", __box(a.Im));
    Console::WriteLine("Modulus of a = {0}", __box(a.Modulus));
    Console::WriteLine("Argument of a = {0}", __box(a.Argument));
    Console::WriteLine();

    //
    // Basic arithmetic:
    //
    Console::WriteLine("Basic arithmetic:");
    DoubleComplex e = -a;
    Console::WriteLine("-a = {0}", __box(e));
    e = a + b;
    Console::WriteLine("a + b = {0}", __box(e));
    e = a - b;
    Console::WriteLine("a - b = {0}", __box(e));
    e = a * b;
    Console::WriteLine("a * b = {0}", __box(e));
    e = a / b;
    Console::WriteLine("a / b = {0}", __box(e));
    // The conjugate of a complex number corresponds to
    // the "Conjugate" method:
    e = a.Conjugate();
    Console::WriteLine("Conjugate(a) = ~a = {0}", __box(e));
    Console::WriteLine();

    //
    // Functions of complex numbers
    //
    // Most of these have corresponding static methods 
    // in the System.Math class, but are extended to complex 
    // arguments.
    Console::WriteLine("Functions of complex numbers:");

    // Exponentials and logarithms
    Console::WriteLine("Exponentials and logarithms:");
    e = DoubleComplex::Exp(a);
    Console::WriteLine("Exp(a) = {0}", __box(e));
    e = DoubleComplex::Log(a);
    Console::WriteLine("Log(a) = {0}", __box(e));
    e = DoubleComplex::Log10(a);
    Console::WriteLine("Log10(a) = {0}", __box(e));
    // You can get a point on the unit circle by calling
    // the ExpI method:
    e = DoubleComplex::ExpI(2*Constants::Pi/3);
    Console::WriteLine("ExpI(2*Pi/3) = {0}", __box(e));
    // The RootOfUnity method also returns points on the
    // unit circle. The above is equivalent to the second
    // root of z^6 = 1:
    e = DoubleComplex::RootOfUnity(6, 2);
    Console::WriteLine("RootOfUnity(6, 2) = {0}", __box(e));


    // The Pow method is overloaded for integer, double,
    // and complex argument:
    e = DoubleComplex::Pow(a, 3);
    Console::WriteLine("Pow(a,3) = {0}", __box(e));
    e = DoubleComplex::Pow(a, 1.5);
    Console::WriteLine("Pow(a,1.5) = {0}", __box(e));
    e = DoubleComplex::Pow(a, b);
    Console::WriteLine("Pow(a,b) = {0}", __box(e));

    // Square root
    e = DoubleComplex::Sqrt(a);
    Console::WriteLine("Sqrt(a) = {0}", __box(e));
    // The Sqrt method is overloaded. Here's the square 
    // root of a negative double:
    e = DoubleComplex::Sqrt(-4);
    Console::WriteLine("Sqrt(-4) = {0}", __box(e));
    Console::WriteLine();

    //
    // Trigonometric functions:
    //
    Console::WriteLine("Trigonometric function:");
    e = DoubleComplex::Sin(a);
    Console::WriteLine("Sin(a) = {0}", __box(e));
    e = DoubleComplex::Cos(a);
    Console::WriteLine("Cos(a) = {0}", __box(e));
    e = DoubleComplex::Tan(a);
    Console::WriteLine("Tan(a) = {0}", __box(e));

    // Inverse Trigonometric functions:
    e = DoubleComplex::Asin(a);
    Console::WriteLine("Asin(a) = {0}", __box(e));
    e = DoubleComplex::Acos(a);
    Console::WriteLine("Acos(a) = {0}", __box(e));
    e = DoubleComplex::Atan(a);
    Console::WriteLine("Atan(a) = {0}", __box(e));

    // Asin and Acos have overloads with real argument
    // not restricted to [-1,1]:
    e = DoubleComplex::Asin(2);
    Console::WriteLine("Asin(2) = {0}", __box(e));
    e = DoubleComplex::Acos(2);
    Console::WriteLine("Acos(2) = {0}", __box(e));
    Console::WriteLine();
    
    //
    // Hyperbolic and inverse hyperbolic functions:
    //
    Console::WriteLine("Hyperbolic function:");
    e = DoubleComplex::Sinh(a);
    Console::WriteLine("Sinh(a) = {0}", __box(e));
    e = DoubleComplex::Cosh(a);
    Console::WriteLine("Cosh(a) = {0}", __box(e));
    e = DoubleComplex::Tanh(a);
    Console::WriteLine("Tanh(a) = {0}", __box(e));
    e = DoubleComplex::Asinh(a);
    Console::WriteLine("Asinh(a) = {0}", __box(e));
    e = DoubleComplex::Acosh(a);
    Console::WriteLine("Acosh(a) = {0}", __box(e));
    e = DoubleComplex::Atanh(a);
    Console::WriteLine("Atanh(a) = {0}", __box(e));
    Console::WriteLine();

    Console::Write("Press Enter key to exit...");
    Console::ReadLine();
    return 0;
}
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army