Thread overview
[Issue 3690] New: Folding Complex!(Complex!T) to Complex!T
Feb 21, 2010
kennytm@gmail.com
January 08, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3690

           Summary: Folding Complex!(Complex!T) to Complex!T
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bugzilla@kyllingen.net


--- Comment #0 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2010-01-08 05:22:54 PST ---
This is a suggestion for a minor change to std.complex that can be implemented with the current DMD, by simply copy-pasting the small code snippet at the bottom.

The idea is to make Complex!(Complex!T) evaluate to just Complex!T. The
rationale is:

1. Complex!(Complex!T) doesn't make sense at all.

2. Just like the real line is a subspace of the complex plane, the complex plane is also, strictly speaking, a subspace of itself.

3. It would make it possible to write functions like:

  Complex!T sqrt(T)(T x) { ... }
  auto i = sqrt(-1);
  auto z = sqrt(i);

For such a simple example it is of course better to write ordinary function overloads, but more complicated cases quickly cause trouble for DMD's IFTI.


The real-life case that inspired me to write this request is the eigenvalues() function in SciD that calculates the eigenvalues of a matrix. It would make life simpler for me (and for the compiler) if I could write it like

  Complex!T[] eigenvalues(T)(Matrix!T m, Complex!T[] buffer=null) { ... }

and test for the complex-ness of T using static ifs inside the function body, thus reducing the need for complicated IFTI with multiple function declarations. (The problem here is when the user does not give the optional parameter.)


Here's the code to implement the feature:

// Fold Complex!(Complex!T) to Complex!T
template Complex(Num : Complex!(U, Representation.cartesian),
    Representation rep=Representation.cartesian, U)
{
    alias Complex!(U, rep) Complex;
}
template Complex(Num : Complex!(U, Representation.polar),
    Representation rep=Representation.cartesian, U)
{
    alias Complex!(U, rep) Complex;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3690


kennytm@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm@gmail.com


--- Comment #1 from kennytm@gmail.com 2010-02-21 03:22:11 PST ---
A complex number of complex number is a quaternion. (http://en.wikipedia.org/wiki/Quaternion#Quaternions_as_pairs_of_complex_numbers).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 08, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3690


Lars T. Kyllingstad <bugzilla@kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #2 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2010-06-08 02:36:42 PDT ---
Implemented in the new std.complex, included since DMD 2.044.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------