Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:49:17+00:00 2026-06-01T11:49:17+00:00

I know that OpenCL doesn’t have support for complex numbers, and by what I’ve

  • 0

I know that OpenCL doesn’t have support for complex numbers, and by what I’ve read this feature isn’t going to show up anytime soon.
Still, several examples make use of complex numbers in OpenCL kernels (FFT implementations for instance).

Does anybody have experience with this? What would be the “best” method to enable complex number support in OpenCL? I’d assume using a float2 to contain the real and imaginary parts, but should I write a set of macros, or are inline functions better? Does anybody know if a set of functions/macros already exists for this purpose?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:49:19+00:00Added an answer on June 1, 2026 at 11:49 am

    So, as I needed a set of functions to handle complex numbers in OpenCL I ended up implementing a set of them. Specifically i needed sum and subtraction (trivial, can be done with standard vector operations), multiplication, division, getting a complex’s modulus, argument (or angle) and square root.
    relevant wikipedia articles:
    http://en.wikipedia.org/wiki/Complex_number#Absolute_value_and_argument
    http://en.wikipedia.org/wiki/Square_root#Principal_square_root_of_a_complex_number
    This is mostly trivial, but it does take some time, so in the hope in might save someone this time, here goes:

    //2 component vector to hold the real and imaginary parts of a complex number:
    typedef float2 cfloat;
    
    #define I ((cfloat)(0.0, 1.0))
    
    
    /*
     * Return Real (Imaginary) component of complex number:
     */
    inline float  real(cfloat a){
         return a.x;
    }
    inline float  imag(cfloat a){
         return a.y;
    }
    
    /*
     * Get the modulus of a complex number (its length):
     */
    inline float cmod(cfloat a){
        return (sqrt(a.x*a.x + a.y*a.y));
    }
    
    /*
     * Get the argument of a complex number (its angle):
     * http://en.wikipedia.org/wiki/Complex_number#Absolute_value_and_argument
     */
    inline float carg(cfloat a){
        if(a.x > 0){
            return atan(a.y / a.x);
    
        }else if(a.x < 0 && a.y >= 0){
            return atan(a.y / a.x) + M_PI;
    
        }else if(a.x < 0 && a.y < 0){
            return atan(a.y / a.x) - M_PI;
    
        }else if(a.x == 0 && a.y > 0){
            return M_PI/2;
    
        }else if(a.x == 0 && a.y < 0){
            return -M_PI/2;
    
        }else{
            return 0;
        }
    }
    
    /*
     * Multiply two complex numbers:
     *
     *  a = (aReal + I*aImag)
     *  b = (bReal + I*bImag)
     *  a * b = (aReal + I*aImag) * (bReal + I*bImag)
     *        = aReal*bReal +I*aReal*bImag +I*aImag*bReal +I^2*aImag*bImag
     *        = (aReal*bReal - aImag*bImag) + I*(aReal*bImag + aImag*bReal)
     */
    inline cfloat  cmult(cfloat a, cfloat b){
        return (cfloat)( a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
    }
    
    
    /*
     * Divide two complex numbers:
     *
     *  aReal + I*aImag     (aReal + I*aImag) * (bReal - I*bImag)
     * ----------------- = ---------------------------------------
     *  bReal + I*bImag     (bReal + I*bImag) * (bReal - I*bImag)
     * 
     *        aReal*bReal - I*aReal*bImag + I*aImag*bReal - I^2*aImag*bImag
     *     = ---------------------------------------------------------------
     *            bReal^2 - I*bReal*bImag + I*bImag*bReal  -I^2*bImag^2
     * 
     *        aReal*bReal + aImag*bImag         aImag*bReal - Real*bImag 
     *     = ---------------------------- + I* --------------------------
     *            bReal^2 + bImag^2                bReal^2 + bImag^2
     * 
     */
    inline cfloat cdiv(cfloat a, cfloat b){
        return (cfloat)((a.x*b.x + a.y*b.y)/(b.x*b.x + b.y*b.y), (a.y*b.x - a.x*b.y)/(b.x*b.x + b.y*b.y));
    }
    
    
    /*
     *  Square root of complex number.
     *  Although a complex number has two square roots, numerically we will
     *  only determine one of them -the principal square root, see wikipedia
     *  for more info: 
     *  http://en.wikipedia.org/wiki/Square_root#Principal_square_root_of_a_complex_number
     */
     inline cfloat csqrt(cfloat a){
         return (cfloat)( sqrt(cmod(a)) * cos(carg(a)/2),  sqrt(cmod(a)) * sin(carg(a)/2));
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible AT ALL? I know that OpenCL doesn't support normal bitfields right
Does anyone know or have good links that explain what iPhone's event loop does
Anyone know how to create an OpenGL ES app that doesn't use the full
I have this assignment due that requires the usage of FLTK. The code is
So I'm not sure how stackoverflow friendly this question is since it doesn't have
I know that I can do something like $int = (int)99; //(int) has a
I know that default cron's behavior is to send normal and error output to
I know that you can insert multiple rows at once, is there a way
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.