Issue #14701 has been updated by naitoh (Jun NAITOH).


nobu (Nobuyoshi Nakada) wrote:
> What about
> 
> ```ruby
> na = Numo::Int32[5, 6]
> a = na.inplace
> a += 1
> ```

When saving in a variable, I do not want to manage the inplace state.

```
> require 'numo/narray'
> a = Numo::Int32[5, 6]
 => Numo::Int32#shape=[2]
[5, 6] 
> a.inplace?
 => false 
> a.inplace + 1
 => Numo::Int32(view)#shape=[2]
[6, 7] 
> a.inplace?
 => false 
```
In above, I want to set it to non-inplace state when saved in a variable.


```
> require 'numo/narray'
> na = Numo::Int32[5, 6]
 => Numo::Int32#shape=[2]
[5, 6] 
> na.inplace?
 => false 
> a = na.inplace
 => Numo::Int32(view)#shape=[2]
[5, 6] 
> a.inplace?
 => true
> a.object_id
 => 6495820  
> a += 1
 => Numo::Int32(view)#shape=[2]
[6, 7] 
> a.object_id
 => 6495820 
> a.inplace?
 => true 
> a.out_of_place!
 => Numo::Int32(view)#shape=[2]
[6, 7] 
> a.inplace?
 => false 
```

I was able to do the operation to become the same object.
However, I do not want to set it to non-inplace state each time like this way.

If you let the variable save the inplace state, the following happens.

* not set inplace state case. (Expected result)

```
> a = Numo::Int32[5, 6]
 => Numo::Int32#shape=[2]
[5, 6] 
> a += 100 - (a * 2)
 => Numo::Int32#shape=[2]
[95, 94] 
```

* set inplace state case.

```
> na = Numo::Int32[5, 6]
 => Numo::Int32#shape=[2]
[5, 6] 
> a = na.inplace
 => Numo::Int32(view)#shape=[2]
[5, 6] 
> a += 100 - (a * 2)
 => Numo::Int32(view)#shape=[2]
[180, 176] 
```

This is because variables in the inplace state are rewritten in the middle of calculation as follows.

```
> na = Numo::Int32[5, 6]
 => Numo::Int32#shape=[2]
[5, 6] 
> a = na.inplace
 => Numo::Int32(view)#shape=[2]
[5, 6] 
> (a * 2)
 => Numo::Int32(view)#shape=[2]
[10, 12] 
> a
 => Numo::Int32(view)#shape=[2]
[10, 12] 
```

----------------------------------------
Feature #14701: If the object is not frozen, I want to be able to redefine the compound assignment operator.
https://bugs.ruby-lang.org/issues/14701#change-71700

* Author: naitoh (Jun NAITOH)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
If the object is not frozen, I want to be able to redefine the compound assignment operator (e.g. +=, -=, *=, /=, ..etc ).

https://docs.ruby-lang.org/ja/latest/doc/spec=2foperator.html

* Redefinable operator (method) 

~~~
    |  ^  &  <=>  ==  ===  =~  >   >=  <   <=   <<  >>
    +  -  *  /    %   **   ~   +@  -@  []  []=  ` ! != !~
~~~

* use case

~~~
> require 'numo/narray'
> a = Numo::Int32[5, 6]
=> Numo::Int32#shape=[2]
[5, 6]
> a.object_id
=> 70326927544920
> a += 1
=> Numo::Int32#shape=[2]
[6, 7]
> a.object_id
=> 70326927530540
> a.inplace + 1
=> Numo::Int32(view)#shape=[2]
[7, 8]
> a.object_id
=> 70326927530540
~~~

With Numo::NArray, using "inplace" instead of "+=" will update the same object so it will be faster.

I want to write "a += 1" instead of "a.inplace + 1".
However, Ruby can not redefine "+=".



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>