Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    SeriousFun01
    @SeriousFun01
    its a statistical package for estimating and working with certain types of matrices. offering this type of syntax helps people migrate from matlab environments. if overloading power and multiplication can be implemented easily and is performant maybe just a well placed documentation would help people along
    Thomas Aarholt
    @thomasaarholt
    Ok, then let me write an example now in the next few min and see what you think (I am a bit busy right now so coming to and fro computer)
    Thomas Aarholt
    @thomasaarholt
    I read here how to subclass a simple example, and then used that to overload the multiplication operator:
    import numpy as np
    
    class Matrix(np.ndarray):
        def __new__(cls, input_array):
            return np.asarray(input_array).view(cls)
    
        def __array_finalize__(self, obj) -> None:
            if obj is None: return
            # This attribute should be maintained!
            default_attributes = {"attr": 1}
            self.__dict__.update(default_attributes)  # another way to set attributes
    
        def __mul__(self, B):
            return self.__class__(self.__array__() @ B.__array__())
    The last entry, __mul__ describes how the class should handle the * operator with another Matrix B
    Then I created two normal numpy arrays that we would like to matrix multiply, and turned them into Matrix objects
    A = np.arange(9).reshape((3,3))
    B = A + 10
    
    AM = Matrix(A)
    BM = Matrix(B)
    They look like:
    >>> AM
    Matrix([[0, 1, 2],
            [3, 4, 5],
            [6, 7, 8]])
    >>> BM
    Matrix([[10, 11, 12],
            [13, 14, 15],
            [16, 17, 18]])
    And then we can just multiply them to do matrix multiplication:
    >>> AM * BM
    Matrix([[ 45,  48,  51],
           [162, 174, 186],
           [279, 300, 321]])
    And we can check that they do the same thing as numpy matrix multiplication:
    >>> A @ B
    array([[ 45,  48,  51],
           [162, 174, 186],
           [279, 300, 321]])
    Thomas Aarholt
    @thomasaarholt
    (I edited my code a tad just now - I wrapped the expression in __mul__(self, B) in self.__class(expression) so that the multiplication operator returns a Matrix object, not a numpy array)
    SeriousFun01
    @SeriousFun01
    I guess a similar pattern for overloading __pow__. Implementing a recursive multiply for the number of powers
    Thomas Aarholt
    @thomasaarholt
    Yep, but I'm leaving that as an exercise to the reader :)
    SeriousFun01
    @SeriousFun01
    ha ha, cool, many thanks. in any case I need to check what else is implemented in np.matrix in this respect (these two operators are from memory).
    Thomas Aarholt
    @thomasaarholt
    Certainly!
    water5
    @water5
    Numpy is it has built-in method generate new array -> [a[i] - a[i - x]] via parameter x, like:
    a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
    np.xxx(a, 3) to generate
    np.array (a[3] - a[0], a[4] - a[1], a[5] - a[2], a[6] - a[3], a[7] -a[4], a[8] - a[5])
    np.xxx(a, 4) to generate
    np.array (a[4] - a[0], a[5] - a[1], a[6] - a[2], a[7] - a[3], a[8] -a[4])
    heitzmann
    @heitzmann:matrix.org
    [m]
    Not that I know of, but you can easily write: a[x:] - a[:-x]
    Thomas Robitaille
    @astrofrog
    Hi all, I am interested in looping over 1-d slices of a 2-d array in a C extension, so e.g. array[:, i], but I can't figure out how to do this using the various npyiter functions - which one is the right one to use for this case?
    SeriousFun01
    @SeriousFun01

    Yep, but I'm leaving that as an exercise to the reader :)

    It dawned on me that if I am to subclass ndarray to create a matrix I might as well just copy the complete example in numpy.matrix (the code / API that is being deprecated :-)

    Iwan Aucamp
    @aucampia
    so I have a 3d Grid, and a path, and I want to determine what grid cells the path passes through
    SeriousFun01
    @SeriousFun01
    An iterative procedure might be the most flexible: given point N in the path is in cell x,y,z, find N+1 by checking first neighbors etc. A cookbook in computational geometry would come handy :-)
    DJMO3
    @DJMO3
    Hello everyone, I have an issue that is persisting since some days now. Using PyCharm with conda environment. The interpreter cannot / does not import the numpy library or any numpy-dependent library. My project is completely paused and I can't seem to find a fix yet.
    I've read about uninstalling and reinstalling but I'm not too inclined to do that because I have so many libraries that depend on numpy right now and it would take a whole day to set up my environment again.
    Is there a known easy fix to this? Thanks a lot in advance!
    Dave Hirschfeld
    @dhirschfeld
    What was the error message?
    Are you on the latest PyCharm?
    PyCharm has had bugs where the conda environment isn't properly activated
    One way to fix that is to activate the environment in the terminal then run PyCharm from the terminal with the activated environment
    DJMO3
    @DJMO3
    I didn't try that
    The error message was "failed to load / import dll.. "
    I'm sorry I'm not in front of my laptop right now but I saw that numerous others have got this problem as well, so I thought that it's something common and I didn't need to describe too much
    I am on the latest PyCharm. But I will surely try launching from the environment itself to see how that works.
    david.rysk
    @david.rysk:matrix.org
    [m]
    Hey — is anyone testing on Apple M1? I'm getting segfaults when running the tests, specifically on test_crackfortran.py.
    Ben Watkins
    @benwtks

    Hi, this might be a stupid question but

    I've got a with shape (313596, 3)

    a = array([[0, 1, 0],
           [0, 1, 0],
           [0, 1, 0],
           ...,
           [1, 1, 0],
           [1, 1, 0],
           [1, 1, 0]])

    and then b with shape (313596, 25, 25, 3)

    b = array([[[[0.98823529, 0.98823529, 0.98823529],
             [0.98431373, 0.98431373, 0.98431373],
             [0.99215686, 0.99215686, 0.99215686],
             ...,

    I want to do [a[i] * b[i] for i in range(313596)]but with numpy so that the end result is (313596, 25, 25, 3)

    I'm not sure how I can do this, I tried using dot and multiply but they didn't work, I somehow need to do do something like a dot product treating each of these sub (25,25,3) matrices as elements and then each multiplication in the dot product is a hadamard

    np.dot(np.transpose(a), b) #-> shapes (3,313596) and (313596,25,25,3) not aligned: 313596 (dim 1) != 25 (dim 2)
    np.matmul

    Please can somebody help me out here, I've seen tensordot and einsum, not sure if they're my answer... I'm getting a bit frustrated trying to figure this out, maybe somebody could point me in the right direction?

    Ben Watkins
    @benwtks
    In other words, is there a more efficient way to do np.vstack([a[i] * b[i] for i in range(313596)])?
    heitzmann
    @heitzmann:matrix.org
    [m]
    einsum is your answer
    Dave Hirschfeld
    @dhirschfeld
    In [28]: a = np.random.randn(313, 3)
    
    In [29]: b = np.random.randn(313, 25, 25, 3)
    
    In [30]: expected = np.stack([a[i] * b[i] for i in range(313)])
    
    In [31]: expected.shape
    Out[31]: (313, 25, 25, 3)
    
    In [32]: actual = a[:, None, None, :] * b
    
    In [33]: np.all(actual == expected)
    Out[33]: True
    
    In [34]: %timeit expected = np.stack([a[i] * b[i] for i in range(313)])
    10.1 ms ± 325 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    
    In [35]: %timeit actual = a[:, None, None, :] * b
    4.44 ms ± 182 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    Pierre-Andre Noel
    @PierreAndreNoel
    Hello! Could someone confirm if this is a bug? I'm trying to edit in place (no copy, "by reference") a sliced array that was passed as a function argument. It works for slice indexing, but not for boolean indexing.
    import numpy as np
    
    def test__without_fun_call__bool_indexing():
        a = np.array([[1., 2.], [3., 4.]])
        lines_to_zero = np.array([True, False])
        a[lines_to_zero, :] = 0.
        assert np.allclose(a, [[0., 0.], [3., 4.]])  # OK
    
    def set_to_zero(x):
        x[...] = 0.
    
    def test__with_fun_call__bool_indexing():
        a = np.array([[1., 2.], [3., 4.]])
        lines_to_zero = np.array([True, False])
        set_to_zero(a[lines_to_zero, :])
        assert np.allclose(a, [[0., 0.], [3., 4.]])  # FAIL
    
    def test__with_fun_call__slice_indexing():
        a = np.array([[1., 2.], [3., 4.]])
        set_to_zero(a[:1, :])
        assert np.allclose(a, [[0., 0.], [3., 4.]])  # OK
    1 reply
    Francesco
    @cescofran_gitlab
    Where I can find the code for np.dot .... the far I can go is here https://github.com/numpy/numpy/blob/872373b030eb6c181ab7f29c0928ccd8b0f74528/numpy/core/multiarray.py#L732 ... from here how can I find the c source file that correspond? ....either pointing out the file that correspond or explain how @array_function_from_c_func_and_dispatcher(_multiarray_umath.dot) mechanism work would be appreciated...
    Matti Picus
    @mattip

    but not for boolean indexing

    Correct. Boolean indexing in general cannot return a view. A view implies consistent strides, and boolean indexing breaks that. You may be interested in masked arrays.

    Matti Picus
    @mattip
    np.dot -> np.dot.__wrapped__ -> <builtin function dot> -> _multiarray_umath array_module_methods -> array_matrix_product -> PyArray_MatrixProduct2. From there it depends on the shape of the arguments.
    also note @ is np.matmul, not np.dot. They have different semantics for ndim>2
    Francesco
    @cescofran_gitlab
    @mattip Thanks you very much!