1

I have a matrix

 m = np.zeros((5,5))

and what to have a view on the last row

row = m[-1]

however if I add a column to m:

m = np.c_[m[:,:2],[0,0,1,1,1],m[:,2:]]

and print out row I don't get the new column.

Is there any way to get the change without use the line

row = m[-1]

again?

Wikunia
  • 1,564
  • 1
  • 16
  • 37
  • You want to modify your previous array in an in-place manner and that's not possible. Every time you change your array you'll end up creating a new object. – Mazdak Aug 20 '17 at 07:10
  • The question should be: "Why do you need to do that?" it makes no sense to me – mforpe Aug 20 '17 at 07:35
  • I think maybe the problem lies in the method `np.c_`, which is different from ordinary operation on view of Numpy array, which can achieve in-place modification. – Guohua Cheng Aug 20 '17 at 07:46
  • @ManelFornos I have a simplex tableau in m and would like to get the objective row (last row) sometimes I need to add columns to the matrix. – Wikunia Aug 20 '17 at 07:47
  • you want to get last row without running this `row = m[-1]` each time? – Mohamed Ali JAMAOUI Aug 20 '17 at 07:55
  • @MedAli I want that row updates itself whenever there is a change of m in anyway but as Kasramvd said: It has to be inline for that – Wikunia Aug 20 '17 at 07:57
  • `c_` is a `concatenate` function. It makes a new array with copies of data from `m`. The fact that you assign it to the variable `m` does not create any connection to `row` or other view of the original. Updating the reference to the last row of `m` is the responsibility of your code. It's not a time expensive operation. – hpaulj Aug 20 '17 at 09:34

1 Answers1

1

What you want to achieve here isn't currently possible within the bounds of the numpy library. See this answer on numpy arrays occupying a contiguous block of memory.

You can use the rx library to get around this by making a subject of the last row.

import numpy as np
from rx import Observable, Observer
from rx.subjects import Subject

m = np.zeros((5,5))

m_stream = Subject()
last_row_stream = Subject()
last_row = None

def update_last(v):
    global last_row
    last_row = v

last_row_stream.subscribe(on_next=update_last)
last_row_stream.on_next(m[-1])

print(last_row)

m_stream.map(
    lambda v: np.insert(v, 2, [0, 0, 1, 1, 1], axis=1)
).subscribe(lambda v: last_row_stream.on_next(v[-1]))

m_stream.on_next(m)


print(last_row)
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • I thought of an observer pattern as well. I would like to observe on every change made so whenever `m` changed I would like to have `row` changed. Doesn't matter whether it really changed row or not. Therefore I would like to not change the `insert` or `c_` row. Would like to have a subscribe to all changes of `m`. – Wikunia Aug 20 '17 at 08:23
  • 1
    `m_stream` is an observable of `m`. Any operations you want to carry out on `m` can be mapped on `m_stream` for a start. You can also dispose of observer(s) whenever you deem it fit. `...subscribe(lambda v: last_row_stream.on_next(v[-1]))` sets a observer on `m_stream` that updates `last_row_stream`. You can dispose of this and set your very own observer. – Oluwafemi Sule Aug 20 '17 at 08:30