I'm trying to construct the identity matrix in Julia 1.1
. After looking at the documentation I found that I could compute a 4x4 Identity matrix as follows:
julia> Id4 =1* Matrix(I, 4, 4)
4×4 Array{Int64,2}:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Is this the most julianic way of coding it or is there a better/shorter way, as it is an often used matrix?
Julia provides a very simple notation to create matrices. A matrix can be created using the following notation: A = [1 2 3; 4 5 6]. Spaces separate entries in a row and semicolons separate rows. We can also get the size of a matrix using size(A).
Standard Functions. Linear algebra functions in Julia are largely implemented by calling functions from LAPACK.
An identity matrix is used to find the inverse of a matrix. Also, an identity matrix is used to verify whether any two given matrices are inverses of each other. An identity matrix is used to find the eigenvalues and eigenvectors.
To write the ⋅ symbol, type \cdot followed by the TAB key. If the first argument is complex, it is conjugated. Now, dot() and ⋅ also work for matrices. before you use the dot product function or operator.
Given using LinearAlgebra
, the most julianic way of expressing the identity matrix is:
I
This answer may seem trite, but it is also kind of profound. The whole point of the operator I
is that in the vast majority of cases where users want an identity matrix, it is not necessary to actually instantiate that matrix.
Let's say you want a 1000x1000
identity matrix. Why waste time building the entire matrix, when you could just use I
, noting that sizeof(I)
evaluates to 1
(ie the size of the object is 1 byte). All functions in base Julia (including LinearAlgebra
) understand what I
is, and can use it appropriately without having to waste time building the actual matrix it represents first.
Now, it may be the case that for some reason you need to specify the type of the elements of your identity matrix. Note:
julia> I UniformScaling{Bool} true*I
so in this case, you are using a notional identity matrix with a diagonal of true
and off-diagonal of false
. This is sufficient in many cases, even if your other matrices are Int
or Float64
. Internally, Julia will use methods that specialize on the types. However, if you want to specify your identity matrix to contain integers or floats, use:
julia> 1I UniformScaling{Int64} 1*I julia> 1.0I UniformScaling{Float64} 1.0*I
Note that sizeof(1I)
evaluates to 8
, indicating the notional Int64
types of the members of that matrix.
Also note that you can use e.g. 5I
if you want a notional matrix with 5
on the diagonal and 0
elsewhere.
In some cases (and these cases are much rarer than many might think), you may need to actually build the matrix. In this case, you can use e.g.:
Matrix(1I, 3, 3) # Identity matrix of Int type Matrix(1.0I, 3, 3) # Identity matrix of Float64 type Matrix(I, 3, 3) # Identity matrix of Bool type
Bogumił has also pointed out in the comments that if you are uncomfortable with implying the type of the output in the first argument of the constructors above, you can also use the (slightly more verbose):
Matrix{Int}(I, 3, 3) # Identity matrix of Int type Matrix{Float64}(I, 3, 3) # Identity matrix of Float64 type Matrix{Bool}(I, 3, 3) # Identity matrix of Bool type
and specify the type explicitly.
But really, the only times you would probably need to do this are as follows:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With