one

one(x)

Get the multiplicative identity element for the type of x (x can also specify the type itself). For matrices, returns an identity matrix of the appropriate size and type.

Examples

julia> one(10)
1
julia> foo = zeros(Int8, (2,2));
julia> one(foo)
2x2 Array{Int8,2}:
 1  0
 0  1
julia> complexNumber = complex(1, 1);   # 1 + 1im
julia> one(complexNumber)
1 + 0im
  1. Get the multiplicative identity element for a numeric type:

    julia> one(3)
    1
    
    julia> one(3.5)
    1.0

    In this example, one returns the multiplicative identity element for the given numeric values.

  2. Get the multiplicative identity element for a custom type:

    julia> struct MyType end
    
    julia> one(MyType())
    MyType()

    Here, one returns the multiplicative identity element for a custom type MyType.

  3. Get the identity matrix for a matrix type:

    julia> A = [1 2; 3 4];
    
    julia> one(A)
    2×2 Matrix{Int64}:
    1  0
    0  1

    When given a matrix A, one returns an identity matrix of the appropriate size and type.

Common mistake example:

julia> one("abc")
ERROR: MethodError: no method matching one(::String)

In this example, one is called with a string argument. However, one is not defined for strings, so it raises a MethodError. It's important to note that one is typically used with numeric types or matrices.

See Also

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: