-
I have a matrix (vector of vectors). What is the equivalent expression in basilisp for matrix[a:b, c:d]? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @dpom, I assume you are referring to slicing a matrix in a library like In Basilisp, You will need to use the raw indexing equivalent using For example: > basilisp repl
basilisp.user=> (import [numpy :as np])
nil
basilisp.user=> (def matrix (np/array [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]))
#'basilisp.user/matrix
basilisp.user=> (aget matrix #py ((slice 1 3) (slice 1 3)))
array([[5, 6],
[8, 9]]) You could simplify repeated slicing with a custom macro as in the example below, that you could adjust as needed for your specific use cases: basilisp.user=> (defmacro mget [m r1 r2 c1 c2]
`(aget ~m (python/tuple [(python/slice ~r1 ~r2) (python/slice ~c1 ~c2)])))
#'basilisp.user/mget
basilisp.user=> (mget matrix 1 3 1 3)
array([[5, 6],
[8, 9]]) related: #1094 I hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Thank you for your prompt and complete reply. |
Beta Was this translation helpful? Give feedback.
-
You are welcome! All credit goes to @chrisrink10 for making Basilisp happen 🙌 |
Beta Was this translation helpful? Give feedback.
Hi @dpom,
I assume you are referring to slicing a matrix in a library like
numpy
orpandas
.In Basilisp, You will need to use the raw indexing equivalent using
slice
s withaget
for thisFor example:
You could simplify repeated slicing with a custom macro as in the example below, that you could adjust as needed for your specific use cases: