src/pylib/builtins/complex

Search:
Group by:
Source   Edit  

builtins.complex and its operators/methods.

Use toNimComplex and pycomplex to convert between PyComplex and Complex

Example:

import src/pylib/builtins/complex
assert complex(1, 3) == complex("1.0+3.0J")
assert complex(1, 3) == 1 + 3.J

# complex only stores floats, not int,
# just as Python's
assert not (type(complex(1, 2).real) is int)

Types

PyComplex = PyTComplex[float]
Source   Edit  
PyTComplex[T] = distinct Complex[T]
generics version of PyComplex Source   Edit  

Procs

func `$`(z: PyTComplex): string
Source   Edit  
func `'j`(s: static string): PyComplex
1+3'j or 1+3'J
Note: Nim disallows custom suffixes without '. Therefore, something like 1+3j is not not allowed.

Consider using complex or j instead, which are totaly Python syntax compatiable.

Example:

assert 1+3'j == 1.0+3.0'J
Source   Edit  
func `**=`[T](self: var PyTComplex[T];
              x: ComplexPowSecondParamType[T] | SomeInteger)
Source   Edit  
func abs[T](z: PyTComplex[T]): T
builtins.abs for complex Source   Edit  
func complex(s: string): PyComplex {....raises: [ValueError], tags: [], forbids: [].}
Source   Edit  
func complex[T: SomeFloat](real: T = 0.0; imag: T = 0.0): PyTComplex[T]
Source   Edit  
func complex[T: SomeInteger](real: T = 0; imag: T = 0): PyComplex
Source   Edit  
func conjugate[T](z: PyTComplex[T]): PyTComplex[T]
complex.conjugate() Source   Edit  
func repr(z: PyTComplex): string
Returns bj/(a+bj)/(a-bj) for complex(a, b)

Example:

assert repr(complex(0.0, 3.0)) == "3j"  # not "(0+3j)", as in Python
assert repr(complex(-0.0, 3.0)) == "(-0+3j)" # not "3j", as in Python

assert repr(complex(1.0, 2.0)) == "(1+2j)"  # '.0' is removed as Python's
Source   Edit  

Templates

template `*`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `*`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `*`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `*`[T](a: SomeInteger; b: PyTComplex[T]): untyped
Source   Edit  
template `*`[T](a: T; b: PyTComplex[T]): untyped
Source   Edit  
template `**`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `**`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `**`[T](a: SomeInteger; b: PyTComplex[T]): untyped
Source   Edit  
template `**`[T](a: T; b: PyTComplex[T]): untyped
Source   Edit  
template `**`[T](self: PyTComplex[T]; x: SomeInteger): PyTComplex[T]
Source   Edit  
template `**`[T](self: PyTComplex[T]; x: static Natural): PyTComplex[T]
Source   Edit  
template `*=`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `*=`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `*=`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `+`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `+`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `+`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `+`[T](a: SomeInteger; b: PyTComplex[T]): untyped
Source   Edit  
template `+`[T](a: T; b: PyTComplex[T]): untyped
Source   Edit  
template `+=`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `+=`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `+=`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `-`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `-`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `-`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `-`[T](a: SomeInteger; b: PyTComplex[T]): untyped
Source   Edit  
template `-`[T](a: T; b: PyTComplex[T]): untyped
Source   Edit  
template `-=`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `-=`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `-=`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `/`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `/`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `/`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `/`[T](a: SomeInteger; b: PyTComplex[T]): untyped
Source   Edit  
template `/`[T](a: T; b: PyTComplex[T]): untyped
Source   Edit  
template `/=`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `/=`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `/=`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `==`[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template `==`[T](a: PyTComplex[T]; b: SomeInteger): untyped
Source   Edit  
template `==`[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template `==`[T](a: SomeInteger; b: PyTComplex[T]): untyped
Source   Edit  
template `==`[T](a: T; b: PyTComplex[T]): untyped
Source   Edit  
template complex(real, imag: HasFloat): PyComplex
Source   Edit  
template complex(real, imag: HasIndex): PyComplex
Admonition: since Python 3.8
Source   Edit  
template imag[T](z: PyTComplex[T]): T
Source   Edit  
template imag=[T](z: PyTComplex[T]; _: T) {.
    error: "AttributeError: readonly attribute".}
Source   Edit  
template j(i: int{lit}): PyComplex

Example:

assert complex(1, 3) == 1+3.j
Source   Edit  
template J(i: int{lit}): PyComplex
the same as j, e.g. 1+3.J Source   Edit  
template pow[T](a: PyTComplex[T]; b: PyTComplex[T]): untyped
Source   Edit  
template pow[T](a: PyTComplex[T]; b: T): untyped
Source   Edit  
template pow[T](a: SomeInteger; b: PyTComplex[T]): untyped
Source   Edit  
template pow[T](a: T; b: PyTComplex[T]): untyped
Source   Edit  
template pow[T](self: ComplexPowSecondParamType[T] | SomeInteger;
                x: PyTComplex[T]; _: NoneType): PyTComplex[T]
Source   Edit  
template pow[T](self: PyTComplex[T];
                x: ComplexPowSecondParamType[T] | SomeInteger; _: NoneType): PyTComplex[
    T]
Source   Edit  
template pow[T](self: PyTComplex[T]; x: SomeInteger): PyTComplex[T]
Source   Edit  
template pow[T](self: PyTComplex[T]; x: static Natural): PyTComplex[T]
Source   Edit  
template pycomplex[T](re: T; im = T(0)): PyTComplex[T]
alias of complex. Useful when import both std/complex and pylib Source   Edit  
template pycomplex[T](z: Complex[T]): PyTComplex[T]
Source   Edit  
template real[T](z: PyTComplex[T]): T
Source   Edit  
template real=[T](z: PyTComplex[T]; _: T) {.
    error: "AttributeError: readonly attribute".}
Source   Edit  
template toNimComplex[T](z: PyTComplex[T]): Complex[T]
Source   Edit