complex

complex(r, [i])

Convert real numbers or arrays to complex. i defaults to zero.

Examples

  1. Create a complex number from real values:

    julia> complex(2, 3)
    2 + 3im

    This example creates a complex number with a real part of 2 and an imaginary part of 3.

  2. Create a complex number with only a real part:

    julia> complex(4)
    4 + 0im

    When only one argument is provided, the function assumes the imaginary part is zero.

  3. Convert an array of real numbers to an array of complex numbers:
    julia> real_nums = [1, 2, 3, 4];
    julia> complex(real_nums)
    4-element Array{Complex{Int64},1}:
    1 + 0im
    2 + 0im
    3 + 0im
    4 + 0im

    The function can also convert an array of real numbers to an array of complex numbers.

Common mistake example:

julia> complex([1, 2, 3], [4, 5])
ERROR: MethodError: no method matching complex(::Array{Int64,1}, ::Array{Int64,1})

In this example, the complex function expects the second argument to be optional or a single value. Providing an array as the second argument will result in a MethodError. Make sure to provide a single value or omit the second argument when using complex.

See Also

base, big, bytes2hex, cconvert, complex, convert, dec, hex, hex2bytes, hex2num, oct, oftype, parse, promote, signed, unsafe_convert, unsigned, widen,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: