Object

spire.math

FastComplex

Related Doc: package math

Permalink

object FastComplex

FastComplex is an ugly, beautiful hack.

The basic idea is to encode two 32-bit Floats into a single 64-bit Long. The lower-32 bits are the "real" Float and the upper-32 are the "imaginary" Float.

Since we're overloading the meaning of Long, all the operations have to be defined on the FastComplex object, meaning the syntax for using this is a bit ugly. To add to the ugly beauty of the whole thing I could imagine defining implicit operators on Long like +@, -@, *@, /@, etc.

You might wonder why it's even worth doing this. The answer is that when you need to allocate an array of e.g. 10-20 million complex numbers, the GC overhead of using *any* object is HUGE. Since we can't build our own "pass-by-value" types on the JVM we are stuck doing an encoding like this.

Here are some profiling numbers for summing an array of complex numbers, timed against a concrete case class implementation using Float (in ms):

size | encoded | class 1M | 5.1 | 5.8 5M | 28.5 | 91.7 10M | 67.7 | 828.1 20M | 228.0 | 2687.0

Not bad, eh?

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. FastComplex
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def abs(d: Long): Float

    Permalink
  5. final def add(a: Long, b: Long): Long

    Permalink
  6. final def angle(d: Long): Float

    Permalink
  7. final def apply(real: Double, imag: Double): Long

    Permalink
  8. final def apply(real: Float, imag: Float): Long

    Permalink
  9. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  10. final def bits(n: Int): Float

    Permalink
    Annotations
    @inline()
  11. final def bits(n: Float): Int

    Permalink
    Annotations
    @inline()
  12. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  13. final def complexSignum(d: Long): Long

    Permalink
  14. final def conjugate(d: Long): Long

    Permalink
  15. final def decode(d: Long): (Float, Float)

    Permalink
  16. final def divide(a: Long, b: Long): Long

    Permalink
  17. final def encode(real: Float, imag: Float): Long

    Permalink
    Annotations
    @inline()
  18. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  19. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  20. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  21. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  22. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  23. final val i: Long

    Permalink
  24. final def imag(d: Long): Float

    Permalink
    Annotations
    @inline()
  25. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  26. final def isWhole(d: Long): Boolean

    Permalink
  27. final def multiply(a: Long, b: Long): Long

    Permalink
  28. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  29. final def negate(a: Long): Long

    Permalink
  30. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  31. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  32. final val one: Long

    Permalink
  33. final def polar(magnitude: Float, angle: Float): Long

    Permalink
    Annotations
    @inline()
  34. final def pow(a: Long, b: Long): Long

    Permalink
  35. final def real(d: Long): Float

    Permalink
    Annotations
    @inline()
  36. final def signum(d: Long): Int

    Permalink
  37. final def subtract(a: Long, b: Long): Long

    Permalink
  38. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  39. final def toRepr(d: Long): String

    Permalink
  40. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  41. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  42. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  43. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  44. final val zero: Long

    Permalink

Inherited from AnyRef

Inherited from Any

Ungrouped