vlang / vtl

The V Tensor Library
https://vlang.github.io/vtl
MIT License
148 stars 21 forks source link

operator overload doesn't work on Tensors #6

Open SleepyRoy opened 3 years ago

SleepyRoy commented 3 years ago

(I guess V's op overload is now stable enough so...)

VTL version: master

OS: win10

What did you do? (Just take plus op as an example)

import vtl

fn main() {
    println(vtl.ones([2,2])+vtl.ones([2,2]))
}

What did you expect to see? a 2*2 matrix with elements 2.0

What did you see instead?

sample.v:4:14: error: undefined operation `vtl.Tensor` + `vtl.Tensor`
    2 |
    3 | fn main() {
    4 |     println(vtl.ones([2,2])+vtl.ones([2,2]))
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    5 | }
cmnemoi commented 2 years ago

Hey, I would like to deal with this issue but I am confused about how to do it.

TL;DR : how to implement an operator overload between .vtl.Tensor<T> (note the . at the beginning) objects ?


I naively tried to implement the + overload like this :

//+ operator overload
[inline]
pub fn (a &Tensor<f64>) + (b &Tensor<f64>) &Tensor<f64>{
    return add(a, b)
}

getting this error on my test :

fn test_plus_operator(){
    a := ones<int>([2,2])
    b := ones<int>([2,2])

    assert a + b == add(a, b)
---- Testing... ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 FAIL   327.520 ms /home/cmnemoi/Documents/Code/V/vtl/math_op_test.v
math_op_test.v:7:9: error: invalid operator `+` to `&.Tensor<int>` and `&.Tensor<int>`
    5 |     b := ones<int>([2,2])
    6 | 
    7 |     assert a + b == add(a, b)
      |            ~~~~~
    8 | }

After some experimentation with the V doc example on operator overloading, I guessed overloading is not supported between object references (is it ?).

Then I implemented the overload on Tensors copies :

[inline]
pub fn (a Tensor<f64>) + (b Tensor<f64>) &Tensor<f64>{
    return add(a, b)
}

and get the following error :

fn test_plus_operator(){
    a := ones<int>([2,2])
    b := ones<int>([2,2])

    assert *a + *b == add(a, b) //dereferencing vtl.Tensors
---- Testing... ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 FAIL   327.520 ms /home/cmnemoi/Documents/Code/V/vtl/math_op_test.v
math_op_test.v:7:9: error: invalid operator `+` to `.Tensor<int>` and `.Tensor<int>`
    5 |     b := ones<int>([2,2])
    6 | 
    7 |     assert *a + *b == add(a, b) //dereferencing vtl.Tensors
      |            ~~~~~
    8 | }

What is a .Tensor ? I couldn't find any reference in V ecosystem.

On another side, when I try to use the + operator outside vtl files I don't deal with .&vtl.Tensor but actual &vtl.Tensor.

I am very confused.

ulises-jeremias commented 2 years ago

@cmnemoi hey! thanks for your contributions! unfortunately I think V is not capable to overload operators on structs that use generics for now :/

.Tensor is nothing 😅 probably an issue in V throwing errors 👀

cmnemoi commented 2 years ago

That makes sense, thanks for the answer.