The multiplication maybe is not the best example. Actually, I wanted to say 
"addition".

I wanted that my operator `+` works for:
1) np.ndarray + np.ndarray
2) np.ndarray + LRmatrix
3) LRmatrix + np.ndarray
4) LRmatrix + LRmatrix

1) is a part of Numpy. 3) and 4) can be implemented by `LRmatrix.__add__`. 
However, 3) does not work because `np.ndarray.__add__` has higher priority 
than `LRmatrix.__radd__`.

In this case, it will be awkward to use an `add` function to wrap them.
 

On Saturday, October 8, 2016 at 8:35:19 AM UTC+2, Sisyphuss wrote:
>
> You mean:
> def mydot(A,B):
>     if isinstance(A, np.ndarray) and isinstance(B, np.ndarray):
>         return np.dot(A, B)
>     else:
>         # mycode
> ?
> Maybe you are right. Since I can't overload `np.dot`, maybe the neater way 
> is to write a higher level function and then delegate the work by 
> inspecting the type.
>
>
> On Friday, October 7, 2016 at 7:56:59 PM UTC+2, Gabriel Gellner wrote:
>>
>> Any reason to not just use a function? (like np.dot etc)
>>
>> My understanding is that in python '*' means elementwise multiplication, 
>> so even if you could monkeypatch numpys __mul__ method to do the right 
>> thing wouldn't you be changing the semantics?
>>
>> Gabriel
>>
>> On Friday, October 7, 2016 at 3:51:11 AM UTC-6, Sisyphuss wrote:
>>
>>> In Julia, we can do multiple dispatch for operators, that is the 
>>> interpreter can identify:
>>> float + integer
>>> integer + integer
>>> integer + float
>>> float + float
>>> as well as *user-defined* data structure.
>>>
>>> Recently, I am working on Python (I have no choice because Spark hasn't 
>>> yet a Julia binding). I intended to do the same thing -- multiplication -- 
>>> between a Numpy matrix and self-defined Low-rank matrix. Of course, I 
>>> defined the `__rmul__ ` method for Low-rank matrix. However, it seems to me 
>>> that the Numpy matrix intercepts the `*` operator as its `__mul__` method, 
>>> which expects the argument on the right side of `*` to be a scalar.
>>>
>>> I would like to know if there is anyway around?
>>>
>>>

Reply via email to