Julia Community 🟣

Vinod V
Vinod V

Posted on

Differences between Julia vector and Python/NumPy vector

Let us try to understand a Julia vector [commented code]

julia> using LinearAlgebra             
julia> x = [1,2,3] #x is a Julia vector                                                                         
3-element Vector{Int64}:                                                                    
 1                                                                                          
 2                                                                                          
 3                                                                                          
julia> size(x)                                                                              
(3,)
julia> A = rand(3,3)                                                                        
3Γ—3 Matrix{Float64}:                                                                        
 0.46205    0.907979  0.57372                                                               
 0.0121377  0.590446  0.376092                                                              
 0.672652   0.909502  0.304637
julia> A*x       #x acts as a column vecotor                                                                           
3-element Vector{Float64}:                                                                  
 3.999167421183575                                                                          
 2.3213037773354586                                                                         
 3.4055688127482697
julia> dot(A[1,:],x)    #First element of A*x is the dot product between first row of A and x                                                                    
3.999167421183575 
julia> x*A #Julia thinks x is a matrix with dimension (3,1)                                                                                 
ERROR: DimensionMismatch: matrix A has dimensions (3,1), matrix B has dimensions (3,3)      
Enter fullscreen mode Exit fullscreen mode

Let us try to make out the behaviour of a vector in python/numpy

>>> import numpy as np
>>> x = np.array([1,2,3])
>>> x.shape
(3,)
#x is an orientationless array 
>>> A = np.random.random((3,3)) #A is a 3 x 3 matrix
>>> A
array([[0.62256804, 0.48535832, 0.86000692],
       [0.24275571, 0.22393731, 0.5531013 ],
       [0.54999826, 0.90498169, 0.35720945]])
>>> A@x #Left mulitply x with matrix A
array([4.17330543, 2.34993421, 3.43158999]) #x acts as a column vector
>>> np.dot(A[0,:],x) #First element of A@x is the dot product between first row of A and x
4.173305432498873
>>> x@A
array([2.75807425, 3.64817801, 3.03783785])
>>> np.dot(A[:,0],x)
2.7580742486325107 #First element of x@A is the dot product between first column of A and x
>>> 
Enter fullscreen mode Exit fullscreen mode

Julia seems to be very particular about the orientation of julia vector while defining.
But Python treats a vector as both column and row vector.

A row vector in Julia can be made as

x = [1 2 3] #without commas in between elements
1Γ—3 Matrix{Int64}:
 1  2  3
Enter fullscreen mode Exit fullscreen mode

A row vector in Python can be made as

x = np.array([[1, 2, 3]])
Enter fullscreen mode Exit fullscreen mode

A column vector in Python can be made as

x = np.array([[1], [2], [3]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)