-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why is there no Vector interface? #100
Comments
Having a separate Vector implementation that implements List would be useful. If you are interested in developing a prototype, a PR would be welcome. One thing to consider when implementing it is how to handle column vector vs row vector. For example, in linear algebra MATLABOctave (MATLAB) forces everything into a 2-dimensional container by default, so 1xN vs Nx1 is explicit. However, using
For the ND case, it doesn't allow linalg operations: > a=zeros(3,3,3)
> a'
error: transpose not defined for N-D object NumpyNumpy allows 1-dimensional tensors, and then makes arbitrary choices on what to do in linear algebra contexts, sometimes treating 1D vectors as either row or column vectors, sometimes producing errors. >>> a=array([1,2,3])
>>> a
array([1, 2, 3])
>>> shape(a) # 1 dimension, no orientation
(3,)
>>> a.T # Transpose does nothing
array([1, 2, 3])
>>> a @ a # Inner product (first `a` is treated as row vec, second `a` treated as col vec)
14
>>> a.T @ a # Inner product still (transpose does nothing)
14
>>> a @ a.T # Inner product still (transpose does nothing)
14
>>> b = array([[1, 2, 3]]) # Explicit 2D container
>>> b.T # Now transpose works
array([[1],
[2],
[3]])
>>> b @ b.T # Expected inner product
array([[14]])
>>> b.T @ b # Expected outer product
array([[1, 2, 3],
[2, 4, 6],
[3, 6, 9]])
>>> b @ a # Inner product (Random choice of treating 1D vector `a` as column vector...)
array([14])
>>> a @ b # Bizarre error: why not outer product?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 3)
>>> reshape(a, (3,1)) @ b # Force a cast on `a` to 2D to get outer product instead of error
array([[1, 2, 3],
[2, 4, 6],
[3, 6, 9]]) JuliaJulia defaults to 2D vectors like MATLAB for literals, but can still handle 1D vectors in linear algebra by marking vectors as col or row (via Adjoint type). julia> a=[1 2 3] # Row vector (Note the two-dimensional shape)
1×3 Array{Int64,2}:
1 2 3
julia> a' # Transpose works for 2D arrays
3×1 LinearAlgebra.Adjoint{Int64,Array{Int64,2}}:
1
2
3
julia> a*a' # Inner product
1×1 Array{Int64,2}:
14
julia> a'*a # Outer product
3×3 Array{Int64,2}:
1 2 3
2 4 6
3 6 9
julia> b=zeros(3) # Force a 1 dimensional vector, defaults to col vector
3-element Array{Float64,1}:
0.0
0.0
0.0
julia> b' # 1D vector transposed gets marked with Adjoint and works, produces a row vector
1×3 LinearAlgebra.Adjoint{Float64,Array{Float64,1}}:
0.0 0.0 0.0
julia> b*b' # Expected outer product
3×3 Array{Float64,2}:
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
julia> b'*b # Expected inner product
0.0 KomaKoma right now has a 2D container (Matrix) as well as an N dimensional container (NDArray). To get a 1D container you'd have to use NDArray and specify N=1, which would lose access to linalg operations. This is similar to MATLAB's approach. You'd have to cast the NDArray to a matrix (and force 2D) in order to get back to linalg. Possible enhancementsThere's a few ways we could go here, and in all cases implementing
Any other proposals would also be welcome. |
Could you comment a little more on what problems you're running into? For example, is it really necessary that Having the API cleanly distinguish between inner and outer products is also an important question. But I think it's a different question from what you were asking about? |
I would like to ask what motivated your choice to cover both vectors and matrices by the unified Matrix interface. Are there some underlying technical difficulties? Did you consider adding Vector in the past and dismissed the idea?
Right now I am converting a lot between List and matrices Matrix about which I know that they are vector-shaped. It would make the code cleaner and more performant, if there was a Vector interface which would extend List.
If this is not on your agenda, I would like to do this in my code as a private extension and would welcome any advice that you have.
Thank you!
The text was updated successfully, but these errors were encountered: