Skip to content

Add easier way to create dense vectors and matrices #378

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

Closed
eriknw opened this issue Jan 30, 2023 · 1 comment · Fixed by #382
Closed

Add easier way to create dense vectors and matrices #378

eriknw opened this issue Jan 30, 2023 · 1 comment · Fixed by #382
Labels
feature Something is missing io Data input, output, and conversions

Comments

@eriknw
Copy link
Member

eriknw commented Jan 30, 2023

It may be nice to have constructors to create Vector or Matrix from "dense" a.k.a "full" data. It should be able to handle numpy arrays, list of lists, and scalars. I think scalars are important to create iso-valued objects.

Creating full objects from numpy arrays is more difficult than it ought to be in GraphBLAS. For example, see Matrix._from_dense.

Relatedly, we could add a method to create a diagonal matrix from an array or scalar (for example, to create an identity matrix).

These are all possible to do today, such as:

A = Matrix(a.dtype, nrows=a.shape[0], ncols=a.shape[1])
A[:, :] = a

# vs

A = Matrix.from_foo(a)

and

A = Matrix(nrows=nrows, ncols=ncols)
A << scalar

# vs

A = Matrix.from_foo(scalar, nrows=nrows, ncols=ncols)

and

v = Vector(a.dtype, size=a.size)
v[:] = a
A = v.diag()

# vs

A = Vector.from_foo(a).diag()

# vs

A = Vector.from_coo(np.arange(n), a, size=n).diag()

# vs

A = Matrix.from_coo(np.arange(n), np.arange(n), a, nrows=n, ncols=n)

# vs

A = Matrix.from_bar(a)

(Note that there are suitesparse-specific constructors such as Matrix.ss.import_fullr)

Possible signatures:

def from_foo(values, dtype=None, *, nrows=None, ncols=None, name=None):
    ...  # create full matrix from array, list of lists, or scalar
    # what about rowwise or columnwise? Should we add `order="rowwise"` keyword?

def from_bar(values, dtype=None, *, k=0, nrows=None, ncols=None, name=None):
    ... # create matrix with data on the kth diagonal from array, list, or scalar (or Vector?)

Also related is gb.io.from_numpy, whose behavior we could reconsider.

@eriknw
Copy link
Member Author

eriknw commented Feb 4, 2023

My current opinion: if we add e.g. Vector.from_dense, then I don't think we need a method to create a diagonal matrix from a numpy array. It's easy enough to do:

>>> D = Vector.from_dense(a).diag()
>>> I = Vector.from_dense(1, size=n).diag()

@eriknw eriknw added feature Something is missing io Data input, output, and conversions labels Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Something is missing io Data input, output, and conversions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant