Skip to content

How to express matrix slicing in basilisp? #1200

Answered by ikappaki
dpom asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @dpom,

I assume you are referring to slicing a matrix in a library like numpy or pandas.

In Basilisp, You will need to use the raw indexing equivalent using slices with aget for this

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

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by chrisrink10
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1199 on January 02, 2025 15:52.