src/pycomplex

Search:
Group by:
Source   Edit  

builtins.complex and its operators/methods.

Use toNimComplex and pycomplex to convert between PyComplex and Complex

Example:

import src/pycomplex
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 `+`[T](z: PyTComplex[T]): PyTComplex[T]
Source   Edit  
func `-`[T](z: PyTComplex[T]): PyTComplex[T]
Source   Edit  
func abs[T](z: PyTComplex[T]): T
builtins.abs for complex Source   Edit  
func complex(s: openArray[char]): 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 complex[T](n: Complex[T]): PyTComplex[T]
Source   Edit  
func conjugate[T](z: PyTComplex[T]): PyTComplex[T]
Source   Edit  
proc formatValue(res: var string; z: PyTComplex; spec: string)

format complex with given spec, e.g. format(complex(1, 2), ".2f") -> "1.00+2.00j"

Note that Python's format does not support e/E for complex, but Nim's does. So we support it as well.

Source   Edit  
proc hash[T](z: PyTComplex[T]): Hash
Source   Edit  
func parsePyComplex(s: openArray[char]): PyComplex {....raises: [ValueError],
    tags: [], forbids: [].}
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
Source   Edit  
template complex_hash_impl[T](z: T; hash_float_proc): Hash

inner. export for implementing hash of complex hash of complex, same as Python's

Note that Python's hash of complex is not the same as Nim's hash of Complex. So we implement it ourselves.

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]): PyTComplex[T]
Source   Edit  
template pow[T](self: PyTComplex[T];
                x: ComplexPowSecondParamType[T] | SomeInteger): 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