Skip to content

FFI-compatible std complex numbers #707

@SciMind2460

Description

@SciMind2460

Proposal

Problem statement

Currently, there is no stable counterpart to the C _Complex types, and a myriad of implementations exist on crates.io. Popular crates like num-complex exist, but more often there are implementations defined by the crates themselves, which, while compatible with the other types in having the exact same implementation, require a myriad of conversion functions to convert with each other. Additionally, there is no syntax supported like 1+2i or 1+2j for complex numbers. (This ACP doesn't cover this, but it does lay ground for future work in this area)

Motivating examples or use cases

A scientific computing library's functions (can be written in C):

// in comp
extern double _Complex computes_function(x: double _Complex);

can be represented in Rust without the need of complex structs and the like:

extern "C" {
  fn computes_function(x: c64) -> c64;
}
fn main() {
  let returned_value = computes_function(c32::new(3, 4))
}

with the special repr that only the standard library can provide.
(This would, however, require that the complex numbers become lang items.)

Solution sketch

Complex numbers will have a implementation similar to this:

// c16, c32, c64, c128 to be defined this way using f16, f32, f64 and f128 respectively
#[lang = "complex"] // to match C
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Default)]
pub struct Complex<T>(Simd<T, 2>) where T: SimdElement + Float>> // can use the num_traits float; 

type c16 = Complex<f16>;
type c32 = Complex<f32>;
type c64 = Complex<f64>;
type c128 = Complex<f128>;

// smorgasbord of impls
impl<T> Complex<T> {
    fn re(self) ->  {
        self.0.into()[0]
    }
    fn im(self) {
        self.0.into()[1]
    }

    fn csin(self) {}
    fn ccos(self) {}
    fn ctan(self) {}
}

impl<T> Add for Complex<T> {}
impl<T> Sub for Complex<T> {}
impl<T> Mul for Complex<T> {}
impl<T> Div for Complex<T> {}

The complex numbers will serve as structs with SIMD reprs.

Alternatives

Don't put it into the std (status quo)

We could just not put complex numbers in the std and let the popular libraries work. This would be acceptable if not for improving FFI with C, as it already implements the _Complex types and adding them to Rust would significantly improve the FFI experience with libraries already in C. I do not recommend this yet.

Links and related work

Standard complex number in std library
C compatible complex types using traits

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions