Skip to content
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

Add API functions for setting rows / columns / sub-matrices #39

Open
mikera opened this issue Apr 20, 2013 · 10 comments
Open

Add API functions for setting rows / columns / sub-matrices #39

mikera opened this issue Apr 20, 2013 · 10 comments

Comments

@mikera
Copy link
Owner

mikera commented Apr 20, 2013

Ideas:

  • (set-row m i v)
  • (set-column m i j)
  • (set-submatrix-at m i j small-matrix)
@kasterma
Copy link

If I understand the suggestion correctly, I would expand and suggest to add the elementary row operations. (switching, multiplication, and row addition; see http://en.wikipedia.org/wiki/Elementary_matrix).

Not sure what the semantics of the last function (set-submatrix-at) is supposed to be.

@mikera
Copy link
Owner Author

mikera commented Apr 20, 2013

@kasterma: good suggestion, though I think that is worth a separate issue. I'll create one.

@mikera
Copy link
Owner Author

mikera commented Apr 20, 2013

#40

@si14
Copy link
Collaborator

si14 commented Jun 14, 2013

If I'm right it can be done using mutable "views" into original array (or at least can pretend to be done this way if underlying matrix representation doesn't support mutation).

@mikera
Copy link
Owner Author

mikera commented Jun 15, 2013

Well we probably ultimately want two versions (immutable and mutable) of each: set-row and set-row! for example.

The mutable versions could certainly be implemented with an mutable view.

The immutable version would need to construct a new matrix. However structural sharing is possible, so parts of the new matrix could be shared with the original matrix - possibly via a view.

@alexott
Copy link

alexott commented Jun 15, 2013

I've also thought already about implementing 2 APIs for Incanter - mutable & immutable.

@mikera
Copy link
Owner Author

mikera commented Jun 15, 2013

We'll get mutable + immutable versions of most operations for free if we complete the Incanter / core.matrix integration.

Most core.matrix functions already have immutable and mutable versions where it makes sense, e.g. add and add!, where the ! versions generally mutate their first argument.

@si14
Copy link
Collaborator

si14 commented Jun 16, 2013

I'm sorry for not being clear enough.

I meant something like this MATLAB/Octave code:

octave:1> a = eye(3)
a =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1

octave:2> a(2,1:2) = [2,2]
a =

   1   0   0
   2   2   0
   0   0   1

The thing here is that we already need a powerful tool to take slices of array. Why don't reuse it to assign values as well, as it is done in MATLAB/Octave? From the point of API, I can imagine "slicing schemes" as first-class objects:

(def s (slicing-scheme [2 1 :to 2]))
(sub (eye 3) s) ; returns a slice
(set (eye 3) s) ; returns new matrix
(set! (eye 3) s) ; mutates in place

DSL for slicing is another issue, of course.

@mikera
Copy link
Owner Author

mikera commented Jun 16, 2013

slicing schemes as first class objects is an interesting idea, I'd be interested to see an implementation that demonstrates that it can be done efficiently and in a general way.

But do note that core.matrix already includes an NDWrapper type that can wrap an arbitrary subset of a matrix. See clojure.core.matrix.impl.wrappers namespace and the submatrix function.

Use example:

(def A (array :ndarray [[1 2 3] [4 5 6] [7 8 9]]))
(def S (submatrix A [[1 1] [0 2]]))
S
=> #<NDWrapper [[4 5]]>

Currently NDWrapper doesn't support mutation, but it could easily be upgraded to do so. In which case, we would be able to do stuff that alters the original matrix like:

(assign! S 0)
S
=> #<NDWrapper [[0 0]]>
A
=> #<NDArray [[1 2 3] [0 0 6] [7 8 9]]>

Overall the NDWrapper is quite flexible and good for simple use cases. The problem is that it adds quite a lot of index lookup overhead for each element (Dmitry: we wouldn't need your shiny new NDArray if the NDWrapper was ultra-fast!)

@mikera
Copy link
Owner Author

mikera commented Oct 28, 2013

Row setting implemented in this commit:
95993ea

Still not sure of how to handle the more general strategy....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants