www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3690] New: Folding Complex!(Complex!T) to Complex!T

reply d-bugmail puremagic.com writes:
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



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: -------
Jan 08 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3690


kennytm gmail.com changed:

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



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: -------
Feb 21 2010
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jun 08 2010