Skip to content

Add easier way to create dense vectors and matrices #378

Closed

Description

@eriknw

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.

Activity

  1. eriknw commented on Feb 4, 2023

    @eriknw
    MemberAuthor

    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()
  2. added
    featureSomething is missing
    ioData input, output, and conversions
    on Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureSomething is missingioData input, output, and conversions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions